int avr_init(avr_t * avr) { avr->flash = malloc(avr->flashend + 1); memset(avr->flash, 0xff, avr->flashend + 1); avr->codeend = avr->flashend; avr->data = malloc(avr->ramend + 1); memset(avr->data, 0, avr->ramend + 1); #ifdef CONFIG_SIMAVR_TRACE avr->trace_data = calloc(1, sizeof(struct avr_trace_data_t)); #endif AVR_LOG(avr, LOG_TRACE, "%s init\n", avr->mmcu); // cpu is in limbo before init is finished. avr->state = cpu_Limbo; avr->frequency = 1000000; // can be overridden via avr_mcu_section avr_cmd_init(avr); avr_interrupt_init(avr); if (avr->custom.init) avr->custom.init(avr, avr->custom.data); if (avr->init) avr->init(avr); // set default (non gdb) fast callbacks avr->run = avr_callback_run_raw; avr->sleep = avr_callback_sleep_raw; // number of address bytes to push/pull on/off the stack avr->address_size = avr->eind ? 3 : 2; avr->log = 1; avr_reset(avr); return 0; }
int avr_init(avr_t * avr) { avr->flash = malloc(avr->flashend + 1); memset(avr->flash, 0xff, avr->flashend + 1); avr->data = malloc(avr->ramend + 1); memset(avr->data, 0, avr->ramend + 1); #ifdef CONFIG_SIMAVR_TRACE avr->trace_data = calloc(1, sizeof(struct avr_trace_data_t)); #endif // cpu is in limbo before init is finished. avr->state = cpu_Limbo; avr->frequency = 1000000; // can be overriden via avr_mcu_section if (avr->special_init) avr->special_init(avr); if (avr->init) avr->init(avr); // set default (non gdb) fast callbacks avr->run = avr_callback_run_raw; avr->sleep = avr_callback_sleep_raw; avr->state = cpu_Running; avr->log = 1; avr_reset(avr); return 0; }
static void handler(int sig) { switch (sig) { case SIGINT: case SIGTERM: avr_terminate(avr); exit(EXIT_FAILURE); break; case SIGHUP: avr_reset(avr); break; } }
static void avr_watchdog_run_callback_software_reset(avr_t * avr) { avr_reset(avr); }
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; }