テスト用です。

Dependencies:   mbed

Committer:
jksoft
Date:
Tue Oct 11 11:09:42 2016 +0000
Revision:
0:8468a4403fea
SB??ver;

Who changed what in which revision?

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