1.1 --- a/debug.c Fri Oct 19 17:50:59 2018 +0200
1.2 +++ b/debug.c Fri Oct 19 18:05:07 2018 +0200
1.3 @@ -20,7 +20,7 @@
1.4 #include "pic32_c.h"
1.5 #include "debug.h"
1.6
1.7 -/* Value output functions. */
1.8 +/* Value output functions using UART1. */
1.9
1.10 void bits(uint32_t reg)
1.11 {
1.12 @@ -59,13 +59,39 @@
1.13 uart_write('\n');
1.14 }
1.15
1.16 -/* General output functions. */
1.17 +
1.18 +
1.19 +/* General input/output functions. */
1.20 +
1.21 +int uart_can_read(int uart)
1.22 +{
1.23 + return REG(UART_REG(uart, UxSTA)) & 1;
1.24 +}
1.25 +
1.26 +char uart_read_char(int uart)
1.27 +{
1.28 + return (char) REG(UART_REG(uart, UxRXREG));
1.29 +}
1.30 +
1.31 +int uart_can_write(int uart)
1.32 +{
1.33 + return !(REG(UART_REG(uart, UxSTA)) & (1 << 9)); /* UTXBF (buffer full) */
1.34 +}
1.35 +
1.36 +void uart_write_char(int uart, char c)
1.37 +{
1.38 + while (!uart_can_write(uart)); /* busy loop */
1.39 +
1.40 + REG(UART_REG(uart, UxTXREG)) = c;
1.41 +}
1.42 +
1.43 +
1.44 +
1.45 +/* Functions using UART1. */
1.46
1.47 void uart_write(char c)
1.48 {
1.49 - while (REG(UART_REG(1, UxSTA)) & (1 << 9)); /* UTXBF (buffer full) */
1.50 -
1.51 - REG(UART_REG(1, UxTXREG)) = c;
1.52 + uart_write_char(1, c);
1.53 }
1.54
1.55 void uart_write_string(const char *s)
2.1 --- a/debug.h Fri Oct 19 17:50:59 2018 +0200
2.2 +++ b/debug.h Fri Oct 19 18:05:07 2018 +0200
2.3 @@ -20,9 +20,21 @@
2.4 #ifndef __DEBUG_H__
2.5 #define __DEBUG_H__
2.6
2.7 +/* Value output functions using UART1. */
2.8 +
2.9 void bits(uint32_t reg);
2.10 void vbits(uint32_t val);
2.11 void vhex(uint32_t val);
2.12 +
2.13 +/* General input/output functions. */
2.14 +
2.15 +int uart_can_read(int uart);
2.16 +char uart_read_char(int uart);
2.17 +int uart_can_write(int uart);
2.18 +void uart_write_char(int uart, char c);
2.19 +
2.20 +/* Functions using UART1. */
2.21 +
2.22 void uart_write(char c);
2.23 void uart_write_string(const char *s);
2.24
3.1 --- a/main.c Fri Oct 19 17:50:59 2018 +0200
3.2 +++ b/main.c Fri Oct 19 18:05:07 2018 +0200
3.3 @@ -132,7 +132,8 @@
3.4
3.5 void interrupt_handler(void)
3.6 {
3.7 - uint32_t ifs, val;
3.8 + uint32_t ifs;
3.9 + char val;
3.10
3.11 /* Check for a UART receive interrupt condition (UxRIF). */
3.12
3.13 @@ -146,11 +147,11 @@
3.14
3.15 /* Write the received data back. */
3.16
3.17 - while (REG(UART_REG(1, UxSTA)) & 1)
3.18 + while (uart_can_read(1))
3.19 {
3.20 - val = REG(UART_REG(1, UxRXREG));
3.21 + val = uart_read_char(1);
3.22 if (uart_echo)
3.23 - uart_write((char) val);
3.24 + uart_write_char(1, val);
3.25
3.26 /* Initiate transfer upon receiving a particular character. */
3.27