i2ctest: Collapsed both i2ctests into one
[simavr] / examples / board_i2ctest / i2ctest.c
1 /*
2         i2ctest.c
3
4         Copyright 2008-2011 Michel Pollet <buserror@gmail.com>
5
6         This file is part of simavr.
7
8         simavr is free software: you can redistribute it and/or modify
9         it under the terms of the GNU General Public License as published by
10         the Free Software Foundation, either version 3 of the License, or
11         (at your option) any later version.
12
13         simavr is distributed in the hope that it will be useful,
14         but WITHOUT ANY WARRANTY; without even the implied warranty of
15         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16         GNU General Public License for more details.
17
18         You should have received a copy of the GNU General Public License
19         along with simavr.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <libgen.h>
25 #include <pthread.h>
26
27 #include "sim_avr.h"
28 #include "avr_twi.h"
29 #include "sim_elf.h"
30 #include "sim_gdb.h"
31 #include "sim_vcd_file.h"
32 #include "i2c_eeprom.h"
33
34 avr_t * avr = NULL;
35 avr_vcd_t vcd_file;
36
37 i2c_eeprom_t ee;
38
39 int main(int argc, char *argv[])
40 {
41         elf_firmware_t f;
42         const char * fname =  "atmega1280_i2ctest.axf";
43
44         printf("Firmware pathname is %s\n", fname);
45         elf_read_firmware(fname, &f);
46
47         printf("firmware %s f=%d mmcu=%s\n", fname, (int)f.frequency, f.mmcu);
48
49         avr = avr_make_mcu_by_name(f.mmcu);
50         if (!avr) {
51                 fprintf(stderr, "%s: AVR '%s' now known\n", argv[0], f.mmcu);
52                 exit(1);
53         }
54         avr_init(avr);
55         avr_load_firmware(avr, &f);
56
57         // initialize our 'peripheral'
58         i2c_eeprom_init(avr, &ee, 0xa0, 0xfe, NULL, 1024);
59
60         i2c_eeprom_attach(avr, &ee, AVR_IOCTL_TWI_GETIRQ(0));
61         ee.verbose = 1;
62
63         // even if not setup at startup, activate gdb if crashing
64         avr->gdb_port = 1234;
65         if (0) {
66                 //avr->state = cpu_Stopped;
67                 avr_gdb_init(avr);
68         }
69
70         /*
71          *      VCD file initialization
72          *
73          *      This will allow you to create a "wave" file and display it in gtkwave
74          *      Pressing "r" and "s" during the demo will start and stop recording
75          *      the pin changes
76          */
77 //      avr_vcd_init(avr, "gtkwave_output.vcd", &vcd_file, 100000 /* usec */);
78 //      avr_vcd_add_signal(&vcd_file,
79 //              avr_io_getirq(avr, AVR_IOCTL_TWI_GETIRQ(0), TWI_IRQ_STATUS), 8 /* bits */ ,
80 //              "TWSR" );
81
82         printf( "\nDemo launching:\n");
83
84         int state = cpu_Running;
85         while((state!= cpu_Done)&&(state != cpu_Crashed ))
86                 state = avr_run(avr);
87
88         printf("\n\nPress enter to terminate the program.");
89         getchar();
90 }