diff options
author | Lars-Dominik Braun <lars@6xq.net> | 2014-02-13 16:45:48 +0100 |
---|---|---|
committer | Lars-Dominik Braun <lars@6xq.net> | 2014-02-13 16:45:48 +0100 |
commit | 9a5753f23fb7d55a7a72c8cf9846cc72349c65de (patch) | |
tree | 81ffaf7f1f5673880fa17546cc8ff3dae08345b9 /uart.c | |
parent | ab0e37395d2390727248361f00f37109fd6bde9c (diff) | |
download | hourglass-9a5753f23fb7d55a7a72c8cf9846cc72349c65de.tar.gz hourglass-9a5753f23fb7d55a7a72c8cf9846cc72349c65de.tar.bz2 hourglass-9a5753f23fb7d55a7a72c8cf9846cc72349c65de.zip |
Split up main.c
Diffstat (limited to 'uart.c')
-rw-r--r-- | uart.c | 46 |
1 files changed, 46 insertions, 0 deletions
@@ -0,0 +1,46 @@ +#include <stdio.h> +#include <avr/io.h> + +#include "uart.h" + +/* blocking uart send + */ +static void uartSend (unsigned char data) { + /* Wait for empty transmit buffer */ + while (!( UCSR0A & (1<<UDRE0))); + /* Put data into buffer, sends the data */ + UDR0 = data; +} + +static int uartPutc (char c, FILE *stream) { + if (c == '\n') { + uartSend ('\r'); + } + uartSend (c); + return 0; +} + +static FILE mystdout = FDEV_SETUP_STREAM (uartPutc, NULL, _FDEV_SETUP_WRITE); + +void uartInit () { + /* Set baud rate (9600, double speed, at 1mhz) */ + UBRR0H = 0; + UBRR0L = 12; + /* enable double speed mode */ + UCSR0A = (1 << U2X0); + /* Enable receiver and transmitter */ + UCSR0B = (1<<RXEN0)|(1<<TXEN0); + /* Set frame format: 8 data, 1 stop bit, even parity */ + UCSR0C = (1<<UPM01) | (0 << UPM00) | (0<<USBS0)|(3<<UCSZ00); + + /* redirect stdout */ + stdout = &mystdout; +} + +static unsigned char uartReceive () { + /* Wait for data to be received */ + while ( !(UCSR0A & (1<<RXC0)) ); + /* Get and return received data from buffer */ + return UDR0; +} + |