int main(int argc, char *argv[]) { // elf_firmware_t f; const char * pwd = dirname(argv[0]); avr = avr_make_mcu_by_name("at90usb162"); if (!avr) { fprintf(stderr, "%s: Error creating the AVR core\n", argv[0]); exit(1); } strcpy(avr_flash_path, "simusb_flash.bin"); // register our own functions avr->special_init = avr_special_init; avr->special_deinit = avr_special_deinit; //avr->reset = NULL; avr_init(avr); avr->frequency = 8000000; // this trick creates a file that contains /and keep/ the flash // in the same state as it was before. This allow the bootloader // app to be kept, and re-run if the bootloader doesn't get a // new one { char path[1024]; uint32_t base, size; snprintf(path, sizeof(path), "%s/../%s", pwd, "at90usb162_cdc_loopback.hex"); uint8_t * boot = read_ihex_file(path, &size, &base); if (!boot) { fprintf(stderr, "%s: Unable to load %s\n", argv[0], path); exit(1); } printf("Booloader %04x: %d\n", base, size); memcpy(avr->flash + base, boot, size); free(boot); avr->pc = base; avr->codeend = avr->flashend; } // even if not setup at startup, activate gdb if crashing avr->gdb_port = 1234; if (0) { //avr->state = cpu_Stopped; avr_gdb_init(avr); } vhci_usb_init(avr, &vhci_usb); vhci_usb_connect(&vhci_usb, '0'); while (1) { avr_run(avr); } }
void avr_sadly_crashed (avr_t * avr, uint8_t signal) { AVR_LOG (avr, LOG_ERROR, "%s\n", __FUNCTION__); avr->state = cpu_Stopped; if (avr->gdb_port && !avr->gdb) // enable gdb server, and wait avr_gdb_init (avr); if (!avr->gdb) avr->state = cpu_Crashed; }
void avr_sadly_crashed(avr_t *avr, uint8_t signal) { printf("%s\n", __FUNCTION__); avr->state = cpu_Stopped; if (avr->gdb_port) { // enable gdb server, and wait if (!avr->gdb) avr_gdb_init(avr); } if (!avr->gdb) avr->state = cpu_Crashed; }
int main(int argc, char *argv[]) { elf_firmware_t f; const char * fname = "atmega1280_i2ctest.axf"; printf("Firmware pathname is %s\n", fname); elf_read_firmware(fname, &f); printf("firmware %s f=%d mmcu=%s\n", fname, (int)f.frequency, f.mmcu); avr = avr_make_mcu_by_name(f.mmcu); if (!avr) { fprintf(stderr, "%s: AVR '%s' not known\n", argv[0], f.mmcu); exit(1); } avr_init(avr); avr_load_firmware(avr, &f); // initialize our 'peripheral', setting the mask to allow read and write i2c_eeprom_init(avr, &ee, 0xa0, 0x01, NULL, 1024); i2c_eeprom_attach(avr, &ee, AVR_IOCTL_TWI_GETIRQ(0)); ee.verbose = 1; // even if not setup at startup, activate gdb if crashing avr->gdb_port = 1234; if (0) { //avr->state = cpu_Stopped; avr_gdb_init(avr); } /* * VCD file initialization * * This will allow you to create a "wave" file and display it in gtkwave * Pressing "r" and "s" during the demo will start and stop recording * the pin changes */ // avr_vcd_init(avr, "gtkwave_output.vcd", &vcd_file, 100000 /* usec */); // avr_vcd_add_signal(&vcd_file, // avr_io_getirq(avr, AVR_IOCTL_TWI_GETIRQ(0), TWI_IRQ_STATUS), 8 /* bits */ , // "TWSR" ); printf( "\nDemo launching:\n"); int state = cpu_Running; while ((state != cpu_Done) && (state != cpu_Crashed)) state = avr_run(avr); }
int main(int argc, char *argv[]) { elf_firmware_t f = {{0}}; long f_cpu = 0; int trace = 0; int gdb = 0; int log = 1; char name[16] = ""; uint32_t loadBase = AVR_SEGMENT_OFFSET_FLASH; int trace_vectors[8] = {0}; int trace_vectors_count = 0; if (argc == 1) display_usage(basename(argv[0])); for (int pi = 1; pi < argc; pi++) { if (!strcmp(argv[pi], "--list-cores")) { list_cores(); } else if (!strcmp(argv[pi], "-h") || !strcmp(argv[pi], "--help")) { display_usage(basename(argv[0])); } else if (!strcmp(argv[pi], "-m") || !strcmp(argv[pi], "-mcu")) { if (pi < argc-1) strcpy(name, argv[++pi]); else display_usage(basename(argv[0])); } else if (!strcmp(argv[pi], "-f") || !strcmp(argv[pi], "-freq")) { if (pi < argc-1) f_cpu = atoi(argv[++pi]); else display_usage(basename(argv[0])); } else if (!strcmp(argv[pi], "-t") || !strcmp(argv[pi], "-trace")) { trace++; } else if (!strcmp(argv[pi], "-ti")) { if (pi < argc-1) trace_vectors[trace_vectors_count++] = atoi(argv[++pi]); } else if (!strcmp(argv[pi], "-g") || !strcmp(argv[pi], "-gdb")) { gdb++; } else if (!strcmp(argv[pi], "-v")) { log++; } else if (!strcmp(argv[pi], "-ee")) { loadBase = AVR_SEGMENT_OFFSET_EEPROM; } else if (!strcmp(argv[pi], "-ff")) { loadBase = AVR_SEGMENT_OFFSET_FLASH; } else if (argv[pi][0] != '-') { char * filename = argv[pi]; char * suffix = strrchr(filename, '.'); if (suffix && !strcasecmp(suffix, ".hex")) { if (!name[0] || !f_cpu) { fprintf(stderr, "%s: -mcu and -freq are mandatory to load .hex files\n", argv[0]); exit(1); } ihex_chunk_p chunk = NULL; int cnt = read_ihex_chunks(filename, &chunk); if (cnt <= 0) { fprintf(stderr, "%s: Unable to load IHEX file %s\n", argv[0], argv[pi]); exit(1); } printf("Loaded %d section of ihex\n", cnt); for (int ci = 0; ci < cnt; ci++) { if (chunk[ci].baseaddr < (1*1024*1024)) { f.flash = chunk[ci].data; f.flashsize = chunk[ci].size; f.flashbase = chunk[ci].baseaddr; printf("Load HEX flash %08x, %d\n", f.flashbase, f.flashsize); } else if (chunk[ci].baseaddr >= AVR_SEGMENT_OFFSET_EEPROM || chunk[ci].baseaddr + loadBase >= AVR_SEGMENT_OFFSET_EEPROM) { // eeprom! f.eeprom = chunk[ci].data; f.eesize = chunk[ci].size; printf("Load HEX eeprom %08x, %d\n", chunk[ci].baseaddr, f.eesize); } } } else { if (elf_read_firmware(filename, &f) == -1) { fprintf(stderr, "%s: Unable to load firmware from file %s\n", argv[0], filename); exit(1); } } } } if (strlen(name)) strcpy(f.mmcu, name); if (f_cpu) f.frequency = f_cpu; avr = avr_make_mcu_by_name(f.mmcu); if (!avr) { fprintf(stderr, "%s: AVR '%s' not known\n", argv[0], f.mmcu); exit(1); } avr_init(avr); avr_load_firmware(avr, &f); if (f.flashbase) { printf("Attempted to load a bootloader at %04x\n", f.flashbase); avr->pc = f.flashbase; } avr->log = (log > LOG_TRACE ? LOG_TRACE : log); avr->trace = trace; for (int ti = 0; ti < trace_vectors_count; ti++) { for (int vi = 0; vi < avr->interrupts.vector_count; vi++) if (avr->interrupts.vector[vi]->vector == trace_vectors[ti]) avr->interrupts.vector[vi]->trace = 1; } // even if not setup at startup, activate gdb if crashing avr->gdb_port = 1234; if (gdb) { avr->state = cpu_Stopped; avr_gdb_init(avr); } signal(SIGINT, sig_int); signal(SIGTERM, sig_int); for (;;) { int state = avr_run(avr); if ( state == cpu_Done || state == cpu_Crashed) break; } avr_terminate(avr); }
int main(int argc, char *argv[]) { elf_firmware_t f; //const char * fname = "atmega48_ledramp.axf"; const char * fname = argv[1]; char path[256]; // sprintf(path, "%s/%s", dirname(argv[0]), fname); // printf("Firmware pathname is %s\n", path); elf_read_firmware(fname, &f); printf("firmware %s f=%d mmcu=%s\n", fname, (int)f.frequency, f.mmcu); avr = avr_make_mcu_by_name(f.mmcu); if (!avr) { fprintf(stderr, "%s: AVR '%s' now known\n", argv[0], f.mmcu); exit(1); } avr_init(avr); avr_load_firmware(avr, &f); // initialize our 'peripheral' button_init(avr, &button, "button"); // "connect" the output irw of the button to the port pin of the AVR avr_connect_irq( button.irq + IRQ_BUTTON_OUT, avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('C'), 0)); // connect all the pins on port B to our callback for (int i = 0; i < 8; i++) avr_irq_register_notify( avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('B'), i), pin_changed_hook, NULL); // even if not setup at startup, activate gdb if crashing avr->gdb_port = 1234; if (0) { //avr->state = cpu_Stopped; avr_gdb_init(avr); } /* * VCD file initialization * * This will allow you to create a "wave" file and display it in gtkwave * Pressing "r" and "s" during the demo will start and stop recording * the pin changes */ avr_vcd_init(avr, "gtkwave_output.vcd", &vcd_file, 100000 /* usec */); avr_vcd_add_signal(&vcd_file, avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('B'), IOPORT_IRQ_PIN_ALL), 8 /* bits */ , "portb" ); avr_vcd_add_signal(&vcd_file, avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('C'), IOPORT_IRQ_PIN0), 1, "portc"); avr_vcd_add_signal(&vcd_file, button.irq + IRQ_BUTTON_OUT, 1 /* bits */ , "button" ); // 'raise' it, it's a "pullup" avr_raise_irq(button.irq + IRQ_BUTTON_OUT, 1); printf( "Demo launching: 'LED' bar is PORTB, updated every 1/64s by the AVR\n" " firmware using a timer. If you press 'space' this presses a virtual\n" " 'button' that is hooked to the virtual PORTC pin 0 and will\n" " trigger a 'pin change interrupt' in the AVR core, and will 'invert'\n" " the display.\n" " Press 'q' to quit\n\n" " Press 'r' to start recording a 'wave' file\n" " Press 's' to stop recording\n" ); /* * OpenGL init, can be ignored */ glutInit(&argc, argv); /* initialize GLUT system */ glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); glutInitWindowSize(8 * pixsize, 1 * pixsize); /* width=400pixels height=500pixels */ window = glutCreateWindow("Glut"); /* create window */ // Set up projection matrix glMatrixMode(GL_PROJECTION); // Select projection matrix glLoadIdentity(); // Start with an identity matrix glOrtho(0, 8 * pixsize, 0, 1 * pixsize, 0, 10); glScalef(1,-1,1); glTranslatef(0, -1 * pixsize, 0); glutDisplayFunc(displayCB); /* set window's display callback */ glutKeyboardFunc(keyCB); /* set window's key callback */ glutTimerFunc(1000 / 24, timerCB, 0); // the AVR run on it's own thread. it even allows for debugging! pthread_t run; pthread_create(&run, NULL, avr_run_thread, NULL); glutMainLoop(); }
int main(int argc, char *argv[]) { elf_firmware_t f; const char * fname = "wordClock-erl"; char path[256]; // sprintf(path, "%s/%s", dirname(argv[0]), fname); // printf("Firmware pathname is %s\n", path); elf_read_firmware(fname, &f); strcpy( f.mmcu, "atmega168" ); // hack f.frequency = 16000000; printf("firmware %s f=%d mmcu=%s\n", fname, (int)f.frequency, f.mmcu); avr = avr_make_mcu_by_name(f.mmcu); if (!avr) { fprintf(stderr, "%s: AVR '%s' now known\n", argv[0], f.mmcu); exit(1); } avr_init(avr); avr_load_firmware(avr, &f); // initialize our 'peripheral' // button_init(avr, &button, "button"); // "connect" the output irw of the button to the port pin of the AVR /* avr_connect_irq( button.irq + IRQ_BUTTON_OUT, avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('C'), 0)); */ // connect all the pins on port B to our callback #if 0 for (int i = 0; i < 8; i++) avr_irq_register_notify( avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('B'), i), pin_changed_hook, NULL); #endif // even if not setup at startup, activate gdb if crashing avr->gdb_port = 1234; if (0) { //avr->state = cpu_Stopped; avr_gdb_init(avr); } /* * VCD file initialization * * This will allow you to create a "wave" file and display it in gtkwave * Pressing "r" and "s" during the demo will start and stop recording * the pin changes * * Initially set to 100 000 usec = 100 ms. I think it is how often * to flush its' log. */ avr_vcd_init(avr, "gtkwave_output.vcd", &vcd_file, 100 /* usec */); /* want MOSI, SCLK, XLAT, BLANK */ avr_vcd_add_signal( &vcd_file, avr_io_getirq(avr, AVR_IOCTL_SPI_GETIRQ(0), SPI_IRQ_OUTPUT), 8, "MOSI" ); avr_vcd_add_signal(&vcd_file, avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('B'), 1 ), 1, "XLAT" ); avr_vcd_add_signal(&vcd_file, avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('B'), 2 ), 1, "BLANK" ); avr_vcd_add_signal(&vcd_file, avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('B'), 5 ), 1, "SCLK" ); #if 0 avr_vcd_add_signal(&vcd_file, button.irq + IRQ_BUTTON_OUT, 1 /* bits */ , "button" ); // 'raise' it, it's a "pullup" avr_raise_irq(button.irq + IRQ_BUTTON_OUT, 1); #endif avr_vcd_start( &vcd_file ); printf( "Demo launching. " ); #if 0 /* * OpenGL init, can be ignored */ glutInit(&argc, argv); /* initialize GLUT system */ glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); glutInitWindowSize(8 * pixsize, 1 * pixsize); /* width=400pixels height=500pixels */ window = glutCreateWindow("Glut"); /* create window */ // Set up projection matrix glMatrixMode(GL_PROJECTION); // Select projection matrix glLoadIdentity(); // Start with an identity matrix glOrtho(0, 8 * pixsize, 0, 1 * pixsize, 0, 10); glScalef(1,-1,1); glTranslatef(0, -1 * pixsize, 0); glutDisplayFunc(displayCB); /* set window's display callback */ glutKeyboardFunc(keyCB); /* set window's key callback */ glutTimerFunc(1000 / 24, timerCB, 0); #endif // the AVR run on it's own thread. it even allows for debugging! pthread_t run; pthread_create(&run, NULL, avr_run_thread, NULL); sleep( 60 ); // wait 5 seconds, then exit. avr_vcd_stop(&vcd_file); /* glutMainLoop(); */ }
int main(int argc, char *argv[]) { const char *argv0 = argv[0]; int exit_state = EXIT_FAILURE; char *env; struct drumfish_cfg config; struct sigaction act; int state = cpu_Limbo; int opt; char **flash_file = NULL; size_t flash_file_len = 0; long port; config.mac = NULL; config.pflash = NULL; config.foreground = 1; config.verbose = 0; config.gdb = 0; config.erase_pflash = 0; config.peripherals[DF_PERIPHERAL_UART0] = strdup("off"); config.peripherals[DF_PERIPHERAL_UART1] = strdup("on"); while ((opt = getopt(argc, argv, "ef:p:m:vg:s:h")) != -1) { switch (opt) { case 'e': config.erase_pflash = 1; break; case 'f': /* Increment how many file names we need to keep track of */ flash_file_len++; /* Set a reasonable max which is less than 2^30 to avoid * overflowing realloc(). */ if (flash_file_len > MAX_FLASH_FILES) { fprintf(stderr, "Unable to load more than %d " "firmware images at once.\n", MAX_FLASH_FILES); exit(EXIT_FAILURE); } /* Make room for our next pointer */ flash_file = realloc(flash_file, sizeof(void *) * flash_file_len); if (!flash_file) { fprintf(stderr, "Failed to allocate memory for " "flash file list.\n"); exit(EXIT_FAILURE); } /* Now let's store our file name */ flash_file[flash_file_len - 1] = strdup(optarg); if (!flash_file[flash_file_len - 1]) { fprintf(stderr, "Failed to allocate memory for " "flash file.\n"); exit(EXIT_FAILURE); } break; case 's': /* path to storage for flash */ config.pflash = strdup(optarg); if (!config.pflash) { fprintf(stderr, "Failed to allocate memory for " "programmable flashh.\n"); exit(EXIT_FAILURE); } break; case 'm': config.mac = strdup(optarg); if (!config.mac) { fprintf(stderr, "Failed to allocate memory for MAC.\n"); exit(EXIT_FAILURE); } break; case 'v': config.verbose++; break; case 'g': errno = 0; port = strtol(optarg, NULL, 10); if (errno != 0) { fprintf(stderr, "Invalid supplied GDB port '%s': %s\n", optarg, strerror(errno)); exit(EXIT_FAILURE); } if (port <= 1024 && port > UINT16_MAX) { fprintf(stderr, "Invalid supplied GDB port %ld. " "Must be 1024 < port <= %d\n", port, UINT16_MAX); exit(EXIT_FAILURE); } config.gdb = port; break; case 'p': /* store requested port (UART) path */ df_peripheral_parse(&config, optarg); break; case 'V': /* print version */ break; case 'h': usage(argv0); exit(EXIT_SUCCESS); break; default: /* '?' */ usage(argv0); exit(EXIT_FAILURE); } } /* Initialize our logging support */ df_log_init(&config); /* If the user did not override the default location of the * programmable flash storage, then set the default */ env = getenv("HOME"); if (!env || !env[0]) { fprintf(stderr, "Unable to determine your HOME.\n"); exit(EXIT_FAILURE); } if (asprintf(&config.pflash, "%s%s", env, DEFAULT_PFLASH_PATH) < 0) { fprintf(stderr, "Failed to allocate memory for pflash filename.\n"); exit(EXIT_FAILURE); } printf("Programmable Flash Storage: %s\n", config.pflash); /* Handle the bare minimum signals */ /* Yes I should use sigset_t here and use sigemptyset() */ memset(&act, 0, sizeof(act)); act.sa_handler = handler; if (sigaction(SIGHUP, &act, NULL) < 0) { fprintf(stderr, "Failed to install SIGHUP handler\n"); exit(EXIT_FAILURE); } if (sigaction(SIGINT, &act, NULL) < 0) { fprintf(stderr, "Failed to install SIGINT handler\n"); exit(EXIT_FAILURE); } if (sigaction(SIGTERM, &act, NULL) < 0) { fprintf(stderr, "Failed to install SIGTERM handler\n"); exit(EXIT_FAILURE); } avr = m128rfa1_create(&config); if (!avr) { fprintf(stderr, "Unable to initialize requested board.\n"); exit(EXIT_FAILURE); } /* Flash in any requested firmware, while cleaning up our memory */ for (size_t i = 0; i < flash_file_len; i++) { if (flash_load(flash_file[i], avr->flash, avr->flashend + 1)) { fprintf(stderr, "Failed to load '%s' into flash.\n", flash_file[i]); exit(EXIT_FAILURE); } /* Don't need this memory anymore */ free(flash_file[i]); } free(flash_file); /* Ensure the instruction we're about to execute is legit */ if (avr->flash[avr->pc] == 0xff) { fprintf(stderr, "No firmware loaded in programmable flash, unable " "to boot.\n"); fprintf(stderr, "Try using '-f firmware.hex' to supply one.\n"); exit(EXIT_FAILURE); } /* If the user wants to run the core with GDB server enabled, * set that up. */ if (config.gdb) { avr->gdb_port = config.gdb; /* Normally starting the CPU should be in limbo, but the * GDB code of simavr wants it to be stopped. */ state = cpu_Stopped; avr_gdb_init(avr); } /* Capture the current time to be used as when our CPU started */ df_log_start_time(); df_log_msg(DF_LOG_INFO, "Booting CPU from 0x%x.\n", avr->pc); /* Our main event loop */ for (;;) { state = avr_run(avr); if (state == cpu_Done) { exit_state = EXIT_SUCCESS; break; } else if (state == cpu_Crashed) { /* many firmwares disable interrupts and enable the watchdog * to cause the MCU to reboot. simavr treats that state as * cpu_Crashed */ df_log_msg(DF_LOG_INFO, "CPU rebooted\n"); avr_reset(avr); } } avr_terminate(avr); df_log_msg(DF_LOG_INFO, "Terminated.\n"); free(config.pflash); return exit_state; }
int main(int argc, char *argv[]) { struct avr_flash flash_data; char boot_path[1024] = "ATmegaBOOT_168_atmega328.ihex"; uint32_t boot_base, boot_size; char * mmcu = "atmega328p"; uint32_t freq = 16000000; int debug = 0; int verbose = 0; /* * To allow other programs to know that the model is * online and ready for programming, we create a file * called emu_online.txt * * At the beginning we clean up the file and then write * to it later on once the emulator is running. */ char * emu_online_file = "emu_online.txt"; int emu_online_flag = 0; for (int i = 1; i < argc; i++) { if (!strcmp(argv[i] + strlen(argv[i]) - 4, ".hex")) strncpy(boot_path, argv[i], sizeof(boot_path)); else if (!strcmp(argv[i], "-d")) debug++; else if (!strcmp(argv[i], "-v")) verbose++; else if (strstr(argv[i], ".ihex") != NULL) strcpy( boot_path, argv[1] ); else { fprintf(stderr, "%s: invalid argument %s\n", argv[0], argv[i]); exit(1); } } if( access(emu_online_file, F_OK) != -1 ){ remove(emu_online_file); } avr = avr_make_mcu_by_name(mmcu); if (!avr) { fprintf(stderr, "%s: Error creating the AVR core\n", argv[0]); exit(1); } uint8_t * boot = read_ihex_file(boot_path, &boot_size, &boot_base); if (!boot) { fprintf(stderr, "%s: Unable to load %s\n", argv[0], boot_path); exit(1); } if (boot_base > 32*1024*1024) { mmcu = "atmega2560"; freq = 20000000; } printf("%s booloader 0x%05x: %d bytes\n", mmcu, boot_base, boot_size); snprintf(flash_data.avr_flash_path, sizeof(flash_data.avr_flash_path), "simduino_%s_flash.bin", mmcu); flash_data.avr_flash_fd = 0; // register our own functions avr->custom.init = avr_special_init; avr->custom.deinit = avr_special_deinit; avr->custom.data = &flash_data; avr_init(avr); avr->frequency = freq; memcpy(avr->flash + boot_base, boot, boot_size); free(boot); avr->pc = boot_base; /* end of flash, remember we are writing /code/ */ avr->codeend = avr->flashend; avr->log = 1 + verbose; // even if not setup at startup, activate gdb if crashing avr->gdb_port = 1234; if (debug) { avr->state = cpu_Stopped; avr_gdb_init(avr); } uart_pty_init(avr, &uart_pty); uart_pty_connect(&uart_pty, '0'); while (1) { int state = avr_run(avr); if ( state == cpu_Done || state == cpu_Crashed) break; if( emu_online_flag == 0){ fopen(emu_online_file, "w+"); emu_online_flag = 1; } } }
int main(int argc, char *argv[]) { int state; elf_firmware_t f; const char *fname = "../BasicMotorControl.elf"; const char *mmcu = "atmega328p"; if (elf_read_firmware(fname, &f) != 0) { exit(1); } f.frequency = 16000000; printf("firmware %s f=%d mmcu=%s\n", fname, (int)f.frequency, mmcu); avr = avr_make_mcu_by_name(mmcu); if (!avr) { fprintf(stderr, "%s: AVR '%s' not known\n", argv[0], mmcu); exit(1); } avr_init(avr); avr_load_firmware(avr, &f); #if 0 /* even if not setup at startup, activate gdb if crashing */ avr->gdb_port = 1234; avr->state = cpu_Stopped; avr_gdb_init(avr); #endif /* VCD file initialization */ avr_vcd_init(avr, "wave.vcd", &vcd_file, 10000 /* usec */); avr_vcd_add_signal(&vcd_file, avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('B'), 0), 1, "B0" ); avr_vcd_add_signal(&vcd_file, avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('B'), 1), 1, "B1" ); avr_vcd_add_signal(&vcd_file, avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('B'), 2), 1, "B2" ); avr_vcd_add_signal(&vcd_file, avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('B'), 3), 1, "B3" ); avr_vcd_start(&vcd_file); /* IRQ callback hooks */ avr_irq_register_notify( avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('B'), 0), pin_changed_hook, NULL); avr_irq_register_notify( avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('B'), 1), pin_changed_hook, NULL); avr_irq_register_notify( avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('B'), 2), pin_changed_hook, NULL); avr_irq_register_notify( avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('B'), 3), pin_changed_hook, NULL); /* show some info */ show_ports(avr); /* install signal handlers */ signal(SIGINT, sig_int); signal(SIGTERM, sig_int); /* main loop */ printf("*** Entering main loop ***\n"); while (1) { state = avr_run(avr); if (state == cpu_Done || state == cpu_Crashed) { printf("CPU State %d\n", state); break; } } avr_vcd_stop(&vcd_file); avr_terminate(avr); return 0; }
int main(int argc, char* argv[]) { int r = 0; char* elf_file = NULL; int gdb_port = 0; for (char** arg = &argv[1]; *arg != NULL; ++arg) { if (strcmp("-d", *arg) == 0) gdb_port = atoi(*(++arg)); else elf_file = *arg; } if (elf_file == NULL) { fprintf(stderr, "Give me a .elf file to load\n"); return 1; } elf_firmware_t f; elf_read_firmware(elf_file, &f); avr_t* avr = avr_make_mcu_by_name("atmega328p"); if (!avr) { fprintf(stderr, "Unsupported cpu atmega328p\n"); return 1; } avr_init(avr); if (gdb_port != 0) { avr->gdb_port = gdb_port; avr_gdb_init(avr); } avr->frequency = 16000000; avr_load_firmware(avr, &f); pcd8544_init(avr, &lcd); avr_connect_irq(avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('C'), 2), lcd.irq + IRQ_PCD8544_DC); avr_connect_irq(avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('C'), 1), lcd.irq + IRQ_PCD8544_CS); avr_connect_irq(avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('C'), 0), lcd.irq + IRQ_PCD8544_RST); avr_connect_irq(avr_io_getirq(avr, AVR_IOCTL_SPI_GETIRQ(0), SPI_IRQ_OUTPUT), lcd.irq + IRQ_PCD8544_SPI_IN); gb_keypad_init(avr, &keypad); for (int i = 0; i < keydefs_length; i++) { const keydef_t *k = keydefs + i; avr_connect_irq(keypad.irq + k->keypad_key, avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ(k->port), k->pin)); // Start with the pin high avr_ioport_t* port = (avr_ioport_t*) avr->io[k->avr_port].w.param; key_io[i].pin_mask = (1<<k->pin); key_io[i].pull_value = &port->external.pull_value; port->external.pull_mask |= key_io[i].pin_mask; port->external.pull_value |= key_io[i].pin_mask; gb_keypad_press(&keypad, k->keypad_key, 1); } if (display_init()) { lcd_ram_mutex = SDL_CreateMutex(); SDL_Thread* avr_thread = SDL_CreateThread(avr_run_thread, "avr-thread", avr); main_loop(); SDL_LockMutex(lcd_ram_mutex); quit_flag = 1; SDL_UnlockMutex(lcd_ram_mutex); int avr_thread_return; SDL_WaitThread(avr_thread, &avr_thread_return); SDL_DestroyMutex(lcd_ram_mutex); } else { r = 1; fprintf(stderr, "%s\n", display_error_message()); } display_destroy(); return r; }
int main(int argc, char *argv[]) { elf_firmware_t f; const char * fname = "atmega168_timer_64led.axf"; //char path[256]; // sprintf(path, "%s/%s", dirname(argv[0]), fname); //printf("Firmware pathname is %s\n", path); elf_read_firmware(fname, &f); printf("firmware %s f=%d mmcu=%s\n", fname, (int)f.frequency, f.mmcu); avr = avr_make_mcu_by_name(f.mmcu); if (!avr) { fprintf(stderr, "%s: AVR '%s' now known\n", argv[0], f.mmcu); exit(1); } avr_init(avr); avr_load_firmware(avr, &f); // // initialize our 'peripherals' // hc595_init(avr, &shifter); button_init(avr, &button[B_START], "button.start"); avr_connect_irq( button[B_START].irq + IRQ_BUTTON_OUT, avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('C'), 0)); button_init(avr, &button[B_STOP], "button.stop"); avr_connect_irq( button[B_STOP].irq + IRQ_BUTTON_OUT, avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('B'), 1)); button_init(avr, &button[B_RESET], "button.reset"); avr_connect_irq( button[B_RESET].irq + IRQ_BUTTON_OUT, avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('B'), 0)); // connects the fake 74HC595 array to the pins avr_irq_t * i_mosi = avr_io_getirq(avr, AVR_IOCTL_SPI_GETIRQ(0), SPI_IRQ_OUTPUT), * i_reset = avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('D'), 4), * i_latch = avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('D'), 7); avr_connect_irq(i_mosi, shifter.irq + IRQ_HC595_SPI_BYTE_IN); avr_connect_irq(i_reset, shifter.irq + IRQ_HC595_IN_RESET); avr_connect_irq(i_latch, shifter.irq + IRQ_HC595_IN_LATCH); avr_irq_t * i_pwm = avr_io_getirq(avr, AVR_IOCTL_TIMER_GETIRQ('0'), TIMER_IRQ_OUT_PWM0); avr_irq_register_notify( i_pwm, pwm_changed_hook, NULL); avr_irq_register_notify( shifter.irq + IRQ_HC595_OUT, hc595_changed_hook, NULL); // even if not setup at startup, activate gdb if crashing avr->gdb_port = 1234; if (0) { //avr->state = cpu_Stopped; avr_gdb_init(avr); } /* * VCD file initialization * * This will allow you to create a "wave" file and display it in gtkwave * Pressing "r" and "s" during the demo will start and stop recording * the pin changes */ avr_vcd_init(avr, "gtkwave_output.vcd", &vcd_file, 10000 /* usec */); avr_vcd_add_signal(&vcd_file, avr_get_interrupt_irq(avr, 7), 1 /* bit */ , "TIMER2_COMPA" ); avr_vcd_add_signal(&vcd_file, avr_get_interrupt_irq(avr, 17), 1 /* bit */ , "SPI_INT" ); avr_vcd_add_signal(&vcd_file, i_mosi, 8 /* bits */ , "MOSI" ); avr_vcd_add_signal(&vcd_file, i_reset, 1 /* bit */ , "595_RESET" ); avr_vcd_add_signal(&vcd_file, i_latch, 1 /* bit */ , "595_LATCH" ); avr_vcd_add_signal(&vcd_file, button[B_START].irq + IRQ_BUTTON_OUT, 1 /* bits */ , "start" ); avr_vcd_add_signal(&vcd_file, button[B_STOP].irq + IRQ_BUTTON_OUT, 1 /* bits */ , "stop" ); avr_vcd_add_signal(&vcd_file, button[B_RESET].irq + IRQ_BUTTON_OUT, 1 /* bits */ , "reset" ); avr_vcd_add_signal(&vcd_file, shifter.irq + IRQ_HC595_OUT, 32 /* bits */ , "HC595" ); avr_vcd_add_signal(&vcd_file, i_pwm, 8 /* bits */ , "PWM" ); // 'raise' it, it's a "pullup" avr_raise_irq(button[B_START].irq + IRQ_BUTTON_OUT, 1); avr_raise_irq(button[B_STOP].irq + IRQ_BUTTON_OUT, 1); avr_raise_irq(button[B_RESET].irq + IRQ_BUTTON_OUT, 1); printf( "Demo : This is a real world firmware, a 'stopwatch'\n" " timer that can count up to 99 days. It features a PWM control of the\n" " brightness, blinks the dots, displays the number of days spent and so on.\n\n" " Press '0' to press the 'start' button\n" " Press '1' to press the 'stop' button\n" " Press '2' to press the 'reset' button\n" " Press 'q' to quit\n\n" " Press 'r' to start recording a 'wave' file - with a LOT of data\n" " Press 's' to stop recording\n" " + Make sure to watch the brightness dim once you stop the timer\n\n" ); /* * OpenGL init, can be ignored */ glutInit(&argc, argv); /* initialize GLUT system */ int w = 22, h = 8; glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); glutInitWindowSize(w * pixsize, h * pixsize); /* width=400pixels height=500pixels */ window = glutCreateWindow("Press 0, 1, 2 or q"); /* create window */ // Set up projection matrix glMatrixMode(GL_PROJECTION); // Select projection matrix glLoadIdentity(); // Start with an identity matrix glOrtho(0, w * pixsize, 0, h * pixsize, 0, 10); glScalef(1,-1,1); glTranslatef(0, -1 * h * pixsize, 0); glutDisplayFunc(displayCB); /* set window's display callback */ glutKeyboardFunc(keyCB); /* set window's key callback */ glutTimerFunc(1000 / 24, timerCB, 0); // the AVR run on it's own thread. it even allows for debugging! pthread_t run; pthread_create(&run, NULL, avr_run_thread, NULL); glutMainLoop(); }