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