Test USART reading

This commit is contained in:
2024-08-27 00:38:12 +02:00
parent 42792bfdf8
commit 0f5a383f14
7 changed files with 94 additions and 81 deletions

View File

@@ -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

View File

@@ -1,6 +1,7 @@
#ifndef MAD_COMMON_H
#define MAD_COMMON_H
#define BIT(n) (0x1U << (n))
#define UNUSED(s) (void)(s)
#include <stdlib.h>

View File

@@ -1,5 +1,5 @@
#include "common.h"
#include "uart.h"
#include "serial.h"
#include <stdio.h>
@@ -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);
}

76
src/serial.c Normal file
View File

@@ -0,0 +1,76 @@
#include "common.h"
#include "serial.h"
#include <avr/io.h>
#include <stdio.h>
#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;
}

8
src/serial.h Normal file
View File

@@ -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

View File

@@ -1,67 +0,0 @@
#include "common.h"
#include <avr/io.h>
#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;
}

View File

@@ -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