├── .gitignore ├── Readme.md ├── ble_cus.c ├── ble_cus.h ├── images ├── advertisement_packet.png ├── advertising_device.png ├── enable_notif.png ├── memory_settings.JPG ├── memory_settings_SDK_v14.JPG ├── memory_settings_SDK_v14_SES.JPG ├── memory_settings_SDK_v15_Keil.JPG ├── memory_settings_SDK_v15_SES.JPG ├── memory_settings_SDK_v15_gcc.JPG ├── service.png ├── service_and_char.png ├── service_char_desc.png ├── write_to_char_1.png └── write_to_char_2.png ├── main.c ├── pca10040 └── s132 │ ├── arm5_no_packs │ ├── ble_app_template_pca10040_s132.uvoptx │ └── ble_app_template_pca10040_s132.uvprojx │ ├── armgcc │ ├── Makefile │ └── ble_app_template_gcc_nrf52.ld │ ├── config │ └── sdk_config.h │ └── ses │ ├── ble_app_template_pca10040_s132.emProject │ ├── ble_app_template_pca10040_s132.emSession │ └── flash_placement.xml └── pdf ├── 01_Nordic_Company_Introduction.pdf ├── 02_nRF52 Student_ Intro_v0.1.pdf ├── 03_Bluetooth Overview.pdf └── 04_Softdevice_Student_Introduction_v0.1.pdf /.gitignore: -------------------------------------------------------------------------------- 1 | *.axf 2 | *.swp 3 | *.htm 4 | *.Inp 5 | *.map 6 | *.tra 7 | *.dep 8 | *._2i 9 | *.l2p 10 | *.__i 11 | *.fed 12 | *.crf 13 | *.d 14 | *.o 15 | *.lst 16 | *.lnp 17 | *.bak 18 | 19 | *.ini 20 | *.iex 21 | *.sct 22 | *.uvgui.* 23 | *.uvguix.* 24 | *.tmp 25 | **/arm/JLinkLog.txt 26 | **/_viminfo 27 | **/_vimtags 28 | _build/ 29 | RTE/ 30 | Output/ -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | nRF52 Bluetooth Course 2 | ------- 3 | 4 | This repository contains the hands-on exercises for the nRF52 Bluetooth Course . At the end of the course you should be able to create your own custom service with a custom characteristic in the ble_app_template project found in the Nordic nRF5 SDK v15.0.0. 5 | 6 | The aim of this tutorial is simply to create one service with one characteristic without too much theory in between the steps. There are no .c or .h files that needs to be downloaded as we will be starting from scratch in the ble_app_template project. 7 | 8 | However, if you simply want to compile the example without following the course steps then you can be clone this repo into SDK v15.0.0/examples/ble_peripheral. 9 | 10 | 11 | 16 | 17 | ## Course Evaluation 18 | 19 | Please take 2 minutes to fill out the Course Evaluation Form, link below, at the end of the course. 20 | 21 | [Course Evaluation Form](https://drive.google.com/open?id=1hzn3SHDQ2Qg0d-_jRboxD2qpFKHSPzwKGH7Nz3AKotQ) 22 | 23 | It is important to us that you tell us what you liked/did not like about the course so that we can improve the course material and presentations. 24 | 25 | The evaluation is of course anonymous. 26 | 27 | ## Presentations 28 | The presentations from the course can be downloaded in PDF-format using the links below: 29 | 30 | [Nordic Introduction](https://github.com/bjornspockeli/custom_ble_service_example/blob/master/pdf/01_Nordic_Company_Introduction.pdf) 31 | 32 | [nRF52832 Intro + Embedded C Intro](https://github.com/NordicPlayground/nRF52-Bluetooth-Course/blob/master/pdf/02_nRF52%20Student_%20Intro_v0.1.pdf) 33 | 34 | [Bluetooth Low Energy Protocol](https://github.com/NordicPlayground/nRF52-Bluetooth-Course/blob/master/pdf/03_Bluetooth%20Overview.pdf) 35 | 36 | [SoftDevice Introduction](https://github.com/NordicPlayground/nRF52-Bluetooth-Course/blob/master/pdf/04_Softdevice_Student_Introduction_v0.1.pdf) 37 | 38 | 39 | ## HW Requirements 40 | - nRF52 Development Kit 41 | 42 | ## SW Requirements 43 | - nRF5 SDK v15.0.0 [download page](http://developer.nordicsemi.com/nRF5_SDK/nRF5_SDK_v15.x.x/) 44 | - Latest version of Segger Embedded Studio[download page](https://www.segger.com/downloads/embedded-studio/) or latest version of Keil ARM MKD [download page](https://www.keil.com/demo/eval/arm.htm) or the GCC ARM Embedded 6.3 2017-q2-update toolchain [download page](https://developer.arm.com/open-source/gnu-toolchain/gnu-rm/downloads/6-2017-q2-update) 45 | - nRF Connect for Mobile, [download page](https://www.nordicsemi.com/eng/Products/Nordic-mobile-Apps/nRF-Connect-for-mobile-previously-called-nRF-Master-Control-Panel) 46 | - nRF Command Line Tools [download page](http://infocenter.nordicsemi.com/topic/com.nordic.infocenter.tools/dita/tools/nrf5x_command_line_tools/nrf5x_installation.html?cp=5_1_1) 47 | 48 | 49 | ## IDE/Toolchain Support 50 | 51 | Nordic Semiconductor added Segger Embedded Studio support in SDK v14.1.0 and the tutorial has been written with that IDE in mind, i.e. steps to change Memory Settings/build Parameters will mainly be for SES. However, the code should compile with the other IDEs/toolchains in the list. 52 | 53 | - Segger Embedded Studio 54 | - Make and GCC 55 | - Keil 56 | 57 | 58 | ## Tutorial Steps 59 | 67 | ### Step 1 - Getting started 68 | 1. Download nRF5_SDK_15.0.0_17b948a from the [download page](http://developer.nordicsemi.com/nRF5_SDK/nRF5_SDK_v15.x.x/) and extract the zip to your drive, e.g. C:\NordicSemi\nRF5_SDK_15.0.0_a53641a. 69 | 70 | 2. Navigate to the nRF5_SDK_15.0.0_a53641a/examples/ble_peripheral folder and find the ble_app_template project folder. 71 | 72 | 3. Create a copy of the folder and name it `custom_ble_service_example`. 73 | 74 | 4. Navigate to custom_ble_service_example\pca10040\s132\ses and open the ble_app_template_pca10040_s132.emProject project 75 | 76 | ### Step 2 - Creating a Custom Base UUID 77 | 78 | The first thing we need to do is to create a new .c file, lets call it ble_cus.c (**Cu**stom **S**ervice), and its accompaning .h file ble_cus.h. Create the two files in the same folder as the main.c file. At the top of the header file ble_cus.h we'll need to include the following .h files 79 | 80 | ```c 81 | /* This code belongs in ble_cus.h*/ 82 | #include 83 | #include 84 | #include "ble.h" 85 | #include "ble_srv_common.h" 86 | ``` 87 | 88 | 89 | 90 | Next, we're going to need a 128-bit UUID for our custom service since we're not going to implement our service with one of the 16-bit Bluetooth SIG UUIDs that are reserved for standardized profiles. There are several ways to generate a 128-bit UUID, but we'll use [this](https://www.uuidgenerator.net/version4) Online UUID generator. The webpage will generate a random 128-bit UUID, which in my case was 91 | 92 | ``` 93 | f364adc9-b000-4042-ba50-05ca45bf8abc 94 | ``` 95 | The UUID is given as the sixteen octets of a UUID are represented as 32 hexadecimal (base 16) digits, displayed in five groups separated by hyphens, in the form 8-4-4-4-12. The 16 octets are given in big-endian, while we use the small-endian representation in our SDK. Thus, we must reverse the byte-ordering when we define our UUID base in the ble_cus.h, as shown below. 96 | 97 | ```c 98 | /* This code belongs in ble_cus.h*/ 99 | #define CUSTOM_SERVICE_UUID_BASE {0xBC, 0x8A, 0xBF, 0x45, 0xCA, 0x05, 0x50, 0xBA, \ 100 | 0x40, 0x42, 0xB0, 0x00, 0xC9, 0xAD, 0x64, 0xF3} 101 | ``` 102 | Now that we have defined our Base UUID, we need to define a 16-bit UUID for the Custom Service and a 16-bit UUID for a Custom Value Characteristic. 103 | 104 | ```c 105 | /* This code belongs in ble_cus.h*/ 106 | #define CUSTOM_SERVICE_UUID 0x1400 107 | #define CUSTOM_VALUE_CHAR_UUID 0x1401 108 | ``` 109 | The values for the 16-bit UUIDs that will be inserted into the base UUID can be choosen by random. 110 | 111 | ### Step 2 - Implementing the Custom Service 112 | 113 | First things first, we need to include the ble_cus.h header file we just created as well as some common SDK header files in ble_cus.c. 114 | 115 | ```c 116 | /* This code belongs in ble_cus.c*/ 117 | #include "sdk_common.h" 118 | #include "ble_srv_common.h" 119 | #include "ble_cus.h" 120 | #include 121 | #include "nrf_gpio.h" 122 | #include "boards.h" 123 | #include "nrf_log.h" 124 | ``` 125 | 126 | The next step is to add a macro for defining a Custom Service(ble_cus) instance by adding the following snippet below the includes in ble_cus.h 127 | 128 | ```c 129 | /* This code belongs in ble_cus.h*/ 130 | 131 | /**@brief Macro for defining a ble_cus instance. 132 | * 133 | * @param _name Name of the instance. 134 | * @hideinitializer 135 | */ 136 | #define BLE_CUS_DEF(_name) \ 137 | static ble_cus_t _name; \ 138 | 139 | ``` 140 | we will use this macro to define a custom service instance in main.c later in the tutorial. 141 | 142 | Ok, so far so good. Now we need to create two structures in ble_cus.h, one Custom Service init structure, ble_cus_init_t struct to hold all the options and data needed to initialize our custom service. 143 | 144 | ```c 145 | /* This code belongs in ble_cus.h*/ 146 | 147 | /**@brief Custom Service init structure. This contains all options and data needed for 148 | * initialization of the service.*/ 149 | typedef struct 150 | { 151 | uint8_t initial_custom_value; /**< Initial custom value */ 152 | ble_srv_cccd_security_mode_t custom_value_char_attr_md; /**< Initial security level for Custom characteristics attribute */ 153 | } ble_cus_init_t; 154 | ``` 155 | 156 | The second struct that we need to create is the Custom Service structure, ble_cus_s, which holds the status information of the service. 157 | 158 | ```c 159 | /* This code belongs in ble_cus.h*/ 160 | 161 | /**@brief Custom Service structure. This contains various status information for the service. */ 162 | struct ble_cus_s 163 | { 164 | uint16_t service_handle; /**< Handle of Custom Service (as provided by the BLE stack). */ 165 | ble_gatts_char_handles_t custom_value_handles; /**< Handles related to the Custom Value characteristic. */ 166 | uint16_t conn_handle; /**< Handle of the current connection (as provided by the BLE stack, is BLE_CONN_HANDLE_INVALID if not in a connection). */ 167 | uint8_t uuid_type; 168 | }; 169 | ``` 170 | 171 | The next step is to add a forward declaration of the ble_cus_t type 172 | ```c 173 | /* This code belongs in ble_cus.h*/ 174 | 175 | // Forward declaration of the ble_cus_t type. 176 | typedef struct ble_cus_s ble_cus_t; 177 | ``` 178 | Make sure that you add this above the BLE_CUS_DEF macro as ble_cus_t is used in this macro, i.e. in this order 179 | 180 | ```c 181 | /* This code belongs in ble_cus.h*/ 182 | 183 | // Forward declaration of the ble_cus_t type. 184 | typedef struct ble_cus_s ble_cus_t; 185 | 186 | /**@brief Macro for defining a ble_cus instance. 187 | * 188 | * @param _name Name of the instance. 189 | * @hideinitializer 190 | */ 191 | #define BLE_CUS_DEF(_name) \ 192 | static ble_cus_t _name; \ 193 | 194 | ``` 195 | 196 | The first function we're going to implement is ble_cus_init function, which we're going to initialize our service with. First, we need to do is to add its function decleration in the ble_cus.h file. 197 | 198 | ```c 199 | /* This code belongs in ble_cus.h*/ 200 | 201 | /**@brief Function for initializing the Custom Service. 202 | * 203 | * @param[out] p_cus Custom Service structure. This structure will have to be supplied by 204 | * the application. It will be initialized by this function, and will later 205 | * be used to identify this particular service instance. 206 | * @param[in] p_cus_init Information needed to initialize the service. 207 | * 208 | * @return NRF_SUCCESS on successful initialization of service, otherwise an error code. 209 | */ 210 | uint32_t ble_cus_init(ble_cus_t * p_cus, const ble_cus_init_t * p_cus_init); 211 | ``` 212 | 213 | The first thing we should do upon entering ble_cus_init is to check that none of the pointers that we passed as arguments are NULL and declare the two variables err_code and ble_uuid. 214 | 215 | ```c 216 | /* This code belongs in ble_cus.c*/ 217 | 218 | uint32_t ble_cus_init(ble_cus_t * p_cus, const ble_cus_init_t * p_cus_init) 219 | { 220 | if (p_cus == NULL || p_cus_init == NULL) 221 | { 222 | return NRF_ERROR_NULL; 223 | } 224 | 225 | uint32_t err_code; 226 | ble_uuid_t ble_uuid; 227 | } 228 | ``` 229 | After verifying that the pointers are valid we can initialize the Custom Service structure 230 | 231 | ```c 232 | /* This code belongs in ble_cus_init() in ble_cus.c*/ 233 | 234 | // Initialize service structure 235 | p_cus->conn_handle = BLE_CONN_HANDLE_INVALID; 236 | ``` 237 | 238 | This consists of setting the connection handle to invalid( should only be valid when we're in a connection). Next, we're going to add our custom (also referred to as vendor specific) base UUID to the BLE stack's table. 239 | 240 | ```c 241 | /* This code belongs in ble_cus_init() ble_cus.c*/ 242 | 243 | // Add Custom Service UUID 244 | ble_uuid128_t base_uuid = {CUSTOM_SERVICE_UUID_BASE}; 245 | err_code = sd_ble_uuid_vs_add(&base_uuid, &p_cus->uuid_type); 246 | VERIFY_SUCCESS(err_code); 247 | 248 | ble_uuid.type = p_cus->uuid_type; 249 | ble_uuid.uuid = CUSTOM_SERVICE_UUID; 250 | ``` 251 | 252 | We're almost done, the last thing we have to do is to add the Custom Service decleration to the BLE Stack's GATT table. 253 | 254 | ```c 255 | /* This code belongs in ble_cus_init() in ble_cus.c*/ 256 | 257 | // Add the Custom Service 258 | err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &p_cus->service_handle); 259 | if (err_code != NRF_SUCCESS) 260 | { 261 | return err_code; 262 | } 263 | ``` 264 | 265 | If you have followed the steps correctly, then ble_cus_init should look like this. 266 | 267 | ```c 268 | /* This code belongs in ble_cus.c*/ 269 | 270 | uint32_t ble_cus_init(ble_cus_t * p_cus, const ble_cus_init_t * p_cus_init) 271 | { 272 | if (p_cus == NULL || p_cus_init == NULL) 273 | { 274 | return NRF_ERROR_NULL; 275 | } 276 | 277 | uint32_t err_code; 278 | ble_uuid_t ble_uuid; 279 | 280 | // Initialize service structure 281 | p_cus->conn_handle = BLE_CONN_HANDLE_INVALID; 282 | 283 | // Add Custom Service UUID 284 | ble_uuid128_t base_uuid = {CUSTOM_SERVICE_UUID_BASE}; 285 | err_code = sd_ble_uuid_vs_add(&base_uuid, &p_cus->uuid_type); 286 | VERIFY_SUCCESS(err_code); 287 | 288 | ble_uuid.type = p_cus->uuid_type; 289 | ble_uuid.uuid = CUSTOM_SERVICE_UUID; 290 | 291 | // Add the Custom Service 292 | err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &p_cus->service_handle); 293 | VERIFY_SUCCESS(err_code); 294 | 295 | return err_code; 296 | } 297 | ``` 298 | 299 | ### Step 3 - Initializing the Service and advertising our 128-bit UUID. 300 | 301 | Now it's time to initialize the service in main.c and put our 128-bit UUID in the advertisement packet so that other BLE devices can see that our device has the Custom Service. 302 | 303 | First, add the ble_cus.h file to the include list in main.c 304 | 305 | ```c 306 | #include "ble_cus.h" 307 | ``` 308 | 309 | and then use the BLE_CUS_DEF macro to add a custom service instance called m_cus, i.e. add the following line below the defines in main.c 310 | 311 | ```c 312 | BLE_CUS_DEF(m_cus); 313 | ``` 314 | 315 | The next step is to find the empty services_init function in main.c, which should look like this 316 | 317 | ```c 318 | /**@brief Function for initializing services that will be used by the application. 319 | */ 320 | static void services_init(void) 321 | { 322 | ret_code_t err_code; 323 | nrf_ble_qwr_init_t qwr_init = {0}; 324 | 325 | // Initialize Queued Write Module. 326 | qwr_init.error_handler = nrf_qwr_error_handler; 327 | 328 | err_code = nrf_ble_qwr_init(&m_qwr, &qwr_init); 329 | APP_ERROR_CHECK(err_code); 330 | 331 | /* YOUR_JOB: Add code to initialize the services used by the application. 332 | ble_xxs_init_t xxs_init; 333 | ble_yys_init_t yys_init; 334 | 335 | // Initialize XXX Service. 336 | memset(&xxs_init, 0, sizeof(xxs_init)); 337 | 338 | xxs_init.evt_handler = NULL; 339 | xxs_init.is_xxx_notify_supported = true; 340 | xxs_init.ble_xx_initial_value.level = 100; 341 | 342 | err_code = ble_bas_init(&m_xxs, &xxs_init); 343 | APP_ERROR_CHECK(err_code); 344 | 345 | // Initialize YYY Service. 346 | memset(&yys_init, 0, sizeof(yys_init)); 347 | yys_init.evt_handler = on_yys_evt; 348 | yys_init.ble_yy_initial_value.counter = 0; 349 | 350 | err_code = ble_yy_service_init(&yys_init, &yy_init); 351 | APP_ERROR_CHECK(err_code); 352 | */ 353 | } 354 | ``` 355 | 356 | Ok, we're going to do as we're told, i.e. create a ble_cus_init_t struct and populate it with the necessary data and then pass it as an argument to our service init function ble_cus_init(); 357 | 358 | ```c 359 | /**@brief Function for initializing services that will be used by the application. 360 | */ 361 | static void services_init(void) 362 | { 363 | ret_code_t err_code; 364 | nrf_ble_qwr_init_t qwr_init = {0}; 365 | 366 | // Initialize Queued Write Module. 367 | qwr_init.error_handler = nrf_qwr_error_handler; 368 | 369 | err_code = nrf_ble_qwr_init(&m_qwr, &qwr_init); 370 | APP_ERROR_CHECK(err_code); 371 | 372 | ble_cus_init_t cus_init; 373 | 374 | // Initialize CUS Service init structure to zero. 375 | memset(&cus_init, 0, sizeof(cus_init)); 376 | 377 | err_code = ble_cus_init(&m_cus, &cus_init); 378 | APP_ERROR_CHECK(err_code); 379 | 380 | /* YOUR_JOB: Add code to initialize the services used by the application. 381 | ble_xxs_init_t xxs_init; 382 | ble_yys_init_t yys_init; 383 | 384 | // Initialize XXX Service. 385 | memset(&xxs_init, 0, sizeof(xxs_init)); 386 | 387 | xxs_init.evt_handler = NULL; 388 | xxs_init.is_xxx_notify_supported = true; 389 | xxs_init.ble_xx_initial_value.level = 100; 390 | 391 | err_code = ble_bas_init(&m_xxs, &xxs_init); 392 | APP_ERROR_CHECK(err_code); 393 | 394 | // Initialize YYY Service. 395 | memset(&yys_init, 0, sizeof(yys_init)); 396 | yys_init.evt_handler = on_yys_evt; 397 | yys_init.ble_yy_initial_value.counter = 0; 398 | 399 | err_code = ble_yy_service_init(&yys_init, &yy_init); 400 | APP_ERROR_CHECK(err_code); 401 | */ 402 | } 403 | ``` 404 | Now that we have initialized the service we have to add the 128bit UUID to the advertisement packet. If you navigate to the top of main.c you should find the m_adv_uuids array. 405 | 406 | ```c 407 | // YOUR_JOB: Use UUIDs for service(s) used in your application. 408 | static ble_uuid_t m_adv_uuids[] = {{BLE_UUID_DEVICE_INFORMATION_SERVICE, BLE_UUID_TYPE_BLE}}; /**< Universally unique service identifiers. */ 409 | ``` 410 | 411 | We need to replace the BLE_UUID_DEVICE_INFORMATION_SERVICE with the CUSTOM_SERVICE_UUID we defined in ble_cus.h as well as replace BLE_UUID_TYPE_BLE with BLE_UUID_TYPE_VENDOR_BEGIN since this is a 128-bit vendor specific UUID and not a 16-bit Bluetooth SIG UUDID. m_adv_uuids should now look like this 412 | 413 | ```c 414 | // YOUR_JOB: Use UUIDs for service(s) used in your application. 415 | ble_uuid_t m_adv_uuids[] = {{CUSTOM_SERVICE_UUID, BLE_UUID_TYPE_VENDOR_BEGIN }}; /**< Universally unique service identifiers. */ 416 | ``` 417 | After this step we need to tell the BLE stack that we're using a vendor-specific 128-bit UUID and not a 16-bit bit UUID. This is done by changing the following define in sdk_config.h from 418 | ```c 419 | #define NRF_SDH_BLE_VS_UUID_COUNT 0 420 | ``` 421 | to 422 | 423 | ``` 424 | #define NRF_SDH_BLE_VS_UUID_COUNT 1 425 | ``` 426 | 427 | Now, adding a vendor-specific UUID to the BLE stack results in the RAM requirement of the SoftDevice increasing, which we need to take into account. 428 | 431 | **GCC:** If you're copiling the code using armgcc then you need to open the ble_app_template_gcc_nrf52.ld file in the nRF5_SDK\nRF5_SDK_15.0.0_a53641a\examples\ble_peripheral\custom_ble_service_example\pca10040\s132\armgcc folder and modify the Memory section as shown below. 432 | 433 | ```c 434 | MEMORY 435 | { 436 | FLASH (rx) : ORIGIN = 0x26000, LENGTH = 0x5a000 437 | RAM (rwx) : ORIGIN = 0x20002220, LENGTH = 0xdde0 438 | } 439 | ``` 440 | 441 | **Segger Embedded Studio(SES):** Click "Project -> Edit Options", select the Common Configuration, then select Linker and then open the Section Placement Macros Section abd modify RAM_START IRAM1 to 0x20002220 and RAM_SIZE to 0xDDE0, as shown in the screenshot below 442 | 443 | Memory Settings Segger Embedded Studio | 444 | ------------ | 445 | | 446 | 447 | **Keil:** Click "Options for Target" in Keil and modify the Read/Write Memory Areas so that IRAM1 has the start address 0x20002220 and size 0xDDE0, as shown in the screenshot below 448 | 449 | Memory Settings Keil | 450 | ------------ | 451 | | 452 | 453 | 454 | The final step we have to do is to change the calling order in main() so that services_init() is called before advertising_init(). This is because we need to add the CUSTOM_SERVICE_UUID_BASE to the BLE stack's table using sd_ble_uuid_vs_add() in ble_cus_init() before we call advertising_init(). Doing it the otherway around will cause advertising_init() to return an error code. 455 | 456 | That should be it, compile the ble_app_template project, flash the S132 v6.0.0 SoftDevice and then flash the ble_app_template application( only applicable if you are using Keil). LED1 on your nRF52 DK should now start blinking, indicating that its advertising. Use nRF Connect for Android/iOS to scan for the device and view the content of the advertisment package. If you connect to the device you should see the service listed as an "Unknow Service" since we're using a vendor-specific UUID. 457 | 458 | Advertising Device | Content of Advertisment Packet | Service listed in the GATT table | 459 | ------------ | ------------- | ------------- | 460 | | | | 461 | 462 | 463 | ### Step 4 - Adding a Custom Value Characteristic to the Custom Service. 464 | 465 | A service is nothing with out a characteristic, so lets add one of those by creating the custom_value_char_add function to ble_cus.c. 466 | 467 | The first thing we have to do is to declare the function and then add several metadata variables that we will later populate, as shown in the snippet below. 468 | 469 | ```c 470 | /* This code belongs in ble_cus.c*/ 471 | 472 | /**@brief Function for adding the Custom Value characteristic. 473 | * 474 | * @param[in] p_cus Custom Service structure. 475 | * @param[in] p_cus_init Information needed to initialize the service. 476 | * 477 | * @return NRF_SUCCESS on success, otherwise an error code. 478 | */ 479 | static uint32_t custom_value_char_add(ble_cus_t * p_cus, const ble_cus_init_t * p_cus_init) 480 | { 481 | uint32_t err_code; 482 | ble_gatts_char_md_t char_md; 483 | ble_gatts_attr_md_t cccd_md; 484 | ble_gatts_attr_t attr_char_value; 485 | ble_uuid_t ble_uuid; 486 | ble_gatts_attr_md_t attr_md; 487 | } 488 | ``` 489 | Now starts the rather tedious part of populating all these variables, we'll start with char_md, which sets the properties that will be displayed to the central during service discovery. 490 | 491 | ```c 492 | /* This code belongs in custom_value_char_add() in ble_cus.c*/ 493 | 494 | memset(&char_md, 0, sizeof(char_md)); 495 | 496 | char_md.char_props.read = 1; 497 | char_md.char_props.write = 1; 498 | char_md.char_props.notify = 0; 499 | char_md.p_char_user_desc = NULL; 500 | char_md.p_char_pf = NULL; 501 | char_md.p_user_desc_md = NULL; 502 | char_md.p_cccd_md = NULL; 503 | char_md.p_sccd_md = NULL; 504 | } 505 | ``` 506 | So we want to be able to both write and read to our Custom Value characteristic, but we do not want to enable the notify property until later. Next we're going to populate the attr_md, which actually sets the properties( i.e. accessability of the attribute). 507 | 508 | ```c 509 | /* This code belongs in custom_value_char_add() in ble_cus.c*/ 510 | 511 | memset(&attr_md, 0, sizeof(attr_md)); 512 | 513 | attr_md.read_perm = p_cus_init->custom_value_char_attr_md.read_perm; 514 | attr_md.write_perm = p_cus_init->custom_value_char_attr_md.write_perm; 515 | attr_md.vloc = BLE_GATTS_VLOC_STACK; 516 | attr_md.rd_auth = 0; 517 | attr_md.wr_auth = 0; 518 | attr_md.vlen = 0; 519 | } 520 | ``` 521 | 522 | The permissions set in the attr_md struct should correspond with the properties set in the characteristic metadata struct char_md. We're going to provide the permissions in the Custom Service init structure that we pass to ble_cus_init in services_init(). The .vloc option is set to BLE_GATTS_VLOC_STACK as we want the characteristic to be stored in the SoftDevice RAM section and not in the Application RAM section. 523 | 524 | The next variable that we have to populate is ble_uuid, which is going to hold our CUSTOM_VALUE_CHAR_UUID and is of the same type as the CUSTOM_SERVICE_UUID_BASE, i.e. vendor specific, which we specified in the .uuid_type field of Custom Service strucure when we added the CUSTOM_SERVICE_UUID_BASE to the BLE stack's table. 525 | 526 | ```c 527 | /* This code belongs in custom_value_char_add() in ble_cus.c*/ 528 | 529 | ble_uuid.type = p_cus->uuid_type; 530 | ble_uuid.uuid = CUSTOM_VALUE_CHAR_UUID; 531 | ``` 532 | 533 | Next, we're going to populate the attr_char_value struct, which sets the UUID, points to the attribute metadata and sets the size of the characteristic, in our case a single byte(uint8_t). 534 | 535 | ```c 536 | /* This code belongs in custom_value_char_add() in ble_cus.c*/ 537 | 538 | memset(&attr_char_value, 0, sizeof(attr_char_value)); 539 | 540 | attr_char_value.p_uuid = &ble_uuid; 541 | attr_char_value.p_attr_md = &attr_md; 542 | attr_char_value.init_len = sizeof(uint8_t); 543 | attr_char_value.init_offs = 0; 544 | attr_char_value.max_len = sizeof(uint8_t); 545 | ``` 546 | 547 | Finally, we're done populating structs and we can add our characteristic by calling sd_ble_gatts_characteristic_add() with the structs as arguments. 548 | 549 | ```c 550 | /* This code belongs in custom_value_char_add() in ble_cus.c*/ 551 | 552 | err_code = sd_ble_gatts_characteristic_add(p_cus->service_handle, &char_md, 553 | &attr_char_value, 554 | &p_cus->custom_value_handles); 555 | if (err_code != NRF_SUCCESS) 556 | { 557 | return err_code; 558 | } 559 | 560 | return NRF_SUCCESS; 561 | ``` 562 | 563 | After all that hard work (copy-pasting) your custom_value_char_add() function should look like this. 564 | 565 | ```c 566 | /* This code belongs in ble_cus.c*/ 567 | 568 | static uint32_t custom_value_char_add(ble_cus_t * p_cus, const ble_cus_init_t * p_cus_init) 569 | { 570 | uint32_t err_code; 571 | ble_gatts_char_md_t char_md; 572 | ble_gatts_attr_md_t cccd_md; 573 | ble_gatts_attr_t attr_char_value; 574 | ble_uuid_t ble_uuid; 575 | ble_gatts_attr_md_t attr_md; 576 | 577 | memset(&char_md, 0, sizeof(char_md)); 578 | 579 | char_md.char_props.read = 1; 580 | char_md.char_props.write = 1; 581 | char_md.char_props.notify = 0; 582 | char_md.p_char_user_desc = NULL; 583 | char_md.p_char_pf = NULL; 584 | char_md.p_user_desc_md = NULL; 585 | char_md.p_cccd_md = NULL; 586 | char_md.p_sccd_md = NULL; 587 | 588 | memset(&attr_md, 0, sizeof(attr_md)); 589 | 590 | attr_md.read_perm = p_cus_init->custom_value_char_attr_md.read_perm; 591 | attr_md.write_perm = p_cus_init->custom_value_char_attr_md.write_perm; 592 | attr_md.vloc = BLE_GATTS_VLOC_STACK; 593 | attr_md.rd_auth = 0; 594 | attr_md.wr_auth = 0; 595 | attr_md.vlen = 0; 596 | 597 | ble_uuid.type = p_cus->uuid_type; 598 | ble_uuid.uuid = CUSTOM_VALUE_CHAR_UUID; 599 | 600 | memset(&attr_char_value, 0, sizeof(attr_char_value)); 601 | 602 | attr_char_value.p_uuid = &ble_uuid; 603 | attr_char_value.p_attr_md = &attr_md; 604 | attr_char_value.init_len = sizeof(uint8_t); 605 | attr_char_value.init_offs = 0; 606 | attr_char_value.max_len = sizeof(uint8_t); 607 | 608 | err_code = sd_ble_gatts_characteristic_add(p_cus->service_handle, &char_md, 609 | &attr_char_value, 610 | &p_cus->custom_value_handles); 611 | if (err_code != NRF_SUCCESS) 612 | { 613 | return err_code; 614 | } 615 | 616 | return NRF_SUCCESS; 617 | } 618 | ``` 619 | The final step is to call custom_value_char_add() at the end of ble_cus_init the service has been added, i.e. 620 | 621 | ```c 622 | /* This code belongs in ble_cus.c*/ 623 | 624 | uint32_t ble_cus_init(ble_cus_t * p_cus, const ble_cus_init_t * p_cus_init) 625 | { 626 | if (p_cus == NULL || p_cus_init == NULL) 627 | { 628 | return NRF_ERROR_NULL; 629 | } 630 | 631 | uint32_t err_code; 632 | ble_uuid_t ble_uuid; 633 | 634 | // Initialize service structure 635 | p_cus->conn_handle = BLE_CONN_HANDLE_INVALID; 636 | 637 | // Add Custom Service UUID 638 | ble_uuid128_t base_uuid = {CUSTOM_SERVICE_UUID_BASE}; 639 | err_code = sd_ble_uuid_vs_add(&base_uuid, &p_cus->uuid_type); 640 | VERIFY_SUCCESS(err_code); 641 | 642 | ble_uuid.type = p_cus->uuid_type; 643 | ble_uuid.uuid = CUSTOM_SERVICE_UUID; 644 | 645 | // Add the Custom Service 646 | err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &p_cus->service_handle); 647 | if (err_code != NRF_SUCCESS) 648 | { 649 | return err_code; 650 | } 651 | 652 | // Add Custom Value characteristic 653 | return custom_value_char_add(p_cus, p_cus_init); 654 | } 655 | ``` 656 | Compile the project and flash it to you nRF5x DK. If you open the nRF Connect app on your smartphone, scan and connect to the device, you should see that the characteristic has been added by clicking on the service, as shown in the screenshot below. 657 | 658 | Service and Characteristic | 659 | ------------ | 660 | | 661 | 662 | 663 | ### Step 5 - Handling events from the SoftDevice. 664 | Great, we now have a Custom Service and a Custom Value Characteristic, but we want to be able to write to the characteristic and perform a specific task based on the value that was written to the characteristic, e.g. turn on a LED. However, before we can do that we need to do some event handling in ble_cus.h and ble_cus.c. 665 | 666 | 667 | Lastly, we're going to add the ble_cus_on_ble_evt function decleration to ble_cus.h, which will handle the events of the ble_cus_evt_type_t from our service. 668 | 669 | ```c 670 | /* This code belongs in ble_cus.h*/ 671 | 672 | /**@brief Function for handling the Application's BLE Stack events. 673 | * 674 | * @details Handles all events from the BLE stack of interest to the Battery Service. 675 | * 676 | * @note 677 | * 678 | * @param[in] p_ble_evt Event received from the BLE stack. 679 | * @param[in] p_context Custom Service structure. 680 | */ 681 | void ble_cus_on_ble_evt( ble_evt_t const * p_ble_evt, void * p_context); 682 | ``` 683 | 684 | We're now going to implement the ble_cus_on_ble_evt event handler in ble_cus.c.Upon entry its considered good practice to check that none of the pointers that we provided as arguments are NULL. 685 | 686 | ```c 687 | /* This code belongs in ble_cus.c*/ 688 | 689 | void ble_cus_on_ble_evt( ble_evt_t const * p_ble_evt, void * p_context) 690 | { 691 | ble_cus_t * p_cus = (ble_cus_t *) p_context; 692 | 693 | if (p_cus == NULL || p_ble_evt == NULL) 694 | { 695 | return; 696 | } 697 | } 698 | ``` 699 | 700 | After the NULL check we're going to add a switch-case to check which event that has been propagated to the application by the SoftDevice. 701 | 702 | ```c 703 | /* This code belongs in ble_cus.c*/ 704 | 705 | void ble_cus_on_ble_evt( ble_evt_t const * p_ble_evt, void * p_context) 706 | { 707 | ble_cus_t * p_cus = (ble_cus_t *) p_context; 708 | 709 | if (p_cus == NULL || p_ble_evt == NULL) 710 | { 711 | return; 712 | } 713 | 714 | switch (p_ble_evt->header.evt_id) 715 | { 716 | case BLE_GAP_EVT_CONNECTED: 717 | break; 718 | 719 | case BLE_GAP_EVT_DISCONNECTED: 720 | break; 721 | 722 | default: 723 | // No implementation needed. 724 | break; 725 | } 726 | } 727 | ``` 728 | 729 | For now we only need to care about the BLE_GAP_EVT_CONNECTED and BLE_GAP_EVT_DISCONNECTED events. So let's create the two functions on_connect() and on_disconnect(), starting with on_connect(). The only thing we need to do when we get the Connect event is to assign the connection handle in the Custom Service structure to the connection handle that is passed with the event. 730 | 731 | ```c 732 | /* This code belongs in ble_cus.c*/ 733 | 734 | /**@brief Function for handling the Connect event. 735 | * 736 | * @param[in] p_cus Custom Service structure. 737 | * @param[in] p_ble_evt Event received from the BLE stack. 738 | */ 739 | static void on_connect(ble_cus_t * p_cus, ble_evt_t const * p_ble_evt) 740 | { 741 | p_cus->conn_handle = p_ble_evt->evt.gap_evt.conn_handle; 742 | } 743 | ``` 744 | 745 | Similarly, when we get the Disconnect event, the only thing we need to do is invalidate the connection handle in the Custom Service structure since the connection is now dead. 746 | 747 | ```c 748 | /* This code belongs in ble_cus.c*/ 749 | 750 | /**@brief Function for handling the Disconnect event. 751 | * 752 | * @param[in] p_cus Custom Service structure. 753 | * @param[in] p_ble_evt Event received from the BLE stack. 754 | */ 755 | static void on_disconnect(ble_cus_t * p_cus, ble_evt_t const * p_ble_evt) 756 | { 757 | UNUSED_PARAMETER(p_ble_evt); 758 | p_cus->conn_handle = BLE_CONN_HANDLE_INVALID; 759 | } 760 | ``` 761 | 762 | Now that we have one function for each event we only need to call the function in ble_cus_on_ble_evt, i.e. 763 | 764 | ```c 765 | /* This code belongs in ble_cus.c*/ 766 | 767 | void ble_cus_on_ble_evt( ble_evt_t const * p_ble_evt, void * p_context) 768 | { 769 | ble_cus_t * p_cus = (ble_cus_t *) p_context; 770 | 771 | if (p_cus == NULL || p_ble_evt == NULL) 772 | { 773 | return; 774 | } 775 | 776 | switch (p_ble_evt->header.evt_id) 777 | { 778 | case BLE_GAP_EVT_CONNECTED: 779 | on_connect(p_cus, p_ble_evt); 780 | break; 781 | 782 | case BLE_GAP_EVT_DISCONNECTED: 783 | on_disconnect(p_cus, p_ble_evt); 784 | break; 785 | 786 | default: 787 | // No implementation needed. 788 | break; 789 | } 790 | } 791 | ``` 792 | 793 | The last thing we have to do is to make sure that our ble_cus_on_ble_evt event handler function receives SoftDevice events. This is done registering the ble_cus_on_ble_evt event handler as event observer using the NRF_SDH_BLE_OBSERVER() macro. It is convenient to do this within the BLE_CUS_DEF macro that we defined in ble_cus.h, which should be modfied as shown below 794 | 795 | ```c 796 | /* This code belongs in ble_cus.h*/ 797 | 798 | #define BLE_CUS_DEF(_name) \ 799 | static ble_cus_t _name; \ 800 | NRF_SDH_BLE_OBSERVER(_name ## _obs, \ 801 | BLE_HRS_BLE_OBSERVER_PRIO, \ 802 | ble_cus_on_ble_evt, &_name) 803 | 804 | ``` 805 | 806 | Compile your project and verify that there are no errors before you proceed to the next step. 807 | 808 | ### Step 6 - Handling the Write event from the SoftDevice. 809 | 810 | Ok, now we really want to be able to write to the characteristic and perform a specific task based on the value that was written to the characteristic, e.g. turn on a LED. How are we going to that? You guessed it! More event handling! 811 | 812 | Whenever a characteristic is written to, a BLE_GATTS_EVT_WRITE event will be propagated to the application and dispatched to the functions in ble_evt_dispatch(). So this means that we need to add another case to our ble_cus_on_ble_evt switch-case statement, namely BLE_GATTS_EVT_WRITE 813 | 814 | 815 | ```c 816 | /* This code belongs in ble_cus.c*/ 817 | 818 | void ble_cus_on_ble_evt( ble_evt_t const * p_ble_evt, void * p_context) 819 | { 820 | ble_cus_t * p_cus = (ble_cus_t *) p_context; 821 | 822 | if (p_cus == NULL || p_ble_evt == NULL) 823 | { 824 | return; 825 | } 826 | 827 | switch (p_ble_evt->header.evt_id) 828 | { 829 | case BLE_GAP_EVT_CONNECTED: 830 | on_connect(p_cus, p_ble_evt); 831 | break; 832 | 833 | case BLE_GAP_EVT_DISCONNECTED: 834 | on_disconnect(p_cus, p_ble_evt); 835 | break; 836 | case BLE_GATTS_EVT_WRITE: 837 | break; 838 | default: 839 | // No implementation needed. 840 | break; 841 | } 842 | } 843 | ``` 844 | 845 | Just like we did for the BLE_GAP_EVT_CONNECTED and BLE_GAP_EVT_DISCONNECTED we're going to create a on_write function that should be called when we get the Write event. 846 | 847 | ```c 848 | /* This code belongs in ble_cus.c*/ 849 | 850 | /**@brief Function for handling the Write event. 851 | * 852 | * @param[in] p_cus Custom Service structure. 853 | * @param[in] p_ble_evt Event received from the BLE stack. 854 | */ 855 | static void on_write(ble_cus_t * p_cus, ble_evt_t const * p_ble_evt) 856 | { 857 | 858 | } 859 | ``` 860 | 861 | Now, once we get the Write event we have to get hold of the Write event parameters that are passed with the event and we have to verify that the the handle that is written to matches the Custom Value Characteristic handle, i.e. 862 | 863 | ```c 864 | /* This code belongs in ble_cus.c*/ 865 | 866 | static void on_write(ble_cus_t * p_cus, ble_evt_t const * p_ble_evt) 867 | { 868 | ble_gatts_evt_write_t * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write; 869 | 870 | // Check if the handle passed with the event matches the Custom Value Characteristic handle. 871 | if (p_evt_write->handle == p_cus->custom_value_handles.value_handle) 872 | { 873 | // Put specific task here. 874 | } 875 | } 876 | ``` 877 | 878 | So lets say that our specifc task is to toggle a LED on the nRF5x DK every time the Custom Value Characteristic is written to. We can do this by calling nrf_gpio_pin_toggle on one of the pins connected to the nRF5x DK LEDs, e.g. LED4. In order to do this we'll have to include boards.h and nrf_gpio.h in ble_cus.h as well as call nrf_gpio_pin_toggle in the on_write function 879 | 880 | 881 | ```c 882 | /* This code belongs in ble_cus.c*/ 883 | 884 | static void on_write(ble_cus_t * p_cus, ble_evt_t const * p_ble_evt) 885 | { 886 | ble_gatts_evt_write_t * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write; 887 | 888 | // Check if the handle passed with the event matches the Custom Value Characteristic handle. 889 | if (p_evt_write->handle == p_cus->custom_value_handles.value_handle) 890 | { 891 | nrf_gpio_pin_toggle(LED_4); 892 | } 893 | } 894 | ``` 895 | 896 | We'we now implemented the necessary event handling so on_write() should be added to the ble_cus_on_ble_evt() function under the BLE_GATTS_EVT_WRITE case, i.e. 897 | 898 | 899 | ```c 900 | /* This code belongs in ble_cus.c*/ 901 | 902 | void ble_cus_on_ble_evt( ble_evt_t const * p_ble_evt, void * p_context) 903 | { 904 | ble_cus_t * p_cus = (ble_cus_t *) p_context; 905 | 906 | if (p_cus == NULL || p_ble_evt == NULL) 907 | { 908 | return; 909 | } 910 | 911 | switch (p_ble_evt->header.evt_id) 912 | { 913 | case BLE_GAP_EVT_CONNECTED: 914 | on_connect(p_cus, p_ble_evt); 915 | break; 916 | 917 | case BLE_GAP_EVT_DISCONNECTED: 918 | on_disconnect(p_cus, p_ble_evt); 919 | break; 920 | case BLE_GATTS_EVT_WRITE: 921 | on_write(p_cus, p_ble_evt); 922 | break; 923 | default: 924 | // No implementation needed. 925 | break; 926 | } 927 | } 928 | ``` 929 | 930 | However, all this will be for nothing if we do not to allow the peer to actually write and/or read from the characteristic value. This is done by adding two lines to services_init() in main.c before we call ble_cus_init(). 931 | 932 | ```c 933 | /* This code belongs in services_init() in main.c*/ 934 | 935 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cus_init.custom_value_char_attr_md.read_perm); 936 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cus_init.custom_value_char_attr_md.write_perm); 937 | ``` 938 | 939 | These two lines sets the write and read permissions to the characteristic value attribute to open, i.e. the peer is allowed to write/read the value without encrypting the link first. Now, try writting to the characteristic using nRF Connect for Desktop or Android/iOS. Every time a value is written to the characteristic, LED4 on the nRF5x DK should toogle. 940 | 941 | Write button | Write value | 942 | ------------ | ------------- | 943 | | | 944 | 945 | 946 | **Challenge 1:** p_evt_write also has a data field. Use the data to decide if the LED is to be turned on or off. 947 | 948 | ### Step 7 - Propagating Custom Service events to the application 949 | 950 | Until now we've only handled the events that are propagated by the SoftDevice, but in some cases it makes sense to propagate events to the application. 951 | 952 | In order to do this we need to add an event handler to our Custom Service Init structure and Custom Service structure 953 | 954 | ```c 955 | /* This code belongs in ble_cus.h*/ 956 | 957 | /**@brief Custom Service init structure. This contains all options and data needed for 958 | * initialization of the service.*/ 959 | typedef struct 960 | { 961 | ble_cus_evt_handler_t evt_handler; /**< Event handler to be called for handling events in the Custom Service. */ 962 | uint8_t initial_custom_value; /**< Initial custom value */ 963 | ble_srv_cccd_security_mode_t custom_value_char_attr_md; /**< Initial security level for Custom characteristics attribute */ 964 | } ble_cus_init_t; 965 | 966 | /**@brief Custom Service structure. This contains various status information for the service. */ 967 | struct ble_cus_s 968 | { 969 | ble_cus_evt_handler_t evt_handler; /**< Event handler to be called for handling events in the Custom Service. */ 970 | uint16_t service_handle; /**< Handle of Custom Service (as provided by the BLE stack). */ 971 | ble_gatts_char_handles_t custom_value_handles; /**< Handles related to the Custom Value characteristic. */ 972 | uint16_t conn_handle; /**< Handle of the current connection (as provided by the BLE stack, is BLE_CONN_HANDLE_INVALID if not in a connection). */ 973 | uint8_t uuid_type; 974 | }; 975 | ``` 976 | 977 | After adding the event handler to the structures we must make sure that we initialize our service correctly in ble_cus_init() 978 | 979 | ```c 980 | /* This code belongs in ble_cus_init() in ble_cus.c*/ 981 | 982 | // Initialize service structure 983 | p_cus->evt_handler = p_cus_init->evt_handler; 984 | p_cus->conn_handle = BLE_CONN_HANDLE_INVALID; 985 | ``` 986 | 987 | Next, we need to declare an event type specific to our service 988 | 989 | ```c 990 | /* This code belongs in ble_cus.h*/ 991 | 992 | typedef enum 993 | { 994 | BLE_CUS_EVT_DISCONNECTED, 995 | BLE_CUS_EVT_CONNECTED 996 | } ble_cus_evt_type_t; 997 | ``` 998 | For now we're only going to add the BLE_CUS_EVT_CONNECTED and BLE_CUS_EVT_DISCONNECTED events, but we'll add some additional events later in the tutorial. 999 | 1000 | After declaring the event type we need to declare an event structure that holds a ble_cus_evt_type_t event, i.e. 1001 | 1002 | ```c 1003 | /* This code belongs in ble_cus.h*/ 1004 | 1005 | /**@brief Custom Service event. */ 1006 | typedef struct 1007 | { 1008 | ble_cus_evt_type_t evt_type; /**< Type of event. */ 1009 | } ble_cus_evt_t; 1010 | ``` 1011 | 1012 | Next, we need declare the Custom Service event handler type 1013 | 1014 | ```c 1015 | /* This code belongs in ble_cus.h*/ 1016 | 1017 | /**@brief Custom Service event handler type. */ 1018 | typedef void (*ble_cus_evt_handler_t) (ble_cus_t * p_cus, ble_cus_evt_t * p_evt); 1019 | ``` 1020 | 1021 | Now, back in main we're going to create the event handler function on_cus_evt, which takes the same parameters as the ble_cus_evt_handler_t type. 1022 | 1023 | ```c 1024 | /* This code belongs in main.c*/ 1025 | 1026 | /**@brief Function for handling the Custom Service Service events. 1027 | * 1028 | * @details This function will be called for all Custom Service events which are passed to 1029 | * the application. 1030 | * 1031 | * @param[in] p_cus_service Custom Service structure. 1032 | * @param[in] p_evt Event received from the Custom Service. 1033 | * 1034 | */ 1035 | static void on_cus_evt(ble_cus_t * p_cus_service, 1036 | ble_cus_evt_t * p_evt) 1037 | { 1038 | switch(p_evt->evt_type) 1039 | { 1040 | case BLE_CUS_EVT_CONNECTED: 1041 | break; 1042 | 1043 | case BLE_CUS_EVT_DISCONNECTED: 1044 | break; 1045 | 1046 | default: 1047 | // No implementation needed. 1048 | break; 1049 | } 1050 | } 1051 | ``` 1052 | 1053 | Now, in order to propagate events from our service we need to assign the on_cus_evt function as the event handler function of our service when we initialize the Custom Service. This is done by setting the .evt_handler field of the cus_init struct equal to on_cus_evt in services_init() in main.c, i.e. 1054 | 1055 | ```c 1056 | /* This code belongs in services_init in main.c*/ 1057 | 1058 | // Set the cus event handler 1059 | cus_init.evt_handler = on_cus_evt; 1060 | ``` 1061 | 1062 | We can now invoke this event handler from ble_cus.c by calling p_cus->evt_handler(p_cus, &evt) and as an example we'll invoke the event handler when we get the BLE_GAP_EVT_CONNECTED event, i.e. in the on_connect() function. It's fairly straight forward, we simply declare a ble_cus_evt_t variable and set its .evt_type field to the BLE_CUS_EVT_CONNECTED and then invoke the event handler with said event. 1063 | 1064 | ```c 1065 | /* This code belongs in ble_cus.c*/ 1066 | 1067 | static void on_connect(ble_cus_t * p_cus, ble_evt_t const * p_ble_evt) 1068 | { 1069 | p_cus->conn_handle = p_ble_evt->evt.gap_evt.conn_handle; 1070 | 1071 | ble_cus_evt_t evt; 1072 | 1073 | evt.evt_type = BLE_CUS_EVT_CONNECTED; 1074 | 1075 | p_cus->evt_handler(p_cus, &evt); 1076 | } 1077 | ``` 1078 | 1079 | ### Step 8 - Notifying the Custom Value Characteristic 1080 | 1081 | Next, we're going to add the ble_cus_custom_value_update function decleration to the ble_cus.h file, which we're going to use to update our Custom Value Characteristic. 1082 | 1083 | ```c 1084 | /* This code belongs in ble_cus.h*/ 1085 | 1086 | /**@brief Function for updating the custom value. 1087 | * 1088 | * @details The application calls this function when the cutom value should be updated. If 1089 | * notification has been enabled, the custom value characteristic is sent to the client. 1090 | * 1091 | * @note 1092 | * 1093 | * @param[in] p_cus Custom Service structure. 1094 | * @param[in] Custom value 1095 | * 1096 | * @return NRF_SUCCESS on success, otherwise an error code. 1097 | */ 1098 | 1099 | uint32_t ble_cus_custom_value_update(ble_cus_t * p_cus, uint8_t custom_value); 1100 | ``` 1101 | Back in ble_cus.c, we're going to continue the good practice of checking that the pointer we passed as an argument is'nt NULL 1102 | 1103 | ```c 1104 | /* This code belongs in ble_cus.c*/ 1105 | 1106 | uint32_t ble_cus_custom_value_update(ble_cus_t * p_cus, uint8_t custom_value){ 1107 | if (p_cus == NULL) 1108 | { 1109 | return NRF_ERROR_NULL; 1110 | } 1111 | } 1112 | ``` 1113 | 1114 | Next, we're going to update the value in the GATT table with our custom_value that we passed as an argument to the ble_cus_custom_value_update() function. 1115 | 1116 | ```c 1117 | /* This code belongs in ble_cus_custom_value_update() in ble_cus.c*/ 1118 | 1119 | uint32_t err_code = NRF_SUCCESS; 1120 | ble_gatts_value_t gatts_value; 1121 | 1122 | // Initialize value struct. 1123 | memset(&gatts_value, 0, sizeof(gatts_value)); 1124 | 1125 | gatts_value.len = sizeof(uint8_t); 1126 | gatts_value.offset = 0; 1127 | gatts_value.p_value = &custom_value; 1128 | 1129 | // Update database. 1130 | err_code = sd_ble_gatts_value_set(p_cus->conn_handle, 1131 | p_cus->custom_value_handles.value_handle, 1132 | &gatts_value); 1133 | if (err_code != NRF_SUCCESS) 1134 | { 1135 | return err_code; 1136 | } 1137 | ``` 1138 | 1139 | After updating the value in the GATT table we're ready to notify our peer that the value of our Custom Value Characteristic has changed. 1140 | 1141 | ```c 1142 | /* This code belongs in ble_cus_custom_value_update() in ble_cus.c*/ 1143 | 1144 | // Send value if connected and notifying. 1145 | if ((p_cus->conn_handle != BLE_CONN_HANDLE_INVALID)) 1146 | { 1147 | ble_gatts_hvx_params_t hvx_params; 1148 | 1149 | memset(&hvx_params, 0, sizeof(hvx_params)); 1150 | 1151 | hvx_params.handle = p_cus->custom_value_handles.value_handle; 1152 | hvx_params.type = BLE_GATT_HVX_NOTIFICATION; 1153 | hvx_params.offset = gatts_value.offset; 1154 | hvx_params.p_len = &gatts_value.len; 1155 | hvx_params.p_data = gatts_value.p_value; 1156 | 1157 | err_code = sd_ble_gatts_hvx(p_cus->conn_handle, &hvx_params); 1158 | } 1159 | else 1160 | { 1161 | err_code = NRF_ERROR_INVALID_STATE; 1162 | } 1163 | 1164 | return err_code; 1165 | 1166 | ``` 1167 | The first thing we should do is to verify that we have a valid connection handle, i.e. that we're actually connected to a peer, if not we should return an error indicating that we're in an invalid state. Next, we set the ble_gatts_hvx_params_t, which contains the handle of custom value attribute, the type(i.e. if its a Notification or Indication), the value offsett( only used if its larger than 20bytes), the length and the data. After setting the hvx_params, we notify the peer by calling sd_ble_gatts_hvx(). Lastly, we should return the error code. 1168 | 1169 | After adding the code snippets above ble_cus_custom_value_update() should look like below 1170 | 1171 | ```c 1172 | /* This code belongs in ble_cus.c*/ 1173 | 1174 | uint32_t ble_cus_custom_value_update(ble_cus_t * p_cus, uint8_t custom_value) 1175 | { 1176 | NRF_LOG_INFO("In ble_cus_custom_value_update. \r\n"); 1177 | if (p_cus == NULL) 1178 | { 1179 | return NRF_ERROR_NULL; 1180 | } 1181 | 1182 | uint32_t err_code = NRF_SUCCESS; 1183 | ble_gatts_value_t gatts_value; 1184 | 1185 | // Initialize value struct. 1186 | memset(&gatts_value, 0, sizeof(gatts_value)); 1187 | 1188 | gatts_value.len = sizeof(uint8_t); 1189 | gatts_value.offset = 0; 1190 | gatts_value.p_value = &custom_value; 1191 | 1192 | // Update database. 1193 | err_code = sd_ble_gatts_value_set(p_cus->conn_handle, 1194 | p_cus->custom_value_handles.value_handle, 1195 | &gatts_value); 1196 | if (err_code != NRF_SUCCESS) 1197 | { 1198 | return err_code; 1199 | } 1200 | 1201 | // Send value if connected and notifying. 1202 | if ((p_cus->conn_handle != BLE_CONN_HANDLE_INVALID)) 1203 | { 1204 | ble_gatts_hvx_params_t hvx_params; 1205 | 1206 | memset(&hvx_params, 0, sizeof(hvx_params)); 1207 | 1208 | hvx_params.handle = p_cus->custom_value_handles.value_handle; 1209 | hvx_params.type = BLE_GATT_HVX_NOTIFICATION; 1210 | hvx_params.offset = gatts_value.offset; 1211 | hvx_params.p_len = &gatts_value.len; 1212 | hvx_params.p_data = gatts_value.p_value; 1213 | 1214 | err_code = sd_ble_gatts_hvx(p_cus->conn_handle, &hvx_params); 1215 | } 1216 | else 1217 | { 1218 | err_code = NRF_ERROR_INVALID_STATE; 1219 | } 1220 | 1221 | 1222 | return err_code; 1223 | } 1224 | ``` 1225 | 1226 | Like the characteristic metadata we need to set the metadata of the CCCD in custom_value_char_add(), which is done by adding the following snippet before the characteristic metadata is set. 1227 | 1228 | ```c 1229 | /* This code belongs in custom_value_char_add() in ble_cus.c*/ 1230 | 1231 | memset(&cccd_md, 0, sizeof(cccd_md)); 1232 | 1233 | // Read operation on Cccd should be possible without authentication. 1234 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm); 1235 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm); 1236 | 1237 | cccd_md.vloc = BLE_GATTS_VLOC_STACK; 1238 | ``` 1239 | We're setting the read and write permissions to the CCCD to open, i.e. no encryption is needed to write or read from the CCCD. The .vloc option is set to BLE_GATTS_VLOC_STACK, which means that the value of the CCCD will be stored in SoftDevice memory section and not in the application memory section. 1240 | 1241 | The next step is to add the Notify property to the Custom Value Characteristic and add a pointer to the CCCD metadata in the characteristic metadata. This is done by modifying the characteristic metadata properties in custom_value_char_add() function , i.e. setting .notify to 1 and .p_cccd_md to point to the CCCD metadata struct. 1242 | 1243 | ```c 1244 | /* This code belongs in custom_value_char_add() in ble_cus.c*/ 1245 | char_md.char_props.notify = 1; 1246 | char_md.p_cccd_md = &cccd_md; 1247 | ``` 1248 | 1249 | This will add a Client Characteristic Configuration Descriptor or CCCD to the Custom Value Characteristic which allows us to enable or disable notifications by writing to the CCCD. Notification is by default disabled and in order to enable it we have to write 0x0001 to the CCCD. Remember that everytime we write to a characteristic or one of its descriptors, we will get a Write event, thus we need to handle the case where a peer writes to the CCCD in the on_write() function. 1250 | 1251 | However, before modifying the on_write() function we need do is to add two additional event, BLE_CUS_EVT_NOTIFICATION_ENABLED and BLE_CUS_EVT_NOTIFICATION_DISABLED, to the ble_cus_evt_type_t enumeration in ble_cus.h 1252 | 1253 | ```c 1254 | /* This code belongs in ble_cus.h*/ 1255 | 1256 | /**@brief Custom Service event type. */ 1257 | typedef enum 1258 | { 1259 | BLE_CUS_EVT_NOTIFICATION_ENABLED, /**< Custom value notification enabled event. */ 1260 | BLE_CUS_EVT_NOTIFICATION_DISABLED, /**< Custom value notification disabled event. */ 1261 | BLE_CUS_EVT_DISCONNECTED, 1262 | BLE_CUS_EVT_CONNECTED 1263 | } ble_cus_evt_type_t; 1264 | ``` 1265 | 1266 | Back to the on_write() function, the first thing we should do is to add a if-statement that checks if the handle that is written to matches the handle of the CCCD and that the value has the correct length. If we pass this check we need to check wether notifications has been enabled or not. 1267 | 1268 | ```c 1269 | /* This code belongs in on_write() in ble_cus.c*/ 1270 | 1271 | // Check if the Custom value CCCD is written to and that the value is the appropriate length, i.e 2 bytes. 1272 | if ((p_evt_write->handle == p_cus->custom_value_handles.cccd_handle) 1273 | && (p_evt_write->len == 2) 1274 | ) 1275 | { 1276 | 1277 | // CCCD written, call application event handler 1278 | if (p_cus->evt_handler != NULL) 1279 | { 1280 | ble_cus_evt_t evt; 1281 | 1282 | if (ble_srv_is_notification_enabled(p_evt_write->data)) 1283 | { 1284 | evt.evt_type = BLE_CUS_EVT_NOTIFICATION_ENABLED; 1285 | } 1286 | else 1287 | { 1288 | evt.evt_type = BLE_CUS_EVT_NOTIFICATION_DISABLED; 1289 | } 1290 | // Call the application event handler. 1291 | p_cus->evt_handler(p_cus, &evt); 1292 | } 1293 | 1294 | } 1295 | ``` 1296 | 1297 | We do not actually need to enable notifications as this is done automatically by the SoftDevice. However, the SoftDevice will propagate the write event and based on this event we can decide if its ok to start notifying characteristic values or not. 1298 | 1299 | Adding the code-snippet above to the on_write() function should result in the following function. 1300 | 1301 | ```c 1302 | /* This code belongs in ble_cus.c*/ 1303 | 1304 | static void on_write(ble_cus_t * p_cus, ble_evt_t const * p_ble_evt) 1305 | { 1306 | ble_gatts_evt_write_t * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write; 1307 | 1308 | // Custom Value Characteristic Written to. 1309 | if (p_evt_write->handle == p_cus->custom_value_handles.value_handle) 1310 | { 1311 | nrf_gpio_pin_toggle(LED_4); 1312 | } 1313 | 1314 | // Check if the Custom value CCCD is written to and that the value is the appropriate length, i.e 2 bytes. 1315 | if ((p_evt_write->handle == p_cus->custom_value_handles.cccd_handle) 1316 | && (p_evt_write->len == 2) 1317 | ) 1318 | { 1319 | // CCCD written, call application event handler 1320 | if (p_cus->evt_handler != NULL) 1321 | { 1322 | ble_cus_evt_t evt; 1323 | 1324 | if (ble_srv_is_notification_enabled(p_evt_write->data)) 1325 | { 1326 | evt.evt_type = BLE_CUS_EVT_NOTIFICATION_ENABLED; 1327 | } 1328 | else 1329 | { 1330 | evt.evt_type = BLE_CUS_EVT_NOTIFICATION_DISABLED; 1331 | } 1332 | // Call the application event handler. 1333 | p_cus->evt_handler(p_cus, &evt); 1334 | } 1335 | } 1336 | } 1337 | ``` 1338 | 1339 | Now the last thing we have to do is to add the BLE_CUS_EVT_NOTIFICATION_ENABLED and BLE_CUS_EVT_NOTIFICATION_DISABLED to the on_cus_evt() event handler in main.c, i.e. 1340 | 1341 | ```c 1342 | /* This code belongs in main.c*/ 1343 | 1344 | static void on_cus_evt(ble_cus_t * p_cus_service, 1345 | ble_cus_evt_t * p_evt) 1346 | { 1347 | 1348 | switch(p_evt->evt_type) 1349 | { 1350 | case BLE_CUS_EVT_NOTIFICATION_ENABLED: 1351 | break; 1352 | 1353 | case BLE_CUS_EVT_NOTIFICATION_DISABLED: 1354 | break; 1355 | 1356 | case BLE_CUS_EVT_CONNECTED : 1357 | break; 1358 | 1359 | case BLE_CUS_EVT_DISCONNECTED: 1360 | break; 1361 | 1362 | default: 1363 | // No implementation needed. 1364 | break; 1365 | } 1366 | } 1367 | ``` 1368 | Compile the project and flash it to you nRF5x DK. If you open the nRF Connect app and connect to the device you should see that a Client Characteristic Configuration Descriptor has been added under the characteristic as shown in the screen shot below. 1369 | 1370 | Service, Characteristic and Descriptor | 1371 | ------------ | 1372 | | 1373 | 1374 | We're now ready to notify some values from the nRF52 DK to the nRF Connect app. In order to do that we're going to create an application timer that calls ble_cus_custom_value_update() at a regular interval and then start it when we get the BLE_CUS_EVT_NOTIFICATION_ENABLED event. So first, add the following define to the top of main.c 1375 | 1376 | ```c 1377 | /* This code belongs in main.c*/ 1378 | #define NOTIFICATION_INTERVAL APP_TIMER_TICKS(1000) 1379 | ``` 1380 | 1381 | which is going to the timeout interval of our application timer. Next you will need to create an application timer id with the name m_notification_timer_id by calling the APP_TIMER_DEF below the defines in main.c, i.e. 1382 | ```c 1383 | /* This code belongs in main.c*/ 1384 | APP_TIMER_DEF(m_notification_timer_id); 1385 | ``` 1386 | 1387 | Below this macro call you can declare a unsigned 8-bit variable called m_custom_value, i.e. 1388 | 1389 | ```c 1390 | /* This code belongs in main.c*/ 1391 | static uint8_t m_custom_value = 0; 1392 | ``` 1393 | 1394 | which will be used to hold the custom value we're going to notify to the nRF Connect app. Next, find the timers_init() functino in main.c and add the folling snippet to create the application timer 1395 | 1396 | ```c 1397 | /* This code belongs in timers_init() in main.c*/ 1398 | // Create timers. 1399 | err_code = app_timer_create(&m_notification_timer_id, APP_TIMER_MODE_REPEATED, notification_timeout_handler); 1400 | APP_ERROR_CHECK(err_code); 1401 | ``` 1402 | 1403 | Great, we now have a application timer and the next step is to start the application timer when we receive the BLE_CUS_EVT_NOTIFICATION_ENABLED, i.e. add the following snippet under the BLE_CUS_EVT_NOTIFICATION_ENABLED case in the on_cus_evt event handler in main.c 1404 | 1405 | ```c 1406 | /* This code belongs in on_cus_evt() in main.c*/ 1407 | err_code = app_timer_start(m_notification_timer_id, NOTIFICATION_INTERVAL, NULL); 1408 | APP_ERROR_CHECK(err_code); 1409 | ``` 1410 | 1411 | Similarly, we want the notification timer to stop when we get the BLE_CUS_EVT_NOTIFICATION_DISABLED event. Thus, we add the following snippet to the BLE_CUS_EVT_NOTIFICATION_DISABLED case in the on_cus_evt event handler in main.c 1412 | 1413 | ```c 1414 | /* This code belongs in on_cus_evt() in main.c*/ 1415 | err_code = app_timer_stop(m_notification_timer_id); 1416 | APP_ERROR_CHECK(err_code); 1417 | ``` 1418 | 1419 | Lastly, we need to create the notification_timeout_handler which will increment the m_custom_value variable and then call ble_cus_custom_value_update by declaring the following function above timers_init() in main.c 1420 | 1421 | ```c 1422 | /* This code belongs in main.c*/ 1423 | 1424 | /**@brief Function for handling the Battery measurement timer timeout. 1425 | * 1426 | * @details This function will be called each time the battery level measurement timer expires. 1427 | * 1428 | * @param[in] p_context Pointer used for passing some arbitrary information (context) from the 1429 | * app_start_timer() call to the timeout handler. 1430 | */ 1431 | static void notification_timeout_handler(void * p_context) 1432 | { 1433 | UNUSED_PARAMETER(p_context); 1434 | ret_code_t err_code; 1435 | 1436 | // Increment the value of m_custom_value before nortifing it. 1437 | m_custom_value++; 1438 | 1439 | err_code = ble_cus_custom_value_update(&m_cus, m_custom_value); 1440 | APP_ERROR_CHECK(err_code); 1441 | } 1442 | ``` 1443 | 1444 | Compile the project and flash it to your nRF52 DK. Open the nRF Connect app, connect to the nRF52 DK and enable notification by clicking the button highlighted in the screenshot below 1445 | 1446 | Enable Notifications | 1447 | ------------ | 1448 | | 1449 | 1450 | You should now see a value field appear below the Unknown Characteristics properties and the value should be incrementing every second. Congratulations you have now created a custom service and notified custom values! 1451 | 1452 | ## Task 2 - Controlling a Servo using BLE 1453 | 1454 | In this task we will use the custom service and its characteristic to control a servo using Pulse-Width Modulation. The PWM library in the nRF SDK uses one of the nRF52s TIMER peripherals in addition to the PPI and GPIOTE peripherals. The app_pwm library is documented on this Infocenter page. 1455 | 1456 | ### Step 1 - Connecting the Servo to the nRF52 DK 1457 | 1458 | There are three wires coming from the SG90 Servo: 1459 | 1460 | Brown: Ground - Should be connected to one of the pins marked GND on your nRF52 DK. 1461 | 1462 | Red: 5V - Should be connected to the pin marked 5V on your nRF52 DK. 1463 | 1464 | Orange: PWM Control Signal - Should be connected to one of the unused GPIO pins of the nRF52 DK (for example P0.04, pin number 4). 1465 | 1466 | ### Step 2 - Adding the necessary libraries and drivers 1467 | 1468 | In order to use the library we need to add the necessary .c and .h files to our project. 1469 | 1470 | app_pwm.h - Add #include "app_pwm.h" to the other include statements at the top of main.c 1471 | 1472 | app_pwm.c - Right-click the nRF_Libraries folder in the Project Explorer and select "Add Existing File ...". Navigate to the nRF5_SDK_15.0.0_a53641a\components\libraries\pwm folder and add app_pwm.c 1473 | 1474 | nrf_drv_ppi.c - Right-click the nRF_Drivers folder in the Project Explorer and select "Add Existing File ...". 1475 | Navigate to the nRF5_SDK_15.0.0_a53641a\integration\nrfx\legacy folder and add nrf_drv_ppi.c 1476 | 1477 | nrfx_timer.c - Right-click the nRF_Drivers folder in the Project Explorer and select "Add Existing File ...". 1478 | Navigate to the nRF5_SDK\nRF5_SDK_15.0.0_a53641a\modules\nrfx\drivers\src folder and add nrfx_timer.c 1479 | 1480 | nrfx_ppi.c - Right-click the nRF_Drivers folder in the Project Explorer and select "Add Existing File ...". 1481 | Navigate to the nRF5_SDK\nRF5_SDK_15.0.0_a53641a\modules\nrfx\drivers\src folder and add nrfx_ppi.c 1482 | 1483 | ### 3 - Enabling the added libraries and drivers in sdk_config.h 1484 | 1485 | In order for the module to be compiled when we build the project we need to enable the modules in the sdk_config.h file. Open the sdk_config file, search for the defines below and set the value to 1. 1486 | 1487 | nRF_Drivers in sdk_config.h 1488 | 1489 | PPI_ENABLED 1 1490 | 1491 | TIMER_ENABLED 1 1492 | 1493 | TIMER1_ENABLED 1 1494 | 1495 | nRF_Libraries in sdk_config.h 1496 | 1497 | APP_PWM_ENABLED 1 1498 | 1499 | ### Step 3 - Adding PWM code to main.c 1500 | 1501 | First we need to create a PWM instance that uses TIMER 1 to generate the PWM signal. This is done by adding APP_PWM_INSTANCE macro with the following parameters to main.c at the same place where we added BLE_CUS_DEF(m_cus) 1502 | ```c 1503 | //PMW instance 1504 | APP_PWM_INSTANCE(PWM1, 1); // Setup a PWM instance with TIMER 1 1505 | ``` 1506 | 1507 | Next, we need to create a function called pwm_init where we initialize the pwm library. Add the following function right above main() in main.c 1508 | ```c 1509 | static void pwm_init() 1510 | { 1511 | ret_code_t err_code; 1512 | 1513 | // Configure the PWM library 1514 | app_pwm_config_t pwm1_cfg = { 1515 | .pins = {4, APP_PWM_NOPIN}, // Use pin 4 as the PWM pin and setting the second pin to APP_PWM_NOPIN, i.e. not used 1516 | .pin_polarity = {APP_PWM_POLARITY_ACTIVE_HIGH, APP_PWM_POLARITY_ACTIVE_LOW}, // Set the polarity of the pin to high, i.e. if the duty cycle is set to 10% the pin will be high 10% of the time and off 90% 1517 | .num_of_channels = 1, // Set the channel number, i.e. the number of pins to 1 1518 | .period_us = 20000L // Set the PWM period to 20msec. 1519 | }; 1520 | 1521 | err_code = app_pwm_init(&PWM1,&pwm1_cfg,NULL); 1522 | APP_ERROR_CHECK(err_code); 1523 | 1524 | app_pwm_enable(&PWM1); 1525 | } 1526 | ``` 1527 | Lastly, we need to call the pwm_init() in main(), add it after peer_manager_init(). 1528 | 1529 | 1530 | In order to verify that the servo has been correctly connected to the NRF52 DK and that we can change the duty cycle, add the following line before the for(;;) loop in main(). 1531 | 1532 | APP_ERROR_CHECK(app_pwm_channel_duty_set(&PWM1, 0, 7)); 1533 | 1534 | This will make the servo move to 90 degrees, so make sure that the servo arm is turned to one of its outer positions before connecting it to the DK. After adding pwm_init() and the code line above your main() function should look like this 1535 | 1536 | ```c 1537 | int main(void) 1538 | { 1539 | bool erase_bonds; 1540 | 1541 | // Initialize. 1542 | log_init(); 1543 | timers_init(); 1544 | buttons_leds_init(&erase_bonds); 1545 | power_management_init(); 1546 | ble_stack_init(); 1547 | gap_params_init(); 1548 | gatt_init(); 1549 | services_init(); 1550 | advertising_init(); 1551 | conn_params_init(); 1552 | peer_manager_init(); 1553 | pwm_init(); 1554 | 1555 | // Start execution. 1556 | NRF_LOG_INFO("Template example started."); 1557 | application_timers_start(); 1558 | 1559 | advertising_start(erase_bonds); 1560 | 1561 | // Comment out this line after you have verified that the servo moves to the 90 degree position. 1562 | APP_ERROR_CHECK(app_pwm_channel_duty_set(&PWM1, 0, 7)); 1563 | 1564 | // Enter main loop. 1565 | for (;;) 1566 | { 1567 | idle_state_handle(); 1568 | } 1569 | } 1570 | ``` 1571 | 1572 | Compile the code, flash it to the nRF52 DK and observe that the servo moves to its 90 degree postition. Comment out the line after you have verified that the servo is correctly connected. 1573 | 1574 | ### Step 4 - Modifying the Custom Service to handle Servo events 1575 | 1576 | Now, we want to modify the custom service to receive the duty cycle of the servo over Bluetooth, i.e. write the duty cycle to the custom value characteristic and then call app_pwm_channel_duty_set() to move the servo to the choosen position. 1577 | 1578 | First, we're going to add a BLE_CUS_EVT_SERVO event to ble_cus_evt_type_t in ble_cus.h 1579 | ```c 1580 | /**@brief Custom Service event type. */ 1581 | typedef enum 1582 | { 1583 | BLE_CUS_EVT_NOTIFICATION_ENABLED, /**< Custom value notification enabled event. */ 1584 | BLE_CUS_EVT_NOTIFICATION_DISABLED, /**< Custom value notification disabled event. */ 1585 | BLE_CUS_EVT_DISCONNECTED, 1586 | BLE_CUS_EVT_CONNECTED, 1587 | BLE_CUS_EVT_SERVO 1588 | } ble_cus_evt_type_t; 1589 | ``` 1590 | The second change we want to do in ble_cus.h is to add a duty_cycle variable to the ble_cus_evt_t struct so that we can pass the duty cycle value to the on_cus_evt event handler in main.c, i.e. 1591 | 1592 | ```c 1593 | /**@brief Custom Service event. */ 1594 | typedef struct 1595 | { 1596 | ble_cus_evt_type_t evt_type; /**< Type of event. */ 1597 | uint8_t duty_cycle; /**< Servo duty cycle */ 1598 | } ble_cus_evt_t; 1599 | ``` 1600 | 1601 | The next thing we need to do is to modify the on_write() in ble_cus.c function to receive the duty cycle sent over BLE 1602 | ```c 1603 | // Custom Value Characteristic Written to. 1604 | if (p_evt_write->handle == p_cus->custom_value_handles.value_handle) 1605 | { 1606 | nrf_gpio_pin_toggle(LED_4); 1607 | 1608 | // Check if data pointer is not a null-pointer 1609 | if(p_evt_write->data != NULL) 1610 | { 1611 | evt.evt_type = BLE_CUS_EVT_SERVO; 1612 | 1613 | // Set duty cycle to value written to the characteristic 1614 | evt.duty_cycle = p_evt_write->data[0]; 1615 | 1616 | NRF_LOG_INFO("New Duty Cycle received %d\r\n", evt.duty_cycle); 1617 | 1618 | // Call the application event handler. 1619 | p_cus->evt_handler(p_cus, &evt); 1620 | } 1621 | ``` 1622 | After the modification the on_write() in ble_cus.c should look like this 1623 | 1624 | ```c 1625 | static void on_write(ble_cus_t * p_cus, ble_evt_t const * p_ble_evt) 1626 | { 1627 | ble_gatts_evt_write_t const * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write; 1628 | ble_cus_evt_t evt; 1629 | 1630 | // Check if its the Custom Value Characteristic thats been written to. 1631 | if (p_evt_write->handle == p_cus->custom_value_handles.value_handle) 1632 | { 1633 | nrf_gpio_pin_toggle(LED_4); 1634 | 1635 | if(p_evt_write->data != NULL) 1636 | { 1637 | evt.evt_type = BLE_CUS_EVT_SERVO; 1638 | 1639 | evt.duty_cycle = p_evt_write->data[0]; 1640 | 1641 | //Print the calulated duty cycle to the debug terminal 1642 | NRF_LOG_INFO("Duty Cycle: %d \r\n", evt.duty_cycle); 1643 | 1644 | // Call the application event handler. 1645 | p_cus->evt_handler(p_cus, &evt); 1646 | } 1647 | } 1648 | 1649 | // Check if the Custom value CCCD is written to and that the value is the appropriate length, i.e 2 bytes. 1650 | if ((p_evt_write->handle == p_cus->custom_value_handles.cccd_handle) 1651 | && (p_evt_write->len == 2) 1652 | ) 1653 | { 1654 | // CCCD written, call application event handler 1655 | if (p_cus->evt_handler != NULL) 1656 | { 1657 | 1658 | if (ble_srv_is_notification_enabled(p_evt_write->data)) 1659 | { 1660 | evt.evt_type = BLE_CUS_EVT_NOTIFICATION_ENABLED; 1661 | } 1662 | else 1663 | { 1664 | evt.evt_type = BLE_CUS_EVT_NOTIFICATION_DISABLED; 1665 | } 1666 | // Call the application event handler. 1667 | p_cus->evt_handler(p_cus, &evt); 1668 | } 1669 | } 1670 | 1671 | } 1672 | ``` 1673 | 1674 | ### Step 5 - Handle the BLE_CUS_EVT_SERVO event in on_cus_evt() 1675 | 1676 | The last thing we need to do is to add the BLE_CUS_EVT_SERVO event to the switch-case in on_cus_evt() in main.c 1677 | 1678 | ```c 1679 | static void on_cus_evt(ble_cus_t * p_cus_service, 1680 | ble_cus_evt_t * p_evt) 1681 | { 1682 | ret_code_t err_code; 1683 | 1684 | switch(p_evt->evt_type) 1685 | { 1686 | case BLE_CUS_EVT_NOTIFICATION_ENABLED: 1687 | 1688 | err_code = app_timer_start(m_notification_timer_id, NOTIFICATION_INTERVAL, NULL); 1689 | APP_ERROR_CHECK(err_code); 1690 | break; 1691 | 1692 | case BLE_CUS_EVT_NOTIFICATION_DISABLED: 1693 | 1694 | err_code = app_timer_stop(m_notification_timer_id); 1695 | APP_ERROR_CHECK(err_code); 1696 | break; 1697 | 1698 | case BLE_CUS_EVT_CONNECTED: 1699 | break; 1700 | 1701 | case BLE_CUS_EVT_DISCONNECTED: 1702 | break; 1703 | // Add this case 1704 | case BLE_CUS_EVT_SERVO: 1705 | break; 1706 | 1707 | default: 1708 | // No implementation needed. 1709 | break; 1710 | } 1711 | } 1712 | ``` 1713 | 1714 | The last thing we need to do now is to add the function that sets the duty cycle, app_pwm_channel_duty_set(), with the duty_cycle parameter passed in the p_evt struct. 1715 | 1716 | ```c 1717 | static void on_cus_evt(ble_cus_t * p_cus_service, 1718 | ble_cus_evt_t * p_evt) 1719 | { 1720 | ret_code_t err_code; 1721 | 1722 | switch(p_evt->evt_type) 1723 | { 1724 | case BLE_CUS_EVT_NOTIFICATION_ENABLED: 1725 | 1726 | err_code = app_timer_start(m_notification_timer_id, NOTIFICATION_INTERVAL, NULL); 1727 | APP_ERROR_CHECK(err_code); 1728 | break; 1729 | 1730 | case BLE_CUS_EVT_NOTIFICATION_DISABLED: 1731 | 1732 | err_code = app_timer_stop(m_notification_timer_id); 1733 | APP_ERROR_CHECK(err_code); 1734 | break; 1735 | 1736 | case BLE_CUS_EVT_CONNECTED: 1737 | break; 1738 | 1739 | case BLE_CUS_EVT_DISCONNECTED: 1740 | break; 1741 | case BLE_CUS_EVT_SERVO: 1742 | err_code = app_pwm_channel_duty_set(&PWM1, 0, p_evt->duty_cycle); 1743 | APP_ERROR_CHECK(err_code); 1744 | break; 1745 | 1746 | default: 1747 | // No implementation needed. 1748 | break; 1749 | } 1750 | } 1751 | ``` 1752 | Now we should be able to send the duty cycle value to the nRF52 DK and change the angle of the servo. Compile the code, flash it to the DK and open the nRF Connect app. Connect to the nRF52 DK and write a value between 3 and 13 to the characteristic. Note: In hexadecimal 3 = 0x03 and 13 = 0xD. 1753 | 1754 | ### Step 7 - Calculating duty cycle from angle 1755 | 1756 | Sending the duty cycle over BLE works, but it would be even better if we could send the angle and then have the nRF52832 calculate the duty cycle based on that. 1757 | 1758 | We know that the servo will be at 0 degrees if we set the duty cycle to 3% and at 180 degrees if the duty cycle is set to 13%. Based on this information we can set up a linear equation on the form 1759 | 1760 | ``` 1761 | y = mx + b 1762 | ``` 1763 | where m = y2-y1/x2-x1 and b = y(0), see this[https://www.mathplanet.com/education/algebra-1/formulating-linear-equations/writing-linear-equations-using-the-slope-intercept-form] for the theory. So if we say that the Duty Cycle is Y and the angle is X, we end up with the following formula 1764 | 1765 | ``` 1766 | Duty Cycle = m*Angle + b 1767 | ``` 1768 | Calculate the value of m and b based on the fact that Duty Cycle(angle=0) = 3 and Duty Cycle(angle=180) = 13. 1769 | 1770 | Replace evt.duty_cycle = (p_evt_write->data[0]) in the on_write() function with the equation. Compile the code, flash it to the DK and open the nRF Connect app. Connect to the nRF52 DK and write a value between 0 and 180 to the characteristic. Note: In hexadecimal 0 = 0x00, 90 = 0x5A and 180 = 0xB4. 1771 | 1772 | 1773 | 1774 | 1775 | -------------------------------------------------------------------------------- /ble_cus.c: -------------------------------------------------------------------------------- 1 | #include "sdk_common.h" 2 | #include "ble_cus.h" 3 | #include 4 | #include "ble_srv_common.h" 5 | #include "nrf_gpio.h" 6 | #include "boards.h" 7 | #include "nrf_log.h" 8 | 9 | /**@brief Function for handling the Connect event. 10 | * 11 | * @param[in] p_cus Custom Service structure. 12 | * @param[in] p_ble_evt Event received from the BLE stack. 13 | */ 14 | static void on_connect(ble_cus_t * p_cus, ble_evt_t const * p_ble_evt) 15 | { 16 | p_cus->conn_handle = p_ble_evt->evt.gap_evt.conn_handle; 17 | 18 | ble_cus_evt_t evt; 19 | 20 | evt.evt_type = BLE_CUS_EVT_CONNECTED; 21 | 22 | p_cus->evt_handler(p_cus, &evt); 23 | } 24 | 25 | /**@brief Function for handling the Disconnect event. 26 | * 27 | * @param[in] p_cus Custom Service structure. 28 | * @param[in] p_ble_evt Event received from the BLE stack. 29 | */ 30 | static void on_disconnect(ble_cus_t * p_cus, ble_evt_t const * p_ble_evt) 31 | { 32 | UNUSED_PARAMETER(p_ble_evt); 33 | p_cus->conn_handle = BLE_CONN_HANDLE_INVALID; 34 | 35 | ble_cus_evt_t evt; 36 | 37 | evt.evt_type = BLE_CUS_EVT_DISCONNECTED; 38 | 39 | p_cus->evt_handler(p_cus, &evt); 40 | } 41 | 42 | /**@brief Function for handling the Write event. 43 | * 44 | * @param[in] p_cus Custom Service structure. 45 | * @param[in] p_ble_evt Event received from the BLE stack. 46 | */ 47 | static void on_write(ble_cus_t * p_cus, ble_evt_t const * p_ble_evt) 48 | { 49 | ble_gatts_evt_write_t const * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write; 50 | 51 | // Custom Value Characteristic Written to. 52 | if (p_evt_write->handle == p_cus->custom_value_handles.value_handle) 53 | { 54 | nrf_gpio_pin_toggle(LED_4); 55 | /* 56 | if(*p_evt_write->data == 0x01) 57 | { 58 | nrf_gpio_pin_clear(20); 59 | } 60 | else if(*p_evt_write->data == 0x02) 61 | { 62 | nrf_gpio_pin_set(20); 63 | } 64 | else 65 | { 66 | //Do nothing 67 | } 68 | */ 69 | } 70 | 71 | // Check if the Custom value CCCD is written to and that the value is the appropriate length, i.e 2 bytes. 72 | if ((p_evt_write->handle == p_cus->custom_value_handles.cccd_handle) 73 | && (p_evt_write->len == 2) 74 | ) 75 | { 76 | // CCCD written, call application event handler 77 | if (p_cus->evt_handler != NULL) 78 | { 79 | ble_cus_evt_t evt; 80 | 81 | if (ble_srv_is_notification_enabled(p_evt_write->data)) 82 | { 83 | evt.evt_type = BLE_CUS_EVT_NOTIFICATION_ENABLED; 84 | } 85 | else 86 | { 87 | evt.evt_type = BLE_CUS_EVT_NOTIFICATION_DISABLED; 88 | } 89 | // Call the application event handler. 90 | p_cus->evt_handler(p_cus, &evt); 91 | } 92 | } 93 | 94 | } 95 | 96 | void ble_cus_on_ble_evt( ble_evt_t const * p_ble_evt, void * p_context) 97 | { 98 | ble_cus_t * p_cus = (ble_cus_t *) p_context; 99 | 100 | NRF_LOG_INFO("BLE event received. Event type = %d\r\n", p_ble_evt->header.evt_id); 101 | if (p_cus == NULL || p_ble_evt == NULL) 102 | { 103 | return; 104 | } 105 | 106 | switch (p_ble_evt->header.evt_id) 107 | { 108 | case BLE_GAP_EVT_CONNECTED: 109 | on_connect(p_cus, p_ble_evt); 110 | break; 111 | 112 | case BLE_GAP_EVT_DISCONNECTED: 113 | on_disconnect(p_cus, p_ble_evt); 114 | break; 115 | 116 | case BLE_GATTS_EVT_WRITE: 117 | on_write(p_cus, p_ble_evt); 118 | break; 119 | /* Handling this event is not necessary 120 | case BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST: 121 | NRF_LOG_INFO("EXCHANGE_MTU_REQUEST event received.\r\n"); 122 | break; 123 | */ 124 | default: 125 | // No implementation needed. 126 | break; 127 | } 128 | } 129 | 130 | /**@brief Function for adding the Custom Value characteristic. 131 | * 132 | * @param[in] p_cus Battery Service structure. 133 | * @param[in] p_cus_init Information needed to initialize the service. 134 | * 135 | * @return NRF_SUCCESS on success, otherwise an error code. 136 | */ 137 | static uint32_t custom_value_char_add(ble_cus_t * p_cus, const ble_cus_init_t * p_cus_init) 138 | { 139 | uint32_t err_code; 140 | ble_gatts_char_md_t char_md; 141 | ble_gatts_attr_md_t cccd_md; 142 | ble_gatts_attr_t attr_char_value; 143 | ble_uuid_t ble_uuid; 144 | ble_gatts_attr_md_t attr_md; 145 | 146 | // Add Custom Value characteristic 147 | memset(&cccd_md, 0, sizeof(cccd_md)); 148 | 149 | // Read operation on cccd should be possible without authentication. 150 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm); 151 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm); 152 | 153 | cccd_md.write_perm = p_cus_init->custom_value_char_attr_md.cccd_write_perm; 154 | cccd_md.vloc = BLE_GATTS_VLOC_STACK; 155 | 156 | memset(&char_md, 0, sizeof(char_md)); 157 | 158 | char_md.char_props.read = 1; 159 | char_md.char_props.write = 1; 160 | char_md.char_props.notify = 1; 161 | char_md.p_char_user_desc = NULL; 162 | char_md.p_char_pf = NULL; 163 | char_md.p_user_desc_md = NULL; 164 | char_md.p_cccd_md = &cccd_md; 165 | char_md.p_sccd_md = NULL; 166 | 167 | ble_uuid.type = p_cus->uuid_type; 168 | ble_uuid.uuid = CUSTOM_VALUE_CHAR_UUID; 169 | 170 | memset(&attr_md, 0, sizeof(attr_md)); 171 | 172 | attr_md.read_perm = p_cus_init->custom_value_char_attr_md.read_perm; 173 | attr_md.write_perm = p_cus_init->custom_value_char_attr_md.write_perm; 174 | attr_md.vloc = BLE_GATTS_VLOC_STACK; 175 | attr_md.rd_auth = 0; 176 | attr_md.wr_auth = 0; 177 | attr_md.vlen = 0; 178 | 179 | memset(&attr_char_value, 0, sizeof(attr_char_value)); 180 | 181 | attr_char_value.p_uuid = &ble_uuid; 182 | attr_char_value.p_attr_md = &attr_md; 183 | attr_char_value.init_len = sizeof(uint8_t); 184 | attr_char_value.init_offs = 0; 185 | attr_char_value.max_len = sizeof(uint8_t); 186 | 187 | err_code = sd_ble_gatts_characteristic_add(p_cus->service_handle, &char_md, 188 | &attr_char_value, 189 | &p_cus->custom_value_handles); 190 | if (err_code != NRF_SUCCESS) 191 | { 192 | return err_code; 193 | } 194 | 195 | return NRF_SUCCESS; 196 | } 197 | 198 | uint32_t ble_cus_init(ble_cus_t * p_cus, const ble_cus_init_t * p_cus_init) 199 | { 200 | if (p_cus == NULL || p_cus_init == NULL) 201 | { 202 | return NRF_ERROR_NULL; 203 | } 204 | 205 | uint32_t err_code; 206 | ble_uuid_t ble_uuid; 207 | 208 | // Initialize service structure 209 | p_cus->evt_handler = p_cus_init->evt_handler; 210 | p_cus->conn_handle = BLE_CONN_HANDLE_INVALID; 211 | 212 | // Add Custom Service UUID 213 | ble_uuid128_t base_uuid = {CUSTOM_SERVICE_UUID_BASE}; 214 | err_code = sd_ble_uuid_vs_add(&base_uuid, &p_cus->uuid_type); 215 | VERIFY_SUCCESS(err_code); 216 | 217 | ble_uuid.type = p_cus->uuid_type; 218 | ble_uuid.uuid = CUSTOM_SERVICE_UUID; 219 | 220 | // Add the Custom Service 221 | err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &p_cus->service_handle); 222 | if (err_code != NRF_SUCCESS) 223 | { 224 | return err_code; 225 | } 226 | 227 | // Add Custom Value characteristic 228 | return custom_value_char_add(p_cus, p_cus_init); 229 | } 230 | 231 | uint32_t ble_cus_custom_value_update(ble_cus_t * p_cus, uint8_t custom_value) 232 | { 233 | NRF_LOG_INFO("In ble_cus_custom_value_update. \r\n"); 234 | if (p_cus == NULL) 235 | { 236 | return NRF_ERROR_NULL; 237 | } 238 | 239 | uint32_t err_code = NRF_SUCCESS; 240 | ble_gatts_value_t gatts_value; 241 | 242 | // Initialize value struct. 243 | memset(&gatts_value, 0, sizeof(gatts_value)); 244 | 245 | gatts_value.len = sizeof(uint8_t); 246 | gatts_value.offset = 0; 247 | gatts_value.p_value = &custom_value; 248 | 249 | // Update database. 250 | err_code = sd_ble_gatts_value_set(p_cus->conn_handle, 251 | p_cus->custom_value_handles.value_handle, 252 | &gatts_value); 253 | if (err_code != NRF_SUCCESS) 254 | { 255 | return err_code; 256 | } 257 | 258 | // Send value if connected and notifying. 259 | if ((p_cus->conn_handle != BLE_CONN_HANDLE_INVALID)) 260 | { 261 | ble_gatts_hvx_params_t hvx_params; 262 | 263 | memset(&hvx_params, 0, sizeof(hvx_params)); 264 | 265 | hvx_params.handle = p_cus->custom_value_handles.value_handle; 266 | hvx_params.type = BLE_GATT_HVX_NOTIFICATION; 267 | hvx_params.offset = gatts_value.offset; 268 | hvx_params.p_len = &gatts_value.len; 269 | hvx_params.p_data = gatts_value.p_value; 270 | 271 | err_code = sd_ble_gatts_hvx(p_cus->conn_handle, &hvx_params); 272 | NRF_LOG_INFO("sd_ble_gatts_hvx result: %x. \r\n", err_code); 273 | } 274 | else 275 | { 276 | err_code = NRF_ERROR_INVALID_STATE; 277 | NRF_LOG_INFO("sd_ble_gatts_hvx result: NRF_ERROR_INVALID_STATE. \r\n"); 278 | } 279 | 280 | 281 | return err_code; 282 | } 283 | -------------------------------------------------------------------------------- /ble_cus.h: -------------------------------------------------------------------------------- 1 | #ifndef BLE_CUS_H__ 2 | #define BLE_CUS_H__ 3 | 4 | #include 5 | #include 6 | #include "ble.h" 7 | #include "ble_srv_common.h" 8 | 9 | /**@brief Macro for defining a ble_hrs instance. 10 | * 11 | * @param _name Name of the instance. 12 | * @hideinitializer 13 | */ 14 | #define BLE_CUS_DEF(_name) \ 15 | static ble_cus_t _name; \ 16 | NRF_SDH_BLE_OBSERVER(_name ## _obs, \ 17 | BLE_HRS_BLE_OBSERVER_PRIO, \ 18 | ble_cus_on_ble_evt, &_name) 19 | 20 | 21 | 22 | // CUSTOM_SERVICE_UUID_BASE f364adc9-b000-4042-ba50-05ca45bf8abc 23 | 24 | #define CUSTOM_SERVICE_UUID_BASE {0xBC, 0x8A, 0xBF, 0x45, 0xCA, 0x05, 0x50, 0xBA, \ 25 | 0x40, 0x42, 0xB0, 0x00, 0xC9, 0xAD, 0x64, 0xF3} 26 | 27 | #define CUSTOM_SERVICE_UUID 0x1400 28 | #define CUSTOM_VALUE_CHAR_UUID 0x1401 29 | 30 | /**@brief Custom Service event type. */ 31 | typedef enum 32 | { 33 | BLE_CUS_EVT_NOTIFICATION_ENABLED, /**< Custom value notification enabled event. */ 34 | BLE_CUS_EVT_NOTIFICATION_DISABLED, /**< Custom value notification disabled event. */ 35 | BLE_CUS_EVT_DISCONNECTED, 36 | BLE_CUS_EVT_CONNECTED 37 | } ble_cus_evt_type_t; 38 | 39 | /**@brief Custom Service event. */ 40 | typedef struct 41 | { 42 | ble_cus_evt_type_t evt_type; /**< Type of event. */ 43 | } ble_cus_evt_t; 44 | 45 | // Forward declaration of the ble_cus_t type. 46 | typedef struct ble_cus_s ble_cus_t; 47 | 48 | 49 | /**@brief Custom Service event handler type. */ 50 | typedef void (*ble_cus_evt_handler_t) (ble_cus_t * p_bas, ble_cus_evt_t * p_evt); 51 | 52 | /**@brief Battery Service init structure. This contains all options and data needed for 53 | * initialization of the service.*/ 54 | typedef struct 55 | { 56 | ble_cus_evt_handler_t evt_handler; /**< Event handler to be called for handling events in the Custom Service. */ 57 | uint8_t initial_custom_value; /**< Initial custom value */ 58 | ble_srv_cccd_security_mode_t custom_value_char_attr_md; /**< Initial security level for Custom characteristics attribute */ 59 | } ble_cus_init_t; 60 | 61 | /**@brief Custom Service structure. This contains various status information for the service. */ 62 | struct ble_cus_s 63 | { 64 | ble_cus_evt_handler_t evt_handler; /**< Event handler to be called for handling events in the Custom Service. */ 65 | uint16_t service_handle; /**< Handle of Custom Service (as provided by the BLE stack). */ 66 | ble_gatts_char_handles_t custom_value_handles; /**< Handles related to the Custom Value characteristic. */ 67 | uint16_t conn_handle; /**< Handle of the current connection (as provided by the BLE stack, is BLE_CONN_HANDLE_INVALID if not in a connection). */ 68 | uint8_t uuid_type; 69 | }; 70 | 71 | /**@brief Function for initializing the Custom Service. 72 | * 73 | * @param[out] p_cus Custom Service structure. This structure will have to be supplied by 74 | * the application. It will be initialized by this function, and will later 75 | * be used to identify this particular service instance. 76 | * @param[in] p_cus_init Information needed to initialize the service. 77 | * 78 | * @return NRF_SUCCESS on successful initialization of service, otherwise an error code. 79 | */ 80 | uint32_t ble_cus_init(ble_cus_t * p_cus, const ble_cus_init_t * p_cus_init); 81 | 82 | /**@brief Function for handling the Application's BLE Stack events. 83 | * 84 | * @details Handles all events from the BLE stack of interest to the Battery Service. 85 | * 86 | * @note 87 | * 88 | * @param[in] p_cus Custom Service structure. 89 | * @param[in] p_ble_evt Event received from the BLE stack. 90 | */ 91 | void ble_cus_on_ble_evt( ble_evt_t const * p_ble_evt, void * p_context); 92 | 93 | /**@brief Function for updating the custom value. 94 | * 95 | * @details The application calls this function when the cutom value should be updated. If 96 | * notification has been enabled, the custom value characteristic is sent to the client. 97 | * 98 | * @note 99 | * 100 | * @param[in] p_bas Custom Service structure. 101 | * @param[in] Custom value 102 | * 103 | * @return NRF_SUCCESS on success, otherwise an error code. 104 | */ 105 | 106 | uint32_t ble_cus_custom_value_update(ble_cus_t * p_cus, uint8_t custom_value); 107 | 108 | #endif // BLE_CUS_H__ 109 | -------------------------------------------------------------------------------- /images/advertisement_packet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NordicPlayground/nRF52-Bluetooth-Course/37311502d5f65c9d1071b71cdec9bdab28924618/images/advertisement_packet.png -------------------------------------------------------------------------------- /images/advertising_device.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NordicPlayground/nRF52-Bluetooth-Course/37311502d5f65c9d1071b71cdec9bdab28924618/images/advertising_device.png -------------------------------------------------------------------------------- /images/enable_notif.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NordicPlayground/nRF52-Bluetooth-Course/37311502d5f65c9d1071b71cdec9bdab28924618/images/enable_notif.png -------------------------------------------------------------------------------- /images/memory_settings.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NordicPlayground/nRF52-Bluetooth-Course/37311502d5f65c9d1071b71cdec9bdab28924618/images/memory_settings.JPG -------------------------------------------------------------------------------- /images/memory_settings_SDK_v14.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NordicPlayground/nRF52-Bluetooth-Course/37311502d5f65c9d1071b71cdec9bdab28924618/images/memory_settings_SDK_v14.JPG -------------------------------------------------------------------------------- /images/memory_settings_SDK_v14_SES.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NordicPlayground/nRF52-Bluetooth-Course/37311502d5f65c9d1071b71cdec9bdab28924618/images/memory_settings_SDK_v14_SES.JPG -------------------------------------------------------------------------------- /images/memory_settings_SDK_v15_Keil.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NordicPlayground/nRF52-Bluetooth-Course/37311502d5f65c9d1071b71cdec9bdab28924618/images/memory_settings_SDK_v15_Keil.JPG -------------------------------------------------------------------------------- /images/memory_settings_SDK_v15_SES.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NordicPlayground/nRF52-Bluetooth-Course/37311502d5f65c9d1071b71cdec9bdab28924618/images/memory_settings_SDK_v15_SES.JPG -------------------------------------------------------------------------------- /images/memory_settings_SDK_v15_gcc.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NordicPlayground/nRF52-Bluetooth-Course/37311502d5f65c9d1071b71cdec9bdab28924618/images/memory_settings_SDK_v15_gcc.JPG -------------------------------------------------------------------------------- /images/service.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NordicPlayground/nRF52-Bluetooth-Course/37311502d5f65c9d1071b71cdec9bdab28924618/images/service.png -------------------------------------------------------------------------------- /images/service_and_char.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NordicPlayground/nRF52-Bluetooth-Course/37311502d5f65c9d1071b71cdec9bdab28924618/images/service_and_char.png -------------------------------------------------------------------------------- /images/service_char_desc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NordicPlayground/nRF52-Bluetooth-Course/37311502d5f65c9d1071b71cdec9bdab28924618/images/service_char_desc.png -------------------------------------------------------------------------------- /images/write_to_char_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NordicPlayground/nRF52-Bluetooth-Course/37311502d5f65c9d1071b71cdec9bdab28924618/images/write_to_char_1.png -------------------------------------------------------------------------------- /images/write_to_char_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NordicPlayground/nRF52-Bluetooth-Course/37311502d5f65c9d1071b71cdec9bdab28924618/images/write_to_char_2.png -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 - 2018, Nordic Semiconductor ASA 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form, except as embedded into a Nordic 13 | * Semiconductor ASA integrated circuit in a product or a software update for 14 | * such product, must reproduce the above copyright notice, this list of 15 | * conditions and the following disclaimer in the documentation and/or other 16 | * materials provided with the distribution. 17 | * 18 | * 3. Neither the name of Nordic Semiconductor ASA nor the names of its 19 | * contributors may be used to endorse or promote products derived from this 20 | * software without specific prior written permission. 21 | * 22 | * 4. This software, with or without modification, must only be used with a 23 | * Nordic Semiconductor ASA integrated circuit. 24 | * 25 | * 5. Any software provided in binary form under this license must not be reverse 26 | * engineered, decompiled, modified and/or disassembled. 27 | * 28 | * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS 29 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 30 | * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE 31 | * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE 32 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 33 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 34 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 37 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 | * 39 | */ 40 | /** @file 41 | * 42 | * @defgroup ble_sdk_app_template_main main.c 43 | * @{ 44 | * @ingroup ble_sdk_app_template 45 | * @brief Template project main file. 46 | * 47 | * This file contains a template for creating a new application. It has the code necessary to wakeup 48 | * from button, advertise, get a connection restart advertising on disconnect and if no new 49 | * connection created go back to system-off mode. 50 | * It can easily be used as a starting point for creating a new application, the comments identified 51 | * with 'YOUR_JOB' indicates where and how you can customize. 52 | */ 53 | 54 | #include 55 | #include 56 | #include 57 | 58 | #include "nordic_common.h" 59 | #include "nrf.h" 60 | #include "app_error.h" 61 | #include "ble.h" 62 | #include "ble_hci.h" 63 | #include "ble_srv_common.h" 64 | #include "ble_advdata.h" 65 | #include "ble_advertising.h" 66 | #include "ble_conn_params.h" 67 | #include "nrf_sdh.h" 68 | #include "nrf_sdh_soc.h" 69 | #include "nrf_sdh_ble.h" 70 | #include "app_timer.h" 71 | #include "fds.h" 72 | #include "peer_manager.h" 73 | #include "bsp_btn_ble.h" 74 | #include "sensorsim.h" 75 | #include "ble_conn_state.h" 76 | #include "nrf_ble_gatt.h" 77 | #include "nrf_ble_qwr.h" 78 | #include "nrf_pwr_mgmt.h" 79 | 80 | #include "nrf_log.h" 81 | #include "nrf_log_ctrl.h" 82 | #include "nrf_log_default_backends.h" 83 | #include "ble_cus.h" 84 | 85 | #define DEVICE_NAME "Nordic_Template" /**< Name of device. Will be included in the advertising data. */ 86 | #define MANUFACTURER_NAME "NordicSemiconductor" /**< Manufacturer. Will be passed to Device Information Service. */ 87 | #define APP_ADV_INTERVAL 300 /**< The advertising interval (in units of 0.625 ms. This value corresponds to 187.5 ms). */ 88 | 89 | #define APP_ADV_DURATION 18000 /**< The advertising duration (180 seconds) in units of 10 milliseconds. */ 90 | #define APP_BLE_OBSERVER_PRIO 3 /**< Application's BLE observer priority. You shouldn't need to modify this value. */ 91 | #define APP_BLE_CONN_CFG_TAG 1 /**< A tag identifying the SoftDevice BLE configuration. */ 92 | 93 | #define MIN_CONN_INTERVAL MSEC_TO_UNITS(100, UNIT_1_25_MS) /**< Minimum acceptable connection interval (0.1 seconds). */ 94 | #define MAX_CONN_INTERVAL MSEC_TO_UNITS(200, UNIT_1_25_MS) /**< Maximum acceptable connection interval (0.2 second). */ 95 | #define SLAVE_LATENCY 0 /**< Slave latency. */ 96 | #define CONN_SUP_TIMEOUT MSEC_TO_UNITS(4000, UNIT_10_MS) /**< Connection supervisory timeout (4 seconds). */ 97 | 98 | #define FIRST_CONN_PARAMS_UPDATE_DELAY APP_TIMER_TICKS(5000) /**< Time from initiating event (connect or start of notification) to first time sd_ble_gap_conn_param_update is called (5 seconds). */ 99 | #define NEXT_CONN_PARAMS_UPDATE_DELAY APP_TIMER_TICKS(30000) /**< Time between each call to sd_ble_gap_conn_param_update after the first call (30 seconds). */ 100 | #define MAX_CONN_PARAMS_UPDATE_COUNT 3 /**< Number of attempts before giving up the connection parameter negotiation. */ 101 | 102 | #define NOTIFICATION_INTERVAL APP_TIMER_TICKS(1000) 103 | 104 | #define SEC_PARAM_BOND 1 /**< Perform bonding. */ 105 | #define SEC_PARAM_MITM 0 /**< Man In The Middle protection not required. */ 106 | #define SEC_PARAM_LESC 0 /**< LE Secure Connections not enabled. */ 107 | #define SEC_PARAM_KEYPRESS 0 /**< Keypress notifications not enabled. */ 108 | #define SEC_PARAM_IO_CAPABILITIES BLE_GAP_IO_CAPS_NONE /**< No I/O capabilities. */ 109 | #define SEC_PARAM_OOB 0 /**< Out Of Band data not available. */ 110 | #define SEC_PARAM_MIN_KEY_SIZE 7 /**< Minimum encryption key size. */ 111 | #define SEC_PARAM_MAX_KEY_SIZE 16 /**< Maximum encryption key size. */ 112 | 113 | #define DEAD_BEEF 0xDEADBEEF /**< Value used as error code on stack dump, can be used to identify stack location on stack unwind. */ 114 | 115 | 116 | NRF_BLE_GATT_DEF(m_gatt); 117 | NRF_BLE_QWR_DEF(m_qwr); /**< GATT module instance. */ 118 | BLE_CUS_DEF(m_cus); /**< Context for the Queued Write module.*/ 119 | BLE_ADVERTISING_DEF(m_advertising); /**< Advertising module instance. */ 120 | 121 | APP_TIMER_DEF(m_notification_timer_id); 122 | 123 | static uint8_t m_custom_value = 0; 124 | 125 | static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID; /**< Handle of the current connection. */ 126 | 127 | /* YOUR_JOB: Declare all services structure your application is using 128 | * BLE_XYZ_DEF(m_xyz); 129 | */ 130 | 131 | // YOUR_JOB: Use UUIDs for service(s) used in your application. 132 | static ble_uuid_t m_adv_uuids[] = /**< Universally unique service identifiers. */ 133 | { 134 | {CUSTOM_SERVICE_UUID, BLE_UUID_TYPE_VENDOR_BEGIN } 135 | }; 136 | 137 | 138 | static void advertising_start(bool erase_bonds); 139 | 140 | 141 | /**@brief Callback function for asserts in the SoftDevice. 142 | * 143 | * @details This function will be called in case of an assert in the SoftDevice. 144 | * 145 | * @warning This handler is an example only and does not fit a final product. You need to analyze 146 | * how your product is supposed to react in case of Assert. 147 | * @warning On assert from the SoftDevice, the system can only recover on reset. 148 | * 149 | * @param[in] line_num Line number of the failing ASSERT call. 150 | * @param[in] file_name File name of the failing ASSERT call. 151 | */ 152 | void assert_nrf_callback(uint16_t line_num, const uint8_t * p_file_name) 153 | { 154 | app_error_handler(DEAD_BEEF, line_num, p_file_name); 155 | } 156 | 157 | 158 | /**@brief Function for handling Peer Manager events. 159 | * 160 | * @param[in] p_evt Peer Manager event. 161 | */ 162 | static void pm_evt_handler(pm_evt_t const * p_evt) 163 | { 164 | ret_code_t err_code; 165 | 166 | switch (p_evt->evt_id) 167 | { 168 | case PM_EVT_BONDED_PEER_CONNECTED: 169 | { 170 | NRF_LOG_INFO("Connected to a previously bonded device."); 171 | } break; 172 | 173 | case PM_EVT_CONN_SEC_SUCCEEDED: 174 | { 175 | NRF_LOG_INFO("Connection secured: role: %d, conn_handle: 0x%x, procedure: %d.", 176 | ble_conn_state_role(p_evt->conn_handle), 177 | p_evt->conn_handle, 178 | p_evt->params.conn_sec_succeeded.procedure); 179 | } break; 180 | 181 | case PM_EVT_CONN_SEC_FAILED: 182 | { 183 | /* Often, when securing fails, it shouldn't be restarted, for security reasons. 184 | * Other times, it can be restarted directly. 185 | * Sometimes it can be restarted, but only after changing some Security Parameters. 186 | * Sometimes, it cannot be restarted until the link is disconnected and reconnected. 187 | * Sometimes it is impossible, to secure the link, or the peer device does not support it. 188 | * How to handle this error is highly application dependent. */ 189 | } break; 190 | 191 | case PM_EVT_CONN_SEC_CONFIG_REQ: 192 | { 193 | // Reject pairing request from an already bonded peer. 194 | pm_conn_sec_config_t conn_sec_config = {.allow_repairing = false}; 195 | pm_conn_sec_config_reply(p_evt->conn_handle, &conn_sec_config); 196 | } break; 197 | 198 | case PM_EVT_STORAGE_FULL: 199 | { 200 | // Run garbage collection on the flash. 201 | err_code = fds_gc(); 202 | if (err_code == FDS_ERR_NO_SPACE_IN_QUEUES) 203 | { 204 | // Retry. 205 | } 206 | else 207 | { 208 | APP_ERROR_CHECK(err_code); 209 | } 210 | } break; 211 | 212 | case PM_EVT_PEERS_DELETE_SUCCEEDED: 213 | { 214 | advertising_start(false); 215 | } break; 216 | 217 | case PM_EVT_PEER_DATA_UPDATE_FAILED: 218 | { 219 | // Assert. 220 | APP_ERROR_CHECK(p_evt->params.peer_data_update_failed.error); 221 | } break; 222 | 223 | case PM_EVT_PEER_DELETE_FAILED: 224 | { 225 | // Assert. 226 | APP_ERROR_CHECK(p_evt->params.peer_delete_failed.error); 227 | } break; 228 | 229 | case PM_EVT_PEERS_DELETE_FAILED: 230 | { 231 | // Assert. 232 | APP_ERROR_CHECK(p_evt->params.peers_delete_failed_evt.error); 233 | } break; 234 | 235 | case PM_EVT_ERROR_UNEXPECTED: 236 | { 237 | // Assert. 238 | APP_ERROR_CHECK(p_evt->params.error_unexpected.error); 239 | } break; 240 | 241 | case PM_EVT_CONN_SEC_START: 242 | case PM_EVT_PEER_DATA_UPDATE_SUCCEEDED: 243 | case PM_EVT_PEER_DELETE_SUCCEEDED: 244 | case PM_EVT_LOCAL_DB_CACHE_APPLIED: 245 | case PM_EVT_LOCAL_DB_CACHE_APPLY_FAILED: 246 | // This can happen when the local DB has changed. 247 | case PM_EVT_SERVICE_CHANGED_IND_SENT: 248 | case PM_EVT_SERVICE_CHANGED_IND_CONFIRMED: 249 | default: 250 | break; 251 | } 252 | } 253 | 254 | 255 | /**@brief Function for handling the Battery measurement timer timeout. 256 | * 257 | * @details This function will be called each time the battery level measurement timer expires. 258 | * 259 | * @param[in] p_context Pointer used for passing some arbitrary information (context) from the 260 | * app_start_timer() call to the timeout handler. 261 | */ 262 | static void notification_timeout_handler(void * p_context) 263 | { 264 | UNUSED_PARAMETER(p_context); 265 | ret_code_t err_code; 266 | 267 | // Increment the value of m_custom_value before nortifing it. 268 | m_custom_value++; 269 | 270 | err_code = ble_cus_custom_value_update(&m_cus, m_custom_value); 271 | APP_ERROR_CHECK(err_code); 272 | } 273 | 274 | /**@brief Function for the Timer initialization. 275 | * 276 | * @details Initializes the timer module. This creates and starts application timers. 277 | */ 278 | static void timers_init(void) 279 | { 280 | // Initialize timer module. 281 | ret_code_t err_code = app_timer_init(); 282 | APP_ERROR_CHECK(err_code); 283 | 284 | // Create timers. 285 | err_code = app_timer_create(&m_notification_timer_id, APP_TIMER_MODE_REPEATED, notification_timeout_handler); 286 | APP_ERROR_CHECK(err_code); 287 | 288 | /* YOUR_JOB: Create any timers to be used by the application. 289 | Below is an example of how to create a timer. 290 | For every new timer needed, increase the value of the macro APP_TIMER_MAX_TIMERS by 291 | one. 292 | ret_code_t err_code; 293 | err_code = app_timer_create(&m_app_timer_id, APP_TIMER_MODE_REPEATED, timer_timeout_handler); 294 | APP_ERROR_CHECK(err_code); */ 295 | } 296 | 297 | 298 | /**@brief Function for the GAP initialization. 299 | * 300 | * @details This function sets up all the necessary GAP (Generic Access Profile) parameters of the 301 | * device including the device name, appearance, and the preferred connection parameters. 302 | */ 303 | static void gap_params_init(void) 304 | { 305 | ret_code_t err_code; 306 | ble_gap_conn_params_t gap_conn_params; 307 | ble_gap_conn_sec_mode_t sec_mode; 308 | 309 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode); 310 | 311 | err_code = sd_ble_gap_device_name_set(&sec_mode, 312 | (const uint8_t *)DEVICE_NAME, 313 | strlen(DEVICE_NAME)); 314 | APP_ERROR_CHECK(err_code); 315 | 316 | /* YOUR_JOB: Use an appearance value matching the application's use case. 317 | err_code = sd_ble_gap_appearance_set(BLE_APPEARANCE_); 318 | APP_ERROR_CHECK(err_code); */ 319 | 320 | memset(&gap_conn_params, 0, sizeof(gap_conn_params)); 321 | 322 | gap_conn_params.min_conn_interval = MIN_CONN_INTERVAL; 323 | gap_conn_params.max_conn_interval = MAX_CONN_INTERVAL; 324 | gap_conn_params.slave_latency = SLAVE_LATENCY; 325 | gap_conn_params.conn_sup_timeout = CONN_SUP_TIMEOUT; 326 | 327 | err_code = sd_ble_gap_ppcp_set(&gap_conn_params); 328 | APP_ERROR_CHECK(err_code); 329 | } 330 | 331 | 332 | /**@brief Function for initializing the GATT module. 333 | */ 334 | static void gatt_init(void) 335 | { 336 | ret_code_t err_code = nrf_ble_gatt_init(&m_gatt, NULL); 337 | APP_ERROR_CHECK(err_code); 338 | } 339 | 340 | 341 | /**@brief Function for handling Queued Write Module errors. 342 | * 343 | * @details A pointer to this function will be passed to each service which may need to inform the 344 | * application about an error. 345 | * 346 | * @param[in] nrf_error Error code containing information about what went wrong. 347 | */ 348 | static void nrf_qwr_error_handler(uint32_t nrf_error) 349 | { 350 | APP_ERROR_HANDLER(nrf_error); 351 | } 352 | 353 | 354 | /**@brief Function for handling the YYY Service events. 355 | * YOUR_JOB implement a service handler function depending on the event the service you are using can generate 356 | * 357 | * @details This function will be called for all YY Service events which are passed to 358 | * the application. 359 | * 360 | * @param[in] p_yy_service YY Service structure. 361 | * @param[in] p_evt Event received from the YY Service. 362 | * 363 | * 364 | static void on_yys_evt(ble_yy_service_t * p_yy_service, 365 | ble_yy_service_evt_t * p_evt) 366 | { 367 | switch (p_evt->evt_type) 368 | { 369 | case BLE_YY_NAME_EVT_WRITE: 370 | APPL_LOG("[APPL]: charact written with value %s. ", p_evt->params.char_xx.value.p_str); 371 | break; 372 | 373 | default: 374 | // No implementation needed. 375 | break; 376 | } 377 | } 378 | */ 379 | /**@brief Function for handling the Custom Service Service events. 380 | * 381 | * @details This function will be called for all Custom Service events which are passed to 382 | * the application. 383 | * 384 | * @param[in] p_cus_service Custom Service structure. 385 | * @param[in] p_evt Event received from the Custom Service. 386 | * 387 | */ 388 | static void on_cus_evt(ble_cus_t * p_cus_service, 389 | ble_cus_evt_t * p_evt) 390 | { 391 | ret_code_t err_code; 392 | 393 | switch(p_evt->evt_type) 394 | { 395 | case BLE_CUS_EVT_NOTIFICATION_ENABLED: 396 | 397 | err_code = app_timer_start(m_notification_timer_id, NOTIFICATION_INTERVAL, NULL); 398 | APP_ERROR_CHECK(err_code); 399 | break; 400 | 401 | case BLE_CUS_EVT_NOTIFICATION_DISABLED: 402 | 403 | err_code = app_timer_stop(m_notification_timer_id); 404 | APP_ERROR_CHECK(err_code); 405 | break; 406 | 407 | case BLE_CUS_EVT_CONNECTED: 408 | break; 409 | 410 | case BLE_CUS_EVT_DISCONNECTED: 411 | break; 412 | 413 | default: 414 | // No implementation needed. 415 | break; 416 | } 417 | } 418 | 419 | /**@brief Function for initializing services that will be used by the application. 420 | */ 421 | static void services_init(void) 422 | { 423 | ret_code_t err_code; 424 | nrf_ble_qwr_init_t qwr_init = {0}; 425 | ble_cus_init_t cus_init = {0}; 426 | 427 | // Initialize Queued Write Module. 428 | qwr_init.error_handler = nrf_qwr_error_handler; 429 | 430 | err_code = nrf_ble_qwr_init(&m_qwr, &qwr_init); 431 | APP_ERROR_CHECK(err_code); 432 | 433 | // Initialize CUS Service init structure to zero. 434 | cus_init.evt_handler = on_cus_evt; 435 | 436 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cus_init.custom_value_char_attr_md.cccd_write_perm); 437 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cus_init.custom_value_char_attr_md.read_perm); 438 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cus_init.custom_value_char_attr_md.write_perm); 439 | 440 | err_code = ble_cus_init(&m_cus, &cus_init); 441 | APP_ERROR_CHECK(err_code); 442 | 443 | /* YOUR_JOB: Add code to initialize the services used by the application. 444 | ble_xxs_init_t xxs_init; 445 | ble_yys_init_t yys_init; 446 | 447 | // Initialize XXX Service. 448 | memset(&xxs_init, 0, sizeof(xxs_init)); 449 | 450 | xxs_init.evt_handler = NULL; 451 | xxs_init.is_xxx_notify_supported = true; 452 | xxs_init.ble_xx_initial_value.level = 100; 453 | 454 | err_code = ble_bas_init(&m_xxs, &xxs_init); 455 | APP_ERROR_CHECK(err_code); 456 | 457 | // Initialize YYY Service. 458 | memset(&yys_init, 0, sizeof(yys_init)); 459 | yys_init.evt_handler = on_yys_evt; 460 | yys_init.ble_yy_initial_value.counter = 0; 461 | 462 | err_code = ble_yy_service_init(&yys_init, &yy_init); 463 | APP_ERROR_CHECK(err_code); 464 | */ 465 | } 466 | 467 | 468 | /**@brief Function for handling the Connection Parameters Module. 469 | * 470 | * @details This function will be called for all events in the Connection Parameters Module which 471 | * are passed to the application. 472 | * @note All this function does is to disconnect. This could have been done by simply 473 | * setting the disconnect_on_fail config parameter, but instead we use the event 474 | * handler mechanism to demonstrate its use. 475 | * 476 | * @param[in] p_evt Event received from the Connection Parameters Module. 477 | */ 478 | static void on_conn_params_evt(ble_conn_params_evt_t * p_evt) 479 | { 480 | ret_code_t err_code; 481 | 482 | if (p_evt->evt_type == BLE_CONN_PARAMS_EVT_FAILED) 483 | { 484 | err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_CONN_INTERVAL_UNACCEPTABLE); 485 | APP_ERROR_CHECK(err_code); 486 | } 487 | } 488 | 489 | 490 | /**@brief Function for handling a Connection Parameters error. 491 | * 492 | * @param[in] nrf_error Error code containing information about what went wrong. 493 | */ 494 | static void conn_params_error_handler(uint32_t nrf_error) 495 | { 496 | APP_ERROR_HANDLER(nrf_error); 497 | } 498 | 499 | 500 | /**@brief Function for initializing the Connection Parameters module. 501 | */ 502 | static void conn_params_init(void) 503 | { 504 | ret_code_t err_code; 505 | ble_conn_params_init_t cp_init; 506 | 507 | memset(&cp_init, 0, sizeof(cp_init)); 508 | 509 | cp_init.p_conn_params = NULL; 510 | cp_init.first_conn_params_update_delay = FIRST_CONN_PARAMS_UPDATE_DELAY; 511 | cp_init.next_conn_params_update_delay = NEXT_CONN_PARAMS_UPDATE_DELAY; 512 | cp_init.max_conn_params_update_count = MAX_CONN_PARAMS_UPDATE_COUNT; 513 | cp_init.start_on_notify_cccd_handle = BLE_GATT_HANDLE_INVALID; 514 | cp_init.disconnect_on_fail = false; 515 | cp_init.evt_handler = on_conn_params_evt; 516 | cp_init.error_handler = conn_params_error_handler; 517 | 518 | err_code = ble_conn_params_init(&cp_init); 519 | APP_ERROR_CHECK(err_code); 520 | } 521 | 522 | 523 | /**@brief Function for starting timers. 524 | */ 525 | static void application_timers_start(void) 526 | { 527 | /* YOUR_JOB: Start your timers. below is an example of how to start a timer. 528 | ret_code_t err_code; 529 | err_code = app_timer_start(m_app_timer_id, TIMER_INTERVAL, NULL); 530 | APP_ERROR_CHECK(err_code); */ 531 | 532 | } 533 | 534 | 535 | /**@brief Function for putting the chip into sleep mode. 536 | * 537 | * @note This function will not return. 538 | */ 539 | static void sleep_mode_enter(void) 540 | { 541 | ret_code_t err_code; 542 | 543 | err_code = bsp_indication_set(BSP_INDICATE_IDLE); 544 | APP_ERROR_CHECK(err_code); 545 | 546 | // Prepare wakeup buttons. 547 | err_code = bsp_btn_ble_sleep_mode_prepare(); 548 | APP_ERROR_CHECK(err_code); 549 | 550 | // Go to system-off mode (this function will not return; wakeup will cause a reset). 551 | err_code = sd_power_system_off(); 552 | APP_ERROR_CHECK(err_code); 553 | } 554 | 555 | 556 | /**@brief Function for handling advertising events. 557 | * 558 | * @details This function will be called for advertising events which are passed to the application. 559 | * 560 | * @param[in] ble_adv_evt Advertising event. 561 | */ 562 | static void on_adv_evt(ble_adv_evt_t ble_adv_evt) 563 | { 564 | ret_code_t err_code; 565 | 566 | switch (ble_adv_evt) 567 | { 568 | case BLE_ADV_EVT_FAST: 569 | NRF_LOG_INFO("Fast advertising."); 570 | err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING); 571 | APP_ERROR_CHECK(err_code); 572 | break; 573 | 574 | case BLE_ADV_EVT_IDLE: 575 | sleep_mode_enter(); 576 | break; 577 | 578 | default: 579 | break; 580 | } 581 | } 582 | 583 | 584 | /**@brief Function for handling BLE events. 585 | * 586 | * @param[in] p_ble_evt Bluetooth stack event. 587 | * @param[in] p_context Unused. 588 | */ 589 | static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context) 590 | { 591 | ret_code_t err_code = NRF_SUCCESS; 592 | 593 | switch (p_ble_evt->header.evt_id) 594 | { 595 | case BLE_GAP_EVT_DISCONNECTED: 596 | NRF_LOG_INFO("Disconnected."); 597 | // LED indication will be changed when advertising starts. 598 | break; 599 | 600 | case BLE_GAP_EVT_CONNECTED: 601 | NRF_LOG_INFO("Connected."); 602 | err_code = bsp_indication_set(BSP_INDICATE_CONNECTED); 603 | APP_ERROR_CHECK(err_code); 604 | m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle; 605 | err_code = nrf_ble_qwr_conn_handle_assign(&m_qwr, m_conn_handle); 606 | APP_ERROR_CHECK(err_code); 607 | break; 608 | 609 | case BLE_GAP_EVT_PHY_UPDATE_REQUEST: 610 | { 611 | NRF_LOG_DEBUG("PHY update request."); 612 | ble_gap_phys_t const phys = 613 | { 614 | .rx_phys = BLE_GAP_PHY_AUTO, 615 | .tx_phys = BLE_GAP_PHY_AUTO, 616 | }; 617 | err_code = sd_ble_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle, &phys); 618 | APP_ERROR_CHECK(err_code); 619 | } break; 620 | 621 | case BLE_GATTC_EVT_TIMEOUT: 622 | // Disconnect on GATT Client timeout event. 623 | NRF_LOG_DEBUG("GATT Client Timeout."); 624 | err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gattc_evt.conn_handle, 625 | BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION); 626 | APP_ERROR_CHECK(err_code); 627 | break; 628 | 629 | case BLE_GATTS_EVT_TIMEOUT: 630 | // Disconnect on GATT Server timeout event. 631 | NRF_LOG_DEBUG("GATT Server Timeout."); 632 | err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gatts_evt.conn_handle, 633 | BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION); 634 | APP_ERROR_CHECK(err_code); 635 | break; 636 | 637 | default: 638 | // No implementation needed. 639 | break; 640 | } 641 | } 642 | 643 | 644 | /**@brief Function for initializing the BLE stack. 645 | * 646 | * @details Initializes the SoftDevice and the BLE event interrupt. 647 | */ 648 | static void ble_stack_init(void) 649 | { 650 | ret_code_t err_code; 651 | 652 | err_code = nrf_sdh_enable_request(); 653 | APP_ERROR_CHECK(err_code); 654 | 655 | // Configure the BLE stack using the default settings. 656 | // Fetch the start address of the application RAM. 657 | uint32_t ram_start = 0; 658 | err_code = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start); 659 | APP_ERROR_CHECK(err_code); 660 | 661 | // Enable BLE stack. 662 | err_code = nrf_sdh_ble_enable(&ram_start); 663 | APP_ERROR_CHECK(err_code); 664 | 665 | // Register a handler for BLE events. 666 | NRF_SDH_BLE_OBSERVER(m_ble_observer, APP_BLE_OBSERVER_PRIO, ble_evt_handler, NULL); 667 | } 668 | 669 | 670 | /**@brief Function for the Peer Manager initialization. 671 | */ 672 | static void peer_manager_init(void) 673 | { 674 | ble_gap_sec_params_t sec_param; 675 | ret_code_t err_code; 676 | 677 | err_code = pm_init(); 678 | APP_ERROR_CHECK(err_code); 679 | 680 | memset(&sec_param, 0, sizeof(ble_gap_sec_params_t)); 681 | 682 | // Security parameters to be used for all security procedures. 683 | sec_param.bond = SEC_PARAM_BOND; 684 | sec_param.mitm = SEC_PARAM_MITM; 685 | sec_param.lesc = SEC_PARAM_LESC; 686 | sec_param.keypress = SEC_PARAM_KEYPRESS; 687 | sec_param.io_caps = SEC_PARAM_IO_CAPABILITIES; 688 | sec_param.oob = SEC_PARAM_OOB; 689 | sec_param.min_key_size = SEC_PARAM_MIN_KEY_SIZE; 690 | sec_param.max_key_size = SEC_PARAM_MAX_KEY_SIZE; 691 | sec_param.kdist_own.enc = 1; 692 | sec_param.kdist_own.id = 1; 693 | sec_param.kdist_peer.enc = 1; 694 | sec_param.kdist_peer.id = 1; 695 | 696 | err_code = pm_sec_params_set(&sec_param); 697 | APP_ERROR_CHECK(err_code); 698 | 699 | err_code = pm_register(pm_evt_handler); 700 | APP_ERROR_CHECK(err_code); 701 | } 702 | 703 | 704 | /**@brief Clear bond information from persistent storage. 705 | */ 706 | static void delete_bonds(void) 707 | { 708 | ret_code_t err_code; 709 | 710 | NRF_LOG_INFO("Erase bonds!"); 711 | 712 | err_code = pm_peers_delete(); 713 | APP_ERROR_CHECK(err_code); 714 | } 715 | 716 | 717 | /**@brief Function for handling events from the BSP module. 718 | * 719 | * @param[in] event Event generated when button is pressed. 720 | */ 721 | static void bsp_event_handler(bsp_event_t event) 722 | { 723 | ret_code_t err_code; 724 | 725 | switch (event) 726 | { 727 | case BSP_EVENT_SLEEP: 728 | sleep_mode_enter(); 729 | break; // BSP_EVENT_SLEEP 730 | 731 | case BSP_EVENT_DISCONNECT: 732 | err_code = sd_ble_gap_disconnect(m_conn_handle, 733 | BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION); 734 | if (err_code != NRF_ERROR_INVALID_STATE) 735 | { 736 | APP_ERROR_CHECK(err_code); 737 | } 738 | break; // BSP_EVENT_DISCONNECT 739 | 740 | case BSP_EVENT_WHITELIST_OFF: 741 | if (m_conn_handle == BLE_CONN_HANDLE_INVALID) 742 | { 743 | err_code = ble_advertising_restart_without_whitelist(&m_advertising); 744 | if (err_code != NRF_ERROR_INVALID_STATE) 745 | { 746 | APP_ERROR_CHECK(err_code); 747 | } 748 | } 749 | break; // BSP_EVENT_KEY_0 750 | 751 | default: 752 | break; 753 | } 754 | } 755 | 756 | 757 | /**@brief Function for initializing the Advertising functionality. 758 | */ 759 | static void advertising_init(void) 760 | { 761 | ret_code_t err_code; 762 | ble_advertising_init_t init; 763 | 764 | memset(&init, 0, sizeof(init)); 765 | 766 | init.advdata.name_type = BLE_ADVDATA_FULL_NAME; 767 | init.advdata.include_appearance = true; 768 | init.advdata.flags = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE; 769 | init.advdata.uuids_complete.uuid_cnt = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]); 770 | init.advdata.uuids_complete.p_uuids = m_adv_uuids; 771 | 772 | init.config.ble_adv_fast_enabled = true; 773 | init.config.ble_adv_fast_interval = APP_ADV_INTERVAL; 774 | init.config.ble_adv_fast_timeout = APP_ADV_DURATION; 775 | 776 | init.evt_handler = on_adv_evt; 777 | 778 | err_code = ble_advertising_init(&m_advertising, &init); 779 | APP_ERROR_CHECK(err_code); 780 | 781 | ble_advertising_conn_cfg_tag_set(&m_advertising, APP_BLE_CONN_CFG_TAG); 782 | } 783 | 784 | 785 | /**@brief Function for initializing buttons and leds. 786 | * 787 | * @param[out] p_erase_bonds Will be true if the clear bonding button was pressed to wake the application up. 788 | */ 789 | static void buttons_leds_init(bool * p_erase_bonds) 790 | { 791 | ret_code_t err_code; 792 | bsp_event_t startup_event; 793 | 794 | err_code = bsp_init(BSP_INIT_LEDS | BSP_INIT_BUTTONS, bsp_event_handler); 795 | APP_ERROR_CHECK(err_code); 796 | 797 | err_code = bsp_btn_ble_init(NULL, &startup_event); 798 | APP_ERROR_CHECK(err_code); 799 | 800 | *p_erase_bonds = (startup_event == BSP_EVENT_CLEAR_BONDING_DATA); 801 | } 802 | 803 | 804 | /**@brief Function for initializing the nrf log module. 805 | */ 806 | static void log_init(void) 807 | { 808 | ret_code_t err_code = NRF_LOG_INIT(NULL); 809 | APP_ERROR_CHECK(err_code); 810 | 811 | NRF_LOG_DEFAULT_BACKENDS_INIT(); 812 | } 813 | 814 | 815 | /**@brief Function for initializing power management. 816 | */ 817 | static void power_management_init(void) 818 | { 819 | ret_code_t err_code; 820 | err_code = nrf_pwr_mgmt_init(); 821 | APP_ERROR_CHECK(err_code); 822 | } 823 | 824 | 825 | /**@brief Function for handling the idle state (main loop). 826 | * 827 | * @details If there is no pending log operation, then sleep until next the next event occurs. 828 | */ 829 | static void idle_state_handle(void) 830 | { 831 | if (NRF_LOG_PROCESS() == false) 832 | { 833 | nrf_pwr_mgmt_run(); 834 | } 835 | } 836 | 837 | 838 | /**@brief Function for starting advertising. 839 | */ 840 | static void advertising_start(bool erase_bonds) 841 | { 842 | if (erase_bonds == true) 843 | { 844 | delete_bonds(); 845 | // Advertising is started by PM_EVT_PEERS_DELETED_SUCEEDED event 846 | } 847 | else 848 | { 849 | ret_code_t err_code = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST); 850 | 851 | APP_ERROR_CHECK(err_code); 852 | } 853 | } 854 | 855 | 856 | /**@brief Function for application main entry. 857 | */ 858 | int main(void) 859 | { 860 | bool erase_bonds; 861 | 862 | // Initialize. 863 | log_init(); 864 | timers_init(); 865 | buttons_leds_init(&erase_bonds); 866 | power_management_init(); 867 | ble_stack_init(); 868 | gap_params_init(); 869 | gatt_init(); 870 | services_init(); 871 | advertising_init(); 872 | conn_params_init(); 873 | peer_manager_init(); 874 | 875 | // Start execution. 876 | NRF_LOG_INFO("Template example started."); 877 | application_timers_start(); 878 | 879 | advertising_start(erase_bonds); 880 | 881 | // Enter main loop. 882 | for (;;) 883 | { 884 | idle_state_handle(); 885 | } 886 | } 887 | 888 | 889 | /** 890 | * @} 891 | */ 892 | -------------------------------------------------------------------------------- /pca10040/s132/arm5_no_packs/ble_app_template_pca10040_s132.uvoptx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 1.0 5 | 6 |
### uVision Project, (C) Keil Software
7 | 8 | 9 | *.c 10 | *.s*; *.src; *.a* 11 | *.obj 12 | *.lib 13 | *.txt; *.h; *.inc 14 | *.plm 15 | *.cpp 16 | 0 17 | 18 | 19 | 20 | 0 21 | 0 22 | 23 | 24 | 25 | nrf52832_xxaa 26 | 0x4 27 | ARM-ADS 28 | 29 | 64000000 30 | 31 | 1 32 | 1 33 | 0 34 | 1 35 | 0 36 | 37 | 38 | 1 39 | 65535 40 | 0 41 | 0 42 | 0 43 | 44 | 45 | 79 46 | 66 47 | 8 48 | .\_build\ 49 | 50 | 51 | 1 52 | 1 53 | 1 54 | 0 55 | 1 56 | 1 57 | 0 58 | 1 59 | 0 60 | 0 61 | 0 62 | 0 63 | 64 | 65 | 1 66 | 1 67 | 1 68 | 1 69 | 1 70 | 1 71 | 1 72 | 0 73 | 0 74 | 75 | 76 | 1 77 | 0 78 | 1 79 | 80 | 5 81 | 82 | 0 83 | 1 84 | 1 85 | 1 86 | 1 87 | 1 88 | 1 89 | 1 90 | 1 91 | 1 92 | 1 93 | 1 94 | 1 95 | 1 96 | 0 97 | 1 98 | 1 99 | 1 100 | 1 101 | 0 102 | 0 103 | 6 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | Segger\JL2CM3.dll 115 | 116 | 117 | 118 | 0 119 | ARMRTXEVENTFLAGS 120 | -L70 -Z18 -C0 -M0 -T1 121 | 122 | 123 | 0 124 | DLGTARM 125 | (1010=-1,-1,-1,-1,0)(1007=-1,-1,-1,-1,0)(1008=-1,-1,-1,-1,0)(1009=-1,-1,-1,-1,0)(1012=-1,-1,-1,-1,0) 126 | 127 | 128 | 0 129 | ARMDBGFLAGS 130 | 131 | 132 | 133 | 0 134 | JL2CM3 135 | -U682427604 -O78 -S2 -ZTIFSpeedSel5000 -A0 -C0 -JU1 -JI127.0.0.1 -JP0 -RST0 -N00("ARM CoreSight SW-DP") -D00(2BA01477) -L00(0) -TO18 -TC10000000 -TP21 -TDS8007 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -TB1 -TFE0 -FO15 -FD20000000 -FC4000 -FN2 -FF0nrf52xxx.flm -FS00 -FL0200000 -FP0($$Device:nRF52832_xxAA$Flash\nrf52xxx.flm) -FF1nrf52xxx_uicr.flm -FS110001000 -FL11000 -FP1($$Device:nRF52832_xxAA$Flash\nrf52xxx_uicr.flm) 136 | 137 | 138 | 0 139 | UL2CM3 140 | UL2CM3(-S0 -C0 -P0 -FD20000000 -FC4000 -FN2 -FF0nrf52xxx -FS00 -FL0200000 -FF1nrf52xxx_uicr -FS110001000 -FL11000 -FP0($$Device:nRF52832_xxAA$Flash\nrf52xxx.flm) -FP1($$Device:nRF52832_xxAA$Flash\nrf52xxx_uicr.flm)) 141 | 142 | 143 | 144 | 145 | 0 146 | 147 | 148 | 0 149 | 0 150 | 1 151 | 0 152 | 0 153 | 0 154 | 0 155 | 1 156 | 0 157 | 0 158 | 0 159 | 0 160 | 0 161 | 0 162 | 0 163 | 0 164 | 0 165 | 0 166 | 0 167 | 0 168 | 0 169 | 0 170 | 0 171 | 0 172 | 173 | 174 | 175 | 0 176 | 177 | 178 | 0 179 | 180 | 181 | 182 | 183 | flash_s132_nrf52_6.0.0_softdevice 184 | 0x4 185 | ARM-ADS 186 | 187 | 64000000 188 | 189 | 0 190 | 1 191 | 1 192 | 0 193 | 0 194 | 195 | 196 | 1 197 | 65535 198 | 0 199 | 0 200 | 0 201 | 202 | 203 | 79 204 | 66 205 | 8 206 | .\_build\ 207 | 208 | 209 | 1 210 | 1 211 | 1 212 | 0 213 | 1 214 | 1 215 | 0 216 | 1 217 | 0 218 | 0 219 | 0 220 | 0 221 | 222 | 223 | 1 224 | 1 225 | 1 226 | 1 227 | 1 228 | 1 229 | 1 230 | 0 231 | 0 232 | 233 | 234 | 0 235 | 0 236 | 0 237 | 238 | 5 239 | 240 | 1 241 | 0 242 | 1 243 | 1 244 | 1 245 | 1 246 | 1 247 | 1 248 | 1 249 | 1 250 | 0 251 | 1 252 | 1 253 | 1 254 | 0 255 | 1 256 | 1 257 | 1 258 | 1 259 | 0 260 | 0 261 | -1 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 0 277 | 278 | 279 | 0 280 | 0 281 | 0 282 | 0 283 | 0 284 | 0 285 | 0 286 | 0 287 | 0 288 | 0 289 | 0 290 | 0 291 | 0 292 | 0 293 | 0 294 | 0 295 | 0 296 | 0 297 | 0 298 | 0 299 | 0 300 | 0 301 | 0 302 | 0 303 | 304 | 305 | 306 | 0 307 | 308 | 309 | 0 310 | 311 | 312 | 313 | 314 | Application 315 | 1 316 | 0 317 | 0 318 | 0 319 | 320 | 1 321 | 1 322 | 1 323 | 0 324 | 0 325 | 0 326 | ..\..\..\main.c 327 | main.c 328 | 0 329 | 0 330 | 331 | 332 | 1 333 | 2 334 | 5 335 | 0 336 | 0 337 | 0 338 | ..\config\sdk_config.h 339 | sdk_config.h 340 | 0 341 | 0 342 | 343 | 344 | 345 | 346 | Board Definition 347 | 0 348 | 0 349 | 0 350 | 0 351 | 352 | 2 353 | 3 354 | 1 355 | 0 356 | 0 357 | 0 358 | ..\..\..\..\..\..\components\boards\boards.c 359 | boards.c 360 | 0 361 | 0 362 | 363 | 364 | 365 | 366 | Board Support 367 | 0 368 | 0 369 | 0 370 | 0 371 | 372 | 3 373 | 4 374 | 1 375 | 0 376 | 0 377 | 0 378 | ..\..\..\..\..\..\components\libraries\bsp\bsp.c 379 | bsp.c 380 | 0 381 | 0 382 | 383 | 384 | 3 385 | 5 386 | 1 387 | 0 388 | 0 389 | 0 390 | ..\..\..\..\..\..\components\libraries\bsp\bsp_btn_ble.c 391 | bsp_btn_ble.c 392 | 0 393 | 0 394 | 395 | 396 | 397 | 398 | nRF_BLE 399 | 0 400 | 0 401 | 0 402 | 0 403 | 404 | 4 405 | 6 406 | 1 407 | 0 408 | 0 409 | 0 410 | ..\..\..\..\..\..\components\ble\common\ble_advdata.c 411 | ble_advdata.c 412 | 0 413 | 0 414 | 415 | 416 | 4 417 | 7 418 | 1 419 | 0 420 | 0 421 | 0 422 | ..\..\..\..\..\..\components\ble\ble_advertising\ble_advertising.c 423 | ble_advertising.c 424 | 0 425 | 0 426 | 427 | 428 | 4 429 | 8 430 | 1 431 | 0 432 | 0 433 | 0 434 | ..\..\..\..\..\..\components\ble\common\ble_conn_params.c 435 | ble_conn_params.c 436 | 0 437 | 0 438 | 439 | 440 | 4 441 | 9 442 | 1 443 | 0 444 | 0 445 | 0 446 | ..\..\..\..\..\..\components\ble\common\ble_conn_state.c 447 | ble_conn_state.c 448 | 0 449 | 0 450 | 451 | 452 | 4 453 | 10 454 | 1 455 | 0 456 | 0 457 | 0 458 | ..\..\..\..\..\..\components\ble\common\ble_srv_common.c 459 | ble_srv_common.c 460 | 0 461 | 0 462 | 463 | 464 | 4 465 | 11 466 | 1 467 | 0 468 | 0 469 | 0 470 | ..\..\..\..\..\..\components\ble\peer_manager\gatt_cache_manager.c 471 | gatt_cache_manager.c 472 | 0 473 | 0 474 | 475 | 476 | 4 477 | 12 478 | 1 479 | 0 480 | 0 481 | 0 482 | ..\..\..\..\..\..\components\ble\peer_manager\gatts_cache_manager.c 483 | gatts_cache_manager.c 484 | 0 485 | 0 486 | 487 | 488 | 4 489 | 13 490 | 1 491 | 0 492 | 0 493 | 0 494 | ..\..\..\..\..\..\components\ble\peer_manager\id_manager.c 495 | id_manager.c 496 | 0 497 | 0 498 | 499 | 500 | 4 501 | 14 502 | 1 503 | 0 504 | 0 505 | 0 506 | ..\..\..\..\..\..\components\ble\nrf_ble_gatt\nrf_ble_gatt.c 507 | nrf_ble_gatt.c 508 | 0 509 | 0 510 | 511 | 512 | 4 513 | 15 514 | 1 515 | 0 516 | 0 517 | 0 518 | ..\..\..\..\..\..\components\ble\nrf_ble_qwr\nrf_ble_qwr.c 519 | nrf_ble_qwr.c 520 | 0 521 | 0 522 | 523 | 524 | 4 525 | 16 526 | 1 527 | 0 528 | 0 529 | 0 530 | ..\..\..\..\..\..\components\ble\peer_manager\peer_data_storage.c 531 | peer_data_storage.c 532 | 0 533 | 0 534 | 535 | 536 | 4 537 | 17 538 | 1 539 | 0 540 | 0 541 | 0 542 | ..\..\..\..\..\..\components\ble\peer_manager\peer_database.c 543 | peer_database.c 544 | 0 545 | 0 546 | 547 | 548 | 4 549 | 18 550 | 1 551 | 0 552 | 0 553 | 0 554 | ..\..\..\..\..\..\components\ble\peer_manager\peer_id.c 555 | peer_id.c 556 | 0 557 | 0 558 | 559 | 560 | 4 561 | 19 562 | 1 563 | 0 564 | 0 565 | 0 566 | ..\..\..\..\..\..\components\ble\peer_manager\peer_manager.c 567 | peer_manager.c 568 | 0 569 | 0 570 | 571 | 572 | 4 573 | 20 574 | 1 575 | 0 576 | 0 577 | 0 578 | ..\..\..\..\..\..\components\ble\peer_manager\pm_buffer.c 579 | pm_buffer.c 580 | 0 581 | 0 582 | 583 | 584 | 4 585 | 21 586 | 1 587 | 0 588 | 0 589 | 0 590 | ..\..\..\..\..\..\components\ble\peer_manager\pm_mutex.c 591 | pm_mutex.c 592 | 0 593 | 0 594 | 595 | 596 | 4 597 | 22 598 | 1 599 | 0 600 | 0 601 | 0 602 | ..\..\..\..\..\..\components\ble\peer_manager\security_dispatcher.c 603 | security_dispatcher.c 604 | 0 605 | 0 606 | 607 | 608 | 4 609 | 23 610 | 1 611 | 0 612 | 0 613 | 0 614 | ..\..\..\..\..\..\components\ble\peer_manager\security_manager.c 615 | security_manager.c 616 | 0 617 | 0 618 | 619 | 620 | 621 | 622 | nRF_Drivers 623 | 0 624 | 0 625 | 0 626 | 0 627 | 628 | 5 629 | 24 630 | 1 631 | 0 632 | 0 633 | 0 634 | ..\..\..\..\..\..\integration\nrfx\legacy\nrf_drv_clock.c 635 | nrf_drv_clock.c 636 | 0 637 | 0 638 | 639 | 640 | 5 641 | 25 642 | 1 643 | 0 644 | 0 645 | 0 646 | ..\..\..\..\..\..\integration\nrfx\legacy\nrf_drv_uart.c 647 | nrf_drv_uart.c 648 | 0 649 | 0 650 | 651 | 652 | 5 653 | 26 654 | 1 655 | 0 656 | 0 657 | 0 658 | ..\..\..\..\..\..\modules\nrfx\drivers\src\nrfx_clock.c 659 | nrfx_clock.c 660 | 0 661 | 0 662 | 663 | 664 | 5 665 | 27 666 | 1 667 | 0 668 | 0 669 | 0 670 | ..\..\..\..\..\..\modules\nrfx\drivers\src\nrfx_gpiote.c 671 | nrfx_gpiote.c 672 | 0 673 | 0 674 | 675 | 676 | 5 677 | 28 678 | 1 679 | 0 680 | 0 681 | 0 682 | ..\..\..\..\..\..\modules\nrfx\drivers\src\nrfx_power_clock.c 683 | nrfx_power_clock.c 684 | 0 685 | 0 686 | 687 | 688 | 5 689 | 29 690 | 1 691 | 0 692 | 0 693 | 0 694 | ..\..\..\..\..\..\modules\nrfx\drivers\src\prs\nrfx_prs.c 695 | nrfx_prs.c 696 | 0 697 | 0 698 | 699 | 700 | 5 701 | 30 702 | 1 703 | 0 704 | 0 705 | 0 706 | ..\..\..\..\..\..\modules\nrfx\drivers\src\nrfx_uart.c 707 | nrfx_uart.c 708 | 0 709 | 0 710 | 711 | 712 | 5 713 | 31 714 | 1 715 | 0 716 | 0 717 | 0 718 | ..\..\..\..\..\..\modules\nrfx\drivers\src\nrfx_uarte.c 719 | nrfx_uarte.c 720 | 0 721 | 0 722 | 723 | 724 | 725 | 726 | nRF_Libraries 727 | 0 728 | 0 729 | 0 730 | 0 731 | 732 | 6 733 | 32 734 | 1 735 | 0 736 | 0 737 | 0 738 | ..\..\..\..\..\..\components\libraries\button\app_button.c 739 | app_button.c 740 | 0 741 | 0 742 | 743 | 744 | 6 745 | 33 746 | 1 747 | 0 748 | 0 749 | 0 750 | ..\..\..\..\..\..\components\libraries\util\app_error.c 751 | app_error.c 752 | 0 753 | 0 754 | 755 | 756 | 6 757 | 34 758 | 1 759 | 0 760 | 0 761 | 0 762 | ..\..\..\..\..\..\components\libraries\util\app_error_handler_keil.c 763 | app_error_handler_keil.c 764 | 0 765 | 0 766 | 767 | 768 | 6 769 | 35 770 | 1 771 | 0 772 | 0 773 | 0 774 | ..\..\..\..\..\..\components\libraries\util\app_error_weak.c 775 | app_error_weak.c 776 | 0 777 | 0 778 | 779 | 780 | 6 781 | 36 782 | 1 783 | 0 784 | 0 785 | 0 786 | ..\..\..\..\..\..\components\libraries\scheduler\app_scheduler.c 787 | app_scheduler.c 788 | 0 789 | 0 790 | 791 | 792 | 6 793 | 37 794 | 1 795 | 0 796 | 0 797 | 0 798 | ..\..\..\..\..\..\components\libraries\timer\app_timer.c 799 | app_timer.c 800 | 0 801 | 0 802 | 803 | 804 | 6 805 | 38 806 | 1 807 | 0 808 | 0 809 | 0 810 | ..\..\..\..\..\..\components\libraries\util\app_util_platform.c 811 | app_util_platform.c 812 | 0 813 | 0 814 | 815 | 816 | 6 817 | 39 818 | 1 819 | 0 820 | 0 821 | 0 822 | ..\..\..\..\..\..\components\libraries\crc16\crc16.c 823 | crc16.c 824 | 0 825 | 0 826 | 827 | 828 | 6 829 | 40 830 | 1 831 | 0 832 | 0 833 | 0 834 | ..\..\..\..\..\..\components\libraries\fds\fds.c 835 | fds.c 836 | 0 837 | 0 838 | 839 | 840 | 6 841 | 41 842 | 1 843 | 0 844 | 0 845 | 0 846 | ..\..\..\..\..\..\components\libraries\hardfault\hardfault_implementation.c 847 | hardfault_implementation.c 848 | 0 849 | 0 850 | 851 | 852 | 6 853 | 42 854 | 1 855 | 0 856 | 0 857 | 0 858 | ..\..\..\..\..\..\components\libraries\util\nrf_assert.c 859 | nrf_assert.c 860 | 0 861 | 0 862 | 863 | 864 | 6 865 | 43 866 | 1 867 | 0 868 | 0 869 | 0 870 | ..\..\..\..\..\..\components\libraries\atomic_fifo\nrf_atfifo.c 871 | nrf_atfifo.c 872 | 0 873 | 0 874 | 875 | 876 | 6 877 | 44 878 | 1 879 | 0 880 | 0 881 | 0 882 | ..\..\..\..\..\..\components\libraries\atomic_flags\nrf_atflags.c 883 | nrf_atflags.c 884 | 0 885 | 0 886 | 887 | 888 | 6 889 | 45 890 | 1 891 | 0 892 | 0 893 | 0 894 | ..\..\..\..\..\..\components\libraries\atomic\nrf_atomic.c 895 | nrf_atomic.c 896 | 0 897 | 0 898 | 899 | 900 | 6 901 | 46 902 | 1 903 | 0 904 | 0 905 | 0 906 | ..\..\..\..\..\..\components\libraries\balloc\nrf_balloc.c 907 | nrf_balloc.c 908 | 0 909 | 0 910 | 911 | 912 | 6 913 | 47 914 | 1 915 | 0 916 | 0 917 | 0 918 | ..\..\..\..\..\..\external\fprintf\nrf_fprintf.c 919 | nrf_fprintf.c 920 | 0 921 | 0 922 | 923 | 924 | 6 925 | 48 926 | 1 927 | 0 928 | 0 929 | 0 930 | ..\..\..\..\..\..\external\fprintf\nrf_fprintf_format.c 931 | nrf_fprintf_format.c 932 | 0 933 | 0 934 | 935 | 936 | 6 937 | 49 938 | 1 939 | 0 940 | 0 941 | 0 942 | ..\..\..\..\..\..\components\libraries\fstorage\nrf_fstorage.c 943 | nrf_fstorage.c 944 | 0 945 | 0 946 | 947 | 948 | 6 949 | 50 950 | 1 951 | 0 952 | 0 953 | 0 954 | ..\..\..\..\..\..\components\libraries\fstorage\nrf_fstorage_sd.c 955 | nrf_fstorage_sd.c 956 | 0 957 | 0 958 | 959 | 960 | 6 961 | 51 962 | 1 963 | 0 964 | 0 965 | 0 966 | ..\..\..\..\..\..\components\libraries\experimental_memobj\nrf_memobj.c 967 | nrf_memobj.c 968 | 0 969 | 0 970 | 971 | 972 | 6 973 | 52 974 | 1 975 | 0 976 | 0 977 | 0 978 | ..\..\..\..\..\..\components\libraries\pwr_mgmt\nrf_pwr_mgmt.c 979 | nrf_pwr_mgmt.c 980 | 0 981 | 0 982 | 983 | 984 | 6 985 | 53 986 | 1 987 | 0 988 | 0 989 | 0 990 | ..\..\..\..\..\..\components\libraries\experimental_section_vars\nrf_section_iter.c 991 | nrf_section_iter.c 992 | 0 993 | 0 994 | 995 | 996 | 6 997 | 54 998 | 1 999 | 0 1000 | 0 1001 | 0 1002 | ..\..\..\..\..\..\components\libraries\strerror\nrf_strerror.c 1003 | nrf_strerror.c 1004 | 0 1005 | 0 1006 | 1007 | 1008 | 6 1009 | 55 1010 | 1 1011 | 0 1012 | 0 1013 | 0 1014 | ..\..\..\..\..\..\components\libraries\sensorsim\sensorsim.c 1015 | sensorsim.c 1016 | 0 1017 | 0 1018 | 1019 | 1020 | 1021 | 1022 | nRF_Log 1023 | 0 1024 | 0 1025 | 0 1026 | 0 1027 | 1028 | 7 1029 | 56 1030 | 1 1031 | 0 1032 | 0 1033 | 0 1034 | ..\..\..\..\..\..\components\libraries\experimental_log\src\nrf_log_backend_rtt.c 1035 | nrf_log_backend_rtt.c 1036 | 0 1037 | 0 1038 | 1039 | 1040 | 7 1041 | 57 1042 | 1 1043 | 0 1044 | 0 1045 | 0 1046 | ..\..\..\..\..\..\components\libraries\experimental_log\src\nrf_log_backend_serial.c 1047 | nrf_log_backend_serial.c 1048 | 0 1049 | 0 1050 | 1051 | 1052 | 7 1053 | 58 1054 | 1 1055 | 0 1056 | 0 1057 | 0 1058 | ..\..\..\..\..\..\components\libraries\experimental_log\src\nrf_log_backend_uart.c 1059 | nrf_log_backend_uart.c 1060 | 0 1061 | 0 1062 | 1063 | 1064 | 7 1065 | 59 1066 | 1 1067 | 0 1068 | 0 1069 | 0 1070 | ..\..\..\..\..\..\components\libraries\experimental_log\src\nrf_log_default_backends.c 1071 | nrf_log_default_backends.c 1072 | 0 1073 | 0 1074 | 1075 | 1076 | 7 1077 | 60 1078 | 1 1079 | 0 1080 | 0 1081 | 0 1082 | ..\..\..\..\..\..\components\libraries\experimental_log\src\nrf_log_frontend.c 1083 | nrf_log_frontend.c 1084 | 0 1085 | 0 1086 | 1087 | 1088 | 7 1089 | 61 1090 | 1 1091 | 0 1092 | 0 1093 | 0 1094 | ..\..\..\..\..\..\components\libraries\experimental_log\src\nrf_log_str_formatter.c 1095 | nrf_log_str_formatter.c 1096 | 0 1097 | 0 1098 | 1099 | 1100 | 1101 | 1102 | nRF_Segger_RTT 1103 | 0 1104 | 0 1105 | 0 1106 | 0 1107 | 1108 | 8 1109 | 62 1110 | 1 1111 | 0 1112 | 0 1113 | 0 1114 | ..\..\..\..\..\..\external\segger_rtt\SEGGER_RTT.c 1115 | SEGGER_RTT.c 1116 | 0 1117 | 0 1118 | 1119 | 1120 | 8 1121 | 63 1122 | 1 1123 | 0 1124 | 0 1125 | 0 1126 | ..\..\..\..\..\..\external\segger_rtt\SEGGER_RTT_Syscalls_KEIL.c 1127 | SEGGER_RTT_Syscalls_KEIL.c 1128 | 0 1129 | 0 1130 | 1131 | 1132 | 8 1133 | 64 1134 | 1 1135 | 0 1136 | 0 1137 | 0 1138 | ..\..\..\..\..\..\external\segger_rtt\SEGGER_RTT_printf.c 1139 | SEGGER_RTT_printf.c 1140 | 0 1141 | 0 1142 | 1143 | 1144 | 1145 | 1146 | nRF_SoftDevice 1147 | 0 1148 | 0 1149 | 0 1150 | 0 1151 | 1152 | 9 1153 | 65 1154 | 1 1155 | 0 1156 | 0 1157 | 0 1158 | ..\..\..\..\..\..\components\softdevice\common\nrf_sdh.c 1159 | nrf_sdh.c 1160 | 0 1161 | 0 1162 | 1163 | 1164 | 9 1165 | 66 1166 | 1 1167 | 0 1168 | 0 1169 | 0 1170 | ..\..\..\..\..\..\components\softdevice\common\nrf_sdh_ble.c 1171 | nrf_sdh_ble.c 1172 | 0 1173 | 0 1174 | 1175 | 1176 | 9 1177 | 67 1178 | 1 1179 | 0 1180 | 0 1181 | 0 1182 | ..\..\..\..\..\..\components\softdevice\common\nrf_sdh_soc.c 1183 | nrf_sdh_soc.c 1184 | 0 1185 | 0 1186 | 1187 | 1188 | 1189 | 1190 | nRF_BLE_Services 1191 | 0 1192 | 0 1193 | 0 1194 | 0 1195 | 1196 | 10 1197 | 68 1198 | 1 1199 | 0 1200 | 0 1201 | 0 1202 | ..\..\..\ble_cus.c 1203 | ble_cus.c 1204 | 0 1205 | 0 1206 | 1207 | 1208 | 1209 | 1210 | ::CMSIS 1211 | 0 1212 | 0 1213 | 0 1214 | 1 1215 | 1216 | 1217 | 1218 | ::Device 1219 | 0 1220 | 0 1221 | 0 1222 | 1 1223 | 1224 | 1225 |
1226 | -------------------------------------------------------------------------------- /pca10040/s132/armgcc/Makefile: -------------------------------------------------------------------------------- 1 | PROJECT_NAME := ble_app_template_pca10040_s132 2 | TARGETS := nrf52832_xxaa 3 | OUTPUT_DIRECTORY := _build 4 | 5 | SDK_ROOT := ../../../../../.. 6 | PROJ_DIR := ../../.. 7 | 8 | $(OUTPUT_DIRECTORY)/nrf52832_xxaa.out: \ 9 | LINKER_SCRIPT := ble_app_template_gcc_nrf52.ld 10 | 11 | # Source files common to all targets 12 | SRC_FILES += \ 13 | $(SDK_ROOT)/modules/nrfx/mdk/gcc_startup_nrf52.S \ 14 | $(SDK_ROOT)/components/libraries/experimental_log/src/nrf_log_backend_rtt.c \ 15 | $(SDK_ROOT)/components/libraries/experimental_log/src/nrf_log_backend_serial.c \ 16 | $(SDK_ROOT)/components/libraries/experimental_log/src/nrf_log_backend_uart.c \ 17 | $(SDK_ROOT)/components/libraries/experimental_log/src/nrf_log_default_backends.c \ 18 | $(SDK_ROOT)/components/libraries/experimental_log/src/nrf_log_frontend.c \ 19 | $(SDK_ROOT)/components/libraries/experimental_log/src/nrf_log_str_formatter.c \ 20 | $(SDK_ROOT)/components/libraries/button/app_button.c \ 21 | $(SDK_ROOT)/components/libraries/util/app_error.c \ 22 | $(SDK_ROOT)/components/libraries/util/app_error_handler_gcc.c \ 23 | $(SDK_ROOT)/components/libraries/util/app_error_weak.c \ 24 | $(SDK_ROOT)/components/libraries/scheduler/app_scheduler.c \ 25 | $(SDK_ROOT)/components/libraries/timer/app_timer.c \ 26 | $(SDK_ROOT)/components/libraries/util/app_util_platform.c \ 27 | $(SDK_ROOT)/components/libraries/crc16/crc16.c \ 28 | $(SDK_ROOT)/components/libraries/fds/fds.c \ 29 | $(SDK_ROOT)/components/libraries/hardfault/hardfault_implementation.c \ 30 | $(SDK_ROOT)/components/libraries/util/nrf_assert.c \ 31 | $(SDK_ROOT)/components/libraries/atomic_fifo/nrf_atfifo.c \ 32 | $(SDK_ROOT)/components/libraries/atomic_flags/nrf_atflags.c \ 33 | $(SDK_ROOT)/components/libraries/atomic/nrf_atomic.c \ 34 | $(SDK_ROOT)/components/libraries/balloc/nrf_balloc.c \ 35 | $(SDK_ROOT)/external/fprintf/nrf_fprintf.c \ 36 | $(SDK_ROOT)/external/fprintf/nrf_fprintf_format.c \ 37 | $(SDK_ROOT)/components/libraries/fstorage/nrf_fstorage.c \ 38 | $(SDK_ROOT)/components/libraries/fstorage/nrf_fstorage_sd.c \ 39 | $(SDK_ROOT)/components/libraries/experimental_memobj/nrf_memobj.c \ 40 | $(SDK_ROOT)/components/libraries/pwr_mgmt/nrf_pwr_mgmt.c \ 41 | $(SDK_ROOT)/components/libraries/experimental_section_vars/nrf_section_iter.c \ 42 | $(SDK_ROOT)/components/libraries/strerror/nrf_strerror.c \ 43 | $(SDK_ROOT)/components/libraries/sensorsim/sensorsim.c \ 44 | $(SDK_ROOT)/modules/nrfx/mdk/system_nrf52.c \ 45 | $(SDK_ROOT)/components/boards/boards.c \ 46 | $(SDK_ROOT)/integration/nrfx/legacy/nrf_drv_clock.c \ 47 | $(SDK_ROOT)/integration/nrfx/legacy/nrf_drv_uart.c \ 48 | $(SDK_ROOT)/modules/nrfx/drivers/src/nrfx_clock.c \ 49 | $(SDK_ROOT)/modules/nrfx/drivers/src/nrfx_gpiote.c \ 50 | $(SDK_ROOT)/modules/nrfx/drivers/src/nrfx_power_clock.c \ 51 | $(SDK_ROOT)/modules/nrfx/drivers/src/prs/nrfx_prs.c \ 52 | $(SDK_ROOT)/modules/nrfx/drivers/src/nrfx_uart.c \ 53 | $(SDK_ROOT)/modules/nrfx/drivers/src/nrfx_uarte.c \ 54 | $(SDK_ROOT)/components/libraries/bsp/bsp.c \ 55 | $(SDK_ROOT)/components/libraries/bsp/bsp_btn_ble.c \ 56 | $(PROJ_DIR)/main.c \ 57 | $(PROJ_DIR)/ble_cus.c \ 58 | $(SDK_ROOT)/external/segger_rtt/SEGGER_RTT_Syscalls_GCC.c \ 59 | $(SDK_ROOT)/external/segger_rtt/SEGGER_RTT.c \ 60 | $(SDK_ROOT)/external/segger_rtt/SEGGER_RTT_printf.c \ 61 | $(SDK_ROOT)/components/ble/common/ble_advdata.c \ 62 | $(SDK_ROOT)/components/ble/ble_advertising/ble_advertising.c \ 63 | $(SDK_ROOT)/components/ble/common/ble_conn_params.c \ 64 | $(SDK_ROOT)/components/ble/common/ble_conn_state.c \ 65 | $(SDK_ROOT)/components/ble/common/ble_srv_common.c \ 66 | $(SDK_ROOT)/components/ble/peer_manager/gatt_cache_manager.c \ 67 | $(SDK_ROOT)/components/ble/peer_manager/gatts_cache_manager.c \ 68 | $(SDK_ROOT)/components/ble/peer_manager/id_manager.c \ 69 | $(SDK_ROOT)/components/ble/nrf_ble_gatt/nrf_ble_gatt.c \ 70 | $(SDK_ROOT)/components/ble/nrf_ble_qwr/nrf_ble_qwr.c \ 71 | $(SDK_ROOT)/components/ble/peer_manager/peer_data_storage.c \ 72 | $(SDK_ROOT)/components/ble/peer_manager/peer_database.c \ 73 | $(SDK_ROOT)/components/ble/peer_manager/peer_id.c \ 74 | $(SDK_ROOT)/components/ble/peer_manager/peer_manager.c \ 75 | $(SDK_ROOT)/components/ble/peer_manager/pm_buffer.c \ 76 | $(SDK_ROOT)/components/ble/peer_manager/pm_mutex.c \ 77 | $(SDK_ROOT)/components/ble/peer_manager/security_dispatcher.c \ 78 | $(SDK_ROOT)/components/ble/peer_manager/security_manager.c \ 79 | $(SDK_ROOT)/components/softdevice/common/nrf_sdh.c \ 80 | $(SDK_ROOT)/components/softdevice/common/nrf_sdh_ble.c \ 81 | $(SDK_ROOT)/components/softdevice/common/nrf_sdh_soc.c \ 82 | 83 | # Include folders common to all targets 84 | INC_FOLDERS += \ 85 | $(SDK_ROOT)/components/nfc/ndef/generic/message \ 86 | $(SDK_ROOT)/components/nfc/t2t_lib \ 87 | $(SDK_ROOT)/components/nfc/t4t_parser/hl_detection_procedure \ 88 | $(SDK_ROOT)/components/ble/ble_services/ble_ancs_c \ 89 | $(SDK_ROOT)/components/ble/ble_services/ble_ias_c \ 90 | $(SDK_ROOT)/components/libraries/pwm \ 91 | $(SDK_ROOT)/components/softdevice/s132/headers/nrf52 \ 92 | $(SDK_ROOT)/components/libraries/usbd/class/cdc/acm \ 93 | $(SDK_ROOT)/components/libraries/usbd/class/hid/generic \ 94 | $(SDK_ROOT)/components/libraries/usbd/class/msc \ 95 | $(SDK_ROOT)/components/libraries/usbd/class/hid \ 96 | $(SDK_ROOT)/modules/nrfx/hal \ 97 | $(SDK_ROOT)/components/libraries/experimental_log \ 98 | $(SDK_ROOT)/components/ble/ble_services/ble_gls \ 99 | $(SDK_ROOT)/components/libraries/fstorage \ 100 | $(SDK_ROOT)/components/nfc/ndef/text \ 101 | $(SDK_ROOT)/components/libraries/mutex \ 102 | $(SDK_ROOT)/components/libraries/gpiote \ 103 | $(SDK_ROOT)/components/libraries/experimental_log/src \ 104 | $(SDK_ROOT)/components/libraries/bootloader/ble_dfu \ 105 | $(SDK_ROOT)/components/nfc/ndef/connection_handover/common \ 106 | $(SDK_ROOT)/components/boards \ 107 | $(SDK_ROOT)/components/nfc/ndef/generic/record \ 108 | $(SDK_ROOT)/components/libraries/experimental_memobj \ 109 | $(SDK_ROOT)/components/nfc/t4t_parser/cc_file \ 110 | $(SDK_ROOT)/components/ble/ble_advertising \ 111 | $(SDK_ROOT)/components/ble/ble_services/ble_bas_c \ 112 | $(SDK_ROOT)/modules/nrfx/drivers/include \ 113 | $(SDK_ROOT)/components/libraries/experimental_task_manager \ 114 | $(SDK_ROOT)/components/ble/ble_services/ble_hrs_c \ 115 | $(SDK_ROOT)/components/nfc/ndef/connection_handover/le_oob_rec \ 116 | $(SDK_ROOT)/components/libraries/queue \ 117 | $(SDK_ROOT)/components/libraries/pwr_mgmt \ 118 | $(SDK_ROOT)/components/ble/ble_dtm \ 119 | $(SDK_ROOT)/components/toolchain/cmsis/include \ 120 | $(SDK_ROOT)/components/ble/ble_services/ble_rscs_c \ 121 | $(SDK_ROOT)/components/ble/common \ 122 | $(SDK_ROOT)/components/ble/ble_services/ble_lls \ 123 | $(SDK_ROOT)/components/libraries/bsp \ 124 | $(SDK_ROOT)/components/nfc/ndef/connection_handover/ac_rec \ 125 | $(SDK_ROOT)/components/ble/ble_services/ble_bas \ 126 | $(SDK_ROOT)/components/libraries/experimental_section_vars \ 127 | $(SDK_ROOT)/components/softdevice/s132/headers \ 128 | $(SDK_ROOT)/components/ble/ble_services/ble_ans_c \ 129 | $(SDK_ROOT)/components/libraries/slip \ 130 | $(SDK_ROOT)/components/libraries/delay \ 131 | $(SDK_ROOT)/components/libraries/experimental_mpu \ 132 | $(SDK_ROOT)/components/libraries/mem_manager \ 133 | $(SDK_ROOT)/components/libraries/csense_drv \ 134 | $(SDK_ROOT)/components/ble/ble_services/ble_nus_c \ 135 | $(SDK_ROOT)/components/libraries/usbd/config \ 136 | $(SDK_ROOT)/components/softdevice/common \ 137 | $(SDK_ROOT)/components/ble/ble_services/ble_ias \ 138 | $(SDK_ROOT)/components/libraries/usbd/class/hid/mouse \ 139 | $(SDK_ROOT)/components/libraries/low_power_pwm \ 140 | $(SDK_ROOT)/components/nfc/ndef/conn_hand_parser/ble_oob_advdata_parser \ 141 | $(SDK_ROOT)/components/ble/ble_services/ble_dfu \ 142 | $(SDK_ROOT)/external/fprintf \ 143 | $(SDK_ROOT)/components/libraries/atomic \ 144 | $(SDK_ROOT)/components \ 145 | $(SDK_ROOT)/components/libraries/scheduler \ 146 | $(SDK_ROOT)/components/libraries/cli \ 147 | $(SDK_ROOT)/components/ble/ble_services/ble_lbs \ 148 | $(SDK_ROOT)/components/ble/ble_services/ble_hts \ 149 | $(SDK_ROOT)/components/libraries/crc16 \ 150 | $(SDK_ROOT)/components/nfc/t4t_parser/apdu \ 151 | $(SDK_ROOT)/components/libraries/util \ 152 | ../config \ 153 | $(SDK_ROOT)/components/libraries/usbd/class/cdc \ 154 | $(SDK_ROOT)/components/libraries/csense \ 155 | $(SDK_ROOT)/components/libraries/balloc \ 156 | $(SDK_ROOT)/components/libraries/ecc \ 157 | $(SDK_ROOT)/components/libraries/hardfault \ 158 | $(SDK_ROOT)/components/ble/ble_services/ble_cscs \ 159 | $(SDK_ROOT)/components/libraries/hci \ 160 | $(SDK_ROOT)/components/libraries/timer \ 161 | $(SDK_ROOT)/integration/nrfx \ 162 | $(SDK_ROOT)/components/nfc/t4t_parser/tlv \ 163 | $(SDK_ROOT)/components/libraries/sortlist \ 164 | $(SDK_ROOT)/components/libraries/spi_mngr \ 165 | $(SDK_ROOT)/components/libraries/experimental_stack_guard \ 166 | $(SDK_ROOT)/components/libraries/led_softblink \ 167 | $(SDK_ROOT)/components/nfc/ndef/conn_hand_parser \ 168 | $(SDK_ROOT)/components/libraries/sdcard \ 169 | $(SDK_ROOT)/components/nfc/ndef/parser/record \ 170 | $(SDK_ROOT)/modules/nrfx/mdk \ 171 | $(SDK_ROOT)/components/ble/ble_services/ble_cts_c \ 172 | $(SDK_ROOT)/components/nfc/ndef/conn_hand_parser/le_oob_rec_parser \ 173 | $(SDK_ROOT)/components/ble/ble_services/ble_nus \ 174 | $(SDK_ROOT)/components/libraries/twi_mngr \ 175 | $(SDK_ROOT)/components/ble/ble_services/ble_hids \ 176 | $(SDK_ROOT)/components/libraries/strerror \ 177 | $(SDK_ROOT)/components/libraries/crc32 \ 178 | $(SDK_ROOT)/components/nfc/ndef/connection_handover/ble_oob_advdata \ 179 | $(SDK_ROOT)/components/nfc/t2t_parser \ 180 | $(SDK_ROOT)/components/nfc/ndef/connection_handover/ble_pair_msg \ 181 | $(SDK_ROOT)/components/libraries/usbd/class/audio \ 182 | $(SDK_ROOT)/components/nfc/t4t_lib/hal_t4t \ 183 | $(SDK_ROOT)/components/libraries/sensorsim \ 184 | $(SDK_ROOT)/components/nfc/t4t_lib \ 185 | $(SDK_ROOT)/components/ble/peer_manager \ 186 | $(SDK_ROOT)/components/drivers_nrf/usbd \ 187 | $(SDK_ROOT)/components/ble/ble_services/ble_tps \ 188 | $(SDK_ROOT)/components/nfc/ndef/parser/message \ 189 | $(SDK_ROOT)/components/ble/ble_services/ble_dis \ 190 | $(SDK_ROOT)/components/nfc/ndef/uri \ 191 | $(SDK_ROOT)/components/ble/nrf_ble_gatt \ 192 | $(SDK_ROOT)/components/ble/nrf_ble_qwr \ 193 | $(SDK_ROOT)/components/libraries/gfx \ 194 | $(SDK_ROOT)/components/libraries/button \ 195 | $(SDK_ROOT)/modules/nrfx \ 196 | $(SDK_ROOT)/components/libraries/twi_sensor \ 197 | $(SDK_ROOT)/integration/nrfx/legacy \ 198 | $(SDK_ROOT)/components/libraries/usbd/class/hid/kbd \ 199 | $(SDK_ROOT)/components/nfc/ndef/connection_handover/ep_oob_rec \ 200 | $(SDK_ROOT)/external/segger_rtt \ 201 | $(SDK_ROOT)/components/libraries/atomic_fifo \ 202 | $(SDK_ROOT)/components/libraries/experimental_ringbuf \ 203 | $(SDK_ROOT)/components/ble/ble_services/ble_lbs_c \ 204 | $(SDK_ROOT)/components/nfc/ndef/connection_handover/ble_pair_lib \ 205 | $(SDK_ROOT)/components/libraries/crypto \ 206 | $(SDK_ROOT)/components/ble/ble_racp \ 207 | $(SDK_ROOT)/components/libraries/fds \ 208 | $(SDK_ROOT)/components/nfc/ndef/launchapp \ 209 | $(SDK_ROOT)/components/libraries/atomic_flags \ 210 | $(SDK_ROOT)/components/ble/ble_services/ble_rscs \ 211 | $(SDK_ROOT)/components/nfc/ndef/connection_handover/hs_rec \ 212 | $(SDK_ROOT)/components/nfc/t2t_lib/hal_t2t \ 213 | $(SDK_ROOT)/components/libraries/usbd \ 214 | $(SDK_ROOT)/components/nfc/ndef/conn_hand_parser/ac_rec_parser \ 215 | $(SDK_ROOT)/components/ble/ble_services/ble_hrs \ 216 | 217 | # Libraries common to all targets 218 | LIB_FILES += \ 219 | 220 | # Optimization flags 221 | OPT = -O3 -g3 222 | # Uncomment the line below to enable link time optimization 223 | #OPT += -flto 224 | 225 | # C flags common to all targets 226 | CFLAGS += $(OPT) 227 | CFLAGS += -DBOARD_PCA10040 228 | CFLAGS += -DCONFIG_GPIO_AS_PINRESET 229 | CFLAGS += -DFLOAT_ABI_HARD 230 | CFLAGS += -DNRF52 231 | CFLAGS += -DNRF52832_XXAA 232 | CFLAGS += -DNRF52_PAN_74 233 | CFLAGS += -DNRF_SD_BLE_API_VERSION=6 234 | CFLAGS += -DS132 235 | CFLAGS += -DSOFTDEVICE_PRESENT 236 | CFLAGS += -DSWI_DISABLE0 237 | CFLAGS += -mcpu=cortex-m4 238 | CFLAGS += -mthumb -mabi=aapcs 239 | CFLAGS += -Wall -Werror 240 | CFLAGS += -mfloat-abi=hard -mfpu=fpv4-sp-d16 241 | # keep every function in a separate section, this allows linker to discard unused ones 242 | CFLAGS += -ffunction-sections -fdata-sections -fno-strict-aliasing 243 | CFLAGS += -fno-builtin -fshort-enums 244 | 245 | # C++ flags common to all targets 246 | CXXFLAGS += $(OPT) 247 | 248 | # Assembler flags common to all targets 249 | ASMFLAGS += -g3 250 | ASMFLAGS += -mcpu=cortex-m4 251 | ASMFLAGS += -mthumb -mabi=aapcs 252 | ASMFLAGS += -mfloat-abi=hard -mfpu=fpv4-sp-d16 253 | ASMFLAGS += -DBOARD_PCA10040 254 | ASMFLAGS += -DCONFIG_GPIO_AS_PINRESET 255 | ASMFLAGS += -DFLOAT_ABI_HARD 256 | ASMFLAGS += -DNRF52 257 | ASMFLAGS += -DNRF52832_XXAA 258 | ASMFLAGS += -DNRF52_PAN_74 259 | ASMFLAGS += -DNRF_SD_BLE_API_VERSION=6 260 | ASMFLAGS += -DS132 261 | ASMFLAGS += -DSOFTDEVICE_PRESENT 262 | ASMFLAGS += -DSWI_DISABLE0 263 | 264 | # Linker flags 265 | LDFLAGS += $(OPT) 266 | LDFLAGS += -mthumb -mabi=aapcs -L$(SDK_ROOT)/modules/nrfx/mdk -T$(LINKER_SCRIPT) 267 | LDFLAGS += -mcpu=cortex-m4 268 | LDFLAGS += -mfloat-abi=hard -mfpu=fpv4-sp-d16 269 | # let linker dump unused sections 270 | LDFLAGS += -Wl,--gc-sections 271 | # use newlib in nano version 272 | LDFLAGS += --specs=nano.specs 273 | 274 | nrf52832_xxaa: CFLAGS += -D__HEAP_SIZE=8192 275 | nrf52832_xxaa: CFLAGS += -D__STACK_SIZE=8192 276 | nrf52832_xxaa: ASMFLAGS += -D__HEAP_SIZE=8192 277 | nrf52832_xxaa: ASMFLAGS += -D__STACK_SIZE=8192 278 | # Add standard libraries at the very end of the linker input, after all objects 279 | # that may need symbols provided by these libraries. 280 | LIB_FILES += -lc -lnosys -lm 281 | 282 | 283 | .PHONY: default help 284 | 285 | # Default target - first one defined 286 | default: nrf52832_xxaa 287 | 288 | # Print all targets that can be built 289 | help: 290 | @echo following targets are available: 291 | @echo nrf52832_xxaa 292 | @echo flash_softdevice 293 | @echo sdk_config - starting external tool for editing sdk_config.h 294 | @echo flash - flashing binary 295 | TEMPLATE_PATH := $(SDK_ROOT)/components/toolchain/gcc 296 | 297 | include $(TEMPLATE_PATH)/Makefile.common 298 | 299 | $(foreach target, $(TARGETS), $(call define_target, $(target))) 300 | 301 | .PHONY: flash flash_softdevice erase 302 | 303 | # Flash the program 304 | flash: $(OUTPUT_DIRECTORY)/nrf52832_xxaa.hex 305 | @echo Flashing: $< 306 | nrfjprog -f nrf52 --program $< --sectorerase 307 | nrfjprog -f nrf52 --reset 308 | 309 | # Flash softdevice 310 | flash_softdevice: 311 | @echo Flashing: s132_nrf52_6.0.0_softdevice.hex 312 | nrfjprog -f nrf52 --program $(SDK_ROOT)/components/softdevice/s132/hex/s132_nrf52_6.0.0_softdevice.hex --sectorerase 313 | nrfjprog -f nrf52 --reset 314 | 315 | erase: 316 | nrfjprog -f nrf52 --eraseall 317 | 318 | SDK_CONFIG_FILE := ../config/sdk_config.h 319 | CMSIS_CONFIG_TOOL := $(SDK_ROOT)/external_tools/cmsisconfig/CMSIS_Configuration_Wizard.jar 320 | sdk_config: 321 | java -jar $(CMSIS_CONFIG_TOOL) $(SDK_CONFIG_FILE) -------------------------------------------------------------------------------- /pca10040/s132/armgcc/ble_app_template_gcc_nrf52.ld: -------------------------------------------------------------------------------- 1 | /* Linker script to configure memory regions. */ 2 | 3 | SEARCH_DIR(.) 4 | GROUP(-lgcc -lc -lnosys) 5 | 6 | MEMORY 7 | { 8 | FLASH (rx) : ORIGIN = 0x26000, LENGTH = 0x5a000 9 | RAM (rwx) : ORIGIN = 0x20002220, LENGTH = 0xdde0 10 | } 11 | 12 | SECTIONS 13 | { 14 | } 15 | 16 | SECTIONS 17 | { 18 | . = ALIGN(4); 19 | .mem_section_dummy_ram : 20 | { 21 | } 22 | .log_dynamic_data : 23 | { 24 | PROVIDE(__start_log_dynamic_data = .); 25 | KEEP(*(SORT(.log_dynamic_data*))) 26 | PROVIDE(__stop_log_dynamic_data = .); 27 | } > RAM 28 | .fs_data : 29 | { 30 | PROVIDE(__start_fs_data = .); 31 | KEEP(*(.fs_data)) 32 | PROVIDE(__stop_fs_data = .); 33 | } > RAM 34 | .cli_sorted_cmd_ptrs : 35 | { 36 | PROVIDE(__start_cli_sorted_cmd_ptrs = .); 37 | KEEP(*(.cli_sorted_cmd_ptrs)) 38 | PROVIDE(__stop_cli_sorted_cmd_ptrs = .); 39 | } > RAM 40 | 41 | } INSERT AFTER .data; 42 | 43 | SECTIONS 44 | { 45 | .mem_section_dummy_rom : 46 | { 47 | } 48 | .sdh_soc_observers : 49 | { 50 | PROVIDE(__start_sdh_soc_observers = .); 51 | KEEP(*(SORT(.sdh_soc_observers*))) 52 | PROVIDE(__stop_sdh_soc_observers = .); 53 | } > FLASH 54 | .pwr_mgmt_data : 55 | { 56 | PROVIDE(__start_pwr_mgmt_data = .); 57 | KEEP(*(SORT(.pwr_mgmt_data*))) 58 | PROVIDE(__stop_pwr_mgmt_data = .); 59 | } > FLASH 60 | .sdh_ble_observers : 61 | { 62 | PROVIDE(__start_sdh_ble_observers = .); 63 | KEEP(*(SORT(.sdh_ble_observers*))) 64 | PROVIDE(__stop_sdh_ble_observers = .); 65 | } > FLASH 66 | .log_const_data : 67 | { 68 | PROVIDE(__start_log_const_data = .); 69 | KEEP(*(SORT(.log_const_data*))) 70 | PROVIDE(__stop_log_const_data = .); 71 | } > FLASH 72 | .nrf_balloc : 73 | { 74 | PROVIDE(__start_nrf_balloc = .); 75 | KEEP(*(.nrf_balloc)) 76 | PROVIDE(__stop_nrf_balloc = .); 77 | } > FLASH 78 | .sdh_state_observers : 79 | { 80 | PROVIDE(__start_sdh_state_observers = .); 81 | KEEP(*(SORT(.sdh_state_observers*))) 82 | PROVIDE(__stop_sdh_state_observers = .); 83 | } > FLASH 84 | .sdh_stack_observers : 85 | { 86 | PROVIDE(__start_sdh_stack_observers = .); 87 | KEEP(*(SORT(.sdh_stack_observers*))) 88 | PROVIDE(__stop_sdh_stack_observers = .); 89 | } > FLASH 90 | .sdh_req_observers : 91 | { 92 | PROVIDE(__start_sdh_req_observers = .); 93 | KEEP(*(SORT(.sdh_req_observers*))) 94 | PROVIDE(__stop_sdh_req_observers = .); 95 | } > FLASH 96 | .nrf_queue : 97 | { 98 | PROVIDE(__start_nrf_queue = .); 99 | KEEP(*(.nrf_queue)) 100 | PROVIDE(__stop_nrf_queue = .); 101 | } > FLASH 102 | .cli_command : 103 | { 104 | PROVIDE(__start_cli_command = .); 105 | KEEP(*(.cli_command)) 106 | PROVIDE(__stop_cli_command = .); 107 | } > FLASH 108 | .crypto_data : 109 | { 110 | PROVIDE(__start_crypto_data = .); 111 | KEEP(*(SORT(.crypto_data*))) 112 | PROVIDE(__stop_crypto_data = .); 113 | } > FLASH 114 | 115 | } INSERT AFTER .text 116 | 117 | INCLUDE "nrf_common.ld" 118 | -------------------------------------------------------------------------------- /pca10040/s132/ses/ble_app_template_pca10040_s132.emProject: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 138 | 143 | 144 | -------------------------------------------------------------------------------- /pca10040/s132/ses/ble_app_template_pca10040_s132.emSession: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /pca10040/s132/ses/flash_placement.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /pdf/01_Nordic_Company_Introduction.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NordicPlayground/nRF52-Bluetooth-Course/37311502d5f65c9d1071b71cdec9bdab28924618/pdf/01_Nordic_Company_Introduction.pdf -------------------------------------------------------------------------------- /pdf/02_nRF52 Student_ Intro_v0.1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NordicPlayground/nRF52-Bluetooth-Course/37311502d5f65c9d1071b71cdec9bdab28924618/pdf/02_nRF52 Student_ Intro_v0.1.pdf -------------------------------------------------------------------------------- /pdf/03_Bluetooth Overview.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NordicPlayground/nRF52-Bluetooth-Course/37311502d5f65c9d1071b71cdec9bdab28924618/pdf/03_Bluetooth Overview.pdf -------------------------------------------------------------------------------- /pdf/04_Softdevice_Student_Introduction_v0.1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NordicPlayground/nRF52-Bluetooth-Course/37311502d5f65c9d1071b71cdec9bdab28924618/pdf/04_Softdevice_Student_Introduction_v0.1.pdf --------------------------------------------------------------------------------