Skip to content

Add USB MSC + MSC/CDC Composite Class #1088

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 37 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
26f5806
initial commit of new cdc_msc and updated msc
May 29, 2020
db11e1b
added endpoint configuration
May 29, 2020
1c247ed
added endpoint define changes
May 29, 2020
0b9e38e
added default msc interface
May 29, 2020
162ade3
fixed cdc handle nad renamed cdc msc flag
May 29, 2020
8d7e9d8
added usb class
May 29, 2020
4b489ef
add new include dirs
May 30, 2020
a144bca
formatting changes, improved usbd_ep_conf and now compiles in marlin
Jun 1, 2020
6717196
fixed bug in descriptor, small changes to USB class
Jun 1, 2020
6f3ebfc
fixed duplicate endpoint definition and somewhat fixed endpoint confi…
Jun 2, 2020
ccef556
fixed styling and changed dummy sd usb so it is always not ready
Jun 2, 2020
ff6cecf
fixed small type and more formatting
Jun 2, 2020
da5c702
added ifdef for USB.c
Jun 2, 2020
ffd635d
fixed bug in ST MSC SCSI library
Jun 2, 2020
7676e07
fixed formatting
Jun 2, 2020
3761125
introduced usb msc abstract class
Jun 3, 2020
6f00830
changes to formatting
Jun 3, 2020
f578d6c
more formatting changes
Jun 3, 2020
805113d
and more formatting changes
Jun 3, 2020
5df8d9c
small bugfixes
Jun 3, 2020
5b3e972
added back early usb initialisation and fixed write protection bug in…
Jun 3, 2020
77d673b
fixed hs/fs/other speed interface descriptors for cdc msc
Jun 3, 2020
3f3783e
small changes to ep conf
Jun 3, 2020
3c4d73d
fixed bug where usb device library relies on pdev->pClassData to dete…
Jun 4, 2020
f0e3993
update msc and cdc classes
Jun 4, 2020
a7fd6e2
fixed ep addresses
Jun 4, 2020
df73d28
fixed formatting
Jun 4, 2020
760d8e3
added back cdc clear buffer
Jun 4, 2020
f7dbf85
remove reference to userdata
Jun 4, 2020
6d0023f
fixed pointer error and warning
Jun 4, 2020
4d0f562
changes to cdc msc interface definitions
Jun 4, 2020
b29c3ea
remove SOF from MSC+CDC
Jun 4, 2020
2207a41
further changes during merging
Jun 4, 2020
a341011
set cdc usb handle to usb handle
Jun 4, 2020
8709e8f
refactored cdc msc descriptor and fixed for windowsn
Jun 7, 2020
def0867
fixed formatting
Jun 7, 2020
4447803
added extra board configurations
Jun 7, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added endpoint configuration
  • Loading branch information
Rudi Horn committed Jun 4, 2020
commit db11e1b27f1f006a2c226a51bd935507581327c2
180 changes: 91 additions & 89 deletions cores/arduino/stm32/usb/cdc/usbd_cdc.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,17 @@
*/

#ifdef USBCON
#ifdef USBD_USE_CDC

/* Includes ------------------------------------------------------------------*/
#include "usbd_cdc.h"
#include "usbd_ctlreq.h"

#ifdef USBD_USE_CDC_CLASS

/** @addtogroup STM32_USB_DEVICE_LIBRARY
* @{
*/


/** @defgroup USBD_CDC
* @brief usbd core module
* @{
Expand All @@ -75,15 +74,13 @@
* @}
*/


/** @defgroup USBD_CDC_Private_Defines
* @{
*/
/**
* @}
*/


/** @defgroup USBD_CDC_Private_Macros
* @{
*/
Expand All @@ -92,22 +89,35 @@
* @}
*/


/** @defgroup USBD_CDC_Private_FunctionPrototypes
* @{
*/

static uint8_t USBD_CDC_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx);
static uint8_t USBD_CDC_DeInit(USBD_HandleTypeDef *pdev, uint8_t cfgidx);
static uint8_t USBD_CDC_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req);
static uint8_t USBD_CDC_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum);
static uint8_t USBD_CDC_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum);
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_Init(USBD_HandleTypeDef *pdev,
uint8_t cfgidx);

static uint8_t USBD_CDC_DeInit(USBD_HandleTypeDef *pdev,
uint8_t cfgidx);

static uint8_t USBD_CDC_Setup(USBD_HandleTypeDef *pdev,
USBD_SetupReqTypedef *req);

static uint8_t USBD_CDC_DataIn(USBD_HandleTypeDef *pdev,
uint8_t epnum);

static uint8_t USBD_CDC_DataOut(USBD_HandleTypeDef *pdev,
uint8_t epnum);

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);

uint8_t *USBD_CDC_GetDeviceQualifierDescriptor(uint16_t *length);

/* USB Standard Device Descriptor */
Expand Down Expand Up @@ -402,6 +412,15 @@ __ALIGN_BEGIN static uint8_t USBD_CDC_OtherSpeedCfgDesc[USB_CDC_CONFIG_DESC_SIZ]
0x00 /* bInterval */
};



USBD_CDC_HandleTypeDef cdc_handle_dat;
USBD_CDC_HandleTypeDef *cdc_handle = &cdc_handle_dat;

USBD_CDC_ItfTypeDef *cdc_itf = NULL; /* TODO */



/**
* @}
*/
Expand All @@ -420,7 +439,8 @@ __ALIGN_BEGIN static uint8_t USBD_CDC_OtherSpeedCfgDesc[USB_CDC_CONFIG_DESC_SIZ]
static uint8_t USBD_CDC_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
{
UNUSED(cfgidx);
USBD_CDC_HandleTypeDef *hcdc;

USBD_CDC_HandleTypeDef *hcdc = cdc_handle;

hcdc = USBD_malloc(sizeof(USBD_CDC_HandleTypeDef));

Expand Down Expand Up @@ -468,23 +488,23 @@ static uint8_t USBD_CDC_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
pdev->ep_in[CDC_CMD_EP & 0xFU].is_used = 1U;

/* Init physical Interface components */
((USBD_CDC_ItfTypeDef *)pdev->pUserData)->Init();
cdc_itf->Init();

/* Init Xfer states */
hcdc->TxState = 0U;
hcdc->RxState = 0U;

if (pdev->dev_speed == USBD_SPEED_HIGH) {
/* Prepare Out endpoint to receive next packet */
(void)USBD_LL_PrepareReceive(pdev, CDC_OUT_EP, hcdc->RxBuffer,
CDC_DATA_HS_OUT_PACKET_SIZE);
USBD_LL_PrepareReceive(pdev, CDC_OUT_EP, hcdc->RxBuffer,
CDC_DATA_HS_OUT_PACKET_SIZE);
} else {
/* Prepare Out endpoint to receive next packet */
(void)USBD_LL_PrepareReceive(pdev, CDC_OUT_EP, hcdc->RxBuffer,
CDC_DATA_FS_OUT_PACKET_SIZE);
USBD_LL_PrepareReceive(pdev, CDC_OUT_EP, hcdc->RxBuffer,
CDC_DATA_FS_OUT_PACKET_SIZE);
}

return (uint8_t)USBD_OK;
return ret;
}

/**
Expand Down Expand Up @@ -513,11 +533,8 @@ static uint8_t USBD_CDC_DeInit(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
pdev->ep_in[CDC_CMD_EP & 0xFU].bInterval = 0U;

/* DeInit physical Interface components */
if (pdev->pClassData != NULL) {
((USBD_CDC_ItfTypeDef *)pdev->pUserData)->DeInit();
(void)USBD_free(pdev->pClassData);
pdev->pClassData = NULL;
}
/* TODO remove user data reference */
cdc_itf->DeInit();

return ret;
}
Expand All @@ -532,18 +549,18 @@ static uint8_t USBD_CDC_DeInit(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
static uint8_t USBD_CDC_Setup(USBD_HandleTypeDef *pdev,
USBD_SetupReqTypedef *req)
{
USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *) pdev->pClassData;
USBD_CDC_HandleTypeDef *hcdc = cdc_handle;
uint8_t ifalt = 0U;
uint16_t status_info = 0U;
USBD_StatusTypeDef ret = USBD_OK;

switch (req->bmRequest & USB_REQ_TYPE_MASK) {
case USB_REQ_TYPE_CLASS:
if (req->wLength != 0U) {
if ((req->bmRequest & 0x80U) != 0U) {
((USBD_CDC_ItfTypeDef *)pdev->pUserData)->Control(req->bRequest,
(uint8_t *)hcdc->data,
req->wLength);
case USB_REQ_TYPE_CLASS :
if (req->wLength) {
if (req->bmRequest & 0x80U) {
cdc_itf->Control(req->bRequest,
(uint8_t *)(void *)hcdc->data,
req->wLength);

(void)USBD_CtlSendData(pdev, (uint8_t *)hcdc->data, req->wLength);
} else {
Expand All @@ -553,8 +570,8 @@ static uint8_t USBD_CDC_Setup(USBD_HandleTypeDef *pdev,
(void)USBD_CtlPrepareRx(pdev, (uint8_t *)hcdc->data, req->wLength);
}
} else {
((USBD_CDC_ItfTypeDef *)pdev->pUserData)->Control(req->bRequest,
(uint8_t *)req, 0U);
cdc_itf->Control(req->bRequest,
(uint8_t *)(void *)req, 0U);
}
break;

Expand Down Expand Up @@ -613,26 +630,23 @@ static uint8_t USBD_CDC_Setup(USBD_HandleTypeDef *pdev,
*/
static uint8_t USBD_CDC_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum)
{
USBD_CDC_HandleTypeDef *hcdc;
USBD_CDC_HandleTypeDef *hcdc = cdc_handle;
PCD_HandleTypeDef *hpcd = pdev->pData;

if (pdev->pClassData == NULL) {
return (uint8_t)USBD_FAIL;
}
hcdc = (USBD_CDC_HandleTypeDef *)pdev->pClassData;
USBD_CDC_ItfTypeDef *ctrl = cdc_itf;

if ((pdev->ep_in[epnum].total_length > 0U) && ((pdev->ep_in[epnum].total_length % hpcd->IN_ep[epnum].maxpacket) == 0U)) {
/* Update the packet total length */
pdev->ep_in[epnum].total_length = 0U;

/* Send ZLP */
(void)USBD_LL_Transmit(pdev, epnum, NULL, 0U);
USBD_LL_Transmit(pdev, epnum, NULL, 0U);
} else {
hcdc->TxState = 0U;
((USBD_CDC_ItfTypeDef *)pdev->pUserData)->TransmitCplt(hcdc->TxBuffer, &hcdc->TxLength, epnum);
if (ctrl->Transferred) {
ctrl->Transferred();
}
}

return (uint8_t)USBD_OK;
return USBD_OK;
}

/**
Expand All @@ -644,21 +658,16 @@ static uint8_t USBD_CDC_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum)
*/
static uint8_t USBD_CDC_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum)
{
USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *)pdev->pClassData;

if (pdev->pClassData == NULL) {
return (uint8_t)USBD_FAIL;
}
USBD_CDC_HandleTypeDef *hcdc = cdc_handle;

/* Get the received data length */
hcdc->RxLength = USBD_LL_GetRxDataSize(pdev, epnum);

/* USB data will be immediately processed, this allow next USB traffic being
NAKed till the end of the application Xfer */
cdc_itf->Receive(hcdc->RxBuffer, &hcdc->RxLength);

((USBD_CDC_ItfTypeDef *)pdev->pUserData)->Receive(hcdc->RxBuffer, &hcdc->RxLength);

return (uint8_t)USBD_OK;
return USBD_OK;
}

/**
Expand All @@ -669,14 +678,13 @@ static uint8_t USBD_CDC_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum)
*/
static uint8_t USBD_CDC_EP0_RxReady(USBD_HandleTypeDef *pdev)
{
USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *)pdev->pClassData;
USBD_CDC_HandleTypeDef *hcdc = cdc_handle;

if ((pdev->pUserData != NULL) && (hcdc->CmdOpCode != 0xFFU)) {
((USBD_CDC_ItfTypeDef *)pdev->pUserData)->Control(hcdc->CmdOpCode,
(uint8_t *)hcdc->data,
(uint16_t)hcdc->CmdLength);
cdc_itf->Control(hcdc->CmdOpCode,
(uint8_t *)(void *)hcdc->data,
(uint16_t)hcdc->CmdLength);
hcdc->CmdOpCode = 0xFFU;

}

return (uint8_t)USBD_OK;
Expand Down Expand Up @@ -764,7 +772,7 @@ uint8_t USBD_CDC_RegisterInterface(USBD_HandleTypeDef *pdev,
uint8_t USBD_CDC_SetTxBuffer(USBD_HandleTypeDef *pdev,
uint8_t *pbuff, uint32_t length)
{
USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *)pdev->pClassData;
USBD_CDC_HandleTypeDef *hcdc = cdc_handle;

hcdc->TxBuffer = pbuff;
hcdc->TxLength = length;
Expand All @@ -781,7 +789,7 @@ uint8_t USBD_CDC_SetTxBuffer(USBD_HandleTypeDef *pdev,
*/
uint8_t USBD_CDC_SetRxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff)
{
USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *)pdev->pClassData;
USBD_CDC_HandleTypeDef *hcdc = cdc_handle;

hcdc->RxBuffer = pbuff;

Expand All @@ -796,12 +804,7 @@ uint8_t USBD_CDC_SetRxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff)
*/
uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev)
{
USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *)pdev->pClassData;
USBD_StatusTypeDef ret = USBD_BUSY;

if (pdev->pClassData == NULL) {
return (uint8_t)USBD_FAIL;
}
USBD_CDC_HandleTypeDef *hcdc = cdc_handle;

if (hcdc->TxState == 0U) {
/* Tx Transfer in progress */
Expand All @@ -811,9 +814,12 @@ uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev)
pdev->ep_in[CDC_IN_EP & 0xFU].total_length = hcdc->TxLength;

/* Transmit next packet */
(void)USBD_LL_Transmit(pdev, CDC_IN_EP, hcdc->TxBuffer, hcdc->TxLength);
USBD_LL_Transmit(pdev, CDC_IN_EP, hcdc->TxBuffer,
(uint16_t)hcdc->TxLength);

ret = USBD_OK;
return USBD_OK;
} else {
return USBD_BUSY;
}

return (uint8_t)ret;
Expand All @@ -828,37 +834,33 @@ uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev)
*/
uint8_t USBD_CDC_ReceivePacket(USBD_HandleTypeDef *pdev)
{
USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *)pdev->pClassData;

if (pdev->pClassData == NULL) {
return (uint8_t)USBD_FAIL;
}
USBD_CDC_HandleTypeDef *hcdc = cdc_handle;

/* Suspend or Resume USB Out process */
if (pdev->dev_speed == USBD_SPEED_HIGH) {
/* Prepare Out endpoint to receive next packet */
(void)USBD_LL_PrepareReceive(pdev, CDC_OUT_EP, hcdc->RxBuffer,
CDC_DATA_HS_OUT_PACKET_SIZE);
USBD_LL_PrepareReceive(pdev,
CDC_OUT_EP,
hcdc->RxBuffer,
CDC_DATA_HS_OUT_PACKET_SIZE);
} else {
/* Prepare Out endpoint to receive next packet */
(void)USBD_LL_PrepareReceive(pdev, CDC_OUT_EP, hcdc->RxBuffer,
CDC_DATA_FS_OUT_PACKET_SIZE);
USBD_LL_PrepareReceive(pdev,
CDC_OUT_EP,
hcdc->RxBuffer,
CDC_DATA_FS_OUT_PACKET_SIZE);
}

return (uint8_t)USBD_OK;
return USBD_OK;
}

uint8_t USBD_CDC_ClearBuffer(USBD_HandleTypeDef *pdev)
{
/* Suspend or Resume USB Out process */
if (pdev->pClassData != NULL) {
/* Prepare Out endpoint to receive next packet */
USBD_LL_PrepareReceive(pdev, CDC_OUT_EP, 0, 0);
return (uint8_t)USBD_OK;
} else {
return (uint8_t)USBD_FAIL;
}
/* Prepare Out endpoint to receive next packet */
USBD_LL_PrepareReceive(pdev, CDC_OUT_EP, 0, 0);
return USBD_OK;
}

#endif /* USBD_USE_CDC */
#endif /* USBD_USE_CDC_CLASS */
#endif /* USBCON */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/