inv-sw-32u4/src/uart.c

51 lines
888 B
C

#include <avr/io.h>
#include <avr/interrupt.h>
#include "include/uart.h"
#include "include/commands.h"
static FILE stream = FDEV_SETUP_STREAM(uart_printf, NULL, _FDEV_SETUP_WRITE);
void uart_init(unsigned int baud)
{
stdout = &stream;
/* Set baud rate */
UBRR1H = (unsigned char)(baud>>8);
UBRR1L = (unsigned char)baud;
/* Enable receiver and transmitter */
UCSR1B = (1<<RXEN1)|(1<<TXEN1);
/*Enable the receive interrupt*/
UCSR1B |= (1 << RXCIE1);
/* Set frame format */
UCSR1C |= (3<<UCSZ10); // 8 data bits
// UCSR1C &= ~(1<<USBS1); // 1 stop bit
}
void uart_sendByte(uint8_t byte)
{
/* Wait for empty transmit buffer */
while (!(UCSR1A & (1<<UDRE1)))
{
}
/* Put data into buffer, sends the data */
UDR1 = byte;
}
int uart_printf(char var, FILE *stream)
{
uart_sendByte(var);
return 0;
}
ISR(USART1_RX_vect)
{
char u;
u = UDR1;
cmd_collect_char(u);
}