Compare commits

...

5 Commits

Author SHA1 Message Date
c16d9662dd Merge pull request 'uart' (#5) from uart into hdlc-interface
Reviewed-on: #5
2023-06-25 13:10:45 +00:00
Kirill Kurshakow
d3b39dbc9f fix 2023-06-07 13:02:45 +03:00
Kirill Kurshakow
bfd1776bba fix 2023-06-07 12:41:42 +03:00
Kirill Kurshakow
4e3047071d fix 2023-06-07 12:34:54 +03:00
Kirill Kurshakow
a2546d2931 uart module added 2023-06-05 18:34:34 +03:00
3 changed files with 127 additions and 0 deletions

25
main.c Normal file
View File

@ -0,0 +1,25 @@
#include <stdint.h>
#include "config.h"
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include "uart_hal.h"
int main(void)
{
//example
uint8_t data = 'A';
uart_init(9600,0);
uart_send_byte(data);
sei();
while (1)
{
if(uart_read_count() > 0){
data = uart_read();
uart_send_byte(data);
}
}
}

82
uart_module.c Normal file
View File

@ -0,0 +1,82 @@
#include "uart_hal.h"
volatile static uint8_t rx_buffer[RX_BUFFER_SIZE] = {0};
volatile static uint16_t rx_count = 0;
volatile static uint8_t uart_tx_busy = 1;
// кольцевой буффер
ISR(USART_RX_vect){
volatile static uint16_t rx_write_pos = 0;
rx_buffer[rx_write_pos] = UDR0;
rx_count++;
rx_write_pos++;
if(rx_write_pos >= RX_BUFFER_SIZE){
rx_write_pos = 0;
}
}
ISR(USART_TX_vect){
uart_tx_busy = 1;
}
void uart_init(uint32_t baud,uint8_t high_speed){
uint8_t speed = 16;
if(high_speed != 0){
speed = 8;
UCSR0A |= 1 << U2X0;
}
baud = (F_CPU/(speed*baud)) - 1;
UBRR0H = (baud & 0x0F00) >> 8;
UBRR0L = (baud & 0x00FF);
UCSR0B |= (1 << TXEN0) | (1 << RXEN0) | (1 << TXCIE0) | (1 << RXCIE0);
}
void uart_send_byte(uint8_t c){
while(uart_tx_busy == 0);
uart_tx_busy = 0;
UDR0 = c;
}
void uart_send_array(uint8_t *c,uint16_t len){
for(uint16_t i = 0; i < len;i++){
uart_send_byte(c[i]);
}
}
void uart_send_string(uint8_t *c){
uint16_t i = 0;
do{
uart_send_byte(c[i]);
i++;
}while(c[i] != '\0');
uart_send_byte(c[i]);
}
uint16_t uart_read_count(void){
return rx_count;
}
uint8_t uart_read(void){
static uint16_t rx_read_pos = 0;
uint8_t data = 0;
data = rx_buffer[rx_read_pos];
rx_read_pos++;
rx_count--;
if(rx_read_pos >= RX_BUFFER_SIZE){
rx_read_pos = 0;
}
return data;
}

20
uart_module.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef UART_HAL_H_
#define UART_HAL_H_
#include <stdint.h>
#include "config.h"
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#define RX_BUFFER_SIZE 128
void uart_init(uint32_t baud,uint8_t high_speed);
void uart_send_byte(uint8_t c);
void uart_send_array(uint8_t *c,uint16_t len);
void uart_send_string(uint8_t *c);
uint16_t uart_read_count(void);
uint8_t uart_read(void);
#endif /* UART_HAL_H_ */