Skip to content

Commit

Permalink
nrf: Update to handle pyboard.py BLE UART serial tunnel.
Browse files Browse the repository at this point in the history
  • Loading branch information
glennrub committed Jan 26, 2020
1 parent e65941b commit 38d6e0f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
6 changes: 6 additions & 0 deletions ports/nrf/drivers/bluetooth/ble_drv.c
Expand Up @@ -1145,6 +1145,12 @@ static void ble_evt_handler(ble_evt_t * p_ble_evt) {
BLE_DRIVER_LOG("GATTS EVT EXCHANGE MTU REQUEST\n");
(void)sd_ble_gatts_exchange_mtu_reply(p_ble_evt->evt.gatts_evt.conn_handle, 23); // MAX MTU size
break;

case BLE_GAP_EVT_DATA_LENGTH_UPDATE_REQUEST:
BLE_DRIVER_LOG("BLE GAP EVT DATA LENGTH UPDATE REQUEST\n");
sd_ble_gap_data_length_update(p_ble_evt->evt.gap_evt.conn_handle, NULL, NULL);
break;

#endif // (BLUETOOTH_SD == 132) || (BLUETOOTH_SD == 140)

default:
Expand Down
32 changes: 30 additions & 2 deletions ports/nrf/drivers/bluetooth/ble_uart.c
Expand Up @@ -32,6 +32,8 @@
#include "mphalport.h"
#include "lib/utils/interrupt_char.h"

#include "py/stream.h"

#if MICROPY_PY_BLE_NUS

static ubluepy_uuid_obj_t uuid_obj_service = {
Expand Down Expand Up @@ -131,8 +133,34 @@ void mp_hal_stdout_tx_strn(const char *str, size_t len) {
}
}

void ble_uart_tx_char(char c) {
// Not connected: drop output
if (!ble_uart_enabled()) return;

ubluepy_characteristic_obj_t * p_char = &ble_uart_char_tx;

ble_drv_attr_s_notify(p_char->p_service->p_periph->conn_handle,
p_char->handle,
1,
(uint8_t *)&c);
}

void mp_hal_stdout_tx_strn_cooked(const char *str, mp_uint_t len) {
mp_hal_stdout_tx_strn(str, len);
for (const char *top = str + len; str < top; str++) {
if (*str == '\n') {
ble_uart_tx_char('\r');
}
ble_uart_tx_char(*str);
}
}

uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
uintptr_t ret = 0;
if ((poll_flags & MP_STREAM_POLL_RD) && ble_uart_enabled()
&& !isBufferEmpty(mp_rx_ring_buffer)) {
ret |= MP_STREAM_POLL_RD;
}
return ret;
}

STATIC void gap_event_handler(mp_obj_t self_in, uint16_t event_id, uint16_t conn_handle, uint16_t length, uint8_t * data) {
Expand Down Expand Up @@ -212,7 +240,7 @@ void ble_uart_init0(void) {

ble_uart_peripheral.conn_handle = 0xFFFF;

char device_name[] = "mpus";
static char device_name[] = "mpus";

mp_obj_t service_list = mp_obj_new_list(0, NULL);
mp_obj_list_append(service_list, MP_OBJ_FROM_PTR(&ble_uart_service));
Expand Down
10 changes: 10 additions & 0 deletions ports/nrf/main.c
Expand Up @@ -59,6 +59,7 @@

#if BLUETOOTH_SD
#include "nrf_sdm.h"
#include "mphalport.h"
#endif

#if (MICROPY_PY_BLE_NUS)
Expand Down Expand Up @@ -317,6 +318,10 @@ led_state(1, 0);
usb_cdc_init();
#endif

#if BLUETOOTH_SD
ble_reset:
#endif

for (;;) {
if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
if (pyexec_raw_repl() != 0) {
Expand All @@ -330,6 +335,11 @@ led_state(1, 0);
}
}

#if BLUETOOTH_SD
printf("MPY: soft reboot\n");
goto ble_reset;
#endif

mp_deinit();

printf("MPY: soft reboot\n");
Expand Down

0 comments on commit 38d6e0f

Please sign in to comment.