Rename USART module to UART to emphasize asynchronous mode operation

This commit is contained in:
2024-10-01 22:15:19 +02:00
parent 7133f54617
commit 8d94bd87a2
6 changed files with 32 additions and 58 deletions

View File

@@ -1,24 +1,24 @@
#include "common.h"
#include "bus/usart.h"
#include "bus/uart.h"
#include <avr/io.h>
#include <avr/interrupt.h>
#define USART_BAUDRATE 9600
#define USART_RXBUF_SIZE 128
#define USART_TXBUF_SIZE 128
#define UART_BAUDRATE 9600
#define UART_RXBUF_SIZE 128
#define UART_TXBUF_SIZE 128
#define USART_RXBUF_MASK (USART_RXBUF_SIZE - 1)
#define USART_TXBUF_MASK (USART_TXBUF_SIZE - 1)
#define USART_BAUD_PRESCALE ((((F_CPU / 16) + \
(USART_BAUDRATE / 2)) / (USART_BAUDRATE)) - 1)
#define UART_RXBUF_MASK (UART_RXBUF_SIZE - 1)
#define UART_TXBUF_MASK (UART_TXBUF_SIZE - 1)
#define UART_BAUD_PRESCALE ((((F_CPU / 16) + \
(UART_BAUDRATE / 2)) / (UART_BAUDRATE)) - 1)
static volatile char rxbuf[USART_RXBUF_SIZE]; // RX ring buffer
static volatile char txbuf[USART_TXBUF_SIZE]; // TX ring buffer
static volatile char rxbuf[UART_RXBUF_SIZE]; // RX ring buffer
static volatile char txbuf[UART_TXBUF_SIZE]; // TX ring buffer
static volatile short rxhead, txhead; // Current write position
static volatile short rxtail, txtail; // Current read position
int USART_Init(void)
int UART_Init(void)
{
rxhead = 0;
rxtail = 0;
@@ -28,29 +28,29 @@ int USART_Init(void)
UCSR0B = BIT(RXCIE0); // Handle RXC interrupts
UCSR0B |= BIT(RXEN0) | BIT(TXEN0); // Enable RX and TX circuitry
UCSR0C = BIT(UCSZ01) | BIT(UCSZ00); // 8-bit data, 1-bit stop, no parity
UBRR0H = (USART_BAUD_PRESCALE >> 8); // Set baud rate upper byte
UBRR0L = USART_BAUD_PRESCALE; // Set baud rate lower byte
UBRR0H = (UART_BAUD_PRESCALE >> 8); // Set baud rate upper byte
UBRR0L = UART_BAUD_PRESCALE; // Set baud rate lower byte
return 0;
}
char USART_Getc(void)
char UART_Getc(void)
{
if (rxhead == rxtail) {
return -1;
}
rxtail = (rxtail + 1) & USART_RXBUF_MASK;
rxtail = (rxtail + 1) & UART_RXBUF_MASK;
return rxbuf[rxtail];
}
void USART_Putc(char ch)
void UART_Putc(char ch)
{
short head;
// Wrap around if end of buffer reached
head = (txhead + 1) & USART_TXBUF_MASK;
head = (txhead + 1) & UART_TXBUF_MASK;
while (head == txtail); // Wait for space
txbuf[head] = ch;
@@ -69,7 +69,7 @@ ISR(USART0_RX_vect)
data = UDR0; // Next byte ready
// Wrap around if end of buffer reached
head = (rxhead + 1) & USART_RXBUF_MASK;
head = (rxhead + 1) & UART_RXBUF_MASK;
// Free space in RX buffer?
// Otherwise discard overflow
@@ -87,7 +87,7 @@ ISR(USART0_UDRE_vect)
// Anything in TX buffer?
if (txhead != txtail) {
// Write next byte to data register
tail = (txtail + 1) & USART_TXBUF_MASK;
tail = (txtail + 1) & UART_TXBUF_MASK;
UDR0 = txbuf[tail];
txtail = tail;
} else {

8
src/bus/uart.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef MAD_CORE_BUS_UART_H
#define MAD_CORE_BUS_UART_H
int UART_Init(void);
void UART_Putc(char ch);
char UART_Getc(void);
#endif // MAD_CORE_BUS_UART_H

View File

@@ -1,8 +0,0 @@
#ifndef MAD_CORE_BUS_USART_H
#define MAD_CORE_BUS_USART_H
int USART_Init(void);
void USART_Putc(char ch);
char USART_Getc(void);
#endif // MAD_CORE_BUS_USART_H

View File

@@ -1,5 +1,5 @@
#include "common.h"
#include "bus/usart.h"
#include "bus/uart.h"
#include <stdio.h>
@@ -42,7 +42,7 @@ void Error(const char *fmt, ...)
static void Puts(const char *str)
{
while (*str != '\0') {
USART_Putc(*str++);
UART_Putc(*str++);
}
}

View File

@@ -1,5 +1,5 @@
#include "common.h"
#include "bus/usart.h"
#include "bus/uart.h"
#include "bus/mosfet.h"
#include "bus/pwm.h"
#include "bus/i2c.h"
@@ -52,7 +52,7 @@ static int Init(void)
// IRQs, so we need to initialize it as soon as
// possible and make sure to enable interrupts.
USART_Init();
UART_Init();
sei();
Info("Initializing...");
@@ -134,7 +134,7 @@ static void Update(void)
cmd_t cmd;
// Parse serial commands
while ((ch = USART_Getc()) >= 0) {
while ((ch = UART_Getc()) >= 0) {
if (!CMD_Parse(ch, &cmd)) {
continue;
}