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