Skip to content

Commit fd9f19c

Browse files
committed
pbio/drv/usb: Minor naming updates.
1 parent 19f53e1 commit fd9f19c

File tree

6 files changed

+40
-38
lines changed

6 files changed

+40
-38
lines changed

lib/pbio/drv/usb/usb.c

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,25 @@ void pbdrv_usb_set_receive_handler(pbdrv_usb_receive_handler_t handler) {
4343
/**
4444
* Buffer scheduled status.
4545
*/
46-
static uint8_t status_data[PBIO_PYBRICKS_EVENT_STATUS_REPORT_SIZE];
47-
static bool status_data_pending;
46+
static uint8_t pbdrv_usb_status_data[PBIO_PYBRICKS_EVENT_STATUS_REPORT_SIZE];
47+
static bool pbdrv_usb_status_data_pending;
4848

4949
void pbdrv_usb_schedule_status_update(const uint8_t *status_msg) {
5050
// Ignore if message identical to last.
51-
if (!memcmp(status_data, status_msg, sizeof(status_data))) {
51+
if (!memcmp(pbdrv_usb_status_data, status_msg, sizeof(pbdrv_usb_status_data))) {
5252
return;
5353
}
5454

5555
// Schedule to send whenever the Bluetooth process gets round to it.
56-
memcpy(status_data, status_msg, sizeof(status_data));
57-
status_data_pending = true;
56+
memcpy(pbdrv_usb_status_data, status_msg, sizeof(pbdrv_usb_status_data));
57+
pbdrv_usb_status_data_pending = true;
5858
pbio_os_request_poll();
5959
}
6060

6161
/**
6262
* Buffer for scheduled stdout.
6363
*/
64-
static lwrb_t stdout_ring_buf;
64+
static lwrb_t pbdrv_usb_stdout_ring_buf;
6565

6666
pbio_error_t pbdrv_usb_stdout_tx(const uint8_t *data, uint32_t *size) {
6767

@@ -71,7 +71,7 @@ pbio_error_t pbdrv_usb_stdout_tx(const uint8_t *data, uint32_t *size) {
7171

7272
// Buffer data to send it more efficiently even if the caller is only
7373
// writing one byte at a time.
74-
if ((*size = lwrb_write(&stdout_ring_buf, data, *size)) == 0) {
74+
if ((*size = lwrb_write(&pbdrv_usb_stdout_ring_buf, data, *size)) == 0) {
7575
return PBIO_ERROR_AGAIN;
7676
}
7777

@@ -86,31 +86,33 @@ uint32_t pbdrv_usb_stdout_tx_available(void) {
8686
if (!pbdrv_usb_connection_is_active()) {
8787
return UINT32_MAX;
8888
}
89-
return lwrb_get_free(&stdout_ring_buf);
89+
return lwrb_get_free(&pbdrv_usb_stdout_ring_buf);
9090
}
9191

9292
bool pbdrv_usb_stdout_tx_is_idle(void) {
9393
if (!pbdrv_usb_connection_is_active()) {
9494
return true;
9595
}
96-
return lwrb_get_full(&stdout_ring_buf) == 0;
96+
return lwrb_get_full(&pbdrv_usb_stdout_ring_buf) == 0;
9797
}
9898

99-
static bool respond_soon;
100-
static pbio_pybricks_error_t respond_result;
99+
static bool pbdrv_usb_respond_soon;
100+
static pbio_pybricks_error_t pbdrv_usb_respond_result;
101101

102102
/**
103103
* Non-blocking poll handler to process pending incoming messages.
104104
*/
105105
static void pbdrv_usb_handle_data_in(void) {
106106

107107
// Ignore incoming data if we haven't sent our previous response yet.
108-
if (respond_soon) {
108+
if (pbdrv_usb_respond_soon) {
109109
return;
110110
}
111111

112-
static uint8_t data_in[PBDRV_CONFIG_USB_MAX_PACKET_SIZE];
113-
uint32_t size = pbdrv_usb_get_data_in(data_in);
112+
// Data is copied here so the driver can immediately clear it and queue
113+
// the next receive.
114+
uint8_t data_in[PBDRV_CONFIG_USB_MAX_PACKET_SIZE];
115+
uint32_t size = pbdrv_usb_get_data_and_start_receive(data_in);
114116

115117
// Expecting at least EP_MSG and payload.
116118
if (size < 2) {
@@ -120,26 +122,26 @@ static void pbdrv_usb_handle_data_in(void) {
120122
switch (data_in[0]) {
121123
case PBIO_PYBRICKS_OUT_EP_MSG_SUBSCRIBE:
122124
pbdrv_usb_events_subscribed = data_in[1];
123-
respond_result = PBIO_PYBRICKS_ERROR_OK;
124-
respond_soon = true;
125+
pbdrv_usb_respond_result = PBIO_PYBRICKS_ERROR_OK;
126+
pbdrv_usb_respond_soon = true;
125127

126128
// Schedule sending current status immediately after subscribing.
127-
status_data_pending = true;
129+
pbdrv_usb_status_data_pending = true;
128130
break;
129131
case PBIO_PYBRICKS_OUT_EP_MSG_COMMAND:
130132
if (pbdrv_usb_receive_handler) {
131-
respond_result = pbdrv_usb_receive_handler(data_in + 1, size - 1);
132-
respond_soon = true;
133+
pbdrv_usb_respond_result = pbdrv_usb_receive_handler(data_in + 1, size - 1);
134+
pbdrv_usb_respond_soon = true;
133135
}
134136
break;
135137
}
136138
}
137139

138140
static void pbdrv_usb_reset_state(void) {
139141
pbdrv_usb_events_subscribed = false;
140-
respond_soon = false;
141-
status_data_pending = false;
142-
lwrb_reset(&stdout_ring_buf);
142+
pbdrv_usb_respond_soon = false;
143+
pbdrv_usb_status_data_pending = false;
144+
lwrb_reset(&pbdrv_usb_stdout_ring_buf);
143145
}
144146

145147
static pbio_os_process_t pbdrv_usb_process;
@@ -167,23 +169,23 @@ static pbio_error_t pbdrv_usb_process_thread(pbio_os_state_t *state, void *conte
167169

168170
// Find out what we should send, if anything, priotizing response, then
169171
// status, then stdout, then other events.
170-
if (respond_soon) {
172+
if (pbdrv_usb_respond_soon) {
171173
// Pack the response to the most recent message.
172174
pbdrv_usb_tx_get_buf(PBIO_PYBRICKS_IN_EP_MSG_RESPONSE, &out_data);
173-
pbio_set_uint32_le(&out_data[1], respond_result);
175+
pbio_set_uint32_le(&out_data[1], pbdrv_usb_respond_result);
174176
out_size = sizeof(uint32_t) + 1;
175-
respond_soon = false;
176-
} else if (pbdrv_usb_connection_is_active() && status_data_pending) {
177+
pbdrv_usb_respond_soon = false;
178+
} else if (pbdrv_usb_connection_is_active() && pbdrv_usb_status_data_pending) {
177179
// Send out status if pending (already includes event code).
178180
pbdrv_usb_tx_get_buf(PBIO_PYBRICKS_IN_EP_MSG_EVENT, &out_data);
179-
memcpy(&out_data[1], status_data, PBIO_PYBRICKS_EVENT_STATUS_REPORT_SIZE);
181+
memcpy(&out_data[1], pbdrv_usb_status_data, PBIO_PYBRICKS_EVENT_STATUS_REPORT_SIZE);
180182
out_size = PBIO_PYBRICKS_USB_MESSAGE_SIZE(PBIO_PYBRICKS_EVENT_STATUS_REPORT_SIZE);
181-
status_data_pending = false;
182-
} else if (pbdrv_usb_connection_is_active() && lwrb_get_full(&stdout_ring_buf) != 0) {
183+
pbdrv_usb_status_data_pending = false;
184+
} else if (pbdrv_usb_connection_is_active() && lwrb_get_full(&pbdrv_usb_stdout_ring_buf) != 0) {
183185
// Send out stdout if anything is buffered.
184186
uint32_t max_size = pbdrv_usb_tx_get_buf(PBIO_PYBRICKS_IN_EP_MSG_EVENT, &out_data);
185187
out_data[1] = PBIO_PYBRICKS_EVENT_WRITE_STDOUT;
186-
out_size = lwrb_read(&stdout_ring_buf, &out_data[2], max_size - 2) + 2;
188+
out_size = lwrb_read(&pbdrv_usb_stdout_ring_buf, &out_data[2], max_size - 2) + 2;
187189
}
188190

189191
// If there was anything to send, send it.
@@ -214,7 +216,7 @@ void pbdrv_usb_init(void) {
214216
pbdrv_usb_init_device();
215217

216218
static uint8_t stdout_buf[PBDRV_CONFIG_USB_MAX_PACKET_SIZE * 2];
217-
lwrb_init(&stdout_ring_buf, stdout_buf, PBIO_ARRAY_SIZE(stdout_buf));
219+
lwrb_init(&pbdrv_usb_stdout_ring_buf, stdout_buf, PBIO_ARRAY_SIZE(stdout_buf));
218220

219221
pbio_os_process_start(&pbdrv_usb_process, pbdrv_usb_process_thread, NULL);
220222
}

lib/pbio/drv/usb/usb.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void pbdrv_usb_deinit_device(void);
4646
* @param [in] data Buffer to copy the message to.
4747
* @return Number of bytes copied. Zero means nothing was available.
4848
*/
49-
uint32_t pbdrv_usb_get_data_in(uint8_t *data);
49+
uint32_t pbdrv_usb_get_data_and_start_receive(uint8_t *data);
5050

5151
/**
5252
* Gets the buffer to match the given endpoint mesage type.
@@ -114,11 +114,11 @@ static inline void pbdrv_usb_init_device(void) {
114114
static inline void pbdrv_usb_deinit_device(void) {
115115
}
116116

117-
static inline pbio_error_t pbdrv_usb_tx(pbio_os_state_t *state, const uint8_t *data, size_t size) {
117+
static inline pbio_error_t pbdrv_usb_tx(pbio_os_state_t *state, const uint8_t *data, uint32_t size) {
118118
return PBIO_ERROR_NOT_IMPLEMENTED;
119119
}
120120

121-
static inline uint32_t pbdrv_usb_get_data_in(uint8_t *data) {
121+
static inline uint32_t pbdrv_usb_get_data_and_start_receive(uint8_t *data) {
122122
return 0;
123123
}
124124

lib/pbio/drv/usb/usb_ev3.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ static void usb_device_intr(void) {
878878
HWREG(USB_0_OTGBASE + USB_0_END_OF_INTR) = 0;
879879
}
880880

881-
uint32_t pbdrv_usb_get_data_in(uint8_t *data) {
881+
uint32_t pbdrv_usb_get_data_and_start_receive(uint8_t *data) {
882882

883883
if (!usb_rx_is_ready) {
884884
return 0;

lib/pbio/drv/usb/usb_nxt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,7 @@ pbio_error_t pbdrv_usb_tx_reset(pbio_os_state_t *state) {
960960
return PBIO_SUCCESS;
961961
}
962962

963-
uint32_t pbdrv_usb_get_data_in(uint8_t *data) {
963+
uint32_t pbdrv_usb_get_data_and_start_receive(uint8_t *data) {
964964

965965
// Use RFCOMM as mock input.
966966
static uint8_t byte[1];

lib/pbio/drv/usb/usb_simulation.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pbio_error_t pbdrv_usb_tx_reset(pbio_os_state_t *state) {
6565
static uint8_t usb_in_buf[PBDRV_CONFIG_USB_MAX_PACKET_SIZE];
6666
static uint32_t usb_in_size;
6767

68-
uint32_t pbdrv_usb_get_data_in(uint8_t *data) {
68+
uint32_t pbdrv_usb_get_data_and_start_receive(uint8_t *data) {
6969

7070
// Invalid size.
7171
if (usb_in_size > PBDRV_CONFIG_USB_MAX_PACKET_SIZE) {

lib/pbio/drv/usb/usb_stm32.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ pbio_error_t pbdrv_usb_tx(pbio_os_state_t *state, const uint8_t *data, uint32_t
338338
PBIO_OS_ASYNC_END(PBIO_SUCCESS);
339339
}
340340

341-
uint32_t pbdrv_usb_get_data_in(uint8_t *data) {
341+
uint32_t pbdrv_usb_get_data_and_start_receive(uint8_t *data) {
342342

343343
if (!usb_in_sz) {
344344
return 0;

0 commit comments

Comments
 (0)