forked from 4ms/stm32mp1-baremetal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added MidiHost class, major refactor.
MidiHost owns and manages; - USBH_Handle (usbhost/phost), and its state change callback - MidiStreamingHandle - USBH_ClassTypeDef (operations and pData ptr to MidiStreamingHandle) It also sets up the OTG interrupt. It provides wrappers for Midi Host functions (start/stop/rx/tx) It's not 100% there yet, but close to supporting multiple MidiHosts (i.e. on different USB ports)
- Loading branch information
Showing
4 changed files
with
130 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#pragma once | ||
#include "drivers/interrupt.hh" | ||
#include "drivers/interrupt_control.hh" | ||
#include "usbh_midi.hh" | ||
|
||
class MidiHost { | ||
public: | ||
static inline uint8_t rx_buffer[128]; | ||
|
||
MidiStreamingHandle MSHandle; | ||
USBH_HandleTypeDef usbhost; | ||
|
||
USBH_ClassTypeDef midi_class_ops = { | ||
"MIDI", | ||
AudioClassCode, | ||
USBH_MIDI_InterfaceInit, | ||
USBH_MIDI_InterfaceDeInit, | ||
USBH_MIDI_ClassRequest, | ||
USBH_MIDI_Process, | ||
USBH_MIDI_SOFProcess, | ||
&MSHandle, | ||
}; | ||
|
||
MidiHost() = default; | ||
|
||
void set_rx_callback(MidiStreamRxCallbackType rx_callback) { MSHandle.rx_callback = rx_callback; } | ||
void set_tx_callback(MidiStreamTxCallbackType tx_callback) { MSHandle.tx_callback = tx_callback; } | ||
|
||
bool init() | ||
{ | ||
auto status = USBH_Init(&usbhost, usbh_state_change_callback, 0); | ||
if (status != USBH_OK) | ||
return false; | ||
|
||
// defined in usbh_conf.c | ||
extern HCD_HandleTypeDef hhcd; | ||
InterruptControl::disable_irq(OTG_IRQn); | ||
InterruptManager::registerISR(OTG_IRQn, [] { HAL_HCD_IRQHandler(&hhcd); }); | ||
InterruptControl::set_irq_priority(OTG_IRQn, 0, 0); | ||
InterruptControl::enable_irq(OTG_IRQn); | ||
|
||
USBH_RegisterClass(&usbhost, &midi_class_ops); | ||
|
||
return true; | ||
} | ||
|
||
bool start() { return USBH_Start(&usbhost) == USBH_OK; } | ||
bool stop() { return USBH_Stop(&usbhost) == USBH_OK; } | ||
void process() { USBH_Process(&usbhost); } | ||
USBH_StatusTypeDef receive() { return USBH_MIDI_Receive(&usbhost, rx_buffer, 128); } | ||
USBH_StatusTypeDef transmit(uint8_t *buff, uint32_t len) { return USBH_MIDI_Transmit(&usbhost, buff, len); } | ||
|
||
static void usbh_state_change_callback(USBH_HandleTypeDef *phost, uint8_t id) | ||
{ | ||
switch (id) { | ||
case HOST_USER_SELECT_CONFIGURATION: | ||
printf("Select config\n"); | ||
break; | ||
|
||
case HOST_USER_CONNECTION: | ||
printf("Connected\n"); | ||
break; | ||
|
||
case HOST_USER_CLASS_SELECTED: | ||
printf("Class selected\n"); | ||
break; | ||
|
||
case HOST_USER_CLASS_ACTIVE: | ||
printf("Class active\n"); | ||
// TODO: Move rx_buffer to class handle, to support multiple instances | ||
USBH_MIDI_Receive(phost, rx_buffer, 128); | ||
break; | ||
|
||
case HOST_USER_DISCONNECTION: | ||
printf("Disconnected\n"); | ||
break; | ||
|
||
case HOST_USER_UNRECOVERED_ERROR: | ||
printf("Error\n"); | ||
break; | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters