From 2fb37847a712127543c879cd4c925ac4ee3c8554 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 22 Mar 2014 13:21:58 +0000 Subject: [PATCH] stmhal: Tidy up USB CDC+MSC device some more. --- stmhal/main.c | 2 +- stmhal/usb.c | 32 +---- stmhal/usb.h | 3 +- stmhal/usbd_cdc_interface.c | 127 +++++++++--------- stmhal/usbd_cdc_interface.h | 1 + .../usbdev/class/cdc_msc/src/usbd_cdc_msc.c | 45 ++++--- 6 files changed, 94 insertions(+), 116 deletions(-) diff --git a/stmhal/main.c b/stmhal/main.c index f07a5eef12..b3e40edf7a 100644 --- a/stmhal/main.c +++ b/stmhal/main.c @@ -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 diff --git a/stmhal/usb.c b/stmhal/usb.c index 43fc6d7a82..aa006b8b8f 100644 --- a/stmhal/usb.c +++ b/stmhal/usb.c @@ -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) { diff --git a/stmhal/usb.h b/stmhal/usb.h index bb6283c89a..e30b1364f9 100644 --- a/stmhal/usb.h +++ b/stmhal/usb.h @@ -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; diff --git a/stmhal/usbd_cdc_interface.c b/stmhal/usbd_cdc_interface.c index 6316439356..5116ce860e 100644 --- a/stmhal/usbd_cdc_interface.c +++ b/stmhal/usbd_cdc_interface.c @@ -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,76 +176,73 @@ 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) - { - case CDC_SEND_ENCAPSULATED_COMMAND: - /* Add your code here */ - break; +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; - case CDC_GET_ENCAPSULATED_RESPONSE: - /* Add your code here */ - break; + case CDC_GET_ENCAPSULATED_RESPONSE: + /* Add your code here */ + break; - case CDC_SET_COMM_FEATURE: - /* Add your code here */ - break; + case CDC_SET_COMM_FEATURE: + /* Add your code here */ + break; - case CDC_GET_COMM_FEATURE: - /* Add your code here */ - break; + case CDC_GET_COMM_FEATURE: + /* Add your code here */ + break; - case CDC_CLEAR_COMM_FEATURE: - /* Add your code here */ - break; + case CDC_CLEAR_COMM_FEATURE: + /* Add your code here */ + break; - case CDC_SET_LINE_CODING: - #if 0 - LineCoding.bitrate = (uint32_t)(pbuf[0] | (pbuf[1] << 8) |\ - (pbuf[2] << 16) | (pbuf[3] << 24)); - LineCoding.format = pbuf[4]; - LineCoding.paritytype = pbuf[5]; - LineCoding.datatype = pbuf[6]; - - /* Set the new configuration */ - #endif - break; + case CDC_SET_LINE_CODING: + #if 0 + LineCoding.bitrate = (uint32_t)(pbuf[0] | (pbuf[1] << 8) |\ + (pbuf[2] << 16) | (pbuf[3] << 24)); + LineCoding.format = pbuf[4]; + LineCoding.paritytype = pbuf[5]; + LineCoding.datatype = pbuf[6]; + /* Set the new configuration */ + #endif + break; - case CDC_GET_LINE_CODING: - #if 0 - pbuf[0] = (uint8_t)(LineCoding.bitrate); - pbuf[1] = (uint8_t)(LineCoding.bitrate >> 8); - pbuf[2] = (uint8_t)(LineCoding.bitrate >> 16); - pbuf[3] = (uint8_t)(LineCoding.bitrate >> 24); - pbuf[4] = LineCoding.format; - pbuf[5] = LineCoding.paritytype; - pbuf[6] = LineCoding.datatype; - #endif - - /* Add your code here */ - pbuf[0] = (uint8_t)(115200); - pbuf[1] = (uint8_t)(115200 >> 8); - pbuf[2] = (uint8_t)(115200 >> 16); - pbuf[3] = (uint8_t)(115200 >> 24); - pbuf[4] = 0; // stop bits (1) - pbuf[5] = 0; // parity (none) - pbuf[6] = 8; // number of bits (8) - break; + case CDC_GET_LINE_CODING: + #if 0 + pbuf[0] = (uint8_t)(LineCoding.bitrate); + pbuf[1] = (uint8_t)(LineCoding.bitrate >> 8); + pbuf[2] = (uint8_t)(LineCoding.bitrate >> 16); + pbuf[3] = (uint8_t)(LineCoding.bitrate >> 24); + pbuf[4] = LineCoding.format; + pbuf[5] = LineCoding.paritytype; + pbuf[6] = LineCoding.datatype; + #endif - case CDC_SET_CONTROL_LINE_STATE: - /* Add your code here */ - break; + /* Add your code here */ + pbuf[0] = (uint8_t)(115200); + pbuf[1] = (uint8_t)(115200 >> 8); + pbuf[2] = (uint8_t)(115200 >> 16); + pbuf[3] = (uint8_t)(115200 >> 24); + pbuf[4] = 0; // stop bits (1) + pbuf[5] = 0; // parity (none) + pbuf[6] = 8; // number of bits (8) + break; - case CDC_SEND_BREAK: - /* Add your code here */ - break; - - default: - break; - } - - return (USBD_OK); + case CDC_SET_CONTROL_LINE_STATE: + dev_is_connected = length & 1; // wValue is passed in Len (bit of a hack) + break; + + case CDC_SEND_BREAK: + /* Add your code here */ + break; + + default: + break; + } + + 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; diff --git a/stmhal/usbd_cdc_interface.h b/stmhal/usbd_cdc_interface.h index b50cbf4c29..bcb6333a6a 100644 --- a/stmhal/usbd_cdc_interface.h +++ b/stmhal/usbd_cdc_interface.h @@ -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); diff --git a/stmhal/usbdev/class/cdc_msc/src/usbd_cdc_msc.c b/stmhal/usbdev/class/cdc_msc/src/usbd_cdc_msc.c index df080ee4f0..07e801e9bd 100644 --- a/stmhal/usbdev/class/cdc_msc/src/usbd_cdc_msc.c +++ b/stmhal/usbdev/class/cdc_msc/src/usbd_cdc_msc.c @@ -287,30 +287,33 @@ 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 : - // req->wIndex is the recipient interface number - if (0) { + // 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 - { - CDC_ClassData.CmdOpCode = req->bRequest; - CDC_ClassData.CmdLength = req->wLength; - USBD_CtlPrepareRx (pdev, (uint8_t *)CDC_ClassData.data, req->wLength); - } - break; - } + } else if (req->wIndex == CDC_IFACE_NUM) { + // CDC component + if (req->wLength) { + 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); + } + } 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) { + } else if (req->wIndex == MSC_IFACE_NUM) { // MSC component switch (req->bRequest) { case BOT_GET_MAX_LUN :