No changes
Fork of nRF51822 by
source/nRF5xn.cpp@493:46fbcb7eab5b, 2015-12-02 (annotated)
- Committer:
- rgrover1
- Date:
- Wed Dec 02 10:31:32 2015 +0000
- Revision:
- 493:46fbcb7eab5b
- Parent:
- 482:0efda47cc288
- Child:
- 494:ed0e3d0353e2
Synchronized with git rev 348c323d
Author: Liyou Zhou
Take modification out of the nordic sdk files
and put them into ARM written files.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rgrover1 | 388:db85a09c27ef | 1 | /* mbed Microcontroller Library |
rgrover1 | 388:db85a09c27ef | 2 | * Copyright (c) 2006-2013 ARM Limited |
rgrover1 | 388:db85a09c27ef | 3 | * |
rgrover1 | 388:db85a09c27ef | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
rgrover1 | 388:db85a09c27ef | 5 | * you may not use this file except in compliance with the License. |
rgrover1 | 388:db85a09c27ef | 6 | * You may obtain a copy of the License at |
rgrover1 | 388:db85a09c27ef | 7 | * |
rgrover1 | 388:db85a09c27ef | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
rgrover1 | 388:db85a09c27ef | 9 | * |
rgrover1 | 388:db85a09c27ef | 10 | * Unless required by applicable law or agreed to in writing, software |
rgrover1 | 388:db85a09c27ef | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
rgrover1 | 388:db85a09c27ef | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
rgrover1 | 388:db85a09c27ef | 13 | * See the License for the specific language governing permissions and |
rgrover1 | 388:db85a09c27ef | 14 | * limitations under the License. |
rgrover1 | 388:db85a09c27ef | 15 | */ |
rgrover1 | 388:db85a09c27ef | 16 | |
rgrover1 | 388:db85a09c27ef | 17 | #include "mbed.h" |
rgrover1 | 388:db85a09c27ef | 18 | #include "nRF5xn.h" |
rgrover1 | 459:98c44deafc70 | 19 | #include "ble/blecommon.h" |
rgrover1 | 388:db85a09c27ef | 20 | #include "nrf_soc.h" |
rgrover1 | 388:db85a09c27ef | 21 | |
rgrover1 | 388:db85a09c27ef | 22 | #include "btle/btle.h" |
rgrover1 | 493:46fbcb7eab5b | 23 | |
rgrover1 | 493:46fbcb7eab5b | 24 | #define asm __ASM |
rgrover1 | 388:db85a09c27ef | 25 | #include "nrf_delay.h" |
rgrover1 | 388:db85a09c27ef | 26 | |
rgrover1 | 493:46fbcb7eab5b | 27 | extern "C" { |
rgrover1 | 388:db85a09c27ef | 28 | #include "softdevice_handler.h" |
rgrover1 | 493:46fbcb7eab5b | 29 | } |
rgrover1 | 388:db85a09c27ef | 30 | |
rgrover1 | 388:db85a09c27ef | 31 | /** |
rgrover1 | 388:db85a09c27ef | 32 | * The singleton which represents the nRF51822 transport for the BLE. |
rgrover1 | 388:db85a09c27ef | 33 | */ |
rgrover1 | 388:db85a09c27ef | 34 | static nRF5xn deviceInstance; |
rgrover1 | 388:db85a09c27ef | 35 | |
rgrover1 | 388:db85a09c27ef | 36 | /** |
rgrover1 | 388:db85a09c27ef | 37 | * BLE-API requires an implementation of the following function in order to |
rgrover1 | 388:db85a09c27ef | 38 | * obtain its transport handle. |
rgrover1 | 388:db85a09c27ef | 39 | */ |
rgrover1 | 388:db85a09c27ef | 40 | BLEInstanceBase * |
rgrover1 | 388:db85a09c27ef | 41 | createBLEInstance(void) |
rgrover1 | 388:db85a09c27ef | 42 | { |
rgrover1 | 388:db85a09c27ef | 43 | return (&deviceInstance); |
rgrover1 | 388:db85a09c27ef | 44 | } |
rgrover1 | 388:db85a09c27ef | 45 | |
rgrover1 | 458:b42881afcc6e | 46 | nRF5xn::nRF5xn(void) : initialized(false), instanceID(BLE::DEFAULT_INSTANCE) |
rgrover1 | 388:db85a09c27ef | 47 | { |
rgrover1 | 388:db85a09c27ef | 48 | } |
rgrover1 | 388:db85a09c27ef | 49 | |
rgrover1 | 388:db85a09c27ef | 50 | nRF5xn::~nRF5xn(void) |
rgrover1 | 388:db85a09c27ef | 51 | { |
rgrover1 | 388:db85a09c27ef | 52 | } |
rgrover1 | 388:db85a09c27ef | 53 | |
rgrover1 | 388:db85a09c27ef | 54 | const char *nRF5xn::getVersion(void) |
rgrover1 | 388:db85a09c27ef | 55 | { |
rgrover1 | 459:98c44deafc70 | 56 | if (!initialized) { |
rgrover1 | 459:98c44deafc70 | 57 | return "INITIALIZATION_INCOMPLETE"; |
rgrover1 | 459:98c44deafc70 | 58 | } |
rgrover1 | 459:98c44deafc70 | 59 | |
rgrover1 | 388:db85a09c27ef | 60 | static char versionString[32]; |
rgrover1 | 388:db85a09c27ef | 61 | static bool versionFetched = false; |
rgrover1 | 388:db85a09c27ef | 62 | |
rgrover1 | 388:db85a09c27ef | 63 | if (!versionFetched) { |
rgrover1 | 388:db85a09c27ef | 64 | ble_version_t version; |
rgrover1 | 388:db85a09c27ef | 65 | if ((sd_ble_version_get(&version) == NRF_SUCCESS) && (version.company_id == 0x0059)) { |
rgrover1 | 388:db85a09c27ef | 66 | switch (version.version_number) { |
rgrover1 | 388:db85a09c27ef | 67 | case 0x07: |
rgrover1 | 403:92d6b4c7bf14 | 68 | case 0x08: |
rgrover1 | 403:92d6b4c7bf14 | 69 | snprintf(versionString, sizeof(versionString), "Nordic BLE4.1 ver:%u fw:%04x", version.version_number, version.subversion_number); |
rgrover1 | 388:db85a09c27ef | 70 | break; |
rgrover1 | 388:db85a09c27ef | 71 | default: |
rgrover1 | 403:92d6b4c7bf14 | 72 | snprintf(versionString, sizeof(versionString), "Nordic (spec unknown) ver:%u fw:%04x", version.version_number, version.subversion_number); |
rgrover1 | 388:db85a09c27ef | 73 | break; |
rgrover1 | 388:db85a09c27ef | 74 | } |
rgrover1 | 388:db85a09c27ef | 75 | versionFetched = true; |
rgrover1 | 388:db85a09c27ef | 76 | } else { |
rgrover1 | 388:db85a09c27ef | 77 | strncpy(versionString, "unknown", sizeof(versionString)); |
rgrover1 | 388:db85a09c27ef | 78 | } |
rgrover1 | 388:db85a09c27ef | 79 | } |
rgrover1 | 388:db85a09c27ef | 80 | |
rgrover1 | 388:db85a09c27ef | 81 | return versionString; |
rgrover1 | 388:db85a09c27ef | 82 | } |
rgrover1 | 388:db85a09c27ef | 83 | |
rgrover1 | 463:b869f947a37a | 84 | ble_error_t nRF5xn::init(BLE::InstanceID_t instanceID, FunctionPointerWithContext<BLE::InitializationCompleteCallbackContext *> callback) |
rgrover1 | 388:db85a09c27ef | 85 | { |
rgrover1 | 458:b42881afcc6e | 86 | if (initialized) { |
rgrover1 | 463:b869f947a37a | 87 | BLE::InitializationCompleteCallbackContext context = { |
rgrover1 | 463:b869f947a37a | 88 | BLE::Instance(instanceID), |
rgrover1 | 463:b869f947a37a | 89 | BLE_ERROR_ALREADY_INITIALIZED |
rgrover1 | 463:b869f947a37a | 90 | }; |
rgrover1 | 463:b869f947a37a | 91 | callback.call(&context); |
rgrover1 | 458:b42881afcc6e | 92 | return BLE_ERROR_ALREADY_INITIALIZED; |
rgrover1 | 458:b42881afcc6e | 93 | } |
rgrover1 | 458:b42881afcc6e | 94 | |
rgrover1 | 458:b42881afcc6e | 95 | instanceID = instanceID; |
rgrover1 | 458:b42881afcc6e | 96 | |
rgrover1 | 388:db85a09c27ef | 97 | /* ToDo: Clear memory contents, reset the SD, etc. */ |
rgrover1 | 388:db85a09c27ef | 98 | btle_init(); |
rgrover1 | 388:db85a09c27ef | 99 | |
rgrover1 | 458:b42881afcc6e | 100 | initialized = true; |
rgrover1 | 463:b869f947a37a | 101 | BLE::InitializationCompleteCallbackContext context = { |
rgrover1 | 463:b869f947a37a | 102 | BLE::Instance(instanceID), |
rgrover1 | 463:b869f947a37a | 103 | BLE_ERROR_NONE |
rgrover1 | 463:b869f947a37a | 104 | }; |
rgrover1 | 463:b869f947a37a | 105 | callback.call(&context); |
rgrover1 | 388:db85a09c27ef | 106 | return BLE_ERROR_NONE; |
rgrover1 | 388:db85a09c27ef | 107 | } |
rgrover1 | 388:db85a09c27ef | 108 | |
rgrover1 | 388:db85a09c27ef | 109 | ble_error_t nRF5xn::shutdown(void) |
rgrover1 | 388:db85a09c27ef | 110 | { |
rgrover1 | 459:98c44deafc70 | 111 | if (!initialized) { |
rgrover1 | 459:98c44deafc70 | 112 | return BLE_ERROR_INITIALIZATION_INCOMPLETE; |
rgrover1 | 459:98c44deafc70 | 113 | } |
rgrover1 | 459:98c44deafc70 | 114 | |
rgrover1 | 461:1f352d938a33 | 115 | if(softdevice_handler_sd_disable() != NRF_SUCCESS) { |
rgrover1 | 461:1f352d938a33 | 116 | return BLE_STACK_BUSY; |
rgrover1 | 461:1f352d938a33 | 117 | } |
rgrover1 | 461:1f352d938a33 | 118 | |
rgrover1 | 461:1f352d938a33 | 119 | initialized = false; |
rgrover1 | 461:1f352d938a33 | 120 | return BLE_ERROR_NONE; |
rgrover1 | 388:db85a09c27ef | 121 | } |
rgrover1 | 388:db85a09c27ef | 122 | |
rgrover1 | 388:db85a09c27ef | 123 | void |
rgrover1 | 388:db85a09c27ef | 124 | nRF5xn::waitForEvent(void) |
rgrover1 | 388:db85a09c27ef | 125 | { |
rgrover1 | 388:db85a09c27ef | 126 | sd_app_evt_wait(); |
rgrover1 | 388:db85a09c27ef | 127 | } |