Skip to content

Commit 0111bdc

Browse files
glennrubdpgeorge
authored andcommitted
nrf: Add support for s132 v5.0.0 bluetooth stack (micropython#139)
* ports/nrf/boards: Adding linker script for nrf52832 using BLE stack s132 v.5.0.0. * ports/nrf/drivers/bluetooth: Updating makefile to add BLE_API_VERSION=4 if s132 v5.0.0 is used. * ports/nrf/drivers/bluetooth: Updating BLE stack download script to also download S132 v5.0.0. * ports/nrf/drivers/bluetooth: Updating ble_drv.c to handle BLE_API_VERSION=4 (s132 v5.0.0). * ports/nrf/boards: Updating linker script for nrf52832 with s132 v.5.0.0 bluetooth stack. * ports/nrf/drivers/bluetooth: Removing commented out code in ble_drv.c * ports/nrf/drivers/bluetooth: Updating define of GATT_MTU_SIZE_DEFAULT for SD132v5 to be defined using the new name defined in the SD headers in a more generic way. * ports/nrf/drivers/bluetooth: Cleaning up use of BLE_API_VERSION in the ble_drv.c. Also considering s140v6 API, so not all has been changed to >= if API version 3 and 4 in combo is used. New s140v6 will differ on these, and add a new API not compatible with the API for 3 and 4.
1 parent 18f35e5 commit 0111bdc

File tree

4 files changed

+111
-13
lines changed

4 files changed

+111
-13
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
GNU linker script for NRF52 w/ s132 5.0.0 SoftDevice
3+
*/
4+
5+
/* Specify the memory areas */
6+
MEMORY
7+
{
8+
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 512K /* entire flash */
9+
FLASH_TEXT (rx) : ORIGIN = 0x00023000, LENGTH = 308K /* app */
10+
FLASH_USER (rx) : ORIGIN = 0x00070000, LENGTH = 64K /* app data, filesystem */
11+
RAM (xrw) : ORIGIN = 0x200039c0, LENGTH = 0x0c640 /* 49.5 KiB, give 8KiB headroom for softdevice */
12+
}
13+
14+
/* produce a link error if there is not this amount of RAM for these sections */
15+
_minimum_stack_size = 2K;
16+
_minimum_heap_size = 16K;
17+
18+
/* top end of the stack */
19+
20+
/*_stack_end = ORIGIN(RAM) + LENGTH(RAM);*/
21+
_estack = ORIGIN(RAM) + LENGTH(RAM);
22+
23+
/* RAM extents for the garbage collector */
24+
_ram_end = ORIGIN(RAM) + LENGTH(RAM);
25+
_heap_end = 0x20007000; /* tunable */
26+
27+
INCLUDE "boards/common.ld"

ports/nrf/drivers/bluetooth/ble_drv.c

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "nrf_sdm.h"
3737
#include "ble_gap.h"
3838
#include "ble.h" // sd_ble_uuid_encode
39+
#include "hal_irq.h"
3940
#include "hal/hal_nvmc.h"
4041
#include "mphalport.h"
4142

@@ -62,6 +63,10 @@
6263
#define BLE_SLAVE_LATENCY 0
6364
#define BLE_CONN_SUP_TIMEOUT MSEC_TO_UNITS(4000, UNIT_10_MS)
6465

66+
#if !defined(GATT_MTU_SIZE_DEFAULT) && defined(BLE_GATT_ATT_MTU_DEFAULT)
67+
#define GATT_MTU_SIZE_DEFAULT BLE_GATT_ATT_MTU_DEFAULT
68+
#endif
69+
6570
#define SD_TEST_OR_ENABLE() \
6671
if (ble_drv_stack_enabled() == 0) { \
6772
(void)ble_drv_stack_enable(); \
@@ -130,14 +135,22 @@ uint32_t ble_drv_stack_enable(void) {
130135
.source = NRF_CLOCK_LF_SRC_RC,
131136
.rc_ctiv = 16,
132137
.rc_temp_ctiv = 2,
138+
#if (BLE_API_VERSION >= 4)
139+
.accuracy = NRF_CLOCK_LF_ACCURACY_250_PPM
140+
#else
133141
.xtal_accuracy = 0
142+
#endif
134143
};
135144
#else
136145
nrf_clock_lf_cfg_t clock_config = {
137146
.source = NRF_CLOCK_LF_SRC_XTAL,
138147
.rc_ctiv = 0,
139148
.rc_temp_ctiv = 0,
149+
#if (BLE_API_VERSION >= 4)
150+
.accuracy = NRF_CLOCK_LF_ACCURACY_20_PPM
151+
#else
140152
.xtal_accuracy = NRF_CLOCK_LF_XTAL_ACCURACY_20_PPM
153+
#endif
141154
};
142155
#endif
143156
uint32_t err_code = sd_softdevice_enable(&clock_config,
@@ -146,32 +159,48 @@ uint32_t ble_drv_stack_enable(void) {
146159

147160
BLE_DRIVER_LOG("SoftDevice enable status: " UINT_FMT "\n", (uint16_t)err_code);
148161

149-
#if NRF51
150-
err_code = sd_nvic_EnableIRQ(SWI2_IRQn);
151-
#else
152-
err_code = sd_nvic_EnableIRQ(SWI2_EGU2_IRQn);
153-
#endif
162+
err_code = sd_nvic_EnableIRQ(SD_EVT_IRQn);
154163

155164
BLE_DRIVER_LOG("IRQ enable status: " UINT_FMT "\n", (uint16_t)err_code);
156-
165+
166+
#if (BLE_API_VERSION >= 4)
167+
168+
ble_cfg_t ble_conf;
169+
uint32_t app_ram_start_cfg = 0x200039c0;
170+
ble_conf.conn_cfg.conn_cfg_tag = 1;
171+
ble_conf.conn_cfg.params.gap_conn_cfg.conn_count = 1;
172+
ble_conf.conn_cfg.params.gap_conn_cfg.event_length = 3;
173+
err_code = sd_ble_cfg_set(BLE_CONN_CFG_GAP, &ble_conf, app_ram_start_cfg);
174+
175+
memset(&ble_conf, 0, sizeof(ble_conf));
176+
177+
ble_conf.gap_cfg.role_count_cfg.periph_role_count = 1;
178+
ble_conf.gap_cfg.role_count_cfg.central_role_count = 1;
179+
ble_conf.gap_cfg.role_count_cfg.central_sec_count = 0;
180+
err_code = sd_ble_cfg_set(BLE_GAP_CFG_ROLE_COUNT, &ble_conf, app_ram_start_cfg);
181+
#else
157182
// Enable BLE stack.
158183
ble_enable_params_t ble_enable_params;
159184
memset(&ble_enable_params, 0x00, sizeof(ble_enable_params));
160185
ble_enable_params.gatts_enable_params.attr_tab_size = BLE_GATTS_ATTR_TAB_SIZE_DEFAULT;
161186
ble_enable_params.gatts_enable_params.service_changed = 0;
162-
#if (BLUETOOTH_SD == 132)
187+
#if (BLUETOOTH_SD == 132)
163188
ble_enable_params.gap_enable_params.periph_conn_count = 1;
164189
ble_enable_params.gap_enable_params.central_conn_count = 1;
190+
#endif
165191
#endif
166192

167-
168193
#if (BLUETOOTH_SD == 100) || (BLUETOOTH_SD == 110)
169194
err_code = sd_ble_enable(&ble_enable_params);
170195
#else
171196

172197
#if (BLUETOOTH_SD == 132)
173198
uint32_t app_ram_start = 0x200039c0;
199+
#if (BLE_API_VERSION == 3)
174200
err_code = sd_ble_enable(&ble_enable_params, &app_ram_start); // 8K SD headroom from linker script.
201+
#elif (BLE_API_VERSION >= 4)
202+
err_code = sd_ble_enable(&app_ram_start); // 8K SD headroom from linker script.
203+
#endif
175204
BLE_DRIVER_LOG("BLE ram size: " UINT_FMT "\n", (uint16_t)app_ram_start);
176205
#else
177206
err_code = sd_ble_enable(&ble_enable_params, (uint32_t *)0x20001870);
@@ -231,7 +260,7 @@ void ble_drv_address_get(ble_drv_addr_t * p_addr) {
231260
SD_TEST_OR_ENABLE();
232261

233262
ble_gap_addr_t local_ble_addr;
234-
#if (BLUETOOTH_SD == 132 && BLE_API_VERSION == 3)
263+
#if (BLE_API_VERSION >= 3)
235264
uint32_t err_code = sd_ble_gap_addr_get(&local_ble_addr);
236265
#else
237266
uint32_t err_code = sd_ble_gap_address_get(&local_ble_addr);
@@ -567,8 +596,12 @@ bool ble_drv_advertise_data(ubluepy_advertise_data_t * p_adv_params) {
567596
m_adv_params.timeout = 0; // infinite advertisment
568597

569598
ble_drv_advertise_stop();
570-
599+
#if (BLE_API_VERSION == 4)
600+
uint8_t conf_tag = BLE_CONN_CFG_TAG_DEFAULT; // Could also be set to tag from sd_ble_cfg_set
601+
err_code = sd_ble_gap_adv_start(&m_adv_params, conf_tag);
602+
#else
571603
err_code = sd_ble_gap_adv_start(&m_adv_params);
604+
#endif
572605
if (err_code != 0) {
573606
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
574607
"Can not start advertisment. status: 0x" HEX2_FMT, (uint16_t)err_code));
@@ -731,7 +764,7 @@ void ble_drv_scan_start(void) {
731764
#if (BLUETOOTH_SD == 130)
732765
scan_params.selective = 0;
733766
scan_params.p_whitelist = NULL;
734-
#elif (BLUETOOTH_SD == 132 && BLE_API_VERSION == 3)
767+
#elif (BLE_API_VERSION == 3 || BLE_API_VERSION == 4)
735768
scan_params.use_whitelist = 0;
736769
#endif
737770

@@ -758,7 +791,7 @@ void ble_drv_connect(uint8_t * p_addr, uint8_t addr_type) {
758791
#if (BLUETOOTH_SD == 130)
759792
scan_params.selective = 0;
760793
scan_params.p_whitelist = NULL;
761-
#elif (BLUETOOTH_SD == 132 && BLE_API_VERSION == 3)
794+
#elif (BLE_API_VERSION == 3 || BLE_API_VERSION == 4)
762795
scan_params.use_whitelist = 0;
763796
#endif
764797

@@ -784,10 +817,21 @@ void ble_drv_connect(uint8_t * p_addr, uint8_t addr_type) {
784817
conn_params.conn_sup_timeout = BLE_CONN_SUP_TIMEOUT;
785818

786819
uint32_t err_code;
820+
#if (BLE_API_VERSION >= 4)
821+
uint8_t conn_tag = BLE_CONN_CFG_TAG_DEFAULT;
822+
if ((err_code = sd_ble_gap_connect(&addr,
823+
&scan_params,
824+
&conn_params,
825+
conn_tag)) != 0) {
826+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
827+
"Can not connect. status: 0x" HEX2_FMT, (uint16_t)err_code));
828+
}
829+
#else
787830
if ((err_code = sd_ble_gap_connect(&addr, &scan_params, &conn_params)) != 0) {
788831
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
789832
"Can not connect. status: 0x" HEX2_FMT, (uint16_t)err_code));
790833
}
834+
#endif
791835
}
792836

793837
bool ble_drv_discover_services(mp_obj_t obj, uint16_t conn_handle, uint16_t start_handle, ble_drv_disc_add_service_callback_t cb) {
@@ -922,14 +966,18 @@ static void ble_evt_handler(ble_evt_t * p_ble_evt) {
922966
(void)sd_ble_gatts_sys_attr_set(p_ble_evt->evt.gatts_evt.conn_handle, NULL, 0, 0);
923967
break;
924968

925-
#if (BLUETOOTH_SD == 132 && BLE_API_VERSION == 3)
969+
#if (BLE_API_VERSION >= 3)
926970
case BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST:
927971
BLE_DRIVER_LOG("GATTS EVT EXCHANGE MTU REQUEST\n");
928972
(void)sd_ble_gatts_exchange_mtu_reply(p_ble_evt->evt.gatts_evt.conn_handle, 23); // MAX MTU size
929973
break;
930974
#endif
931975

976+
#if (BLE_API_VERSION >= 4)
977+
case BLE_GATTS_EVT_HVN_TX_COMPLETE:
978+
#else
932979
case BLE_EVT_TX_COMPLETE:
980+
#endif
933981
BLE_DRIVER_LOG("BLE EVT TX COMPLETE\n");
934982
m_tx_in_progress = false;
935983
break;

ports/nrf/drivers/bluetooth/bluetooth_common.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ ifeq ($(SOFTDEV_VERSION), 2.0.1)
2323
CFLAGS += -DBLE_API_VERSION=2
2424
else ifeq ($(SOFTDEV_VERSION), 3.0.0)
2525
CFLAGS += -DBLE_API_VERSION=3
26+
else ifeq ($(SOFTDEV_VERSION), 5.0.0)
27+
CFLAGS += -DBLE_API_VERSION=4
2628
endif
2729

2830
SOFTDEV_HEX_NAME = $(SD)_$(MCU_VARIANT)_$(SOFTDEV_VERSION)_softdevice.hex

ports/nrf/drivers/bluetooth/download_ble_stack.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,32 @@ function download_s132_nrf52_3_0_0
5353
}
5454

5555

56+
function download_s132_nrf52_5_0_0
57+
{
58+
echo ""
59+
echo "####################################"
60+
echo "### Downloading s132_nrf52_5.0.0 ###"
61+
echo "####################################"
62+
echo ""
63+
64+
mkdir -p $1/s132_nrf52_5.0.0
65+
cd $1/s132_nrf52_5.0.0
66+
67+
wget https://www.nordicsemi.com/eng/nordic/download_resource/58987/11/28978944/116068
68+
mv 116068 temp.zip
69+
unzip -u temp.zip
70+
rm temp.zip
71+
cd -
72+
}
73+
5674
SCRIPT_DIR="$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
5775

5876
if [ $# -eq 0 ]; then
5977
echo "No Bluetooth LE stack defined, downloading all."
6078
download_s110_nrf51_8_0_0 ${SCRIPT_DIR}
6179
download_s132_nrf52_2_0_1 ${SCRIPT_DIR}
6280
download_s132_nrf52_3_0_0 ${SCRIPT_DIR}
81+
download_s132_nrf52_5_0_0 ${SCRIPT_DIR}
6382
else
6483
case $1 in
6584
"s110_nrf51" )
@@ -68,6 +87,8 @@ else
6887
download_s132_nrf52_2_0_1 ${SCRIPT_DIR} ;;
6988
"s132_nrf52_3_0_0" )
7089
download_s132_nrf52_3_0_0 ${SCRIPT_DIR} ;;
90+
"s132_nrf52_5_0_0" )
91+
download_s132_nrf52_5_0_0 ${SCRIPT_DIR} ;;
7192
esac
7293
fi
7394

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy