Factor uart code into separate driver code

This commit is contained in:
Nero 2018-01-29 00:50:22 +00:00 committed by Nero
parent 24ee77720d
commit 122c0e6c35
5 changed files with 31 additions and 14 deletions

10
drivers/uart.c Normal file
View File

@ -0,0 +1,10 @@
#include "uart.h"
#include <avr/io.h>
#define BAUD_PRESCALE (((F_CPU/(BAUDRATE*8UL)))-1)
void __attribute__ ((naked)) __attribute__ ((section (".init5"))) uart_init() {
UCSR0A = (1<<U2X0);
UCSR0C = (1<<UCSZ00) | (1<<UCSZ01); // 8N1
UBRR0 = BAUD_PRESCALE;
}

0
drivers/uart.h Normal file
View File

16
drivers/uart_rx_async.c Normal file
View File

@ -0,0 +1,16 @@
#include "uart_rx_async.h"
#include "uart.h"
#include <avr/io.h>
#include <avr/interrupt.h>
void __attribute__ ((naked)) __attribute__ ((section (".init6"))) uart_rx_async_init() {
UCSR0B = (1<<RXEN0) | (1<<RXCIE0);
}
void __attribute__ ((naked)) __attribute__ ((section (".init8"))) uart_rx_sei() {
sei();
}
ISR(USART_RX_vect) {
uart_process(UDR0);
}

3
drivers/uart_rx_async.h Normal file
View File

@ -0,0 +1,3 @@
#include <stdint.h>
extern void uart_process(uint8_t c);

View File

@ -1,9 +1,7 @@
#include <stdio.h>
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#define BAUD_PRESCALE (((F_CPU/(BAUDRATE*8UL)))-1)
#include "drivers/uart_rx_async.h"
/* Button matrix between PB3-PB4, PC0-PC3
* PC0-PC3 are inputs, default pull-up
@ -70,8 +68,7 @@ void setrelay(uint8_t n, uint8_t s) {
}
char umode = 't';
ISR(USART_RX_vect) {
uint8_t c = UDR0;
void uart_process(uint8_t c) {
if (c == 't' || c == 'e' || c == 'd') {
umode = c;
} else if (c >= '0' && c <= '7') {
@ -99,15 +96,6 @@ int main() {
PORTD |= 0b11111100;
PORTB |= 0b00000011;
// Init uart
UCSR0A = (1<<U2X0);
// Transmit is enabled to set PIN levels, but we will never write
UCSR0B = (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0);
UCSR0C = (1<<UCSZ00) | (1<<UCSZ01); // 8N1
UBRR0 = BAUD_PRESCALE;
sei();
// Now handle button inputs
uint8_t c,p,d,i;
p = 0;