Updated to latest online libraries (See also mbed Apps: HelloBlue, FOTA4)
Dependencies: BLE_API mbed nRF51822
Fork of BLE_Default_APP by
nRF51822/nRF51822n.cpp@1:a607cd9655d7, 2014-10-10 (annotated)
- Committer:
- yihui
- Date:
- Fri Oct 10 03:36:28 2014 +0000
- Revision:
- 1:a607cd9655d7
use NVIC_SystemReset() to run bootloader
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yihui | 1:a607cd9655d7 | 1 | /* mbed Microcontroller Library |
yihui | 1:a607cd9655d7 | 2 | * Copyright (c) 2006-2013 ARM Limited |
yihui | 1:a607cd9655d7 | 3 | * |
yihui | 1:a607cd9655d7 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
yihui | 1:a607cd9655d7 | 5 | * you may not use this file except in compliance with the License. |
yihui | 1:a607cd9655d7 | 6 | * You may obtain a copy of the License at |
yihui | 1:a607cd9655d7 | 7 | * |
yihui | 1:a607cd9655d7 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
yihui | 1:a607cd9655d7 | 9 | * |
yihui | 1:a607cd9655d7 | 10 | * Unless required by applicable law or agreed to in writing, software |
yihui | 1:a607cd9655d7 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
yihui | 1:a607cd9655d7 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
yihui | 1:a607cd9655d7 | 13 | * See the License for the specific language governing permissions and |
yihui | 1:a607cd9655d7 | 14 | * limitations under the License. |
yihui | 1:a607cd9655d7 | 15 | */ |
yihui | 1:a607cd9655d7 | 16 | |
yihui | 1:a607cd9655d7 | 17 | #include "mbed.h" |
yihui | 1:a607cd9655d7 | 18 | #include "nRF51822n.h" |
yihui | 1:a607cd9655d7 | 19 | #include "nrf_soc.h" |
yihui | 1:a607cd9655d7 | 20 | |
yihui | 1:a607cd9655d7 | 21 | #include "btle/btle.h" |
yihui | 1:a607cd9655d7 | 22 | #include "nrf_delay.h" |
yihui | 1:a607cd9655d7 | 23 | |
yihui | 1:a607cd9655d7 | 24 | /** |
yihui | 1:a607cd9655d7 | 25 | * The singleton which represents the nRF51822 transport for the BLEDevice. |
yihui | 1:a607cd9655d7 | 26 | */ |
yihui | 1:a607cd9655d7 | 27 | static nRF51822n deviceInstance; |
yihui | 1:a607cd9655d7 | 28 | |
yihui | 1:a607cd9655d7 | 29 | /** |
yihui | 1:a607cd9655d7 | 30 | * BLE-API requires an implementation of the following function in order to |
yihui | 1:a607cd9655d7 | 31 | * obtain its transport handle. |
yihui | 1:a607cd9655d7 | 32 | */ |
yihui | 1:a607cd9655d7 | 33 | BLEDeviceInstanceBase * |
yihui | 1:a607cd9655d7 | 34 | createBLEDeviceInstance(void) |
yihui | 1:a607cd9655d7 | 35 | { |
yihui | 1:a607cd9655d7 | 36 | return (&deviceInstance); |
yihui | 1:a607cd9655d7 | 37 | } |
yihui | 1:a607cd9655d7 | 38 | |
yihui | 1:a607cd9655d7 | 39 | /**************************************************************************/ |
yihui | 1:a607cd9655d7 | 40 | /*! |
yihui | 1:a607cd9655d7 | 41 | @brief Constructor |
yihui | 1:a607cd9655d7 | 42 | */ |
yihui | 1:a607cd9655d7 | 43 | /**************************************************************************/ |
yihui | 1:a607cd9655d7 | 44 | nRF51822n::nRF51822n(void) |
yihui | 1:a607cd9655d7 | 45 | { |
yihui | 1:a607cd9655d7 | 46 | } |
yihui | 1:a607cd9655d7 | 47 | |
yihui | 1:a607cd9655d7 | 48 | /**************************************************************************/ |
yihui | 1:a607cd9655d7 | 49 | /*! |
yihui | 1:a607cd9655d7 | 50 | @brief Destructor |
yihui | 1:a607cd9655d7 | 51 | */ |
yihui | 1:a607cd9655d7 | 52 | /**************************************************************************/ |
yihui | 1:a607cd9655d7 | 53 | nRF51822n::~nRF51822n(void) |
yihui | 1:a607cd9655d7 | 54 | { |
yihui | 1:a607cd9655d7 | 55 | } |
yihui | 1:a607cd9655d7 | 56 | |
yihui | 1:a607cd9655d7 | 57 | const char *nRF51822n::getVersion(void) |
yihui | 1:a607cd9655d7 | 58 | { |
yihui | 1:a607cd9655d7 | 59 | static char versionString[10]; |
yihui | 1:a607cd9655d7 | 60 | static bool versionFetched = false; |
yihui | 1:a607cd9655d7 | 61 | |
yihui | 1:a607cd9655d7 | 62 | if (!versionFetched) { |
yihui | 1:a607cd9655d7 | 63 | ble_version_t version; |
yihui | 1:a607cd9655d7 | 64 | if (sd_ble_version_get(&version) == NRF_SUCCESS) { |
yihui | 1:a607cd9655d7 | 65 | snprintf(versionString, sizeof(versionString), "%u.%u", version.version_number, version.subversion_number); |
yihui | 1:a607cd9655d7 | 66 | versionFetched = true; |
yihui | 1:a607cd9655d7 | 67 | } else { |
yihui | 1:a607cd9655d7 | 68 | strncpy(versionString, "unknown", sizeof(versionString)); |
yihui | 1:a607cd9655d7 | 69 | } |
yihui | 1:a607cd9655d7 | 70 | } |
yihui | 1:a607cd9655d7 | 71 | |
yihui | 1:a607cd9655d7 | 72 | return versionString; |
yihui | 1:a607cd9655d7 | 73 | } |
yihui | 1:a607cd9655d7 | 74 | |
yihui | 1:a607cd9655d7 | 75 | /* (Valid values are -40, -20, -16, -12, -8, -4, 0, 4) */ |
yihui | 1:a607cd9655d7 | 76 | ble_error_t nRF51822n::setTxPower(int8_t txPower) |
yihui | 1:a607cd9655d7 | 77 | { |
yihui | 1:a607cd9655d7 | 78 | unsigned rc; |
yihui | 1:a607cd9655d7 | 79 | if ((rc = sd_ble_gap_tx_power_set(txPower)) != NRF_SUCCESS) { |
yihui | 1:a607cd9655d7 | 80 | switch (rc) { |
yihui | 1:a607cd9655d7 | 81 | case NRF_ERROR_BUSY: |
yihui | 1:a607cd9655d7 | 82 | return BLE_STACK_BUSY; |
yihui | 1:a607cd9655d7 | 83 | case NRF_ERROR_INVALID_PARAM: |
yihui | 1:a607cd9655d7 | 84 | default: |
yihui | 1:a607cd9655d7 | 85 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
yihui | 1:a607cd9655d7 | 86 | } |
yihui | 1:a607cd9655d7 | 87 | } |
yihui | 1:a607cd9655d7 | 88 | |
yihui | 1:a607cd9655d7 | 89 | return BLE_ERROR_NONE; |
yihui | 1:a607cd9655d7 | 90 | } |
yihui | 1:a607cd9655d7 | 91 | |
yihui | 1:a607cd9655d7 | 92 | /**************************************************************************/ |
yihui | 1:a607cd9655d7 | 93 | /*! |
yihui | 1:a607cd9655d7 | 94 | @brief Initialises anything required to start using BLE |
yihui | 1:a607cd9655d7 | 95 | |
yihui | 1:a607cd9655d7 | 96 | @returns ble_error_t |
yihui | 1:a607cd9655d7 | 97 | |
yihui | 1:a607cd9655d7 | 98 | @retval BLE_ERROR_NONE |
yihui | 1:a607cd9655d7 | 99 | Everything executed properly |
yihui | 1:a607cd9655d7 | 100 | |
yihui | 1:a607cd9655d7 | 101 | @section EXAMPLE |
yihui | 1:a607cd9655d7 | 102 | |
yihui | 1:a607cd9655d7 | 103 | @code |
yihui | 1:a607cd9655d7 | 104 | |
yihui | 1:a607cd9655d7 | 105 | @endcode |
yihui | 1:a607cd9655d7 | 106 | */ |
yihui | 1:a607cd9655d7 | 107 | /**************************************************************************/ |
yihui | 1:a607cd9655d7 | 108 | ble_error_t nRF51822n::init(void) |
yihui | 1:a607cd9655d7 | 109 | { |
yihui | 1:a607cd9655d7 | 110 | /* ToDo: Clear memory contents, reset the SD, etc. */ |
yihui | 1:a607cd9655d7 | 111 | btle_init(); |
yihui | 1:a607cd9655d7 | 112 | |
yihui | 1:a607cd9655d7 | 113 | reset(); |
yihui | 1:a607cd9655d7 | 114 | |
yihui | 1:a607cd9655d7 | 115 | return BLE_ERROR_NONE; |
yihui | 1:a607cd9655d7 | 116 | } |
yihui | 1:a607cd9655d7 | 117 | |
yihui | 1:a607cd9655d7 | 118 | /**************************************************************************/ |
yihui | 1:a607cd9655d7 | 119 | /*! |
yihui | 1:a607cd9655d7 | 120 | @brief Resets the BLE HW, removing any existing services and |
yihui | 1:a607cd9655d7 | 121 | characteristics |
yihui | 1:a607cd9655d7 | 122 | |
yihui | 1:a607cd9655d7 | 123 | @returns ble_error_t |
yihui | 1:a607cd9655d7 | 124 | |
yihui | 1:a607cd9655d7 | 125 | @retval BLE_ERROR_NONE |
yihui | 1:a607cd9655d7 | 126 | Everything executed properly |
yihui | 1:a607cd9655d7 | 127 | |
yihui | 1:a607cd9655d7 | 128 | @section EXAMPLE |
yihui | 1:a607cd9655d7 | 129 | |
yihui | 1:a607cd9655d7 | 130 | @code |
yihui | 1:a607cd9655d7 | 131 | |
yihui | 1:a607cd9655d7 | 132 | @endcode |
yihui | 1:a607cd9655d7 | 133 | */ |
yihui | 1:a607cd9655d7 | 134 | /**************************************************************************/ |
yihui | 1:a607cd9655d7 | 135 | ble_error_t nRF51822n::reset(void) |
yihui | 1:a607cd9655d7 | 136 | { |
yihui | 1:a607cd9655d7 | 137 | nrf_delay_us(500000); |
yihui | 1:a607cd9655d7 | 138 | |
yihui | 1:a607cd9655d7 | 139 | /* Wait for the radio to come back up */ |
yihui | 1:a607cd9655d7 | 140 | nrf_delay_us(1000000); |
yihui | 1:a607cd9655d7 | 141 | |
yihui | 1:a607cd9655d7 | 142 | return BLE_ERROR_NONE; |
yihui | 1:a607cd9655d7 | 143 | } |
yihui | 1:a607cd9655d7 | 144 | |
yihui | 1:a607cd9655d7 | 145 | void |
yihui | 1:a607cd9655d7 | 146 | nRF51822n::waitForEvent(void) |
yihui | 1:a607cd9655d7 | 147 | { |
yihui | 1:a607cd9655d7 | 148 | sd_app_evt_wait(); |
yihui | 1:a607cd9655d7 | 149 | } |