diff --git a/Makefile b/Makefile index 31d2d05..a0a0c4f 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,8 @@ # GENERAL SETTINGS # ============================================================================== +# NOTE: System clock frequency should be a multiple of 1.8432MHz for USART. + SRCDIR := src BINDIR := bin TMPDIR := $(BINDIR)/build @@ -17,6 +19,7 @@ LOGFILE := $(BINDIR)/core.log VERBOSE := false ARCH := m32 +#FREQ := 7372800UL FREQ := 8000000UL MCU := atmega32a ASP := usbasp diff --git a/src/common.h b/src/common.h index fa33a1c..c6764e4 100644 --- a/src/common.h +++ b/src/common.h @@ -1,6 +1,7 @@ #ifndef MAD_COMMON_H #define MAD_COMMON_H +#define BIT(n) (0x1U << (n)) #define UNUSED(s) (void)(s) #include diff --git a/src/main.c b/src/main.c index 040659c..016fa6d 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,5 @@ #include "common.h" -#include "uart.h" +#include "serial.h" #include @@ -17,17 +17,17 @@ int main(void) return 1; } - UART_Init(); + USART_Init(); for (i = 1;; i++) { if (i >= 99999) i = 1; sprintf(buf, "[CORE] Fetching sensors #%05lu...\r\n", i); - UART_Write(buf); + USART_Write(buf); - UART_Read(&cmd, MAX_CMDBUF); - sprintf(buf, "[CORE] Checking cmdbuf=\"%s\"\r\n", cmd); - UART_Write(buf); + // USART_Read(&cmd, MAX_CMDBUF); + // sprintf(buf, "[CORE] Checking cmdbuf=\"%s\"\r\n", cmd); + // USART_Write(buf); Sleep(3000); } diff --git a/src/serial.c b/src/serial.c new file mode 100644 index 0000000..84cb57a --- /dev/null +++ b/src/serial.c @@ -0,0 +1,76 @@ +#include "common.h" +#include "serial.h" +#include + +#include + +#define USART_BAUDRATE 9600 + +// Ensure closest fit for target baudrate +//#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1) +#define BAUD_PRESCALE ((((F_CPU / 16) + (USART_BAUDRATE / 2)) \ + / (USART_BAUDRATE)) - 1) + +static char USART_Rx(void); +static void USART_Tx(char ch); + +void USART_Init(void) +{ + UCSRB |= BIT(RXEN) | BIT(TXEN); // Enable rx and tx circuitry + UCSRC |= BIT(URSEL) | BIT(UCSZ0) | BIT(UCSZ1); // 8-bit chars + UBRRH = (BAUD_PRESCALE >> 8); // Baud Rate upper byte + UBRRL = BAUD_PRESCALE; // Baud rate lower byte +} + +void USART_Read(char **buf, int size) +{ + int n = 0; + char *p = *buf; + char ch; + + if ((UCSRA & BIT(RXC)) == 0) { + p = '\0'; + return; + } + + do { + // Discard messages exceeding + // given size limit quietly + if (n == size) { + p = '\0'; + while (USART_Rx()); + return; + } + + ch = USART_Rx(); + p[n] = ch; + n++; + + } while (ch != '\0'); +} + +void USART_Write(const char *buf) +{ + int n = 0; + + while (buf[n] != '\0') { + USART_Tx(buf[n++]); + } +} + +static char USART_Rx(void) +{ + // Anything to read from UDR? + if ((UCSRA & BIT(RXC)) == 0) + return '\0'; + + return UDR; +} + +static void USART_Tx(char ch) +{ + // Wait until UDR can receive data + while ((UCSRA & BIT(UDRE)) == 0); + + UDR = ch; +} diff --git a/src/serial.h b/src/serial.h new file mode 100644 index 0000000..fe74d29 --- /dev/null +++ b/src/serial.h @@ -0,0 +1,8 @@ +#ifndef MAD_SERIAL_H +#define MAD_SERIAL_H + +void USART_Init(void); +void USART_Read(char **buf, int size); +void USART_Write(const char *buf); + +#endif // MAD_SERIAL_H diff --git a/src/uart.c b/src/uart.c deleted file mode 100644 index cdde2cc..0000000 --- a/src/uart.c +++ /dev/null @@ -1,67 +0,0 @@ -#include "common.h" -#include - -#define BAUD_RATE 9600 -#define BAUD_PRESCALE (((F_CPU / (BAUD_RATE * 16UL))) - 1) - -static char UART_Rx(void); -static void UART_Tx(char ch); - -void UART_Init(void) -{ - UCSRB |= (1 << RXEN) | (1 << TXEN); - UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); - UBRRH = (BAUD_PRESCALE >> 8); - UBRRL = BAUD_PRESCALE; -} - - -void UART_Read(char **buf, int size) -{ - int n = 0; - char *p = *buf; - char ch; - - do { - // Discard messages exceeding - // given size limit quietly - if (n == size) { - p = '\0'; - while (UART_Rx()); - return; - } - - ch = UART_Rx(); - p[n] = ch; - n++; - - } while (ch != '\0'); -} - -void UART_Write(const char *buf) -{ - int n = 0; - - while (buf[n] != '\0') { - UART_Tx(buf[n++]); - } -} - -static bool UART_IsReceiving(void) -{ - return ((UCSRA & (1 << RXC)) ? true : false); -} - -static char UART_Rx(void) -{ - if (!UART_IsReceiving() || UDR == '\n') - return '\0'; - - return UDR; -} - -static void UART_Tx(char ch) -{ - while ((UCSRA & (1 << UDRE)) == 0); - UDR = ch; -} diff --git a/src/uart.h b/src/uart.h deleted file mode 100644 index 26edfe4..0000000 --- a/src/uart.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef MAD_UART_H -#define MAD_UART_H - -void UART_Init(void); -void UART_Read(char **buf, int size); -void UART_Write(const char *buf); - -#endif // MAD_UART_H