From 620596cd6f823efbcee3af5e66677d1e2ff68508 Mon Sep 17 00:00:00 2001 From: Devoalda Date: Wed, 6 Sep 2023 08:36:30 +0800 Subject: [PATCH] fix(Lab 2): Added: - Buffer SIZE and clearing - Enabled GP15 pull-up resistor Modified: - Sleep time according to Lab instruction - Variables to unsigned char --- .gitignore | 3 ++- lab_2/lab_2.c | 47 +++++++++++++++++++++++++++++++---------------- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index 3b277c8..95561fa 100644 --- a/.gitignore +++ b/.gitignore @@ -269,4 +269,5 @@ gen # Android studio 3.1+ serialized cache file -*/build/* \ No newline at end of file +*/build/* +*/*.cmake \ No newline at end of file diff --git a/lab_2/lab_2.c b/lab_2/lab_2.c index 458c333..5e26ea6 100644 --- a/lab_2/lab_2.c +++ b/lab_2/lab_2.c @@ -1,7 +1,8 @@ /** * Lab 2 - * Note that this program would print out garbage for the first few characters - * received. It will work as intended after a few characters are received. + * This is configured with GPIO15 as a pseudo button with a pull up resistor + * Since the button is active low, the program will send '1' when the button is not pressed (high) + * and send 'A' to 'Z' when the button is pressed (low) */ #include @@ -9,12 +10,13 @@ #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 -#define GPIO_PIN 15 // GPIO for pseudo button +#define PSEUDO_BTN 15 // GPIO for pseudo button -#define SLEEP_TIME 100 // For testing can use a smaller value +#define SLEEP_TIME 1000 // For testing can use a smaller value // 1000ms is based on the lab requirements /** @@ -22,24 +24,32 @@ * @param uart_input Pointer to a character to be sent through UART0 (Space optimisation) * @return void Prints the character in uart_receive() */ -void uart_transmit(char *uart_input) { - if (gpio_get(GPIO_PIN)) { // Active low +static void uart_transmit(char *uart_input) { + if (gpio_get(PSEUDO_BTN)) { // If GPIO15 is high uart_putc(UART_ID, '1'); - } else { // Active high + } else { // If GPIO15 is low uart_putc(UART_ID, *uart_input); - *uart_input = (*uart_input - 'A' + 1) % 26 + 'A'; // Next character - // Wrap around back to A + *uart_input = (*uart_input - 'A' + 1) % 26 + 'A'; // Next character and wrap around back to A } } +/** + * 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 - * Ideally, the buffer should be cleared after each read * NOTE: Flipping bit 5 effectively converts a character to lowercase * @param uart_output Pointer to a location to store the received character (Space optimisation) * @return void Character is read and printed */ -void uart_receive(char *uart_output) { +static void uart_receive(char *uart_output) { if (uart_is_readable(UART_ID)) { *uart_output = uart_getc(UART_ID); @@ -50,6 +60,8 @@ 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); } } @@ -58,22 +70,25 @@ void uart_receive(char *uart_output) { * Initializes UART0 and GPIO pins * @return void */ -void pin_init() { +static void pin_init() { // UART init uart_init(UART_ID, BAUD_RATE); gpio_set_function(UART_TX_PIN, GPIO_FUNC_UART); gpio_set_function(UART_RX_PIN, GPIO_FUNC_UART); // Pseudo button init - gpio_set_dir(GPIO_PIN, GPIO_IN); + gpio_set_dir(PSEUDO_BTN, GPIO_IN); + + // Enable pull up resistor + gpio_set_pulls(PSEUDO_BTN, true, false); } int main() { stdio_init_all(); pin_init(); - char uart_input = 'A'; // Initial value of A - char uart_output; // To store received character + u_char uart_input = 'A'; // Initial value of A + u_char uart_output; // To store received character while (true) { uart_transmit(&uart_input); // Pass by reference to change @@ -82,7 +97,7 @@ int main() { uart_receive(&uart_output); // Pass by reference to change // value of uart_output during reception - sleep_ms(SLEEP_TIME); // Sleep based on SLEEP_TIME + sleep_ms(SLEEP_TIME); // Sleep based on SLEEP_TIME } }