main.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include "bsp/board.h"
  29. #include "tusb.h"
  30. //--------------------------------------------------------------------+
  31. // MACRO CONSTANT TYPEDEF PROTYPES
  32. //--------------------------------------------------------------------+
  33. /* Blink pattern
  34. * - 250 ms : device not mounted
  35. * - 0 ms : device mounted
  36. * - 2500 ms : device is suspended
  37. */
  38. enum {
  39. BLINK_NOT_MOUNTED = 250,
  40. BLINK_MOUNTED = 0,
  41. BLINK_SUSPENDED = 2500,
  42. };
  43. static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED;
  44. void led_blinking_task(void);
  45. /*------------- MAIN -------------*/
  46. int main(void)
  47. {
  48. board_init();
  49. tusb_init();
  50. while (1)
  51. {
  52. tud_task(); // tinyusb device task
  53. led_blinking_task();
  54. }
  55. return 0;
  56. }
  57. //--------------------------------------------------------------------+
  58. // Device callbacks
  59. //--------------------------------------------------------------------+
  60. // Invoked when device is mounted
  61. void tud_mount_cb(void)
  62. {
  63. blink_interval_ms = BLINK_MOUNTED;
  64. }
  65. // Invoked when device is unmounted
  66. void tud_umount_cb(void)
  67. {
  68. blink_interval_ms = BLINK_NOT_MOUNTED;
  69. }
  70. // Invoked when usb bus is suspended
  71. // remote_wakeup_en : if host allow us to perform remote wakeup
  72. // Within 7ms, device must draw an average of current less than 2.5 mA from bus
  73. void tud_suspend_cb(bool remote_wakeup_en)
  74. {
  75. (void) remote_wakeup_en;
  76. blink_interval_ms = BLINK_SUSPENDED;
  77. }
  78. // Invoked when usb bus is resumed
  79. void tud_resume_cb(void)
  80. {
  81. blink_interval_ms = BLINK_MOUNTED;
  82. }
  83. //--------------------------------------------------------------------+
  84. // BLINKING TASK + Indicator pulse
  85. //--------------------------------------------------------------------+
  86. volatile uint8_t doPulse = false;
  87. // called from USB context
  88. void led_indicator_pulse(void) {
  89. doPulse = true;
  90. }
  91. void led_blinking_task(void)
  92. {
  93. static uint32_t start_ms = 0;
  94. static bool led_state = false;
  95. if(blink_interval_ms == BLINK_MOUNTED) // Mounted
  96. {
  97. if(doPulse)
  98. {
  99. led_state = true;
  100. board_led_write(true);
  101. start_ms = board_millis();
  102. doPulse = false;
  103. }
  104. else if (led_state == true)
  105. {
  106. if ( board_millis() - start_ms < 750) //Spec says blink must be between 500 and 1000 ms.
  107. {
  108. return; // not enough time
  109. }
  110. led_state = false;
  111. board_led_write(false);
  112. }
  113. }
  114. else
  115. {
  116. // Blink every interval ms
  117. if ( board_millis() - start_ms < blink_interval_ms) return; // not enough time
  118. start_ms += blink_interval_ms;
  119. board_led_write(led_state);
  120. led_state = 1 - led_state; // toggle
  121. }
  122. }