Skip to content

Commit

Permalink
usbmidi sending to host working!
Browse files Browse the repository at this point in the history
  • Loading branch information
trentgill committed Jun 11, 2024
1 parent e5f5f19 commit cd8eeef
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 28 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ SRC = main.c \
$(USBD_COMPOSITE)/App/usbd_cdc_acm_if.c \
$(USBD_COMPOSITE)/App/usbd_desc.c \
$(USBD_COMPOSITE)/App/usbd_msc_if.c \
$(USBD_COMPOSITE)/App/usbd_midi_if.c \
$(wildcard $(USBD_COMPOSITE)/Class/CDC_ACM/Src/*.c) \
$(wildcard $(USBD_COMPOSITE)/Class/COMPOSITE/Src/*.c) \
$(wildcard $(USBD_COMPOSITE)/Class/MSC/Src/*.c) \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static int8_t MIDI_Control(uint8_t cmd, uint8_t *pbuf, uint16_t length){
static int8_t MIDI_Receive(uint8_t *pbuf, uint32_t *Len){
Caw_printf("midi receive\n\r");

uint32_t len = *Len;
// uint32_t len = *Len;
// handle len worth of bytes in pbuf
// just print to Caw for now

Expand All @@ -50,22 +50,22 @@ static int8_t MIDI_TransmitCplt(uint8_t *Buf, uint32_t *Len, uint8_t epnum){



uint8_t USBD_MIDI_GetDeviceState(USBD_HandleTypeDef *pdev){
return pdev->dev_state;
uint8_t USBD_MIDI_GetDeviceState(void){
return hUsbDevice.dev_state;
}

uint8_t USBD_MIDI_GetState(USBD_HandleTypeDef *pdev){
USBD_MIDI_HandleTypeDef *hmidi = (USBD_MIDI_HandleTypeDef*)pdev->pClassData_MIDI;
uint8_t USBD_MIDI_GetState(void){
USBD_MIDI_HandleTypeDef *hmidi = hUsbDevice.pClassData_MIDI;
return hmidi->state;
}

uint8_t USBD_MIDI_SendReport(USBD_HandleTypeDef *pdev, uint8_t *report, uint16_t len){
USBD_MIDI_HandleTypeDef *hmidi = (USBD_MIDI_HandleTypeDef*)pdev->pClassData_MIDI;
uint8_t USBD_MIDI_SendReport(uint8_t *report, uint16_t len){
USBD_MIDI_HandleTypeDef *hmidi = hUsbDevice.pClassData_MIDI;

if (pdev->dev_state == USBD_STATE_CONFIGURED){
if (hUsbDevice.dev_state == USBD_STATE_CONFIGURED){
if(hmidi->state == MIDI_IDLE){
hmidi->state = MIDI_BUSY;
USBD_LL_Transmit (pdev, MIDI_EPIN_ADDR, report, len);
USBD_LL_Transmit (&hUsbDevice, MIDI_IN_EP, report, len);
}
}
return USBD_OK;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

extern USBD_MIDI_ItfTypeDef USBD_MIDI_fops;

uint8_t USBD_MIDI_GetDeviceState(USBD_HandleTypeDef *pdev);
uint8_t USBD_MIDI_GetState(USBD_HandleTypeDef *pdev);
uint8_t USBD_MIDI_SendReport (USBD_HandleTypeDef *pdev, uint8_t *report, uint16_t len);
uint8_t USBD_MIDI_GetDeviceState(void);
uint8_t USBD_MIDI_GetState(void);
uint8_t USBD_MIDI_SendReport(uint8_t *report, uint16_t len);
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ static uint8_t USBD_COMPOSITE_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum)
}
#endif
#if (USBD_USE_MIDI == 1)
if (epnum == (MIDI_EPIN_ADDR & 0x7F))
if (epnum == (MIDI_IN_EP & 0x7F))
{
USBD_MIDI.DataIn(pdev, epnum);
}
Expand Down Expand Up @@ -762,7 +762,7 @@ static uint8_t USBD_COMPOSITE_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum)
}
#endif
#if (USBD_USE_MIDI == 1)
if (epnum == MIDI_EPOUT_ADDR)
if (epnum == MIDI_OUT_EP)
{
USBD_MIDI.DataOut(pdev, epnum);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
#define MIDI_IN_PORTS_NUM 0x01
#define MIDI_OUT_PORTS_NUM 0x01

#define MIDI_EPIN_ADDR 0x81
// #define MIDI_EPIN_ADDR 0x81
#define MIDI_EPIN_SIZE 0x40

#define MIDI_EPOUT_ADDR 0x01
// #define MIDI_EPOUT_ADDR 0x01
#define MIDI_EPOUT_SIZE 0x40

#define USB_MIDI_CLASS_DESC_SHIFT 18
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -525,19 +525,19 @@ static uint8_t USBD_MIDI_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx){
pdev->pClassData_MIDI = (void*)hmidi;

USBD_LL_OpenEP(pdev,
MIDI_EPIN_ADDR,
MIDI_IN_EP,
USBD_EP_TYPE_INTR,
MIDI_EPIN_SIZE);
pdev->ep_out[MIDI_EPIN_ADDR & 0xFU].is_used = 1U;
pdev->ep_out[MIDI_IN_EP & 0xFU].is_used = 1U;

USBD_LL_OpenEP(pdev,
MIDI_EPOUT_ADDR,
MIDI_OUT_EP,
USBD_EP_TYPE_INTR,
MIDI_EPOUT_SIZE);
pdev->ep_in[MIDI_EPOUT_ADDR & 0xFU].is_used = 1U;
pdev->ep_in[MIDI_OUT_EP & 0xFU].is_used = 1U;

USBD_LL_PrepareReceive(pdev,
MIDI_EPOUT_ADDR,
MIDI_OUT_EP,
hmidi->RxBuffer,
MIDI_EPOUT_SIZE);
return ret;
Expand All @@ -546,11 +546,11 @@ static uint8_t USBD_MIDI_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx){
static uint8_t USBD_MIDI_DeInit (USBD_HandleTypeDef *pdev, uint8_t cfgidx){
/* Close MIDI EPs */
// USBD_LL_CloseEP(pdev, MIDI_EPIN_SIZE);
USBD_LL_CloseEP(pdev, MIDI_EPIN_ADDR);
pdev->ep_in[MIDI_EPIN_ADDR & 0xFU].is_used = 0U;
USBD_LL_CloseEP(pdev, MIDI_IN_EP);
pdev->ep_in[MIDI_IN_EP & 0xFU].is_used = 0U;

USBD_LL_CloseEP(pdev, MIDI_EPOUT_ADDR);
pdev->ep_in[MIDI_EPOUT_ADDR & 0xFU].is_used = 0U;
USBD_LL_CloseEP(pdev, MIDI_OUT_EP);
pdev->ep_in[MIDI_OUT_EP & 0xFU].is_used = 0U;

/* FRee allocated memory */
if(pdev->pClassData_MIDI != NULL){
Expand Down Expand Up @@ -630,15 +630,15 @@ static uint8_t USBD_MIDI_DataIn (USBD_HandleTypeDef *pdev, uint8_t epnum){
}

static uint8_t USBD_MIDI_DataOut (USBD_HandleTypeDef *pdev, uint8_t epnum){
if (epnum != (MIDI_EPOUT_ADDR & 0x0F)) return USBD_FAIL;
if (epnum != (MIDI_OUT_EP & 0x0F)) return USBD_FAIL;

USBD_MIDI_HandleTypeDef *hmidi = (USBD_MIDI_HandleTypeDef*)pdev->pClassData_MIDI;
hmidi->RxLength = USBD_LL_GetRxDataSize(pdev, epnum);

((USBD_MIDI_ItfTypeDef*)pdev->pUserData_MIDI)->Receive(hmidi->RxBuffer, &hmidi->RxLength);

USBD_LL_PrepareReceive(pdev,
MIDI_EPOUT_ADDR,
MIDI_OUT_EP,
hmidi->RxBuffer,
MIDI_EPOUT_SIZE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev)
HAL_PCDEx_SetTxFiFoInBytes(hpcd_USB_OTG_PTR, (PRNT_IN_EP & 0x7F), 128);
#endif
#if (USBD_USE_MIDI == 1)
HAL_PCDEx_SetTxFiFoInBytes(hpcd_USB_OTG_PTR, (MIDI_EPIN_ADDR & 0x7F), 128);
HAL_PCDEx_SetTxFiFoInBytes(hpcd_USB_OTG_PTR, (MIDI_IN_EP & 0x7F), 128);
#endif
#if (USBD_USE_CDC_ACM == 1)
for (uint8_t i = 0; i < USBD_CDC_ACM_COUNT; i++)
Expand Down
Binary file modified crow.dfu
Binary file not shown.
22 changes: 22 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
// new USB composite device setup
#include "lib/usb_otg.h"
#include "usb_device.h"
#include "usbd_midi_if.h"

#include "lib/midi.h"
// #include "lib/mhost.h"
Expand Down Expand Up @@ -130,6 +131,27 @@ int main(void)
char* crow_msg = "print('hi')\n\r";
USBHost_Send((unsigned char*)crow_msg, strlen(crow_msg));
// Caw_printf("hi\n\r");

// the midiparser lib probably wraps this
// 144 60 127 - turn ON note #60 on MIDI channel 1 with a velocity of 127
uint8_t cable = 0;
uint8_t code = 0x9; // note-on message (see usb-midi pdf doc)
uint8_t message = 0x9; // note-on
uint8_t channel = 0;
uint8_t note = 60;
uint8_t velocity = 127;

uint8_t reportBuffer[4] = {
// cable - represents physical/virtual port number (0 - 15) of the device
// code - in general cases is equal to midi message
(cable << 4) | code,
(message << 4) | channel,
note,
velocity,
};
// while (USBD_MIDI_GetState() != MIDI_IDLE) {};
USBD_MIDI_SendReport(reportBuffer, 4);
Caw_printf(".\n\r");
}

U_PrintNow();
Expand Down

0 comments on commit cd8eeef

Please sign in to comment.