Fixed issues with UART and added proper interupts

This commit is contained in:
_N0x 2021-12-04 23:11:51 +01:00
parent 4f5b174bb1
commit 4021359230
2 changed files with 118 additions and 92 deletions

View File

@ -1,106 +1,132 @@
/*
* UART.c
*
* Created: 18/11/2021 17:13:25
* Created: 25/11/2021 16:26:10
* Author: n0x
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <string.h>
#include "UART.h"
volatile int enabled = 1;
#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;
/* Schreiben Sie eine Funktion, welche die UART Schnittstelle für das Senden von
Daten mit
9600Baud,
8 Bit,
keine Parität und
1 Stopbit
initialisiert. */
void
uart_init(void)
{
/* set BAUD rate to 9600 */
UBRR0 = 103;
/* Async UART */
UCSR0C |= (0<<UMSEL00)|(0<<UMSEL01);
UBRR0 = 103; /* set BAUD rate to 9600 */
/* Disable Parity */
UCSR0C |= (0<<UPM00)|(0<<UPM01);
UCSR0C |=
(0<<UMSEL00)|(0<<UMSEL01)| /* Async UART */
(0<<UPM00)|(0<<UPM01)| /* Disable Parity */
(1<<UCSZ00)|(1<<UCSZ01)| /* 8 Bit */
(0<<USBS0); /* 1 Stopbit */
/* 8 Bit */
UCSR0C |= (1<<UCSZ00)|(1<<UCSZ01);
/* 1 Stopbit */
UCSR0C |= (0<<USBS0);
UCSR0B |=
(1<<TXEN0)|(1<<RXEN0)| /* enable send and receive */
(1<<RXCIE0); /* enable Receive interrupts */
/* enable send */
UCSR0B = (1<<TXEN0);
TxBuffer.Readpointer = 0;
TxBuffer.Writepointer = 0;
RxBuffer.Readpointer = 0;
RxBuffer.Writepointer = 0;
}
/* Schreiben Sie eine Funktion, welche einen String übergeben bekommt, und diesen
auf der UART Schnittstelle ausgibt. */
/* ------------------------ */
/* Sending Data */
/* ------------------------ */
void
uart_send (char* string){
int len = strlen(string);
for(int i = 0; i < len; i++){
while (!( UCSR0A & (1<<UDRE0)) );
UDR0 = string[i];
uart_send_string(char* string)
{
int i = 0;
while(string[i] != '\0'){
uart_send_byte(string[i]);
i++;
}
}
/* Schreiben Sie eine Funktion, welche die UART Schnittstelle für das Senden von Daten mit
9600Baud,
8 Bit,
keine Parität und
1 Stoppbit
initialisiert. Nutzen Sie dabei ein Uart-Interrupt. */
void
uart_init_isr(void)
uart_send_byte(char c)
{
/* set BAUD rate to 9600 */
UBRR0 = 103;
UCSR0B&=~(1<<TXCIE0);
/* Async UART */
UCSR0C |= (0<<UMSEL00)|(0<<UMSEL01);
/* Disable Parity */
UCSR0C |= (0<<UPM00)|(0<<UPM01);
/* 8 Bit */
UCSR0C |= (1<<UCSZ00)|(1<<UCSZ01);
/* 1 Stopbit */
UCSR0C |= (0<<USBS0);
/* enable send */
UCSR0B |= (1<<TXEN0);
/* enable interrupt */
UCSR0B |= (1<<UDRIE0);
if(TxActive){
TxBuffer.data[TxBuffer.Writepointer++] = c;
if (TxBuffer.Writepointer>=SIZE_BUFFER){
TxBuffer.Writepointer = 0;
}
} else {
TxActive = 1;
UDR0 = c;
}
UCSR0B |= (1<<TXCIE0);
}
/* Schreiben Sie eine Funktion, welche einen String übergeben bekommt, und diesen
auf der UART Schnittstelle ausgibt. (Interrupts) */
void
uart_send_isr(char* string)
ISR(USART0_TX_vect)
{
int i = 0;
while( i < strlen(string)){
if(enabled){
UDR0 = string[i];
i++;
enabled = 0;
if(TxBuffer.Readpointer != TxBuffer.Writepointer)
{
UDR0 = TxBuffer.data[TxBuffer.Readpointer++];
if(TxBuffer.Readpointer >= SIZE_BUFFER){
TxBuffer.Readpointer = 0;
}
} else {
TxActive = 0;
}
}
/* ------------------------ */
/* Receiving Data */
/* ------------------------ */
uint8_t
uart_data_available(void)
{
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;
}
}
UCSR0B |= (1<<RXCIE0);
return data;
}
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;
}
}
}
ISR(USART0_UDRE_vect){
enabled = 1;
}

View File

@ -1,21 +1,21 @@
/*
* UART.h
* UART2.h
*
* Created: 18/11/2021 17:13:16
* Created: 25/11/2021 16:25:59
* Author: n0x
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#ifndef UART_H_
#define UART_H_
#ifndef UART2_H_
#define UART2_H_
void uart_init(void);
void uart_send(char* string);
void uart_send_byte(char c);
void uart_send_string(char* string);
uint8_t uart_data_available(void);
char uart_get_data(void);
void uart_init_isr(void);
void uart_send_isr(char* string);
int checkIfBlocked(void);
#endif /* UART_H_ */
#endif /* UART2_H_ */