Handle USART_RXC interrupt

This commit is contained in:
2024-08-27 01:33:39 +02:00
parent 0f5a383f14
commit 382b4770b0
3 changed files with 35 additions and 42 deletions

View File

@@ -2,6 +2,7 @@
#include "serial.h" #include "serial.h"
#include <stdio.h> #include <stdio.h>
#include <avr/interrupt.h>
#define MAX_CMDBUF 256 #define MAX_CMDBUF 256
@@ -17,6 +18,10 @@ int main(void)
return 1; return 1;
} }
// Handle USART_RXC interrupt
UCSRB |= (1 << RXCIE);
sei();
USART_Init(); USART_Init();
for (i = 1;; i++) { for (i = 1;; i++) {
@@ -24,6 +29,7 @@ int main(void)
sprintf(buf, "[CORE] Fetching sensors #%05lu...\r\n", i); sprintf(buf, "[CORE] Fetching sensors #%05lu...\r\n", i);
USART_Write(buf); USART_Write(buf);
USART_Update();
// USART_Read(&cmd, MAX_CMDBUF); // USART_Read(&cmd, MAX_CMDBUF);
// sprintf(buf, "[CORE] Checking cmdbuf=\"%s\"\r\n", cmd); // sprintf(buf, "[CORE] Checking cmdbuf=\"%s\"\r\n", cmd);

View File

@@ -1,18 +1,21 @@
#include "common.h" #include "common.h"
#include "serial.h" #include "serial.h"
#include <avr/io.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdio.h> #include <stdio.h>
#define USART_BAUDRATE 9600 #define USART_BAUDRATE 9600
// TODO: Use ring buffer! https://stackoverflow.com/a/1771607
// Ensure closest fit for target baudrate // Ensure closest fit for target baudrate
//#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1) //#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
#define BAUD_PRESCALE ((((F_CPU / 16) + (USART_BAUDRATE / 2)) \ #define BAUD_PRESCALE ((((F_CPU / 16) + (USART_BAUDRATE / 2)) \
/ (USART_BAUDRATE)) - 1) / (USART_BAUDRATE)) - 1)
static char USART_Rx(void); static size_t cmdlen;
static void USART_Tx(char ch); static char cmdbuf[256];
void USART_Init(void) void USART_Init(void)
{ {
@@ -22,31 +25,11 @@ void USART_Init(void)
UBRRL = BAUD_PRESCALE; // Baud rate lower byte UBRRL = BAUD_PRESCALE; // Baud rate lower byte
} }
void USART_Read(char **buf, int size) void USART_Update(void)
{ {
int n = 0; char buf[512];
char *p = *buf; sprintf(buf, "[CORE] CMDBUF='%s'\r\n", cmdbuf);
char ch; USART_Write(buf);
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) void USART_Write(const char *buf)
@@ -54,23 +37,26 @@ void USART_Write(const char *buf)
int n = 0; int n = 0;
while (buf[n] != '\0') { while (buf[n] != '\0') {
USART_Tx(buf[n++]); // Wait until UDR can receive data
while ((UCSRA & BIT(UDRE)) == 0);
UDR = buf[n++];
} }
} }
static char USART_Rx(void) // USART RX Interrupt
ISR(USART_RXC_vect)
{ {
// Anything to read from UDR? char ch;
if ((UCSRA & BIT(RXC)) == 0)
return '\0';
return UDR; ch = UDR;
}
// Exceeded buffer capacity?
static void USART_Tx(char ch) if (cmdlen >= sizeof(cmdbuf)) {
{ if (ch == '\n') {
// Wait until UDR can receive data cmdlen = 0;
while ((UCSRA & BIT(UDRE)) == 0); }
return;
UDR = ch; }
cmdbuf[cmdlen++] = ch;
} }

View File

@@ -2,7 +2,8 @@
#define MAD_SERIAL_H #define MAD_SERIAL_H
void USART_Init(void); void USART_Init(void);
void USART_Read(char **buf, int size); void USART_Update(void);
//void USART_Read(char **buf, int size);
void USART_Write(const char *buf); void USART_Write(const char *buf);
#endif // MAD_SERIAL_H #endif // MAD_SERIAL_H