# HG changeset patch # User Paul Boddie # Date 1539965107 -7200 # Node ID f56fc460bc08d37983c11514ce68311679df0243 # Parent 0f9eb8bc112d20aea4fe6d07ed1772b7876ec433 Introduced some UART convenience functions. diff -r 0f9eb8bc112d -r f56fc460bc08 debug.c --- a/debug.c Fri Oct 19 17:50:59 2018 +0200 +++ b/debug.c Fri Oct 19 18:05:07 2018 +0200 @@ -20,7 +20,7 @@ #include "pic32_c.h" #include "debug.h" -/* Value output functions. */ +/* Value output functions using UART1. */ void bits(uint32_t reg) { @@ -59,13 +59,39 @@ uart_write('\n'); } -/* General output functions. */ + + +/* General input/output functions. */ + +int uart_can_read(int uart) +{ + return REG(UART_REG(uart, UxSTA)) & 1; +} + +char uart_read_char(int uart) +{ + return (char) REG(UART_REG(uart, UxRXREG)); +} + +int uart_can_write(int uart) +{ + return !(REG(UART_REG(uart, UxSTA)) & (1 << 9)); /* UTXBF (buffer full) */ +} + +void uart_write_char(int uart, char c) +{ + while (!uart_can_write(uart)); /* busy loop */ + + REG(UART_REG(uart, UxTXREG)) = c; +} + + + +/* Functions using UART1. */ void uart_write(char c) { - while (REG(UART_REG(1, UxSTA)) & (1 << 9)); /* UTXBF (buffer full) */ - - REG(UART_REG(1, UxTXREG)) = c; + uart_write_char(1, c); } void uart_write_string(const char *s) diff -r 0f9eb8bc112d -r f56fc460bc08 debug.h --- a/debug.h Fri Oct 19 17:50:59 2018 +0200 +++ b/debug.h Fri Oct 19 18:05:07 2018 +0200 @@ -20,9 +20,21 @@ #ifndef __DEBUG_H__ #define __DEBUG_H__ +/* Value output functions using UART1. */ + void bits(uint32_t reg); void vbits(uint32_t val); void vhex(uint32_t val); + +/* General input/output functions. */ + +int uart_can_read(int uart); +char uart_read_char(int uart); +int uart_can_write(int uart); +void uart_write_char(int uart, char c); + +/* Functions using UART1. */ + void uart_write(char c); void uart_write_string(const char *s); diff -r 0f9eb8bc112d -r f56fc460bc08 main.c --- a/main.c Fri Oct 19 17:50:59 2018 +0200 +++ b/main.c Fri Oct 19 18:05:07 2018 +0200 @@ -132,7 +132,8 @@ void interrupt_handler(void) { - uint32_t ifs, val; + uint32_t ifs; + char val; /* Check for a UART receive interrupt condition (UxRIF). */ @@ -146,11 +147,11 @@ /* Write the received data back. */ - while (REG(UART_REG(1, UxSTA)) & 1) + while (uart_can_read(1)) { - val = REG(UART_REG(1, UxRXREG)); + val = uart_read_char(1); if (uart_echo) - uart_write((char) val); + uart_write_char(1, val); /* Initiate transfer upon receiving a particular character. */