CSSE4011_BLE_IMU IMU Seeed Tiny Ble
Dependencies: BLE_API_Tiny_BLE MPU6050-DMP-Seeed-Tiny-BLE mbed
nRF51822/nRF51822n.cpp@0:f90c3452d779, 2015-06-10 (annotated)
- Committer:
- flywind
- Date:
- Wed Jun 10 09:46:16 2015 +0000
- Revision:
- 0:f90c3452d779
CSSE4011_Seed-Tiny-BLE
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
flywind | 0:f90c3452d779 | 1 | /* mbed Microcontroller Library |
flywind | 0:f90c3452d779 | 2 | * Copyright (c) 2006-2013 ARM Limited |
flywind | 0:f90c3452d779 | 3 | * |
flywind | 0:f90c3452d779 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
flywind | 0:f90c3452d779 | 5 | * you may not use this file except in compliance with the License. |
flywind | 0:f90c3452d779 | 6 | * You may obtain a copy of the License at |
flywind | 0:f90c3452d779 | 7 | * |
flywind | 0:f90c3452d779 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
flywind | 0:f90c3452d779 | 9 | * |
flywind | 0:f90c3452d779 | 10 | * Unless required by applicable law or agreed to in writing, software |
flywind | 0:f90c3452d779 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
flywind | 0:f90c3452d779 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
flywind | 0:f90c3452d779 | 13 | * See the License for the specific language governing permissions and |
flywind | 0:f90c3452d779 | 14 | * limitations under the License. |
flywind | 0:f90c3452d779 | 15 | */ |
flywind | 0:f90c3452d779 | 16 | |
flywind | 0:f90c3452d779 | 17 | #include "mbed.h" |
flywind | 0:f90c3452d779 | 18 | #include "nRF51822n.h" |
flywind | 0:f90c3452d779 | 19 | #include "nrf_soc.h" |
flywind | 0:f90c3452d779 | 20 | |
flywind | 0:f90c3452d779 | 21 | #include "btle/btle.h" |
flywind | 0:f90c3452d779 | 22 | #include "nrf_delay.h" |
flywind | 0:f90c3452d779 | 23 | |
flywind | 0:f90c3452d779 | 24 | #include "softdevice_handler.h" |
flywind | 0:f90c3452d779 | 25 | |
flywind | 0:f90c3452d779 | 26 | /** |
flywind | 0:f90c3452d779 | 27 | * The singleton which represents the nRF51822 transport for the BLEDevice. |
flywind | 0:f90c3452d779 | 28 | */ |
flywind | 0:f90c3452d779 | 29 | static nRF51822n deviceInstance; |
flywind | 0:f90c3452d779 | 30 | |
flywind | 0:f90c3452d779 | 31 | /** |
flywind | 0:f90c3452d779 | 32 | * BLE-API requires an implementation of the following function in order to |
flywind | 0:f90c3452d779 | 33 | * obtain its transport handle. |
flywind | 0:f90c3452d779 | 34 | */ |
flywind | 0:f90c3452d779 | 35 | BLEDeviceInstanceBase * |
flywind | 0:f90c3452d779 | 36 | createBLEDeviceInstance(void) |
flywind | 0:f90c3452d779 | 37 | { |
flywind | 0:f90c3452d779 | 38 | return (&deviceInstance); |
flywind | 0:f90c3452d779 | 39 | } |
flywind | 0:f90c3452d779 | 40 | |
flywind | 0:f90c3452d779 | 41 | nRF51822n::nRF51822n(void) |
flywind | 0:f90c3452d779 | 42 | { |
flywind | 0:f90c3452d779 | 43 | } |
flywind | 0:f90c3452d779 | 44 | |
flywind | 0:f90c3452d779 | 45 | nRF51822n::~nRF51822n(void) |
flywind | 0:f90c3452d779 | 46 | { |
flywind | 0:f90c3452d779 | 47 | } |
flywind | 0:f90c3452d779 | 48 | |
flywind | 0:f90c3452d779 | 49 | const char *nRF51822n::getVersion(void) |
flywind | 0:f90c3452d779 | 50 | { |
flywind | 0:f90c3452d779 | 51 | static char versionString[10]; |
flywind | 0:f90c3452d779 | 52 | static bool versionFetched = false; |
flywind | 0:f90c3452d779 | 53 | |
flywind | 0:f90c3452d779 | 54 | if (!versionFetched) { |
flywind | 0:f90c3452d779 | 55 | ble_version_t version; |
flywind | 0:f90c3452d779 | 56 | if (sd_ble_version_get(&version) == NRF_SUCCESS) { |
flywind | 0:f90c3452d779 | 57 | snprintf(versionString, sizeof(versionString), "%u.%u", version.version_number, version.subversion_number); |
flywind | 0:f90c3452d779 | 58 | versionFetched = true; |
flywind | 0:f90c3452d779 | 59 | } else { |
flywind | 0:f90c3452d779 | 60 | strncpy(versionString, "unknown", sizeof(versionString)); |
flywind | 0:f90c3452d779 | 61 | } |
flywind | 0:f90c3452d779 | 62 | } |
flywind | 0:f90c3452d779 | 63 | |
flywind | 0:f90c3452d779 | 64 | return versionString; |
flywind | 0:f90c3452d779 | 65 | } |
flywind | 0:f90c3452d779 | 66 | |
flywind | 0:f90c3452d779 | 67 | /* (Valid values are -40, -20, -16, -12, -8, -4, 0, 4) */ |
flywind | 0:f90c3452d779 | 68 | ble_error_t nRF51822n::setTxPower(int8_t txPower) |
flywind | 0:f90c3452d779 | 69 | { |
flywind | 0:f90c3452d779 | 70 | unsigned rc; |
flywind | 0:f90c3452d779 | 71 | if ((rc = sd_ble_gap_tx_power_set(txPower)) != NRF_SUCCESS) { |
flywind | 0:f90c3452d779 | 72 | switch (rc) { |
flywind | 0:f90c3452d779 | 73 | case NRF_ERROR_BUSY: |
flywind | 0:f90c3452d779 | 74 | return BLE_STACK_BUSY; |
flywind | 0:f90c3452d779 | 75 | case NRF_ERROR_INVALID_PARAM: |
flywind | 0:f90c3452d779 | 76 | default: |
flywind | 0:f90c3452d779 | 77 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
flywind | 0:f90c3452d779 | 78 | } |
flywind | 0:f90c3452d779 | 79 | } |
flywind | 0:f90c3452d779 | 80 | |
flywind | 0:f90c3452d779 | 81 | return BLE_ERROR_NONE; |
flywind | 0:f90c3452d779 | 82 | } |
flywind | 0:f90c3452d779 | 83 | |
flywind | 0:f90c3452d779 | 84 | void nRF51822n::getPermittedTxPowerValues(const int8_t **valueArrayPP, size_t *countP) |
flywind | 0:f90c3452d779 | 85 | { |
flywind | 0:f90c3452d779 | 86 | static const int8_t permittedTxValues[] = { |
flywind | 0:f90c3452d779 | 87 | -40, -30, -20, -16, -12, -8, -4, 0, 4 |
flywind | 0:f90c3452d779 | 88 | }; |
flywind | 0:f90c3452d779 | 89 | |
flywind | 0:f90c3452d779 | 90 | *valueArrayPP = permittedTxValues; |
flywind | 0:f90c3452d779 | 91 | *countP = sizeof(permittedTxValues) / sizeof(int8_t); |
flywind | 0:f90c3452d779 | 92 | } |
flywind | 0:f90c3452d779 | 93 | |
flywind | 0:f90c3452d779 | 94 | ble_error_t nRF51822n::init(void) |
flywind | 0:f90c3452d779 | 95 | { |
flywind | 0:f90c3452d779 | 96 | /* ToDo: Clear memory contents, reset the SD, etc. */ |
flywind | 0:f90c3452d779 | 97 | btle_init(); |
flywind | 0:f90c3452d779 | 98 | |
flywind | 0:f90c3452d779 | 99 | reset(); |
flywind | 0:f90c3452d779 | 100 | |
flywind | 0:f90c3452d779 | 101 | return BLE_ERROR_NONE; |
flywind | 0:f90c3452d779 | 102 | } |
flywind | 0:f90c3452d779 | 103 | |
flywind | 0:f90c3452d779 | 104 | ble_error_t nRF51822n::shutdown(void) |
flywind | 0:f90c3452d779 | 105 | { |
flywind | 0:f90c3452d779 | 106 | return (softdevice_handler_sd_disable() == NRF_SUCCESS) ? BLE_ERROR_NONE : BLE_STACK_BUSY; |
flywind | 0:f90c3452d779 | 107 | } |
flywind | 0:f90c3452d779 | 108 | |
flywind | 0:f90c3452d779 | 109 | ble_error_t nRF51822n::reset(void) |
flywind | 0:f90c3452d779 | 110 | { |
flywind | 0:f90c3452d779 | 111 | nrf_delay_us(500000); |
flywind | 0:f90c3452d779 | 112 | |
flywind | 0:f90c3452d779 | 113 | /* Wait for the radio to come back up */ |
flywind | 0:f90c3452d779 | 114 | nrf_delay_us(1000000); |
flywind | 0:f90c3452d779 | 115 | |
flywind | 0:f90c3452d779 | 116 | return BLE_ERROR_NONE; |
flywind | 0:f90c3452d779 | 117 | } |
flywind | 0:f90c3452d779 | 118 | |
flywind | 0:f90c3452d779 | 119 | void |
flywind | 0:f90c3452d779 | 120 | nRF51822n::waitForEvent(void) |
flywind | 0:f90c3452d779 | 121 | { |
flywind | 0:f90c3452d779 | 122 | sd_app_evt_wait(); |
flywind | 0:f90c3452d779 | 123 | } |