Commit 2fb37847 authored by Damien George's avatar Damien George
Browse files

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

parent fb1d6d09
......@@ -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
......
......@@ -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) {
......
......@@ -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;
......
......@@ -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;
case CDC_GET_ENCAPSULATED_RESPONSE:
/* 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_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_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_SET_CONTROL_LINE_STATE:
/* Add your code here */
break;
case CDC_SEND_BREAK:
/* Add your code here */
break;
default:
break;
}
return (USBD_OK);
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_SET_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_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_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;
......
......@@ -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);
......
......@@ -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 :
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment