main.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /**************************************************************************/
  2. /*!
  3. @file main.c
  4. @author hathach (tinyusb.org)
  5. @section LICENSE
  6. Software License Agreement (BSD License)
  7. Copyright (c) 2013, hathach (tinyusb.org)
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. 1. Redistributions of source code must retain the above copyright
  12. notice, this list of conditions and the following disclaimer.
  13. 2. Redistributions in binary form must reproduce the above copyright
  14. notice, this list of conditions and the following disclaimer in the
  15. documentation and/or other materials provided with the distribution.
  16. 3. Neither the name of the copyright holders nor the
  17. names of its contributors may be used to endorse or promote products
  18. derived from this software without specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
  20. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
  23. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND
  26. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. This file is part of the tinyusb stack.
  30. */
  31. /**************************************************************************/
  32. //--------------------------------------------------------------------+
  33. // INCLUDE
  34. //--------------------------------------------------------------------+
  35. #include <stdlib.h>
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include "bsp/board.h"
  39. #include "app_os_prio.h"
  40. #include "tusb.h"
  41. #include "tusb_descriptors.h"
  42. #include "msc_device_app.h"
  43. #include "keyboard_device_app.h"
  44. #include "mouse_device_app.h"
  45. #include "cdc_device_app.h"
  46. //--------------------------------------------------------------------+
  47. // MACRO CONSTANT TYPEDEF
  48. //--------------------------------------------------------------------+
  49. //--------------------------------------------------------------------+
  50. // INTERNAL OBJECT & FUNCTION DECLARATION
  51. //--------------------------------------------------------------------+
  52. void print_greeting(void);
  53. void led_blinking_init(void);
  54. void led_blinking_task(void* param);
  55. #if CFG_TUSB_OS == OPT_OS_NONE
  56. // like a real RTOS, this function is a main loop invoking each task in application and never return
  57. void os_none_start_scheduler(void)
  58. {
  59. while (1)
  60. {
  61. tusb_task();
  62. led_blinking_task(NULL);
  63. msc_app_task(NULL);
  64. keyboard_app_task(NULL);
  65. mouse_app_task(NULL);
  66. cdc_serial_app_task(NULL);
  67. }
  68. }
  69. #endif
  70. int main(void)
  71. {
  72. board_init();
  73. print_greeting();
  74. tusb_init();
  75. tud_set_descriptors(&usb_desc_init);
  76. //------------- application task init -------------//
  77. led_blinking_init();
  78. msc_app_init();
  79. keyboard_app_init();
  80. mouse_app_init();
  81. cdc_serial_app_init();
  82. //------------- start OS scheduler (never return) -------------//
  83. #if CFG_TUSB_OS == OPT_OS_FREERTOS
  84. vTaskStartScheduler();
  85. #elif CFG_TUSB_OS == OPT_OS_NONE
  86. os_none_start_scheduler();
  87. #else
  88. #error need to start RTOS schduler
  89. #endif
  90. return 0;
  91. }
  92. //--------------------------------------------------------------------+
  93. // tinyusb callbacks
  94. //--------------------------------------------------------------------+
  95. void tud_mount_cb(uint8_t rhport)
  96. {
  97. cdc_serial_app_mount(rhport);
  98. keyboard_app_mount(rhport);
  99. msc_app_mount(rhport);
  100. }
  101. void tud_umount_cb(uint8_t rhport)
  102. {
  103. cdc_serial_app_umount(rhport);
  104. keyboard_app_umount(rhport);
  105. msc_app_umount(rhport);
  106. }
  107. //--------------------------------------------------------------------+
  108. // BLINKING TASK
  109. //--------------------------------------------------------------------+
  110. static uint32_t led_blink_interval_ms = 1000; // default is 1 second
  111. void led_blinking_init(void)
  112. {
  113. led_blink_interval_ms = 1000;
  114. osal_task_create(led_blinking_task, "blinky", 128, NULL, LED_BLINKING_APP_TASK_PRIO);
  115. }
  116. void led_blinking_set_interval(uint32_t ms)
  117. {
  118. led_blink_interval_ms = ms;
  119. }
  120. tusb_error_t led_blinking_subtask(void);
  121. void led_blinking_task(void* param)
  122. {
  123. (void) param;
  124. OSAL_TASK_BEGIN
  125. led_blinking_subtask();
  126. OSAL_TASK_END
  127. }
  128. tusb_error_t led_blinking_subtask(void)
  129. {
  130. OSAL_SUBTASK_BEGIN
  131. static bool led_state = false;
  132. osal_task_delay(led_blink_interval_ms);
  133. board_led_control(BOARD_LED0, led_state);
  134. led_state = 1 - led_state; // toggle
  135. // uint32_t btn_mask;
  136. // btn_mask = board_buttons();
  137. //
  138. // for(uint8_t i=0; i<32; i++)
  139. // {
  140. // if ( BIT_TEST_(btn_mask, i) ) printf("button %d is pressed\n", i);
  141. // }
  142. OSAL_SUBTASK_END
  143. }
  144. //--------------------------------------------------------------------+
  145. // HELPER FUNCTION
  146. //--------------------------------------------------------------------+
  147. void print_greeting(void)
  148. {
  149. char const * const rtos_name[] =
  150. {
  151. [OPT_OS_NONE] = "None",
  152. [OPT_OS_FREERTOS] = "FreeRTOS",
  153. };
  154. printf("\n\
  155. --------------------------------------------------------------------\n\
  156. - Device Demo (a tinyusb example)\n\
  157. - if you find any bugs or get any questions, feel free to file an\n\
  158. - issue at https://github.com/hathach/tinyusb\n\
  159. --------------------------------------------------------------------\n\n"
  160. );
  161. puts("This DEVICE demo is configured to support:");
  162. printf(" - RTOS = %s\n", rtos_name[CFG_TUSB_OS]);
  163. if (CFG_TUD_HID_MOUSE ) puts(" - HID Mouse");
  164. if (CFG_TUD_HID_KEYBOARD ) puts(" - HID Keyboard");
  165. if (CFG_TUD_MSC ) puts(" - Mass Storage");
  166. if (CFG_TUD_CDC ) puts(" - Communication Device Class");
  167. }