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