Skip to content

Commit 64563e3

Browse files
committed
stmhal: Add CDC+HID USB device.
The HID device must appear before the CDC in order for both to work at the same time. Whilst the code is working, it's not currently used.
1 parent f4417a1 commit 64563e3

4 files changed

Lines changed: 740 additions & 10 deletions

File tree

stmhal/modpyb.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@
2828
#include "servo.h"
2929
#include "dac.h"
3030
#include "i2c.h"
31-
#if 0
3231
#include "usb.h"
33-
#endif
3432
#include "modpyb.h"
3533
#include "ff.h"
3634

@@ -185,15 +183,14 @@ STATIC mp_obj_t pyb_standby(void) {
185183
MP_DEFINE_CONST_FUN_OBJ_0(pyb_standby_obj, pyb_standby);
186184

187185
STATIC mp_obj_t pyb_hid_send_report(mp_obj_t arg) {
188-
#if 0
189-
mp_obj_t *items = mp_obj_get_array_fixed_n(arg, 4);
186+
mp_obj_t *items;
187+
mp_obj_get_array_fixed_n(arg, 4, &items);
190188
uint8_t data[4];
191189
data[0] = mp_obj_get_int(items[0]);
192190
data[1] = mp_obj_get_int(items[1]);
193191
data[2] = mp_obj_get_int(items[2]);
194192
data[3] = mp_obj_get_int(items[3]);
195193
usb_hid_send_report(data);
196-
#endif
197194
return mp_const_none;
198195
}
199196

@@ -267,9 +264,7 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = {
267264
{ MP_OBJ_NEW_QSTR(MP_QSTR_Accel), (mp_obj_t)&pyb_accel_type },
268265
#endif
269266

270-
#if 0
271267
{ MP_OBJ_NEW_QSTR(MP_QSTR_hid), (mp_obj_t)&pyb_hid_send_report_obj },
272-
#endif
273268

274269
// input
275270
{ MP_OBJ_NEW_QSTR(MP_QSTR_input), (mp_obj_t)&pyb_input_obj },

stmhal/usb.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,7 @@ void usb_vcp_send_strn_cooked(const char *str, int len) {
102102

103103
void usb_hid_send_report(uint8_t *buf) {
104104
#ifdef USE_DEVICE_MODE
105-
#if 0
106-
USBD_HID_SendReport(&USB_OTG_Core, buf, 4);
107-
#endif
105+
USBD_HID_SendReport(&hUSBDDevice, buf, 4);
108106
#endif
109107
}
110108

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#ifndef _USB_CDC_HID_CORE_H_
2+
#define _USB_CDC_HID_CORE_H_
3+
4+
#include "usbd_ioreq.h"
5+
6+
// CDC and HID packet sizes
7+
#define CDC_DATA_FS_MAX_PACKET_SIZE (64) // endpoint IN & OUT packet size
8+
9+
#if 0
10+
// CDC
11+
#define USB_CDC_HID_CONFIG_DESC_SIZ (75)
12+
#define USB_CDC_HID_NUM_INTERFACES (2)
13+
#define USB_CDC_HID_USE_CDC (1)
14+
#define USB_CDC_HID_USE_HID (0)
15+
#define CDC_IN_EP 0x81 /* EP1 for data IN */
16+
#define CDC_OUT_EP 0x01 /* EP1 for data OUT */
17+
#define CDC_CMD_EP 0x82 /* EP2 for CDC commands */
18+
#define HID_IN_EP (0x83)
19+
#define HID_OUT_EP (0x03)
20+
#elif 0
21+
// HID
22+
#define USB_CDC_HID_CONFIG_DESC_SIZ (32)
23+
#define USB_CDC_HID_NUM_INTERFACES (1)
24+
#define USB_CDC_HID_USE_CDC (0)
25+
#define USB_CDC_HID_USE_HID (1)
26+
#define CDC_IN_EP 0x81 /* EP1 for data IN */
27+
#define CDC_OUT_EP 0x01 /* EP1 for data OUT */
28+
#define CDC_CMD_EP 0x82 /* EP2 for CDC commands */
29+
#define HID_IFACE_NUM (0)
30+
#define HID_IN_EP (0x81)
31+
#define HID_OUT_EP (0x01)
32+
#else
33+
// HID + CDC
34+
#define USB_CDC_HID_CONFIG_DESC_SIZ (100)
35+
#define USB_CDC_HID_NUM_INTERFACES (3)
36+
#define USB_CDC_HID_USE_CDC (1)
37+
#define USB_CDC_HID_USE_HID (1)
38+
#define CDC_IFACE_NUM (1)
39+
#define CDC_IN_EP (0x83)
40+
#define CDC_OUT_EP (0x03)
41+
#define CDC_CMD_EP (0x82)
42+
#define HID_IFACE_NUM (0)
43+
#define HID_IN_EP (0x81)
44+
#endif
45+
46+
typedef struct {
47+
uint32_t bitrate;
48+
uint8_t format;
49+
uint8_t paritytype;
50+
uint8_t datatype;
51+
} USBD_CDC_LineCodingTypeDef;
52+
53+
typedef struct _USBD_CDC_Itf {
54+
int8_t (* Init) (void);
55+
int8_t (* DeInit) (void);
56+
int8_t (* Control) (uint8_t, uint8_t * , uint16_t);
57+
int8_t (* Receive) (uint8_t *, uint32_t *);
58+
} USBD_CDC_ItfTypeDef;
59+
60+
typedef struct {
61+
uint32_t data[CDC_DATA_FS_MAX_PACKET_SIZE/4]; /* Force 32bits alignment */
62+
uint8_t CmdOpCode;
63+
uint8_t CmdLength;
64+
uint8_t *RxBuffer;
65+
uint8_t *TxBuffer;
66+
uint32_t RxLength;
67+
uint32_t TxLength;
68+
69+
__IO uint32_t TxState;
70+
__IO uint32_t RxState;
71+
} USBD_CDC_HandleTypeDef;
72+
73+
extern USBD_ClassTypeDef USBD_CDC_HID;
74+
75+
uint8_t USBD_CDC_RegisterInterface(USBD_HandleTypeDef *pdev, USBD_CDC_ItfTypeDef *fops);
76+
uint8_t USBD_CDC_SetTxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff, uint16_t length);
77+
uint8_t USBD_CDC_SetRxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff);
78+
uint8_t USBD_CDC_ReceivePacket(USBD_HandleTypeDef *pdev);
79+
uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev);
80+
81+
uint8_t USBD_HID_SendReport(USBD_HandleTypeDef *pdev, uint8_t *report, uint16_t len);
82+
83+
#endif // _USB_CDC_HID_CORE_H_

0 commit comments

Comments
 (0)