Skip to content

Commit e86896f

Browse files
committed
pbio/drv/usb_simulation: Add virtual hub driver.
This will help prepare the common USB driver code, and test USB and BLE both enabled.
1 parent b3c9f81 commit e86896f

File tree

6 files changed

+91
-3
lines changed

6 files changed

+91
-3
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
"request": "launch",
6767
"program": "${workspaceFolder}/bricks/virtualhub/build-debug/firmware.elf",
6868
"args": [
69-
"${workspaceFolder}/tests/virtualhub/motor/car.py",
69+
"${workspaceFolder}/tests/virtualhub/basics/hello.py",
7070
],
7171
"stopAtEntry": false,
7272
"cwd": "${workspaceFolder}",

bricks/_common/sources.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,11 @@ PBIO_SRC_C = $(addprefix lib/pbio/,\
188188
drv/uart/uart_stm32f0.c \
189189
drv/uart/uart_stm32f4_ll_irq.c \
190190
drv/uart/uart_stm32l4_ll_dma.c \
191+
drv/usb/usb.c \
191192
drv/usb/usb_common_desc.c \
192193
drv/usb/usb_ev3.c \
193194
drv/usb/usb_nxt.c \
195+
drv/usb/usb_simulation.c \
194196
drv/usb/usb_stm32.c \
195197
drv/watchdog/watchdog_ev3.c \
196198
drv/watchdog/watchdog_stm32.c \

lib/pbio/drv/usb/usb.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// SPDX-License-Identifier: MIT
2+
// Copyright (c) 2025 The Pybricks Authors
3+
4+
#include <pbdrv/config.h>
5+
6+
#if PBDRV_CONFIG_USB_SIMULATION
7+
8+
#include <errno.h>
9+
#include <signal.h>
10+
#include <stdio.h>
11+
#include <stdlib.h>
12+
#include <unistd.h>
13+
14+
#include "usb.h"
15+
#include <pbdrv/usb.h>
16+
17+
#include <pbio/error.h>
18+
#include <pbio/os.h>
19+
20+
void pbdrv_usb_set_receive_handler(pbdrv_usb_receive_handler_t handler) {
21+
}
22+
23+
void pbdrv_usb_schedule_status_update(const uint8_t *status_msg) {
24+
}
25+
26+
pbio_error_t pbdrv_usb_stdout_tx(const uint8_t *data, uint32_t *size) {
27+
return PBIO_SUCCESS;
28+
}
29+
30+
uint32_t pbdrv_usb_stdout_tx_available(void) {
31+
return UINT32_MAX;
32+
}
33+
34+
bool pbdrv_usb_stdout_tx_is_idle(void) {
35+
return true;
36+
}
37+
38+
bool pbdrv_usb_connection_is_active(void) {
39+
return false;
40+
}
41+
42+
void pbdrv_usb_init(void) {
43+
pbdrv_usb_init_device();
44+
}
45+
46+
void pbdrv_usb_deinit(void) {
47+
}
48+
49+
#endif // PBDRV_CONFIG_USB_SIMULATION

lib/pbio/drv/usb/usb.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,27 @@
1515
*/
1616
void pbdrv_usb_init(void);
1717

18+
/**
19+
* Platform specific device initialization.
20+
*/
21+
void pbdrv_usb_init_device(void);
22+
1823
/**
1924
* De-initializes the USB driver for data transfers on soft-poweroff. Keeps charging if supported.
2025
*/
2126
void pbdrv_usb_deinit(void);
2227

2328
#else // PBDRV_CONFIG_USB
2429

25-
#define pbdrv_usb_init()
26-
#define pbdrv_usb_deinit()
30+
static inline void pbdrv_usb_init(void) {
31+
}
32+
33+
static inline void pbdrv_usb_deinit(void) {
34+
}
35+
36+
static inline void pbdrv_usb_init_device(void) {
37+
}
38+
2739

2840
#endif // PBDRV_CONFIG_USB
2941

lib/pbio/drv/usb/usb_simulation.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// SPDX-License-Identifier: MIT
2+
// Copyright (c) 2025 The Pybricks Authors
3+
4+
#include <pbdrv/config.h>
5+
6+
#if PBDRV_CONFIG_USB_SIMULATION
7+
8+
#include <pbdrv/usb.h>
9+
10+
#include <pbio/error.h>
11+
#include <pbio/os.h>
12+
13+
pbdrv_usb_bcd_t pbdrv_usb_get_bcd(void) {
14+
return PBDRV_USB_BCD_NONE;
15+
}
16+
17+
void pbdrv_usb_init_device(void) {
18+
}
19+
20+
#endif // PBDRV_CONFIG_USB_SIMULATION

lib/pbio/platform/virtual_hub/pbdrvconfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,8 @@
4040
#define PBDRV_CONFIG_HAS_PORT_E (1)
4141
#define PBDRV_CONFIG_HAS_PORT_F (1)
4242
#define PBDRV_CONFIG_HAS_PORT_VCC_CONTROL (1)
43+
44+
#define PBDRV_CONFIG_USB (0)
45+
#define PBDRV_CONFIG_USB_SIMULATION (0)
46+
#define PBDRV_CONFIG_USB_MFG_STR u"Pybricks"
47+
#define PBDRV_CONFIG_USB_PROD_STR u"Virtual Hub"

0 commit comments

Comments
 (0)