ref: e5b73212f6addcfdb5e306df63d7135e543c4f8d
src/libs/mynewt-nimble/nimble/drivers/nrf51/src/ble_hw.c
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 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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 |
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ #include <stdint.h> #include <assert.h> #include <string.h> #include "syscfg/syscfg.h" #include "os/os.h" #include "ble/xcvr.h" #include "nimble/ble.h" #include "nimble/nimble_opt.h" #include "nrfx.h" #include "controller/ble_hw.h" #if MYNEWT #include "mcu/cmsis_nvic.h" #else #include "core_cm0.h" #include <nimble/nimble_npl_os.h> #endif #include "os/os_trace_api.h" /* Total number of resolving list elements */ #define BLE_HW_RESOLV_LIST_SIZE (16) /* We use this to keep track of which entries are set to valid addresses */ static uint8_t g_ble_hw_whitelist_mask; /* Random number generator isr callback */ ble_rng_isr_cb_t g_ble_rng_isr_cb; /* If LL privacy is enabled, allocate memory for AAR */ #if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) /* The NRF51 supports up to 16 IRK entries */ #if (MYNEWT_VAL(BLE_LL_RESOLV_LIST_SIZE) < 16) #define NRF_IRK_LIST_ENTRIES (MYNEWT_VAL(BLE_LL_RESOLV_LIST_SIZE)) #else #define NRF_IRK_LIST_ENTRIES (16) #endif /* NOTE: each entry is 16 bytes long. */ uint32_t g_nrf_irk_list[NRF_IRK_LIST_ENTRIES * 4]; /* Current number of IRK entries */ uint8_t g_nrf_num_irks; #endif /* Returns public device address or -1 if not present */ int ble_hw_get_public_addr(ble_addr_t *addr) { uint32_t addr_high; uint32_t addr_low; /* Does FICR have a public address */ if ((NRF_FICR->DEVICEADDRTYPE & 1) != 0) { return -1; } /* Copy into device address. We can do this because we know platform */ addr_low = NRF_FICR->DEVICEADDR[0]; addr_high = NRF_FICR->DEVICEADDR[1]; memcpy(addr->val, &addr_low, 4); memcpy(&addr->val[4], &addr_high, 2); addr->type = BLE_ADDR_PUBLIC; return 0; } /* Returns random static address or -1 if not present */ int ble_hw_get_static_addr(ble_addr_t *addr) { int rc; if ((NRF_FICR->DEVICEADDRTYPE & 1) == 1) { memcpy(addr->val, (void *)&NRF_FICR->DEVICEADDR[0], 4); memcpy(&addr->val[4], (void *)&NRF_FICR->DEVICEADDR[1], 2); addr->val[5] |= 0xc0; addr->type = BLE_ADDR_RANDOM; rc = 0; } else { rc = -1; } return rc; } /** * Clear the whitelist * * @return int */ void ble_hw_whitelist_clear(void) { NRF_RADIO->DACNF = 0; g_ble_hw_whitelist_mask = 0; } /** * Add a device to the hw whitelist * * @param addr * @param addr_type * * @return int 0: success, BLE error code otherwise */ int ble_hw_whitelist_add(const uint8_t *addr, uint8_t addr_type) { int i; uint32_t mask; /* Find first ununsed device address match element */ mask = 0x01; for (i = 0; i < BLE_HW_WHITE_LIST_SIZE; ++i) { if ((mask & g_ble_hw_whitelist_mask) == 0) { NRF_RADIO->DAB[i] = get_le32(addr); NRF_RADIO->DAP[i] = get_le16(addr + 4); if (addr_type == BLE_ADDR_RANDOM) { NRF_RADIO->DACNF |= (mask << 8); } g_ble_hw_whitelist_mask |= mask; return BLE_ERR_SUCCESS; } mask <<= 1; } return BLE_ERR_MEM_CAPACITY; } /** * Remove a device from the hw whitelist * * @param addr * @param addr_type * */ void ble_hw_whitelist_rmv(const uint8_t *addr, uint8_t addr_type) { int i; uint8_t cfg_addr; uint16_t dap; uint16_t txadd; uint32_t dab; uint32_t mask; /* Find first ununsed device address match element */ dab = get_le32(addr); dap = get_le16(addr + 4); txadd = NRF_RADIO->DACNF >> 8; mask = 0x01; for (i = 0; i < BLE_HW_WHITE_LIST_SIZE; ++i) { if (mask & g_ble_hw_whitelist_mask) { if ((dab == NRF_RADIO->DAB[i]) && (dap == NRF_RADIO->DAP[i])) { cfg_addr = txadd & mask; if (addr_type == BLE_ADDR_RANDOM) { if (cfg_addr != 0) { break; } } else { if (cfg_addr == 0) { break; } } } } mask <<= 1; } if (i < BLE_HW_WHITE_LIST_SIZE) { g_ble_hw_whitelist_mask &= ~mask; NRF_RADIO->DACNF &= ~mask; } } /** * Returns the size of the whitelist in HW * * @return int Number of devices allowed in whitelist */ uint8_t ble_hw_whitelist_size(void) { return BLE_HW_WHITE_LIST_SIZE; } /** * Enable the whitelisted devices */ void ble_hw_whitelist_enable(void) { /* Enable the configured device addresses */ NRF_RADIO->DACNF |= g_ble_hw_whitelist_mask; } /** * Disables the whitelisted devices */ void ble_hw_whitelist_disable(void) { /* Disable all whitelist devices */ NRF_RADIO->DACNF &= 0x0000ff00; } /** * Boolean function which returns true ('1') if there is a match on the * whitelist. * * @return int */ int ble_hw_whitelist_match(void) { return (int)NRF_RADIO->EVENTS_DEVMATCH; } /* Encrypt data */ int ble_hw_encrypt_block(struct ble_encryption_block *ecb) { int rc; uint32_t end; uint32_t err; /* Stop ECB */ NRF_ECB->TASKS_STOPECB = 1; /* XXX: does task stop clear these counters? Anyway to do this quicker? */ NRF_ECB->EVENTS_ENDECB = 0; NRF_ECB->EVENTS_ERRORECB = 0; NRF_ECB->ECBDATAPTR = (uint32_t)ecb; /* Start ECB */ NRF_ECB->TASKS_STARTECB = 1; /* Wait till error or done */ rc = 0; while (1) { end = NRF_ECB->EVENTS_ENDECB; err = NRF_ECB->EVENTS_ERRORECB; if (end || err) { if (err) { rc = -1; } break; } } return rc; } /** * Random number generator ISR. */ static void ble_rng_isr(void) { uint8_t rnum; os_trace_isr_enter(); /* No callback? Clear and disable interrupts */ if (g_ble_rng_isr_cb == NULL) { NRF_RNG->INTENCLR = 1; NRF_RNG->EVENTS_VALRDY = 0; (void)NRF_RNG->SHORTS; os_trace_isr_exit(); return; } /* If there is a value ready grab it */ if (NRF_RNG->EVENTS_VALRDY) { NRF_RNG->EVENTS_VALRDY = 0; rnum = (uint8_t)NRF_RNG->VALUE; (*g_ble_rng_isr_cb)(rnum); } os_trace_isr_exit(); } /** * Initialize the random number generator * * @param cb * @param bias * * @return int */ int ble_hw_rng_init(ble_rng_isr_cb_t cb, int bias) { /* Set bias */ if (bias) { NRF_RNG->CONFIG = 1; } else { NRF_RNG->CONFIG = 0; } /* If we were passed a function pointer we need to enable the interrupt */ if (cb != NULL) { #ifndef RIOT_VERSION NVIC_SetPriority(RNG_IRQn, (1 << __NVIC_PRIO_BITS) - 1); #endif #if MYNEWT NVIC_SetVector(RNG_IRQn, (uint32_t)ble_rng_isr); #else ble_npl_hw_set_isr(RNG_IRQn, ble_rng_isr); #endif NVIC_EnableIRQ(RNG_IRQn); g_ble_rng_isr_cb = cb; } return 0; } /** * Start the random number generator * * @return int */ int ble_hw_rng_start(void) { os_sr_t sr; /* No need for interrupt if there is no callback */ OS_ENTER_CRITICAL(sr); NRF_RNG->EVENTS_VALRDY = 0; if (g_ble_rng_isr_cb) { NRF_RNG->INTENSET = 1; } NRF_RNG->TASKS_START = 1; OS_EXIT_CRITICAL(sr); return 0; } /** * Stop the random generator * * @return int */ int ble_hw_rng_stop(void) { os_sr_t sr; /* No need for interrupt if there is no callback */ OS_ENTER_CRITICAL(sr); NRF_RNG->INTENCLR = 1; NRF_RNG->TASKS_STOP = 1; NRF_RNG->EVENTS_VALRDY = 0; OS_EXIT_CRITICAL(sr); return 0; } /** * Read the random number generator. * * @return uint8_t */ uint8_t ble_hw_rng_read(void) { uint8_t rnum; /* Wait for a sample */ while (NRF_RNG->EVENTS_VALRDY == 0) { } NRF_RNG->EVENTS_VALRDY = 0; rnum = (uint8_t)NRF_RNG->VALUE; return rnum; } #if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) /** * Clear the resolving list * * @return int */ void ble_hw_resolv_list_clear(void) { g_nrf_num_irks = 0; } /** * Add a device to the hw resolving list * * @param irk Pointer to IRK to add * * @return int 0: success, BLE error code otherwise */ int ble_hw_resolv_list_add(uint8_t *irk) { uint32_t *nrf_entry; /* Find first ununsed device address match element */ if (g_nrf_num_irks == NRF_IRK_LIST_ENTRIES) { return BLE_ERR_MEM_CAPACITY; } /* Copy into irk list */ nrf_entry = &g_nrf_irk_list[4 * g_nrf_num_irks]; memcpy(nrf_entry, irk, 16); /* Add to total */ ++g_nrf_num_irks; return BLE_ERR_SUCCESS; } /** * Remove a device from the hw resolving list * * @param index Index of IRK to remove */ void ble_hw_resolv_list_rmv(int index) { uint32_t *irk_entry; if (index < g_nrf_num_irks) { --g_nrf_num_irks; irk_entry = &g_nrf_irk_list[index]; if (g_nrf_num_irks > index) { memmove(irk_entry, irk_entry + 4, 16 * (g_nrf_num_irks - index)); } } } /** * Returns the size of the resolving list. NOTE: this returns the maximum * allowable entries in the HW. Configuration options may limit this. * * @return int Number of devices allowed in resolving list */ uint8_t ble_hw_resolv_list_size(void) { return BLE_HW_RESOLV_LIST_SIZE; } /** * Called to determine if the address received was resolved. * * @return int Negative values indicate unresolved address; positive values * indicate index in resolving list of resolved address. */ int ble_hw_resolv_list_match(void) { uint32_t index; if (NRF_AAR->EVENTS_END) { if (NRF_AAR->EVENTS_RESOLVED) { index = NRF_AAR->STATUS; return (int)index; } } return -1; } #endif |