Skip to content

Commit

Permalink
Rename usbh_cdc.c/h=> usbh_midi.cc/hh
Browse files Browse the repository at this point in the history
Start refactoring MIDI Interface init
  • Loading branch information
danngreen committed Nov 12, 2022
1 parent fa1d8bc commit e2c0e6d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 28 deletions.
2 changes: 1 addition & 1 deletion examples/usb_midi_audio_host/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ SOURCES = startup.s \
$(USBLIBDIR)/Core/Src/usbh_ctlreq.c \
$(USBLIBDIR)/Core/Src/usbh_ioreq.c \
$(USBLIBDIR)/Core/Src/usbh_pipes.c \
usbh_cdc.c \
usbh_midi.cc \


INCLUDES = -I. \
Expand Down
2 changes: 1 addition & 1 deletion examples/usb_midi_audio_host/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include "drivers/uart.hh"
#include "stm32mp1xx.h"
#include "system_clk.hh"
#include "usbh_cdc.h"
#include "usbh_core.h"
#include "usbh_midi.hh"
#include <cstdint>

#include "osd32brk_conf.hh"
Expand Down
2 changes: 1 addition & 1 deletion examples/usb_midi_audio_host/usbh_conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/

#include "usbh_core.h"
#include "usbh_cdc.h"
#include "usbh_midi.hh"

HCD_HandleTypeDef hhcd;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
******************************************************************************
* @file usbh_cdc.c
* @file usbh_midi.cc
* @author MCD Application Team
* @brief This file is the CDC Layer Handlers for USB Host CDC class.
* @brief This file is the for USB Host MIDI subclass of Audio Class.
*
* @verbatim
*
Expand Down Expand Up @@ -37,7 +37,7 @@
******************************************************************************
*/

#include "usbh_cdc.h"
#include "usbh_midi.hh"

#define USBH_MIDI_BUFFER_SIZE 1024

Expand All @@ -50,18 +50,32 @@ static USBH_StatusTypeDef USBH_MIDI_ClassRequest(USBH_HandleTypeDef *phost);
static void MIDI_ProcessTransmission(USBH_HandleTypeDef *phost);
static void MIDI_ProcessReception(USBH_HandleTypeDef *phost);

constexpr uint8_t AudioClassCode = 0x02;
constexpr uint8_t MIDISubclassCode = 0x02;
constexpr uint8_t AnyProtocol = 0xFF;

USBH_ClassTypeDef MIDI_Class = {
"MIDI",
USB_CDC_CLASS,
AudioClassCode,
USBH_MIDI_InterfaceInit,
USBH_MIDI_InterfaceDeInit,
USBH_MIDI_ClassRequest,
USBH_MIDI_Process,
USBH_MIDI_SOFProcess,
NULL,
nullptr,
};

class MIDIHost {
public:
static CDC_HandleTypeDef *new_handle()
{
auto handle = static_cast<CDC_HandleTypeDef *>(USBH_malloc(sizeof(CDC_HandleTypeDef)));
if (handle)
USBH_memset(handle, 0, sizeof(CDC_HandleTypeDef));

return handle;
}
};

/**
* @brief USBH_MIDI_InterfaceInit
Expand All @@ -74,33 +88,26 @@ static USBH_StatusTypeDef USBH_MIDI_InterfaceInit(USBH_HandleTypeDef *phost)

USBH_StatusTypeDef status;
uint8_t interface;
CDC_HandleTypeDef *CDC_Handle;

interface =
USBH_FindInterface(phost, COMMUNICATION_INTERFACE_CLASS_CODE, ABSTRACT_CONTROL_MODEL, COMMON_AT_COMMAND);
interface = USBH_FindInterface(phost, AudioClassCode, MIDISubclassCode, AnyProtocol);

if ((interface == 0xFFU) || (interface >= USBH_MAX_NUM_INTERFACES)) /* No Valid Interface */ {
USBH_DbgLog("Cannot Find the interface for Communication Interface Class. %s", phost->pActiveClass->Name);
return USBH_FAIL;
}

status = USBH_SelectInterface(phost, interface);

if (status != USBH_OK) {
if (status != USBH_OK)
return USBH_FAIL;
}

phost->pActiveClass->pData = (CDC_HandleTypeDef *)USBH_malloc(sizeof(CDC_HandleTypeDef));
CDC_Handle = (CDC_HandleTypeDef *)phost->pActiveClass->pData;
auto CDC_Handle = MIDIHost::new_handle();
phost->pActiveClass->pData = CDC_Handle;

if (CDC_Handle == NULL) {
if (CDC_Handle == nullptr) {
USBH_DbgLog("Cannot allocate memory for CDC Handle");
return USBH_FAIL;
}

/* Initialize cdc handler */
USBH_memset(CDC_Handle, 0, sizeof(CDC_HandleTypeDef));

/*Collect the notification endpoint address and length*/
if (phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bEndpointAddress & 0x80U) {
CDC_Handle->CommItf.NotifEp = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bEndpointAddress;
Expand Down Expand Up @@ -129,12 +136,16 @@ static USBH_StatusTypeDef USBH_MIDI_InterfaceInit(USBH_HandleTypeDef *phost)
}

/*Collect the class specific endpoint address and length*/
if (phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bEndpointAddress & 0x80U) {
CDC_Handle->DataItf.InEp = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bEndpointAddress;
CDC_Handle->DataItf.InEpSize = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].wMaxPacketSize;
} else {
CDC_Handle->DataItf.OutEp = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bEndpointAddress;
CDC_Handle->DataItf.OutEpSize = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].wMaxPacketSize;
{
auto ep_addr = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bEndpointAddress;
auto mps = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].wMaxPacketSize;
if (ep_addr & 0x80U) {
CDC_Handle->DataItf.InEp = ep_addr;
CDC_Handle->DataItf.InEpSize = mps;
} else {
CDC_Handle->DataItf.OutEp = ep_addr;
CDC_Handle->DataItf.OutEpSize = mps;
}
}

if (phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[1].bEndpointAddress & 0x80U) {
Expand Down Expand Up @@ -533,4 +544,3 @@ __weak void USBH_MIDI_ReceiveCallback(USBH_HandleTypeDef *phost)
/* Prevent unused argument(s) compilation warning */
UNUSED(phost);
}

Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,6 @@ extern USBH_ClassTypeDef MIDI_Class;
* @{
*/


USBH_StatusTypeDef USBH_MIDI_Transmit(USBH_HandleTypeDef *phost, uint8_t *pbuff, uint32_t length);
USBH_StatusTypeDef USBH_MIDI_Receive(USBH_HandleTypeDef *phost, uint8_t *pbuff, uint32_t length);
uint16_t USBH_MIDI_GetLastReceivedDataSize(USBH_HandleTypeDef *phost);
Expand Down

0 comments on commit e2c0e6d

Please sign in to comment.