library for BLE_GAP_backpack
Dependencies: nrf51-sdk
Fork of nRF51822 by
source/nRF5xn.cpp@402:448a564f5c9e, 2015-08-07 (annotated)
- Committer:
- rgrover1
- Date:
- Fri Aug 07 15:57:07 2015 +0100
- Revision:
- 402:448a564f5c9e
- Parent:
- 401:bd0f4eaad9f0
- Child:
- 403:92d6b4c7bf14
Synchronized with git rev b22c2e82
Author: Jean-Philippe Brucker
Select the clock source dynamically on SoftDevice initialisation
Instead of relying on build macros to know which oscillator we're
supposedly using, check the value of LFCLKSRC register. This way, we can
trust mbed's init code to fallback on the internal clock if the user
specified a wrong target.
This change, along with the mbed patch that selects clock source
dynamically, will allow lots of application to still run and provide
FOTA, when the wrong Nordic platform is specified.
Signed-off-by: Jean-Philippe Brucker <jean-philippe.brucker@arm.com>
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 | 388:db85a09c27ef | 19 | #include "nrf_soc.h" |
rgrover1 | 388:db85a09c27ef | 20 | |
rgrover1 | 388:db85a09c27ef | 21 | #include "btle/btle.h" |
rgrover1 | 388:db85a09c27ef | 22 | #include "nrf_delay.h" |
rgrover1 | 388:db85a09c27ef | 23 | |
rgrover1 | 388:db85a09c27ef | 24 | #include "softdevice_handler.h" |
rgrover1 | 388:db85a09c27ef | 25 | |
rgrover1 | 388:db85a09c27ef | 26 | /** |
rgrover1 | 388:db85a09c27ef | 27 | * The singleton which represents the nRF51822 transport for the BLE. |
rgrover1 | 388:db85a09c27ef | 28 | */ |
rgrover1 | 388:db85a09c27ef | 29 | static nRF5xn deviceInstance; |
rgrover1 | 388:db85a09c27ef | 30 | |
rgrover1 | 388:db85a09c27ef | 31 | /** |
rgrover1 | 388:db85a09c27ef | 32 | * BLE-API requires an implementation of the following function in order to |
rgrover1 | 388:db85a09c27ef | 33 | * obtain its transport handle. |
rgrover1 | 388:db85a09c27ef | 34 | */ |
rgrover1 | 388:db85a09c27ef | 35 | BLEInstanceBase * |
rgrover1 | 388:db85a09c27ef | 36 | createBLEInstance(void) |
rgrover1 | 388:db85a09c27ef | 37 | { |
rgrover1 | 388:db85a09c27ef | 38 | return (&deviceInstance); |
rgrover1 | 388:db85a09c27ef | 39 | } |
rgrover1 | 388:db85a09c27ef | 40 | |
rgrover1 | 388:db85a09c27ef | 41 | nRF5xn::nRF5xn(void) |
rgrover1 | 388:db85a09c27ef | 42 | { |
rgrover1 | 388:db85a09c27ef | 43 | } |
rgrover1 | 388:db85a09c27ef | 44 | |
rgrover1 | 388:db85a09c27ef | 45 | nRF5xn::~nRF5xn(void) |
rgrover1 | 388:db85a09c27ef | 46 | { |
rgrover1 | 388:db85a09c27ef | 47 | } |
rgrover1 | 388:db85a09c27ef | 48 | |
rgrover1 | 388:db85a09c27ef | 49 | const char *nRF5xn::getVersion(void) |
rgrover1 | 388:db85a09c27ef | 50 | { |
rgrover1 | 388:db85a09c27ef | 51 | static char versionString[32]; |
rgrover1 | 388:db85a09c27ef | 52 | static bool versionFetched = false; |
rgrover1 | 388:db85a09c27ef | 53 | |
rgrover1 | 388:db85a09c27ef | 54 | if (!versionFetched) { |
rgrover1 | 388:db85a09c27ef | 55 | ble_version_t version; |
rgrover1 | 388:db85a09c27ef | 56 | if ((sd_ble_version_get(&version) == NRF_SUCCESS) && (version.company_id == 0x0059)) { |
rgrover1 | 388:db85a09c27ef | 57 | switch (version.version_number) { |
rgrover1 | 388:db85a09c27ef | 58 | case 0x07: |
rgrover1 | 402:448a564f5c9e | 59 | snprintf(versionString, sizeof(versionString), "Nordic BLE4.1 fw:%04x", version.subversion_number); |
rgrover1 | 388:db85a09c27ef | 60 | break; |
rgrover1 | 388:db85a09c27ef | 61 | default: |
rgrover1 | 402:448a564f5c9e | 62 | snprintf(versionString, sizeof(versionString), "Nordic (spec unknown) fw:%04x", version.subversion_number); |
rgrover1 | 388:db85a09c27ef | 63 | break; |
rgrover1 | 388:db85a09c27ef | 64 | } |
rgrover1 | 388:db85a09c27ef | 65 | versionFetched = true; |
rgrover1 | 388:db85a09c27ef | 66 | } else { |
rgrover1 | 388:db85a09c27ef | 67 | strncpy(versionString, "unknown", sizeof(versionString)); |
rgrover1 | 388:db85a09c27ef | 68 | } |
rgrover1 | 388:db85a09c27ef | 69 | } |
rgrover1 | 388:db85a09c27ef | 70 | |
rgrover1 | 388:db85a09c27ef | 71 | return versionString; |
rgrover1 | 388:db85a09c27ef | 72 | } |
rgrover1 | 388:db85a09c27ef | 73 | |
rgrover1 | 388:db85a09c27ef | 74 | ble_error_t nRF5xn::init(void) |
rgrover1 | 388:db85a09c27ef | 75 | { |
rgrover1 | 388:db85a09c27ef | 76 | /* ToDo: Clear memory contents, reset the SD, etc. */ |
rgrover1 | 388:db85a09c27ef | 77 | btle_init(); |
rgrover1 | 388:db85a09c27ef | 78 | |
rgrover1 | 388:db85a09c27ef | 79 | return BLE_ERROR_NONE; |
rgrover1 | 388:db85a09c27ef | 80 | } |
rgrover1 | 388:db85a09c27ef | 81 | |
rgrover1 | 388:db85a09c27ef | 82 | ble_error_t nRF5xn::shutdown(void) |
rgrover1 | 388:db85a09c27ef | 83 | { |
rgrover1 | 388:db85a09c27ef | 84 | return (softdevice_handler_sd_disable() == NRF_SUCCESS) ? BLE_ERROR_NONE : BLE_STACK_BUSY; |
rgrover1 | 388:db85a09c27ef | 85 | } |
rgrover1 | 388:db85a09c27ef | 86 | |
rgrover1 | 388:db85a09c27ef | 87 | void |
rgrover1 | 388:db85a09c27ef | 88 | nRF5xn::waitForEvent(void) |
rgrover1 | 388:db85a09c27ef | 89 | { |
rgrover1 | 388:db85a09c27ef | 90 | sd_app_evt_wait(); |
rgrover1 | 388:db85a09c27ef | 91 | } |