Skip to content

Commit 9aef65b

Browse files
committed
pbio/drv/usb: Add common USB driver.
This de-duplicates common USB code such as handling stdout or sending the hub status. This was quite hard to keep track of between the EV3 and STM32 drivers, causing subtle differences and issues which were not so easy to synchronize. The simulation driver shows the bare minimum driver-specific functions that each driver must implement, which will we do in the next commits.
1 parent 5eb00ae commit 9aef65b

File tree

3 files changed

+370
-4
lines changed

3 files changed

+370
-4
lines changed

lib/pbio/drv/usb/usb.c

Lines changed: 179 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,208 @@
1717
#include <pbio/error.h>
1818
#include <pbio/os.h>
1919

20+
#include <lwrb/lwrb.h>
21+
22+
/**
23+
* Host is subscribed to our outgoing event messages.
24+
*/
25+
static bool pbdrv_usb_events_subscribed;
26+
27+
bool pbdrv_usb_connection_is_active(void) {
28+
return pbdrv_usb_events_subscribed;
29+
}
30+
31+
/**
32+
* Pybricks system command handler.
33+
*/
34+
static pbdrv_usb_receive_handler_t pbdrv_usb_receive_handler;
35+
2036
void pbdrv_usb_set_receive_handler(pbdrv_usb_receive_handler_t handler) {
37+
pbdrv_usb_receive_handler = handler;
2138
}
2239

40+
/**
41+
* Buffer scheduled status.
42+
*/
43+
static uint8_t status_data[PBIO_PYBRICKS_EVENT_STATUS_REPORT_SIZE];
44+
static bool status_data_pending;
45+
2346
void pbdrv_usb_schedule_status_update(const uint8_t *status_msg) {
47+
// Ignore if message identical to last.
48+
if (!memcmp(status_data, status_msg, sizeof(status_data))) {
49+
return;
50+
}
51+
52+
// Schedule to send whenever the Bluetooth process gets round to it.
53+
memcpy(status_data, status_msg, sizeof(status_data));
54+
status_data_pending = true;
55+
pbio_os_request_poll();
2456
}
2557

58+
/**
59+
* Buffer for scheduled stdout.
60+
*/
61+
static lwrb_t stdout_ring_buf;
62+
2663
pbio_error_t pbdrv_usb_stdout_tx(const uint8_t *data, uint32_t *size) {
64+
65+
if (!pbdrv_usb_connection_is_active()) {
66+
return PBIO_ERROR_INVALID_OP;
67+
}
68+
69+
// Buffer data to send it more efficiently even if the caller is only
70+
// writing one byte at a time.
71+
if ((*size = lwrb_write(&stdout_ring_buf, data, *size)) == 0) {
72+
return PBIO_ERROR_AGAIN;
73+
}
74+
75+
// Poke the process to start tx soon-ish. This way, we can accumulate
76+
// data bytes before actually transmitting.
77+
pbio_os_request_poll();
78+
2779
return PBIO_SUCCESS;
2880
}
2981

3082
uint32_t pbdrv_usb_stdout_tx_available(void) {
31-
return UINT32_MAX;
83+
if (!pbdrv_usb_connection_is_active()) {
84+
return UINT32_MAX;
85+
}
86+
return lwrb_get_free(&stdout_ring_buf);
3287
}
3388

3489
bool pbdrv_usb_stdout_tx_is_idle(void) {
35-
return true;
90+
if (!pbdrv_usb_connection_is_active()) {
91+
return true;
92+
}
93+
return lwrb_get_full(&stdout_ring_buf) == 0;
3694
}
3795

38-
bool pbdrv_usb_connection_is_active(void) {
39-
return false;
96+
static bool respond_soon;
97+
static pbio_pybricks_error_t respond_result;
98+
99+
/**
100+
* Non-blocking poll handler to process pending incoming messages.
101+
*/
102+
static void pbdrv_usb_handle_data_in(void) {
103+
104+
// Ignore incoming data if we haven't sent our previous response yet.
105+
if (respond_soon) {
106+
return;
107+
}
108+
109+
static uint8_t data_in[PBDRV_USB_PYBRICKS_MAX_PACKET_SIZE];
110+
uint32_t size = pbdrv_usb_get_data_in(data_in);
111+
112+
// Expecting at least EP_MSG and payload.
113+
if (size < 2) {
114+
return;
115+
}
116+
117+
switch (data_in[0]) {
118+
case PBIO_PYBRICKS_OUT_EP_MSG_SUBSCRIBE:
119+
pbdrv_usb_events_subscribed = data_in[1];
120+
respond_result = PBIO_PYBRICKS_ERROR_OK;
121+
respond_soon = true;
122+
123+
// Schedule sending current status immediately after subscribing.
124+
status_data_pending = true;
125+
break;
126+
case PBIO_PYBRICKS_OUT_EP_MSG_COMMAND:
127+
if (pbdrv_usb_receive_handler) {
128+
respond_result = pbdrv_usb_receive_handler(data_in + 1, size - 1);
129+
respond_soon = true;
130+
}
131+
break;
132+
}
133+
}
134+
135+
static void pbdrv_usb_reset_state(void) {
136+
pbdrv_usb_events_subscribed = false;
137+
respond_soon = false;
138+
status_data_pending = false;
139+
lwrb_reset(&stdout_ring_buf);
140+
}
141+
142+
static pbio_os_process_t pbdrv_usb_process;
143+
144+
static pbio_error_t pbdrv_usb_process_thread(pbio_os_state_t *state, void *context) {
145+
146+
static pbio_os_state_t sub;
147+
148+
static uint8_t out_data[PBDRV_USB_PYBRICKS_MAX_PACKET_SIZE];
149+
static uint32_t out_size;
150+
151+
pbio_error_t err;
152+
153+
// Runs every time. If there is no connection, there just won't be data.
154+
pbdrv_usb_handle_data_in();
155+
156+
PBIO_OS_ASYNC_BEGIN(state);
157+
158+
for (;;) {
159+
160+
// Run charger detection: wait for USB to become physically plugged in.
161+
PBIO_OS_AWAIT(state, &sub, err = pbdrv_usb_wait_for_charger(&sub));
162+
163+
while (pbdrv_usb_process.request != PBIO_OS_PROCESS_REQUEST_TYPE_CANCEL && pbdrv_usb_is_ready()) {
164+
165+
// Find out what we should send, if anything, priotizing response, then
166+
// status, then stdout, then other events.
167+
if (respond_soon) {
168+
// Pack the response to the most recent message.
169+
out_data[0] = PBIO_PYBRICKS_IN_EP_MSG_RESPONSE;
170+
pbio_set_uint32_le(&out_data[1], respond_result);
171+
out_size = sizeof(uint32_t) + 1;
172+
respond_soon = false;
173+
} else if (pbdrv_usb_connection_is_active() && status_data_pending) {
174+
// Send out status if pending (already includes event code).
175+
out_data[0] = PBIO_PYBRICKS_IN_EP_MSG_EVENT;
176+
memcpy(&out_data[1], status_data, PBIO_PYBRICKS_EVENT_STATUS_REPORT_SIZE);
177+
out_size = PBIO_PYBRICKS_USB_MESSAGE_SIZE(PBIO_PYBRICKS_EVENT_STATUS_REPORT_SIZE);
178+
status_data_pending = false;
179+
} else if (pbdrv_usb_connection_is_active() && lwrb_get_full(&stdout_ring_buf) != 0) {
180+
// Send out stdout if anything is buffered.
181+
out_data[0] = PBIO_PYBRICKS_IN_EP_MSG_EVENT;
182+
out_data[1] = PBIO_PYBRICKS_EVENT_WRITE_STDOUT;
183+
out_size = lwrb_read(&stdout_ring_buf, &out_data[2], PBDRV_USB_PYBRICKS_MAX_PACKET_SIZE - 2) + 2;
184+
}
185+
186+
// If there was anything to send, send it.
187+
if (out_size) {
188+
PBIO_OS_AWAIT(state, &sub, err = pbdrv_usb_tx(&sub, out_data, out_size));
189+
out_size = 0;
190+
if (err != PBIO_SUCCESS) {
191+
pbdrv_usb_reset_state();
192+
PBIO_OS_AWAIT(state, &sub, pbdrv_usb_tx_reset(&sub));
193+
}
194+
} else {
195+
// Otherwise yield once before going and check again.
196+
PBIO_OS_AWAIT_ONCE(state);
197+
}
198+
}
199+
200+
PBIO_OS_AWAIT_WHILE(state, pbdrv_usb_is_ready());
201+
pbdrv_usb_reset_state();
202+
pbdrv_usb_tx_reset(&sub);
203+
}
204+
205+
// Unreachable. On cancellation, the charger detection step in the above
206+
// loop keeps running. It will just skip the data handler.
207+
PBIO_OS_ASYNC_END(PBIO_ERROR_FAILED);
40208
}
41209

42210
void pbdrv_usb_init(void) {
43211
pbdrv_usb_init_device();
212+
213+
static uint8_t stdout_buf[PBDRV_USB_PYBRICKS_MAX_PACKET_SIZE * 2];
214+
lwrb_init(&stdout_ring_buf, stdout_buf, PBIO_ARRAY_SIZE(stdout_buf));
215+
216+
pbio_os_process_start(&pbdrv_usb_process, pbdrv_usb_process_thread, NULL);
44217
}
45218

46219
void pbdrv_usb_deinit(void) {
220+
pbdrv_usb_deinit_device();
221+
pbio_os_process_make_request(&pbdrv_usb_process, PBIO_OS_PROCESS_REQUEST_TYPE_CANCEL);
47222
}
48223

49224
#endif // PBDRV_CONFIG_USB_SIMULATION

lib/pbio/drv/usb/usb.h

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@
1010

1111
#if PBDRV_CONFIG_USB
1212

13+
#include <pbio/error.h>
14+
#include <pbio/os.h>
15+
16+
#include <stdint.h>
17+
18+
// revisit, this is also defined elsewhere
19+
#define PBDRV_USB_PYBRICKS_MAX_PACKET_SIZE (64u)
20+
#define PBDRV_USB_TRANSMIT_TIMEOUT (50)
21+
1322
/**
1423
* Initializes the USB driver on boot.
1524
*/
@@ -25,6 +34,64 @@ void pbdrv_usb_init_device(void);
2534
*/
2635
void pbdrv_usb_deinit(void);
2736

37+
/**
38+
* Platform specific device deinitialization.
39+
*/
40+
void pbdrv_usb_deinit_device(void);
41+
42+
/**
43+
* Gets most recent incoming message and copies it to provided buffer.
44+
*
45+
* The message is then cleared and the driver prepares to read a new message.
46+
*
47+
* @param [in] data Buffer to copy the message to.
48+
* @return Number of bytes copied. Zero means nothing was available.
49+
*/
50+
uint32_t pbdrv_usb_get_data_in(uint8_t *data);
51+
52+
/**
53+
* Sends and awaits message from hub to host via the Pybricks USB interface OUT endpoint.
54+
*
55+
* Driver-specific implementation. Must return within ::PBDRV_USB_TRANSMIT_TIMEOUT.
56+
*
57+
* The USB process ensures that only one call is made at once.
58+
*
59+
* @param [in] state Protothread state.
60+
* @param [in] data Data to send.
61+
* @param [in] size Data size.
62+
* @return ::PBIO_SUCCESS on completion.
63+
* ::PBIO_ERROR_INVALID_OP if there is no connection.
64+
* ::PBIO_ERROR_AGAIN while awaiting.
65+
* ::PBIO_ERROR_BUSY if this operation is already ongoing.
66+
* ::PBIO_ERROR_INVALID_ARG if @p size is too large.
67+
* ::PBIO_ERROR_TIMEDOUT if the operation was started but could not complete.
68+
*/
69+
pbio_error_t pbdrv_usb_tx(pbio_os_state_t *state, const uint8_t *data, uint32_t size);
70+
71+
/**
72+
* Resets the driver transmission state.
73+
*
74+
* @param [in] state Protothread state.
75+
* @return ::PBIO_SUCCESS on completion.
76+
* ::PBIO_ERROR_AGAIN while awaiting.
77+
*/
78+
pbio_error_t pbdrv_usb_tx_reset(pbio_os_state_t *state);
79+
80+
/**
81+
* Waits for USB to be plugged in and detects what charger type is connected.
82+
*
83+
* @param [in] state Protothread state.
84+
* @return ::PBIO_SUCCESS on completion.
85+
* ::PBIO_ERROR_AGAIN while awaiting.
86+
* ::PBIO_ERROR_NOT_SUPPORTED if platform has no charger.
87+
*/
88+
pbio_error_t pbdrv_usb_wait_for_charger(pbio_os_state_t *state);
89+
90+
/**
91+
* Tests if USB is ready for communication.
92+
*/
93+
bool pbdrv_usb_is_ready(void);
94+
2895
#else // PBDRV_CONFIG_USB
2996

3097
static inline void pbdrv_usb_init(void) {
@@ -36,6 +103,29 @@ static inline void pbdrv_usb_deinit(void) {
36103
static inline void pbdrv_usb_init_device(void) {
37104
}
38105

106+
static inline void pbdrv_usb_deinit_device(void) {
107+
}
108+
109+
static inline pbio_error_t pbdrv_usb_tx(pbio_os_state_t *state, const uint8_t *data, size_t size) {
110+
return PBIO_ERROR_NOT_IMPLEMENTED;
111+
}
112+
113+
static inline uint32_t pbdrv_usb_get_data_in(uint8_t *data) {
114+
return 0;
115+
}
116+
117+
static inline pbio_error_t pbdrv_usb_tx_reset(pbio_os_state_t *state) {
118+
return PBIO_ERROR_NOT_IMPLEMENTED;
119+
}
120+
121+
static inline pbio_error_t pbdrv_usb_wait_for_charger(pbio_os_state_t *state) {
122+
return PBIO_ERROR_NOT_IMPLEMENTED;
123+
}
124+
125+
static inline bool pbdrv_usb_is_ready(void) {
126+
return false;
127+
}
128+
39129

40130
#endif // PBDRV_CONFIG_USB
41131

0 commit comments

Comments
 (0)