Skip to content

Commit 19f53e1

Browse files
committed
pbio/drv/usb_nxt: Implement mock driver with RFCOMM.
This lets us start from a working REPL, and make everything work like the other hubs. Now that all communication goes through the proper sys/host module, we can also finally use the common mphalport for NXT.
1 parent 18a83a9 commit 19f53e1

File tree

4 files changed

+39
-124
lines changed

4 files changed

+39
-124
lines changed

bricks/_common/common.mk

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -276,18 +276,8 @@ endif
276276

277277
PY_EXTRA_SRC_C += $(addprefix bricks/_common/,\
278278
micropython.c \
279-
)
280-
281-
# TODO: NXT should eventually use the same mphalport.c as well.
282-
ifeq ($(PB_MCU_FAMILY),AT91SAM7)
283-
PY_EXTRA_SRC_C += $(addprefix bricks/nxt/,\
284-
mphalport.c \
285-
)
286-
else
287-
PY_EXTRA_SRC_C += $(addprefix bricks/_common/,\
288279
mphalport.c \
289280
)
290-
endif
291281

292282
# Not all MCUs support thumb2 instructions.
293283
ifeq ($(PB_MCU_FAMILY),native)

bricks/nxt/mphalport.c

Lines changed: 0 additions & 94 deletions
This file was deleted.

lib/pbio/drv/usb/usb_nxt.c

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -924,16 +924,30 @@ uint32_t pbdrv_usb_tx_get_buf(pbio_pybricks_usb_in_ep_msg_t message_type, uint8_
924924
return sizeof(pbdrv_usb_tx_buf);
925925
}
926926

927+
// As a stepping stone, we use RFCOMM to provide the REPL through this USB
928+
// module. This should be removed when USB is fully implemented.
929+
#include <nxos/drivers/bt.h>
930+
931+
extern bool nx_bt_is_ready(void);
932+
927933
pbio_error_t pbdrv_usb_tx(pbio_os_state_t *state, const uint8_t *data, uint32_t size) {
928934

929935
static pbio_os_timer_t timer;
930936

931937
PBIO_OS_ASYNC_BEGIN(state);
932938

933-
// TODO: Transmit and await completion
934-
pbio_os_timer_set(&timer, PBDRV_USB_TRANSMIT_TIMEOUT);
939+
if (!nx_bt_is_ready()) {
940+
return PBIO_SUCCESS;
941+
}
942+
943+
// Only map USB stdout messages to this RFCOMM test.
944+
if (size < 2 || data[0] != PBIO_PYBRICKS_IN_EP_MSG_EVENT || data[1] != PBIO_PYBRICKS_EVENT_WRITE_STDOUT) {
945+
return PBIO_SUCCESS;
946+
}
935947

936-
PBIO_OS_AWAIT_UNTIL(state, /*!transmitting ||*/ pbio_os_timer_is_expired(&timer));
948+
pbio_os_timer_set(&timer, PBDRV_USB_TRANSMIT_TIMEOUT * 100);
949+
nx_bt_stream_write((uint8_t *)data + 2, size - 2);
950+
PBIO_OS_AWAIT_UNTIL(state, nx_bt_stream_data_written() || pbio_os_timer_is_expired(&timer));
937951
if (pbio_os_timer_is_expired(&timer)) {
938952
return PBIO_ERROR_TIMEDOUT;
939953
}
@@ -948,18 +962,34 @@ pbio_error_t pbdrv_usb_tx_reset(pbio_os_state_t *state) {
948962

949963
uint32_t pbdrv_usb_get_data_in(uint8_t *data) {
950964

965+
// Use RFCOMM as mock input.
966+
static uint8_t byte[1];
967+
static bool initialized;
968+
if (!initialized && nx_bt_is_ready()) {
969+
initialized = true;
970+
971+
// nxos API requires size in advance, so start reading one byte.
972+
nx_bt_stream_read(byte, sizeof(byte));
973+
974+
// First USB message is the pretend-subscribe message.
975+
const uint8_t subscribe[] = { PBIO_PYBRICKS_OUT_EP_MSG_SUBSCRIBE, 1};
976+
memcpy(data, subscribe, sizeof(subscribe));
977+
return sizeof(subscribe);
978+
}
979+
951980
// Nothing received yet.
952-
if (0) {
981+
if (!initialized || !nx_bt_stream_data_read()) {
953982
return 0;
954983
}
955984

956-
uint32_t size = 0;
957-
958-
// TODO: memcpy to data
985+
// Pretend to send a single-character stdin message.
986+
const uint8_t stdin[] = { PBIO_PYBRICKS_OUT_EP_MSG_COMMAND, PBIO_PYBRICKS_COMMAND_WRITE_STDIN, byte[0]};
987+
memcpy(data, stdin, sizeof(stdin));
959988

960-
// TODO: Reset to start waiting for new data.
989+
// Start waiting for another character.
990+
nx_bt_stream_read(byte, sizeof(byte));
961991

962-
return size;
992+
return sizeof(stdin);
963993
}
964994

965995
#endif // PBDRV_CONFIG_USB_NXT

lib/pbio/platform/nxt/platform.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,6 @@ static pbio_error_t legacy_bluetooth_connect_process_thread(pbio_os_state_t *sta
9393
nx_display_cursor_set_pos(0, 0);
9494

9595
nx_display_string("RFCOMM ready.\n");
96-
nx_display_string("Press a key.\n");
97-
98-
// Receive one character to get going.
99-
static uint8_t flush_buf[1];
100-
nx_bt_stream_read(flush_buf, sizeof(flush_buf));
101-
102-
while (nx_bt_stream_data_read() != sizeof(flush_buf)) {
103-
PBIO_OS_AWAIT_MS(state, &timer, 2);
104-
}
105-
106-
nx_display_string("Let's code!\n");
10796

10897
PBIO_OS_ASYNC_END(PBIO_SUCCESS);
10998
}

0 commit comments

Comments
 (0)