e65fd56b840ff649b2e8752a4c41efe7a709647b
[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 #if 0 // don't want character translation for this test
41         if (c == '\n')
42                 uart_putchar('\r', stream);
43 #endif
44         loop_until_bit_is_set(UCSR0A, UDRE0);
45         UDR0 = c;
46         return 0;
47 }
48
49 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL,
50                                          _FDEV_SETUP_WRITE);
51
52 volatile uint8_t bindex = 0;
53 uint8_t buffer[80];
54 volatile uint8_t done = 0;
55
56 ISR(USART_RX_vect)
57 {
58         uint8_t b = UDR0;
59         GPIOR1 = b; // for the trace file
60         buffer[bindex++] = b;
61         buffer[bindex] = 0;
62         if (b == '\n')
63                 done++;
64 }
65
66 int main()
67 {
68         // this tell simavr to put the UART in loopback mode
69         GPIOR0 = SIMAVR_CMD_UART_LOOPBACK;
70
71         stdout = &mystdout;
72
73         UCSR0C |= (3 << UCSZ00); // 8 bits
74         // see http://www.nongnu.org/avr-libc/user-manual/group__util__setbaud.html
75 #define BAUD 38400
76 #include <util/setbaud.h>
77         UBRR0H = UBRRH_VALUE;
78         UBRR0L = UBRRL_VALUE;
79 #if USE_2X
80         UCSR0A |= (1 << U2X0);
81 #else
82         UCSR0A &= ~(1 << U2X0);
83 #endif
84
85         // enable receiver & transmitter
86         UCSR0B |= (1 << RXCIE0) | (1 << RXEN0) | (1 << TXEN0);
87
88         // this tells simavr to start the trace
89         GPIOR0 = SIMAVR_CMD_VCD_START_TRACE;
90         sei();
91         printf("Hey there, this should be received back\n");
92
93         while (!done)
94                 sleep_cpu();
95
96         cli();
97         printf("Received: %s", buffer);
98
99         // this quits the simulator, since interupts are off
100         // this is a "feature" that allows running tests cases and exit
101         sleep_cpu();
102 }