Skip to content

Commit 2fb3784

Browse files
committed
stmhal: Tidy up USB CDC+MSC device some more.
1 parent fb1d6d0 commit 2fb3784

File tree

6 files changed

+102
-124
lines changed

6 files changed

+102
-124
lines changed

stmhal/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ int main(void) {
399399
pyb_usb_host_init();
400400
#elif defined(USE_DEVICE_MODE)
401401
// USB device
402-
pyb_usb_dev_init(USBD_DEVICE_MSC, usbd_medium_kind);
402+
pyb_usb_dev_init(USBD_DEVICE_CDC_MSC, usbd_medium_kind);
403403
#endif
404404

405405
#if MICROPY_HW_HAS_MMA7660

stmhal/usb.c

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,14 @@ USBD_HandleTypeDef hUSBDDevice;
1717
#endif
1818

1919
static int dev_is_enabled = 0;
20-
uint32_t APP_dev_is_connected = 0; /* used by usbd_cdc_vcp */
2120
mp_obj_t mp_const_vcp_interrupt = MP_OBJ_NULL;
2221

2322
void pyb_usb_dev_init(usbd_device_kind_t device_kind, usbd_storage_medium_kind_t medium_kind) {
2423
#ifdef USE_DEVICE_MODE
2524
if (!dev_is_enabled) {
2625
// only init USB once in the device's power-lifetime
2726
switch (device_kind) {
28-
#if 0
29-
case USBD_DEVICE_CDC:
30-
// XXX USBD_CDC_Init (called by one of these functions below) uses malloc,
31-
// so the memory is invalid after a soft reset (which resets the GC).
32-
USBD_Init(&hUSBDDevice, &VCP_Desc, 0);
33-
USBD_RegisterClass(&hUSBDDevice, &USBD_CDC);
34-
USBD_CDC_RegisterInterface(&hUSBDDevice, (USBD_CDC_ItfTypeDef*)&USBD_CDC_fops);
35-
USBD_Start(&hUSBDDevice);
36-
//USBD_Init(&USB_OTG_Core, USB_OTG_FS_CORE_ID, &USR_desc, &USBD_PYB_cb, &USR_cb);
37-
break;
38-
39-
case USBD_DEVICE_MSC:
40-
// XXX USBD_CDC_Init (called by one of these functions below) uses malloc,
41-
// so the memory is invalid after a soft reset (which resets the GC).
42-
USBD_Init(&hUSBDDevice, &MSC_Desc, 0);
43-
USBD_RegisterClass(&hUSBDDevice, &USBD_MSC);
44-
if (medium_kind == USBD_STORAGE_MEDIUM_FLASH) {
45-
USBD_MSC_RegisterStorage(&hUSBDDevice, (USBD_StorageTypeDef*)&USBD_FLASH_STORAGE_fops);
46-
} else {
47-
USBD_MSC_RegisterStorage(&hUSBDDevice, (USBD_StorageTypeDef*)&USBD_SDCARD_STORAGE_fops);
48-
}
49-
USBD_Start(&hUSBDDevice);
50-
break;
51-
#endif
52-
53-
case USBD_DEVICE_CDC:
54-
case USBD_DEVICE_MSC:
27+
case USBD_DEVICE_CDC_MSC:
5528
USBD_Init(&hUSBDDevice, &VCP_Desc, 0);
5629
USBD_RegisterClass(&hUSBDDevice, &USBD_CDC_MSC);
5730
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
6336
USBD_Start(&hUSBDDevice);
6437
break;
6538

66-
6739
case USBD_DEVICE_HID:
6840
//USBD_Init(&USB_OTG_Core, USB_OTG_FS_CORE_ID, &USR_desc, &USBD_PYB_HID_cb, &USR_cb);
6941
// TODO
@@ -82,7 +54,7 @@ bool usb_vcp_is_enabled(void) {
8254
}
8355

8456
bool usb_vcp_is_connected(void) {
85-
return APP_dev_is_connected;
57+
return USBD_CDC_IsConnected();
8658
}
8759

8860
void usb_vcp_set_interrupt_char(int c) {

stmhal/usb.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
#define VCP_CHAR_CTRL_D (4)
66

77
typedef enum {
8-
USBD_DEVICE_CDC,
9-
USBD_DEVICE_MSC,
8+
USBD_DEVICE_CDC_MSC,
109
USBD_DEVICE_HID,
1110
} usbd_device_kind_t;
1211

stmhal/usbd_cdc_interface.c

Lines changed: 73 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
/* Private macro -------------------------------------------------------------*/
5353
/* Private variables ---------------------------------------------------------*/
5454

55+
static uint8_t dev_is_connected = 0; // indicates if we are connected
56+
5557
static uint8_t UserRxBuffer[APP_RX_DATA_SIZE]; // received data from USB OUT endpoint is stored in this buffer
5658
static uint16_t UserRxBufCur = 0; // points to next available character in UserRxBuffer
5759
static uint16_t UserRxBufLen = 0; // counts number of valid characters in UserRxBuffer
@@ -174,76 +176,73 @@ static int8_t CDC_Itf_DeInit(void)
174176
* @param Len: Number of data to be sent (in bytes)
175177
* @retval Result of the opeartion: USBD_OK if all operations are OK else USBD_FAIL
176178
*/
177-
static int8_t CDC_Itf_Control (uint8_t cmd, uint8_t* pbuf, uint16_t length)
178-
{
179-
switch (cmd)
180-
{
181-
case CDC_SEND_ENCAPSULATED_COMMAND:
182-
/* Add your code here */
183-
break;
184-
185-
case CDC_GET_ENCAPSULATED_RESPONSE:
186-
/* Add your code here */
187-
break;
188-
189-
case CDC_SET_COMM_FEATURE:
190-
/* Add your code here */
191-
break;
192-
193-
case CDC_GET_COMM_FEATURE:
194-
/* Add your code here */
195-
break;
196-
197-
case CDC_CLEAR_COMM_FEATURE:
198-
/* Add your code here */
199-
break;
200-
201-
case CDC_SET_LINE_CODING:
202-
#if 0
203-
LineCoding.bitrate = (uint32_t)(pbuf[0] | (pbuf[1] << 8) |\
204-
(pbuf[2] << 16) | (pbuf[3] << 24));
205-
LineCoding.format = pbuf[4];
206-
LineCoding.paritytype = pbuf[5];
207-
LineCoding.datatype = pbuf[6];
208-
209-
/* Set the new configuration */
210-
#endif
211-
break;
212-
213-
case CDC_GET_LINE_CODING:
214-
#if 0
215-
pbuf[0] = (uint8_t)(LineCoding.bitrate);
216-
pbuf[1] = (uint8_t)(LineCoding.bitrate >> 8);
217-
pbuf[2] = (uint8_t)(LineCoding.bitrate >> 16);
218-
pbuf[3] = (uint8_t)(LineCoding.bitrate >> 24);
219-
pbuf[4] = LineCoding.format;
220-
pbuf[5] = LineCoding.paritytype;
221-
pbuf[6] = LineCoding.datatype;
222-
#endif
223-
224-
/* Add your code here */
225-
pbuf[0] = (uint8_t)(115200);
226-
pbuf[1] = (uint8_t)(115200 >> 8);
227-
pbuf[2] = (uint8_t)(115200 >> 16);
228-
pbuf[3] = (uint8_t)(115200 >> 24);
229-
pbuf[4] = 0; // stop bits (1)
230-
pbuf[5] = 0; // parity (none)
231-
pbuf[6] = 8; // number of bits (8)
232-
break;
233-
234-
case CDC_SET_CONTROL_LINE_STATE:
235-
/* Add your code here */
236-
break;
237-
238-
case CDC_SEND_BREAK:
239-
/* Add your code here */
240-
break;
241-
242-
default:
243-
break;
244-
}
245-
246-
return (USBD_OK);
179+
static int8_t CDC_Itf_Control(uint8_t cmd, uint8_t* pbuf, uint16_t length) {
180+
switch (cmd) {
181+
case CDC_SEND_ENCAPSULATED_COMMAND:
182+
/* Add your code here */
183+
break;
184+
185+
case CDC_GET_ENCAPSULATED_RESPONSE:
186+
/* Add your code here */
187+
break;
188+
189+
case CDC_SET_COMM_FEATURE:
190+
/* Add your code here */
191+
break;
192+
193+
case CDC_GET_COMM_FEATURE:
194+
/* Add your code here */
195+
break;
196+
197+
case CDC_CLEAR_COMM_FEATURE:
198+
/* Add your code here */
199+
break;
200+
201+
case CDC_SET_LINE_CODING:
202+
#if 0
203+
LineCoding.bitrate = (uint32_t)(pbuf[0] | (pbuf[1] << 8) |\
204+
(pbuf[2] << 16) | (pbuf[3] << 24));
205+
LineCoding.format = pbuf[4];
206+
LineCoding.paritytype = pbuf[5];
207+
LineCoding.datatype = pbuf[6];
208+
/* Set the new configuration */
209+
#endif
210+
break;
211+
212+
case CDC_GET_LINE_CODING:
213+
#if 0
214+
pbuf[0] = (uint8_t)(LineCoding.bitrate);
215+
pbuf[1] = (uint8_t)(LineCoding.bitrate >> 8);
216+
pbuf[2] = (uint8_t)(LineCoding.bitrate >> 16);
217+
pbuf[3] = (uint8_t)(LineCoding.bitrate >> 24);
218+
pbuf[4] = LineCoding.format;
219+
pbuf[5] = LineCoding.paritytype;
220+
pbuf[6] = LineCoding.datatype;
221+
#endif
222+
223+
/* Add your code here */
224+
pbuf[0] = (uint8_t)(115200);
225+
pbuf[1] = (uint8_t)(115200 >> 8);
226+
pbuf[2] = (uint8_t)(115200 >> 16);
227+
pbuf[3] = (uint8_t)(115200 >> 24);
228+
pbuf[4] = 0; // stop bits (1)
229+
pbuf[5] = 0; // parity (none)
230+
pbuf[6] = 8; // number of bits (8)
231+
break;
232+
233+
case CDC_SET_CONTROL_LINE_STATE:
234+
dev_is_connected = length & 1; // wValue is passed in Len (bit of a hack)
235+
break;
236+
237+
case CDC_SEND_BREAK:
238+
/* Add your code here */
239+
break;
240+
241+
default:
242+
break;
243+
}
244+
245+
return USBD_OK;
247246
}
248247

249248
/**
@@ -339,6 +338,10 @@ static int8_t CDC_Itf_Receive(uint8_t* Buf, uint32_t *Len) {
339338
return (USBD_OK);
340339
}
341340

341+
int USBD_CDC_IsConnected(void) {
342+
return dev_is_connected;
343+
}
344+
342345
void USBD_CDC_SetInterrupt(int chr, void *data) {
343346
user_interrupt_char = chr;
344347
user_interrupt_data = data;

stmhal/usbd_cdc_interface.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949

5050
extern const USBD_CDC_ItfTypeDef USBD_CDC_fops;
5151

52+
int USBD_CDC_IsConnected(void);
5253
void USBD_CDC_SetInterrupt(int chr, void *data);
5354
void USBD_CDC_Tx(const char *str, uint32_t len);
5455
int USBD_CDC_RxNum(void);

stmhal/usbdev/class/cdc_msc/src/usbd_cdc_msc.c

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -287,30 +287,33 @@ static uint8_t USBD_CDC_MSC_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef
287287

288288
switch (req->bmRequest & USB_REQ_TYPE_MASK) {
289289

290-
/* Class request */
291-
case USB_REQ_TYPE_CLASS :
292-
// req->wIndex is the recipient interface number
293-
if (0) {
290+
// Class request
291+
case USB_REQ_TYPE_CLASS:
292+
// req->wIndex is the recipient interface number
293+
if (0) {
294294
#if USE_CDC
295-
} else if (req->wIndex == CDC_IFACE_NUM) {
296-
// CDC component
297-
if (req->wLength) {
298-
if (req->bmRequest & 0x80)
299-
{
300-
CDC_fops->Control(req->bRequest, (uint8_t *)CDC_ClassData.data, req->wLength);
301-
USBD_CtlSendData (pdev, (uint8_t *)CDC_ClassData.data, req->wLength);
302-
}
303-
else
304-
{
305-
CDC_ClassData.CmdOpCode = req->bRequest;
306-
CDC_ClassData.CmdLength = req->wLength;
307-
USBD_CtlPrepareRx (pdev, (uint8_t *)CDC_ClassData.data, req->wLength);
308-
}
309-
break;
310-
}
295+
} else if (req->wIndex == CDC_IFACE_NUM) {
296+
// CDC component
297+
if (req->wLength) {
298+
if (req->bmRequest & 0x80) {
299+
// device-to-host request
300+
CDC_fops->Control(req->bRequest, (uint8_t*)CDC_ClassData.data, req->wLength);
301+
USBD_CtlSendData(pdev, (uint8_t*)CDC_ClassData.data, req->wLength);
302+
} else {
303+
// host-to-device request
304+
CDC_ClassData.CmdOpCode = req->bRequest;
305+
CDC_ClassData.CmdLength = req->wLength;
306+
USBD_CtlPrepareRx(pdev, (uint8_t*)CDC_ClassData.data, req->wLength);
307+
}
308+
} else {
309+
// Not a Data request
310+
// Transfer the command to the interface layer
311+
return CDC_fops->Control(req->bRequest, NULL, req->wValue);
312+
}
313+
break;
311314
#endif
312315
#if USE_MSC
313-
} else if (req->wIndex == MSC_IFACE_NUM) {
316+
} else if (req->wIndex == MSC_IFACE_NUM) {
314317
// MSC component
315318
switch (req->bRequest) {
316319
case BOT_GET_MAX_LUN :

0 commit comments

Comments
 (0)