Test USART reading
This commit is contained in:
3
Makefile
3
Makefile
@@ -8,6 +8,8 @@
|
|||||||
# GENERAL SETTINGS
|
# GENERAL SETTINGS
|
||||||
# ==============================================================================
|
# ==============================================================================
|
||||||
|
|
||||||
|
# NOTE: System clock frequency should be a multiple of 1.8432MHz for USART.
|
||||||
|
|
||||||
SRCDIR := src
|
SRCDIR := src
|
||||||
BINDIR := bin
|
BINDIR := bin
|
||||||
TMPDIR := $(BINDIR)/build
|
TMPDIR := $(BINDIR)/build
|
||||||
@@ -17,6 +19,7 @@ LOGFILE := $(BINDIR)/core.log
|
|||||||
|
|
||||||
VERBOSE := false
|
VERBOSE := false
|
||||||
ARCH := m32
|
ARCH := m32
|
||||||
|
#FREQ := 7372800UL
|
||||||
FREQ := 8000000UL
|
FREQ := 8000000UL
|
||||||
MCU := atmega32a
|
MCU := atmega32a
|
||||||
ASP := usbasp
|
ASP := usbasp
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#ifndef MAD_COMMON_H
|
#ifndef MAD_COMMON_H
|
||||||
#define MAD_COMMON_H
|
#define MAD_COMMON_H
|
||||||
|
|
||||||
|
#define BIT(n) (0x1U << (n))
|
||||||
#define UNUSED(s) (void)(s)
|
#define UNUSED(s) (void)(s)
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|||||||
12
src/main.c
12
src/main.c
@@ -1,5 +1,5 @@
|
|||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "uart.h"
|
#include "serial.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
@@ -17,17 +17,17 @@ int main(void)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
UART_Init();
|
USART_Init();
|
||||||
|
|
||||||
for (i = 1;; i++) {
|
for (i = 1;; i++) {
|
||||||
if (i >= 99999) i = 1;
|
if (i >= 99999) i = 1;
|
||||||
|
|
||||||
sprintf(buf, "[CORE] Fetching sensors #%05lu...\r\n", i);
|
sprintf(buf, "[CORE] Fetching sensors #%05lu...\r\n", i);
|
||||||
UART_Write(buf);
|
USART_Write(buf);
|
||||||
|
|
||||||
UART_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);
|
||||||
UART_Write(buf);
|
// USART_Write(buf);
|
||||||
|
|
||||||
Sleep(3000);
|
Sleep(3000);
|
||||||
}
|
}
|
||||||
|
|||||||
76
src/serial.c
Normal file
76
src/serial.c
Normal 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
8
src/serial.h
Normal 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
|
||||||
67
src/uart.c
67
src/uart.c
@@ -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;
|
|
||||||
}
|
|
||||||
@@ -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
|
|
||||||
Reference in New Issue
Block a user