Merge pull request #2 from schuay/watchpoints
[simavr] / examples / board_ledramp / ledramp.c
1 /*
2         ledramp.c
3         
4         Copyright 2008, 2009 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 #if __APPLE__
26 #include <GLUT/glut.h>
27 #else
28 #include <GL/glut.h>
29 #endif
30 #include <pthread.h>
31
32 #include "sim_avr.h"
33 #include "avr_ioport.h"
34 #include "sim_elf.h"
35 #include "sim_gdb.h"
36 #include "sim_vcd_file.h"
37
38 #include "button.h"
39
40 button_t button;
41 int do_button_press = 0;
42 avr_t * avr = NULL;
43 avr_vcd_t vcd_file;
44 uint8_t pin_state = 0;  // current port B
45
46 float pixsize = 64;
47 int window;
48
49 /*
50  * called when the AVR change any of the pins on port B
51  * so lets update our buffer
52  */
53 void pin_changed_hook(struct avr_irq_t * irq, uint32_t value, void * param)
54 {
55         pin_state = (pin_state & ~(1 << irq->irq)) | (value << irq->irq);
56 }
57
58 void displayCB(void)            /* function called whenever redisplay needed */
59 {
60         // OpenGL rendering goes here...
61         glClear(GL_COLOR_BUFFER_BIT);
62
63         // Set up modelview matrix
64         glMatrixMode(GL_MODELVIEW); // Select modelview matrix
65         glLoadIdentity(); // Start with an identity matrix
66
67         float grid = pixsize;
68         float size = grid * 0.8;
69     glBegin(GL_QUADS);
70         glColor3f(1,0,0);
71
72         for (int di = 0; di < 8; di++) {
73                 char on = (pin_state & (1 << di)) != 0;
74                 if (on) {
75                         float x = (di) * grid;
76                         float y = 0; //(si * grid * 8) + (di * grid);
77                         glVertex2f(x + size, y + size);
78                         glVertex2f(x, y + size);
79                         glVertex2f(x, y);
80                         glVertex2f(x + size, y);
81                 }
82         }
83
84     glEnd();
85     glutSwapBuffers();
86     //glFlush();                                /* Complete any pending operations */
87 }
88
89 void keyCB(unsigned char key, int x, int y)     /* called on key press */
90 {
91         if (key == 'q')
92                 exit(0);
93         //static uint8_t buf[64];
94         switch (key) {
95                 case 'q':
96                 case 0x1f: // escape
97                         exit(0);
98                         break;
99                 case ' ':
100                         do_button_press++; // pass the message to the AVR thread
101                         break;
102                 case 'r':
103                         printf("Starting VCD trace\n");
104                         avr_vcd_start(&vcd_file);
105                         break;
106                 case 's':
107                         printf("Stopping VCD trace\n");
108                         avr_vcd_stop(&vcd_file);
109                         break;
110         }
111 }
112
113 // gl timer. if the pin have changed states, refresh display
114 void timerCB(int i)
115 {
116         static uint8_t oldstate = 0xff;
117         // restart timer
118         glutTimerFunc(1000/64, timerCB, 0);
119
120         if (oldstate != pin_state) {
121                 oldstate = pin_state;
122                 glutPostRedisplay();
123         }
124 }
125
126 static void * avr_run_thread(void * oaram)
127 {
128         int b_press = do_button_press;
129         
130         while (1) {
131                 avr_run(avr);
132                 if (do_button_press != b_press) {
133                         b_press = do_button_press;
134                         printf("Button pressed\n");
135                         button_press(&button, 1000000);
136                 }
137         }
138         return NULL;
139 }
140
141
142 int main(int argc, char *argv[])
143 {
144         elf_firmware_t f;
145         const char * fname =  "atmega48_ledramp.axf";
146         //char path[256];
147
148 //      sprintf(path, "%s/%s", dirname(argv[0]), fname);
149 //      printf("Firmware pathname is %s\n", path);
150         elf_read_firmware(fname, &f);
151
152         printf("firmware %s f=%d mmcu=%s\n", fname, (int)f.frequency, f.mmcu);
153
154         avr = avr_make_mcu_by_name(f.mmcu);
155         if (!avr) {
156                 fprintf(stderr, "%s: AVR '%s' not known\n", argv[0], f.mmcu);
157                 exit(1);
158         }
159         avr_init(avr);
160         avr_load_firmware(avr, &f);
161
162         // initialize our 'peripheral'
163         button_init(avr, &button, "button");
164         // "connect" the output irw of the button to the port pin of the AVR
165         avr_connect_irq(
166                 button.irq + IRQ_BUTTON_OUT,
167                 avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('C'), 0));
168
169         // connect all the pins on port B to our callback
170         for (int i = 0; i < 8; i++)
171                 avr_irq_register_notify(
172                         avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('B'), i),
173                         pin_changed_hook, 
174                         NULL);
175
176         // even if not setup at startup, activate gdb if crashing
177         avr->gdb_port = 1234;
178         if (0) {
179                 //avr->state = cpu_Stopped;
180                 avr_gdb_init(avr);
181         }
182
183         /*
184          *      VCD file initialization
185          *      
186          *      This will allow you to create a "wave" file and display it in gtkwave
187          *      Pressing "r" and "s" during the demo will start and stop recording
188          *      the pin changes
189          */
190         avr_vcd_init(avr, "gtkwave_output.vcd", &vcd_file, 100000 /* usec */);
191         avr_vcd_add_signal(&vcd_file, 
192                 avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('B'), IOPORT_IRQ_PIN_ALL), 8 /* bits */ ,
193                 "portb" );
194         avr_vcd_add_signal(&vcd_file, 
195                 button.irq + IRQ_BUTTON_OUT, 1 /* bits */ ,
196                 "button" );
197
198         // 'raise' it, it's a "pullup"
199         avr_raise_irq(button.irq + IRQ_BUTTON_OUT, 1);
200
201         printf( "Demo launching: 'LED' bar is PORTB, updated every 1/64s by the AVR\n"
202                         "   firmware using a timer. If you press 'space' this presses a virtual\n"
203                         "   'button' that is hooked to the virtual PORTC pin 0 and will\n"
204                         "   trigger a 'pin change interrupt' in the AVR core, and will 'invert'\n"
205                         "   the display.\n"
206                         "   Press 'q' to quit\n\n"
207                         "   Press 'r' to start recording a 'wave' file\n"
208                         "   Press 's' to stop recording\n"
209                         );
210
211         /*
212          * OpenGL init, can be ignored
213          */
214         glutInit(&argc, argv);          /* initialize GLUT system */
215
216         glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
217         glutInitWindowSize(8 * pixsize, 1 * pixsize);           /* width=400pixels height=500pixels */
218         window = glutCreateWindow("Glut");      /* create window */
219
220         // Set up projection matrix
221         glMatrixMode(GL_PROJECTION); // Select projection matrix
222         glLoadIdentity(); // Start with an identity matrix
223         glOrtho(0, 8 * pixsize, 0, 1 * pixsize, 0, 10);
224         glScalef(1,-1,1);
225         glTranslatef(0, -1 * pixsize, 0);
226
227         glutDisplayFunc(displayCB);             /* set window's display callback */
228         glutKeyboardFunc(keyCB);                /* set window's key callback */
229         glutTimerFunc(1000 / 24, timerCB, 0);
230
231         // the AVR run on it's own thread. it even allows for debugging!
232         pthread_t run;
233         pthread_create(&run, NULL, avr_run_thread, NULL);
234
235         glutMainLoop();
236 }