diff --git a/Makefile b/Makefile index a12cc9f..94220c3 100644 --- a/Makefile +++ b/Makefile @@ -46,6 +46,7 @@ STM32_INCLUDES = \ -I$(USBD_COMPOSITE)/Class/CDC_ACM/Inc/ \ -I$(USBD_COMPOSITE)/Class/COMPOSITE/Inc/ \ -I$(USBD_COMPOSITE)/Class/MSC/Inc/ \ + -I$(USBD_COMPOSITE)/Class/MIDI/Inc/ \ -I$(USBD_COMPOSITE)/Core/Inc/ \ -I$(USBD_COMPOSITE)/Target/ \ -Iusbh/ \ @@ -139,6 +140,7 @@ SRC = main.c \ $(wildcard $(USBD_COMPOSITE)/Class/CDC_ACM/Src/*.c) \ $(wildcard $(USBD_COMPOSITE)/Class/COMPOSITE/Src/*.c) \ $(wildcard $(USBD_COMPOSITE)/Class/MSC/Src/*.c) \ + $(wildcard $(USBD_COMPOSITE)/Class/MIDI/Src/*.c) \ $(wildcard $(USBD_COMPOSITE)/Core/Src/*.c) \ $(wildcard $(USBD_COMPOSITE)/Target/*.c) \ $(wildcard usbh/*.c) \ diff --git a/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/App/usb_device.c b/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/App/usb_device.c index ef8f837..e640e3e 100755 --- a/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/App/usb_device.c +++ b/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/App/usb_device.c @@ -89,6 +89,12 @@ void MX_USB_DEVICE_Init(void){ { Error_Handler(); } +#endif +#if (USBD_USE_PRNTR == 1) + if (USBD_MIDI_RegisterInterface(&hUsbDevice, &USBD_MIDI_fops) != USBD_OK) + { + Error_Handler(); + } #endif // printf("usbd_start\n\r"); // U_PrintNow(); diff --git a/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/App/usbd_cdc_acm_if.c b/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/App/usbd_cdc_acm_if.c index 29f5a7c..3234ba8 100755 --- a/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/App/usbd_cdc_acm_if.c +++ b/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/App/usbd_cdc_acm_if.c @@ -265,71 +265,3 @@ static void USB_Timer_Callback(int count){ } } } - -/* USER CODE BEGIN PRIVATE_FUNCTIONS_IMPLEMENTATION */ -//void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) -//{ -// /* Initiate next USB packet transfer once UART completes transfer (transmitting data over Tx line) */ -// //USBD_CDC_ReceivePacket(UART_Handle_TO_CDC_CH(huart), &hUsbDevice); -//} - -//void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) -//{ -// for (uint8_t i = 0; i < NUMBER_OF_CDC; i++) -// { -// uint32_t buffptr; -// uint32_t buffsize; -// -// if (Read_Index[i] != Write_Index[i]) -// { -// if (Read_Index[i] > Write_Index[i]) /* Rollback */ -// { -// buffsize = APP_TX_DATA_SIZE - Read_Index[i]; -// } -// else -// { -// buffsize = Write_Index[i] - Read_Index[i]; -// } -// -// buffptr = Read_Index[i]; -// -// USBD_CDC_SetTxBuffer(i, &hUsbDevice, &TX_Buffer[i][buffptr], buffsize); -// -// if (USBD_CDC_TransmitPacket(i, &hUsbDevice) == USBD_OK) -// { -// Read_Index[i] += buffsize; -// if (Read_Index[i] == APP_RX_DATA_SIZE) -// { -// Read_Index[i] = 0; -// } -// } -// } -// } -//} - -//void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) -//{ -// uint8_t cdc_ch = UART_Handle_TO_CDC_CH(huart); -// /* Increment Index for buffer writing */ -// Write_Index[cdc_ch]++; -// -// /* To avoid buffer overflow */ -// if (Write_Index[cdc_ch] == APP_RX_DATA_SIZE) -// { -// Write_Index[cdc_ch] = 0; -// } -// -// /* Start another reception: provide the buffer pointer with offset and the buffer size */ -// HAL_UART_Receive_IT(huart, (TX_Buffer[cdc_ch] + Write_Index[cdc_ch]), 1); -//} -/* USER CODE END PRIVATE_FUNCTIONS_IMPLEMENTATION */ - -/** - * @} - */ - -/** - * @} - */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/App/usbd_desc.c b/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/App/usbd_desc.c index 40a9db1..d15fba8 100755 --- a/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/App/usbd_desc.c +++ b/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/App/usbd_desc.c @@ -71,9 +71,9 @@ #else #define USBD_PID 21156 // 0x52A4 #endif -#define USBD_PRODUCT_STRING "Crown: vcp + msc" +#define USBD_PRODUCT_STRING "Crown: vcp + midi" #define USBD_CONFIGURATION_STRING "CONFIGURATION 0" -#define USBD_INTERFACE_STRING "Composite Interface" +#define USBD_INTERFACE_STRING "Composite VCP+MIDI Interface" /* USER CODE BEGIN PRIVATE_DEFINES */ diff --git a/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/App/usbd_midi_if.c b/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/App/usbd_midi_if.c new file mode 100644 index 0000000..4fbad93 --- /dev/null +++ b/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/App/usbd_midi_if.c @@ -0,0 +1,72 @@ +#include "usbd_midi_if.h" + +#define MIDI_RX_DATA_SIZE MIDI_EPOUT_SIZE +static uint8_t usb_rx_buffer[MIDI_RX_DATA_SIZE] = {0}; + +extern USBD_HandleTypeDef hUsbDevice; + +static int8_t MIDI_Init(void); +static int8_t MIDI_DeInit(void); +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); +static int8_t MIDI_TransmitCplt(uint8_t *Buf, uint32_t *Len, uint8_t epnum); + +USBD_MIDI_ItfTypeDef USBD_MIDI_fops = {MIDI_Init, + MIDI_DeInit, + MIDI_Control, + MIDI_Receive, + MIDI_TransmitCplt}; + +#include "lib/caw.h" +static int8_t MIDI_Init(void){ + Caw_printf("midi init\n\r"); + USBD_MIDI_SetRxBuffer(&hUsbDevice, usb_rx_buffer); + return (USBD_OK); +} + +static int8_t MIDI_DeInit(void){ + return (USBD_OK); +} +static int8_t MIDI_Control(uint8_t cmd, uint8_t *pbuf, uint16_t length){ + Caw_printf("midi control\n\r"); + return (USBD_OK); +} +static int8_t MIDI_Receive(uint8_t *pbuf, uint32_t *Len){ + Caw_printf("midi receive\n\r"); + + uint32_t len = *Len; + // handle len worth of bytes in pbuf + // just print to Caw for now + + memset(usb_rx_buffer, 0, MIDI_RX_DATA_SIZE); + + return (USBD_OK); +} +static int8_t MIDI_TransmitCplt(uint8_t *Buf, uint32_t *Len, uint8_t epnum){ + Caw_printf("midi txcplt\n\r"); + return (USBD_OK); +} + + + + +uint8_t USBD_MIDI_GetDeviceState(USBD_HandleTypeDef *pdev){ + return pdev->dev_state; +} + +uint8_t USBD_MIDI_GetState(USBD_HandleTypeDef *pdev){ + USBD_MIDI_HandleTypeDef *hmidi = (USBD_MIDI_HandleTypeDef*)pdev->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; + + if (pdev->dev_state == USBD_STATE_CONFIGURED){ + if(hmidi->state == MIDI_IDLE){ + hmidi->state = MIDI_BUSY; + USBD_LL_Transmit (pdev, MIDI_EPIN_ADDR, report, len); + } + } + return USBD_OK; +} diff --git a/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/App/usbd_midi_if.h b/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/App/usbd_midi_if.h new file mode 100644 index 0000000..e67ede1 --- /dev/null +++ b/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/App/usbd_midi_if.h @@ -0,0 +1,9 @@ +#pragma once + +#include "usbd_midi.h" + +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); diff --git a/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/Class/CDC_ACM/Src/usbd_cdc_acm.c b/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/Class/CDC_ACM/Src/usbd_cdc_acm.c index 9775b78..343f000 100755 --- a/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/Class/CDC_ACM/Src/usbd_cdc_acm.c +++ b/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/Class/CDC_ACM/Src/usbd_cdc_acm.c @@ -122,7 +122,6 @@ static uint8_t USBD_CDC_EP0_RxReady(USBD_HandleTypeDef *pdev); static uint8_t *USBD_CDC_GetFSCfgDesc(uint16_t *length); static uint8_t *USBD_CDC_GetHSCfgDesc(uint16_t *length); static uint8_t *USBD_CDC_GetOtherSpeedCfgDesc(uint16_t *length); -static uint8_t *USBD_CDC_GetOtherSpeedCfgDesc(uint16_t *length); static uint8_t *USBD_CDC_GetDeviceQualifierDescriptor(uint16_t *length); USBD_CDC_ACM_HandleTypeDef CDC_ACM_Class_Data[NUMBER_OF_CDC]; diff --git a/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/Class/COMPOSITE/Inc/AL94.I-CUBE-USBD-COMPOSITE_conf.h b/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/Class/COMPOSITE/Inc/AL94.I-CUBE-USBD-COMPOSITE_conf.h index 5f59575..4921c89 100644 --- a/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/Class/COMPOSITE/Inc/AL94.I-CUBE-USBD-COMPOSITE_conf.h +++ b/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/Class/COMPOSITE/Inc/AL94.I-CUBE-USBD-COMPOSITE_conf.h @@ -64,7 +64,7 @@ #define _USBD_USE_UVC false /*---------- _USBD_USE_MSC -----------*/ -#define _USBD_USE_MSC true +#define _USBD_USE_MSC false /*---------- _USBD_USE_DFU -----------*/ #define _USBD_USE_DFU false @@ -72,6 +72,9 @@ /*---------- _USBD_USE_PRNTR -----------*/ #define _USBD_USE_PRNTR false +/*---------- _USBD_USE_MIDI -----------*/ +#define _USBD_USE_MIDI true + /*---------- _STM32F1_DEVICE -----------*/ #define _STM32F1_DEVICE false diff --git a/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/Class/COMPOSITE/Inc/usbd_composite.h b/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/Class/COMPOSITE/Inc/usbd_composite.h index 6483231..3383c17 100755 --- a/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/Class/COMPOSITE/Inc/usbd_composite.h +++ b/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/Class/COMPOSITE/Inc/usbd_composite.h @@ -58,6 +58,7 @@ extern "C" { #define USBD_USE_MSC _USBD_USE_MSC #define USBD_USE_DFU _USBD_USE_DFU #define USBD_USE_PRNTR _USBD_USE_PRNTR +#define USBD_USE_MIDI _USBD_USE_MIDI #define STM32F1_DEVICE _STM32F1_DEVICE @@ -97,6 +98,9 @@ extern "C" { #if(USBD_USE_PRNTR == 1) #include "usbd_printer_if.h" #endif +#if(USBD_USE_MIDI == 1) +#include "usbd_midi_if.h" +#endif /** * @} diff --git a/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/Class/COMPOSITE/Src/usbd_composite.c b/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/Class/COMPOSITE/Src/usbd_composite.c index 35fc826..8f908ab 100755 --- a/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/Class/COMPOSITE/Src/usbd_composite.c +++ b/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/Class/COMPOSITE/Src/usbd_composite.c @@ -157,6 +157,9 @@ typedef struct USBD_COMPOSITE_CFG_DESC_t #if (USBD_USE_CDC_ACM == 1) uint8_t USBD_CDC_ACM_DESC[USB_CDC_CONFIG_DESC_SIZ - 0x09]; #endif +#if (USBD_USE_MIDI == 1) + uint8_t USBD_MIDI_DESC[USB_MIDI_CONFIG_DESC_SIZE - 0x09]; +#endif } __PACKED USBD_COMPOSITE_CFG_DESC_t; @@ -237,6 +240,9 @@ static uint8_t USBD_COMPOSITE_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx) #if (USBD_USE_PRNTR == 1) USBD_PRNT.Init(pdev, cfgidx); #endif +#if (USBD_USE_MIDI == 1) + USBD_MIDI.Init(pdev, cfgidx); +#endif return (uint8_t)USBD_OK; } @@ -286,6 +292,9 @@ static uint8_t USBD_COMPOSITE_DeInit(USBD_HandleTypeDef *pdev, uint8_t cfgidx) #if (USBD_USE_PRNTR == 1) USBD_PRNT.DeInit(pdev, cfgidx); #endif +#if (USBD_USE_MIDI == 1) + USBD_MIDI.DeInit(pdev, cfgidx); +#endif return (uint8_t)USBD_OK; } @@ -375,6 +384,12 @@ static uint8_t USBD_COMPOSITE_Setup(USBD_HandleTypeDef *pdev, USBD_PRNT.Setup(pdev, req); } #endif +#if (USBD_USE_MIDI == 1) + if (LOBYTE(req->wIndex) == MIDI_ITF_NBR) + { + USBD_MIDI.Setup(pdev, req); + } +#endif return USBD_FAIL; } @@ -455,6 +470,12 @@ static uint8_t USBD_COMPOSITE_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum) USBD_PRNT.DataIn(pdev, epnum); } #endif +#if (USBD_USE_MIDI == 1) + if (epnum == (MIDI_EPIN_ADDR & 0x7F)) + { + USBD_MIDI.DataIn(pdev, epnum); + } +#endif return USBD_FAIL; } @@ -497,6 +518,8 @@ static uint8_t USBD_COMPOSITE_EP0_RxReady(USBD_HandleTypeDef *pdev) USBD_DFU.EP0_RxReady(pdev); #endif #if (USBD_USE_PRNTR == 1) +#endif +#if (USBD_USE_MIDI == 1) #endif return (uint8_t)USBD_OK; @@ -536,6 +559,8 @@ static uint8_t USBD_COMPOSITE_EP0_TxReady(USBD_HandleTypeDef *pdev) USBD_DFU.EP0_TxSent(pdev); #endif #if (USBD_USE_PRNTR == 1) +#endif +#if (USBD_USE_MIDI == 1) #endif return (uint8_t)USBD_OK; @@ -576,6 +601,8 @@ static uint8_t USBD_COMPOSITE_SOF(USBD_HandleTypeDef *pdev) USBD_DFU.SOF(pdev); #endif #if (USBD_USE_PRNTR == 1) +#endif +#if (USBD_USE_MIDI == 1) #endif return (uint8_t)USBD_OK; @@ -621,6 +648,8 @@ static uint8_t USBD_COMPOSITE_IsoINIncomplete(USBD_HandleTypeDef *pdev, uint8_t #if (USBD_USE_DFU == 1) #endif #if (USBD_USE_PRNTR == 1) +#endif +#if (USBD_USE_MIDI == 1) #endif return (uint8_t)USBD_OK; @@ -662,6 +691,8 @@ static uint8_t USBD_COMPOSITE_IsoOutIncomplete(USBD_HandleTypeDef *pdev, uint8_t #if (USBD_USE_DFU == 1) #endif #if (USBD_USE_PRNTR == 1) +#endif +#if (USBD_USE_MIDI == 1) #endif return (uint8_t)USBD_OK; @@ -730,7 +761,12 @@ static uint8_t USBD_COMPOSITE_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum) USBD_PRNT.DataOut(pdev, epnum); } #endif - +#if (USBD_USE_MIDI == 1) + if (epnum == MIDI_EPOUT_ADDR) + { + USBD_MIDI.DataOut(pdev, epnum); + } +#endif return USBD_FAIL; } @@ -879,6 +915,12 @@ static uint8_t *USBD_COMPOSITE_GetUsrStringDesc(USBD_HandleTypeDef *pdev, uint8_ { USBD_GetString((uint8_t *)PRNT_STR_DESC, USBD_StrDesc, length); } +#endif +#if (USBD_USE_MIDI == 1) + if (index == MIDI_STR_DESC_IDX) + { + USBD_GetString((uint8_t *)MIDI_STR_DESC, USBD_StrDesc, length); + } #endif return USBD_StrDesc; } @@ -1117,6 +1159,21 @@ void USBD_COMPOSITE_Mount_Class(void) USBD_Track_String_Index += USBD_CDC_ACM_COUNT; #endif +#if (USBD_USE_MIDI == 1) + ptr = USBD_MIDI.GetFSConfigDescriptor(&len); + USBD_Update_MIDI_DESC(ptr, interface_no_track, in_ep_track, out_ep_track, USBD_Track_String_Index); + memcpy(USBD_COMPOSITE_FSCfgDesc.USBD_MIDI_DESC, ptr + 0x09, len - 0x09); + + ptr = USBD_MIDI.GetHSConfigDescriptor(&len); + USBD_Update_MIDI_DESC(ptr, interface_no_track, in_ep_track, out_ep_track, USBD_Track_String_Index); + memcpy(USBD_COMPOSITE_HSCfgDesc.USBD_MIDI_DESC, ptr + 0x09, len - 0x09); + + in_ep_track += 1; + out_ep_track += 1; + interface_no_track += 1; + USBD_Track_String_Index += 1; +#endif + uint16_t CFG_SIZE = sizeof(USBD_COMPOSITE_CFG_DESC_t); ptr = USBD_COMPOSITE_HSCfgDesc.CONFIG_DESC; /* Configuration Descriptor */ diff --git a/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/Class/MIDI/Inc/usbd_midi.h b/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/Class/MIDI/Inc/usbd_midi.h new file mode 100644 index 0000000..a954bb0 --- /dev/null +++ b/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/Class/MIDI/Inc/usbd_midi.h @@ -0,0 +1,100 @@ +#pragma once + +#include "usbd_ioreq.h" +#include "AL94.I-CUBE-USBD-COMPOSITE_conf.h" + +#define MIDI_STR_DESC "STM32 MIDI DEVICE" + +#define MIDI_IN_PORTS_NUM 0x01 +#define MIDI_OUT_PORTS_NUM 0x01 + +#define MIDI_EPIN_ADDR 0x81 +#define MIDI_EPIN_SIZE 0x40 + +#define MIDI_EPOUT_ADDR 0x01 +#define MIDI_EPOUT_SIZE 0x40 + +#define USB_MIDI_CLASS_DESC_SHIFT 18 +#define USB_MIDI_DESC_SIZE 7 +#define USB_MIDI_REPORT_DESC_SIZE (MIDI_IN_PORTS_NUM * 16 + MIDI_OUT_PORTS_NUM * 16 + 33) +#define USB_MIDI_CONFIG_DESC_SIZE (USB_MIDI_REPORT_DESC_SIZE + USB_MIDI_CLASS_DESC_SHIFT) + +#define MIDI_DESCRIPTOR_TYPE 0x21 + +#define MIDI_REQ_SET_PROTOCOL 0x0B +#define MIDI_REQ_GET_PROTOCOL 0x03 + +#define MIDI_REQ_SET_IDLE 0x0A +#define MIDI_REQ_GET_IDLE 0x02 + +#define MIDI_REQ_SET_REPORT 0x09 +#define MIDI_REQ_GET_REPORT 0x01 + +#define MIDI_JACK_1 0x01 +#define MIDI_JACK_2 0x02 +#define MIDI_JACK_3 0x03 +#define MIDI_JACK_4 0x04 +#define MIDI_JACK_5 0x05 +#define MIDI_JACK_6 0x06 +#define MIDI_JACK_7 0x07 +#define MIDI_JACK_8 0x08 +#define MIDI_JACK_9 0x09 +#define MIDI_JACK_10 0x0a +#define MIDI_JACK_11 0x0b +#define MIDI_JACK_12 0x0c +#define MIDI_JACK_13 0x0d +#define MIDI_JACK_14 0x0e +#define MIDI_JACK_15 0x0f +#define MIDI_JACK_16 0x10 +#define MIDI_JACK_17 (MIDI_IN_PORTS_NUM * 2 + 0x01) +#define MIDI_JACK_18 (MIDI_IN_PORTS_NUM * 2 + 0x02) +#define MIDI_JACK_19 (MIDI_IN_PORTS_NUM * 2 + 0x03) +#define MIDI_JACK_20 (MIDI_IN_PORTS_NUM * 2 + 0x04) +#define MIDI_JACK_21 (MIDI_IN_PORTS_NUM * 2 + 0x05) +#define MIDI_JACK_22 (MIDI_IN_PORTS_NUM * 2 + 0x06) +#define MIDI_JACK_23 (MIDI_IN_PORTS_NUM * 2 + 0x07) +#define MIDI_JACK_24 (MIDI_IN_PORTS_NUM * 2 + 0x08) +#define MIDI_JACK_25 (MIDI_IN_PORTS_NUM * 2 + 0x09) +#define MIDI_JACK_26 (MIDI_IN_PORTS_NUM * 2 + 0x0a) +#define MIDI_JACK_27 (MIDI_IN_PORTS_NUM * 2 + 0x0b) +#define MIDI_JACK_28 (MIDI_IN_PORTS_NUM * 2 + 0x0c) +#define MIDI_JACK_29 (MIDI_IN_PORTS_NUM * 2 + 0x0d) +#define MIDI_JACK_30 (MIDI_IN_PORTS_NUM * 2 + 0x0e) +#define MIDI_JACK_31 (MIDI_IN_PORTS_NUM * 2 + 0x0f) +#define MIDI_JACK_32 (MIDI_IN_PORTS_NUM * 2 + 0x10) + +typedef enum{ + MIDI_IDLE = 0, + MIDI_BUSY, +} MIDI_StateTypeDef; + +typedef struct _USBD_MIDI_Itf{ + int8_t (*Init)(void); + int8_t (*DeInit)(void); + int8_t (*Control)(uint8_t cmd, uint8_t *pbuf, uint16_t length); + int8_t (*Receive)(uint8_t *Buf, uint32_t *Len); + int8_t (*TransmitCplt)(uint8_t *Buf, uint32_t *Len, uint8_t epnum); +} USBD_MIDI_ItfTypeDef; + +typedef struct{ + uint32_t Protocol; + uint32_t IdleState; + uint32_t AltSetting; + MIDI_StateTypeDef state; + uint8_t *RxBuffer; + uint32_t RxLength; +} USBD_MIDI_HandleTypeDef; + +extern USBD_ClassTypeDef USBD_MIDI; + +extern USBD_MIDI_HandleTypeDef MIDI_Class_Data; + +extern uint8_t MIDI_IN_EP; +extern uint8_t MIDI_OUT_EP; +extern uint8_t MIDI_ITF_NBR; +extern uint8_t MIDI_STR_DESC_IDX; + +uint8_t USBD_MIDI_RegisterInterface(USBD_HandleTypeDef *pdev, USBD_MIDI_ItfTypeDef *fops); +uint8_t USBD_MIDI_SetRxBuffer(USBD_HandleTypeDef *pdev, uint8_t* pbuf); + +void USBD_Update_MIDI_DESC(uint8_t *desc, uint8_t itf_no, uint8_t in_ep, uint8_t out_ep, uint8_t str_idx); diff --git a/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/Class/MIDI/Src/usbd_midi.c b/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/Class/MIDI/Src/usbd_midi.c new file mode 100644 index 0000000..770e187 --- /dev/null +++ b/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/Class/MIDI/Src/usbd_midi.c @@ -0,0 +1,687 @@ +#include "usbd_midi.h" +#include "usbd_desc.h" +#include "usbd_ctlreq.h" + +#define _MIDI_IN_EP 0x81 +#define _MIDI_OUT_EP 0x01 +#define _MIDI_ITF_NBR 0x00 +#define _MIDI_STR_DESC_IDX 0x00U + +uint8_t MIDI_IN_EP = _MIDI_IN_EP; +uint8_t MIDI_OUT_EP = _MIDI_OUT_EP; +uint8_t MIDI_ITF_NBR = _MIDI_ITF_NBR; +uint8_t MIDI_STR_DESC_IDX = _MIDI_STR_DESC_IDX; + +static uint8_t USBD_MIDI_Init (USBD_HandleTypeDef *pdev, uint8_t cfgidx); +static uint8_t USBD_MIDI_DeInit (USBD_HandleTypeDef *pdev, uint8_t cfgidx); +static uint8_t USBD_MIDI_Setup (USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req); +static uint8_t USBD_MIDI_DataIn (USBD_HandleTypeDef *pdev, uint8_t epnum); +static uint8_t USBD_MIDI_DataOut (USBD_HandleTypeDef *pdev, uint8_t epnum); + +static uint8_t *USBD_MIDI_GetCfgDesc (uint16_t *length); +static uint8_t *USBD_MIDI_GetDeviceQualifierDescriptor (uint16_t *length); + +USBD_MIDI_HandleTypeDef MIDI_Class_Data; + +/* USB Standard Device Descriptor */ +__ALIGN_BEGIN static uint8_t USBD_MIDI_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC] __ALIGN_END = +{ + USB_LEN_DEV_QUALIFIER_DESC, + USB_DESC_TYPE_DEVICE_QUALIFIER, + 0x00, + 0x02, + 0x00, + 0x00, + 0x00, + 0x40, + 0x01, + 0x00, +}; + +USBD_ClassTypeDef USBD_MIDI = { + USBD_MIDI_Init, + USBD_MIDI_DeInit, + USBD_MIDI_Setup, + NULL, /*EP0_TxSent*/ + NULL, /*EP0_RxReady*/ + USBD_MIDI_DataIn, /*DataIn*/ + USBD_MIDI_DataOut, /*DataOut*/ + NULL, /*SOF */ + NULL, + NULL, + USBD_MIDI_GetCfgDesc, + USBD_MIDI_GetCfgDesc, + USBD_MIDI_GetCfgDesc, + USBD_MIDI_GetDeviceQualifierDescriptor, +}; + +/* USB MIDI device Configuration Descriptor */ +__ALIGN_BEGIN static uint8_t USBD_MIDI_CfgDesc[USB_MIDI_CONFIG_DESC_SIZE] __ALIGN_END = +{ + 0x09, /* bLength: Configuration Descriptor size */ + USB_DESC_TYPE_CONFIGURATION,/* bDescriptorType: Configuration */ + USB_MIDI_CONFIG_DESC_SIZE, + 0x00, /*Length of the total configuration block, including this descriptor, in bytes.*/ + 0x01, /*bNumInterfaces: 1 interface*/ + 0x01, /*bConfigurationValue: ID of this configuration. */ + 0x00, /*iConfiguration: Index of string descriptor describing the configuration (Unused.)*/ +#if (USBD_SELF_POWERED == 1U) + 0xC0, /* bmAttributes: Bus Powered according to user configuration */ +#else + 0x80, /* bmAttributes: Bus Powered according to user configuration */ +#endif + USBD_MAX_POWER, /* MaxPower (mA) */ + + /************** MIDI Adapter Standard MS Interface Descriptor ****************/ + 0x09, /*bLength: Interface Descriptor size*/ + USB_DESC_TYPE_INTERFACE,/*bDescriptorType: Interface descriptor type*/ + _MIDI_ITF_NBR, /*bInterfaceNumber: Index of this interface.*/ // 11 + 0x00, /*bAlternateSetting: Alternate setting*/ + 0x02, /*bNumEndpoints*/ + 0x01, /*bInterfaceClass: AUDIO*/ + 0x03, /*bInterfaceSubClass : MIDISTREAMING*/ + 0x00, /*nInterfaceProtocol : Unused*/ + _MIDI_STR_DESC_IDX, /*iInterface: Unused*/ // 17 + + /******************** MIDI Adapter Class-specific MS Interface Descriptor ********************/ + /* USB_MIDI_CLASS_DESC_SHIFT */ + 0x07, /*bLength: Descriptor size*/ + 0x24, /*bDescriptorType: CS_INTERFACE descriptor*/ + 0x01, /*bDescriptorSubtype: MS_HEADER subtype*/ + 0x00, + 0x01, /*BcdADC: Revision of this class specification*/ + USB_MIDI_REPORT_DESC_SIZE, + 0x00, /*wTotalLength: Total size of class-specific descriptors*/ // 24 + +#if MIDI_IN_PORTS_NUM >= 1 + /******************** MIDI Adapter MIDI IN Jack Descriptor (External) ********************/ + 0x06, /*bLength: Size of this descriptor, in bytes*/ + 0x24, /*bDescriptorType: CS_INTERFACE descriptor.*/ + 0x02, /*bDescriptorSubtype: MIDI_IN_JACK subtype*/ + 0x02, /*bJackType: EXTERNAL.*/ + MIDI_JACK_1, /*bJackID: ID of this Jack.*/ + 0x00, /*iJack: Unused.*/ + + /******************** MIDI Adapter MIDI OUT Jack Descriptor (Embedded) ********************/ + 0x09, /*bLength: Size of this descriptor, in bytes*/ + 0x24, /*bDescriptorType: CS_INTERFACE descriptor.*/ + 0x03, /*bDescriptorSubtype: MIDI_OUT_JACK subtype*/ + 0x01, /*bJackType: EMBEDDED*/ + MIDI_JACK_2, /*bJackID: ID of this Jack.*/ + 0x01, /*bNrInputPins: Number of Input Pins of this Jack.*/ + MIDI_JACK_1, /*BaSourceID(1): ID of the Entity to which this Pin is connected.*/ + 0x01, /*BaSourcePin(1): Output Pin number of the Entity to which this Input Pin is connected.*/ + 0x00, /*iJack: Unused.*/ +#endif + +#if MIDI_IN_PORTS_NUM >= 2 + /******************** MIDI Adapter MIDI IN Jack Descriptor (External) ********************/ + 0x06, /*bLength: Size of this descriptor, in bytes*/ + 0x24, /*bDescriptorType: CS_INTERFACE descriptor.*/ + 0x02, /*bDescriptorSubtype: MIDI_IN_JACK subtype*/ + 0x02, /*bJackType: EXTERNAL.*/ + MIDI_JACK_3, /*bJackID: ID of this Jack.*/ + 0x00, /*iJack: Unused.*/ + + /******************** MIDI Adapter MIDI OUT Jack Descriptor (Embedded) ********************/ + 0x09, /*bLength: Size of this descriptor, in bytes*/ + 0x24, /*bDescriptorType: CS_INTERFACE descriptor.*/ + 0x03, /*bDescriptorSubtype: MIDI_OUT_JACK subtype*/ + 0x01, /*bJackType: EMBEDDED*/ + MIDI_JACK_4, /*bJackID: ID of this Jack.*/ + 0x01, /*bNrInputPins: Number of Input Pins of this Jack.*/ + MIDI_JACK_3, /*BaSourceID(1): ID of the Entity to which this Pin is connected.*/ + 0x01, /*BaSourcePin(1): Output Pin number of the Entity to which this Input Pin is connected.*/ + 0x00, /*iJack: Unused.*/ +#endif + +#if MIDI_IN_PORTS_NUM >= 3 + /******************** MIDI Adapter MIDI IN Jack Descriptor (External) ********************/ + 0x06, /*bLength: Size of this descriptor, in bytes*/ + 0x24, /*bDescriptorType: CS_INTERFACE descriptor.*/ + 0x02, /*bDescriptorSubtype: MIDI_IN_JACK subtype*/ + 0x02, /*bJackType: EXTERNAL.*/ + MIDI_JACK_5, /*bJackID: ID of this Jack.*/ + 0x00, /*iJack: Unused.*/ + + /******************** MIDI Adapter MIDI OUT Jack Descriptor (Embedded) ********************/ + 0x09, /*bLength: Size of this descriptor, in bytes*/ + 0x24, /*bDescriptorType: CS_INTERFACE descriptor.*/ + 0x03, /*bDescriptorSubtype: MIDI_OUT_JACK subtype*/ + 0x01, /*bJackType: EMBEDDED*/ + MIDI_JACK_6, /*bJackID: ID of this Jack.*/ + 0x01, /*bNrInputPins: Number of Input Pins of this Jack.*/ + MIDI_JACK_5, /*BaSourceID(1): ID of the Entity to which this Pin is connected.*/ + 0x01, /*BaSourcePin(1): Output Pin number of the Entity to which this Input Pin is connected.*/ + 0x00, /*iJack: Unused.*/ +#endif + +#if MIDI_IN_PORTS_NUM >= 4 + /******************** MIDI Adapter MIDI IN Jack Descriptor (External) ********************/ + 0x06, /*bLength: Size of this descriptor, in bytes*/ + 0x24, /*bDescriptorType: CS_INTERFACE descriptor.*/ + 0x02, /*bDescriptorSubtype: MIDI_IN_JACK subtype*/ + 0x02, /*bJackType: EXTERNAL.*/ + MIDI_JACK_7, /*bJackID: ID of this Jack.*/ + 0x00, /*iJack: Unused.*/ + + /******************** MIDI Adapter MIDI OUT Jack Descriptor (Embedded) ********************/ + 0x09, /*bLength: Size of this descriptor, in bytes*/ + 0x24, /*bDescriptorType: CS_INTERFACE descriptor.*/ + 0x03, /*bDescriptorSubtype: MIDI_OUT_JACK subtype*/ + 0x01, /*bJackType: EMBEDDED*/ + MIDI_JACK_8, /*bJackID: ID of this Jack.*/ + 0x01, /*bNrInputPins: Number of Input Pins of this Jack.*/ + MIDI_JACK_7, /*BaSourceID(1): ID of the Entity to which this Pin is connected.*/ + 0x01, /*BaSourcePin(1): Output Pin number of the Entity to which this Input Pin is connected.*/ + 0x00, /*iJack: Unused.*/ +#endif + +#if MIDI_IN_PORTS_NUM >= 5 + /******************** MIDI Adapter MIDI IN Jack Descriptor (External) ********************/ + 0x06, /*bLength: Size of this descriptor, in bytes*/ + 0x24, /*bDescriptorType: CS_INTERFACE descriptor.*/ + 0x02, /*bDescriptorSubtype: MIDI_IN_JACK subtype*/ + 0x02, /*bJackType: EXTERNAL.*/ + MIDI_JACK_9, /*bJackID: ID of this Jack.*/ + 0x00, /*iJack: Unused.*/ + + /******************** MIDI Adapter MIDI OUT Jack Descriptor (Embedded) ********************/ + 0x09, /*bLength: Size of this descriptor, in bytes*/ + 0x24, /*bDescriptorType: CS_INTERFACE descriptor.*/ + 0x03, /*bDescriptorSubtype: MIDI_OUT_JACK subtype*/ + 0x01, /*bJackType: EMBEDDED*/ + MIDI_JACK_10, /*bJackID: ID of this Jack.*/ + 0x01, /*bNrInputPins: Number of Input Pins of this Jack.*/ + MIDI_JACK_9, /*BaSourceID(1): ID of the Entity to which this Pin is connected.*/ + 0x01, /*BaSourcePin(1): Output Pin number of the Entity to which this Input Pin is connected.*/ + 0x00, /*iJack: Unused.*/ +#endif + +#if MIDI_IN_PORTS_NUM >= 6 + /******************** MIDI Adapter MIDI IN Jack Descriptor (External) ********************/ + 0x06, /*bLength: Size of this descriptor, in bytes*/ + 0x24, /*bDescriptorType: CS_INTERFACE descriptor.*/ + 0x02, /*bDescriptorSubtype: MIDI_IN_JACK subtype*/ + 0x02, /*bJackType: EXTERNAL.*/ + MIDI_JACK_11, /*bJackID: ID of this Jack.*/ + 0x00, /*iJack: Unused.*/ + + /******************** MIDI Adapter MIDI OUT Jack Descriptor (Embedded) ********************/ + 0x09, /*bLength: Size of this descriptor, in bytes*/ + 0x24, /*bDescriptorType: CS_INTERFACE descriptor.*/ + 0x03, /*bDescriptorSubtype: MIDI_OUT_JACK subtype*/ + 0x01, /*bJackType: EMBEDDED*/ + MIDI_JACK_12, /*bJackID: ID of this Jack.*/ + 0x01, /*bNrInputPins: Number of Input Pins of this Jack.*/ + MIDI_JACK_11, /*BaSourceID(1): ID of the Entity to which this Pin is connected.*/ + 0x01, /*BaSourcePin(1): Output Pin number of the Entity to which this Input Pin is connected.*/ + 0x00, /*iJack: Unused.*/ +#endif + +#if MIDI_IN_PORTS_NUM >= 7 + /******************** MIDI Adapter MIDI IN Jack Descriptor (External) ********************/ + 0x06, /*bLength: Size of this descriptor, in bytes*/ + 0x24, /*bDescriptorType: CS_INTERFACE descriptor.*/ + 0x02, /*bDescriptorSubtype: MIDI_IN_JACK subtype*/ + 0x02, /*bJackType: EXTERNAL.*/ + MIDI_JACK_13, /*bJackID: ID of this Jack.*/ + 0x00, /*iJack: Unused.*/ + + /******************** MIDI Adapter MIDI OUT Jack Descriptor (Embedded) ********************/ + 0x09, /*bLength: Size of this descriptor, in bytes*/ + 0x24, /*bDescriptorType: CS_INTERFACE descriptor.*/ + 0x03, /*bDescriptorSubtype: MIDI_OUT_JACK subtype*/ + 0x01, /*bJackType: EMBEDDED*/ + MIDI_JACK_14, /*bJackID: ID of this Jack.*/ + 0x01, /*bNrInputPins: Number of Input Pins of this Jack.*/ + MIDI_JACK_13, /*BaSourceID(1): ID of the Entity to which this Pin is connected.*/ + 0x01, /*BaSourcePin(1): Output Pin number of the Entity to which this Input Pin is connected.*/ + 0x00, /*iJack: Unused.*/ +#endif + +#if MIDI_IN_PORTS_NUM >= 8 + /******************** MIDI Adapter MIDI IN Jack Descriptor (External) ********************/ + 0x06, /*bLength: Size of this descriptor, in bytes*/ + 0x24, /*bDescriptorType: CS_INTERFACE descriptor.*/ + 0x02, /*bDescriptorSubtype: MIDI_IN_JACK subtype*/ + 0x02, /*bJackType: EXTERNAL.*/ + MIDI_JACK_15, /*bJackID: ID of this Jack.*/ + 0x00, /*iJack: Unused.*/ + + /******************** MIDI Adapter MIDI OUT Jack Descriptor (Embedded) ********************/ + 0x09, /*bLength: Size of this descriptor, in bytes*/ + 0x24, /*bDescriptorType: CS_INTERFACE descriptor.*/ + 0x03, /*bDescriptorSubtype: MIDI_OUT_JACK subtype*/ + 0x01, /*bJackType: EMBEDDED*/ + MIDI_JACK_16, /*bJackID: ID of this Jack.*/ + 0x01, /*bNrInputPins: Number of Input Pins of this Jack.*/ + MIDI_JACK_15, /*BaSourceID(1): ID of the Entity to which this Pin is connected.*/ + 0x01, /*BaSourcePin(1): Output Pin number of the Entity to which this Input Pin is connected.*/ + 0x00, /*iJack: Unused.*/ +#endif + +#if MIDI_OUT_PORTS_NUM >= 1 + /******************** MIDI Adapter MIDI IN Jack Descriptor (Embedded) ********************/ + 0x06, /*bLength: Size of this descriptor, in bytes*/ + 0x24, /*bDescriptorType: CS_INTERFACE descriptor.*/ + 0x02, /*bDescriptorSubtype: MIDI_IN_JACK subtype*/ + 0x01, /*bJackType: EMBEDDED*/ + MIDI_JACK_17, /*bJackID: ID of this Jack.*/ + 0x00, /*iJack: Unused.*/ + + /******************** MIDI Adapter MIDI OUT Jack Descriptor (External) ********************/ + 0x09, /*bLength: Size of this descriptor, in bytes*/ + 0x24, /*bDescriptorType: CS_INTERFACE descriptor.*/ + 0x03, /*bDescriptorSubtype: MIDI_OUT_JACK subtype*/ + 0x02, /*bJackType: EXTERNAL.*/ + MIDI_JACK_18, /*bJackID: ID of this Jack.*/ + 0x01, /*bNrInputPins: Number of Input Pins of this Jack.*/ + MIDI_JACK_17, /*BaSourceID(1): ID of the Entity to which this Pin is connected.*/ + 0x01, /*BaSourcePin(1): Output Pin number of the Entity to which this Input Pin is connected.*/ + 0x00, /*iJack: Unused.*/ +#endif + +#if MIDI_OUT_PORTS_NUM >= 2 + /******************** MIDI Adapter MIDI IN Jack Descriptor (Embedded) ********************/ + 0x06, /*bLength: Size of this descriptor, in bytes*/ + 0x24, /*bDescriptorType: CS_INTERFACE descriptor.*/ + 0x02, /*bDescriptorSubtype: MIDI_IN_JACK subtype*/ + 0x01, /*bJackType: EMBEDDED*/ + MIDI_JACK_19, /*bJackID: ID of this Jack.*/ + 0x00, /*iJack: Unused.*/ + + /******************** MIDI Adapter MIDI OUT Jack Descriptor (External) ********************/ + 0x09, /*bLength: Size of this descriptor, in bytes*/ + 0x24, /*bDescriptorType: CS_INTERFACE descriptor.*/ + 0x03, /*bDescriptorSubtype: MIDI_OUT_JACK subtype*/ + 0x02, /*bJackType: EXTERNAL.*/ + MIDI_JACK_20, /*bJackID: ID of this Jack.*/ + 0x01, /*bNrInputPins: Number of Input Pins of this Jack.*/ + MIDI_JACK_19, /*BaSourceID(1): ID of the Entity to which this Pin is connected.*/ + 0x01, /*BaSourcePin(1): Output Pin number of the Entity to which this Input Pin is connected.*/ + 0x00, /*iJack: Unused.*/ +#endif + +#if MIDI_OUT_PORTS_NUM >= 3 + /******************** MIDI Adapter MIDI IN Jack Descriptor (Embedded) ********************/ + 0x06, /*bLength: Size of this descriptor, in bytes*/ + 0x24, /*bDescriptorType: CS_INTERFACE descriptor.*/ + 0x02, /*bDescriptorSubtype: MIDI_IN_JACK subtype*/ + 0x01, /*bJackType: EMBEDDED*/ + MIDI_JACK_21, /*bJackID: ID of this Jack.*/ + 0x00, /*iJack: Unused.*/ + + /******************** MIDI Adapter MIDI OUT Jack Descriptor (External) ********************/ + 0x09, /*bLength: Size of this descriptor, in bytes*/ + 0x24, /*bDescriptorType: CS_INTERFACE descriptor.*/ + 0x03, /*bDescriptorSubtype: MIDI_OUT_JACK subtype*/ + 0x02, /*bJackType: EXTERNAL.*/ + MIDI_JACK_22, /*bJackID: ID of this Jack.*/ + 0x01, /*bNrInputPins: Number of Input Pins of this Jack.*/ + MIDI_JACK_21, /*BaSourceID(1): ID of the Entity to which this Pin is connected.*/ + 0x01, /*BaSourcePin(1): Output Pin number of the Entity to which this Input Pin is connected.*/ + 0x00, /*iJack: Unused.*/ +#endif + +#if MIDI_OUT_PORTS_NUM >= 4 + /******************** MIDI Adapter MIDI IN Jack Descriptor (Embedded) ********************/ + 0x06, /*bLength: Size of this descriptor, in bytes*/ + 0x24, /*bDescriptorType: CS_INTERFACE descriptor.*/ + 0x02, /*bDescriptorSubtype: MIDI_IN_JACK subtype*/ + 0x01, /*bJackType: EMBEDDED*/ + MIDI_JACK_23, /*bJackID: ID of this Jack.*/ + 0x00, /*iJack: Unused.*/ + + /******************** MIDI Adapter MIDI OUT Jack Descriptor (External) ********************/ + 0x09, /*bLength: Size of this descriptor, in bytes*/ + 0x24, /*bDescriptorType: CS_INTERFACE descriptor.*/ + 0x03, /*bDescriptorSubtype: MIDI_OUT_JACK subtype*/ + 0x02, /*bJackType: EXTERNAL.*/ + MIDI_JACK_24, /*bJackID: ID of this Jack.*/ + 0x01, /*bNrInputPins: Number of Input Pins of this Jack.*/ + MIDI_JACK_23, /*BaSourceID(1): ID of the Entity to which this Pin is connected.*/ + 0x01, /*BaSourcePin(1): Output Pin number of the Entity to which this Input Pin is connected.*/ + 0x00, /*iJack: Unused.*/ +#endif + +#if MIDI_OUT_PORTS_NUM >= 5 + /******************** MIDI Adapter MIDI IN Jack Descriptor (Embedded) ********************/ + 0x06, /*bLength: Size of this descriptor, in bytes*/ + 0x24, /*bDescriptorType: CS_INTERFACE descriptor.*/ + 0x02, /*bDescriptorSubtype: MIDI_IN_JACK subtype*/ + 0x01, /*bJackType: EMBEDDED*/ + MIDI_JACK_25, /*bJackID: ID of this Jack.*/ + 0x00, /*iJack: Unused.*/ + + /******************** MIDI Adapter MIDI OUT Jack Descriptor (External) ********************/ + 0x09, /*bLength: Size of this descriptor, in bytes*/ + 0x24, /*bDescriptorType: CS_INTERFACE descriptor.*/ + 0x03, /*bDescriptorSubtype: MIDI_OUT_JACK subtype*/ + 0x02, /*bJackType: EXTERNAL.*/ + MIDI_JACK_26, /*bJackID: ID of this Jack.*/ + 0x01, /*bNrInputPins: Number of Input Pins of this Jack.*/ + MIDI_JACK_25, /*BaSourceID(1): ID of the Entity to which this Pin is connected.*/ + 0x01, /*BaSourcePin(1): Output Pin number of the Entity to which this Input Pin is connected.*/ + 0x00, /*iJack: Unused.*/ +#endif + +#if MIDI_OUT_PORTS_NUM >= 6 + /******************** MIDI Adapter MIDI IN Jack Descriptor (Embedded) ********************/ + 0x06, /*bLength: Size of this descriptor, in bytes*/ + 0x24, /*bDescriptorType: CS_INTERFACE descriptor.*/ + 0x02, /*bDescriptorSubtype: MIDI_IN_JACK subtype*/ + 0x01, /*bJackType: EMBEDDED*/ + MIDI_JACK_27, /*bJackID: ID of this Jack.*/ + 0x00, /*iJack: Unused.*/ + + /******************** MIDI Adapter MIDI OUT Jack Descriptor (External) ********************/ + 0x09, /*bLength: Size of this descriptor, in bytes*/ + 0x24, /*bDescriptorType: CS_INTERFACE descriptor.*/ + 0x03, /*bDescriptorSubtype: MIDI_OUT_JACK subtype*/ + 0x02, /*bJackType: EXTERNAL.*/ + MIDI_JACK_28, /*bJackID: ID of this Jack.*/ + 0x01, /*bNrInputPins: Number of Input Pins of this Jack.*/ + MIDI_JACK_27, /*BaSourceID(1): ID of the Entity to which this Pin is connected.*/ + 0x01, /*BaSourcePin(1): Output Pin number of the Entity to which this Input Pin is connected.*/ + 0x00, /*iJack: Unused.*/ +#endif + +#if MIDI_OUT_PORTS_NUM >= 7 + /******************** MIDI Adapter MIDI IN Jack Descriptor (Embedded) ********************/ + 0x06, /*bLength: Size of this descriptor, in bytes*/ + 0x24, /*bDescriptorType: CS_INTERFACE descriptor.*/ + 0x02, /*bDescriptorSubtype: MIDI_IN_JACK subtype*/ + 0x01, /*bJackType: EMBEDDED*/ + MIDI_JACK_29, /*bJackID: ID of this Jack.*/ + 0x00, /*iJack: Unused.*/ + + /******************** MIDI Adapter MIDI OUT Jack Descriptor (External) ********************/ + 0x09, /*bLength: Size of this descriptor, in bytes*/ + 0x24, /*bDescriptorType: CS_INTERFACE descriptor.*/ + 0x03, /*bDescriptorSubtype: MIDI_OUT_JACK subtype*/ + 0x02, /*bJackType: EXTERNAL.*/ + MIDI_JACK_30, /*bJackID: ID of this Jack.*/ + 0x01, /*bNrInputPins: Number of Input Pins of this Jack.*/ + MIDI_JACK_29, /*BaSourceID(1): ID of the Entity to which this Pin is connected.*/ + 0x01, /*BaSourcePin(1): Output Pin number of the Entity to which this Input Pin is connected.*/ + 0x00, /*iJack: Unused.*/ +#endif + +#if MIDI_OUT_PORTS_NUM >= 8 + /******************** MIDI Adapter MIDI IN Jack Descriptor (Embedded) ********************/ + 0x06, /*bLength: Size of this descriptor, in bytes*/ + 0x24, /*bDescriptorType: CS_INTERFACE descriptor.*/ + 0x02, /*bDescriptorSubtype: MIDI_IN_JACK subtype*/ + 0x01, /*bJackType: EMBEDDED*/ + MIDI_JACK_31, /*bJackID: ID of this Jack.*/ + 0x00, /*iJack: Unused.*/ + + /******************** MIDI Adapter MIDI OUT Jack Descriptor (External) ********************/ + 0x09, /*bLength: Size of this descriptor, in bytes*/ + 0x24, /*bDescriptorType: CS_INTERFACE descriptor.*/ + 0x03, /*bDescriptorSubtype: MIDI_OUT_JACK subtype*/ + 0x02, /*bJackType: EXTERNAL.*/ + MIDI_JACK_32, /*bJackID: ID of this Jack.*/ + 0x01, /*bNrInputPins: Number of Input Pins of this Jack.*/ + MIDI_JACK_31, /*BaSourceID(1): ID of the Entity to which this Pin is connected.*/ + 0x01, /*BaSourcePin(1): Output Pin number of the Entity to which this Input Pin is connected.*/ + 0x00, /*iJack: Unused.*/ +#endif + + /******************** MIDI Adapter Standard Bulk OUT Endpoint Descriptor ********************/ + 0x09, /*bLength: Size of this descriptor, in bytes*/ + USB_DESC_TYPE_ENDPOINT, /*bDescriptorType: ENDPOINT descriptor.*/ + _MIDI_OUT_EP, /*bEndpointAddress: OUT Endpoint 1.*/ + 0x02, /*bmAttributes: Bulk, not shared.*/ + MIDI_EPOUT_SIZE, + 0x00, /*wMaxPacketSize*/ + 0x00, /*bInterval: Ignored for Bulk. Set to zero.*/ + 0x00, /*bRefresh: Unused.*/ + 0x00, /*bSynchAddress: Unused.*/ + + /******************** MIDI Adapter Class-specific Bulk OUT Endpoint Descriptor ********************/ + (4 + MIDI_OUT_PORTS_NUM), /*bLength: Size of this descriptor, in bytes*/ + 0x25, /*bDescriptorType: CS_ENDPOINT descriptor*/ + 0x01, /*bDescriptorSubtype: MS_GENERAL subtype.*/ + MIDI_OUT_PORTS_NUM, /*bNumEmbMIDIJack: Number of embedded MIDI IN Jacks.*/ +#if MIDI_OUT_PORTS_NUM >= 1 + MIDI_JACK_17, /*BaAssocJackID(1): ID of the Embedded MIDI IN Jack.*/ +#endif +#if MIDI_OUT_PORTS_NUM >= 2 + MIDI_JACK_19, /*BaAssocJackID(2): ID of the Embedded MIDI IN Jack.*/ +#endif +#if MIDI_OUT_PORTS_NUM >= 3 + MIDI_JACK_21, /*BaAssocJackID(3): ID of the Embedded MIDI IN Jack.*/ +#endif +#if MIDI_OUT_PORTS_NUM >= 4 + MIDI_JACK_23, /*BaAssocJackID(4): ID of the Embedded MIDI IN Jack.*/ +#endif +#if MIDI_OUT_PORTS_NUM >= 5 + MIDI_JACK_25, /*BaAssocJackID(5): ID of the Embedded MIDI IN Jack.*/ +#endif +#if MIDI_OUT_PORTS_NUM >= 6 + MIDI_JACK_27, /*BaAssocJackID(6): ID of the Embedded MIDI IN Jack.*/ +#endif +#if MIDI_OUT_PORTS_NUM >= 7 + MIDI_JACK_29, /*BaAssocJackID(7): ID of the Embedded MIDI IN Jack.*/ +#endif +#if MIDI_OUT_PORTS_NUM >= 8 + MIDI_JACK_31, /*BaAssocJackID(8): ID of the Embedded MIDI IN Jack.*/ +#endif + + /******************** MIDI Adapter Standard Bulk IN Endpoint Descriptor ********************/ + 0x09, /*bLength: Size of this descriptor, in bytes*/ + USB_DESC_TYPE_ENDPOINT, /*bDescriptorType: ENDPOINT descriptor.*/ + _MIDI_IN_EP, /*bEndpointAddress: IN Endpoint 1.*/ + 0x02, /*bmAttributes: Bulk, not shared.*/ + MIDI_EPIN_SIZE, + 0x00, /*wMaxPacketSize*/ + 0x00, /*bInterval: Ignored for Bulk. Set to zero.*/ + 0x00, /*bRefresh: Unused.*/ + 0x00, /*bSynchAddress: Unused.*/ + + /******************** MIDI Adapter Class-specific Bulk IN Endpoint Descriptor ********************/ + (4 + MIDI_IN_PORTS_NUM), /*bLength: Size of this descriptor, in bytes*/ + 0x25, /*bDescriptorType: CS_ENDPOINT descriptor*/ + 0x01, /*bDescriptorSubtype: MS_GENERAL subtype.*/ + MIDI_IN_PORTS_NUM, /*bNumEmbMIDIJack: Number of embedded MIDI OUT Jacks.*/ +#if MIDI_IN_PORTS_NUM >= 1 + MIDI_JACK_2, /*BaAssocJackID(1): ID of the Embedded MIDI OUT Jack.*/ +#endif +#if MIDI_IN_PORTS_NUM >= 2 + MIDI_JACK_4, /*BaAssocJackID(2): ID of the Embedded MIDI OUT Jack.*/ +#endif +#if MIDI_IN_PORTS_NUM >= 3 + MIDI_JACK_6, /*BaAssocJackID(3): ID of the Embedded MIDI OUT Jack.*/ +#endif +#if MIDI_IN_PORTS_NUM >= 4 + MIDI_JACK_8, /*BaAssocJackID(4): ID of the Embedded MIDI OUT Jack.*/ +#endif +#if MIDI_IN_PORTS_NUM >= 5 + MIDI_JACK_10, /*BaAssocJackID(5): ID of the Embedded MIDI OUT Jack.*/ +#endif +#if MIDI_IN_PORTS_NUM >= 6 + MIDI_JACK_12, /*BaAssocJackID(6): ID of the Embedded MIDI OUT Jack.*/ +#endif +#if MIDI_IN_PORTS_NUM >= 7 + MIDI_JACK_14, /*BaAssocJackID(7): ID of the Embedded MIDI OUT Jack.*/ +#endif +#if MIDI_IN_PORTS_NUM >= 8 + MIDI_JACK_16, /*BaAssocJackID(8): ID of the Embedded MIDI OUT Jack.*/ +#endif +}; + +static uint8_t USBD_MIDI_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx){ + uint8_t ret = 0; + + USBD_MIDI_HandleTypeDef* hmidi = &MIDI_Class_Data; + + if(hmidi == NULL){ + pdev->pClassData_MIDI = NULL; + return (uint8_t)USBD_EMEM; + } + + pdev->pClassData_MIDI = (void*)hmidi; + + USBD_LL_OpenEP(pdev, + MIDI_EPIN_ADDR, + USBD_EP_TYPE_INTR, + MIDI_EPIN_SIZE); + pdev->ep_out[MIDI_EPIN_ADDR & 0xFU].is_used = 1U; + + USBD_LL_OpenEP(pdev, + MIDI_EPOUT_ADDR, + USBD_EP_TYPE_INTR, + MIDI_EPOUT_SIZE); + pdev->ep_in[MIDI_EPOUT_ADDR & 0xFU].is_used = 1U; + + USBD_LL_PrepareReceive(pdev, + MIDI_EPOUT_ADDR, + hmidi->RxBuffer, + MIDI_EPOUT_SIZE); + return ret; +} + +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_EPOUT_ADDR); + pdev->ep_in[MIDI_EPOUT_ADDR & 0xFU].is_used = 0U; + + /* FRee allocated memory */ + if(pdev->pClassData_MIDI != NULL){ + pdev->pClassData_MIDI = NULL; + } + + return USBD_OK; +} + +static uint8_t USBD_MIDI_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req){ + uint16_t len = 0; + uint8_t *pbuf = NULL; + USBD_MIDI_HandleTypeDef *hmidi = (USBD_MIDI_HandleTypeDef*)pdev->pClassData_MIDI; + + switch (req->bmRequest & USB_REQ_TYPE_MASK){ + case USB_REQ_TYPE_CLASS : + switch (req->bRequest){ + case MIDI_REQ_SET_PROTOCOL: + hmidi->Protocol = (uint8_t)(req->wValue); + break; + + case MIDI_REQ_GET_PROTOCOL: + USBD_CtlSendData (pdev, + (uint8_t *)&hmidi->Protocol, + 1); + break; + + case MIDI_REQ_SET_IDLE: + hmidi->IdleState = (uint8_t)(req->wValue >> 8); + break; + + case MIDI_REQ_GET_IDLE: + USBD_CtlSendData (pdev, + (uint8_t *)&hmidi->IdleState, + 1); + break; + + default: + USBD_CtlError (pdev, req); + return USBD_FAIL; + } + break; + + case USB_REQ_TYPE_STANDARD: + switch (req->bRequest) + { + case USB_REQ_GET_DESCRIPTOR: + if( req->wValue >> 8 == MIDI_DESCRIPTOR_TYPE) + { + pbuf = USBD_MIDI_CfgDesc + USB_MIDI_CLASS_DESC_SHIFT; + len = MIN(USB_MIDI_DESC_SIZE , req->wLength); + } + + USBD_CtlSendData (pdev, pbuf, len); + break; + + case USB_REQ_GET_INTERFACE : + USBD_CtlSendData (pdev, + (uint8_t *)&hmidi->AltSetting, + 1); + break; + + case USB_REQ_SET_INTERFACE : + hmidi->AltSetting = (uint8_t)(req->wValue); + break; + } + } + return USBD_OK; +} + +static uint8_t USBD_MIDI_DataIn (USBD_HandleTypeDef *pdev, uint8_t epnum){ + /* Ensure that the FIFO is empty before a new transfer, this condition could + be caused by a new transfer before the end of the previous transfer */ + USBD_MIDI_HandleTypeDef *hmidi = (USBD_MIDI_HandleTypeDef*)pdev->pClassData_MIDI; + hmidi->state = MIDI_IDLE; + return USBD_OK; +} + +static uint8_t USBD_MIDI_DataOut (USBD_HandleTypeDef *pdev, uint8_t epnum){ + if (epnum != (MIDI_EPOUT_ADDR & 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, + hmidi->RxBuffer, + MIDI_EPOUT_SIZE); + + return USBD_OK; +} + + +static uint8_t *USBD_MIDI_GetCfgDesc (uint16_t *length){ + *length = sizeof(USBD_MIDI_CfgDesc); + return USBD_MIDI_CfgDesc; +} + +static uint8_t *USBD_MIDI_GetDeviceQualifierDescriptor (uint16_t *length){ + *length = sizeof (USBD_MIDI_DeviceQualifierDesc); + return USBD_MIDI_DeviceQualifierDesc; +} + +uint8_t USBD_MIDI_RegisterInterface(USBD_HandleTypeDef *pdev, USBD_MIDI_ItfTypeDef *fops){ + if (fops == NULL){ + return (uint8_t)USBD_FAIL; + } + pdev->pUserData_MIDI = fops; + return (uint8_t)USBD_OK; +} + +uint8_t USBD_MIDI_SetRxBuffer(USBD_HandleTypeDef *pdev, uint8_t* pbuf){ + USBD_MIDI_HandleTypeDef *hmidi = NULL; + + hmidi = &MIDI_Class_Data; + + hmidi->RxBuffer = pbuf; + + return (uint8_t)USBD_OK; +} + +void USBD_Update_MIDI_DESC(uint8_t *desc, uint8_t itf_no, uint8_t in_ep, uint8_t out_ep, uint8_t str_idx){ + desc[11] = itf_no; // line 78 + desc[17] = str_idx; // line 84 + desc[24 + MIDI_IN_PORTS_NUM*15 + MIDI_OUT_PORTS_NUM*15 + 3] = out_ep; // line 435 + desc[24 + MIDI_IN_PORTS_NUM*15 + MIDI_OUT_PORTS_NUM*16 + 13 + 3] = in_ep; // line 476 + + MIDI_IN_EP = in_ep; + MIDI_OUT_EP = out_ep; + MIDI_ITF_NBR = itf_no; + MIDI_STR_DESC_IDX = str_idx; +} diff --git a/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/Core/Inc/usbd_def.h b/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/Core/Inc/usbd_def.h index 9787d49..942350c 100755 --- a/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/Core/Inc/usbd_def.h +++ b/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/Core/Inc/usbd_def.h @@ -325,6 +325,8 @@ typedef struct _USBD_HandleTypeDef void *pUserData_DFU; void *pClassData_PRNTR; void *pUserData_PRNTR; + void *pClassData_MIDI; + void *pUserData_MIDI; void *pData; void *pBosDesc; void *pConfDesc; diff --git a/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/Target/usbd_conf.c b/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/Target/usbd_conf.c index da421bb..35b6421 100755 --- a/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/Target/usbd_conf.c +++ b/Middlewares/Third_Party/AL94_USB_Composite/COMPOSITE/Target/usbd_conf.c @@ -342,6 +342,9 @@ USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev) #if (USBD_USE_PRNTR == 1) 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_IN_EP & 0x7F), 128); +#endif #if (USBD_USE_CDC_ACM == 1) for (uint8_t i = 0; i < USBD_CDC_ACM_COUNT; i++) { @@ -485,6 +488,9 @@ USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev) #if (USBD_USE_PRNTR == 1) 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); +#endif #if (USBD_USE_CDC_ACM == 1) for (uint8_t i = 0; i < USBD_CDC_ACM_COUNT; i++) { diff --git a/crow.dfu b/crow.dfu index f060dcc..711d373 100644 Binary files a/crow.dfu and b/crow.dfu differ diff --git a/main.c b/main.c index bf9d864..c634406 100755 --- a/main.c +++ b/main.c @@ -129,7 +129,7 @@ int main(void) // char* crow_msg = "^^v\n\r"; char* crow_msg = "print('hi')\n\r"; USBHost_Send((unsigned char*)crow_msg, strlen(crow_msg)); - Caw_printf("hi\n\r"); + // Caw_printf("hi\n\r"); } U_PrintNow(); diff --git a/usbd/usbd_cdc_interface.c b/usbd/usbd_cdc_interface.c index 8883081..c99dbd7 100755 --- a/usbd/usbd_cdc_interface.c +++ b/usbd/usbd_cdc_interface.c @@ -1,51 +1,3 @@ -/** - ****************************************************************************** - * @file USB_Device/CDC_Standalone/Src/usbd_cdc_interface.c - * @author MCD Application Team - * @version V1.2.1 - * @date 14-April-2017 - * @brief Source file for USBD CDC interface - ****************************************************************************** - * @attention - * - *