stmhal: Tidy up USB CDC+MSC device some more.

This commit is contained in:
Damien George 2014-03-22 13:21:58 +00:00
parent fb1d6d097e
commit 2fb37847a7
6 changed files with 94 additions and 116 deletions

View File

@ -399,7 +399,7 @@ soft_reset:
pyb_usb_host_init(); pyb_usb_host_init();
#elif defined(USE_DEVICE_MODE) #elif defined(USE_DEVICE_MODE)
// USB device // USB device
pyb_usb_dev_init(USBD_DEVICE_MSC, usbd_medium_kind); pyb_usb_dev_init(USBD_DEVICE_CDC_MSC, usbd_medium_kind);
#endif #endif
#if MICROPY_HW_HAS_MMA7660 #if MICROPY_HW_HAS_MMA7660

View File

@ -17,7 +17,6 @@ USBD_HandleTypeDef hUSBDDevice;
#endif #endif
static int dev_is_enabled = 0; static int dev_is_enabled = 0;
uint32_t APP_dev_is_connected = 0; /* used by usbd_cdc_vcp */
mp_obj_t mp_const_vcp_interrupt = MP_OBJ_NULL; mp_obj_t mp_const_vcp_interrupt = MP_OBJ_NULL;
void pyb_usb_dev_init(usbd_device_kind_t device_kind, usbd_storage_medium_kind_t medium_kind) { void pyb_usb_dev_init(usbd_device_kind_t device_kind, usbd_storage_medium_kind_t medium_kind) {
@ -25,33 +24,7 @@ void pyb_usb_dev_init(usbd_device_kind_t device_kind, usbd_storage_medium_kind_t
if (!dev_is_enabled) { if (!dev_is_enabled) {
// only init USB once in the device's power-lifetime // only init USB once in the device's power-lifetime
switch (device_kind) { switch (device_kind) {
#if 0 case USBD_DEVICE_CDC_MSC:
case USBD_DEVICE_CDC:
// XXX USBD_CDC_Init (called by one of these functions below) uses malloc,
// so the memory is invalid after a soft reset (which resets the GC).
USBD_Init(&hUSBDDevice, &VCP_Desc, 0);
USBD_RegisterClass(&hUSBDDevice, &USBD_CDC);
USBD_CDC_RegisterInterface(&hUSBDDevice, (USBD_CDC_ItfTypeDef*)&USBD_CDC_fops);
USBD_Start(&hUSBDDevice);
//USBD_Init(&USB_OTG_Core, USB_OTG_FS_CORE_ID, &USR_desc, &USBD_PYB_cb, &USR_cb);
break;
case USBD_DEVICE_MSC:
// XXX USBD_CDC_Init (called by one of these functions below) uses malloc,
// so the memory is invalid after a soft reset (which resets the GC).
USBD_Init(&hUSBDDevice, &MSC_Desc, 0);
USBD_RegisterClass(&hUSBDDevice, &USBD_MSC);
if (medium_kind == USBD_STORAGE_MEDIUM_FLASH) {
USBD_MSC_RegisterStorage(&hUSBDDevice, (USBD_StorageTypeDef*)&USBD_FLASH_STORAGE_fops);
} else {
USBD_MSC_RegisterStorage(&hUSBDDevice, (USBD_StorageTypeDef*)&USBD_SDCARD_STORAGE_fops);
}
USBD_Start(&hUSBDDevice);
break;
#endif
case USBD_DEVICE_CDC:
case USBD_DEVICE_MSC:
USBD_Init(&hUSBDDevice, &VCP_Desc, 0); USBD_Init(&hUSBDDevice, &VCP_Desc, 0);
USBD_RegisterClass(&hUSBDDevice, &USBD_CDC_MSC); USBD_RegisterClass(&hUSBDDevice, &USBD_CDC_MSC);
USBD_CDC_RegisterInterface(&hUSBDDevice, (USBD_CDC_ItfTypeDef*)&USBD_CDC_fops); USBD_CDC_RegisterInterface(&hUSBDDevice, (USBD_CDC_ItfTypeDef*)&USBD_CDC_fops);
@ -63,7 +36,6 @@ void pyb_usb_dev_init(usbd_device_kind_t device_kind, usbd_storage_medium_kind_t
USBD_Start(&hUSBDDevice); USBD_Start(&hUSBDDevice);
break; break;
case USBD_DEVICE_HID: case USBD_DEVICE_HID:
//USBD_Init(&USB_OTG_Core, USB_OTG_FS_CORE_ID, &USR_desc, &USBD_PYB_HID_cb, &USR_cb); //USBD_Init(&USB_OTG_Core, USB_OTG_FS_CORE_ID, &USR_desc, &USBD_PYB_HID_cb, &USR_cb);
// TODO // TODO
@ -82,7 +54,7 @@ bool usb_vcp_is_enabled(void) {
} }
bool usb_vcp_is_connected(void) { bool usb_vcp_is_connected(void) {
return APP_dev_is_connected; return USBD_CDC_IsConnected();
} }
void usb_vcp_set_interrupt_char(int c) { void usb_vcp_set_interrupt_char(int c) {

View File

@ -5,8 +5,7 @@
#define VCP_CHAR_CTRL_D (4) #define VCP_CHAR_CTRL_D (4)
typedef enum { typedef enum {
USBD_DEVICE_CDC, USBD_DEVICE_CDC_MSC,
USBD_DEVICE_MSC,
USBD_DEVICE_HID, USBD_DEVICE_HID,
} usbd_device_kind_t; } usbd_device_kind_t;

View File

@ -52,6 +52,8 @@
/* Private macro -------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/
static uint8_t dev_is_connected = 0; // indicates if we are connected
static uint8_t UserRxBuffer[APP_RX_DATA_SIZE]; // received data from USB OUT endpoint is stored in this buffer static uint8_t UserRxBuffer[APP_RX_DATA_SIZE]; // received data from USB OUT endpoint is stored in this buffer
static uint16_t UserRxBufCur = 0; // points to next available character in UserRxBuffer static uint16_t UserRxBufCur = 0; // points to next available character in UserRxBuffer
static uint16_t UserRxBufLen = 0; // counts number of valid characters in UserRxBuffer static uint16_t UserRxBufLen = 0; // counts number of valid characters in UserRxBuffer
@ -174,10 +176,8 @@ static int8_t CDC_Itf_DeInit(void)
* @param Len: Number of data to be sent (in bytes) * @param Len: Number of data to be sent (in bytes)
* @retval Result of the opeartion: USBD_OK if all operations are OK else USBD_FAIL * @retval Result of the opeartion: USBD_OK if all operations are OK else USBD_FAIL
*/ */
static int8_t CDC_Itf_Control (uint8_t cmd, uint8_t* pbuf, uint16_t length) static int8_t CDC_Itf_Control(uint8_t cmd, uint8_t* pbuf, uint16_t length) {
{ switch (cmd) {
switch (cmd)
{
case CDC_SEND_ENCAPSULATED_COMMAND: case CDC_SEND_ENCAPSULATED_COMMAND:
/* Add your code here */ /* Add your code here */
break; break;
@ -205,7 +205,6 @@ static int8_t CDC_Itf_Control (uint8_t cmd, uint8_t* pbuf, uint16_t length)
LineCoding.format = pbuf[4]; LineCoding.format = pbuf[4];
LineCoding.paritytype = pbuf[5]; LineCoding.paritytype = pbuf[5];
LineCoding.datatype = pbuf[6]; LineCoding.datatype = pbuf[6];
/* Set the new configuration */ /* Set the new configuration */
#endif #endif
break; break;
@ -232,7 +231,7 @@ static int8_t CDC_Itf_Control (uint8_t cmd, uint8_t* pbuf, uint16_t length)
break; break;
case CDC_SET_CONTROL_LINE_STATE: case CDC_SET_CONTROL_LINE_STATE:
/* Add your code here */ dev_is_connected = length & 1; // wValue is passed in Len (bit of a hack)
break; break;
case CDC_SEND_BREAK: case CDC_SEND_BREAK:
@ -243,7 +242,7 @@ static int8_t CDC_Itf_Control (uint8_t cmd, uint8_t* pbuf, uint16_t length)
break; break;
} }
return (USBD_OK); return USBD_OK;
} }
/** /**
@ -339,6 +338,10 @@ static int8_t CDC_Itf_Receive(uint8_t* Buf, uint32_t *Len) {
return (USBD_OK); return (USBD_OK);
} }
int USBD_CDC_IsConnected(void) {
return dev_is_connected;
}
void USBD_CDC_SetInterrupt(int chr, void *data) { void USBD_CDC_SetInterrupt(int chr, void *data) {
user_interrupt_char = chr; user_interrupt_char = chr;
user_interrupt_data = data; user_interrupt_data = data;

View File

@ -49,6 +49,7 @@
extern const USBD_CDC_ItfTypeDef USBD_CDC_fops; extern const USBD_CDC_ItfTypeDef USBD_CDC_fops;
int USBD_CDC_IsConnected(void);
void USBD_CDC_SetInterrupt(int chr, void *data); void USBD_CDC_SetInterrupt(int chr, void *data);
void USBD_CDC_Tx(const char *str, uint32_t len); void USBD_CDC_Tx(const char *str, uint32_t len);
int USBD_CDC_RxNum(void); int USBD_CDC_RxNum(void);

View File

@ -287,7 +287,7 @@ static uint8_t USBD_CDC_MSC_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef
switch (req->bmRequest & USB_REQ_TYPE_MASK) { switch (req->bmRequest & USB_REQ_TYPE_MASK) {
/* Class request */ // Class request
case USB_REQ_TYPE_CLASS: case USB_REQ_TYPE_CLASS:
// req->wIndex is the recipient interface number // req->wIndex is the recipient interface number
if (0) { if (0) {
@ -295,19 +295,22 @@ static uint8_t USBD_CDC_MSC_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef
} else if (req->wIndex == CDC_IFACE_NUM) { } else if (req->wIndex == CDC_IFACE_NUM) {
// CDC component // CDC component
if (req->wLength) { if (req->wLength) {
if (req->bmRequest & 0x80) if (req->bmRequest & 0x80) {
{ // device-to-host request
CDC_fops->Control(req->bRequest, (uint8_t*)CDC_ClassData.data, req->wLength); CDC_fops->Control(req->bRequest, (uint8_t*)CDC_ClassData.data, req->wLength);
USBD_CtlSendData(pdev, (uint8_t*)CDC_ClassData.data, req->wLength); USBD_CtlSendData(pdev, (uint8_t*)CDC_ClassData.data, req->wLength);
} } else {
else // host-to-device request
{
CDC_ClassData.CmdOpCode = req->bRequest; CDC_ClassData.CmdOpCode = req->bRequest;
CDC_ClassData.CmdLength = req->wLength; CDC_ClassData.CmdLength = req->wLength;
USBD_CtlPrepareRx(pdev, (uint8_t*)CDC_ClassData.data, req->wLength); USBD_CtlPrepareRx(pdev, (uint8_t*)CDC_ClassData.data, req->wLength);
} }
break; } else {
// Not a Data request
// Transfer the command to the interface layer
return CDC_fops->Control(req->bRequest, NULL, req->wValue);
} }
break;
#endif #endif
#if USE_MSC #if USE_MSC
} else if (req->wIndex == MSC_IFACE_NUM) { } else if (req->wIndex == MSC_IFACE_NUM) {