|  | 
|  | 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 | + | 
|  | 26 | +#include <stdio.h> | 
|  | 27 | +#include <stdlib.h> | 
|  | 28 | +#include <string.h> | 
|  | 29 | + | 
|  | 30 | +#include "bsp/board_api.h" | 
|  | 31 | +#include "tusb.h" | 
|  | 32 | + | 
|  | 33 | +//--------------------------------------------------------------------+ | 
|  | 34 | +// STATIC GLOBALS DECLARATION | 
|  | 35 | +//--------------------------------------------------------------------+ | 
|  | 36 | + | 
|  | 37 | +//--------------------------------------------------------------------+ | 
|  | 38 | +// MACRO CONSTANT TYPEDEF PROTOTYPES | 
|  | 39 | +//--------------------------------------------------------------------+ | 
|  | 40 | +void led_blinking_task(void); | 
|  | 41 | +void midi_host_rx_task(void); | 
|  | 42 | + | 
|  | 43 | +/*------------- MAIN -------------*/ | 
|  | 44 | +int main(void) { | 
|  | 45 | +  board_init(); | 
|  | 46 | + | 
|  | 47 | +  printf("TinyUSB Host MIDI Example\r\n"); | 
|  | 48 | + | 
|  | 49 | +  // init host stack on configured roothub port | 
|  | 50 | +  tusb_rhport_init_t host_init = { | 
|  | 51 | +    .role = TUSB_ROLE_HOST, | 
|  | 52 | +    .speed = TUSB_SPEED_AUTO | 
|  | 53 | +  }; | 
|  | 54 | +  tusb_init(BOARD_TUH_RHPORT, &host_init); | 
|  | 55 | + | 
|  | 56 | +  while (1) { | 
|  | 57 | +    tuh_task(); | 
|  | 58 | +    led_blinking_task(); | 
|  | 59 | +    midi_host_rx_task(); | 
|  | 60 | +  } | 
|  | 61 | + | 
|  | 62 | +  return 0; | 
|  | 63 | +} | 
|  | 64 | + | 
|  | 65 | +//--------------------------------------------------------------------+ | 
|  | 66 | +// Blinking Task | 
|  | 67 | +//--------------------------------------------------------------------+ | 
|  | 68 | +void led_blinking_task(void) { | 
|  | 69 | +  const uint32_t interval_ms = 1000; | 
|  | 70 | +  static uint32_t start_ms = 0; | 
|  | 71 | + | 
|  | 72 | +  static bool led_state = false; | 
|  | 73 | + | 
|  | 74 | +  // Blink every interval ms | 
|  | 75 | +  if (board_millis() - start_ms < interval_ms) return;// not enough time | 
|  | 76 | +  start_ms += interval_ms; | 
|  | 77 | + | 
|  | 78 | +  board_led_write(led_state); | 
|  | 79 | +  led_state = 1 - led_state;// toggle | 
|  | 80 | +} | 
|  | 81 | + | 
|  | 82 | +//--------------------------------------------------------------------+ | 
|  | 83 | +// MIDI host receive task | 
|  | 84 | +//--------------------------------------------------------------------+ | 
|  | 85 | +void midi_host_rx_task(void) { | 
|  | 86 | +  // nothing to do, we just print out received data in callback | 
|  | 87 | +} | 
|  | 88 | + | 
|  | 89 | +//--------------------------------------------------------------------+ | 
|  | 90 | +// TinyUSB Callbacks | 
|  | 91 | +//--------------------------------------------------------------------+ | 
|  | 92 | + | 
|  | 93 | +// Invoked when device with MIDI interface is mounted. | 
|  | 94 | +void tuh_midi_mount_cb(uint8_t idx, const tuh_midi_mount_cb_t* mount_cb_data) { | 
|  | 95 | +  printf("MIDI Interface Index = %u, Address = %u, Number of RX cables = %u, Number of TX cables = %u\r\n", | 
|  | 96 | +          idx, mount_cb_data->daddr, mount_cb_data->rx_cable_count, mount_cb_data->tx_cable_count); | 
|  | 97 | +} | 
|  | 98 | + | 
|  | 99 | +// Invoked when device with hid interface is un-mounted | 
|  | 100 | +void tuh_midi_umount_cb(uint8_t idx) { | 
|  | 101 | +  printf("MIDI Interface Index = %u is unmounted\r\n", idx); | 
|  | 102 | +} | 
|  | 103 | + | 
|  | 104 | +void tuh_midi_rx_cb(uint8_t idx, uint32_t xferred_bytes) { | 
|  | 105 | +  if (xferred_bytes == 0) { | 
|  | 106 | +    return; | 
|  | 107 | +  } | 
|  | 108 | + | 
|  | 109 | +  uint8_t buffer[48]; | 
|  | 110 | +  uint8_t cable_num = 0; | 
|  | 111 | +  uint32_t bytes_read = tuh_midi_stream_read(idx, &cable_num, buffer, sizeof(buffer)); | 
|  | 112 | + | 
|  | 113 | +  printf("Cable %u rx: ", cable_num); | 
|  | 114 | +  for (uint32_t i = 0; i < bytes_read; i++) { | 
|  | 115 | +    printf("%02X ", buffer[i]); | 
|  | 116 | +  } | 
|  | 117 | +  printf("\r\n"); | 
|  | 118 | +} | 
|  | 119 | + | 
|  | 120 | +void tuh_midi_tx_cb(uint8_t idx, uint32_t xferred_bytes) { | 
|  | 121 | +  (void) idx; | 
|  | 122 | +  (void) xferred_bytes; | 
|  | 123 | +} | 
0 commit comments