Initial Commit
[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 /* declare this in a .eeprom ELF section */
24 uint32_t value EEMEM = 0xdeadbeef;
25
26 static int uart_putchar(char c, FILE *stream) {
27   if (c == '\n')
28     uart_putchar('\r', stream);
29   loop_until_bit_is_set(UCSR0A, UDRE0);
30   UDR0 = c;
31   return 0;
32 }
33
34 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL,
35                                          _FDEV_SETUP_WRITE);
36
37
38 int main()
39 {
40         stdout = &mystdout;
41
42         // read the eeprom value
43         uint32_t c = eeprom_read_dword((void*)&value);
44         printf("Read from eeprom 0x%08lx -- should be 0xdeadbeef\n", c);
45         // change the eeprom
46         eeprom_write_dword((void*)&value, 0xcafef00d);
47         // re-read it
48         c = eeprom_read_dword((void*)&value);
49         printf("Read from eeprom 0x%08lx -- should be 0xcafef00d\n", c);
50
51         // this quits the simulator, since interupts are off
52         // this is a "feature" that allows running tests cases and exit
53         sleep_cpu();
54 }