main.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. /* After device is enumerated, run following command
  26. *
  27. * $ dfu-util -l
  28. *
  29. * It should be able to list our device as in Runtime mode. Then run
  30. *
  31. * $ dfu-util -e
  32. *
  33. * This will send DETTACH command to put device into bootloader. Since this example
  34. * is minimal, it doesn't actually go into DFU mode but rather change the LED blinking
  35. * pattern to fast rate as indicator.
  36. */
  37. #include <stdlib.h>
  38. #include <stdio.h>
  39. #include <string.h>
  40. #include "bsp/board.h"
  41. #include "tusb.h"
  42. //--------------------------------------------------------------------+
  43. // MACRO CONSTANT TYPEDEF PROTYPES
  44. //--------------------------------------------------------------------+
  45. /* Blink pattern
  46. * - 1000 ms : device should reboot
  47. * - 250 ms : device not mounted
  48. * - 0 ms : device mounted
  49. * - 2500 ms : device is suspended
  50. */
  51. enum {
  52. BLINK_DFU_MODE = 100,
  53. BLINK_NOT_MOUNTED = 250,
  54. BLINK_MOUNTED = 1000,
  55. BLINK_SUSPENDED = 2500,
  56. };
  57. static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED;
  58. void led_blinking_task(void);
  59. /*------------- MAIN -------------*/
  60. int main(void)
  61. {
  62. board_init();
  63. // init device stack on configured roothub port
  64. tud_init(BOARD_TUD_RHPORT);
  65. while (1)
  66. {
  67. tud_task(); // tinyusb device task
  68. led_blinking_task();
  69. }
  70. return 0;
  71. }
  72. //--------------------------------------------------------------------+
  73. // Device callbacks
  74. //--------------------------------------------------------------------+
  75. // Invoked when device is mounted
  76. void tud_mount_cb(void)
  77. {
  78. blink_interval_ms = BLINK_MOUNTED;
  79. }
  80. // Invoked when device is unmounted
  81. void tud_umount_cb(void)
  82. {
  83. blink_interval_ms = BLINK_NOT_MOUNTED;
  84. }
  85. // Invoked when usb bus is suspended
  86. // remote_wakeup_en : if host allow us to perform remote wakeup
  87. // Within 7ms, device must draw an average of current less than 2.5 mA from bus
  88. void tud_suspend_cb(bool remote_wakeup_en)
  89. {
  90. (void) remote_wakeup_en;
  91. blink_interval_ms = BLINK_SUSPENDED;
  92. }
  93. // Invoked when usb bus is resumed
  94. void tud_resume_cb(void)
  95. {
  96. blink_interval_ms = BLINK_MOUNTED;
  97. }
  98. // Invoked on DFU_DETACH request to reboot to the bootloader
  99. void tud_dfu_runtime_reboot_to_dfu_cb(void)
  100. {
  101. blink_interval_ms = BLINK_DFU_MODE;
  102. }
  103. //--------------------------------------------------------------------+
  104. // BLINKING TASK + Indicator pulse
  105. //--------------------------------------------------------------------+
  106. void led_blinking_task(void)
  107. {
  108. static uint32_t start_ms = 0;
  109. static bool led_state = false;
  110. // Blink every interval ms
  111. if ( board_millis() - start_ms < blink_interval_ms) return; // not enough time
  112. start_ms += blink_interval_ms;
  113. board_led_write(led_state);
  114. led_state = 1 - led_state; // toggle
  115. }