Skip to content

Commit

Permalink
cdc + midi composite device identified on PC!
Browse files Browse the repository at this point in the history
  • Loading branch information
trentgill committed Jun 11, 2024
1 parent 3dfab02 commit e5f5f19
Show file tree
Hide file tree
Showing 17 changed files with 953 additions and 122 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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/ \
Expand Down Expand Up @@ -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) \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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****/
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,17 @@
#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

/*---------- _USBD_USE_PRNTR -----------*/
#define _USBD_USE_PRNTR false

/*---------- _USBD_USE_MIDI -----------*/
#define _USBD_USE_MIDI true

/*---------- _STM32F1_DEVICE -----------*/
#define _STM32F1_DEVICE false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

/**
* @}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 */
Expand Down
Loading

0 comments on commit e5f5f19

Please sign in to comment.