main.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 "tusb.h"
  40. //--------------------------------------------------------------------+
  41. // MACRO CONSTANT TYPEDEF
  42. //--------------------------------------------------------------------+
  43. //--------------------------------------------------------------------+
  44. // INTERNAL OBJECT & FUNCTION DECLARATION
  45. //--------------------------------------------------------------------+
  46. void print_greeting(void);
  47. void led_blinking_task(void);
  48. void virtual_com_task(void);
  49. /*------------- MAIN -------------*/
  50. int main(void)
  51. {
  52. board_init();
  53. print_greeting();
  54. //tusb_init();
  55. while (1)
  56. {
  57. //tusb_task();
  58. led_blinking_task();
  59. //virtual_com_task();
  60. }
  61. return 0;
  62. }
  63. void virtual_com_task(void)
  64. {
  65. // connected and there are data available
  66. if ( tud_mounted(0) && tud_cdc_available(0) )
  67. {
  68. uint8_t buf[64];
  69. // read and echo back
  70. uint32_t count = tud_cdc_read(0, buf, sizeof(buf));
  71. tud_cdc_write(0, buf, count);
  72. }
  73. }
  74. //--------------------------------------------------------------------+
  75. // tinyusb callbacks
  76. //--------------------------------------------------------------------+
  77. void tud_mount_cb(uint8_t port)
  78. {
  79. }
  80. void tud_umount_cb(uint8_t port)
  81. {
  82. }
  83. void tud_cdc_rx_cb(uint8_t port)
  84. {
  85. }
  86. //--------------------------------------------------------------------+
  87. // BLINKING TASK
  88. //--------------------------------------------------------------------+
  89. void led_blinking_task(void)
  90. {
  91. enum { BLINK_INTEVAL = 1000 };
  92. static uint32_t led_on_mask = 0;
  93. static uint32_t last_blink = 0;
  94. // not enough time
  95. if ( last_blink + BLINK_INTEVAL > hal_tick_get() ) return;
  96. last_blink += BLINK_INTEVAL;
  97. board_leds(led_on_mask, 1 - led_on_mask);
  98. led_on_mask = 1 - led_on_mask; // toggle
  99. }
  100. //--------------------------------------------------------------------+
  101. // HELPER FUNCTION
  102. //--------------------------------------------------------------------+
  103. void print_greeting(void)
  104. {
  105. char const * const rtos_name[] =
  106. {
  107. [TUSB_OS_NONE] = "None",
  108. [TUSB_OS_FREERTOS] = "FreeRTOS",
  109. };
  110. printf("\n--------------------------------------------------------------------\n");
  111. printf("- Device Demo (a tinyusb example)\n");
  112. printf("- if you find any bugs or get any questions, feel free to file an\n");
  113. printf("- issue at https://github.com/hathach/tinyusb\n");
  114. printf("--------------------------------------------------------------------\n\n");
  115. printf("This DEVICE demo is configured to support:");
  116. printf(" - RTOS = %s\n", rtos_name[TUSB_CFG_OS]);
  117. if (TUSB_CFG_DEVICE_HID_MOUSE ) puts(" - HID Mouse");
  118. if (TUSB_CFG_DEVICE_HID_KEYBOARD ) puts(" - HID Keyboard");
  119. if (TUSB_CFG_DEVICE_MSC ) puts(" - Mass Storage");
  120. if (TUSB_CFG_DEVICE_CDC ) puts(" - Communication Device Class");
  121. }