Skip to content

Commit 323ad00

Browse files
committed
can2040: Add enum with state machine names to can2040.c
Signed-off-by: Kevin O'Connor <[email protected]>
1 parent 2d08b05 commit 323ad00

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

src/can2040.c

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ static const uint16_t can2040_program_instructions[] = {
130130
#define SI_RX_DATA PIO_IRQ0_INTE_SM1_RXNEMPTY_BITS
131131
#define SI_TXPENDING PIO_IRQ0_INTE_SM1_BITS // Misc bit manually forced
132132

133+
// Local names of the four PIO state machines
134+
enum { SM_SYNC = 0, SM_RX = 1, SM_MATCH = 2, SM_TX = 3 };
135+
133136
// Return the gpio bank offset (on rp2350 chips)
134137
static uint32_t
135138
pio_gpiobase(struct can2040 *cd)
@@ -144,7 +147,7 @@ static void
144147
pio_sync_setup(struct can2040 *cd)
145148
{
146149
pio_hw_t *pio_hw = cd->pio_hw;
147-
pio_sm_hw_t *sm = &pio_hw->sm[0];
150+
pio_sm_hw_t *sm = &pio_hw->sm[SM_SYNC];
148151
uint32_t gpio_rx = (cd->gpio_rx - pio_gpiobase(cd)) & 0x1f;
149152
sm->execctrl = (
150153
gpio_rx << PIO_SM0_EXECCTRL_JMP_PIN_LSB
@@ -155,7 +158,7 @@ pio_sync_setup(struct can2040 *cd)
155158
| gpio_rx << PIO_SM0_PINCTRL_SET_BASE_LSB);
156159
sm->instr = 0xe080; // set pindirs, 0
157160
sm->pinctrl = 0;
158-
pio_hw->txf[0] = 9 + 6 * PIO_CLOCK_PER_BIT / 2;
161+
pio_hw->txf[SM_SYNC] = 9 + 6 * PIO_CLOCK_PER_BIT / 2;
159162
sm->instr = 0x80a0; // pull block
160163
sm->instr = can2040_offset_sync_entry; // jmp sync_entry
161164
}
@@ -165,7 +168,7 @@ static void
165168
pio_rx_setup(struct can2040 *cd)
166169
{
167170
pio_hw_t *pio_hw = cd->pio_hw;
168-
pio_sm_hw_t *sm = &pio_hw->sm[1];
171+
pio_sm_hw_t *sm = &pio_hw->sm[SM_RX];
169172
uint32_t gpio_rx = (cd->gpio_rx - pio_gpiobase(cd)) & 0x1f;
170173
sm->execctrl = (
171174
(can2040_offset_shared_rx_end - 1) << PIO_SM0_EXECCTRL_WRAP_TOP_LSB
@@ -183,7 +186,7 @@ static void
183186
pio_match_setup(struct can2040 *cd)
184187
{
185188
pio_hw_t *pio_hw = cd->pio_hw;
186-
pio_sm_hw_t *sm = &pio_hw->sm[2];
189+
pio_sm_hw_t *sm = &pio_hw->sm[SM_MATCH];
187190
sm->execctrl = (
188191
(can2040_offset_match_end - 1) << PIO_SM0_EXECCTRL_WRAP_TOP_LSB
189192
| can2040_offset_shared_rx_read << PIO_SM0_EXECCTRL_WRAP_BOTTOM_LSB);
@@ -201,7 +204,7 @@ static void
201204
pio_tx_setup(struct can2040 *cd)
202205
{
203206
pio_hw_t *pio_hw = cd->pio_hw;
204-
pio_sm_hw_t *sm = &pio_hw->sm[3];
207+
pio_sm_hw_t *sm = &pio_hw->sm[SM_TX];
205208
uint32_t gpio_rx = (cd->gpio_rx - pio_gpiobase(cd)) & 0x1f;
206209
uint32_t gpio_tx = (cd->gpio_tx - pio_gpiobase(cd)) & 0x1f;
207210
sm->execctrl = (
@@ -249,7 +252,7 @@ static void
249252
pio_match_check(struct can2040 *cd, uint32_t match_key)
250253
{
251254
pio_hw_t *pio_hw = cd->pio_hw;
252-
pio_hw->txf[2] = match_key;
255+
pio_hw->txf[SM_MATCH] = match_key;
253256
}
254257

255258
// Calculate pos+bits identifier for PIO "match" state machine
@@ -276,7 +279,7 @@ pio_tx_reset(struct can2040 *cd)
276279
| (0x08 << PIO_CTRL_SM_RESTART_LSB));
277280
pio_hw->irq = (SI_MATCHED | SI_ACKDONE) >> 8; // clear PIO irq flags
278281
// Clear tx fifo
279-
pio_sm_hw_t *sm = &pio_hw->sm[3];
282+
pio_sm_hw_t *sm = &pio_hw->sm[SM_TX];
280283
sm->shiftctrl = 0;
281284
sm->shiftctrl = (PIO_SM0_SHIFTCTRL_FJOIN_TX_BITS
282285
| PIO_SM0_SHIFTCTRL_AUTOPULL_BITS);
@@ -291,8 +294,8 @@ pio_tx_send(struct can2040 *cd, uint32_t *data, uint32_t count)
291294
pio_hw->instr_mem[can2040_offset_tx_got_recessive] = 0x6021; // out x, 1
292295
uint32_t i;
293296
for (i=0; i<count; i++)
294-
pio_hw->txf[3] = data[i];
295-
pio_sm_hw_t *sm = &pio_hw->sm[3];
297+
pio_hw->txf[SM_TX] = data[i];
298+
pio_sm_hw_t *sm = &pio_hw->sm[SM_TX];
296299
sm->instr = 0xe001; // set pins, 1
297300
sm->instr = 0x6021; // out x, 1
298301
sm->instr = can2040_offset_tx_write_pin; // jmp tx_write_pin
@@ -307,8 +310,8 @@ pio_tx_inject_ack(struct can2040 *cd, uint32_t match_key)
307310
pio_hw_t *pio_hw = cd->pio_hw;
308311
pio_tx_reset(cd);
309312
pio_hw->instr_mem[can2040_offset_tx_got_recessive] = 0xc023; // irq wait 3
310-
pio_hw->txf[3] = 0x7fffffff;
311-
pio_sm_hw_t *sm = &pio_hw->sm[3];
313+
pio_hw->txf[SM_TX] = 0x7fffffff;
314+
pio_sm_hw_t *sm = &pio_hw->sm[SM_TX];
312315
sm->instr = 0xe001; // set pins, 1
313316
sm->instr = 0x6021; // out x, 1
314317
sm->instr = can2040_offset_tx_write_pin; // jmp tx_write_pin
@@ -324,7 +327,7 @@ pio_tx_did_fail(struct can2040 *cd)
324327
{
325328
pio_hw_t *pio_hw = cd->pio_hw;
326329
// Check for passive/dominant bit conflict without parser noticing
327-
if (pio_hw->sm[3].addr == can2040_offset_tx_conflict)
330+
if (pio_hw->sm[SM_TX].addr == can2040_offset_tx_conflict)
328331
return !(pio_hw->intr & SI_RX_DATA);
329332
// Check for unexpected drain of transmit queue without parser noticing
330333
return (!(pio_hw->flevel & PIO_FLEVEL_TX3_BITS)
@@ -1313,7 +1316,7 @@ can2040_pio_irq_handler(struct can2040 *cd)
13131316
pio_hw_t *pio_hw = cd->pio_hw;
13141317
uint32_t ints = pio_hw->ints0;
13151318
while (likely(ints & SI_RX_DATA)) {
1316-
uint32_t rx_data = pio_hw->rxf[1];
1319+
uint32_t rx_data = pio_hw->rxf[SM_RX];
13171320
process_rx(cd, rx_data);
13181321
ints = pio_hw->ints0;
13191322
if (likely(!ints))

0 commit comments

Comments
 (0)