rp2040/example/peripherals/uart/uart_interrupt/main.c

44 lines
1.1 KiB
C
Raw Normal View History

2025-06-02 20:37:44 +08:00
#include "gpio.h"
#include "uart.h"
#include "resets.h"
#include "irq.h"
#include "stdio.h"
#define UART_ID uart0_hw
void uart_isr(void)
{
int c, r;
while (1) {
c = uart_get_char(UART_ID);
if (c < 0) {
break;
} else {
while (1) {
r = uart_put_char(UART_ID, (uint8_t)c);
if (r < 0) {
continue;
} else {
break;
}
}
}
}
}
int main(void)
{
reset_unreset_blocks_wait(RESETS_BLOCK_IO_BANK0 | RESETS_BLOCK_UART0);
gpio_init(0, GPIO_FUNC_UART | GPIO_PULL_UP | GPIO_DRIVE_4MA); /* UART_TX pin */
gpio_init(1, GPIO_FUNC_UART | GPIO_PULL_UP | GPIO_SCHMITT | GPIO_PAD_IE | GPIO_PAD_OD); /* UART_RX pin */
uart_init(UART_ID, 6 * 1000 * 1000, UART_DATABITS_8 | UART_PARITY_NONE | UART_STOPBITS_1);
printf("uart_interrupt example\r\n");
uart_int_enable(UART_ID, UART_INT_RX | UART_INT_RTO);
irq_attach(UART0_IRQ, uart_isr);
irq_enable(UART0_IRQ);
return 0;
}