summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoratok <[email protected]>2022-09-08 21:13:55 +0200
committeratoktoto <[email protected]>2022-11-13 14:31:12 +0100
commit19563b441463aed9b3d622f2d57d61082199fbdd (patch)
tree1c9e3d0585400993f70d93b07888281d37ed03c0
parent4fbf99621d5a5b8b5de698a792578a2066c667f3 (diff)
Simple MIDI rx example
-rw-r--r--examples/host/CMakeLists.txt1
-rw-r--r--examples/host/midi_rx/CMakeLists.txt27
-rw-r--r--examples/host/midi_rx/only.txt1
-rw-r--r--examples/host/midi_rx/src/main.c152
-rw-r--r--examples/host/midi_rx/src/tusb_config.h96
-rw-r--r--src/class/midi/midi_host.c4
6 files changed, 279 insertions, 2 deletions
diff --git a/examples/host/CMakeLists.txt b/examples/host/CMakeLists.txt
index 5c63ec0c0..39a41c4ae 100644
--- a/examples/host/CMakeLists.txt
+++ b/examples/host/CMakeLists.txt
@@ -8,3 +8,4 @@ family_initialize_project(tinyusb_host_examples ${CMAKE_CURRENT_LIST_DIR})
# family_add_subdirectory will filter what to actually add based on selected FAMILY
family_add_subdirectory(cdc_msc_hid)
family_add_subdirectory(hid_controller)
+family_add_subdirectory(midi_rx) \ No newline at end of file
diff --git a/examples/host/midi_rx/CMakeLists.txt b/examples/host/midi_rx/CMakeLists.txt
new file mode 100644
index 000000000..4856e0a47
--- /dev/null
+++ b/examples/host/midi_rx/CMakeLists.txt
@@ -0,0 +1,27 @@
+cmake_minimum_required(VERSION 3.5)
+
+include(${CMAKE_CURRENT_SOURCE_DIR}/../../../hw/bsp/family_support.cmake)
+
+# gets PROJECT name for the example (e.g. <BOARD>-<DIR_NAME>)
+family_get_project_name(PROJECT ${CMAKE_CURRENT_LIST_DIR})
+
+project(${PROJECT})
+
+# Checks this example is valid for the family and initializes the project
+family_initialize_project(${PROJECT} ${CMAKE_CURRENT_LIST_DIR})
+
+add_executable(${PROJECT})
+
+# Example source
+target_sources(${PROJECT} PUBLIC
+ ${CMAKE_CURRENT_SOURCE_DIR}/src/main.c
+ )
+
+# Example include
+target_include_directories(${PROJECT} PUBLIC
+ ${CMAKE_CURRENT_SOURCE_DIR}/src
+ )
+
+# Configure compilation flags and libraries for the example... see the corresponding function
+# in hw/bsp/FAMILY/family.cmake for details.
+family_configure_host_example(${PROJECT}) \ No newline at end of file
diff --git a/examples/host/midi_rx/only.txt b/examples/host/midi_rx/only.txt
new file mode 100644
index 000000000..2ba4438fd
--- /dev/null
+++ b/examples/host/midi_rx/only.txt
@@ -0,0 +1 @@
+mcu:RP2040
diff --git a/examples/host/midi_rx/src/main.c b/examples/host/midi_rx/src/main.c
new file mode 100644
index 000000000..64428932f
--- /dev/null
+++ b/examples/host/midi_rx/src/main.c
@@ -0,0 +1,152 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 Ha Thach (tinyusb.org)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "bsp/board.h"
+#include "tusb.h"
+#include "class/midi/midi_host.h"
+
+
+#if CFG_TUH_MIDI
+
+//--------------------------------------------------------------------+
+// STATIC GLOBALS DECLARATION
+//--------------------------------------------------------------------+
+static uint8_t midi_dev_addr = 0;
+
+//--------------------------------------------------------------------+
+// MACRO CONSTANT TYPEDEF PROTYPES
+//--------------------------------------------------------------------+
+void led_blinking_task(void);
+void midi_host_rx_task(void);
+
+/*------------- MAIN -------------*/
+int main(void)
+{
+ board_init();
+
+ printf("TinyUSB Host MIDI Example\r\n");
+
+ tusb_init();
+
+ while (1)
+ {
+ tuh_task();
+ led_blinking_task();
+ midi_host_rx_task();
+ }
+
+ return 0;
+}
+
+#endif
+
+//--------------------------------------------------------------------+
+// Blinking Task
+//--------------------------------------------------------------------+
+void led_blinking_task(void)
+{
+ const uint32_t interval_ms = 1000;
+ static uint32_t start_ms = 0;
+
+ static bool led_state = false;
+
+ // Blink every interval ms
+ if ( board_millis() - start_ms < interval_ms) return; // not enough time
+ start_ms += interval_ms;
+
+ board_led_write(led_state);
+ led_state = 1 - led_state; // toggle
+}
+
+//--------------------------------------------------------------------+
+// MIDI host receive task
+//--------------------------------------------------------------------+
+void midi_host_rx_task(void)
+{
+ // device must be attached and have at least one endpoint ready to receive a message
+ if (!midi_dev_addr || !tuh_midi_configured(midi_dev_addr))
+ {
+ return;
+ }
+ if (tuh_midih_get_num_rx_cables(midi_dev_addr) < 1)
+ {
+ return;
+ }
+ tuh_midi_read_poll(midi_dev_addr);
+}
+
+//--------------------------------------------------------------------+
+// TinyUSB Callbacks
+//--------------------------------------------------------------------+
+
+// Invoked when device with hid interface is mounted
+// Report descriptor is also available for use. tuh_hid_parse_report_descriptor()
+// can be used to parse common/simple enough descriptor.
+// Note: if report descriptor length > CFG_TUH_ENUMERATION_BUFSIZE, it will be skipped
+// therefore report_desc = NULL, desc_len = 0
+void tuh_midi_mount_cb(uint8_t dev_addr, uint8_t in_ep, uint8_t out_ep, uint8_t num_cables_rx, uint16_t num_cables_tx)
+{
+ TU_LOG1("MIDI device address = %u, IN endpoint %u has %u cables, OUT endpoint %u has %u cables\r\n",
+ dev_addr, in_ep & 0xf, num_cables_rx, out_ep & 0xf, num_cables_tx);
+
+ midi_dev_addr = dev_addr;
+}
+
+// Invoked when device with hid interface is un-mounted
+void tuh_midi_umount_cb(uint8_t dev_addr, uint8_t instance)
+{
+ TU_LOG1("MIDI device address = %d, instance = %d is unmounted\r\n", dev_addr, instance);
+
+ midi_dev_addr = 0;
+}
+
+void tuh_midi_rx_cb(uint8_t dev_addr, uint32_t num_packets)
+{
+ if (midi_dev_addr != dev_addr)
+ {
+ return;
+ }
+
+ if(num_packets == 0)
+ {
+ return;
+ }
+
+ uint8_t cable_num;
+ uint8_t buffer[48];
+ uint32_t bytes_read = tuh_midi_stream_read(dev_addr, &cable_num, buffer, sizeof(buffer));
+
+ TU_LOG1("Read bytes %u cable %u", bytes_read, cable_num);
+ TU_LOG1_MEM(buffer, bytes_read, 2);
+}
+
+void tuh_midi_tx_cb(uint8_t dev_addr)
+{
+ (void ) dev_addr;
+} \ No newline at end of file
diff --git a/examples/host/midi_rx/src/tusb_config.h b/examples/host/midi_rx/src/tusb_config.h
new file mode 100644
index 000000000..5cc354ea9
--- /dev/null
+++ b/examples/host/midi_rx/src/tusb_config.h
@@ -0,0 +1,96 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 Ha Thach (tinyusb.org)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+#ifndef _TUSB_CONFIG_H_
+#define _TUSB_CONFIG_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+//--------------------------------------------------------------------
+// COMMON CONFIGURATION
+//--------------------------------------------------------------------
+
+// defined by compiler flags for flexibility
+#ifndef CFG_TUSB_MCU
+#error CFG_TUSB_MCU must be defined
+#endif
+
+#if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX
+#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_HOST | OPT_MODE_HIGH_SPEED)
+#else
+#define CFG_TUSB_RHPORT0_MODE OPT_MODE_HOST
+#endif
+
+#ifndef CFG_TUSB_OS
+#define CFG_TUSB_OS OPT_OS_NONE
+#endif
+
+// CFG_TUSB_DEBUG is defined by compiler in DEBUG build
+// #define CFG_TUSB_DEBUG 0
+
+/* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment.
+ * Tinyusb use follows macros to declare transferring memory so that they can be put
+ * into those specific section.
+ * e.g
+ * - CFG_TUSB_MEM SECTION : __attribute__ (( section(".usb_ram") ))
+ * - CFG_TUSB_MEM_ALIGN : __attribute__ ((aligned(4)))
+ */
+#ifndef CFG_TUSB_MEM_SECTION
+#define CFG_TUSB_MEM_SECTION
+#endif
+
+#ifndef CFG_TUSB_MEM_ALIGN
+#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4)))
+#endif
+
+//--------------------------------------------------------------------
+// CONFIGURATION
+//--------------------------------------------------------------------
+
+// Size of buffer to hold descriptors and other data used for enumeration
+#define CFG_TUH_ENUMERATION_BUFSIZE 256
+
+#define CFG_TUH_HUB 1
+#define CFG_TUH_CDC 0
+#define CFG_TUH_HID 0 // typical keyboard + mouse device can have 3-4 HID interfaces
+#define CFG_TUH_MIDI 1 // there will be at most one MIDIStreaming Interface descriptor
+#define CFG_TUH_MSC 0
+#define CFG_TUH_VENDOR 0
+
+// max device support (excluding hub device)
+#define CFG_TUH_DEVICE_MAX (CFG_TUH_HUB ? 4 : 1) // hub typically has 4 ports
+#define CFG_MIDI_HOST_DEVSTRINGS 1
+
+//------------- HID -------------//
+#define CFG_TUH_HID_EPIN_BUFSIZE 64
+#define CFG_TUH_HID_EPOUT_BUFSIZE 64
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _TUSB_CONFIG_H_ */
diff --git a/src/class/midi/midi_host.c b/src/class/midi/midi_host.c
index a6ad1303a..aafa340cd 100644
--- a/src/class/midi/midi_host.c
+++ b/src/class/midi/midi_host.c
@@ -333,10 +333,10 @@ bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *d
p_midi_host->out_jack_info[p_midi_host->next_out_jack].jack_id = p_mdoj->bJackID;
p_midi_host->out_jack_info[p_midi_host->next_out_jack].jack_type = p_mdoj->bJackType;
p_midi_host->out_jack_info[p_midi_host->next_out_jack].num_source_ids = p_mdoj->bNrInputPins;
- struct associated_jack_s {
+ const struct associated_jack_s {
uint8_t id;
uint8_t pin;
- } *associated_jack = (struct associated_jack_s *)(p_desc+6);
+ } *associated_jack = (const struct associated_jack_s *)(p_desc+6);
int jack;
for (jack = 0; jack < p_mdoj->bNrInputPins; jack++)
{