Skip to content

Commit 474626e

Browse files
committed
pbio/drv/usb: Use out buffers from drivers.
The platform specific drivers have specific requirements and cleanups for the outgoing data, and they have different buffers for each. So get those buffers instead of using a local one.
1 parent b15a938 commit 474626e

File tree

5 files changed

+41
-16
lines changed

5 files changed

+41
-16
lines changed

lib/pbio/drv/usb/usb.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212
#include <unistd.h>
1313

1414
#include "usb.h"
15+
16+
#include <pbdrv/config.h>
1517
#include <pbdrv/usb.h>
1618

1719
#include <pbio/error.h>
1820
#include <pbio/os.h>
21+
#include <pbio/protocol.h>
1922

2023
#include <lwrb/lwrb.h>
2124

@@ -25,7 +28,7 @@
2528
static bool pbdrv_usb_events_subscribed;
2629

2730
bool pbdrv_usb_connection_is_active(void) {
28-
return pbdrv_usb_events_subscribed;
31+
return pbdrv_usb_events_subscribed && pbdrv_usb_is_ready();
2932
}
3033

3134
/**
@@ -106,7 +109,7 @@ static void pbdrv_usb_handle_data_in(void) {
106109
return;
107110
}
108111

109-
static uint8_t data_in[PBDRV_USB_PYBRICKS_MAX_PACKET_SIZE];
112+
static uint8_t data_in[PBDRV_CONFIG_USB_MAX_PACKET_SIZE];
110113
uint32_t size = pbdrv_usb_get_data_in(data_in);
111114

112115
// Expecting at least EP_MSG and payload.
@@ -145,7 +148,7 @@ static pbio_error_t pbdrv_usb_process_thread(pbio_os_state_t *state, void *conte
145148

146149
static pbio_os_state_t sub;
147150

148-
static uint8_t out_data[PBDRV_USB_PYBRICKS_MAX_PACKET_SIZE];
151+
static uint8_t *out_data;
149152
static uint32_t out_size;
150153

151154
pbio_error_t err;
@@ -166,21 +169,21 @@ static pbio_error_t pbdrv_usb_process_thread(pbio_os_state_t *state, void *conte
166169
// status, then stdout, then other events.
167170
if (respond_soon) {
168171
// Pack the response to the most recent message.
169-
out_data[0] = PBIO_PYBRICKS_IN_EP_MSG_RESPONSE;
172+
pbdrv_usb_tx_get_buf(PBIO_PYBRICKS_IN_EP_MSG_RESPONSE, &out_data);
170173
pbio_set_uint32_le(&out_data[1], respond_result);
171174
out_size = sizeof(uint32_t) + 1;
172175
respond_soon = false;
173176
} else if (pbdrv_usb_connection_is_active() && status_data_pending) {
174177
// Send out status if pending (already includes event code).
175-
out_data[0] = PBIO_PYBRICKS_IN_EP_MSG_EVENT;
178+
pbdrv_usb_tx_get_buf(PBIO_PYBRICKS_IN_EP_MSG_EVENT, &out_data);
176179
memcpy(&out_data[1], status_data, PBIO_PYBRICKS_EVENT_STATUS_REPORT_SIZE);
177180
out_size = PBIO_PYBRICKS_USB_MESSAGE_SIZE(PBIO_PYBRICKS_EVENT_STATUS_REPORT_SIZE);
178181
status_data_pending = false;
179182
} else if (pbdrv_usb_connection_is_active() && lwrb_get_full(&stdout_ring_buf) != 0) {
180183
// Send out stdout if anything is buffered.
181-
out_data[0] = PBIO_PYBRICKS_IN_EP_MSG_EVENT;
184+
uint32_t max_size = pbdrv_usb_tx_get_buf(PBIO_PYBRICKS_IN_EP_MSG_EVENT, &out_data);
182185
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;
186+
out_size = lwrb_read(&stdout_ring_buf, &out_data[2], max_size - 2) + 2;
184187
}
185188

186189
// If there was anything to send, send it.
@@ -210,7 +213,7 @@ static pbio_error_t pbdrv_usb_process_thread(pbio_os_state_t *state, void *conte
210213
void pbdrv_usb_init(void) {
211214
pbdrv_usb_init_device();
212215

213-
static uint8_t stdout_buf[PBDRV_USB_PYBRICKS_MAX_PACKET_SIZE * 2];
216+
static uint8_t stdout_buf[PBDRV_CONFIG_USB_MAX_PACKET_SIZE * 2];
214217
lwrb_init(&stdout_ring_buf, stdout_buf, PBIO_ARRAY_SIZE(stdout_buf));
215218

216219
pbio_os_process_start(&pbdrv_usb_process, pbdrv_usb_process_thread, NULL);

lib/pbio/drv/usb/usb.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@
1212

1313
#include <pbio/error.h>
1414
#include <pbio/os.h>
15+
#include <pbio/protocol.h>
1516

1617
#include <stdint.h>
1718

18-
// revisit, this is also defined elsewhere
19-
#define PBDRV_USB_PYBRICKS_MAX_PACKET_SIZE (64u)
2019
#define PBDRV_USB_TRANSMIT_TIMEOUT (50)
2120

2221
/**
@@ -49,6 +48,15 @@ void pbdrv_usb_deinit_device(void);
4948
*/
5049
uint32_t pbdrv_usb_get_data_in(uint8_t *data);
5150

51+
/**
52+
* Gets the buffer to match the given endpoint mesage type.
53+
*
54+
* @param [in] message_type The message type to find the buffer for
55+
* @param [out] buf The corresponding buffer to write data to before sending.
56+
* @return Maximum size to write.
57+
*/
58+
uint32_t pbdrv_usb_tx_get_buf(pbio_pybricks_usb_in_ep_msg_t message_type, uint8_t **buf);
59+
5260
/**
5361
* Sends and awaits message from hub to host via the Pybricks USB interface OUT endpoint.
5462
*
@@ -126,6 +134,10 @@ static inline bool pbdrv_usb_is_ready(void) {
126134
return false;
127135
}
128136

137+
static inline uint32_t pbdrv_usb_tx_get_buf(pbio_pybricks_usb_in_ep_msg_t message_type, uint8_t **buf) {
138+
return 0;
139+
}
140+
129141

130142
#endif // PBDRV_CONFIG_USB
131143

lib/pbio/drv/usb/usb_simulation.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ pbdrv_usb_bcd_t pbdrv_usb_get_bcd(void) {
2929
return PBDRV_USB_BCD_NONE;
3030
}
3131

32+
static uint8_t pbdrv_usb_tx_buf[PBDRV_CONFIG_USB_MAX_PACKET_SIZE];
33+
34+
uint32_t pbdrv_usb_tx_get_buf(pbio_pybricks_usb_in_ep_msg_t message_type, uint8_t **buf) {
35+
// We can just use the same buffer for any transfer in this simulation.
36+
pbdrv_usb_tx_buf[0] = message_type;
37+
*buf = pbdrv_usb_tx_buf;
38+
return sizeof(pbdrv_usb_tx_buf);
39+
}
40+
3241
pbio_error_t pbdrv_usb_tx(pbio_os_state_t *state, const uint8_t *data, uint32_t size) {
3342

3443
static pbio_os_timer_t timer;
@@ -53,13 +62,13 @@ pbio_error_t pbdrv_usb_tx_reset(pbio_os_state_t *state) {
5362
return PBIO_SUCCESS;
5463
}
5564

56-
static uint8_t usb_in_buf[PBDRV_USB_PYBRICKS_MAX_PACKET_SIZE];
65+
static uint8_t usb_in_buf[PBDRV_CONFIG_USB_MAX_PACKET_SIZE];
5766
static uint32_t usb_in_size;
5867

5968
uint32_t pbdrv_usb_get_data_in(uint8_t *data) {
6069

6170
// Invalid size.
62-
if (usb_in_size > PBDRV_USB_PYBRICKS_MAX_PACKET_SIZE) {
71+
if (usb_in_size > PBDRV_CONFIG_USB_MAX_PACKET_SIZE) {
6372
usb_in_size = 0;
6473
}
6574

lib/pbio/include/pbio/protocol.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -457,23 +457,23 @@ enum {
457457
// 0 is skipped to avoid a zeroed buffer from being misinterpreted as a message.
458458

459459
/** Hub to host messages via the Pybricks interface IN endpoint. */
460-
enum {
460+
typedef enum {
461461
/**
462462
* Analog of BLE status response. Emitted in response to every OUT message
463463
* received.
464464
*/
465465
PBIO_PYBRICKS_IN_EP_MSG_RESPONSE = 1,
466466
/**Analog to BLE notification. Only emitted if subscribed. */
467467
PBIO_PYBRICKS_IN_EP_MSG_EVENT = 2,
468-
};
468+
} pbio_pybricks_usb_in_ep_msg_t;
469469

470470
/** Host to hub messages via the Pybricks USB interface OUT endpoint. */
471-
enum {
471+
typedef enum {
472472
/** Analog of BLE Client Characteristic Configuration Descriptor (CCCD). */
473473
PBIO_PYBRICKS_OUT_EP_MSG_SUBSCRIBE = 1,
474474
/** Analog of BLE Client Characteristic Write with response. */
475475
PBIO_PYBRICKS_OUT_EP_MSG_COMMAND = 2,
476-
};
476+
} pbio_pybricks_usb_out_ep_msg_t;
477477

478478
/**
479479
* Size of USB messages for Pybricks USB interface.

lib/pbio/platform/virtual_hub/pbdrvconfig.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#define PBDRV_CONFIG_HAS_PORT_VCC_CONTROL (1)
4343

4444
#define PBDRV_CONFIG_USB (0)
45+
#define PBDRV_CONFIG_USB_MAX_PACKET_SIZE (64)
4546
#define PBDRV_CONFIG_USB_SIMULATION (0)
4647
#define PBDRV_CONFIG_USB_MFG_STR u"Pybricks"
4748
#define PBDRV_CONFIG_USB_PROD_STR u"Virtual Hub"

0 commit comments

Comments
 (0)