36
36
#include "nrf_sdm.h"
37
37
#include "ble_gap.h"
38
38
#include "ble.h" // sd_ble_uuid_encode
39
+ #include "hal_irq.h"
39
40
#include "hal/hal_nvmc.h"
40
41
#include "mphalport.h"
41
42
62
63
#define BLE_SLAVE_LATENCY 0
63
64
#define BLE_CONN_SUP_TIMEOUT MSEC_TO_UNITS(4000, UNIT_10_MS)
64
65
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
+
65
70
#define SD_TEST_OR_ENABLE () \
66
71
if (ble_drv_stack_enabled() == 0) { \
67
72
(void)ble_drv_stack_enable(); \
@@ -130,14 +135,22 @@ uint32_t ble_drv_stack_enable(void) {
130
135
.source = NRF_CLOCK_LF_SRC_RC ,
131
136
.rc_ctiv = 16 ,
132
137
.rc_temp_ctiv = 2 ,
138
+ #if (BLE_API_VERSION >= 4 )
139
+ .accuracy = NRF_CLOCK_LF_ACCURACY_250_PPM
140
+ #else
133
141
.xtal_accuracy = 0
142
+ #endif
134
143
};
135
144
#else
136
145
nrf_clock_lf_cfg_t clock_config = {
137
146
.source = NRF_CLOCK_LF_SRC_XTAL ,
138
147
.rc_ctiv = 0 ,
139
148
.rc_temp_ctiv = 0 ,
149
+ #if (BLE_API_VERSION >= 4 )
150
+ .accuracy = NRF_CLOCK_LF_ACCURACY_20_PPM
151
+ #else
140
152
.xtal_accuracy = NRF_CLOCK_LF_XTAL_ACCURACY_20_PPM
153
+ #endif
141
154
};
142
155
#endif
143
156
uint32_t err_code = sd_softdevice_enable (& clock_config ,
@@ -146,32 +159,48 @@ uint32_t ble_drv_stack_enable(void) {
146
159
147
160
BLE_DRIVER_LOG ("SoftDevice enable status: " UINT_FMT "\n" , (uint16_t )err_code );
148
161
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 );
154
163
155
164
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
157
182
// Enable BLE stack.
158
183
ble_enable_params_t ble_enable_params ;
159
184
memset (& ble_enable_params , 0x00 , sizeof (ble_enable_params ));
160
185
ble_enable_params .gatts_enable_params .attr_tab_size = BLE_GATTS_ATTR_TAB_SIZE_DEFAULT ;
161
186
ble_enable_params .gatts_enable_params .service_changed = 0 ;
162
- #if (BLUETOOTH_SD == 132 )
187
+ #if (BLUETOOTH_SD == 132 )
163
188
ble_enable_params .gap_enable_params .periph_conn_count = 1 ;
164
189
ble_enable_params .gap_enable_params .central_conn_count = 1 ;
190
+ #endif
165
191
#endif
166
192
167
-
168
193
#if (BLUETOOTH_SD == 100 ) || (BLUETOOTH_SD == 110 )
169
194
err_code = sd_ble_enable (& ble_enable_params );
170
195
#else
171
196
172
197
#if (BLUETOOTH_SD == 132 )
173
198
uint32_t app_ram_start = 0x200039c0 ;
199
+ #if (BLE_API_VERSION == 3 )
174
200
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
175
204
BLE_DRIVER_LOG ("BLE ram size: " UINT_FMT "\n" , (uint16_t )app_ram_start );
176
205
#else
177
206
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) {
231
260
SD_TEST_OR_ENABLE ();
232
261
233
262
ble_gap_addr_t local_ble_addr ;
234
- #if (BLUETOOTH_SD == 132 && BLE_API_VERSION = = 3 )
263
+ #if (BLE_API_VERSION > = 3 )
235
264
uint32_t err_code = sd_ble_gap_addr_get (& local_ble_addr );
236
265
#else
237
266
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) {
567
596
m_adv_params .timeout = 0 ; // infinite advertisment
568
597
569
598
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
571
603
err_code = sd_ble_gap_adv_start (& m_adv_params );
604
+ #endif
572
605
if (err_code != 0 ) {
573
606
nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_OSError ,
574
607
"Can not start advertisment. status: 0x" HEX2_FMT , (uint16_t )err_code ));
@@ -731,7 +764,7 @@ void ble_drv_scan_start(void) {
731
764
#if (BLUETOOTH_SD == 130 )
732
765
scan_params .selective = 0 ;
733
766
scan_params .p_whitelist = NULL ;
734
- #elif (BLUETOOTH_SD == 132 && BLE_API_VERSION == 3 )
767
+ #elif (BLE_API_VERSION == 3 || BLE_API_VERSION == 4 )
735
768
scan_params .use_whitelist = 0 ;
736
769
#endif
737
770
@@ -758,7 +791,7 @@ void ble_drv_connect(uint8_t * p_addr, uint8_t addr_type) {
758
791
#if (BLUETOOTH_SD == 130 )
759
792
scan_params .selective = 0 ;
760
793
scan_params .p_whitelist = NULL ;
761
- #elif (BLUETOOTH_SD == 132 && BLE_API_VERSION == 3 )
794
+ #elif (BLE_API_VERSION == 3 || BLE_API_VERSION == 4 )
762
795
scan_params .use_whitelist = 0 ;
763
796
#endif
764
797
@@ -784,10 +817,21 @@ void ble_drv_connect(uint8_t * p_addr, uint8_t addr_type) {
784
817
conn_params .conn_sup_timeout = BLE_CONN_SUP_TIMEOUT ;
785
818
786
819
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
787
830
if ((err_code = sd_ble_gap_connect (& addr , & scan_params , & conn_params )) != 0 ) {
788
831
nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_OSError ,
789
832
"Can not connect. status: 0x" HEX2_FMT , (uint16_t )err_code ));
790
833
}
834
+ #endif
791
835
}
792
836
793
837
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) {
922
966
(void )sd_ble_gatts_sys_attr_set (p_ble_evt -> evt .gatts_evt .conn_handle , NULL , 0 , 0 );
923
967
break ;
924
968
925
- #if (BLUETOOTH_SD == 132 && BLE_API_VERSION = = 3 )
969
+ #if (BLE_API_VERSION > = 3 )
926
970
case BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST :
927
971
BLE_DRIVER_LOG ("GATTS EVT EXCHANGE MTU REQUEST\n" );
928
972
(void )sd_ble_gatts_exchange_mtu_reply (p_ble_evt -> evt .gatts_evt .conn_handle , 23 ); // MAX MTU size
929
973
break ;
930
974
#endif
931
975
976
+ #if (BLE_API_VERSION >= 4 )
977
+ case BLE_GATTS_EVT_HVN_TX_COMPLETE :
978
+ #else
932
979
case BLE_EVT_TX_COMPLETE :
980
+ #endif
933
981
BLE_DRIVER_LOG ("BLE EVT TX COMPLETE\n" );
934
982
m_tx_in_progress = false;
935
983
break ;
0 commit comments