Root/atusb/fw/uart.c

1/*
2 * fw/uart.h - Functions needed for debugging over uart
3 *
4 * Code adapted from http://www.roboternetz.de/wissen/index.php/UART_mit_avr-gcc
5 * and http://www.mikrocontroller.net/articles/AVR-GCC-Tutorial
6 *
7 * Published under the Creative Commons Share-Alike licence
8 * https://creativecommons.org/licenses/by-sa/2.0/de/
9 *
10 * S. Salewski 2007
11 *
12 * Adapted by
13 * Josef Filzmaier 2017
14 */
15
16#include <avr/io.h>
17#include "uart.h"
18
19#define USART_BAUD 38400UL
20#define F_CPU 8000000UL
21
22#define Wait_USART_Ready() while (!(UCSR1A & (1<<UDRE1)))
23#define UART_UBRR (F_CPU/(16L*USART_BAUD)-1)
24
25// initialize USART, 8N1 mode
26void
27uart_init(void)
28{
29/* TODO: Find a working configuration for uart for the atmega32u2 */
30#if CHIP == at90usb1287
31    CLKPR = (1 << CLKPCE);
32    CLKPR = 0; // clock prescaler == 0, so we have 16 MHz mpu frequency
33    UBRR1 = UART_UBRR;
34    UCSR1C = (1 << UCSZ10) | (1 << UCSZ11);
35    UCSR1B = (1 << TXEN1);
36    do
37    {
38        UDR1;
39    }
40    while (UCSR1A & (1 << RXC1));
41#endif
42
43}
44
45int uart_write_char(char c, FILE* stream)
46{
47    if (c == '\n'){
48        uart_new_line();
49    }
50    else {
51        Wait_USART_Ready();
52        UDR1 = c;
53    }
54    return 0;
55}
56
57void
58uart_new_line(void)
59{
60    Wait_USART_Ready();
61    UDR1 = '\r';
62    Wait_USART_Ready();
63    UDR1 = '\n';
64}
65

Archive Download this file



interactive