From 59fa7829da0b5effc849bca9a3aa0c671896dc4a Mon Sep 17 00:00:00 2001 From: Devoalda Date: Wed, 6 Sep 2023 19:09:57 +0800 Subject: [PATCH] fix(UART FIFO): Removed Buffer clearing Disabled UART FIFO --- lab_2/lab_2.c | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/lab_2/lab_2.c b/lab_2/lab_2.c index 5e26ea6..82af630 100644 --- a/lab_2/lab_2.c +++ b/lab_2/lab_2.c @@ -7,10 +7,10 @@ #include #include "pico/stdlib.h" +#include "hardware/uart.h" #define UART_ID uart0 #define BAUD_RATE 115200 // Baud rate of UART -#define UART_BUF_SIZE 64 // Buffer size for UART #define UART_TX_PIN 16 // GPIO for UART TX pin #define UART_RX_PIN 17 // GPIO for UART RX pin @@ -33,15 +33,6 @@ static void uart_transmit(char *uart_input) { } } -/** - * Clear buffer (read until buffer size is UART_BUF_SIZE) - * This is to prevent the program from printing out garbage - * for the first few characters received or during pseudo button press - * @return void - */ -static void uart_buf_clr() { - while (uart_is_readable(UART_ID) && uart_getc(UART_ID) != UART_BUF_SIZE); -} /** * Receives a character through UART0 and prints it out @@ -60,8 +51,6 @@ static void uart_receive(char *uart_output) { *uart_output = '2'; // If '1' is received, print '2' } - uart_buf_clr(); // Clear buffer - printf("%c\n", *uart_output); } } @@ -76,11 +65,15 @@ static void pin_init() { gpio_set_function(UART_TX_PIN, GPIO_FUNC_UART); gpio_set_function(UART_RX_PIN, GPIO_FUNC_UART); + // Disable UART FIFO for data to be tx/rx immediately + uart_set_fifo_enabled(UART_ID, false); + // Pseudo button init gpio_set_dir(PSEUDO_BTN, GPIO_IN); // Enable pull up resistor gpio_set_pulls(PSEUDO_BTN, true, false); + } int main() {