main.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2019 Ha Thach (tinyusb.org)
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. *
  24. */
  25. /*
  26. * After device is enumerated in dfu mode run the following commands
  27. *
  28. * To transfer firmware from host to device (best to test with text file)
  29. *
  30. * $ dfu-util -d cafe -a 0 -D [filename]
  31. * $ dfu-util -d cafe -a 1 -D [filename]
  32. *
  33. * To transfer firmware from device to host:
  34. *
  35. * $ dfu-util -d cafe -a 0 -U [filename]
  36. * $ dfu-util -d cafe -a 1 -U [filename]
  37. *
  38. */
  39. #include <stdlib.h>
  40. #include <stdio.h>
  41. #include <string.h>
  42. #include "bsp/board.h"
  43. #include "tusb.h"
  44. //--------------------------------------------------------------------+
  45. // MACRO CONSTANT TYPEDEF PROTYPES
  46. //--------------------------------------------------------------------+
  47. const char* upload_image[2]=
  48. {
  49. "Hello world from TinyUSB DFU! - Partition 0",
  50. "Hello world from TinyUSB DFU! - Partition 1"
  51. };
  52. /* Blink pattern
  53. * - 250 ms : device not mounted
  54. * - 1000 ms : device mounted
  55. * - 2500 ms : device is suspended
  56. */
  57. enum {
  58. BLINK_NOT_MOUNTED = 250,
  59. BLINK_MOUNTED = 1000,
  60. BLINK_SUSPENDED = 2500,
  61. };
  62. static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED;
  63. void led_blinking_task(void);
  64. /*------------- MAIN -------------*/
  65. int main(void)
  66. {
  67. board_init();
  68. tusb_init();
  69. while (1)
  70. {
  71. tud_task(); // tinyusb device task
  72. led_blinking_task();
  73. }
  74. return 0;
  75. }
  76. //--------------------------------------------------------------------+
  77. // Device callbacks
  78. //--------------------------------------------------------------------+
  79. // Invoked when device is mounted
  80. void tud_mount_cb(void)
  81. {
  82. blink_interval_ms = BLINK_MOUNTED;
  83. }
  84. // Invoked when device is unmounted
  85. void tud_umount_cb(void)
  86. {
  87. blink_interval_ms = BLINK_NOT_MOUNTED;
  88. }
  89. // Invoked when usb bus is suspended
  90. // remote_wakeup_en : if host allow us to perform remote wakeup
  91. // Within 7ms, device must draw an average of current less than 2.5 mA from bus
  92. void tud_suspend_cb(bool remote_wakeup_en)
  93. {
  94. (void) remote_wakeup_en;
  95. blink_interval_ms = BLINK_SUSPENDED;
  96. }
  97. // Invoked when usb bus is resumed
  98. void tud_resume_cb(void)
  99. {
  100. blink_interval_ms = BLINK_MOUNTED;
  101. }
  102. //--------------------------------------------------------------------+
  103. // DFU callbacks
  104. // Note: alt is used as the partition number, in order to support multiple partitions like FLASH, EEPROM, etc.
  105. //--------------------------------------------------------------------+
  106. // Invoked right before tud_dfu_download_cb() (state=DFU_DNBUSY) or tud_dfu_manifest_cb() (state=DFU_MANIFEST)
  107. // Application return timeout in milliseconds (bwPollTimeout) for the next download/manifest operation.
  108. // During this period, USB host won't try to communicate with us.
  109. uint32_t tud_dfu_get_timeout_cb(uint8_t alt, uint8_t state)
  110. {
  111. if ( state == DFU_DNBUSY )
  112. {
  113. // For this example
  114. // - Atl0 Flash is fast : 1 ms
  115. // - Alt1 EEPROM is slow: 100 ms
  116. return (alt == 0) ? 1 : 100;
  117. }
  118. else if (state == DFU_MANIFEST)
  119. {
  120. // since we don't buffer entire image and do any flashing in manifest stage
  121. return 0;
  122. }
  123. return 0;
  124. }
  125. // Invoked when received DFU_DNLOAD (wLength>0) following by DFU_GETSTATUS (state=DFU_DNBUSY) requests
  126. // This callback could be returned before flashing op is complete (async).
  127. // Once finished flashing, application must call tud_dfu_finish_flashing()
  128. void tud_dfu_download_cb(uint8_t alt, uint16_t block_num, uint8_t const* data, uint16_t length)
  129. {
  130. (void) alt;
  131. (void) block_num;
  132. //printf("\r\nReceived Alt %u BlockNum %u of length %u\r\n", alt, wBlockNum, length);
  133. for(uint16_t i=0; i<length; i++)
  134. {
  135. printf("%c", data[i]);
  136. }
  137. // flashing op for download complete without error
  138. tud_dfu_finish_flashing(DFU_STATUS_OK);
  139. }
  140. // Invoked when download process is complete, received DFU_DNLOAD (wLength=0) following by DFU_GETSTATUS (state=Manifest)
  141. // Application can do checksum, or actual flashing if buffered entire image previously.
  142. // Once finished flashing, application must call tud_dfu_finish_flashing()
  143. void tud_dfu_manifest_cb(uint8_t alt)
  144. {
  145. (void) alt;
  146. printf("Download completed, enter manifestation\r\n");
  147. // flashing op for manifest is complete without error
  148. // Application can perform checksum, should it fail, use appropriate status such as errVERIFY.
  149. tud_dfu_finish_flashing(DFU_STATUS_OK);
  150. }
  151. // Invoked when received DFU_UPLOAD request
  152. // Application must populate data with up to length bytes and
  153. // Return the number of written bytes
  154. uint16_t tud_dfu_upload_cb(uint8_t alt, uint16_t block_num, uint8_t* data, uint16_t length)
  155. {
  156. (void) block_num;
  157. (void) length;
  158. uint16_t const xfer_len = (uint16_t) strlen(upload_image[alt]);
  159. memcpy(data, upload_image[alt], xfer_len);
  160. return xfer_len;
  161. }
  162. // Invoked when the Host has terminated a download or upload transfer
  163. void tud_dfu_abort_cb(uint8_t alt)
  164. {
  165. (void) alt;
  166. printf("Host aborted transfer\r\n");
  167. }
  168. // Invoked when a DFU_DETACH request is received
  169. void tud_dfu_detach_cb(void)
  170. {
  171. printf("Host detach, we should probably reboot\r\n");
  172. }
  173. //--------------------------------------------------------------------+
  174. // BLINKING TASK + Indicator pulse
  175. //--------------------------------------------------------------------+
  176. void led_blinking_task(void)
  177. {
  178. static uint32_t start_ms = 0;
  179. static bool led_state = false;
  180. // Blink every interval ms
  181. if ( board_millis() - start_ms < blink_interval_ms) return; // not enough time
  182. start_ms += blink_interval_ms;
  183. board_led_write(led_state);
  184. led_state = 1 - led_state; // toggle
  185. }