snesuploader.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* Name: set-led.c
  2. * Project: custom-class, a basic USB example
  3. * Author: Christian Starkjohann
  4. * Creation Date: 2008-04-10
  5. * Tabsize: 4
  6. * Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH
  7. * License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt)
  8. * This Revision: $Id: set-led.c 692 2008-11-07 15:07:40Z cs $
  9. */
  10. /*
  11. General Description:
  12. This is the host-side driver for the custom-class example device. It searches
  13. the USB for the LEDControl device and sends the requests understood by this
  14. device.
  15. This program must be linked with libusb on Unix and libusb-win32 on Windows.
  16. See http://libusb.sourceforge.net/ or http://libusb-win32.sourceforge.net/
  17. respectively.
  18. */
  19. #define BUFFER_SIZE 8
  20. #define BUFFER_CRC (1024 * 64)
  21. #define BANK_SIZE (1<<15)
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <stdint.h>
  25. #include <string.h>
  26. #include <usb.h> /* this is libusb */
  27. #include "opendevice.h" /* common code moved to separate module */
  28. #include "../requests.h" /* custom request numbers */
  29. #include "../usbconfig.h" /* device's VID/PID and names */
  30. uint16_t crc_xmodem_update (uint16_t crc, uint8_t data){
  31. int i;
  32. crc = crc ^ ((uint16_t)data << 8);
  33. for (i=0; i<8; i++)
  34. {
  35. if (crc & 0x8000)
  36. crc = (crc << 1) ^ 0x1021;
  37. else
  38. crc <<= 1;
  39. }
  40. return crc;
  41. }
  42. uint16_t do_crc(uint8_t * data,uint16_t size){
  43. uint16_t crc =0;
  44. uint16_t i;
  45. for (i=0; i<size; i++){
  46. crc = crc_xmodem_update(crc,data[i]);
  47. }
  48. return crc;
  49. }
  50. uint16_t do_crc_update(uint16_t crc,uint8_t * data,uint16_t size){
  51. uint16_t i;
  52. for (i=0; i<size; i++)
  53. crc = crc_xmodem_update(crc,data[i]);
  54. return crc;
  55. }
  56. static void usage(char *name)
  57. {
  58. fprintf(stderr, "usage:\n");
  59. fprintf(stderr, " %s upload filename.. upload\n", name);
  60. }
  61. int main(int argc, char **argv)
  62. {
  63. usb_dev_handle *handle = NULL;
  64. const unsigned char rawVid[2] = {USB_CFG_VENDOR_ID}, rawPid[2] = {USB_CFG_DEVICE_ID};
  65. char vendor[] = {USB_CFG_VENDOR_NAME, 0}, product[] = {USB_CFG_DEVICE_NAME, 0};
  66. int cnt, vid, pid;
  67. int cnt_crc = 0;
  68. unsigned char *read_buffer;
  69. unsigned char *crc_buffer;
  70. unsigned char setup_buffer[8];
  71. unsigned int addr = 0;
  72. unsigned int bank_addr;
  73. unsigned int bank_num;
  74. uint16_t crc = 0;
  75. FILE *fp ;
  76. usb_init();
  77. if(argc < 2){ /* we need at least one argument */
  78. usage(argv[0]);
  79. exit(1);
  80. }
  81. /* compute VID/PID from usbconfig.h so that there is a central source of information */
  82. vid = rawVid[1] * 256 + rawVid[0];
  83. pid = rawPid[1] * 256 + rawPid[0];
  84. /* The following function is in opendevice.c: */
  85. if(usbOpenDevice(&handle, vid, vendor, pid, product, NULL, NULL, NULL) != 0){
  86. fprintf(stderr, "Could not find USB device \"%s\" with vid=0x%x pid=0x%x\n", product, vid, pid);
  87. exit(1);
  88. }
  89. if(strcasecmp(argv[1], "upload") == 0){
  90. if(argc < 3){ /* we need at least one argument */
  91. usage(argv[0]);
  92. exit(1);
  93. }
  94. fp = fopen (argv[2],"r") ;
  95. if (fp==NULL){
  96. fprintf(stderr, "Cannot open file %s ", argv[2]);
  97. exit(1);
  98. }
  99. read_buffer = (unsigned char*)malloc(BUFFER_SIZE);
  100. crc_buffer = (unsigned char*)malloc(BUFFER_CRC);
  101. memset(crc_buffer,0,BUFFER_CRC);
  102. addr = 0x000000;
  103. bank_addr = addr& 0xffff;
  104. bank_num = (addr>>16) & 0xff;
  105. while((cnt = fread(read_buffer, BUFFER_SIZE, 1, fp)) > 0){
  106. bank_addr = addr& 0xffff;
  107. bank_num = (addr>>16) & 0xff;
  108. memcpy(crc_buffer + cnt_crc,read_buffer,BUFFER_SIZE);
  109. cnt_crc += BUFFER_SIZE;
  110. if (cnt_crc == BANK_SIZE){
  111. crc = do_crc(crc_buffer,BANK_SIZE);
  112. printf("Addr: 0x%06x Bank: 0x%02x Rom Addr: 0x%04x Addr: 0x%06x Cnt: 0x%04x\n",addr,bank_num, bank_addr, addr,crc);
  113. memset(crc_buffer,0,BUFFER_CRC);
  114. cnt_crc =0;
  115. }
  116. usb_control_msg(handle,
  117. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT,
  118. CUSTOM_RQ_UPLOAD, bank_num, bank_addr,
  119. (char*) read_buffer, BUFFER_SIZE,
  120. 5000);
  121. addr += BUFFER_SIZE;
  122. //printf("Addr: 0x%06x Bank: 0x%02x Rom Addr: 0x%04x Addr: 0x%06x Cnt: 0x%04x cnt: %i\n",addr,bank_num, bank_addr, addr,crc,cnt_crc);
  123. }
  124. cnt = usb_control_msg(handle,
  125. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT,
  126. CUSTOM_RQ_CRC_CHECK, bank_num + 1, 0,
  127. (char*) setup_buffer, sizeof(setup_buffer),
  128. 5000);
  129. if(cnt < 1){
  130. if(cnt < 0){
  131. fprintf(stderr, "USB error: %s\n", usb_strerror());
  132. }else{
  133. fprintf(stderr, "only %d bytes received.\n", cnt);
  134. }
  135. }
  136. }else{
  137. usage(argv[0]);
  138. exit(1);
  139. }
  140. usb_close(handle);
  141. return 0;
  142. }