main.c 6.1 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. #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. * - 1000 ms : device mounted
  36. * - 2500 ms : device is suspended
  37. */
  38. enum {
  39. BLINK_NOT_MOUNTED = 250,
  40. BLINK_MOUNTED = 1000,
  41. BLINK_SUSPENDED = 2500,
  42. };
  43. static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED;
  44. void led_blinking_task(void);
  45. void cdc_task(void);
  46. void midi_task(void);
  47. /*------------- MAIN -------------*/
  48. int main(void)
  49. {
  50. board_init();
  51. // init device stack on configured roothub port
  52. tud_init(BOARD_TUD_RHPORT);
  53. while (1)
  54. {
  55. tud_task(); // tinyusb device task
  56. led_blinking_task();
  57. cdc_task();
  58. midi_task();
  59. }
  60. return 0;
  61. }
  62. //--------------------------------------------------------------------+
  63. // Device callbacks
  64. //--------------------------------------------------------------------+
  65. // Invoked when device is mounted
  66. void tud_mount_cb(void)
  67. {
  68. blink_interval_ms = BLINK_MOUNTED;
  69. }
  70. // Invoked when device is unmounted
  71. void tud_umount_cb(void)
  72. {
  73. blink_interval_ms = BLINK_NOT_MOUNTED;
  74. }
  75. // Invoked when usb bus is suspended
  76. // remote_wakeup_en : if host allow us to perform remote wakeup
  77. // Within 7ms, device must draw an average of current less than 2.5 mA from bus
  78. void tud_suspend_cb(bool remote_wakeup_en)
  79. {
  80. (void) remote_wakeup_en;
  81. blink_interval_ms = BLINK_SUSPENDED;
  82. }
  83. // Invoked when usb bus is resumed
  84. void tud_resume_cb(void)
  85. {
  86. blink_interval_ms = BLINK_MOUNTED;
  87. }
  88. //--------------------------------------------------------------------+
  89. // USB CDC
  90. //--------------------------------------------------------------------+
  91. void cdc_task(void)
  92. {
  93. if ( tud_cdc_connected() )
  94. {
  95. // connected and there are data available
  96. if ( tud_cdc_available() )
  97. {
  98. uint8_t buf[64];
  99. // read and echo back
  100. uint32_t count = tud_cdc_read(buf, sizeof(buf));
  101. for(uint32_t i=0; i<count; i++)
  102. {
  103. tud_cdc_write_char(buf[i]);
  104. if ( buf[i] == '\r' ) tud_cdc_write_char('\n');
  105. }
  106. tud_cdc_write_flush();
  107. }
  108. }
  109. }
  110. // Invoked when cdc when line state changed e.g connected/disconnected
  111. void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts)
  112. {
  113. (void) itf;
  114. // connected
  115. if ( dtr && rts )
  116. {
  117. // print initial message when connected
  118. tud_cdc_write_str("\r\nTinyUSB CDC MSC device example\r\n");
  119. }
  120. }
  121. // Invoked when CDC interface received data from host
  122. void tud_cdc_rx_cb(uint8_t itf)
  123. {
  124. (void) itf;
  125. }
  126. //--------------------------------------------------------------------+
  127. // MIDI Task
  128. //--------------------------------------------------------------------+
  129. // Variable that holds the current position in the sequence.
  130. uint32_t note_pos = 0;
  131. // Store example melody as an array of note values
  132. static const uint8_t note_sequence[] =
  133. {
  134. 74,78,81,86,90,93,98,102,57,61,66,69,73,78,81,85,88,92,97,100,97,92,88,85,81,78,
  135. 74,69,66,62,57,62,66,69,74,78,81,86,90,93,97,102,97,93,90,85,81,78,73,68,64,61,
  136. 56,61,64,68,74,78,81,86,90,93,98,102
  137. };
  138. void midi_task(void)
  139. {
  140. static uint32_t start_ms = 0;
  141. uint8_t const cable_num = 0; // MIDI jack associated with USB endpoint
  142. uint8_t const channel = 0; // 0 for channel 1
  143. // The MIDI interface always creates input and output port/jack descriptors
  144. // regardless of these being used or not. Therefore incoming traffic should be read
  145. // (possibly just discarded) to avoid the sender blocking in IO
  146. uint8_t packet[4];
  147. while( tud_midi_available() ) tud_midi_packet_read(packet);
  148. // send note every 1000 ms
  149. if (board_millis() - start_ms < 286) return; // not enough time
  150. start_ms += 286;
  151. // Previous positions in the note sequence.
  152. int previous = note_pos - 1;
  153. // If we currently are at position 0, set the
  154. // previous position to the last note in the sequence.
  155. if (previous < 0) previous = sizeof(note_sequence) - 1;
  156. // Send Note On for current position at full velocity (127) on channel 1.
  157. uint8_t note_on[3] = { 0x90 | channel, note_sequence[note_pos], 127 };
  158. tud_midi_stream_write(cable_num, note_on, 3);
  159. // Send Note Off for previous note.
  160. uint8_t note_off[3] = { 0x80 | channel, note_sequence[previous], 0};
  161. tud_midi_stream_write(cable_num, note_off, 3);
  162. // Increment position
  163. note_pos++;
  164. // If we are at the end of the sequence, start over.
  165. if (note_pos >= sizeof(note_sequence)) note_pos = 0;
  166. }
  167. //--------------------------------------------------------------------+
  168. // BLINKING TASK
  169. //--------------------------------------------------------------------+
  170. void led_blinking_task(void)
  171. {
  172. static uint32_t start_ms = 0;
  173. static bool led_state = false;
  174. // Blink every interval ms
  175. if ( board_millis() - start_ms < blink_interval_ms) return; // not enough time
  176. start_ms += blink_interval_ms;
  177. board_led_write(led_state);
  178. led_state = 1 - led_state; // toggle
  179. }