8000 Merge remote-tracking branch 'tralamazza/nrf5_no_sdk' into nrf52 · pcurry/circuitpython@b7729a2 · GitHub
[go: up one dir, main page]

Skip to content

Commit b7729a2

Browse files
committed
Merge remote-tracking branch 'tralamazza/nrf5_no_sdk' into nrf52
2 parents 847f6c0 + 2e1335b commit b7729a2

File tree

102 files changed

+494
-4891
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+494
-4891
lines changed

lib/utils/pyexec.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ int pyexec_frozen_module(const char *name);
5555
void pyexec_event_repl_init(void);
5656
int pyexec_event_repl_process_char(int c);
5757
extern uint8_t pyexec_repl_active;
58+
mp_obj_t pyb_set_repl_info(mp_obj_t o_value);
5859

5960
MP_DECLARE_CONST_FUN_OBJ_1(pyb_set_repl_info_obj);
6061

nrf5/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ else
3131
include ../py/mkenv.mk
3232
include boards/$(BOARD)/mpconfigboard_$(SD_LOWER).mk
3333

34-
include sdk/sdk_common.mk
34+
include bluetooth/bluetooth_common.mk
3535
endif
3636

3737
# qstr definitions (must come before including py.mk)

nrf5/sdk/ble_drv.c renamed to nrf5/bluetooth/ble_drv.c

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ if (ble_drv_stack_enabled() == 0) { \
7171
(void)ble_drv_stack_enable(); \
7272
}
7373

74-
static volatile bool m_adv_in_progress = false;
< F438 /code>
75-
static volatile bool m_tx_in_progress = false;
74+
static volatile bool m_adv_in_progress;
75+
static volatile bool m_tx_in_progress;
7676

7777
static ubluepy_gap_evt_callback_t ubluepy_gap_event_handler;
7878
static ubluepy_gatts_evt_callback_t ubluepy_gatts_event_handler;
@@ -101,13 +101,21 @@ void softdevice_assert_handler(uint32_t id, uint32_t pc, uint32_t info) {
101101
}
102102
#endif
103103
uint32_t ble_drv_stack_enable(void) {
104+
m_adv_in_progress = false;
105+
m_tx_in_progress = false;
106+
104107
#if (BLUETOOTH_SD != 100) && (BLUETOOTH_SD != 110)
105108
memset(&nrf_nvic_state, 0, sizeof(nrf_nvic_state_t));
106109
#endif
107110

108111
#if (BLUETOOTH_SD == 100) || (BLUETOOTH_SD == 110)
112+
#if BLUETOOTH_LFCLK_RC
113+
uint32_t err_code = sd_softdevice_enable(NRF_CLOCK_LFCLKSRC_RC_250_PPM_250MS_CALIBRATION,
114+
softdevice_assert_handler);
115+
#else
109116
uint32_t err_code = sd_softdevice_enable(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM,
110117
softdevice_assert_handler);
118+
#endif // BLUETOOTH_LFCLK_RC
111119
#else
112120
nrf_clock_lf_cfg_t clock_config = {
113121
.source = NRF_CLOCK_LF_SRC_XTAL,
@@ -286,8 +294,9 @@ bool ble_drv_service_add(ubluepy_service_obj_t * p_service_obj) {
286294
if (p_service_obj->p_uuid->type > BLE_UUID_TYPE_BLE) {
287295

288296
ble_uuid_t uuid;
289-
uuid.type = p_service_obj->p_uuid->uuid_vs_idx;
290-
uuid.uuid = (uint16_t)(*(uint16_t *)&p_service_obj->p_uuid->value[0]);
297+
uuid.type = p_service_obj->p_uuid->uuid_vs_idx;
298+
uuid.uuid = p_service_obj->p_uuid->value[0];
299+
uuid.uuid += p_service_obj->p_uuid->value[1] << 8;
291300

292301
if (sd_ble_gatts_service_add(p_service_obj->type,
293302
&uuid,
@@ -296,11 +305,14 @@ bool ble_drv_service_add(ubluepy_service_obj_t * p_service_obj) {
296305
"Can not add Service."));
297306
}
298307
} else if (p_service_obj->p_uuid->type == BLE_UUID_TYPE_BLE) {
308+
printf("adding service\n");
299309

300310
ble_uuid_t uuid;
301-
uuid.type = p_service_obj->p_uuid->type;
302-
uuid.uuid = (uint16_t)(*(uint16_t *)&p_service_obj->p_uuid->value[0]);
311+
uuid.type = p_service_obj->p_uuid->type;
312+
uuid.uuid = p_service_obj->p_uuid->value[0];
313+
uuid.uuid += p_service_obj->p_uuid->value[1] << 8;
303314

315+
printf("adding service\n");
304316
if (sd_ble_gatts_service_add(p_service_obj->type,
305317
&uuid,
306318
&p_service_obj->handle) != 0) {
@@ -347,8 +359,9 @@ bool ble_drv_characteristic_add(ubluepy_characteristic_obj_t * p_char_obj) {
347359
char_md.p_cccd_md = NULL;
348360
}
349361

350-
uuid.type = p_char_obj->p_uuid->type;
351-
uuid.uuid = (uint16_t)(*(uint16_t *)&p_char_obj->p_uuid->value[0]);
362+
uuid.type = p_char_obj->p_uuid->type;
363+
uuid.uuid = p_char_obj->p_uuid->value[0];
364+
uuid.uuid += p_char_obj->p_uuid->value[1] << 8;
352365

353366
memset(&attr_md, 0, sizeof(attr_md));
354367

@@ -458,9 +471,9 @@ bool ble_drv_advertise_data(ubluepy_advertise_data_t * p_adv_params) {
458471
ubluepy_service_obj_t * p_service = (ubluepy_service_obj_t *)p_adv_params->p_services[i];
459472

460473
ble_uuid_t uuid;
461-
uuid.type = p_service->p_uuid->type;
462-
uuid.uuid = (uint16_t)(*(uint16_t *)&p_service->p_uuid->value[0]);
463-
474+
uuid.type = p_service->p_uuid->type;
475+
uuid.uuid = p_service->p_uuid->value[0];
476+
uuid.uuid += p_service->p_uuid->value[1] << 8;
464477
// calculate total size of uuids
465478
if (sd_ble_uuid_encode(&uuid, &encoded_size, NULL) != 0) {
466479
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
@@ -481,9 +494,10 @@ bool ble_drv_advertise_data(ubluepy_advertise_data_t * p_adv_params) {
481494

482495
uuid_total_size += encoded_size; // size of entry
483496
byte_pos += encoded_size; // relative to adv data packet
484-
BLE_DRIVER_LOG("ADV: uuid size: %u, type: %u, uuid: %u, vs_idx: %u\n",
497+
BLE_DRIVER_LOG("ADV: uuid size: %u, type: %u, uuid: %x%x, vs_idx: %u\n",
485498
encoded_size, p_service->p_uuid->type,
486-
(uint16_t)(*(uint16_t *)&p_service->p_uuid->value[0]),
499+
p_service->p_uuid->value[1],
500+
p_service->p_uuid->value[0],
487501
p_service->p_uuid->uuid_vs_idx);
488502
}
489503

@@ -506,8 +520,9 @@ bool ble_drv_advertise_data(ubluepy_advertise_data_t * p_adv_params) {
506520
ubluepy_service_obj_t * p_service = (ubluepy_service_obj_t *)p_adv_params->p_services[i];
507521

508522
ble_uuid_t uuid;
509-
uuid.type = p_service->p_uuid->uuid_vs_idx;
510-
uuid.uuid = (uint16_t)(*(uint16_t *)&p_service->p_uuid->value[0]);
523+
uuid.type = p_service->p_uuid->uuid_vs_idx;
524+
uuid.uuid = p_service->p_uuid->value[0];
525+
uuid.uuid += p_service->p_uuid->value[1] << 8;
511526

512527
// calculate total size of uuids
513528
if (sd_ble_uuid_encode(&uuid, &encoded_size, NULL) != 0) {
@@ -529,9 +544,10 @@ bool ble_drv_advertise_data(ubluepy_advertise_data_t * p_adv_params) {
529544

530545
uuid_total_size += encoded_size; // size of entry
531546
byte_pos += encoded_size; // relative to adv data packet
532-
BLE_DRIVER_LOG("ADV: uuid size: %u, type: %u, uuid: %u, vs_idx: %u\n",
547+
BLE_DRIVER_LOG("ADV: uuid size: %u, type: %x%x, uuid: %u, vs_idx: %u\n",
533548
encoded_size, p_service->p_uuid->type,
534-
(uint16_t)(*(uint16_t *)&p_service->p_uuid->value[0]),
549+
p_service->p_uuid->value[1],
550+
p_service->p_uuid->value[0],
535551
p_service->p_uuid->uuid_vs_idx);
536552
}
537553

@@ -556,10 +572,14 @@ bool ble_drv_advertise_data(ubluepy_advertise_data_t * p_adv_params) {
556572
m_adv_params.interval = MSEC_TO_UNITS(100, UNIT_0_625_MS); // approx 8 ms
557573
m_adv_params.timeout = 0; // infinite advertisment
558574

559-
if (m_adv_in_progress && sd_ble_gap_adv_stop() != 0) {
560-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
561-
"Can not stop advertisment."));
575+
#if (BLUETOOTH_SD == 132)
576+
if (m_adv_in_progress == true) {
577+
if (sd_ble_gap_adv_stop() != 0) {
578+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
579+
"Can not stop advertisment."));
580+
}
562581
}
582+
#endif
563583

564584
m_adv_in_progress = false;
565585
uint32_t err_code = sd_ble_gap_adv_start(&m_adv_params);
@@ -653,6 +673,7 @@ static void ble_evt_handler(ble_evt_t * p_ble_evt) {
653673
BLE_DRIVER_LOG("GAP CONNECT\n");
654674
m_adv_in_progress = false;
655675
ubluepy_gap_event_handler(mp_gap_observer, p_ble_evt->header.evt_id, p_ble_evt->evt.gap_evt.conn_handle, p_ble_evt->header.evt_len - (2 * sizeof(uint16_t)), NULL);
676+
656677
ble_gap_conn_params_t conn_params;
657678
(void)sd_ble_gap_ppcp_get(&conn_params);
658679
(void)sd_ble_gap_conn_param_update(p_ble_evt->evt.gap_evt.conn_handle, &conn_params);
@@ -670,7 +691,7 @@ static void ble_evt_handler(ble_evt_t * p_ble_evt) {
670691
case BLE_GATTS_EVT_WRITE:
671692
BLE_DRIVER_LOG("GATTS write\n");
672693

673-
uint16_t handle = p_ble_evt->evt.gatts_evt.params.write.handle;
694+
uint16_t handle = p_ble_evt->evt.gatts_evt.params.write.handle;
674695
uint16_t data_len = p_ble_evt->evt.gatts_evt.params.write.len;
675696
uint8_t * p_data = &p_ble_evt->evt.gatts_evt.params.write.data[0];
676697

@@ -686,16 +707,26 @@ static void ble_evt_handler(ble_evt_t * p_ble_evt) {
686707
(void)sd_ble_gatts_sys_attr_set(p_ble_evt->evt.gatts_evt.conn_handle, NULL, 0, 0);
687708
break;
688709

710+
#if (BLUETOOTH_SD == 132)
689711
case BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST:
690712
BLE_DRIVER_LOG("GATTS EVT EXCHANGE MTU REQUEST\n");
691713
(void)sd_ble_gatts_exchange_mtu_reply(p_ble_evt->evt.gatts_evt.conn_handle, 23); // MAX MTU size
692714
break;
715+
#endif
693716

694717
case BLE_EVT_TX_COMPLETE:
695718
BLE_DRIVER_LOG("BLE EVT TX COMPLETE\n");
696719
m_tx_in_progress = false;
697720
break;
698721

722+
case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
723+
BLE_DRIVER_LOG("BLE EVT SEC PARAMS REQUEST\n");
724+
// pairing not supported
725+
(void)sd_ble_gap_sec_params_reply(p_ble_evt->evt.gatts_evt.conn_handle,
726+
BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP,
727+
NULL, NULL);
728+
break;
729+
699730
default:
700731
BLE_DRIVER_LOG(">>> unhandled evt: 0x" HEX2_FMT "\n", p_ble_evt->header.evt_id);
701732
break;
File renamed without changes.

nrf5/sdk/ble_uart.c renamed to nrf5/bluetooth/ble_uart.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ static ubluepy_peripheral_obj_t ble_uart_peripheral = {
7474
.conn_handle = 0xFFFF,
7575
};
7676

77-
static bool cccd_enabled;
77+
static volatile bool m_cccd_enabled;
7878

7979
ringBuffer_typedef(uint8_t, ringbuffer_t);
8080

@@ -138,7 +138,7 @@ STATIC void gatts_event_handler(mp_obj_t self_in, uint16_t event_id, uint16_t at
138138

139139
if (event_id == 80) { // gatts write
140140
if (ble_uart_char_tx.cccd_handle == attr_handle) {
141-
cccd_enabled = true;
141+
m_cccd_enabled = true;
142142
} else if (ble_uart_char_rx.handle == attr_handle) {
143143
for (uint16_t i = 0; i < length; i++) {
144144
bufferWrite(mp_rx_ring_buffer, data[i]);
@@ -177,7 +177,6 @@ void ble_uart_init0(void) {
177177
mp_obj_list_append(ble_uart_service.char_list, MP_OBJ_FROM_PTR(&ble_uart_char_rx));
178178

179179
// setup the peripheral
180-
(void)ble_uart_peripheral;
181180
ble_uart_peripheral.service_list = mp_obj_new_list(0, NULL);
182181
mp_obj_list_append(ble_uart_peripheral.service_list, MP_OBJ_FROM_PTR(&ble_uart_service));
183182
ble_uart_service.p_periph = &ble_uart_peripheral;
@@ -206,7 +205,7 @@ void ble_uart_init0(void) {
206205
(void)device_name;
207206
(void)services;
208207

209-
cccd_enabled = false;
208+
m_cccd_enabled = false;
210209

211210
// initialize ring buffer
212211
m_rx_ring_buffer.size = sizeof(m_rx_ring_buffer_data) + 1;
@@ -216,7 +215,7 @@ void ble_uart_init0(void) {
216215

217216
(void)ble_drv_advertise_data(&adv_data);
218217

219-
while (cccd_enabled != true) {
218+
while (m_cccd_enabled != true) {
220219
;
221220
}
222221
}
File renamed without changes.

nrf5/bluetooth/bluetooth_common.mk

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
SOFTDEV_HEX_NAME ?=
3+
4+
ifeq ($(SD), s110)
5+
INC += -I$(SDK_ROOT)components/softdevice/$(SD)/headers
6+
CFLAGS += -DBLUETOOTH_SD_DEBUG=1
7+
CFLAGS += -DBLUETOOTH_SD=110
8+
SOFTDEV_HEX_NAME = s110_nrf51_8.0.0_softdevice.hex
9+
else ifeq ($(SD), s120)
10+
$(error No BLE wrapper available yet)
11+
else ifeq ($(SD), s130)
12+
$(error No BLE wrapper available yet)
13+
else ifeq ($(SD), s132)
14+
INC += -I$(SDK_ROOT)components/softdevice/$(SD)/headers
15+
INC += -I$(SDK_ROOT)components/softdevice/$(SD)/headers/$(MCU_VARIANT)
16+
CFLAGS += -DBLUETOOTH_SD_DEBUG=1
17+
CFLAGS += -DBLUETOOTH_SD=132
18+
SOFTDEV_HEX_NAME = s132_nrf52_3.0.0_softdevice.hex
19+
#else ifeq ($(SD), s1xx)
20+
# include bluetooth/iot_0.9.0/sdk.mk
21+
else
22+
$(error Incorrect softdevice set flag)
23+
endif
24+
25+
SOFTDEV_HEX = $(lastword $(wildcard $(SDK_ROOT)/components/softdevice/$(SD)/hex/$(SOFTDEV_HEX_NAME)))
26+
27+
INC += -I./bluetooth
28+
29+
SRC_C += \
30+
bluetooth/modble.c \
31+
bluetooth/ble_drv.c \
32+
bluetooth/ble_uart.c

nrf5/sdk/nrf5_sdk_conf.h renamed to nrf5/bluetooth/bluetooth_conf.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef NRF_SDK_CONF_H__
2-
#define NRF_SDK_CONF_H__
1+
#ifndef BLUETOOTH_CONF_H__
2+
#define BLUETOOTH_CONF_H__
33

44
// SD specific configurations.
55

nrf5/sdk/help_sd.h renamed to nrf5/bluetooth/help_sd.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#ifndef HELP_SD_H__
2828
#define HELP_SD_H__
2929

30-
#include "nrf5_sdk_conf.h"
30+
#include "bluetooth_conf.h"
3131

3232
#if MICROPY_PY_BLE
3333

File renamed without changes.

nrf5/sdk/iot_0.9.0/modnwble6lowpan.c renamed to nrf5/bluetooth/iot_0.9.0/modnwble6lowpan.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#include "nrf5_sdk_conf.h"
27+
#include "bluetooth_conf.h"
2828

2929
#if MICROPY_PY_BLE_6LOWPAN
3030

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

nrf5/sdk/ringbuffer.h renamed to nrf5/bluetooth/ringbuffer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@
6767
#define ringBuffer_typedef(T, NAME) \
6868
typedef struct { \
6969
int size; \
70-
int start; \
71-
int end; \
70+
volatile int start; \
71+
volatile int end; \
7272
T* elems; \
7373
} NAME
7474

nrf5/boards/microbit/mpconfigboard.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,13 @@
3030
#define MICROPY_HW_MCU_NAME "NRF51822"
3131
#define MICROPY_PY_SYS_PLATFORM "nrf51"
3232

33-
#define MICROPY_PY_MACHINE_HW_SPI (0)
33+
#define MICROPY_PY_MACHINE_HW_SPI (1)
3434
#define MICROPY_PY_MACHINE_PWM (0)
35-
#define MICROPY_PY_MACHINE_TIMER (0)
36-
#define MICROPY_PY_MACHINE_RTC (0)
35+
#define MICROPY_PY_MACHINE_TIMER (1)
36+
#define MICROPY_PY_MACHINE_RTC (1)
37+
#define MICROPY_PY_MACHINE_I2C (1)
38+
#define MICROPY_PY_MACHINE_ADC (1)
39+
#define MICROPY_PY_MACHINE_TEMP (1)
3740

3841
#define MICROPY_PY_USOCKET (0)
3942
#define MICROPY_PY_NETWORK (0)
@@ -63,4 +66,10 @@
6366
#define MICROPY_HW_UART1_TX (pin_A24)
6467
#define MICROPY_HW_UART1_HWFC (0)
6568

69+
// SPI0 config
70+
#define MICROPY_HW_SPI0_NAME "SPI0"
71+
#define MICROPY_HW_SPI0_SCK (pin_A13)
72+
#define MICROPY_HW_SPI0_MOSI (pin_A15)
73+
#define MICROPY_HW_SPI0_MISO (pin_A14)
74+
6675
#define HELP_TEXT_BOARD_LED "1,2,3,4"

nrf5/boards/microbit/mpconfigboard.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
MCU_SERIES = m0
22
MCU_VARIANT = nrf51
33
MCU_SUB_VARIANT = nrf51822
4-
LD_FILE = boards/$(MCU_SUB_VARIANT)_aa.ld
4+
LD_FILE = boards/nrf51x22_256k_16k.ld
55
FLASHER = pyocd
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
MCU_SERIES = m0
22
MCU_VARIANT = nrf51
33
MCU_SUB_VARIANT = nrf51822
4-
LD_FILE = boards/nrf51822_aa_s110.ld
4+
LD_FILE = boards/nrf51x22_256k_16k_s110.ld
55
FLASHER = pyocd
6+
7+
CFLAGS += -DBLUETOOTH_LFCLK_RC

nrf5/boards/microbit/nrf51_hal_conf.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
#define NRF51_HAL_CONF_H__
33

44
#define HAL_UART_MODULE_ENABLED
5-
// #define HAL_SPI_MODULE_ENABLED
5+
#define HAL_SPI_MODULE_ENABLED
66
#define HAL_TIME_MODULE_ENABLED
7-
// #define HAL_RTC_MODULE_ENABLED
8-
// #define HAL_TIMER_MODULE_ENABLED
7+
#define HAL_RTC_MODULE_ENABLED
8+
#define HAL_TIMER_MODULE_ENABLED
9+
#define HAL_TWI_MODULE_ENABLED
10+
#define HAL_ADC_MODULE_ENABLED
11+
#define HAL_TEMP_MODULE_ENABLED
912

1013
#endif // NRF51_HAL_CONF_H__
File renamed without changes.

0 commit comments

Comments
 (0)
0