misc: Github markdown uses underscores to italicize words
[simavr] / tests / atmega88_example.c
1 /*
2         atmega88_example.c
3
4  */
5
6 #ifndef F_CPU
7 #define F_CPU 8000000
8 #endif
9 #include <avr/io.h>
10 #include <stdio.h>
11 #include <avr/interrupt.h>
12 #include <avr/eeprom.h>
13 #include <avr/sleep.h>
14
15 /*
16  * This demonstrate how to use the avr_mcu_section.h file
17  * The macro adds a section to the ELF file with useful
18  * information for the simulator
19  */
20 #include "avr_mcu_section.h"
21 AVR_MCU(F_CPU, "atmega88");
22
23 /*
24  * This small section tells simavr to generate a VCD trace dump with changes to these
25  * registers.
26  * Opening it with gtkwave will show you the data being pumped out into the data register
27  * UDR0, and the UDRE0 bit being set, then cleared
28  */
29 const struct avr_mmcu_vcd_trace_t _mytrace[]  _MMCU_ = {
30         { AVR_MCU_VCD_SYMBOL("UDR0"), .what = (void*)&UDR0, },  
31         { AVR_MCU_VCD_SYMBOL("UDRE0"), .mask = (1 << UDRE0), .what = (void*)&UCSR0A, }, 
32 };
33
34
35 /* declare this in a .eeprom ELF section */
36 uint32_t value EEMEM = 0xdeadbeef;
37
38 static int uart_putchar(char c, FILE *stream) {
39         if (c == '\n')
40                 uart_putchar('\r', stream);
41         loop_until_bit_is_set(UCSR0A, UDRE0);
42         UDR0 = c;
43         return 0;
44 }
45
46 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL,
47                                          _FDEV_SETUP_WRITE);
48
49
50 int main()
51 {
52         stdout = &mystdout;
53
54         // read the eeprom value
55         uint32_t c = eeprom_read_dword((void*)&value);
56         printf("Read from eeprom 0x%08lx -- should be 0xdeadbeef\n", c);
57         // change the eeprom
58         eeprom_write_dword((void*)&value, 0xcafef00d);
59         // re-read it
60         c = eeprom_read_dword((void*)&value);
61         printf("Read from eeprom 0x%08lx -- should be 0xcafef00d\n", c);
62
63         // this quits the simulator, since interupts are off
64         // this is a "feature" that allows running tests cases and exit
65         sleep_cpu();
66 }