coding style fixes, copyright waiver (#395)
Co-authored-by: Paulo Marques <pm@quant-insight.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2021-2023 pmarques-dev @ github
|
* Copyright (c) 2023 Raspberry Pi (Trading) Ltd.
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-3-Clause
|
* SPDX-License-Identifier: BSD-3-Clause
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
;
|
;
|
||||||
; Copyright (c) 2021-2023 pmarques-dev @ github
|
; Copyright (c) 2023 Raspberry Pi (Trading) Ltd.
|
||||||
;
|
;
|
||||||
; SPDX-License-Identifier: BSD-3-Clause
|
; SPDX-License-Identifier: BSD-3-Clause
|
||||||
;
|
;
|
||||||
@@ -26,63 +26,63 @@
|
|||||||
; to sysclk / 10 (e.g., sysclk 125MHz, max step rate = 12.5 Msteps/sec)
|
; to sysclk / 10 (e.g., sysclk 125MHz, max step rate = 12.5 Msteps/sec)
|
||||||
|
|
||||||
; 00 state
|
; 00 state
|
||||||
JMP update ; read 00
|
JMP update ; read 00
|
||||||
JMP decrement ; read 01
|
JMP decrement ; read 01
|
||||||
JMP increment ; read 10
|
JMP increment ; read 10
|
||||||
JMP update ; read 11
|
JMP update ; read 11
|
||||||
|
|
||||||
; 01 state
|
; 01 state
|
||||||
JMP increment ; read 00
|
JMP increment ; read 00
|
||||||
JMP update ; read 01
|
JMP update ; read 01
|
||||||
JMP update ; read 10
|
JMP update ; read 10
|
||||||
JMP decrement ; read 11
|
JMP decrement ; read 11
|
||||||
|
|
||||||
; 10 state
|
; 10 state
|
||||||
JMP decrement ; read 00
|
JMP decrement ; read 00
|
||||||
JMP update ; read 01
|
JMP update ; read 01
|
||||||
JMP update ; read 10
|
JMP update ; read 10
|
||||||
JMP increment ; read 11
|
JMP increment ; read 11
|
||||||
|
|
||||||
; to reduce code size, the last 2 states are implemented in place and become the
|
; to reduce code size, the last 2 states are implemented in place and become the
|
||||||
; target for the other jumps
|
; target for the other jumps
|
||||||
|
|
||||||
; 11 state
|
; 11 state
|
||||||
JMP update ; read 00
|
JMP update ; read 00
|
||||||
JMP increment ; read 01
|
JMP increment ; read 01
|
||||||
decrement:
|
decrement:
|
||||||
; note: the target of this instruction must be the next address, so that
|
; note: the target of this instruction must be the next address, so that
|
||||||
; the effect of the instruction does not depend on the value of Y. The
|
; the effect of the instruction does not depend on the value of Y. The
|
||||||
; same is true for the "JMP X--" below. Basically "JMP Y--, <next addr>"
|
; same is true for the "JMP X--" below. Basically "JMP Y--, <next addr>"
|
||||||
; is just a pure "decrement Y" instruction, with no other side effects
|
; is just a pure "decrement Y" instruction, with no other side effects
|
||||||
JMP Y--, update ; read 10
|
JMP Y--, update ; read 10
|
||||||
|
|
||||||
; this is where the main loop starts
|
; this is where the main loop starts
|
||||||
.wrap_target
|
.wrap_target
|
||||||
update:
|
update:
|
||||||
MOV ISR, Y
|
MOV ISR, Y ; read 11
|
||||||
PUSH noblock
|
PUSH noblock
|
||||||
|
|
||||||
sample_pins:
|
sample_pins:
|
||||||
; we shift into ISR the last state of the 2 input pins (now in OSR) and
|
; we shift into ISR the last state of the 2 input pins (now in OSR) and
|
||||||
; the new state of the 2 pins, thus producing the 4 bit target for the
|
; the new state of the 2 pins, thus producing the 4 bit target for the
|
||||||
; computed jump into the correct action for this state. Both the PUSH
|
; computed jump into the correct action for this state. Both the PUSH
|
||||||
; above and the OUT below zero the other bits in ISR
|
; above and the OUT below zero out the other bits in ISR
|
||||||
OUT ISR, 2
|
OUT ISR, 2
|
||||||
IN PINS, 2
|
IN PINS, 2
|
||||||
|
|
||||||
; save the state in the OSR, so that we can use ISR for other purposes
|
; save the state in the OSR, so that we can use ISR for other purposes
|
||||||
MOV OSR, ISR
|
MOV OSR, ISR
|
||||||
; jump to the correct state machine action
|
; jump to the correct state machine action
|
||||||
MOV PC, ISR
|
MOV PC, ISR
|
||||||
|
|
||||||
; the PIO does not have a increment instruction, so to do that we do a
|
; the PIO does not have a increment instruction, so to do that we do a
|
||||||
; negate, decrement, negate sequence
|
; negate, decrement, negate sequence
|
||||||
increment:
|
increment:
|
||||||
MOV Y, ~Y
|
MOV Y, ~Y
|
||||||
JMP Y--, increment_cont
|
JMP Y--, increment_cont
|
||||||
increment_cont:
|
increment_cont:
|
||||||
MOV Y, ~Y
|
MOV Y, ~Y
|
||||||
.wrap ; the .wrap here avoids one jump instruction and saves a cycle too
|
.wrap ; the .wrap here avoids one jump instruction and saves a cycle too
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -97,46 +97,45 @@ increment_cont:
|
|||||||
|
|
||||||
static inline void quadrature_encoder_program_init(PIO pio, uint sm, uint pin, int max_step_rate)
|
static inline void quadrature_encoder_program_init(PIO pio, uint sm, uint pin, int max_step_rate)
|
||||||
{
|
{
|
||||||
pio_sm_set_consecutive_pindirs(pio, sm, pin, 2, false);
|
pio_sm_set_consecutive_pindirs(pio, sm, pin, 2, false);
|
||||||
gpio_pull_up(pin);
|
gpio_pull_up(pin);
|
||||||
gpio_pull_up(pin + 1);
|
gpio_pull_up(pin + 1);
|
||||||
|
|
||||||
pio_sm_config c = quadrature_encoder_program_get_default_config(0);
|
pio_sm_config c = quadrature_encoder_program_get_default_config(0);
|
||||||
|
|
||||||
sm_config_set_in_pins(&c, pin); // for WAIT, IN
|
sm_config_set_in_pins(&c, pin); // for WAIT, IN
|
||||||
sm_config_set_jmp_pin(&c, pin); // for JMP
|
sm_config_set_jmp_pin(&c, pin); // for JMP
|
||||||
// shift to left, autopull disabled
|
// shift to left, autopull disabled
|
||||||
sm_config_set_in_shift(&c, false, false, 32);
|
sm_config_set_in_shift(&c, false, false, 32);
|
||||||
// don't join FIFO's
|
// don't join FIFO's
|
||||||
sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_NONE);
|
sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_NONE);
|
||||||
|
|
||||||
// passing "0" as the sample frequency,
|
// passing "0" as the sample frequency,
|
||||||
if (max_step_rate == 0) {
|
if (max_step_rate == 0) {
|
||||||
sm_config_set_clkdiv(&c, 1.0);
|
sm_config_set_clkdiv(&c, 1.0);
|
||||||
} else {
|
} else {
|
||||||
// one state machine loop takes at most 10 cycles
|
// one state machine loop takes at most 10 cycles
|
||||||
float div = (float)clock_get_hz(clk_sys) / (10 * max_step_rate);
|
float div = (float)clock_get_hz(clk_sys) / (10 * max_step_rate);
|
||||||
sm_config_set_clkdiv(&c, div);
|
sm_config_set_clkdiv(&c, div);
|
||||||
}
|
}
|
||||||
|
|
||||||
pio_sm_init(pio, sm, 0, &c);
|
pio_sm_init(pio, sm, 0, &c);
|
||||||
pio_sm_set_enabled(pio, sm, true);
|
pio_sm_set_enabled(pio, sm, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline int32_t quadrature_encoder_get_count(PIO pio, uint sm)
|
static inline int32_t quadrature_encoder_get_count(PIO pio, uint sm)
|
||||||
{
|
{
|
||||||
uint ret;
|
uint ret;
|
||||||
int n;
|
int n;
|
||||||
|
|
||||||
// if the FIFO has N entries, we fetch them to drain the FIFO,
|
// if the FIFO has N entries, we fetch them to drain the FIFO,
|
||||||
// plus one entry which will be guaranteed to not be stale
|
// plus one entry which will be guaranteed to not be stale
|
||||||
n = pio_sm_get_rx_fifo_level(pio, sm) + 1;
|
n = pio_sm_get_rx_fifo_level(pio, sm) + 1;
|
||||||
while (n > 0) {
|
while (n > 0) {
|
||||||
ret = pio_sm_get_blocking(pio, sm);
|
ret = pio_sm_get_blocking(pio, sm);
|
||||||
n--;
|
n--;
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
%}
|
%}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user