main.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright (C) 2013 Michael McMaster <michael@codesrc.com>
  2. //
  3. // This file is part of SCSI2SD.
  4. //
  5. // SCSI2SD is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // SCSI2SD is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with SCSI2SD. If not, see <http://www.gnu.org/licenses/>.
  17. #include <stdint.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include "hidapi.h"
  23. #include "cybtldr_api.h"
  24. #include "cybtldr_api2.h"
  25. hid_device *handle = NULL;
  26. static int OpenConnection(void)
  27. {
  28. return 0;
  29. }
  30. static int CloseConnection(void)
  31. {
  32. return 0;
  33. }
  34. static int ReadData(unsigned char* data, int count)
  35. {
  36. unsigned char buf[65];
  37. buf[0] = 0; // Report ID
  38. int result = hid_read(handle, buf, count);
  39. if (result < 0)
  40. {
  41. fprintf(stderr, "USB HID Read Failure: %ls\n", hid_error(handle));
  42. }
  43. memcpy(data, buf, count);
  44. return (result >= 0) ? 0 : -1;
  45. }
  46. static int WriteData(unsigned char* data, int count)
  47. {
  48. unsigned char buf[65];
  49. buf[0] = 0; // report ID
  50. int i;
  51. for (i = 0; i < count; ++i)
  52. {
  53. buf[i+1] = data[i];
  54. }
  55. int result = hid_write(handle, buf, count + 1);
  56. if (result < 0)
  57. {
  58. fprintf(stderr, "USB HID Write Failure: %ls\n", hid_error(handle));
  59. }
  60. return (result >= 0) ? 0 : -1;
  61. }
  62. static void ProgressUpdate(unsigned char arrayId, unsigned short rowNum)
  63. {
  64. printf("Programmed flash array %d, row %d\n", arrayId, rowNum);
  65. }
  66. static void usage()
  67. {
  68. printf("Usage: bootloaderhost [-v UsbVendorId] [-p UsbProductId] /path/to/firmware.cyacd\n");
  69. printf("\n\n");
  70. }
  71. int main(int argc, char* argv[])
  72. {
  73. CyBtldr_CommunicationsData cyComms =
  74. {
  75. &OpenConnection,
  76. &CloseConnection,
  77. &ReadData,
  78. &WriteData,
  79. 64
  80. };
  81. printf("PSoC 3/5LP USB HID Bootloader Host\n");
  82. printf("Copyright (C) 2013 Michael McMaster <michael@codesrc.com>\n\n");
  83. uint16_t vendorId = 0x04B4; // Cypress
  84. uint16_t productId = 0xB71D; // Default PSoC3/5LP Bootloader
  85. opterr = 0;
  86. int c;
  87. while ((c = getopt(argc, argv, "v:p:")) != -1)
  88. {
  89. switch (c)
  90. {
  91. case 'v':
  92. sscanf(optarg, "%hx", &vendorId);
  93. break;
  94. case 'p':
  95. sscanf(optarg, "%hx", &productId);
  96. break;
  97. case '?':
  98. usage();
  99. exit(1);
  100. }
  101. }
  102. const char* filename;
  103. if (optind < argc)
  104. {
  105. filename = argv[optind];
  106. }
  107. else
  108. {
  109. usage();
  110. exit(1);
  111. }
  112. printf(
  113. "USB device parameters\n\tVendor ID:\t0x%04X\n\tProduct ID:\t0x%04X\n",
  114. vendorId,
  115. productId);
  116. // Enumerate and print the HID devices on the system
  117. struct hid_device_info *dev = hid_enumerate(vendorId, productId);
  118. if (!dev)
  119. {
  120. printf("Waiting for device connection\n");
  121. printf("Connect USB cable to the bus-powered device now, or otherwise reset the device.\n");
  122. }
  123. while (!dev)
  124. {
  125. dev = hid_enumerate(vendorId, productId);
  126. usleep(10000); // 10ms
  127. }
  128. printf("Device Found\n type: %04hx %04hx\n path: %s\n serial_number: %ls",
  129. dev->vendor_id, dev->product_id, dev->path, dev->serial_number);
  130. printf("\n");
  131. printf(" Manufacturer: %ls\n", dev->manufacturer_string);
  132. printf(" Product: %ls\n", dev->product_string);
  133. printf("\n");
  134. //hid_free_enumeration(devs);
  135. // Open the device using the VID, PID,
  136. // and optionally the Serial number.
  137. handle = hid_open(vendorId, productId, NULL);
  138. if (!handle)
  139. {
  140. fprintf(
  141. stderr,
  142. "Could not open device %s. Check permissions.\n", dev->path
  143. );
  144. exit(1);
  145. }
  146. printf("Starting firmware upload: %s\n", filename);
  147. int result = CyBtldr_Program(
  148. filename,
  149. &cyComms,
  150. &ProgressUpdate);
  151. if (result == 0)
  152. {
  153. printf("Firmware update complete\n");
  154. }
  155. else
  156. {
  157. printf("Firmware update failed\n");
  158. }
  159. return 0;
  160. }