main.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <getopt.h>
  4. #include <sprog.h>
  5. #include <serial.h>
  6. #include <lpc.h>
  7. struct option sprog_options[] = {
  8. {"port", required_argument, NULL, 'p'},
  9. {"family", required_argument, NULL, 'f'},
  10. {"baud", required_argument, NULL, 'b'},
  11. {"exec", required_argument, NULL, 'e'},
  12. {"write", required_argument, NULL, 'w'},
  13. {"version", no_argument, NULL, 'v'},
  14. {"help", no_argument, NULL, 'h'},
  15. {NULL, 0, NULL, 0}
  16. };
  17. void usage(const char *name);
  18. void print_version(void);
  19. int main(int argc, char *argv[]) {
  20. const struct sprog_family *fam;
  21. struct sprog_data d;
  22. int opt;
  23. int option_index;
  24. int baud;
  25. int mode;
  26. char port[128];
  27. char path[128];
  28. port[0] = 0;
  29. path[0] = 0;
  30. fam = NULL;
  31. baud = 9600;
  32. mode = 0;
  33. while((opt = getopt_long(argc, argv, "p:b:f:e:w:h", sprog_options, &option_index))!=-1) {
  34. switch(opt) {
  35. case 'p':
  36. strncpy(port, optarg, sizeof(port));
  37. break;
  38. case 'e':
  39. mode = 1;
  40. strncpy(path, optarg, sizeof(path));
  41. break;
  42. case 'w':
  43. mode = 2;
  44. strncpy(path, optarg, sizeof(path));
  45. break;
  46. case 'f':
  47. if(strcmp(optarg, "lpc")==0)
  48. fam = &lpc_family;
  49. else
  50. sprog_error("Family '%s' not recognized\n", optarg);
  51. break;
  52. case 'b':
  53. if(sscanf(optarg, "%d", &baud)!=1) {
  54. sprog_error("Invalid value for baud rate: '%s'\n", optarg);
  55. return 1;
  56. }
  57. break;
  58. case 'v':
  59. print_version();
  60. return 0;
  61. break;
  62. case 'h':
  63. usage(argv[0]);
  64. return 0;
  65. break;
  66. default:
  67. usage(argv[0]);
  68. return 1;
  69. }
  70. }
  71. if(!port[0]) {
  72. sprog_error("Serial port not specified!\n");
  73. usage(argv[0]);
  74. return 1;
  75. }
  76. if(!fam) {
  77. sprog_error("Family not specified!\n");
  78. usage(argv[0]);
  79. return 1;
  80. }
  81. if(mode) {
  82. d.data = NULL;
  83. d.size = 0;
  84. sprog_load(&d, path);
  85. sprog_communicate(fam, &d, mode, port, baud);
  86. } else
  87. sprog_communicate(fam, NULL, mode, port, baud);
  88. return 0;
  89. }
  90. void usage(const char *name) {
  91. printf("%s -p <port> -f <family> [options]\n", name);
  92. printf("Following options are supported:\n");
  93. printf(" -p <port>, --port <port> specify the serial port, eg. /dev/ttyS0\n");
  94. printf(" -f <family>, --family <family> specify the microcontroller family, eg. lpc\n");
  95. printf(" -b <baud>, --baud <baud> specify the baud rate\n");
  96. printf(" -e <file>, --exec <file> execute the given file\n");
  97. printf(" -w <file>, --write <file> write the given file to Flash\n");
  98. printf(" -h, --help display this help\n");
  99. printf(" --version print version and exit\n");
  100. }
  101. void print_version(void) {
  102. printf("SProg v%s (built on " __DATE__ ")\n", sprog_version);
  103. }