cores: Move static to front of declaration
[simavr] / tests / atmega88_uart_echo.c
1 /*
2         atmega88_uart_echo.c
3
4         This test case enables uart RX interupts, does a "printf" and then receive characters
5         via the interupt handler until it reaches a \r.
6
7         This tests the uart reception fifo system. It relies on the uart "irq" input and output
8         to be wired together (see simavr.c)
9  */
10
11 #include <avr/io.h>
12 #include <stdio.h>
13 #include <avr/interrupt.h>
14 #include <avr/eeprom.h>
15 #include <avr/sleep.h>
16
17 /*
18  * This demonstrate how to use the avr_mcu_section.h file
19  * The macro adds a section to the ELF file with useful
20  * information for the simulator
21  */
22 #include "avr_mcu_section.h"
23 AVR_MCU(F_CPU, "atmega88");
24 // tell simavr to listen to commands written in this (unused) register
25 AVR_MCU_SIMAVR_COMMAND(&GPIOR0);
26
27 /*
28  * This small section tells simavr to generate a VCD trace dump with changes to these
29  * registers.
30  * Opening it with gtkwave will show you the data being pumped out into the data register
31  * UDR0, and the UDRE0 bit being set, then cleared
32  */
33 const struct avr_mmcu_vcd_trace_t _mytrace[]  _MMCU_ = {
34         { AVR_MCU_VCD_SYMBOL("UDR0"), .what = (void*)&UDR0, },
35         { AVR_MCU_VCD_SYMBOL("UDRE0"), .mask = (1 << UDRE0), .what = (void*)&UCSR0A, },
36         { AVR_MCU_VCD_SYMBOL("GPIOR1"), .what = (void*)&GPIOR1, },
37 };
38
39 static int uart_putchar(char c, FILE *stream)
40 {
41         if (c == '\n')
42                 uart_putchar('\r', stream);
43         loop_until_bit_is_set(UCSR0A, UDRE0);
44         UDR0 = c;
45         return 0;
46 }
47
48 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL,
49                                          _FDEV_SETUP_WRITE);
50
51 volatile uint8_t bindex = 0;
52 uint8_t buffer[80];
53 volatile uint8_t done = 0;
54
55 ISR(USART_RX_vect)
56 {
57         uint8_t b = UDR0;
58         GPIOR1 = b; // for the trace file
59         buffer[bindex++] = b;
60         buffer[bindex] = 0;
61         if (b == '\n')
62                 done++;
63 }
64
65 int main()
66 {
67         // this tell simavr to put the UART in loopback mode
68         GPIOR0 = SIMAVR_CMD_UART_LOOPBACK;
69
70         stdout = &mystdout;
71
72         UCSR0C |= (3 << UCSZ00); // 8 bits
73         // see http://www.nongnu.org/avr-libc/user-manual/group__util__setbaud.html
74 #define BAUD 38400
75 #include <util/setbaud.h>
76         UBRR0H = UBRRH_VALUE;
77         UBRR0L = UBRRL_VALUE;
78 #if USE_2X
79         UCSR0A |= (1 << U2X0);
80 #else
81         UCSR0A &= ~(1 << U2X0);
82 #endif
83
84         // enable receiver & transmitter
85         UCSR0B |= (1 << RXCIE0) | (1 << RXEN0) | (1 << TXEN0);
86
87         // this tells simavr to start the trace
88         GPIOR0 = SIMAVR_CMD_VCD_START_TRACE;
89         sei();
90         printf("Hey there, this should be received back\n");
91
92         while (!done)
93                 sleep_cpu();
94
95         cli();
96         printf("Received: %s", buffer);
97
98         // this quits the simulator, since interupts are off
99         // this is a "feature" that allows running tests cases and exit
100         sleep_cpu();
101 }