diff --git a/rp2040_uart/2024-03_rp2040-uart.md b/rp2040_uart/2024-03_rp2040-uart.md index 9daf10438daded379adf44aa3a4ee1af8db9d4db..823818e3ede12b574cea410db11df60f400c4117 100644 --- a/rp2040_uart/2024-03_rp2040-uart.md +++ b/rp2040_uart/2024-03_rp2040-uart.md @@ -35,8 +35,28 @@ So we should be able to to clk/8 : 16MBit/s, and indeed we can see things workin But this is not terribly interesting: we want to see that we can catch words fast enough: the ISR on the RX side is normally where we meet our limits. -### UART PIO RX'ing +### UART PIO RX'ing with PIO Example So, I should spin up an RX line now and see about firing an interrupt there... I'll get one chip TX'ing at a fixed rate and then start in on no. 2 -... doing this using [the blocking example](https://github.com/raspberrypi/pico-examples/blob/master/pio/uart_rx/uart_rx.c) catches *some* bytes, but not that many (at only 1mbaud), and it's perhaps only latching when we *just* catch the byte in time, i.e. if we poll just as the last bit has arrived. \ No newline at end of file +... doing this using [the blocking example](https://github.com/raspberrypi/pico-examples/blob/master/pio/uart_rx/uart_rx.c) catches *some* bytes, but not that many (at only 1mbaud), and it's perhaps only latching when we *just* catch the byte in time, i.e. if we poll just as the last bit has arrived - or perhaps we're only-sometimes catching in time, and the thing is not receiving next bytes etc etc.. + +So, the interrupt version... works at a similar quality: some bytes are captured, many are not. It tends to happen in phases. In traces below, CH1 goes lo-then-hi whenever a new byte is loaded into the TX chip, CH2 is the UART trace (TX/RX), and CH4 flips state whenever the UART RX IRQ fires. + + + + +So, this is all kind of bad news for our project, and I suspect I would have to get into some of the PIO depths to figure out what's going wrong, which I don't really have the time for at the moment. I can try slowing it down to see if this is a chunking error or something else... it's the same even at 115200 BAUD. + +So - for troubleshooting, I am perhaps missing some pin config? But that looks to be handled in the example's setup. + +Not totally sure, but I'm going to move on to try out Earle's software serial PIO, which I have some prior experience with, but also found some bugs in (?) IIRC. + +### UART PIO RX'ing with Earle's Software Serial PIO + +[the earle commit](https://github.com/earlephilhower/arduino-pico/pull/391) +[the earle pio_uart.h](https://github.com/earlephilhower/arduino-pico/blob/326697bbe1cc3b4b5f7c140dca10a6924262539d/cores/rp2040/pio_uart.pio.h) +[the earle pio softwareserial.h](https://github.com/earlephilhower/arduino-pico/blob/4c1c72c996b6a1243b0eafa956dd0eb6410e2362/cores/rp2040/SerialPIO.h) +[the earle pio softwareserial.cpp](https://github.com/earlephilhower/arduino-pico/blob/4c1c72c996b6a1243b0eafa956dd0eb6410e2362/cores/rp2040/SerialPIO.cpp) + +So - let's try this out. \ No newline at end of file diff --git a/rp2040_uart/code/uart_pio_rx/uart_pio_rx.ino b/rp2040_uart/code/uart_pio_rx/uart_pio_rx.ino index d79b4d0fa9414b985c939032a62d28a07f2d2702..81d4fe9e1286d235e6035f569733de829fa4aca9 100644 --- a/rp2040_uart/code/uart_pio_rx/uart_pio_rx.ino +++ b/rp2040_uart/code/uart_pio_rx/uart_pio_rx.ino @@ -9,13 +9,22 @@ // on XIAO "RX" - GPIO 1 #define PIN_RX 1 -#define PIO_BAUD 1000000 +#define PIO_BAUD 115200 // the PIO, and statemachine ? PIO pio = pio0; uint sm = 0; uint offset = 0; +// irq handler, +void pio_irq_func(void){ + while(!pio_sm_is_rx_fifo_empty(pio, sm)){ + digitalWrite(PIN_DEBUG, !digitalRead(PIN_DEBUG)); + uint8_t data = uart_rx_program_getc(pio, sm); + } +} + + void setup(void){ pinMode(PIN_LED_B, OUTPUT); digitalWrite(PIN_LED_B, LOW); @@ -27,25 +36,33 @@ void setup(void){ displaySetup(); displayPrint("bonjour..."); + // add and init uart offset = pio_add_program(pio, &uart_rx_program); uart_rx_program_init(pio, sm, offset, PIN_RX, PIO_BAUD); + + // setup the IRQ, + irq_add_shared_handler(PIO0_IRQ_0, pio_irq_func, PICO_SHARED_IRQ_HANDLER_DEFAULT_ORDER_PRIORITY); + irq_set_enabled(PIO0_IRQ_0, true); + // ???? lol ffs + const uint irq_index = PIO0_IRQ_0 - ((pio == pio0) ? PIO0_IRQ_0 : PIO1_IRQ_0); // Get index of the IRQ; + // set PIO to interrupt when FIFO is NOT empty + pio_set_irqn_source_enabled(pio, irq_index, pis_sm0_rx_fifo_not_empty, true); } uint32_t lastUpdate = 0; uint32_t updateInterval = 200; void loop(void){ - digitalWrite(PIN_DEBUG, !digitalRead(PIN_DEBUG)); - uint8_t data = uart_rx_program_getc(pio, sm); + // uint8_t data = uart_rx_program_getc(pio, sm); // digitalWrite(PIN_DEBUG, LOW); // ... - // if(lastUpdate + updateInterval < millis()){ - // lastUpdate = millis(); - // digitalWrite(PIN_LED_B, !digitalRead(PIN_LED_B)); - // // displayPrint(spipi_print()); - // // displayPrint(String(rxCount) + "\n" + - // // String(rxSize) - // // ); - // } + if(lastUpdate + updateInterval < millis()){ + lastUpdate = millis(); + digitalWrite(PIN_LED_B, !digitalRead(PIN_LED_B)); + // displayPrint(spipi_print()); + // displayPrint(String(rxCount) + "\n" + + // String(rxSize) + // ); + } } diff --git a/rp2040_uart/code/uart_pio_tx/uart_pio_tx.ino b/rp2040_uart/code/uart_pio_tx/uart_pio_tx.ino index cf3e913337e31597d6740b6de5e11ee2cb02b54d..73a5095fe126fd217072aaad8d364e3409f3b3b4 100644 --- a/rp2040_uart/code/uart_pio_tx/uart_pio_tx.ino +++ b/rp2040_uart/code/uart_pio_tx/uart_pio_tx.ino @@ -9,7 +9,7 @@ // on XIAO "TX" - GPIO 0 #define PIN_TX 0 -#define PIO_BAUD 1000000 +#define PIO_BAUD 115200 // the PIO, and statemachine ? PIO pio = pio0; @@ -38,7 +38,7 @@ void loop(void){ digitalWrite(PIN_DEBUG, HIGH); // blocking tx-put: uart_tx_program_putc(pio, sm, 85); - delayMicroseconds(30); + delayMicroseconds(200); digitalWrite(PIN_DEBUG, LOW); // ... if(lastUpdate + updateInterval < millis()){ diff --git a/rp2040_uart/images/2024-01-03_iffy-irq-rx-01.png b/rp2040_uart/images/2024-01-03_iffy-irq-rx-01.png new file mode 100644 index 0000000000000000000000000000000000000000..6b5e5aaa51f4440ce35a2ed8decb93b1baee26c5 Binary files /dev/null and b/rp2040_uart/images/2024-01-03_iffy-irq-rx-01.png differ diff --git a/rp2040_uart/images/2024-01-03_iffy-irq-rx-02.png b/rp2040_uart/images/2024-01-03_iffy-irq-rx-02.png new file mode 100644 index 0000000000000000000000000000000000000000..7bebcba0a699155630db716032668aaa0d56ad19 Binary files /dev/null and b/rp2040_uart/images/2024-01-03_iffy-irq-rx-02.png differ