예제 #1
0
파일: main.c 프로젝트: leeku11/KBDMOD_M3
int main(int argc, char **argv)
{
char    *file = NULL;

    if(argc < 2){
        printUsage(argv[0]);
        return 1;
    }
    if(strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0){
        printUsage(argv[0]);
        return 1;
    }
    if(strcmp(argv[1], "-r") == 0){
        leaveBootLoader = 1;
        if(argc >= 3){
            file = argv[2];
        }
    }else{
        file = argv[1];
    }
//    startAddress = sizeof(dataBuffer);
//    endAddress = 0;
    if(file != NULL){   // an upload file was given, load the data
        memset(dataBuffer, -1, sizeof(dataBuffer));
        if(parseIntelHex(file, dataBuffer))
            return 1;
        if(startAddress[0] == endAddress[0]){
            fprintf(stderr, "No data in input file, exiting.\n");
            return 0;
        }
    }
#if 0
{

FILE    *output;
output  = fopen("uploaddata.bin", "w");
fwrite(dataBuffer, 1, sizeof(dataBuffer),output);
fclose(output);
return 0;
}
#endif
    // if no file was given, endAddress is less than startAddress and no data is uploaded


    if(uploadData(dataBuffer))
        return 1;
    return 0;
}
예제 #2
0
/******************************************************************************
* Main function!
******************************************************************************/
int main(int argc, char **argv) {
  int res;
  char *file = NULL;
  micronucleus *my_device = NULL;

  // parse arguments
  int run = 0;
  int file_type = FILE_TYPE_INTEL_HEX;
  int arg_pointer = 1;
  char* usage = "usage: micronucleus [--run] [--dump-progress] [--type intel-hex|raw] [--no-ansi] [--timeout integer] filename";
  progress_step = 0;
  progress_total_steps = 5; // steps: waiting, connecting, parsing, erasing, writing, (running)?
  dump_progress = 0;
  timeout = 0; // no timeout by default
  //#if defined(WIN)
  //  use_ansi = 0;
  //#else
    use_ansi = 1;
  //#endif
  
  while (arg_pointer < argc) {
    if (strcmp(argv[arg_pointer], "--run") == 0) {
      run = 1;
      progress_total_steps += 1;
    } else if (strcmp(argv[arg_pointer], "--type") == 0) {
      arg_pointer += 1;
      if (strcmp(argv[arg_pointer], "intel-hex") == 0) {
        file_type = FILE_TYPE_INTEL_HEX;
      } else if (strcmp(argv[arg_pointer], "raw") == 0) {
        file_type = FILE_TYPE_RAW;
      } else {
        printf("Unknown File Type specified with --type option");
        return EXIT_FAILURE;
      }
    } else if (strcmp(argv[arg_pointer], "--help") == 0 || strcmp(argv[arg_pointer], "-h") == 0) {
      puts(usage);
      puts("");
      puts("  --type [intel-hex, raw]: Set upload file type to either intel hex or raw");
      puts("                           bytes (intel hex is default)");
      puts("          --dump-progress: Output progress data in computer-friendly form");
      puts("                           for driving GUIs");
      puts("                    --run: Ask bootloader to run the program when finished");
      puts("                           uploading provided program");
      //#ifndef WIN
      puts("                --no-ansi: Don't use ANSI in terminal output");
      //#endif
      puts("      --timeout [integer]: Timeout after waiting specified number of seconds");
      puts("                 filename: Path to intel hex or raw data file to upload,");
      puts("                           or \"-\" to read from stdin");
      return EXIT_SUCCESS;
    } else if (strcmp(argv[arg_pointer], "--dump-progress") == 0) {
      dump_progress = 1;
    } else if (strcmp(argv[arg_pointer], "--no-ansi") == 0) {
      use_ansi = 0;
    } else if (strcmp(argv[arg_pointer], "--timeout") == 0) {
      arg_pointer += 1;
      if (sscanf(argv[arg_pointer], "%d", &timeout) != 1) {
        printf("Did not understand --timeout value\n");
        return EXIT_FAILURE;
      }
    } else {
      file = argv[arg_pointer];
    }
    
    arg_pointer += 1;
  }
  
  if (argc < 2) {
    puts(usage);
    return EXIT_FAILURE;
  }
  
  setProgressData("waiting", 1);
  if (dump_progress) printProgress(0.5);
  printf("> Please plug in the device ... \n");
  printf("> Press CTRL+C to terminate the program.\n");
  
  
  time_t start_time, current_time;
  time(&start_time);
  
  while (my_device == NULL) {
    delay(100);
    my_device = micronucleus_connect();
    
    time(&current_time);
    if (timeout && start_time + timeout < current_time) {
      break;
    }
  }
  
  if (my_device == NULL) {
    printf("> Device search timed out\n");
    return EXIT_FAILURE;
  }
  
  printf("> Device is found!\n");
  
  // wait for CONNECT_WAIT milliseconds with progress output
  float wait = 0.0f;
  setProgressData("connecting", 2);
  while (wait < CONNECT_WAIT) {
    printProgress((wait / ((float) CONNECT_WAIT)) * 0.9f);
    wait += 50.0f;
    delay(50);
  }
  
  //my_device = micronucleus_connect();
  printProgress(1.0);
    
  // if (my_device->page_size == 64) {
  //   printf("> Device looks like ATtiny85!\n");
  // } else if (my_device->page_size == 32)  {
  //   printf("> Device looks like ATtiny45!\n");
  // } else {
  //   printf("> Unsupported device!\n");
  //   return EXIT_FAILURE;
  // }
  
  printf("> Available space for user application: %d bytes\n", my_device->flash_size);
  printf("> Suggested sleep time between sending pages: %ums\n", my_device->write_sleep);
  printf("> Whole page count: %d\n", my_device->pages);
  printf("> Erase function sleep duration: %dms\n", my_device->erase_sleep);
  
  setProgressData("parsing", 3);
  printProgress(0.0);
  memset(dataBuffer, 0xFF, sizeof(dataBuffer));
  
  int startAddress = 1, endAddress = 0;
  if (file_type == FILE_TYPE_INTEL_HEX) {
    if (parseIntelHex(file, dataBuffer, &startAddress, &endAddress)) {
      printf("> Error loading or parsing hex file.\n");
      return EXIT_FAILURE;
    }
  } else if (file_type == FILE_TYPE_RAW) {
    if (parseRaw(file, dataBuffer, &startAddress, &endAddress)) {
      printf("> Error loading raw file.\n");
      return EXIT_FAILURE;
    }
  }
  
  printProgress(1.0);

  if (startAddress >= endAddress) {
    printf("> No data in input file, exiting.\n");
    return EXIT_FAILURE;
  }
  
  if (endAddress > my_device->flash_size) {
    printf("> Program file is %d bytes too big for the bootloader!\n", endAddress - my_device->flash_size);
    return EXIT_FAILURE;
  }
  
  setProgressData("erasing", 4);
  printf("> Erasing the memory ...\n");
  res = micronucleus_eraseFlash(my_device, printProgress);
  
  if (res == 1) { // erase disconnection bug workaround
    printf(">> Eep! Connection to device lost during erase! Not to worry\n");
    printf(">> This happens on some computers - reconnecting...\n");
    my_device = NULL;
    
    delay(CONNECT_WAIT);
    
    int deciseconds_till_reconnect_notice = 50; // notice after 5 seconds
    while (my_device == NULL) {
      delay(100);
      my_device = micronucleus_connect();
      deciseconds_till_reconnect_notice -= 1;
      
      if (deciseconds_till_reconnect_notice == 0) {
        printf(">> (!) Automatic reconnection not working. Unplug and reconnect\n");
        printf("   device usb connector, or reset it some other way to continue.\n");
      }
    }
    
    printf(">> Reconnected! Continuing upload sequence...\n");
    
  } else if (res != 0) {
    printf(">> Abort mission! %d error has occured ...\n", res);
    printf(">> Please unplug the device and restart the program.\n");
    return EXIT_FAILURE;
  }
  printProgress(1.0);
  
  printf("> Starting to upload ...\n");
  setProgressData("writing", 5);
  res = micronucleus_writeFlash(my_device, endAddress, dataBuffer, printProgress);
  if (res != 0) {
    printf(">> Abort mission! An error has occured ...\n");
    printf(">> Please unplug the device and restart the program.\n");
    return EXIT_FAILURE;
  }
  
  if (run) {
    
    printf("> Starting the user app ...\n");
    setProgressData("running", 6);
    printProgress(0.0);
    
    res = micronucleus_startApp(my_device);
    
    if (res != 0) {
      printf(">> Abort mission! An error has occured ...\n");
      printf(">> Please unplug the device and restart the program. \n");
      return EXIT_FAILURE;
    }
    
    printProgress(1.0);
  }
  
  printf(">> Micronucleus done. Thank you!\n");
  
  return EXIT_SUCCESS;
}