4 Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
6 This file is part of simavr.
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.
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.
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/>.
30 #include "avr_ioport.h"
32 #include "avr_timer8.h"
35 #include "sim_vcd_file.h"
41 B_START = 0, B_STOP, B_RESET,
44 button_t button[B_MAX]; // Start/Stop/Reset
45 volatile int do_button_press[B_MAX] = {0};
51 volatile uint32_t display_bits = 0;
52 volatile uint8_t display_pwm = 0;
58 * called when the AVR has latched the 595
60 void hc595_changed_hook(struct avr_irq_t * irq, uint32_t value, void * param)
67 * called when the AVR has changed the display brightness
69 void pwm_changed_hook(struct avr_irq_t * irq, uint32_t value, void * param)
75 void displayCB(void) /* function called whenever redisplay needed */
77 // OpenGL rendering goes here...
78 glClear(GL_COLOR_BUFFER_BIT);
80 // Set up modelview matrix
81 glMatrixMode(GL_MODELVIEW); // Select modelview matrix
82 glLoadIdentity(); // Start with an identity matrix
85 float size = grid * 0.8;
86 float color_on = (float)(0xff - display_pwm) / 15.0f;
87 float color_off = 0.1;
88 if (color_on < color_off)
91 glTranslatef(pixsize / 2.25f, pixsize / 1.8f, 0);
95 for (int di = 0; di < 4; di++) {
96 uint8_t digit = display_bits >> (di * 8);
98 for (int i = 0; i < 8; i++) {
99 glColor3f(0,0, digit & (1 << i) ? color_on : color_off);
100 float dx = ((di * 5.5)) * pixsize, dy = 0*pixsize;
103 dy += 3.0f * pixsize;
105 dy += 3.0f * pixsize;
107 dx += 1.0f * pixsize;
108 glVertex2f(dx + size, dy + size); glVertex2f(dx, dy + size); glVertex2f(dx, dy); glVertex2f(dx + size, dy);
109 dx += 1.0f * pixsize;
110 glVertex2f(dx + size, dy + size); glVertex2f(dx, dy + size); glVertex2f(dx, dy); glVertex2f(dx + size, dy);
113 dx += 4.25 * pixsize;
117 dy += 6.25 * pixsize;
124 dx -= 5.50 * pixsize;
128 glVertex2f(dx + size, dy + size); glVertex2f(dx, dy + size); glVertex2f(dx, dy); glVertex2f(dx + size, dy);
131 if (i == 1 || i == 2)
132 dx += 3.0f * pixsize;
133 if (i == 4 || i == 2)
134 dy += 4.0f * pixsize;
136 dy += 1.0f * pixsize;
137 glVertex2f(dx + size, dy + size); glVertex2f(dx, dy + size); glVertex2f(dx, dy); glVertex2f(dx + size, dy);
138 dy += 1.0f * pixsize;
139 glVertex2f(dx + size, dy + size); glVertex2f(dx, dy + size); glVertex2f(dx, dy); glVertex2f(dx + size, dy);
147 //glFlush(); /* Complete any pending operations */
150 void keyCB(unsigned char key, int x, int y) /* called on key press */
154 static uint8_t buf[64];
161 printf("Press %d\n", key-'1');
162 do_button_press[key-'1']++; // pass the message to the AVR thread
165 printf("Starting VCD trace\n");
166 avr_vcd_start(&vcd_file);
169 printf("Stopping VCD trace\n");
170 avr_vcd_stop(&vcd_file);
175 // gl timer. if the pin have changed states, refresh display
178 static int oldstate = -1;
180 glutTimerFunc(1000/64, timerCB, 0);
182 if (oldstate != display_flag) {
183 oldstate = display_flag;
188 static void * avr_run_thread(void * ignore)
190 int b_press[3] = {0};
195 for (int i = 0; i < 3; i++) {
196 if (do_button_press[i] != b_press[i]) {
197 b_press[i] = do_button_press[i];
198 printf("Button pressed %d\n", i);
199 button_press(&button[i], 100000);
206 int main(int argc, char *argv[])
209 const char * fname = "atmega168_timer_64led.axf";
212 sprintf(path, "%s/%s", dirname(argv[0]), fname);
213 printf("Firmware pathname is %s\n", path);
214 elf_read_firmware(path, &f);
216 printf("firmware %s f=%d mmcu=%s\n", fname, (int)f.frequency, f.mmcu);
218 avr = avr_make_mcu_by_name(f.mmcu);
220 fprintf(stderr, "%s: AVR '%s' now known\n", argv[0], f.mmcu);
224 avr_load_firmware(avr, &f);
227 // initialize our 'peripherals'
229 hc595_init(&shifter);
231 button_init(avr, &button[B_START]);
233 button[B_START].irq + IRQ_BUTTON_OUT,
234 avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('C'), 0));
235 button_init(avr, &button[B_STOP]);
237 button[B_STOP].irq + IRQ_BUTTON_OUT,
238 avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('B'), 1));
239 button_init(avr, &button[B_RESET]);
241 button[B_RESET].irq + IRQ_BUTTON_OUT,
242 avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('B'), 0));
244 // connects the fake 74HC595 array to the pins
245 avr_irq_t * i_mosi = avr_io_getirq(avr, AVR_IOCTL_SPI_GETIRQ(0), SPI_IRQ_OUTPUT),
246 * i_reset = avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('D'), 4),
247 * i_latch = avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('D'), 7);
248 avr_connect_irq(i_mosi, shifter.irq + IRQ_HC595_SPI_BYTE_IN);
249 avr_connect_irq(i_reset, shifter.irq + IRQ_HC595_IN_RESET);
250 avr_connect_irq(i_latch, shifter.irq + IRQ_HC595_IN_LATCH);
252 avr_irq_t * i_pwm = avr_io_getirq(avr, AVR_IOCTL_TIMER8_GETIRQ('0'), TIMER8_IRQ_OUT_PWM0);
253 avr_irq_register_notify(
257 avr_irq_register_notify(
258 shifter.irq + IRQ_HC595_OUT,
262 // even if not setup at startup, activate gdb if crashing
263 avr->gdb_port = 1234;
265 //avr->state = cpu_Stopped;
270 * VCD file initialization
272 * This will allow you to create a "wave" file and display it in gtkwave
273 * Pressing "r" and "s" during the demo will start and stop recording
276 avr_vcd_init(avr, "gtkwave_output.vcd", &vcd_file, 10000 /* usec */);
278 avr_vcd_add_signal(&vcd_file,
279 avr_get_interupt_irq(avr, 7), 1 /* bit */ ,
281 avr_vcd_add_signal(&vcd_file,
282 avr_get_interupt_irq(avr, 17), 1 /* bit */ ,
284 avr_vcd_add_signal(&vcd_file,
285 i_mosi, 8 /* bits */ ,
288 avr_vcd_add_signal(&vcd_file,
289 i_reset, 1 /* bit */ ,
291 avr_vcd_add_signal(&vcd_file,
292 i_latch, 1 /* bit */ ,
294 avr_vcd_add_signal(&vcd_file,
295 button[B_START].irq + IRQ_BUTTON_OUT, 1 /* bits */ ,
297 avr_vcd_add_signal(&vcd_file,
298 button[B_STOP].irq + IRQ_BUTTON_OUT, 1 /* bits */ ,
300 avr_vcd_add_signal(&vcd_file,
301 button[B_RESET].irq + IRQ_BUTTON_OUT, 1 /* bits */ ,
304 avr_vcd_add_signal(&vcd_file,
305 shifter.irq + IRQ_HC595_OUT, 32 /* bits */ ,
307 avr_vcd_add_signal(&vcd_file,
308 i_pwm, 8 /* bits */ ,
311 // 'raise' it, it's a "pullup"
312 avr_raise_irq(button[B_START].irq + IRQ_BUTTON_OUT, 1);
313 avr_raise_irq(button[B_STOP].irq + IRQ_BUTTON_OUT, 1);
314 avr_raise_irq(button[B_RESET].irq + IRQ_BUTTON_OUT, 1);
316 printf( "Demo : This is a real world firmware, a 'stopwatch'\n"
317 " timer that can count up to 99 days. It features a PWM control of the\n"
318 " brightness, blinks the dots, displays the number of days spent and so on.\n\n"
319 " Press '0' to press the 'start' button\n"
320 " Press '1' to press the 'stop' button\n"
321 " Press '2' to press the 'reset' button\n"
322 " Press 'q' to quit\n\n"
323 " Press 'r' to start recording a 'wave' file - with a LOT of data\n"
324 " Press 's' to stop recording\n"
325 " + Make sure to watch the brightness dim once you stop the timer\n\n"
329 * OpenGL init, can be ignored
331 glutInit(&argc, argv); /* initialize GLUT system */
336 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
337 glutInitWindowSize(w * pixsize, h * pixsize); /* width=400pixels height=500pixels */
338 window = glutCreateWindow("Press 0, 1, 2 or q"); /* create window */
340 // Set up projection matrix
341 glMatrixMode(GL_PROJECTION); // Select projection matrix
342 glLoadIdentity(); // Start with an identity matrix
343 glOrtho(0, w * pixsize, 0, h * pixsize, 0, 10);
345 glTranslatef(0, -1 * h * pixsize, 0);
347 glutDisplayFunc(displayCB); /* set window's display callback */
348 glutKeyboardFunc(keyCB); /* set window's key callback */
349 glutTimerFunc(1000 / 24, timerCB, 0);
351 // the AVR run on it's own thread. it even allows for debugging!
353 pthread_create(&run, NULL, avr_run_thread, NULL);