Created
January 27, 2022 18:35
-
-
Save nmk456/68e56a52dfa75990f447cc9a9be18c30 to your computer and use it in GitHub Desktop.
Code to test RP2040 UART FIFO level trigger settings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include "pico/stdlib.h" | |
#include "pico/time.h" | |
#include "pico/multicore.h" | |
#include "hardware/irq.h" | |
#include "hardware/uart.h" | |
#define UART_TX_ID uart1 | |
#define UART_TX 4 | |
#define UART_RX_ID uart0 | |
#define UART_RX 1 | |
uint rx_ints; | |
uint rx_bytes; | |
void uart_rx_handle() { | |
rx_ints++; | |
while(uart_is_readable(UART_RX_ID)) { | |
uint8_t x = uart_getc(UART_RX_ID); | |
rx_bytes++; | |
} | |
} | |
// Run all UART TX stuff on second core | |
void core1_main() { | |
uart_init(UART_TX_ID, 115200); | |
gpio_set_function(UART_TX, GPIO_FUNC_UART); | |
uart_set_hw_flow(UART_RX_ID, false, false); | |
uart_set_fifo_enabled(UART_RX_ID, true); | |
while (1) { | |
uart_putc_raw(UART_TX_ID, 0x55); | |
} | |
} | |
int main() { | |
multicore_reset_core1(); | |
multicore_launch_core1(core1_main); | |
stdio_init_all(); | |
// Set up UART | |
uart_init(UART_RX_ID, 115200); | |
gpio_set_function(UART_RX, GPIO_FUNC_UART); | |
uart_set_hw_flow(UART_RX_ID, false, false); | |
uart_set_fifo_enabled(UART_RX_ID, true); | |
// Set up interrupt | |
int UART_IRQ = UART_RX_ID == uart0 ? UART0_IRQ : UART1_IRQ; | |
irq_set_exclusive_handler(UART_IRQ, uart_rx_handle); | |
irq_set_enabled(UART_IRQ, true); | |
while(1) { | |
for (int i=UART_FIFO_LEVEL_1_8; i<=UART_FIFO_LEVEL_7_8; i++) { | |
rx_ints = 0; | |
rx_bytes = 0; | |
// Run for 1 second | |
while(uart_is_readable(UART_RX_ID)) uart_getc(UART_RX_ID); | |
uart_set_irq_enables(UART_RX_ID, true, false); | |
uart_set_irq_fifo_levels(UART_RX_ID, i, i); | |
sleep_ms(1000); | |
uart_set_irq_enables(UART_RX_ID, false, false); | |
float avg = (float) rx_bytes / (float) rx_ints; | |
switch (i) { | |
case UART_FIFO_LEVEL_1_8: | |
printf("FIFO Trigger 1/8"); | |
break; | |
case UART_FIFO_LEVEL_1_4: | |
printf("FIFO Trigger 1/4"); | |
break; | |
case UART_FIFO_LEVEL_1_2: | |
printf("FIFO Trigger 1/2"); | |
break; | |
case UART_FIFO_LEVEL_3_4: | |
printf("FIFO Trigger 3/4"); | |
break; | |
case UART_FIFO_LEVEL_7_8: | |
printf("FIFO Trigger 7/8"); | |
break; | |
} | |
printf(", average bytes per interrupt: %f, bytes: %d, interrupts: %d\n", avg, rx_bytes, rx_ints); | |
sleep_ms(500); | |
} | |
} | |
return 0; | |
} |
Author
nmk456
commented
Jan 27, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment