Added a serial0_flush_rxbuf() function

This commit is contained in:
Jake Goodwin 2023-01-14 12:59:34 -08:00
parent 3554cb60f2
commit 6c9a11828b
2 changed files with 55 additions and 12 deletions

View File

@ -7,6 +7,7 @@
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <stdint.h>
#include <util/delay.h>
#include "avr_usart.h"
@ -30,14 +31,17 @@ int main() {
uint8_t buf_len = 12;
//unsigned char data[12] = "snd 12chars\0";
unsigned char data[1] = "A";
//unsigned char data[1] = "A";
unsigned char data = '0';
while(1) {
serial0_write(data, buf_len);
_delay_ms(1000);
serial0_read(data, buf_len);
led_blink();
//serial0_write(data, buf_len);
tx_usart0(data);
//serial0_read(data, buf_len);
//data = rx_usart0();
serial0_flush_rxbuf();
serial0_read(data, 1);
//led_blink();
}
return 0;
@ -66,8 +70,6 @@ void init_usart0(void) {
UBRR0H |= (uint8_t) (0x33>>8);
UBRR0L |= (uint8_t) 0x33;
//Enable recv and Transmit pins, overrides other uses.
//IN the usart control and status register 0B
UCSR0B = (1<<RXEN0) | (1<<TXEN0);
@ -108,7 +110,13 @@ unsigned char rx_usart0(void) {
}
//now return the data
return (unsigned char) UDR0;
return UDR0;
}
void serial0_flush_rxbuf(void) {
uint8_t none;
while( UCSR0A & (1<<RXC0)) none = UDR0;
}
@ -139,13 +147,46 @@ void serial0_write(unsigned char* buffer, uint8_t write_length) {
*/
void serial0_read(unsigned char* buffer, uint8_t buf_length) {
//clear buffer out?
buffer = 0;
//buffer = 0;
for(uint8_t i = 0; i < buf_length; i++) {
buffer[i] = rx_usart0();
}
}
/*
* Inupt: A serialbuffer and length
* Output: None
* Description: Reads the serial data into a buffer of x length,
* it does error checking from the register first.
*/
void serial0_read_with_err_checking(unsigned char* buffer, uint8_t buf_length) {
//first wait for the data to be received.
while( !(UCSR0A & (1<<RXC0)) ) {
;//Equiv of continue
}
//check for errors in the register.
//Check for frame error.
if(UCSR0A & (1<<FE0)) {
led_blink();
}
//check for data over-run
else if(UCSR0A & (1<<DOR0)) {
led_blink();
led_blink();
}
//check for parity error
else if(UCSR0A & (1<<UPE0)) {
led_blink();
led_blink();
led_blink();
}
//now return the data
buffer[0] = UDR0;
}
/*
* Inupt: BUAD RATE as uint8_t

View File

@ -6,6 +6,7 @@
*/
#include <inttypes.h>
#include <stdint.h>
//#############################
//PIN DEFINES AND CONSTS
@ -48,9 +49,10 @@ unsigned char rx_usart0(void);
void serial0_write(unsigned char* buffer, uint8_t write_length);
void serial0_read(unsigned char* buffer, uint8_t buf_length);
void serial0_read_with_err_checking(unsigned char* buffer, uint8_t buf_length);
void serial0_set_baud(uint8_t baud);
void seiral0_enable_timeouts(uint8_t ms);
void serial0_flush_rxbuf(void);