From 76f5c0179829be979dc84708492fef21dcf886c0 Mon Sep 17 00:00:00 2001 From: arturo182 Date: Fri, 2 Feb 2018 21:01:01 +0100 Subject: [PATCH] nrf: Get the port working on pca10056 (nRF52840) --- ports/nrf/README.md | 2 +- ports/nrf/bluetooth_conf.h | 2 +- .../nrf52840_1M_256k_s140_6.0.0-6.alpha.ld | 28 +++ ports/nrf/boards/pca10056/board.c | 85 ++++++++ ports/nrf/boards/pca10056/mpconfigboard.h | 3 + .../nrf/boards/pca10056/mpconfigboard_s140.mk | 8 + ports/nrf/boards/pca10056/nrf52_hal_conf.h | 4 +- ports/nrf/boards/pca10056/pins.c | 191 ++++++++++++++++++ ports/nrf/boards/pca10056/pins.h | 51 +++++ ports/nrf/common-hal/busio/SPI.c | 7 + ports/nrf/drivers/bluetooth/ble_drv.c | 102 +++++++++- .../nrf/drivers/bluetooth/bluetooth_common.mk | 8 + 12 files changed, 480 insertions(+), 11 deletions(-) create mode 100644 ports/nrf/boards/nrf52840_1M_256k_s140_6.0.0-6.alpha.ld create mode 100644 ports/nrf/boards/pca10056/board.c create mode 100644 ports/nrf/boards/pca10056/mpconfigboard_s140.mk create mode 100644 ports/nrf/boards/pca10056/pins.c create mode 100644 ports/nrf/boards/pca10056/pins.h diff --git a/ports/nrf/README.md b/ports/nrf/README.md index d1c292e7d0..2cfa52c39d 100644 --- a/ports/nrf/README.md +++ b/ports/nrf/README.md @@ -85,7 +85,7 @@ wt51822_s4at | s110 | Peripheral | Manual pca10040 | s132 | Peripheral and Central | [Segger](#segger-targets) feather52 | s132 | Peripheral and Central | [UART DFU](#dfu-targets) arduino_primo | s132 | Peripheral and Central | [PyOCD](#pyocdopenocd-targets) -pca10056 | | | [Segger](#segger-targets) +pca10056 | s140 | Peripheral | [Segger](#segger-targets) ## Segger Targets diff --git a/ports/nrf/bluetooth_conf.h b/ports/nrf/bluetooth_conf.h index 6a3cbdc83e..94a45eaca9 100644 --- a/ports/nrf/bluetooth_conf.h +++ b/ports/nrf/bluetooth_conf.h @@ -11,7 +11,7 @@ #define MICROPY_PY_UBLUEPY (1) #define MICROPY_PY_UBLUEPY_PERIPHERAL (1) -#elif (BLUETOOTH_SD == 132) +#elif (BLUETOOTH_SD == 132 || BLUETOOTH_SD == 140) #define MICROPY_PY_BLE (1) #define MICROPY_PY_BLE_NUS (0) diff --git a/ports/nrf/boards/nrf52840_1M_256k_s140_6.0.0-6.alpha.ld b/ports/nrf/boards/nrf52840_1M_256k_s140_6.0.0-6.alpha.ld new file mode 100644 index 0000000000..be2a9a75af --- /dev/null +++ b/ports/nrf/boards/nrf52840_1M_256k_s140_6.0.0-6.alpha.ld @@ -0,0 +1,28 @@ +/* + GNU linker script for NRF52840 w/ s140 6.0.0-6.alpha SoftDevice +*/ + +/* Specify the memory areas */ +MEMORY +{ + FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x100000 /* entire flash, 1 MiB */ + FLASH_ISR (rx) : ORIGIN = 0x00025000, LENGTH = 0x001000 /* sector 0, 4 KiB */ + FLASH_TEXT (rx) : ORIGIN = 0x00026000, LENGTH = 0x09A000 /* 616 KiB */ + FLASH_FATFS (r) : ORIGIN = 0x000C0000, LENGTH = 0x040000 /* File system 256 KiB */ + RAM (xrw) : ORIGIN = 0x20004000, LENGTH = 0x03C000 /* 240 KiB */ +} + +/* produce a link error if there is not this amount of RAM for these sections */ +_minimum_stack_size = 40K; +_minimum_heap_size = 0; + +/* top end of the stack */ + +/*_stack_end = ORIGIN(RAM) + LENGTH(RAM);*/ +_estack = ORIGIN(RAM) + LENGTH(RAM); + +/* RAM extents for the garbage collector */ +_ram_end = ORIGIN(RAM) + LENGTH(RAM); +_heap_end = 0x20020000; /* tunable */ + +INCLUDE "boards/common.ld" diff --git a/ports/nrf/boards/pca10056/board.c b/ports/nrf/boards/pca10056/board.c new file mode 100644 index 0000000000..8ca87759b4 --- /dev/null +++ b/ports/nrf/boards/pca10056/board.c @@ -0,0 +1,85 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include + +#include "nrf.h" + +#include "boards/board.h" + +volatile uint32_t ticks_ms = 0; + +#define HAL_LFCLK_FREQ (32768UL) +#define HAL_RTC_FREQ (1024UL) +#define HAL_RTC_COUNTER_PRESCALER ((HAL_LFCLK_FREQ/HAL_RTC_FREQ)-1) + +/* Maximum RTC ticks */ +#define portNRF_RTC_MAXTICKS ((1U<<24)-1U) + +void board_init(void) +{ + // 32Khz XTAL + NRF_CLOCK->LFCLKSRC = (uint32_t)((CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos) & CLOCK_LFCLKSRC_SRC_Msk); + NRF_CLOCK->TASKS_LFCLKSTART = 1UL; + + // Set up RTC1 as tick timer + NVIC_DisableIRQ(RTC1_IRQn); + NRF_RTC1->EVTENCLR = RTC_EVTEN_COMPARE0_Msk; + NRF_RTC1->INTENCLR = RTC_INTENSET_COMPARE0_Msk; + NRF_RTC1->TASKS_STOP = 1; + NRF_RTC1->TASKS_CLEAR = 1; + + ticks_ms = 0; + + NRF_RTC1->PRESCALER = HAL_RTC_COUNTER_PRESCALER; + NRF_RTC1->INTENSET = RTC_INTENSET_TICK_Msk; + NRF_RTC1->TASKS_START = 1; + NRF_RTC1->EVTENSET = RTC_EVTEN_OVRFLW_Msk; + NVIC_SetPriority(RTC1_IRQn, 0xf); // lowest priority + NVIC_EnableIRQ(RTC1_IRQn); +} + +void RTC1_IRQHandler(void) +{ + // Clear event + NRF_RTC1->EVENTS_TICK = 0; + volatile uint32_t dummy = NRF_RTC1->EVENTS_TICK; + (void) dummy; + // + //// Tick correction + uint32_t systick_counter = NRF_RTC1->COUNTER; + uint32_t diff = (systick_counter - ticks_ms) & portNRF_RTC_MAXTICKS; + ticks_ms += diff; +} + +bool board_requests_safe_mode(void) { + return false; +} + +void reset_board(void) { + +} diff --git a/ports/nrf/boards/pca10056/mpconfigboard.h b/ports/nrf/boards/pca10056/mpconfigboard.h index dc16f65674..3d3cf5fbd5 100644 --- a/ports/nrf/boards/pca10056/mpconfigboard.h +++ b/ports/nrf/boards/pca10056/mpconfigboard.h @@ -82,3 +82,6 @@ #endif #define HELP_TEXT_BOARD_LED "1,2,3,4" + +#define PORT_HEAP_SIZE (128*1024) +#define CIRCUITPY_AUTORELOAD_DELAY_MS 500 diff --git a/ports/nrf/boards/pca10056/mpconfigboard_s140.mk b/ports/nrf/boards/pca10056/mpconfigboard_s140.mk new file mode 100644 index 0000000000..fbff1f6d8b --- /dev/null +++ b/ports/nrf/boards/pca10056/mpconfigboard_s140.mk @@ -0,0 +1,8 @@ +MCU_SERIES = m4 +MCU_VARIANT = nrf52 +MCU_SUB_VARIANT = nrf52840 +SOFTDEV_VERSION ?= 6.0.0-6.alpha + +LD_FILE = boards/nrf52840_1M_256k_s140_$(SOFTDEV_VERSION).ld + +NRF_DEFINES += -DNRF52840_XXAA diff --git a/ports/nrf/boards/pca10056/nrf52_hal_conf.h b/ports/nrf/boards/pca10056/nrf52_hal_conf.h index 0f42e8975b..fd6073a187 100644 --- a/ports/nrf/boards/pca10056/nrf52_hal_conf.h +++ b/ports/nrf/boards/pca10056/nrf52_hal_conf.h @@ -1,7 +1,7 @@ #ifndef NRF52_HAL_CONF_H__ #define NRF52_HAL_CONF_H__ -// #define HAL_UART_MODULE_ENABLED +#define HAL_UART_MODULE_ENABLED #define HAL_SPI_MODULE_ENABLED #define HAL_TIME_MODULE_ENABLED #define HAL_PWM_MODULE_ENABLED @@ -11,7 +11,7 @@ #define HAL_ADCE_MODULE_ENABLED #define HAL_TEMP_MODULE_ENABLED #define HAL_RNG_MODULE_ENABLED -#define HAL_UARTE_MODULE_ENABLED +// #define HAL_UARTE_MODULE_ENABLED // #define HAL_SPIE_MODULE_ENABLED // #define HAL_TWIE_MODULE_ENABLED diff --git a/ports/nrf/boards/pca10056/pins.c b/ports/nrf/boards/pca10056/pins.c new file mode 100644 index 0000000000..30e9a365c8 --- /dev/null +++ b/ports/nrf/boards/pca10056/pins.c @@ -0,0 +1,191 @@ +// This file was automatically generated by make-pins.py +// +// --af nrf52_af.csv +// --board boards/pca10056/pins.csv +// --prefix boards/nrf52_prefix.c + +// nrf52_prefix.c becomes the initial portion of the generated pins file. + +#include + +#include "py/obj.h" +#include "py/mphal.h" +#include "pin.h" + +#define AF(af_idx, af_fn, af_unit, af_type, af_ptr) \ +{ \ + { &pin_af_type }, \ + .name = MP_QSTR_AF ## af_idx ## _ ## af_fn ## af_unit, \ + .idx = (af_idx), \ + .fn = AF_FN_ ## af_fn, \ + .unit = (af_unit), \ + .type = AF_PIN_TYPE_ ## af_fn ## _ ## af_type, \ + .af_fn = (af_ptr) \ +} + +#define PIN(p_port, p_pin, p_af, p_adc_channel) \ +{ \ + { &mcu_pin_type }, \ + .name = MP_QSTR_ ## p_port ## p_pin, \ + .port = PORT_ ## p_port, \ + .pin = (p_pin), \ + .num_af = (sizeof(p_af) / sizeof(pin_af_obj_t)), \ + /*.pin_mask = (1 << p_pin), */\ + .af = p_af, \ + .adc_channel = p_adc_channel,\ +} + +#define NO_ADC 0 + +const pin_obj_t pin_A0 = PIN(A, 0, NULL, 0); +const pin_obj_t pin_A1 = PIN(A, 1, NULL, 0); +const pin_obj_t pin_A2 = PIN(A, 2, NULL, SAADC_CH_PSELP_PSELP_AnalogInput0); +const pin_obj_t pin_A3 = PIN(A, 3, NULL, SAADC_CH_PSELP_PSELP_AnalogInput1); +const pin_obj_t pin_A4 = PIN(A, 4, NULL, SAADC_CH_PSELP_PSELP_AnalogInput2); +const pin_obj_t pin_A5 = PIN(A, 5, NULL, SAADC_CH_PSELP_PSELP_AnalogInput3); +const pin_obj_t pin_A6 = PIN(A, 6, NULL, 0); +const pin_obj_t pin_A7 = PIN(A, 7, NULL, 0); +const pin_obj_t pin_A8 = PIN(A, 8, NULL, 0); +const pin_obj_t pin_A9 = PIN(A, 9, NULL, 0); +const pin_obj_t pin_A10 = PIN(A, 10, NULL, 0); +const pin_obj_t pin_A11 = PIN(A, 11, NULL, 0); +const pin_obj_t pin_A12 = PIN(A, 12, NULL, 0); +const pin_obj_t pin_A13 = PIN(A, 13, NULL, 0); +const pin_obj_t pin_A14 = PIN(A, 14, NULL, 0); +const pin_obj_t pin_A15 = PIN(A, 15, NULL, 0); +const pin_obj_t pin_A16 = PIN(A, 16, NULL, 0); +const pin_obj_t pin_A17 = PIN(A, 17, NULL, 0); +const pin_obj_t pin_A18 = PIN(A, 18, NULL, 0); +const pin_obj_t pin_A19 = PIN(A, 19, NULL, 0); +const pin_obj_t pin_A20 = PIN(A, 20, NULL, 0); +const pin_obj_t pin_A21 = PIN(A, 21, NULL, 0); +const pin_obj_t pin_A22 = PIN(A, 22, NULL, 0); +const pin_obj_t pin_A23 = PIN(A, 23, NULL, 0); +const pin_obj_t pin_A24 = PIN(A, 24, NULL, 0); +const pin_obj_t pin_A25 = PIN(A, 25, NULL, 0); +const pin_obj_t pin_A26 = PIN(A, 26, NULL, 0); +const pin_obj_t pin_A27 = PIN(A, 27, NULL, 0); +const pin_obj_t pin_A28 = PIN(A, 28, NULL, SAADC_CH_PSELP_PSELP_AnalogInput4); +const pin_obj_t pin_A29 = PIN(A, 29, NULL, SAADC_CH_PSELP_PSELP_AnalogInput5); +const pin_obj_t pin_A30 = PIN(A, 30, NULL, SAADC_CH_PSELP_PSELP_AnalogInput6); +const pin_obj_t pin_A31 = PIN(A, 31, NULL, SAADC_CH_PSELP_PSELP_AnalogInput7); +const pin_obj_t pin_B0 = PIN(B, 0, NULL, 0); +const pin_obj_t pin_B1 = PIN(B, 1, NULL, 0); +const pin_obj_t pin_B2 = PIN(B, 2, NULL, 0); +const pin_obj_t pin_B3 = PIN(B, 3, NULL, 0); +const pin_obj_t pin_B4 = PIN(B, 4, NULL, 0); +const pin_obj_t pin_B5 = PIN(B, 5, NULL, 0); +const pin_obj_t pin_B6 = PIN(B, 6, NULL, 0); +const pin_obj_t pin_B7 = PIN(B, 7, NULL, 0); +const pin_obj_t pin_B8 = PIN(B, 8, NULL, 0); +const pin_obj_t pin_B9 = PIN(B, 9, NULL, 0); +const pin_obj_t pin_B10 = PIN(B, 10, NULL, 0); +const pin_obj_t pin_B11 = PIN(B, 11, NULL, 0); +const pin_obj_t pin_B12 = PIN(B, 12, NULL, 0); +const pin_obj_t pin_B13 = PIN(B, 13, NULL, 0); +const pin_obj_t pin_B14 = PIN(B, 14, NULL, 0); +const pin_obj_t pin_B15 = PIN(B, 15, NULL, 0); + +STATIC const mp_rom_map_elem_t mcu_pin_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR_A0), MP_ROM_PTR(&pin_A0) }, + { MP_ROM_QSTR(MP_QSTR_A1), MP_ROM_PTR(&pin_A1) }, + { MP_ROM_QSTR(MP_QSTR_A2), MP_ROM_PTR(&pin_A2) }, + { MP_ROM_QSTR(MP_QSTR_A3), MP_ROM_PTR(&pin_A3) }, + { MP_ROM_QSTR(MP_QSTR_A4), MP_ROM_PTR(&pin_A4) }, + { MP_ROM_QSTR(MP_QSTR_A5), MP_ROM_PTR(&pin_A5) }, + { MP_ROM_QSTR(MP_QSTR_A6), MP_ROM_PTR(&pin_A6) }, + { MP_ROM_QSTR(MP_QSTR_A7), MP_ROM_PTR(&pin_A7) }, + { MP_ROM_QSTR(MP_QSTR_A8), MP_ROM_PTR(&pin_A8) }, + { MP_ROM_QSTR(MP_QSTR_A9), MP_ROM_PTR(&pin_A9) }, + { MP_ROM_QSTR(MP_QSTR_A10), MP_ROM_PTR(&pin_A10) }, + { MP_ROM_QSTR(MP_QSTR_A11), MP_ROM_PTR(&pin_A11) }, + { MP_ROM_QSTR(MP_QSTR_A12), MP_ROM_PTR(&pin_A12) }, + { MP_ROM_QSTR(MP_QSTR_A13), MP_ROM_PTR(&pin_A13) }, + { MP_ROM_QSTR(MP_QSTR_A14), MP_ROM_PTR(&pin_A14) }, + { MP_ROM_QSTR(MP_QSTR_A15), MP_ROM_PTR(&pin_A15) }, + { MP_ROM_QSTR(MP_QSTR_A16), MP_ROM_PTR(&pin_A16) }, + { MP_ROM_QSTR(MP_QSTR_A17), MP_ROM_PTR(&pin_A17) }, + { MP_ROM_QSTR(MP_QSTR_A18), MP_ROM_PTR(&pin_A18) }, + { MP_ROM_QSTR(MP_QSTR_A19), MP_ROM_PTR(&pin_A19) }, + { MP_ROM_QSTR(MP_QSTR_A20), MP_ROM_PTR(&pin_A20) }, + { MP_ROM_QSTR(MP_QSTR_A21), MP_ROM_PTR(&pin_A21) }, + { MP_ROM_QSTR(MP_QSTR_A22), MP_ROM_PTR(&pin_A22) }, + { MP_ROM_QSTR(MP_QSTR_A23), MP_ROM_PTR(&pin_A23) }, + { MP_ROM_QSTR(MP_QSTR_A24), MP_ROM_PTR(&pin_A24) }, + { MP_ROM_QSTR(MP_QSTR_A25), MP_ROM_PTR(&pin_A25) }, + { MP_ROM_QSTR(MP_QSTR_A26), MP_ROM_PTR(&pin_A26) }, + { MP_ROM_QSTR(MP_QSTR_A27), MP_ROM_PTR(&pin_A27) }, + { MP_ROM_QSTR(MP_QSTR_A28), MP_ROM_PTR(&pin_A28) }, + { MP_ROM_QSTR(MP_QSTR_A29), MP_ROM_PTR(&pin_A29) }, + { MP_ROM_QSTR(MP_QSTR_A30), MP_ROM_PTR(&pin_A30) }, + { MP_ROM_QSTR(MP_QSTR_A31), MP_ROM_PTR(&pin_A31) }, + { MP_ROM_QSTR(MP_QSTR_B0), MP_ROM_PTR(&pin_B0) }, + { MP_ROM_QSTR(MP_QSTR_B1), MP_ROM_PTR(&pin_B1) }, + { MP_ROM_QSTR(MP_QSTR_B2), MP_ROM_PTR(&pin_B2) }, + { MP_ROM_QSTR(MP_QSTR_B3), MP_ROM_PTR(&pin_B3) }, + { MP_ROM_QSTR(MP_QSTR_B4), MP_ROM_PTR(&pin_B4) }, + { MP_ROM_QSTR(MP_QSTR_B5), MP_ROM_PTR(&pin_B5) }, + { MP_ROM_QSTR(MP_QSTR_B6), MP_ROM_PTR(&pin_B6) }, + { MP_ROM_QSTR(MP_QSTR_B7), MP_ROM_PTR(&pin_B7) }, + { MP_ROM_QSTR(MP_QSTR_B8), MP_ROM_PTR(&pin_B8) }, + { MP_ROM_QSTR(MP_QSTR_B9), MP_ROM_PTR(&pin_B9) }, + { MP_ROM_QSTR(MP_QSTR_B10), MP_ROM_PTR(&pin_B10) }, + { MP_ROM_QSTR(MP_QSTR_B11), MP_ROM_PTR(&pin_B11) }, + { MP_ROM_QSTR(MP_QSTR_B12), MP_ROM_PTR(&pin_B12) }, + { MP_ROM_QSTR(MP_QSTR_B13), MP_ROM_PTR(&pin_B13) }, + { MP_ROM_QSTR(MP_QSTR_B14), MP_ROM_PTR(&pin_B14) }, + { MP_ROM_QSTR(MP_QSTR_B15), MP_ROM_PTR(&pin_B15) }, +}; +MP_DEFINE_CONST_DICT(mcu_pin_globals, mcu_pin_globals_table); + +STATIC const mp_rom_map_elem_t board_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR_PA0), MP_ROM_PTR(&pin_A0) }, + { MP_ROM_QSTR(MP_QSTR_PA1), MP_ROM_PTR(&pin_A1) }, + { MP_ROM_QSTR(MP_QSTR_PA2), MP_ROM_PTR(&pin_A2) }, + { MP_ROM_QSTR(MP_QSTR_PA3), MP_ROM_PTR(&pin_A3) }, + { MP_ROM_QSTR(MP_QSTR_PA4), MP_ROM_PTR(&pin_A4) }, + { MP_ROM_QSTR(MP_QSTR_PA5), MP_ROM_PTR(&pin_A5) }, + { MP_ROM_QSTR(MP_QSTR_PA6), MP_ROM_PTR(&pin_A6) }, + { MP_ROM_QSTR(MP_QSTR_PA7), MP_ROM_PTR(&pin_A7) }, + { MP_ROM_QSTR(MP_QSTR_PA8), MP_ROM_PTR(&pin_A8) }, + { MP_ROM_QSTR(MP_QSTR_PA9), MP_ROM_PTR(&pin_A9) }, + { MP_ROM_QSTR(MP_QSTR_PA10), MP_ROM_PTR(&pin_A10) }, + { MP_ROM_QSTR(MP_QSTR_PA11), MP_ROM_PTR(&pin_A11) }, + { MP_ROM_QSTR(MP_QSTR_PA12), MP_ROM_PTR(&pin_A12) }, + { MP_ROM_QSTR(MP_QSTR_PA13), MP_ROM_PTR(&pin_A13) }, + { MP_ROM_QSTR(MP_QSTR_PA14), MP_ROM_PTR(&pin_A14) }, + { MP_ROM_QSTR(MP_QSTR_PA15), MP_ROM_PTR(&pin_A15) }, + { MP_ROM_QSTR(MP_QSTR_PA16), MP_ROM_PTR(&pin_A16) }, + { MP_ROM_QSTR(MP_QSTR_PA17), MP_ROM_PTR(&pin_A17) }, + { MP_ROM_QSTR(MP_QSTR_PA18), MP_ROM_PTR(&pin_A18) }, + { MP_ROM_QSTR(MP_QSTR_PA19), MP_ROM_PTR(&pin_A19) }, + { MP_ROM_QSTR(MP_QSTR_PA20), MP_ROM_PTR(&pin_A20) }, + { MP_ROM_QSTR(MP_QSTR_PA21), MP_ROM_PTR(&pin_A21) }, + { MP_ROM_QSTR(MP_QSTR_PA22), MP_ROM_PTR(&pin_A22) }, + { MP_ROM_QSTR(MP_QSTR_PA23), MP_ROM_PTR(&pin_A23) }, + { MP_ROM_QSTR(MP_QSTR_PA24), MP_ROM_PTR(&pin_A24) }, + { MP_ROM_QSTR(MP_QSTR_PA25), MP_ROM_PTR(&pin_A25) }, + { MP_ROM_QSTR(MP_QSTR_PA26), MP_ROM_PTR(&pin_A26) }, + { MP_ROM_QSTR(MP_QSTR_PA27), MP_ROM_PTR(&pin_A27) }, + { MP_ROM_QSTR(MP_QSTR_PA28), MP_ROM_PTR(&pin_A28) }, + { MP_ROM_QSTR(MP_QSTR_PA29), MP_ROM_PTR(&pin_A29) }, + { MP_ROM_QSTR(MP_QSTR_PA30), MP_ROM_PTR(&pin_A30) }, + { MP_ROM_QSTR(MP_QSTR_PA31), MP_ROM_PTR(&pin_A31) }, + { MP_ROM_QSTR(MP_QSTR_PB0), MP_ROM_PTR(&pin_B0) }, + { MP_ROM_QSTR(MP_QSTR_PB1), MP_ROM_PTR(&pin_B1) }, + { MP_ROM_QSTR(MP_QSTR_PB2), MP_ROM_PTR(&pin_B2) }, + { MP_ROM_QSTR(MP_QSTR_PB3), MP_ROM_PTR(&pin_B3) }, + { MP_ROM_QSTR(MP_QSTR_PB4), MP_ROM_PTR(&pin_B4) }, + { MP_ROM_QSTR(MP_QSTR_PB5), MP_ROM_PTR(&pin_B5) }, + { MP_ROM_QSTR(MP_QSTR_PB6), MP_ROM_PTR(&pin_B6) }, + { MP_ROM_QSTR(MP_QSTR_PB7), MP_ROM_PTR(&pin_B7) }, + { MP_ROM_QSTR(MP_QSTR_PB8), MP_ROM_PTR(&pin_B8) }, + { MP_ROM_QSTR(MP_QSTR_PB9), MP_ROM_PTR(&pin_B9) }, + { MP_ROM_QSTR(MP_QSTR_PB10), MP_ROM_PTR(&pin_B10) }, + { MP_ROM_QSTR(MP_QSTR_PB11), MP_ROM_PTR(&pin_B11) }, + { MP_ROM_QSTR(MP_QSTR_PB12), MP_ROM_PTR(&pin_B12) }, + { MP_ROM_QSTR(MP_QSTR_PB13), MP_ROM_PTR(&pin_B13) }, + { MP_ROM_QSTR(MP_QSTR_PB14), MP_ROM_PTR(&pin_B14) }, + { MP_ROM_QSTR(MP_QSTR_PB15), MP_ROM_PTR(&pin_B15) }, +}; +MP_DEFINE_CONST_DICT(board_module_globals, board_module_globals_table); diff --git a/ports/nrf/boards/pca10056/pins.h b/ports/nrf/boards/pca10056/pins.h new file mode 100644 index 0000000000..0ea1f0450a --- /dev/null +++ b/ports/nrf/boards/pca10056/pins.h @@ -0,0 +1,51 @@ +extern const pin_obj_t pin_A0; +extern const pin_obj_t pin_A1; +extern const pin_obj_t pin_A2; +extern const pin_obj_t pin_A3; +extern const pin_obj_t pin_A4; +extern const pin_obj_t pin_A5; +extern const pin_obj_t pin_A6; +extern const pin_obj_t pin_A7; +extern const pin_obj_t pin_A8; +extern const pin_obj_t pin_A9; +extern const pin_obj_t pin_A10; +extern const pin_obj_t pin_A11; +extern const pin_obj_t pin_A12; +extern const pin_obj_t pin_A13; +extern const pin_obj_t pin_A14; +extern const pin_obj_t pin_A15; +extern const pin_obj_t pin_A16; +extern const pin_obj_t pin_A17; +extern const pin_obj_t pin_A18; +extern const pin_obj_t pin_A19; +extern const pin_obj_t pin_A20; +extern const pin_obj_t pin_A21; +extern const pin_obj_t pin_A22; +extern const pin_obj_t pin_A23; +extern const pin_obj_t pin_A24; +extern const pin_obj_t pin_A25; +extern const pin_obj_t pin_A26; +extern const pin_obj_t pin_A27; +extern const pin_obj_t pin_A28; +extern const pin_obj_t pin_A29; +extern const pin_obj_t pin_A30; +extern const pin_obj_t pin_A31; +extern const pin_obj_t pin_B0; +extern const pin_obj_t pin_B1; +extern const pin_obj_t pin_B2; +extern const pin_obj_t pin_B3; +extern const pin_obj_t pin_B4; +extern const pin_obj_t pin_B5; +extern const pin_obj_t pin_B6; +extern const pin_obj_t pin_B7; +extern const pin_obj_t pin_B8; +extern const pin_obj_t pin_B9; +extern const pin_obj_t pin_B10; +extern const pin_obj_t pin_B11; +extern const pin_obj_t pin_B12; +extern const pin_obj_t pin_B13; +extern const pin_obj_t pin_B14; +extern const pin_obj_t pin_B15; +extern const pin_obj_t * const pin_adc1[]; +extern const pin_obj_t * const pin_adc2[]; +extern const pin_obj_t * const pin_adc3[]; diff --git a/ports/nrf/common-hal/busio/SPI.c b/ports/nrf/common-hal/busio/SPI.c index efce3568ed..62245252ef 100644 --- a/ports/nrf/common-hal/busio/SPI.c +++ b/ports/nrf/common-hal/busio/SPI.c @@ -58,6 +58,13 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self, const mcu_pin_obj_t * self->spi->PSELSCK = clock->pin; self->spi->PSELMOSI = mosi->pin; self->spi->PSELMISO = miso->pin; + + +#if NRF52840_XXAA + self->spi->PSELSCK |= (clock->port << SPI_PSEL_SCK_PORT_Pos); + self->spi->PSELMOSI |= (mosi->port << SPI_PSEL_MOSI_PORT_Pos); + self->spi->PSELMISO |= (miso->port << SPI_PSEL_MISO_PORT_Pos); +#endif } bool common_hal_busio_spi_deinited(busio_spi_obj_t *self) { diff --git a/ports/nrf/drivers/bluetooth/ble_drv.c b/ports/nrf/drivers/bluetooth/ble_drv.c index 2bb6fac2d2..8fe8e8771c 100644 --- a/ports/nrf/drivers/bluetooth/ble_drv.c +++ b/ports/nrf/drivers/bluetooth/ble_drv.c @@ -74,7 +74,7 @@ static ble_drv_gatts_evt_callback_t gatts_event_handler; static mp_obj_t mp_gap_observer; static mp_obj_t mp_gatts_observer; -#if (BLUETOOTH_SD == 130) || (BLUETOOTH_SD == 132) +#if (BLUETOOTH_SD == 130) || (BLUETOOTH_SD == 132) || (BLUETOOTH_SD == 140) static volatile bool m_primary_service_found; static volatile bool m_characteristic_found; static volatile bool m_write_done; @@ -128,14 +128,22 @@ uint32_t ble_drv_stack_enable(void) { .source = NRF_CLOCK_LF_SRC_RC, .rc_ctiv = 16, .rc_temp_ctiv = 2, +#if (BLUETOOTH_SD == 140) + .accuracy = 0 +#else .xtal_accuracy = 0 +#endif }; #else nrf_clock_lf_cfg_t clock_config = { .source = NRF_CLOCK_LF_SRC_XTAL, .rc_ctiv = 0, .rc_temp_ctiv = 0, +#if (BLUETOOTH_SD == 140) + .accuracy = NRF_CLOCK_LF_ACCURACY_20_PPM +#else .xtal_accuracy = NRF_CLOCK_LF_XTAL_ACCURACY_20_PPM +#endif }; #endif uint32_t err_code = sd_softdevice_enable(&clock_config, @@ -153,6 +161,7 @@ uint32_t ble_drv_stack_enable(void) { BLE_DRIVER_LOG("IRQ enable status: " UINT_FMT "\n", (uint16_t)err_code); // Enable BLE stack. +#if (BLUETOOTH_SD != 140) ble_enable_params_t ble_enable_params; memset(&ble_enable_params, 0x00, sizeof(ble_enable_params)); ble_enable_params.gatts_enable_params.attr_tab_size = BLE_GATTS_ATTR_TAB_SIZE_DEFAULT; @@ -161,7 +170,7 @@ uint32_t ble_drv_stack_enable(void) { ble_enable_params.gap_enable_params.periph_conn_count = 1; ble_enable_params.gap_enable_params.central_conn_count = 1; #endif - +#endif #if (BLUETOOTH_SD == 100) || (BLUETOOTH_SD == 110) err_code = sd_ble_enable(&ble_enable_params); @@ -171,6 +180,10 @@ uint32_t ble_drv_stack_enable(void) { uint32_t app_ram_start = 0x200039c0; err_code = sd_ble_enable(&ble_enable_params, &app_ram_start); // 8K SD headroom from linker script. BLE_DRIVER_LOG("BLE ram size: " UINT_FMT "\n", (uint16_t)app_ram_start); +#elif (BLUETOOTH_SD == 140) + uint32_t app_ram_start = 0x20004000; + err_code = sd_ble_enable(&app_ram_start); + BLE_DRIVER_LOG("BLE ram size: " UINT_FMT "\n", (uint16_t)app_ram_start); #else err_code = sd_ble_enable(&ble_enable_params, (uint32_t *)0x20001870); #endif @@ -229,7 +242,7 @@ void ble_drv_address_get(ble_drv_addr_t * p_addr) { SD_TEST_OR_ENABLE(); ble_gap_addr_t local_ble_addr; -#if (BLUETOOTH_SD == 132 && BLE_API_VERSION == 3) +#if (BLUETOOTH_SD == 132 && BLE_API_VERSION == 3) || (BLUETOOTH_SD == 140) uint32_t err_code = sd_ble_gap_addr_get(&local_ble_addr); #else uint32_t err_code = sd_ble_gap_address_get(&local_ble_addr); @@ -352,7 +365,11 @@ bool ble_drv_characteristic_add(ubluepy_characteristic_obj_t * p_char_obj) { attr_char_value.p_attr_md = &attr_md; attr_char_value.init_len = sizeof(uint8_t); attr_char_value.init_offs = 0; +#if (BLUETOOTH_SD == 140) + attr_char_value.max_len = (BLE_GATT_ATT_MTU_DEFAULT - 3); +#else attr_char_value.max_len = (GATT_MTU_SIZE_DEFAULT - 3); +#endif ble_gatts_char_handles_t handles; @@ -378,7 +395,11 @@ bool ble_drv_advertise_data(ubluepy_advertise_data_t * p_adv_params) { uint8_t byte_pos = 0; +#if (BLUETOOTH_SD == 140) + uint8_t adv_data[BLE_GAP_ADV_SR_MAX_LEN_DEFAULT]; +#else uint8_t adv_data[BLE_GAP_ADV_MAX_SIZE]; +#endif if (p_adv_params->device_name_len > 0) { ble_gap_conn_sec_mode_t sec_mode; @@ -532,7 +553,11 @@ bool ble_drv_advertise_data(ubluepy_advertise_data_t * p_adv_params) { } if ((p_adv_params->data_len > 0) && (p_adv_params->p_data != NULL)) { +#if (BLUETOOTH_SD == 140) + if (p_adv_params->data_len + byte_pos > BLE_GAP_ADV_SR_MAX_LEN_DEFAULT) { +#else if (p_adv_params->data_len + byte_pos > BLE_GAP_ADV_MAX_SIZE) { +#endif nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "Can not fit data into the advertisment packet.")); } @@ -543,7 +568,16 @@ bool ble_drv_advertise_data(ubluepy_advertise_data_t * p_adv_params) { // scan response data not set uint32_t err_code; +#if (BLUETOOTH_SD == 140) + const ble_data_t ble_adv_data = { + .p_data = adv_data, + .len = byte_pos + }; + + if ((err_code = sd_ble_gap_adv_data_set(BLE_GAP_ADV_SET_HANDLE_DEFAULT, &ble_adv_data, NULL)) != 0) { +#else if ((err_code = sd_ble_gap_adv_data_set(adv_data, byte_pos, NULL, 0)) != 0) { +#endif nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "Can not apply advertisment data. status: 0x" HEX2_FMT, (uint16_t)err_code)); } @@ -554,19 +588,38 @@ bool ble_drv_advertise_data(ubluepy_advertise_data_t * p_adv_params) { // initialize advertising params memset(&m_adv_params, 0, sizeof(m_adv_params)); if (p_adv_params->connectable) { +#if (BLUETOOTH_SD == 140) + m_adv_params.properties.connectable = 1; + m_adv_params.properties.scannable = 1; +#else m_adv_params.type = BLE_GAP_ADV_TYPE_ADV_IND; +#endif } else { +#if (BLUETOOTH_SD == 140) + m_adv_params.properties.connectable = 0; +#else m_adv_params.type = BLE_GAP_ADV_TYPE_ADV_NONCONN_IND; +#endif } m_adv_params.p_peer_addr = NULL; // undirected advertisement m_adv_params.fp = BLE_GAP_ADV_FP_ANY; m_adv_params.interval = MSEC_TO_UNITS(100, UNIT_0_625_MS); // approx 8 ms +#if (BLUETOOTH_SD == 140) + m_adv_params.duration = 0; // infinite advertisment + m_adv_params.properties.legacy_pdu = 1; + m_adv_params.primary_phy = BLE_GAP_PHY_1MBPS; +#else m_adv_params.timeout = 0; // infinite advertisment +#endif ble_drv_advertise_stop(); +#if (BLUETOOTH_SD == 140) + err_code = sd_ble_gap_adv_start(BLE_GAP_ADV_SET_HANDLE_DEFAULT, &m_adv_params, BLE_CONN_CFG_TAG_DEFAULT); +#else err_code = sd_ble_gap_adv_start(&m_adv_params); +#endif if (err_code != 0) { nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "Can not start advertisment. status: 0x" HEX2_FMT, (uint16_t)err_code)); @@ -580,7 +633,11 @@ bool ble_drv_advertise_data(ubluepy_advertise_data_t * p_adv_params) { void ble_drv_advertise_stop(void) { if (m_adv_in_progress == true) { uint32_t err_code; +#if (BLUETOOTH_SD == 140) + if ((err_code = sd_ble_gap_adv_stop(BLE_GAP_ADV_SET_HANDLE_DEFAULT)) != 0) { +#else if ((err_code = sd_ble_gap_adv_stop()) != 0) { +#endif nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "Can not stop advertisment. status: 0x" HEX2_FMT, (uint16_t)err_code)); } @@ -656,7 +713,7 @@ void ble_drv_gatts_event_handler_set(mp_obj_t obj, ble_drv_gatts_evt_callback_t gatts_event_handler = evt_handler; } -#if (BLUETOOTH_SD == 130) || (BLUETOOTH_SD == 132) +#if (BLUETOOTH_SD == 130) || (BLUETOOTH_SD == 132) || (BLUETOOTH_SD == 140) void ble_drv_gattc_event_handler_set(mp_obj_t obj, ble_drv_gattc_evt_callback_t evt_handler) { mp_gattc_observer = obj; @@ -721,10 +778,19 @@ void ble_drv_scan_start(void) { SD_TEST_OR_ENABLE(); ble_gap_scan_params_t scan_params; + memset(&scan_params, 0, sizeof(ble_gap_scan_params_t)); + scan_params.active = 1; scan_params.interval = MSEC_TO_UNITS(100, UNIT_0_625_MS); scan_params.window = MSEC_TO_UNITS(100, UNIT_0_625_MS); +#if (BLUETOOTH_SD == 140) + scan_params.filter_policy = BLE_GAP_SCAN_FP_ACCEPT_ALL; + scan_params.filter_duplicates = BLE_GAP_SCAN_DUPLICATES_SUPPRESS; + scan_params.scan_phy = BLE_GAP_PHY_1MBPS; + scan_params.duration = 0; // Infinite +#else scan_params.timeout = 0; // Infinite +#endif #if (BLUETOOTH_SD == 130) scan_params.selective = 0; @@ -751,7 +817,11 @@ void ble_drv_connect(uint8_t * p_addr, uint8_t addr_type) { scan_params.active = 1; scan_params.interval = MSEC_TO_UNITS(100, UNIT_0_625_MS); scan_params.window = MSEC_TO_UNITS(100, UNIT_0_625_MS); - scan_params.timeout = 0; // infinite +#if (BLUETOOTH_SD == 140) + scan_params.duration = 0; // Infinite +#else + scan_params.timeout = 0; // Infinite +#endif #if (BLUETOOTH_SD == 130) scan_params.selective = 0; @@ -782,7 +852,11 @@ void ble_drv_connect(uint8_t * p_addr, uint8_t addr_type) { conn_params.conn_sup_timeout = BLE_CONN_SUP_TIMEOUT; uint32_t err_code; +#if (BLUETOOTH_SD == 140) + if ((err_code = sd_ble_gap_connect(&addr, &scan_params, &conn_params, BLE_CONN_CFG_TAG_DEFAULT)) != 0) { +#else if ((err_code = sd_ble_gap_connect(&addr, &scan_params, &conn_params)) != 0) { +#endif nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "Can not connect. status: 0x" HEX2_FMT, (uint16_t)err_code)); } @@ -904,14 +978,18 @@ static void ble_evt_handler(ble_evt_t * p_ble_evt) { (void)sd_ble_gatts_sys_attr_set(p_ble_evt->evt.gatts_evt.conn_handle, NULL, 0, 0); break; -#if (BLUETOOTH_SD == 132 && BLE_API_VERSION == 3) +#if (BLUETOOTH_SD == 132 && BLE_API_VERSION == 3) || (BLUETOOTH_SD == 140) case BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST: 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; #endif +#if (BLUETOOTH_SD == 140) + case BLE_GATTS_EVT_HVN_TX_COMPLETE: +#else case BLE_EVT_TX_COMPLETE: +#endif BLE_DRIVER_LOG("BLE EVT TX COMPLETE\n"); m_tx_in_progress = false; break; @@ -924,17 +1002,23 @@ static void ble_evt_handler(ble_evt_t * p_ble_evt) { NULL, NULL); break; -#if (BLUETOOTH_SD == 130) || (BLUETOOTH_SD == 132) +#if (BLUETOOTH_SD == 130) || (BLUETOOTH_SD == 132) || (BLUETOOTH_SD == 140) case BLE_GAP_EVT_ADV_REPORT: BLE_DRIVER_LOG("BLE EVT ADV REPORT\n"); ble_drv_adv_data_t adv_data = { .p_peer_addr = p_ble_evt->evt.gap_evt.params.adv_report.peer_addr.addr, .addr_type = p_ble_evt->evt.gap_evt.params.adv_report.peer_addr.addr_type, +#if (BLUETOOTH_SD == 140) + .is_scan_resp = p_ble_evt->evt.gap_evt.params.adv_report.type.scannable, +#else .is_scan_resp = p_ble_evt->evt.gap_evt.params.adv_report.scan_rsp, +#endif .rssi = p_ble_evt->evt.gap_evt.params.adv_report.rssi, .data_len = p_ble_evt->evt.gap_evt.params.adv_report.dlen, .p_data = p_ble_evt->evt.gap_evt.params.adv_report.data, +#if (BLUETOOTH_SD != 140) .adv_type = p_ble_evt->evt.gap_evt.params.adv_report.type +#endif }; // TODO: Fix unsafe callback to possible undefined callback... @@ -1040,7 +1124,11 @@ static void ble_evt_handler(ble_evt_t * p_ble_evt) { } } +#if (BLUETOOTH_SD == 140) +static uint8_t m_ble_evt_buf[sizeof(ble_evt_t) + (BLE_GATT_ATT_MTU_DEFAULT)] __attribute__ ((aligned (4))); +#else static uint8_t m_ble_evt_buf[sizeof(ble_evt_t) + (GATT_MTU_SIZE_DEFAULT)] __attribute__ ((aligned (4))); +#endif #ifdef NRF51 void SWI2_IRQHandler(void) { diff --git a/ports/nrf/drivers/bluetooth/bluetooth_common.mk b/ports/nrf/drivers/bluetooth/bluetooth_common.mk index ebc221ee57..97fdce3b86 100644 --- a/ports/nrf/drivers/bluetooth/bluetooth_common.mk +++ b/ports/nrf/drivers/bluetooth/bluetooth_common.mk @@ -27,6 +27,14 @@ endif SOFTDEV_HEX_NAME = $(SD)_$(MCU_VARIANT)_$(SOFTDEV_VERSION)_softdevice.hex SOFTDEV_HEX_PATH = drivers/bluetooth/$(SD)_$(MCU_VARIANT)_$(SOFTDEV_VERSION) +else ifeq ($(SD), s140) + INC += -Idrivers/bluetooth/$(SD)_$(MCU_VARIANT)_$(SOFTDEV_VERSION)/$(SD)_$(MCU_SUB_VARIANT)_$(SOFTDEV_VERSION)_API/include + INC += -Idrivers/bluetooth/$(SD)_$(MCU_VARIANT)_$(SOFTDEV_VERSION)/$(SD)_$(MCU_SUB_VARIANT)_$(SOFTDEV_VERSION)_API/include/$(MCU_VARIANT) + CFLAGS += -DBLUETOOTH_SD_DEBUG=1 + CFLAGS += -DBLUETOOTH_SD=140 + + SOFTDEV_HEX_NAME = $(SD)_$(MCU_SUB_VARIANT)_$(SOFTDEV_VERSION)_softdevice.hex + SOFTDEV_HEX_PATH = drivers/bluetooth/$(SD)_$(MCU_VARIANT)_$(SOFTDEV_VERSION) else $(error Incorrect softdevice set flag) endif