2021-11-18 19:34:41 +01:00
|
|
|
/*
|
|
|
|
* UART.c
|
|
|
|
*
|
2021-12-04 23:11:51 +01:00
|
|
|
* Created: 25/11/2021 16:26:10
|
2021-11-18 19:34:41 +01:00
|
|
|
* Author: n0x
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "UART.h"
|
|
|
|
|
2021-12-04 23:11:51 +01:00
|
|
|
#define SIZE_BUFFER 500
|
|
|
|
struct CircularBuffer{
|
|
|
|
volatile char data[SIZE_BUFFER];
|
|
|
|
volatile uint16_t Readpointer;
|
|
|
|
volatile uint16_t Writepointer;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CircularBuffer TxBuffer;
|
|
|
|
struct CircularBuffer RxBuffer;
|
|
|
|
volatile uint8_t TxActive;
|
2021-11-18 19:34:41 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
uart_init(void)
|
|
|
|
{
|
2021-12-04 23:11:51 +01:00
|
|
|
|
|
|
|
UBRR0 = 103; /* set BAUD rate to 9600 */
|
2021-11-18 19:34:41 +01:00
|
|
|
|
2021-12-04 23:11:51 +01:00
|
|
|
UCSR0C |=
|
|
|
|
(0<<UMSEL00)|(0<<UMSEL01)| /* Async UART */
|
|
|
|
(0<<UPM00)|(0<<UPM01)| /* Disable Parity */
|
|
|
|
(1<<UCSZ00)|(1<<UCSZ01)| /* 8 Bit */
|
|
|
|
(0<<USBS0); /* 1 Stopbit */
|
|
|
|
|
2021-11-18 19:34:41 +01:00
|
|
|
|
2021-12-04 23:11:51 +01:00
|
|
|
UCSR0B |=
|
|
|
|
(1<<TXEN0)|(1<<RXEN0)| /* enable send and receive */
|
|
|
|
(1<<RXCIE0); /* enable Receive interrupts */
|
2021-11-18 19:34:41 +01:00
|
|
|
|
2021-12-04 23:11:51 +01:00
|
|
|
TxBuffer.Readpointer = 0;
|
|
|
|
TxBuffer.Writepointer = 0;
|
|
|
|
RxBuffer.Readpointer = 0;
|
|
|
|
RxBuffer.Writepointer = 0;
|
2021-11-18 19:34:41 +01:00
|
|
|
}
|
|
|
|
|
2021-12-04 23:11:51 +01:00
|
|
|
/* ------------------------ */
|
|
|
|
/* Sending Data */
|
|
|
|
/* ------------------------ */
|
2021-11-18 19:34:41 +01:00
|
|
|
|
2021-12-04 23:11:51 +01:00
|
|
|
void
|
|
|
|
uart_send_string(char* string)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
while(string[i] != '\0'){
|
|
|
|
uart_send_byte(string[i]);
|
|
|
|
i++;
|
2021-11-18 19:34:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-04 23:11:51 +01:00
|
|
|
void
|
|
|
|
uart_send_byte(char c)
|
2021-11-18 19:34:41 +01:00
|
|
|
{
|
2021-12-04 23:11:51 +01:00
|
|
|
UCSR0B&=~(1<<TXCIE0);
|
2021-11-18 19:34:41 +01:00
|
|
|
|
2021-12-04 23:11:51 +01:00
|
|
|
if(TxActive){
|
|
|
|
TxBuffer.data[TxBuffer.Writepointer++] = c;
|
|
|
|
if (TxBuffer.Writepointer>=SIZE_BUFFER){
|
|
|
|
TxBuffer.Writepointer = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
TxActive = 1;
|
|
|
|
UDR0 = c;
|
|
|
|
}
|
|
|
|
UCSR0B |= (1<<TXCIE0);
|
|
|
|
}
|
|
|
|
|
|
|
|
ISR(USART0_TX_vect)
|
|
|
|
{
|
|
|
|
if(TxBuffer.Readpointer != TxBuffer.Writepointer)
|
|
|
|
{
|
|
|
|
UDR0 = TxBuffer.data[TxBuffer.Readpointer++];
|
|
|
|
|
|
|
|
if(TxBuffer.Readpointer >= SIZE_BUFFER){
|
|
|
|
TxBuffer.Readpointer = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
TxActive = 0;
|
|
|
|
}
|
2021-11-18 19:34:41 +01:00
|
|
|
}
|
|
|
|
|
2021-12-04 23:11:51 +01:00
|
|
|
/* ------------------------ */
|
|
|
|
/* Receiving Data */
|
|
|
|
/* ------------------------ */
|
2021-11-18 19:34:41 +01:00
|
|
|
|
2021-12-04 23:11:51 +01:00
|
|
|
uint8_t
|
|
|
|
uart_data_available(void)
|
2021-11-18 19:34:41 +01:00
|
|
|
{
|
2021-12-04 23:11:51 +01:00
|
|
|
uint8_t dataAvailabel = 0;
|
|
|
|
UCSR0B &= ~(1<<RXCIE0);
|
|
|
|
if(RxBuffer.Readpointer != RxBuffer.Writepointer){
|
|
|
|
dataAvailabel = 1;
|
|
|
|
}
|
|
|
|
UCSR0B |= (1<<RXCIE0);
|
|
|
|
return dataAvailabel;
|
|
|
|
}
|
|
|
|
|
|
|
|
char
|
|
|
|
uart_get_data(void)
|
|
|
|
{
|
|
|
|
char data = 0;
|
|
|
|
UCSR0B &= ~(1<<RXCIE0);
|
|
|
|
if(RxBuffer.Readpointer != RxBuffer.Writepointer)
|
|
|
|
{
|
|
|
|
data = RxBuffer.data[RxBuffer.Readpointer++];
|
|
|
|
if(RxBuffer.Readpointer >= SIZE_BUFFER){
|
|
|
|
RxBuffer.Readpointer = 0;
|
2021-11-18 19:34:41 +01:00
|
|
|
}
|
|
|
|
}
|
2021-12-04 23:11:51 +01:00
|
|
|
UCSR0B |= (1<<RXCIE0);
|
|
|
|
return data;
|
2021-11-18 19:34:41 +01:00
|
|
|
}
|
|
|
|
|
2021-12-04 23:11:51 +01:00
|
|
|
ISR(USART0_RX_vect)
|
|
|
|
{
|
|
|
|
uint8_t status = UCSR0A;
|
|
|
|
uint8_t data = UDR0;
|
|
|
|
|
|
|
|
if((status & ((1<<DOR0) | (1>>FE0))) == 0){
|
|
|
|
RxBuffer.data[RxBuffer.Writepointer++] = data;
|
|
|
|
if(RxBuffer.Writepointer >= SIZE_BUFFER) {
|
|
|
|
RxBuffer.Writepointer = 0;
|
|
|
|
}
|
|
|
|
}
|
2021-11-18 19:34:41 +01:00
|
|
|
}
|