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();
#elif defined(USE_DEVICE_MODE)
// USB device
pyb_usb_dev_init(USBD_DEVICE_MSC, usbd_medium_kind);
pyb_usb_dev_init(USBD_DEVICE_CDC_MSC, usbd_medium_kind);
#endif
#if MICROPY_HW_HAS_MMA7660

View File

@ -17,7 +17,6 @@ USBD_HandleTypeDef hUSBDDevice;
#endif
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;
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) {
// only init USB once in the device's power-lifetime
switch (device_kind) {
#if 0
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:
case USBD_DEVICE_CDC_MSC:
USBD_Init(&hUSBDDevice, &VCP_Desc, 0);
USBD_RegisterClass(&hUSBDDevice, &USBD_CDC_MSC);
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);
break;
case USBD_DEVICE_HID:
//USBD_Init(&USB_OTG_Core, USB_OTG_FS_CORE_ID, &USR_desc, &USBD_PYB_HID_cb, &USR_cb);
// TODO
@ -82,7 +54,7 @@ bool usb_vcp_is_enabled(void) {
}
bool usb_vcp_is_connected(void) {
return APP_dev_is_connected;
return USBD_CDC_IsConnected();
}
void usb_vcp_set_interrupt_char(int c) {

View File

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

View File

@ -52,6 +52,8 @@
/* Private macro -------------------------------------------------------------*/
/* 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 uint16_t UserRxBufCur = 0; // points to next available character 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)
* @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)
{
switch (cmd)
{
static int8_t CDC_Itf_Control(uint8_t cmd, uint8_t* pbuf, uint16_t length) {
switch (cmd) {
case CDC_SEND_ENCAPSULATED_COMMAND:
/* Add your code here */
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.paritytype = pbuf[5];
LineCoding.datatype = pbuf[6];
/* Set the new configuration */
#endif
break;
@ -232,7 +231,7 @@ static int8_t CDC_Itf_Control (uint8_t cmd, uint8_t* pbuf, uint16_t length)
break;
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;
case CDC_SEND_BREAK:
@ -243,7 +242,7 @@ static int8_t CDC_Itf_Control (uint8_t cmd, uint8_t* pbuf, uint16_t length)
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);
}
int USBD_CDC_IsConnected(void) {
return dev_is_connected;
}
void USBD_CDC_SetInterrupt(int chr, void *data) {
user_interrupt_char = chr;
user_interrupt_data = data;

View File

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

View File

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