Skip to content

Commit 3c4db9f

Browse files
committed
stmhal: Add USB_VCP class/object, for direct USB VCP control.
Before, pyb.stdin/pyb.stdout allowed some kind of access to the USB VCP device, but it was basic access. This patch adds a proper USB_VCP class and object with much more control over the USB VCP device. Create an object with pyb.USB_VCP(), then use this object as if it were a UART object. It has send, recv, read, write, and other methods. send and recv allow a timeout to be specified. Addresses issue 774.
1 parent 5f27a7e commit 3c4db9f

File tree

7 files changed

+227
-29
lines changed

7 files changed

+227
-29
lines changed

stmhal/modpyb.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = {
367367
{ MP_OBJ_NEW_QSTR(MP_QSTR_have_cdc), (mp_obj_t)&pyb_have_cdc_obj },
368368
{ MP_OBJ_NEW_QSTR(MP_QSTR_repl_uart), (mp_obj_t)&pyb_repl_uart_obj },
369369
{ MP_OBJ_NEW_QSTR(MP_QSTR_hid), (mp_obj_t)&pyb_hid_send_report_obj },
370+
{ MP_OBJ_NEW_QSTR(MP_QSTR_USB_VCP), (mp_obj_t)&pyb_usb_vcp_type },
370371

371372
{ MP_OBJ_NEW_QSTR(MP_QSTR_millis), (mp_obj_t)&pyb_millis_obj },
372373
{ MP_OBJ_NEW_QSTR(MP_QSTR_delay), (mp_obj_t)&pyb_delay_obj },

stmhal/pybstdio.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,10 @@ int stdin_rx_chr(void) {
8484
}
8585
#endif
8686
#endif
87-
if (usb_vcp_rx_num() != 0) {
88-
return usb_vcp_rx_get();
87+
88+
byte c;
89+
if (usb_vcp_recv_byte(&c) != 0) {
90+
return c;
8991
} else if (pyb_stdio_uart != PYB_UART_NONE && uart_rx_any(pyb_stdio_uart)) {
9092
return uart_rx_char(pyb_stdio_uart);
9193
}

stmhal/qstrdefsport.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ Q(millis)
7171
Q(seek)
7272
Q(tell)
7373

74+
// for USB VCP class
75+
Q(USB_VCP)
76+
Q(send)
77+
Q(recv)
78+
Q(timeout)
79+
7480
// for RTC class
7581
Q(RTC)
7682
Q(info)

stmhal/usb.c

Lines changed: 148 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,13 @@
3232
#include "usbd_cdc_interface.h"
3333
#include "usbd_msc_storage.h"
3434

35-
#include "misc.h"
3635
#include "mpconfig.h"
36+
#include "misc.h"
3737
#include "qstr.h"
3838
#include "obj.h"
39+
#include "runtime.h"
40+
#include "stream.h"
41+
#include "bufhelper.h"
3942
#include "usb.h"
4043

4144
#ifdef USE_DEVICE_MODE
@@ -99,18 +102,14 @@ void usb_vcp_set_interrupt_char(int c) {
99102
}
100103
}
101104

102-
int usb_vcp_rx_num(void) {
103-
return USBD_CDC_RxNum();
104-
}
105-
106-
char usb_vcp_rx_get(void) {
107-
return USBD_CDC_RxGet();
105+
int usb_vcp_recv_byte(uint8_t *c) {
106+
return USBD_CDC_Rx(c, 1, 0);
108107
}
109108

110109
void usb_vcp_send_strn(const char *str, int len) {
111110
#ifdef USE_DEVICE_MODE
112111
if (dev_is_enabled) {
113-
USBD_CDC_Tx(str, len);
112+
USBD_CDC_TxAlways((const uint8_t*)str, len);
114113
}
115114
#endif
116115
}
@@ -120,9 +119,9 @@ void usb_vcp_send_strn_cooked(const char *str, int len) {
120119
if (dev_is_enabled) {
121120
for (const char *top = str + len; str < top; str++) {
122121
if (*str == '\n') {
123-
USBD_CDC_Tx("\r\n", 2);
122+
USBD_CDC_TxAlways((const uint8_t*)"\r\n", 2);
124123
} else {
125-
USBD_CDC_Tx(str, 1);
124+
USBD_CDC_TxAlways((const uint8_t*)str, 1);
126125
}
127126
}
128127
}
@@ -135,6 +134,145 @@ void usb_hid_send_report(uint8_t *buf) {
135134
#endif
136135
}
137136

137+
/******************************************************************************/
138+
// Micro Python bindings for USB VCP
139+
140+
typedef struct _pyb_usb_vcp_obj_t {
141+
mp_obj_base_t base;
142+
} pyb_usb_vcp_obj_t;
143+
144+
STATIC const pyb_usb_vcp_obj_t pyb_usb_vcp_obj = {{&pyb_usb_vcp_type}};
145+
146+
STATIC void pyb_usb_vcp_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
147+
print(env, "USB_VCP()");
148+
}
149+
150+
STATIC mp_obj_t pyb_usb_vcp_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
151+
// check arguments
152+
mp_arg_check_num(n_args, n_kw, 0, 0, false);
153+
154+
// return the USB VCP object
155+
return (mp_obj_t)&pyb_usb_vcp_obj;
156+
}
157+
158+
/// \method any()
159+
/// Return `True` if any characters waiting, else `False`.
160+
STATIC mp_obj_t pyb_usb_vcp_any(mp_obj_t self_in) {
161+
if (USBD_CDC_RxNum() > 0) {
162+
return mp_const_true;
163+
} else {
164+
return mp_const_false;
165+
}
166+
}
167+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_usb_vcp_any_obj, pyb_usb_vcp_any);
168+
169+
/// \method send(data, *, timeout=5000)
170+
/// Send data over the USB VCP:
171+
///
172+
/// - `data` is the data to send (an integer to send, or a buffer object).
173+
/// - `timeout` is the timeout in milliseconds to wait for the send.
174+
///
175+
/// Return value: number of bytes sent.
176+
STATIC const mp_arg_t pyb_usb_vcp_send_args[] = {
177+
{ MP_QSTR_data, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
178+
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 5000} },
179+
};
180+
#define PYB_USB_VCP_SEND_NUM_ARGS MP_ARRAY_SIZE(pyb_usb_vcp_send_args)
181+
182+
STATIC mp_obj_t pyb_usb_vcp_send(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) {
183+
// parse args
184+
mp_arg_val_t vals[PYB_USB_VCP_SEND_NUM_ARGS];
185+
mp_arg_parse_all(n_args - 1, args + 1, kw_args, PYB_USB_VCP_SEND_NUM_ARGS, pyb_usb_vcp_send_args, vals);
186+
187+
// get the buffer to send from
188+
mp_buffer_info_t bufinfo;
189+
uint8_t data[1];
190+
pyb_buf_get_for_send(vals[0].u_obj, &bufinfo, data);
191+
192+
// send the data
193+
int ret = USBD_CDC_Tx(bufinfo.buf, bufinfo.len, vals[1].u_int);
194+
195+
return mp_obj_new_int(ret);
196+
}
197+
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_usb_vcp_send_obj, 1, pyb_usb_vcp_send);
198+
199+
/// \method recv(data, *, timeout=5000)
200+
///
201+
/// Receive data on the bus:
202+
///
203+
/// - `data` can be an integer, which is the number of bytes to receive,
204+
/// or a mutable buffer, which will be filled with received bytes.
205+
/// - `timeout` is the timeout in milliseconds to wait for the receive.
206+
///
207+
/// Return value: if `data` is an integer then a new buffer of the bytes received,
208+
/// otherwise the number of bytes read into `data` is returned.
209+
STATIC mp_obj_t pyb_usb_vcp_recv(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) {
210+
// parse args
211+
mp_arg_val_t vals[PYB_USB_VCP_SEND_NUM_ARGS];
212+
mp_arg_parse_all(n_args - 1, args + 1, kw_args, PYB_USB_VCP_SEND_NUM_ARGS, pyb_usb_vcp_send_args, vals);
213+
214+
// get the buffer to receive into
215+
mp_buffer_info_t bufinfo;
216+
mp_obj_t o_ret = pyb_buf_get_for_recv(vals[0].u_obj, &bufinfo);
217+
218+
// receive the data
219+
int ret = USBD_CDC_Rx(bufinfo.buf, bufinfo.len, vals[1].u_int);
220+
221+
// return the received data
222+
if (o_ret == MP_OBJ_NULL) {
223+
return mp_obj_new_int(ret); // number of bytes read into given buffer
224+
} else {
225+
return mp_obj_str_builder_end_with_len(o_ret, ret); // create a new buffer
226+
}
227+
}
228+
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_usb_vcp_recv_obj, 1, pyb_usb_vcp_recv);
229+
230+
STATIC mp_uint_t pyb_usb_vcp_read(mp_obj_t self_in, void *buf, mp_uint_t size, int *errcode) {
231+
int ret = USBD_CDC_Rx((byte*)buf, size, -1);
232+
return ret;
233+
}
234+
235+
STATIC mp_uint_t pyb_usb_vcp_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) {
236+
int ret = USBD_CDC_Tx((const byte*)buf, size, -1);
237+
return ret;
238+
}
239+
240+
mp_obj_t pyb_usb_vcp___exit__(uint n_args, const mp_obj_t *args) {
241+
return mp_const_none;
242+
}
243+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_usb_vcp___exit___obj, 4, 4, pyb_usb_vcp___exit__);
244+
245+
STATIC const mp_map_elem_t pyb_usb_vcp_locals_dict_table[] = {
246+
{ MP_OBJ_NEW_QSTR(MP_QSTR_send), (mp_obj_t)&pyb_usb_vcp_send_obj },
247+
{ MP_OBJ_NEW_QSTR(MP_QSTR_recv), (mp_obj_t)&pyb_usb_vcp_recv_obj },
248+
{ MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&mp_stream_read_obj },
249+
{ MP_OBJ_NEW_QSTR(MP_QSTR_readall), (mp_obj_t)&mp_stream_readall_obj },
250+
{ MP_OBJ_NEW_QSTR(MP_QSTR_readline), (mp_obj_t)&mp_stream_unbuffered_readline_obj},
251+
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj },
252+
{ MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&mp_identity_obj },
253+
{ MP_OBJ_NEW_QSTR(MP_QSTR___del__), (mp_obj_t)&mp_identity_obj },
254+
{ MP_OBJ_NEW_QSTR(MP_QSTR___enter__), (mp_obj_t)&mp_identity_obj },
255+
{ MP_OBJ_NEW_QSTR(MP_QSTR___exit__), (mp_obj_t)&pyb_usb_vcp___exit___obj },
256+
};
257+
258+
STATIC MP_DEFINE_CONST_DICT(pyb_usb_vcp_locals_dict, pyb_usb_vcp_locals_dict_table);
259+
260+
STATIC const mp_stream_p_t pyb_usb_vcp_stream_p = {
261+
.read = pyb_usb_vcp_read,
262+
.write = pyb_usb_vcp_write,
263+
};
264+
265+
const mp_obj_type_t pyb_usb_vcp_type = {
266+
{ &mp_type_type },
267+
.name = MP_QSTR_USB_VCP,
268+
.print = pyb_usb_vcp_print,
269+
.make_new = pyb_usb_vcp_make_new,
270+
.getiter = mp_identity,
271+
.iternext = mp_stream_unbuffered_iter,
272+
.stream_p = &pyb_usb_vcp_stream_p,
273+
.locals_dict = (mp_obj_t)&pyb_usb_vcp_locals_dict,
274+
};
275+
138276
/******************************************************************************/
139277
// code for experimental USB OTG support
140278

stmhal/usb.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,14 @@ typedef enum {
4141
USB_STORAGE_MEDIUM_SDCARD,
4242
} usb_storage_medium_t;
4343

44+
const mp_obj_type_t pyb_usb_vcp_type;
45+
4446
void pyb_usb_dev_init(usb_device_mode_t mode, usb_storage_medium_t medium);
4547
void pyb_usb_dev_stop(void);
4648
bool usb_vcp_is_enabled(void);
4749
bool usb_vcp_is_connected(void);
4850
void usb_vcp_set_interrupt_char(int c);
49-
int usb_vcp_rx_num(void);
50-
char usb_vcp_rx_get(void);
51+
int usb_vcp_recv_byte(uint8_t *c);
5152
void usb_vcp_send_strn(const char* str, int len);
5253
void usb_vcp_send_strn_cooked(const char *str, int len);
5354
void usb_hid_send_report(uint8_t *buf); // 4 bytes for mouse: ?, x, y, ?

stmhal/usbd_cdc_interface.c

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,18 @@
3333
*/
3434

3535
/* Includes ------------------------------------------------------------------*/
36+
3637
#include <stdbool.h>
38+
3739
#include "stm32f4xx_hal.h"
3840
#include "usbd_cdc_msc_hid.h"
3941
#include "usbd_cdc_interface.h"
4042
#include "pendsv.h"
43+
44+
#include "mpconfig.h"
45+
#include "misc.h"
46+
#include "qstr.h"
47+
#include "obj.h"
4148
#include "usb.h"
4249

4350
// CDC control commands
@@ -59,7 +66,7 @@
5966
/* Private macro -------------------------------------------------------------*/
6067
/* Private variables ---------------------------------------------------------*/
6168

62-
static uint8_t dev_is_connected = 0; // indicates if we are connected
69+
static __IO uint8_t dev_is_connected = 0; // indicates if we are connected
6370

6471
static uint8_t UserRxBuffer[APP_RX_DATA_SIZE]; // received data from USB OUT endpoint is stored in this buffer
6572
static uint16_t UserRxBufCur = 0; // points to next available character in UserRxBuffer
@@ -399,7 +406,34 @@ void USBD_CDC_SetInterrupt(int chr, void *data) {
399406
user_interrupt_data = data;
400407
}
401408

402-
void USBD_CDC_Tx(const char *str, uint32_t len) {
409+
// timout in milliseconds.
410+
// Returns number of bytes written to the device.
411+
int USBD_CDC_Tx(const uint8_t *buf, uint32_t len, uint32_t timeout) {
412+
for (uint32_t i = 0; i < len; i++) {
413+
// Wait until the device is connected and the buffer has space, with a given timeout
414+
uint32_t start = HAL_GetTick();
415+
while (!dev_is_connected || ((UserTxBufPtrIn + 1) & (APP_TX_DATA_SIZE - 1)) == UserTxBufPtrOut) {
416+
// Wraparound of tick is taken care of by 2's complement arithmetic.
417+
if (HAL_GetTick() - start >= timeout) {
418+
// timeout
419+
return i;
420+
}
421+
__WFI(); // enter sleep mode, waiting for interrupt
422+
}
423+
424+
// Write data to device buffer
425+
UserTxBuffer[UserTxBufPtrIn] = buf[i];
426+
UserTxBufPtrIn = (UserTxBufPtrIn + 1) & (APP_TX_DATA_SIZE - 1);
427+
}
428+
429+
// Success, return number of bytes read
430+
return len;
431+
}
432+
433+
// Always write all of the data to the device tx buffer, even if the
434+
// device is not connected, or if the buffer is full. Has a small timeout
435+
// to wait for the buffer to be drained, in the case the device is connected.
436+
void USBD_CDC_TxAlways(const uint8_t *buf, uint32_t len) {
403437
for (int i = 0; i < len; i++) {
404438
// If the CDC device is not connected to the host then we don't have anyone to receive our data.
405439
// The device may become connected in the future, so we should at least try to fill the buffer
@@ -433,23 +467,36 @@ void USBD_CDC_Tx(const char *str, uint32_t len) {
433467
*/
434468
}
435469

436-
UserTxBuffer[UserTxBufPtrIn] = str[i];
470+
UserTxBuffer[UserTxBufPtrIn] = buf[i];
437471
UserTxBufPtrIn = (UserTxBufPtrIn + 1) & (APP_TX_DATA_SIZE - 1);
438472
}
439473
}
440474

475+
// Returns number of bytes in the rx buffer.
441476
int USBD_CDC_RxNum(void) {
442477
return UserRxBufLen - UserRxBufCur;
443478
}
444479

445-
int USBD_CDC_RxGet(void) {
446-
// wait for buffer to have at least 1 character in it
447-
while (USBD_CDC_RxNum() == 0) {
448-
__WFI();
449-
}
480+
// timout in milliseconds.
481+
// Returns number of bytes read from the device.
482+
int USBD_CDC_Rx(uint8_t *buf, uint32_t len, uint32_t timeout) {
483+
// loop to read bytes
484+
for (uint32_t i = 0; i < len; i++) {
485+
// Wait until we have at least 1 byte to read
486+
uint32_t start = HAL_GetTick();
487+
while (!dev_is_connected || UserRxBufLen == UserRxBufCur) {
488+
// Wraparound of tick is taken care of by 2's complement arithmetic.
489+
if (HAL_GetTick() - start >= timeout) {
490+
// timeout
491+
return i;
492+
}
493+
__WFI(); // enter sleep mode, waiting for interrupt
494+
}
450495

451-
// get next character
452-
int c = UserRxBuffer[UserRxBufCur++];
496+
// Copy byte from device to user buffer
497+
buf[i] = UserRxBuffer[UserRxBufCur++];
498+
}
453499

454-
return c;
500+
// Success, return number of bytes read
501+
return len;
455502
}

stmhal/usbd_cdc_interface.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
*
2121
* http://www.st.com/software_license_agreement_liberty_v2
2222
*
23-
* Unless required by applicable law or agreed to in writing, software
24-
* distributed under the License is distributed on an "AS IS" BASIS,
23+
* Unless required by applicable law or agreed to in writing, software
24+
* distributed under the License is distributed on an "AS IS" BASIS,
2525
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2626
* See the License for the specific language governing permissions and
2727
* limitations under the License.
@@ -36,6 +36,9 @@ void USBD_CDC_HAL_TIM_PeriodElapsedCallback(void);
3636

3737
int USBD_CDC_IsConnected(void);
3838
void USBD_CDC_SetInterrupt(int chr, void *data);
39-
void USBD_CDC_Tx(const char *str, uint32_t len);
39+
40+
int USBD_CDC_Tx(const uint8_t *buf, uint32_t len, uint32_t timeout);
41+
void USBD_CDC_TxAlways(const uint8_t *buf, uint32_t len);
42+
4043
int USBD_CDC_RxNum(void);
41-
int USBD_CDC_RxGet(void);
44+
int USBD_CDC_Rx(uint8_t *buf, uint32_t len, uint32_t timeout);

0 commit comments

Comments
 (0)