remove generic uart driver, integrate specialized uart handling into main

This commit is contained in:
Nero 2017-05-25 22:33:45 +00:00 committed by Nero
parent e81210ae82
commit cf86bd6cf2
4 changed files with 29 additions and 102 deletions

View File

@ -5,7 +5,7 @@ MCU_TTY_BAUD = 57600
MCU_PROGRAMMER = arduino MCU_PROGRAMMER = arduino
PRG = main PRG = main
OBJ = main.o uart.o OBJ = main.o
OPTIMIZE = -O2 OPTIMIZE = -O2
DEFS = -DF_CPU=$(MCU_FREQ) -DBAUDRATE=$(MCU_TTY_BAUD) DEFS = -DF_CPU=$(MCU_FREQ) -DBAUDRATE=$(MCU_TTY_BAUD)

43
main.c
View File

@ -2,24 +2,14 @@
#include <avr/io.h> #include <avr/io.h>
#include <util/delay.h> #include <util/delay.h>
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include "uart.h"
#define BAUD_PRESCALE (((F_CPU/(BAUDRATE*8UL)))-1)
/* Button matrix between PB3-PB4, PC0-PC3 /* Button matrix between PB3-PB4, PC0-PC3
* PC0-PC3 are inputs, default pull-up * PC0-PC3 are inputs, default pull-up
* PB3-PB4 are pulled to ground (alternating) while the above are read * PB3-PB4 are pulled to ground (alternating) while the above are read
*/ */
void print_nibble(uint8_t nibble) {
nibble += 0x30;
if (nibble > 0x39) nibble += 7;
uart_putc(nibble);
}
void print_int8hex(uint8_t val) {
print_nibble(val >> 4);
print_nibble(val & 0x0F);
}
uint8_t getButtonState() { uint8_t getButtonState() {
uint8_t val = 0; uint8_t val = 0;
@ -79,6 +69,23 @@ void setrelay(uint8_t n, uint8_t s) {
} }
} }
char umode = 't';
ISR(USART_RX_vect) {
uint8_t c = UDR0;
if (c == 't' || c == 'e' || c == 'd') {
umode = c;
} else if (c >= '0' && c <= '7') {
c = c - '0';
if (umode == 'e') {
setrelay(c,1);
} else if (umode == 'd') {
setrelay(c,0);
} else if (umode == 't') {
setrelay(c,1 - getrelay(c));
}
}
}
int main() { int main() {
DDRC &= ~(0b1111); // This sets PC0 to PC3 to input DDRC &= ~(0b1111); // This sets PC0 to PC3 to input
PORTC |= 0b1111; // This makes dito pull-up per default PORTC |= 0b1111; // This makes dito pull-up per default
@ -92,7 +99,16 @@ int main() {
PORTD |= 0b11111100; PORTD |= 0b11111100;
PORTB |= 0b00000011; 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(); sei();
// Now handle button inputs
uint8_t c,p,d,i; uint8_t c,p,d,i;
p = 0; p = 0;
@ -103,9 +119,6 @@ int main() {
for (i = 0; i<8; i++) { for (i = 0; i<8; i++) {
if (d & c & 0x80) { if (d & c & 0x80) {
setrelay(i, 1 - getrelay(i)); setrelay(i, 1 - getrelay(i));
print_nibble(i);
uart_putc('\r');
uart_putc('\n');
} }
d = d << 1; d = d << 1;
c = c << 1; c = c << 1;

82
uart.c
View File

@ -1,82 +0,0 @@
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/atomic.h>
#include "uart.h"
#define UART_BUFSIZE 64
typedef volatile struct {
char data[UART_BUFSIZE];
uint8_t r;
uint8_t w;
} buf;
buf __attribute__ ((section (".noinit"))) uart_rxbuf;
buf __attribute__ ((section (".noinit"))) uart_txbuf;
#define BAUD_PRESCALE (((F_CPU/(BAUDRATE*8UL)))-1)
void __attribute__ ((naked)) __attribute__ ((section (".init5"))) uart_init() {
uart_rxbuf.r = 0;
uart_rxbuf.w = 0;
uart_txbuf.r = 0;
uart_txbuf.w = 0;
UCSR0A = (1<<U2X0);
UCSR0B = (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0);
UCSR0C = (1<<UCSZ00) | (1<<UCSZ01); // 8N1
UBRR0 = BAUD_PRESCALE;
}
void __attribute__ ((naked)) __attribute__ ((section (".fini5"))) uart_deinit() {
while ((UCSR0B & (1 << UDRIE0))) {};
while (!(UCSR0A & (1 << TXC0))) {};
}
// RX Facilities
ISR(USART_RX_vect) {
uint8_t c = UDR0;
if (((uart_rxbuf.w - uart_rxbuf.r) & (UART_BUFSIZE - 1)) == 1) return;
uart_rxbuf.data[uart_rxbuf.w++] = c;
uart_rxbuf.w = uart_rxbuf.w & (UART_BUFSIZE - 1);
}
uint8_t uart_getc() {
uint8_t c;
while (uart_rxbuf.w == uart_rxbuf.r) {};
// Disable interrupt to avoid race condition
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
c = uart_rxbuf.data[uart_rxbuf.r++];
uart_rxbuf.r = uart_rxbuf.r & (UART_BUFSIZE - 1);
}
return c;
}
// TX Facilities
ISR(USART_UDRE_vect) {
if ((uart_txbuf.w != uart_txbuf.r) && (UCSR0B & (1<<TXEN0))) {
UDR0 = uart_txbuf.data[uart_txbuf.r++];
uart_txbuf.r = uart_txbuf.r & (UART_BUFSIZE - 1);
} else {
UCSR0B &= ~(1<<UDRIE0); // UDRE off
}
}
uint8_t uart_putc(unsigned char c) {
while (((uart_txbuf.r - uart_txbuf.w) & (UART_BUFSIZE - 1)) == 1) {};
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
uart_txbuf.data[uart_txbuf.w++] = c;
uart_txbuf.w = uart_txbuf.w & (UART_BUFSIZE - 1);
}
UCSR0B |= (1<<UDRIE0); // UDRE on
return 1;
}

4
uart.h
View File

@ -1,4 +0,0 @@
#include <stdint.h>
uint8_t uart_getc();
uint8_t uart_putc(unsigned char c);