nrf: Fix ble uart using the new API

This commit is contained in:
arturo182 2018-07-23 23:43:24 +02:00
parent d5a71a4b8a
commit cf79316002
6 changed files with 172 additions and 238 deletions

View File

@ -4,6 +4,7 @@
* The MIT License (MIT)
*
* Copyright (c) 2017 Glenn Ruben Bakke
* Copyright (c) 2018 Artur Pacholec
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@ -24,252 +25,172 @@
* THE SOFTWARE.
*/
#if BLUETOOTH_SD
#include <string.h>
#include "ble.h"
#include "ble_uart.h"
#include "ringbuffer.h"
#include "py/mphal.h"
#include "py/runtime.h"
#include "lib/utils/interrupt_char.h"
#include "shared-bindings/bleio/Adapter.h"
#include "shared-bindings/bleio/Characteristic.h"
#include "shared-bindings/bleio/Device.h"
#include "shared-bindings/bleio/Service.h"
#include "shared-bindings/bleio/UUID.h"
#if MICROPY_PY_BLE_NUS
#if (MICROPY_PY_BLE_NUS == 1)
static bleio_uuid_obj_t uuid_obj_service = {
.base.type = &bleio_uuid_type,
.type = UUID_128_BIT,
.value = {0x01, 0x00}
};
static const char default_name[] = "CP-REPL"; // max 8 chars or uuid won't fit in adv data
static const char NUS_UUID[] = "6e400001-b5a3-f393-e0a9-e50e24dcca9e";
static bleio_uuid_obj_t uuid_obj_char_tx = {
.base.type = &bleio_uuid_type,
.type = UUID_128_BIT,
.value = {0x03, 0x00}
};
static bleio_uuid_obj_t uuid_obj_char_rx = {
.base.type = &bleio_uuid_type,
.type = UUID_128_BIT,
.value = {0x02, 0x00}
};
static ubluepy_service_obj_t ble_uart_service = {
.base.type = &ubluepy_service_type,
.p_uuid = &uuid_obj_service,
.type = UBLUEPY_SERVICE_PRIMARY
};
static ubluepy_characteristic_obj_t ble_uart_char_rx = {
.base.type = &ubluepy_characteristic_type,
.p_uuid = &uuid_obj_char_rx,
.props = UBLUEPY_PROP_WRITE | UBLUEPY_PROP_WRITE_WO_RESP,
.attrs = 0,
};
static ubluepy_characteristic_obj_t ble_uart_char_tx = {
.base.type = &ubluepy_characteristic_type,
.p_uuid = &uuid_obj_char_tx,
.props = UBLUEPY_PROP_NOTIFY,
.attrs = UBLUEPY_ATTR_CCCD,
};
static ubluepy_peripheral_obj_t ble_uart_peripheral = {
.base.type = &ubluepy_peripheral_type,
.conn_handle = 0xFFFF,
};
static volatile bool m_cccd_enabled;
static volatile bool m_connected;
#define NUS_RX_UUID 0x0002
#define NUS_TX_UUID 0x0003
#define BUFFER_SIZE 128
ringBuffer_typedef(uint8_t, ringbuffer_t);
static ringbuffer_t m_rx_ring_buffer;
static ringbuffer_t * mp_rx_ring_buffer = &m_rx_ring_buffer;
static uint8_t m_rx_ring_buffer_data[128];
static bleio_device_obj_t m_device;
static bleio_service_obj_t *m_nus;
static bleio_characteristic_obj_t *m_tx_chara;
static bleio_characteristic_obj_t *m_rx_chara;
static ubluepy_advertise_data_t m_adv_data_uart_service;
static volatile bool m_cccd_enabled;
#if BLUETOOTH_WEBBLUETOOTH_REPL
static ubluepy_advertise_data_t m_adv_data_eddystone_url;
#endif // BLUETOOTH_WEBBLUETOOTH_REPL
static uint8_t m_rx_ring_buffer_data[BUFFER_SIZE];
static ringbuffer_t m_rx_ring_buffer = {
.size = sizeof(m_rx_ring_buffer_data) + 1,
.elems = m_rx_ring_buffer_data,
};
int mp_hal_stdin_rx_chr(void) {
while (isBufferEmpty(mp_rx_ring_buffer)) {
;
}
uint8_t byte;
bufferRead(mp_rx_ring_buffer, byte);
return (int)byte;
}
bool mp_hal_stdin_any(void) {
return !isBufferEmpty(mp_rx_ring_buffer);
}
void mp_hal_stdout_tx_strn(const char *str, size_t len) {
uint8_t *buf = (uint8_t *)str;
size_t send_len;
while (len > 0) {
if (len >= 20) {
send_len = 20; // (GATT_MTU_SIZE_DEFAULT - 3)
} else {
send_len = len;
STATIC void on_ble_evt(ble_evt_t *ble_evt, void *param) {
switch (ble_evt->header.evt_id) {
case BLE_GAP_EVT_DISCONNECTED:
{
mp_obj_t device_obj = MP_OBJ_FROM_PTR(&m_device);
mp_call_function_0(mp_load_attr(device_obj, qstr_from_str("start_advertising")));
break;
}
ubluepy_characteristic_obj_t * p_char = &ble_uart_char_tx;
case BLE_GATTS_EVT_WRITE:
{
ble_gatts_evt_write_t *write = &ble_evt->evt.gatts_evt.params.write;
ble_drv_attr_s_notify(p_char->p_service->p_periph->conn_handle,
p_char->handle,
send_len,
buf);
len -= send_len;
buf += send_len;
}
}
void mp_hal_stdout_tx_strn_cooked(const char *str, mp_uint_t len) {
mp_hal_stdout_tx_strn(str, len);
}
STATIC void gap_event_handler(mp_obj_t self_in, uint16_t event_id, uint16_t conn_handle, uint16_t length, uint8_t * data) {
ubluepy_peripheral_obj_t * self = MP_OBJ_TO_PTR(self_in);
if (event_id == 16) { // connect event
self->conn_handle = conn_handle;
m_connected = true;
} else if (event_id == 17) { // disconnect event
self->conn_handle = 0xFFFF; // invalid connection handle
m_connected = false;
ble_uart_advertise();
}
}
STATIC void gatts_event_handler(mp_obj_t self_in, uint16_t event_id, uint16_t attr_handle, uint16_t length, uint8_t * data) {
ubluepy_peripheral_obj_t * self = MP_OBJ_TO_PTR(self_in);
(void)self;
if (event_id == 80) { // gatts write
if (ble_uart_char_tx.cccd_handle == attr_handle) {
m_cccd_enabled = true;
} else if (ble_uart_char_rx.handle == attr_handle) {
for (uint16_t i = 0; i < length; i++) {
#if MICROPY_KBD_EXCEPTION
if (data[i] == mp_interrupt_char) {
mp_keyboard_interrupt();
} else
#endif
{
bufferWrite(mp_rx_ring_buffer, data[i]);
if (write->handle == m_tx_chara->cccd_handle) {
m_cccd_enabled = true;
} else if (write->handle == m_rx_chara->handle) {
for (size_t i = 0; i < write->len; ++i) {
#if MICROPY_KBD_EXCEPTION
if (write->data[i] == mp_interrupt_char) {
mp_keyboard_interrupt();
} else
#endif
{
bufferWrite(&m_rx_ring_buffer, write->data[i]);
}
}
}
}
}
}
void ble_uart_init0(void) {
uint8_t base_uuid[] = {0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0, 0x93, 0xF3, 0xA3, 0xB5, 0x00, 0x00, 0x40, 0x6E};
uint8_t uuid_vs_idx;
void ble_uart_init(void) {
mp_obj_t device_obj = MP_OBJ_FROM_PTR(&m_device);
m_device.base.type = &bleio_device_type;
m_device.service_list = mp_obj_new_list(0, NULL);
m_device.notif_handler = mp_const_none;
m_device.conn_handler = mp_const_none;
m_device.conn_handle = 0xFFFF;
m_device.is_peripheral = true;
m_device.name = mp_obj_new_str(default_name, strlen(default_name), false);
common_hal_bleio_adapter_get_address(&m_device.address);
(void)ble_drv_uuid_add_vs(base_uuid, &uuid_vs_idx);
mp_obj_t nus_uuid_str = mp_obj_new_str(NUS_UUID, strlen(NUS_UUID), false);
mp_obj_t nus_uuid_obj = bleio_uuid_type.make_new(&bleio_uuid_type, 1, 0, &nus_uuid_str);
mp_obj_t nus_obj = bleio_service_type.make_new(&bleio_service_type, 1, 0, &nus_uuid_obj);
m_nus = MP_OBJ_TO_PTR(nus_obj);
mp_call_function_1(mp_load_attr(device_obj, qstr_from_str("add_service")), nus_obj);
uuid_obj_service.uuid_vs_idx = uuid_vs_idx;
uuid_obj_char_tx.uuid_vs_idx = uuid_vs_idx;
uuid_obj_char_rx.uuid_vs_idx = uuid_vs_idx;
mp_obj_t tx_uuid_int = mp_obj_new_int(NUS_TX_UUID);
mp_obj_t tx_uuid_obj = bleio_uuid_type.make_new(&bleio_uuid_type, 1, 0, &tx_uuid_int);
mp_obj_t tx_obj = bleio_characteristic_type.make_new(&bleio_characteristic_type, 1, 0, &tx_uuid_obj);
m_tx_chara = MP_OBJ_TO_PTR(tx_obj);
m_tx_chara->uuid->type = UUID_TYPE_128BIT;
m_tx_chara->uuid->uuid_vs_idx = m_nus->uuid->uuid_vs_idx;
m_tx_chara->props.notify = true;
mp_call_function_1(mp_load_attr(nus_obj, qstr_from_str("add_characteristic")), tx_obj);
(void)ble_drv_service_add(&ble_uart_service);
ble_uart_service.char_list = mp_obj_new_list(0, NULL);
mp_obj_t rx_uuid_int = mp_obj_new_int(NUS_RX_UUID);
mp_obj_t rx_uuid_obj = bleio_uuid_type.make_new(&bleio_uuid_type, 1, 0, &rx_uuid_int);
mp_obj_t rx_obj = bleio_characteristic_type.make_new(&bleio_characteristic_type, 1, 0, &rx_uuid_obj);
m_rx_chara = MP_OBJ_TO_PTR(rx_obj);
m_rx_chara->uuid->type = UUID_TYPE_128BIT;
m_rx_chara->uuid->uuid_vs_idx = m_nus->uuid->uuid_vs_idx;
m_rx_chara->props.write = true;
m_rx_chara->props.write_wo_resp = true;
mp_call_function_1(mp_load_attr(nus_obj, qstr_from_str("add_characteristic")), rx_obj);
// add TX characteristic
ble_uart_char_tx.service_handle = ble_uart_service.handle;
bool retval = ble_drv_characteristic_add(&ble_uart_char_tx);
if (retval) {
ble_uart_char_tx.p_service = &ble_uart_service;
}
mp_obj_list_append(ble_uart_service.char_list, MP_OBJ_FROM_PTR(&ble_uart_char_tx));
mp_call_function_0(mp_load_attr(device_obj, qstr_from_str("start_advertising")));
// add RX characteristic
ble_uart_char_rx.service_handle = ble_uart_service.handle;
retval = ble_drv_characteristic_add(&ble_uart_char_rx);
if (retval) {
ble_uart_char_rx.p_service = &ble_uart_service;
}
mp_obj_list_append(ble_uart_service.char_list, MP_OBJ_FROM_PTR(&ble_uart_char_rx));
// setup the peripheral
ble_uart_peripheral.service_list = mp_obj_new_list(0, NULL);
mp_obj_list_append(ble_uart_peripheral.service_list, MP_OBJ_FROM_PTR(&ble_uart_service));
ble_uart_service.p_periph = &ble_uart_peripheral;
ble_drv_gap_event_handler_set(MP_OBJ_FROM_PTR(&ble_uart_peripheral), gap_event_handler);
ble_drv_gatts_event_handler_set(MP_OBJ_FROM_PTR(&ble_uart_peripheral), gatts_event_handler);
ble_uart_peripheral.conn_handle = 0xFFFF;
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));
mp_obj_t * services = NULL;
mp_uint_t num_services;
mp_obj_get_array(service_list, &num_services, &services);
m_adv_data_uart_service.p_services = services;
m_adv_data_uart_service.num_of_services = num_services;
m_adv_data_uart_service.p_device_name = (uint8_t *)device_name;
m_adv_data_uart_service.device_name_len = strlen(device_name);
m_adv_data_uart_service.connectable = true;
m_adv_data_uart_service.p_data = NULL;
#if BLUETOOTH_WEBBLUETOOTH_REPL
// for now point eddystone URL to https://goo.gl/x46FES => https://glennrub.github.io/webbluetooth/micropython/repl/
static uint8_t eddystone_url_data[27] = {0x2, 0x1, 0x6,
0x3, 0x3, 0xaa, 0xfe,
19, 0x16, 0xaa, 0xfe, 0x10, 0xee, 0x3, 'g', 'o', 'o', '.', 'g', 'l', '/', 'x', '4', '6', 'F', 'E', 'S'};
// eddystone url adv data
m_adv_data_eddystone_url.p_data = eddystone_url_data;
m_adv_data_eddystone_url.data_len = sizeof(eddystone_url_data);
m_adv_data_eddystone_url.connectable = false;
#endif
ble_drv_add_event_handler(on_ble_evt, &m_device);
m_cccd_enabled = false;
// initialize ring buffer
m_rx_ring_buffer.size = sizeof(m_rx_ring_buffer_data) + 1;
m_rx_ring_buffer.start = 0;
m_rx_ring_buffer.end = 0;
m_rx_ring_buffer.elems = m_rx_ring_buffer_data;
m_connected = false;
ble_uart_advertise();
}
void ble_uart_advertise(void) {
#if BLUETOOTH_WEBBLUETOOTH_REPL
while (!m_connected) {
(void)ble_drv_advertise_data(&m_adv_data_uart_service);
mp_hal_delay_ms(500);
(void)ble_drv_advertise_data(&m_adv_data_eddystone_url);
mp_hal_delay_ms(500);
while (!m_cccd_enabled) {
#ifdef MICROPY_VM_HOOK_LOOP
MICROPY_VM_HOOK_LOOP
#endif
}
ble_drv_advertise_stop();
#else
(void)ble_drv_advertise_data(&m_adv_data_uart_service);
#endif // BLUETOOTH_WEBBLUETOOTH_REPL
}
bool ble_uart_connected(void) {
return (m_connected);
return (m_device.conn_handle != BLE_CONN_HANDLE_INVALID);
}
bool ble_uart_enabled(void) {
return (m_cccd_enabled);
char ble_uart_rx_chr(void) {
while (isBufferEmpty(&m_rx_ring_buffer)) {
#ifdef MICROPY_VM_HOOK_LOOP
MICROPY_VM_HOOK_LOOP
#endif
}
uint8_t byte;
bufferRead(&m_rx_ring_buffer, byte);
return (int)byte;
}
bool ble_uart_stdin_any(void) {
return !isBufferEmpty(&m_rx_ring_buffer);
}
void ble_uart_stdout_tx_str(const char *text) {
mp_hal_stdout_tx_strn(text, strlen(text));
}
int mp_hal_stdin_rx_chr(void) {
return ble_uart_rx_chr();
}
void mp_hal_stdout_tx_strn(const char *str, size_t len) {
size_t send_len;
while (len > 0) {
if (len >= BLE_GATT_ATT_MTU_DEFAULT - 3) {
send_len = (BLE_GATT_ATT_MTU_DEFAULT - 3);
} else {
send_len = len;
}
mp_buffer_info_t bufinfo = {
.buf = (uint8_t*)str,
.len = send_len,
};
common_hal_bleio_characteristic_write_value(m_tx_chara, &bufinfo);
len -= send_len;
str += send_len;
}
}
#endif // MICROPY_PY_BLE_NUS
#endif // BLUETOOTH_SD

View File

@ -24,16 +24,17 @@
* THE SOFTWARE.
*/
#ifndef BLUETOOTH_LE_UART_H__
#define BLUETOOTH_LE_UART_H__
#ifndef MICROPY_INCLUDED_NRF_BLUETOOTH_BLE_UART_H
#define MICROPY_INCLUDED_NRF_BLUETOOTH_BLE_UART_H
#include <stdbool.h>
#include "ble_drv.h"
void ble_uart_init0(void);
void ble_uart_advertise(void);
void ble_uart_init(void);
bool ble_uart_connected(void);
bool ble_uart_enabled(void);
char ble_uart_rx_chr(void);
bool ble_uart_stdin_any(void);
void ble_uart_stdout_tx_str(const char *text);
#endif // BLUETOOTH_LE_UART_H__
#endif // MICROPY_INCLUDED_NRF_BLUETOOTH_BLE_UART_H

View File

@ -79,21 +79,21 @@
BUF.elems = (T*)calloc(BUF.size, sizeof(T))
#define bufferDestroy(BUF) free(BUF->elems)
#define nextStartIndex(BUF) ((BUF->start + 1) % BUF->size)
#define nextEndIndex(BUF) ((BUF->end + 1) % BUF->size)
#define isBufferEmpty(BUF) (BUF->end == BUF->start)
#define isBufferFull(BUF) (nextEndIndex(BUF) == BUF->start)
#define bufferDestroy(BUF) free((BUF)->elems)
#define nextStartIndex(BUF) (((BUF)->start + 1) % (BUF)->size)
#define nextEndIndex(BUF) (((BUF)->end + 1) % (BUF)->size)
#define isBufferEmpty(BUF) ((BUF)->end == (BUF)->start)
#define isBufferFull(BUF) (nextEndIndex(BUF) == (BUF)->start)
#define bufferWrite(BUF, ELEM) \
BUF->elems[BUF->end] = ELEM; \
BUF->end = (BUF->end + 1) % BUF->size; \
(BUF)->elems[(BUF)->end] = ELEM; \
(BUF)->end = ((BUF)->end + 1) % (BUF)->size; \
if (isBufferEmpty(BUF)) { \
BUF->start = nextStartIndex(BUF); \
(BUF)->start = nextStartIndex(BUF); \
}
#define bufferRead(BUF, ELEM) \
ELEM = BUF->elems[BUF->start]; \
BUF->start = nextStartIndex(BUF);
ELEM = (BUF)->elems[(BUF)->start]; \
(BUF)->start = nextStartIndex(BUF);
#endif

View File

@ -131,7 +131,6 @@
#if BLUETOOTH_SD
#define MICROPY_PY_BLEIO (1)
#define MICROPY_PY_BLE_NUS (0)
#define BLUETOOTH_WEBBLUETOOTH_REPL (0)
#else
#ifndef MICROPY_PY_BLEIO
#define MICROPY_PY_BLEIO (0)

View File

@ -109,8 +109,7 @@ void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
#endif // USB
#endif // NUS
#endif // MICROPY_PY_BLE_NUS
/*------------------------------------------------------------------*/
/* delay

View File

@ -26,25 +26,41 @@
#include "py/mphal.h"
#if MICROPY_PY_BLE_NUS
#if (MICROPY_PY_BLE_NUS == 1)
#include "ble_uart.h"
#else
#include "nrf_gpio.h"
#include "nrfx_uarte.h"
#endif
#if !defined(NRF52840_XXAA)
#if (MICROPY_PY_BLE_NUS == 1)
void serial_init(void) {
ble_uart_init();
}
bool serial_connected(void) {
return ble_uart_connected();
}
char serial_read(void) {
return (char) ble_uart_rx_chr();
}
bool serial_bytes_available(void) {
return ble_uart_stdin_any();
}
void serial_write(const char *text) {
ble_uart_stdout_tx_str(text);
}
#elif !defined(NRF52840_XXAA)
uint8_t serial_received_char;
nrfx_uarte_t serial_instance = NRFX_UARTE_INSTANCE(0);
void serial_init(void) {
#if MICROPY_PY_BLE_NUS
ble_uart_init0();
while (!ble_uart_enabled()) {
;
}
#else
nrfx_uarte_config_t config = {
.pseltxd = MICROPY_HW_UART_TX,
.pselrxd = MICROPY_HW_UART_RX,
@ -66,7 +82,6 @@ void serial_init(void) {
// enabled receiving
nrf_uarte_task_trigger(serial_instance.p_reg, NRF_UARTE_TASK_STARTRX);
#endif
}
bool serial_connected(void) {
@ -93,7 +108,6 @@ void serial_init(void) {
// usb is already initialized in board_init()
}
bool serial_connected(void) {
return tud_cdc_connected();
}