mamont mamont
/
test_ble
nrf52-dk
shields/TARGET_ST_BLUENRG/source/BlueNRGDevice.cpp@0:70ce6da7a141, 2020-06-05 (annotated)
- Committer:
- mamont090671
- Date:
- Fri Jun 05 10:49:10 2020 +0000
- Revision:
- 0:70ce6da7a141
nrf52840
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mamont090671 | 0:70ce6da7a141 | 1 | /* mbed Microcontroller Library |
mamont090671 | 0:70ce6da7a141 | 2 | * Copyright (c) 2006-2013 ARM Limited |
mamont090671 | 0:70ce6da7a141 | 3 | * |
mamont090671 | 0:70ce6da7a141 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mamont090671 | 0:70ce6da7a141 | 5 | * you may not use this file except in compliance with the License. |
mamont090671 | 0:70ce6da7a141 | 6 | * You may obtain a copy of the License at |
mamont090671 | 0:70ce6da7a141 | 7 | * |
mamont090671 | 0:70ce6da7a141 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mamont090671 | 0:70ce6da7a141 | 9 | * |
mamont090671 | 0:70ce6da7a141 | 10 | * Unless required by applicable law or agreed to in writing, software |
mamont090671 | 0:70ce6da7a141 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mamont090671 | 0:70ce6da7a141 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mamont090671 | 0:70ce6da7a141 | 13 | * See the License for the specific language governing permissions and |
mamont090671 | 0:70ce6da7a141 | 14 | * limitations under the License. |
mamont090671 | 0:70ce6da7a141 | 15 | */ |
mamont090671 | 0:70ce6da7a141 | 16 | |
mamont090671 | 0:70ce6da7a141 | 17 | /** |
mamont090671 | 0:70ce6da7a141 | 18 | ****************************************************************************** |
mamont090671 | 0:70ce6da7a141 | 19 | * @file BlueNRGDevice.cpp |
mamont090671 | 0:70ce6da7a141 | 20 | * @author STMicroelectronics |
mamont090671 | 0:70ce6da7a141 | 21 | * @brief Implementation of BLEDeviceInstanceBase |
mamont090671 | 0:70ce6da7a141 | 22 | ****************************************************************************** |
mamont090671 | 0:70ce6da7a141 | 23 | * @copy |
mamont090671 | 0:70ce6da7a141 | 24 | * |
mamont090671 | 0:70ce6da7a141 | 25 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS |
mamont090671 | 0:70ce6da7a141 | 26 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE |
mamont090671 | 0:70ce6da7a141 | 27 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY |
mamont090671 | 0:70ce6da7a141 | 28 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING |
mamont090671 | 0:70ce6da7a141 | 29 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE |
mamont090671 | 0:70ce6da7a141 | 30 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. |
mamont090671 | 0:70ce6da7a141 | 31 | * |
mamont090671 | 0:70ce6da7a141 | 32 | * <h2><center>© COPYRIGHT 2013 STMicroelectronics</center></h2> |
mamont090671 | 0:70ce6da7a141 | 33 | */ |
mamont090671 | 0:70ce6da7a141 | 34 | |
mamont090671 | 0:70ce6da7a141 | 35 | /** @defgroup BlueNRGDevice |
mamont090671 | 0:70ce6da7a141 | 36 | * @brief BlueNRG BLE_API Device Adaptation |
mamont090671 | 0:70ce6da7a141 | 37 | * @{ |
mamont090671 | 0:70ce6da7a141 | 38 | */ |
mamont090671 | 0:70ce6da7a141 | 39 | |
mamont090671 | 0:70ce6da7a141 | 40 | #ifdef YOTTA_CFG_MBED_OS |
mamont090671 | 0:70ce6da7a141 | 41 | #include "mbed-drivers/mbed.h" |
mamont090671 | 0:70ce6da7a141 | 42 | #else |
mamont090671 | 0:70ce6da7a141 | 43 | #include "mbed.h" |
mamont090671 | 0:70ce6da7a141 | 44 | #endif |
mamont090671 | 0:70ce6da7a141 | 45 | #include "BlueNRGDevice.h" |
mamont090671 | 0:70ce6da7a141 | 46 | #include "BlueNRGGap.h" |
mamont090671 | 0:70ce6da7a141 | 47 | #include "BlueNRGGattServer.h" |
mamont090671 | 0:70ce6da7a141 | 48 | |
mamont090671 | 0:70ce6da7a141 | 49 | #include "btle.h" |
mamont090671 | 0:70ce6da7a141 | 50 | #include "ble_utils.h" |
mamont090671 | 0:70ce6da7a141 | 51 | #include "ble_osal.h" |
mamont090671 | 0:70ce6da7a141 | 52 | |
mamont090671 | 0:70ce6da7a141 | 53 | #include "ble_debug.h" |
mamont090671 | 0:70ce6da7a141 | 54 | #include "stm32_bluenrg_ble.h" |
mamont090671 | 0:70ce6da7a141 | 55 | |
mamont090671 | 0:70ce6da7a141 | 56 | extern "C" { |
mamont090671 | 0:70ce6da7a141 | 57 | #include "ble_hci.h" |
mamont090671 | 0:70ce6da7a141 | 58 | #include "bluenrg_utils.h" |
mamont090671 | 0:70ce6da7a141 | 59 | } |
mamont090671 | 0:70ce6da7a141 | 60 | |
mamont090671 | 0:70ce6da7a141 | 61 | #define HEADER_SIZE 5 |
mamont090671 | 0:70ce6da7a141 | 62 | #define MAX_BUFFER_SIZE 255 |
mamont090671 | 0:70ce6da7a141 | 63 | |
mamont090671 | 0:70ce6da7a141 | 64 | /** |
mamont090671 | 0:70ce6da7a141 | 65 | * The singleton which represents the BlueNRG transport for the BLEDevice. |
mamont090671 | 0:70ce6da7a141 | 66 | * |
mamont090671 | 0:70ce6da7a141 | 67 | * See file 'bluenrg_targets.h' for details regarding the peripheral pins used! |
mamont090671 | 0:70ce6da7a141 | 68 | */ |
mamont090671 | 0:70ce6da7a141 | 69 | #include "bluenrg_targets.h" |
mamont090671 | 0:70ce6da7a141 | 70 | |
mamont090671 | 0:70ce6da7a141 | 71 | BlueNRGDevice bluenrgDeviceInstance(BLUENRG_PIN_SPI_MOSI, |
mamont090671 | 0:70ce6da7a141 | 72 | BLUENRG_PIN_SPI_MISO, |
mamont090671 | 0:70ce6da7a141 | 73 | BLUENRG_PIN_SPI_SCK, |
mamont090671 | 0:70ce6da7a141 | 74 | BLUENRG_PIN_SPI_nCS, |
mamont090671 | 0:70ce6da7a141 | 75 | BLUENRG_PIN_SPI_RESET, |
mamont090671 | 0:70ce6da7a141 | 76 | BLUENRG_PIN_SPI_IRQ); |
mamont090671 | 0:70ce6da7a141 | 77 | |
mamont090671 | 0:70ce6da7a141 | 78 | /** |
mamont090671 | 0:70ce6da7a141 | 79 | * BLE-API requires an implementation of the following function in order to |
mamont090671 | 0:70ce6da7a141 | 80 | * obtain its transport handle. |
mamont090671 | 0:70ce6da7a141 | 81 | */ |
mamont090671 | 0:70ce6da7a141 | 82 | BLEInstanceBase * |
mamont090671 | 0:70ce6da7a141 | 83 | createBLEInstance(void) |
mamont090671 | 0:70ce6da7a141 | 84 | { |
mamont090671 | 0:70ce6da7a141 | 85 | return (&bluenrgDeviceInstance); |
mamont090671 | 0:70ce6da7a141 | 86 | } |
mamont090671 | 0:70ce6da7a141 | 87 | |
mamont090671 | 0:70ce6da7a141 | 88 | /**************************************************************************/ |
mamont090671 | 0:70ce6da7a141 | 89 | /** |
mamont090671 | 0:70ce6da7a141 | 90 | @brief Constructor |
mamont090671 | 0:70ce6da7a141 | 91 | * @param mosi mbed pin to use for MOSI line of SPI interface |
mamont090671 | 0:70ce6da7a141 | 92 | * @param miso mbed pin to use for MISO line of SPI interface |
mamont090671 | 0:70ce6da7a141 | 93 | * @param sck mbed pin to use for SCK line of SPI interface |
mamont090671 | 0:70ce6da7a141 | 94 | * @param cs mbed pin to use for not chip select line of SPI interface |
mamont090671 | 0:70ce6da7a141 | 95 | * @param rst mbed pin to use for BlueNRG reset |
mamont090671 | 0:70ce6da7a141 | 96 | * @param irq mbed pin for BlueNRG IRQ |
mamont090671 | 0:70ce6da7a141 | 97 | */ |
mamont090671 | 0:70ce6da7a141 | 98 | /**************************************************************************/ |
mamont090671 | 0:70ce6da7a141 | 99 | BlueNRGDevice::BlueNRGDevice(PinName mosi, |
mamont090671 | 0:70ce6da7a141 | 100 | PinName miso, |
mamont090671 | 0:70ce6da7a141 | 101 | PinName sck, |
mamont090671 | 0:70ce6da7a141 | 102 | PinName cs, |
mamont090671 | 0:70ce6da7a141 | 103 | PinName rst, |
mamont090671 | 0:70ce6da7a141 | 104 | PinName irq) : |
mamont090671 | 0:70ce6da7a141 | 105 | isInitialized(false), spi_(mosi, miso, sck), nCS_(cs), rst_(rst), irq_(irq) |
mamont090671 | 0:70ce6da7a141 | 106 | { |
mamont090671 | 0:70ce6da7a141 | 107 | // Setup the spi for 8 bit data, low clock polarity, |
mamont090671 | 0:70ce6da7a141 | 108 | // 1-edge phase, with an 8MHz clock rate |
mamont090671 | 0:70ce6da7a141 | 109 | spi_.format(8, 0); |
mamont090671 | 0:70ce6da7a141 | 110 | spi_.frequency(8000000); |
mamont090671 | 0:70ce6da7a141 | 111 | |
mamont090671 | 0:70ce6da7a141 | 112 | // Deselect the BlueNRG chip by keeping its nCS signal high |
mamont090671 | 0:70ce6da7a141 | 113 | nCS_ = 1; |
mamont090671 | 0:70ce6da7a141 | 114 | |
mamont090671 | 0:70ce6da7a141 | 115 | wait_us(500); |
mamont090671 | 0:70ce6da7a141 | 116 | |
mamont090671 | 0:70ce6da7a141 | 117 | // Prepare communication between the host and the BlueNRG SPI interface |
mamont090671 | 0:70ce6da7a141 | 118 | HCI_Init(); |
mamont090671 | 0:70ce6da7a141 | 119 | |
mamont090671 | 0:70ce6da7a141 | 120 | // Set the interrupt handler for the device |
mamont090671 | 0:70ce6da7a141 | 121 | irq_.mode(PullDown); // set irq mode |
mamont090671 | 0:70ce6da7a141 | 122 | irq_.rise(&HCI_Isr); |
mamont090671 | 0:70ce6da7a141 | 123 | } |
mamont090671 | 0:70ce6da7a141 | 124 | |
mamont090671 | 0:70ce6da7a141 | 125 | /**************************************************************************/ |
mamont090671 | 0:70ce6da7a141 | 126 | /** |
mamont090671 | 0:70ce6da7a141 | 127 | @brief Destructor |
mamont090671 | 0:70ce6da7a141 | 128 | */ |
mamont090671 | 0:70ce6da7a141 | 129 | /**************************************************************************/ |
mamont090671 | 0:70ce6da7a141 | 130 | BlueNRGDevice::~BlueNRGDevice(void) |
mamont090671 | 0:70ce6da7a141 | 131 | { |
mamont090671 | 0:70ce6da7a141 | 132 | } |
mamont090671 | 0:70ce6da7a141 | 133 | |
mamont090671 | 0:70ce6da7a141 | 134 | /** |
mamont090671 | 0:70ce6da7a141 | 135 | * @brief Get BlueNRG HW version in bootloader mode |
mamont090671 | 0:70ce6da7a141 | 136 | * @param hw_version The HW version is written to this parameter |
mamont090671 | 0:70ce6da7a141 | 137 | * @retval It returns BLE_STATUS_SUCCESS on success or an error code otherwise |
mamont090671 | 0:70ce6da7a141 | 138 | */ |
mamont090671 | 0:70ce6da7a141 | 139 | uint8_t BlueNRGDevice::getUpdaterHardwareVersion(uint8_t *hw_version) |
mamont090671 | 0:70ce6da7a141 | 140 | { |
mamont090671 | 0:70ce6da7a141 | 141 | uint8_t status; |
mamont090671 | 0:70ce6da7a141 | 142 | |
mamont090671 | 0:70ce6da7a141 | 143 | status = getBlueNRGUpdaterHWVersion(hw_version); |
mamont090671 | 0:70ce6da7a141 | 144 | |
mamont090671 | 0:70ce6da7a141 | 145 | return (status); |
mamont090671 | 0:70ce6da7a141 | 146 | } |
mamont090671 | 0:70ce6da7a141 | 147 | |
mamont090671 | 0:70ce6da7a141 | 148 | /** |
mamont090671 | 0:70ce6da7a141 | 149 | * @brief Flash a new firmware using internal bootloader. |
mamont090671 | 0:70ce6da7a141 | 150 | * @param fw_image Pointer to the firmware image (raw binary data, |
mamont090671 | 0:70ce6da7a141 | 151 | * little-endian). |
mamont090671 | 0:70ce6da7a141 | 152 | * @param fw_size Size of the firmware image. The firmware image size shall |
mamont090671 | 0:70ce6da7a141 | 153 | * be multiple of 4 bytes. |
mamont090671 | 0:70ce6da7a141 | 154 | * @retval int It returns BLE_STATUS_SUCCESS on success, or a number |
mamont090671 | 0:70ce6da7a141 | 155 | * not equal to 0 in case of error |
mamont090671 | 0:70ce6da7a141 | 156 | * (ACI_ERROR, UNSUPPORTED_VERSION, WRONG_IMAGE_SIZE, CRC_ERROR) |
mamont090671 | 0:70ce6da7a141 | 157 | */ |
mamont090671 | 0:70ce6da7a141 | 158 | int BlueNRGDevice::updateFirmware(const uint8_t *fw_image, uint32_t fw_size) |
mamont090671 | 0:70ce6da7a141 | 159 | { |
mamont090671 | 0:70ce6da7a141 | 160 | int status = program_device(fw_image, fw_size); |
mamont090671 | 0:70ce6da7a141 | 161 | |
mamont090671 | 0:70ce6da7a141 | 162 | return (status); |
mamont090671 | 0:70ce6da7a141 | 163 | } |
mamont090671 | 0:70ce6da7a141 | 164 | |
mamont090671 | 0:70ce6da7a141 | 165 | |
mamont090671 | 0:70ce6da7a141 | 166 | /** |
mamont090671 | 0:70ce6da7a141 | 167 | * @brief Initialises anything required to start using BLE |
mamont090671 | 0:70ce6da7a141 | 168 | * @param[in] instanceID |
mamont090671 | 0:70ce6da7a141 | 169 | * The ID of the instance to initialize. |
mamont090671 | 0:70ce6da7a141 | 170 | * @param[in] callback |
mamont090671 | 0:70ce6da7a141 | 171 | * A callback for when initialization completes for a BLE |
mamont090671 | 0:70ce6da7a141 | 172 | * instance. This is an optional parameter set to NULL when not |
mamont090671 | 0:70ce6da7a141 | 173 | * supplied. |
mamont090671 | 0:70ce6da7a141 | 174 | * |
mamont090671 | 0:70ce6da7a141 | 175 | * @return BLE_ERROR_NONE if the initialization procedure was started |
mamont090671 | 0:70ce6da7a141 | 176 | * successfully. |
mamont090671 | 0:70ce6da7a141 | 177 | */ |
mamont090671 | 0:70ce6da7a141 | 178 | ble_error_t BlueNRGDevice::init(BLE::InstanceID_t instanceID, FunctionPointerWithContext<BLE::InitializationCompleteCallbackContext *> callback) |
mamont090671 | 0:70ce6da7a141 | 179 | { |
mamont090671 | 0:70ce6da7a141 | 180 | if (isInitialized) { |
mamont090671 | 0:70ce6da7a141 | 181 | BLE::InitializationCompleteCallbackContext context = { |
mamont090671 | 0:70ce6da7a141 | 182 | BLE::Instance(instanceID), |
mamont090671 | 0:70ce6da7a141 | 183 | BLE_ERROR_ALREADY_INITIALIZED |
mamont090671 | 0:70ce6da7a141 | 184 | }; |
mamont090671 | 0:70ce6da7a141 | 185 | callback.call(&context); |
mamont090671 | 0:70ce6da7a141 | 186 | return BLE_ERROR_ALREADY_INITIALIZED; |
mamont090671 | 0:70ce6da7a141 | 187 | } |
mamont090671 | 0:70ce6da7a141 | 188 | |
mamont090671 | 0:70ce6da7a141 | 189 | // Init the BlueNRG/BlueNRG-MS stack |
mamont090671 | 0:70ce6da7a141 | 190 | btleInit(); |
mamont090671 | 0:70ce6da7a141 | 191 | |
mamont090671 | 0:70ce6da7a141 | 192 | isInitialized = true; |
mamont090671 | 0:70ce6da7a141 | 193 | BLE::InitializationCompleteCallbackContext context = { |
mamont090671 | 0:70ce6da7a141 | 194 | BLE::Instance(instanceID), |
mamont090671 | 0:70ce6da7a141 | 195 | BLE_ERROR_NONE |
mamont090671 | 0:70ce6da7a141 | 196 | }; |
mamont090671 | 0:70ce6da7a141 | 197 | callback.call(&context); |
mamont090671 | 0:70ce6da7a141 | 198 | |
mamont090671 | 0:70ce6da7a141 | 199 | return BLE_ERROR_NONE; |
mamont090671 | 0:70ce6da7a141 | 200 | } |
mamont090671 | 0:70ce6da7a141 | 201 | |
mamont090671 | 0:70ce6da7a141 | 202 | |
mamont090671 | 0:70ce6da7a141 | 203 | /** |
mamont090671 | 0:70ce6da7a141 | 204 | @brief Resets the BLE HW, removing any existing services and |
mamont090671 | 0:70ce6da7a141 | 205 | characteristics |
mamont090671 | 0:70ce6da7a141 | 206 | @param[in] void |
mamont090671 | 0:70ce6da7a141 | 207 | @returns void |
mamont090671 | 0:70ce6da7a141 | 208 | */ |
mamont090671 | 0:70ce6da7a141 | 209 | void BlueNRGDevice::reset(void) |
mamont090671 | 0:70ce6da7a141 | 210 | { |
mamont090671 | 0:70ce6da7a141 | 211 | /* Reset BlueNRG SPI interface. Hold reset line to 0 for 1500us */ |
mamont090671 | 0:70ce6da7a141 | 212 | rst_ = 0; |
mamont090671 | 0:70ce6da7a141 | 213 | wait_us(1500); |
mamont090671 | 0:70ce6da7a141 | 214 | rst_ = 1; |
mamont090671 | 0:70ce6da7a141 | 215 | |
mamont090671 | 0:70ce6da7a141 | 216 | /* Wait for the radio to come back up */ |
mamont090671 | 0:70ce6da7a141 | 217 | wait_us(5000); |
mamont090671 | 0:70ce6da7a141 | 218 | } |
mamont090671 | 0:70ce6da7a141 | 219 | |
mamont090671 | 0:70ce6da7a141 | 220 | /*! |
mamont090671 | 0:70ce6da7a141 | 221 | @brief Wait for any BLE Event like BLE Connection, Read Request etc. |
mamont090671 | 0:70ce6da7a141 | 222 | @param[in] void |
mamont090671 | 0:70ce6da7a141 | 223 | @returns char * |
mamont090671 | 0:70ce6da7a141 | 224 | */ |
mamont090671 | 0:70ce6da7a141 | 225 | void BlueNRGDevice::waitForEvent(void) |
mamont090671 | 0:70ce6da7a141 | 226 | { |
mamont090671 | 0:70ce6da7a141 | 227 | bool must_return = false; |
mamont090671 | 0:70ce6da7a141 | 228 | |
mamont090671 | 0:70ce6da7a141 | 229 | do { |
mamont090671 | 0:70ce6da7a141 | 230 | bluenrgDeviceInstance.processEvents(); |
mamont090671 | 0:70ce6da7a141 | 231 | |
mamont090671 | 0:70ce6da7a141 | 232 | if(must_return) return; |
mamont090671 | 0:70ce6da7a141 | 233 | |
mamont090671 | 0:70ce6da7a141 | 234 | __WFE(); /* it is recommended that SEVONPEND in the |
mamont090671 | 0:70ce6da7a141 | 235 | System Control Register is NOT set */ |
mamont090671 | 0:70ce6da7a141 | 236 | must_return = true; /* after returning from WFE we must guarantee |
mamont090671 | 0:70ce6da7a141 | 237 | that conrol is given back to main loop before next WFE */ |
mamont090671 | 0:70ce6da7a141 | 238 | } while(true); |
mamont090671 | 0:70ce6da7a141 | 239 | |
mamont090671 | 0:70ce6da7a141 | 240 | } |
mamont090671 | 0:70ce6da7a141 | 241 | |
mamont090671 | 0:70ce6da7a141 | 242 | /*! |
mamont090671 | 0:70ce6da7a141 | 243 | @brief get GAP version |
mamont090671 | 0:70ce6da7a141 | 244 | @brief Get the BLE stack version information |
mamont090671 | 0:70ce6da7a141 | 245 | @param[in] void |
mamont090671 | 0:70ce6da7a141 | 246 | @returns char * |
mamont090671 | 0:70ce6da7a141 | 247 | @returns char * |
mamont090671 | 0:70ce6da7a141 | 248 | */ |
mamont090671 | 0:70ce6da7a141 | 249 | const char *BlueNRGDevice::getVersion(void) |
mamont090671 | 0:70ce6da7a141 | 250 | { |
mamont090671 | 0:70ce6da7a141 | 251 | return getVersionString(); |
mamont090671 | 0:70ce6da7a141 | 252 | } |
mamont090671 | 0:70ce6da7a141 | 253 | |
mamont090671 | 0:70ce6da7a141 | 254 | /**************************************************************************/ |
mamont090671 | 0:70ce6da7a141 | 255 | /*! |
mamont090671 | 0:70ce6da7a141 | 256 | @brief get reference to GAP object |
mamont090671 | 0:70ce6da7a141 | 257 | @param[in] void |
mamont090671 | 0:70ce6da7a141 | 258 | @returns Gap& |
mamont090671 | 0:70ce6da7a141 | 259 | */ |
mamont090671 | 0:70ce6da7a141 | 260 | /**************************************************************************/ |
mamont090671 | 0:70ce6da7a141 | 261 | Gap &BlueNRGDevice::getGap() |
mamont090671 | 0:70ce6da7a141 | 262 | { |
mamont090671 | 0:70ce6da7a141 | 263 | return BlueNRGGap::getInstance(); |
mamont090671 | 0:70ce6da7a141 | 264 | } |
mamont090671 | 0:70ce6da7a141 | 265 | |
mamont090671 | 0:70ce6da7a141 | 266 | const Gap &BlueNRGDevice::getGap() const |
mamont090671 | 0:70ce6da7a141 | 267 | { |
mamont090671 | 0:70ce6da7a141 | 268 | return BlueNRGGap::getInstance(); |
mamont090671 | 0:70ce6da7a141 | 269 | } |
mamont090671 | 0:70ce6da7a141 | 270 | |
mamont090671 | 0:70ce6da7a141 | 271 | /**************************************************************************/ |
mamont090671 | 0:70ce6da7a141 | 272 | /*! |
mamont090671 | 0:70ce6da7a141 | 273 | @brief get reference to GATT server object |
mamont090671 | 0:70ce6da7a141 | 274 | @param[in] void |
mamont090671 | 0:70ce6da7a141 | 275 | @returns GattServer& |
mamont090671 | 0:70ce6da7a141 | 276 | */ |
mamont090671 | 0:70ce6da7a141 | 277 | /**************************************************************************/ |
mamont090671 | 0:70ce6da7a141 | 278 | GattServer &BlueNRGDevice::getGattServer() |
mamont090671 | 0:70ce6da7a141 | 279 | { |
mamont090671 | 0:70ce6da7a141 | 280 | return BlueNRGGattServer::getInstance(); |
mamont090671 | 0:70ce6da7a141 | 281 | } |
mamont090671 | 0:70ce6da7a141 | 282 | |
mamont090671 | 0:70ce6da7a141 | 283 | const GattServer &BlueNRGDevice::getGattServer() const |
mamont090671 | 0:70ce6da7a141 | 284 | { |
mamont090671 | 0:70ce6da7a141 | 285 | return BlueNRGGattServer::getInstance(); |
mamont090671 | 0:70ce6da7a141 | 286 | } |
mamont090671 | 0:70ce6da7a141 | 287 | |
mamont090671 | 0:70ce6da7a141 | 288 | /**************************************************************************/ |
mamont090671 | 0:70ce6da7a141 | 289 | /*! |
mamont090671 | 0:70ce6da7a141 | 290 | @brief shut down the BLE device |
mamont090671 | 0:70ce6da7a141 | 291 | @param[out] error if any |
mamont090671 | 0:70ce6da7a141 | 292 | */ |
mamont090671 | 0:70ce6da7a141 | 293 | /**************************************************************************/ |
mamont090671 | 0:70ce6da7a141 | 294 | ble_error_t BlueNRGDevice::shutdown(void) { |
mamont090671 | 0:70ce6da7a141 | 295 | PRINTF("BlueNRGDevice::reset\n"); |
mamont090671 | 0:70ce6da7a141 | 296 | |
mamont090671 | 0:70ce6da7a141 | 297 | if (!isInitialized) { |
mamont090671 | 0:70ce6da7a141 | 298 | return BLE_ERROR_INITIALIZATION_INCOMPLETE; |
mamont090671 | 0:70ce6da7a141 | 299 | } |
mamont090671 | 0:70ce6da7a141 | 300 | |
mamont090671 | 0:70ce6da7a141 | 301 | /* Reset the BlueNRG device first */ |
mamont090671 | 0:70ce6da7a141 | 302 | reset(); |
mamont090671 | 0:70ce6da7a141 | 303 | |
mamont090671 | 0:70ce6da7a141 | 304 | /* Shutdown the BLE API and BlueNRG glue code */ |
mamont090671 | 0:70ce6da7a141 | 305 | ble_error_t error; |
mamont090671 | 0:70ce6da7a141 | 306 | |
mamont090671 | 0:70ce6da7a141 | 307 | /* GattServer instance */ |
mamont090671 | 0:70ce6da7a141 | 308 | error = BlueNRGGattServer::getInstance().reset(); |
mamont090671 | 0:70ce6da7a141 | 309 | if (error != BLE_ERROR_NONE) { |
mamont090671 | 0:70ce6da7a141 | 310 | return error; |
mamont090671 | 0:70ce6da7a141 | 311 | } |
mamont090671 | 0:70ce6da7a141 | 312 | |
mamont090671 | 0:70ce6da7a141 | 313 | /* GattClient instance */ |
mamont090671 | 0:70ce6da7a141 | 314 | error = BlueNRGGattClient::getInstance().reset(); |
mamont090671 | 0:70ce6da7a141 | 315 | if (error != BLE_ERROR_NONE) { |
mamont090671 | 0:70ce6da7a141 | 316 | return error; |
mamont090671 | 0:70ce6da7a141 | 317 | } |
mamont090671 | 0:70ce6da7a141 | 318 | |
mamont090671 | 0:70ce6da7a141 | 319 | /* Gap instance */ |
mamont090671 | 0:70ce6da7a141 | 320 | error = BlueNRGGap::getInstance().reset(); |
mamont090671 | 0:70ce6da7a141 | 321 | if (error != BLE_ERROR_NONE) { |
mamont090671 | 0:70ce6da7a141 | 322 | return error; |
mamont090671 | 0:70ce6da7a141 | 323 | } |
mamont090671 | 0:70ce6da7a141 | 324 | |
mamont090671 | 0:70ce6da7a141 | 325 | isInitialized = false; |
mamont090671 | 0:70ce6da7a141 | 326 | |
mamont090671 | 0:70ce6da7a141 | 327 | PRINTF("BlueNRGDevice::reset complete\n"); |
mamont090671 | 0:70ce6da7a141 | 328 | return BLE_ERROR_NONE; |
mamont090671 | 0:70ce6da7a141 | 329 | |
mamont090671 | 0:70ce6da7a141 | 330 | } |
mamont090671 | 0:70ce6da7a141 | 331 | |
mamont090671 | 0:70ce6da7a141 | 332 | /** |
mamont090671 | 0:70ce6da7a141 | 333 | * @brief Reads from BlueNRG SPI buffer and store data into local buffer. |
mamont090671 | 0:70ce6da7a141 | 334 | * @param buffer : Buffer where data from SPI are stored |
mamont090671 | 0:70ce6da7a141 | 335 | * @param buff_size: Buffer size |
mamont090671 | 0:70ce6da7a141 | 336 | * @retval int32_t : Number of read bytes |
mamont090671 | 0:70ce6da7a141 | 337 | */ |
mamont090671 | 0:70ce6da7a141 | 338 | int32_t BlueNRGDevice::spiRead(uint8_t *buffer, uint8_t buff_size) |
mamont090671 | 0:70ce6da7a141 | 339 | { |
mamont090671 | 0:70ce6da7a141 | 340 | uint16_t byte_count; |
mamont090671 | 0:70ce6da7a141 | 341 | uint8_t len = 0; |
mamont090671 | 0:70ce6da7a141 | 342 | uint8_t char_ff = 0xff; |
mamont090671 | 0:70ce6da7a141 | 343 | volatile uint8_t read_char; |
mamont090671 | 0:70ce6da7a141 | 344 | |
mamont090671 | 0:70ce6da7a141 | 345 | uint8_t i = 0; |
mamont090671 | 0:70ce6da7a141 | 346 | volatile uint8_t tmpreg; |
mamont090671 | 0:70ce6da7a141 | 347 | |
mamont090671 | 0:70ce6da7a141 | 348 | uint8_t header_master[HEADER_SIZE] = {0x0b, 0x00, 0x00, 0x00, 0x00}; |
mamont090671 | 0:70ce6da7a141 | 349 | uint8_t header_slave[HEADER_SIZE]; |
mamont090671 | 0:70ce6da7a141 | 350 | |
mamont090671 | 0:70ce6da7a141 | 351 | /* Select the chip */ |
mamont090671 | 0:70ce6da7a141 | 352 | nCS_ = 0; |
mamont090671 | 0:70ce6da7a141 | 353 | |
mamont090671 | 0:70ce6da7a141 | 354 | /* Read the header */ |
mamont090671 | 0:70ce6da7a141 | 355 | for (i = 0; i < 5; i++) |
mamont090671 | 0:70ce6da7a141 | 356 | { |
mamont090671 | 0:70ce6da7a141 | 357 | tmpreg = spi_.write(header_master[i]); |
mamont090671 | 0:70ce6da7a141 | 358 | header_slave[i] = (uint8_t)(tmpreg); |
mamont090671 | 0:70ce6da7a141 | 359 | } |
mamont090671 | 0:70ce6da7a141 | 360 | |
mamont090671 | 0:70ce6da7a141 | 361 | if (header_slave[0] == 0x02) { |
mamont090671 | 0:70ce6da7a141 | 362 | /* device is ready */ |
mamont090671 | 0:70ce6da7a141 | 363 | byte_count = (header_slave[4]<<8)|header_slave[3]; |
mamont090671 | 0:70ce6da7a141 | 364 | |
mamont090671 | 0:70ce6da7a141 | 365 | if (byte_count > 0) { |
mamont090671 | 0:70ce6da7a141 | 366 | |
mamont090671 | 0:70ce6da7a141 | 367 | /* avoid to read more data that size of the buffer */ |
mamont090671 | 0:70ce6da7a141 | 368 | if (byte_count > buff_size){ |
mamont090671 | 0:70ce6da7a141 | 369 | byte_count = buff_size; |
mamont090671 | 0:70ce6da7a141 | 370 | } |
mamont090671 | 0:70ce6da7a141 | 371 | |
mamont090671 | 0:70ce6da7a141 | 372 | for (len = 0; len < byte_count; len++){ |
mamont090671 | 0:70ce6da7a141 | 373 | read_char = spi_.write(char_ff); |
mamont090671 | 0:70ce6da7a141 | 374 | buffer[len] = read_char; |
mamont090671 | 0:70ce6da7a141 | 375 | } |
mamont090671 | 0:70ce6da7a141 | 376 | } |
mamont090671 | 0:70ce6da7a141 | 377 | } |
mamont090671 | 0:70ce6da7a141 | 378 | /* Release CS line to deselect the chip */ |
mamont090671 | 0:70ce6da7a141 | 379 | nCS_ = 1; |
mamont090671 | 0:70ce6da7a141 | 380 | |
mamont090671 | 0:70ce6da7a141 | 381 | // Add a small delay to give time to the BlueNRG to set the IRQ pin low |
mamont090671 | 0:70ce6da7a141 | 382 | // to avoid a useless SPI read at the end of the transaction |
mamont090671 | 0:70ce6da7a141 | 383 | for(volatile int i = 0; i < 2; i++)__NOP(); |
mamont090671 | 0:70ce6da7a141 | 384 | |
mamont090671 | 0:70ce6da7a141 | 385 | #ifdef PRINT_CSV_FORMAT |
mamont090671 | 0:70ce6da7a141 | 386 | if (len > 0) { |
mamont090671 | 0:70ce6da7a141 | 387 | print_csv_time(); |
mamont090671 | 0:70ce6da7a141 | 388 | for (int i=0; i<len; i++) { |
mamont090671 | 0:70ce6da7a141 | 389 | PRINT_CSV(" %02x", buffer[i]); |
mamont090671 | 0:70ce6da7a141 | 390 | } |
mamont090671 | 0:70ce6da7a141 | 391 | PRINT_CSV("\n"); |
mamont090671 | 0:70ce6da7a141 | 392 | } |
mamont090671 | 0:70ce6da7a141 | 393 | #endif |
mamont090671 | 0:70ce6da7a141 | 394 | |
mamont090671 | 0:70ce6da7a141 | 395 | return len; |
mamont090671 | 0:70ce6da7a141 | 396 | } |
mamont090671 | 0:70ce6da7a141 | 397 | |
mamont090671 | 0:70ce6da7a141 | 398 | /** |
mamont090671 | 0:70ce6da7a141 | 399 | * @brief Writes data from local buffer to SPI. |
mamont090671 | 0:70ce6da7a141 | 400 | * @param data1 : First data buffer to be written |
mamont090671 | 0:70ce6da7a141 | 401 | * @param data2 : Second data buffer to be written |
mamont090671 | 0:70ce6da7a141 | 402 | * @param Nb_bytes1: Size of first data buffer to be written |
mamont090671 | 0:70ce6da7a141 | 403 | * @param Nb_bytes2: Size of second data buffer to be written |
mamont090671 | 0:70ce6da7a141 | 404 | * @retval Number of read bytes |
mamont090671 | 0:70ce6da7a141 | 405 | */ |
mamont090671 | 0:70ce6da7a141 | 406 | int32_t BlueNRGDevice::spiWrite(uint8_t* data1, |
mamont090671 | 0:70ce6da7a141 | 407 | uint8_t* data2, uint8_t Nb_bytes1, uint8_t Nb_bytes2) |
mamont090671 | 0:70ce6da7a141 | 408 | { |
mamont090671 | 0:70ce6da7a141 | 409 | int32_t result = 0; |
mamont090671 | 0:70ce6da7a141 | 410 | uint32_t i; |
mamont090671 | 0:70ce6da7a141 | 411 | volatile uint8_t tmpreg; |
mamont090671 | 0:70ce6da7a141 | 412 | |
mamont090671 | 0:70ce6da7a141 | 413 | unsigned char header_master[HEADER_SIZE] = {0x0a, 0x00, 0x00, 0x00, 0x00}; |
mamont090671 | 0:70ce6da7a141 | 414 | unsigned char header_slave[HEADER_SIZE] = {0xaa, 0x00, 0x00, 0x00, 0x00}; |
mamont090671 | 0:70ce6da7a141 | 415 | |
mamont090671 | 0:70ce6da7a141 | 416 | disable_irq(); |
mamont090671 | 0:70ce6da7a141 | 417 | |
mamont090671 | 0:70ce6da7a141 | 418 | /* CS reset */ |
mamont090671 | 0:70ce6da7a141 | 419 | nCS_ = 0; |
mamont090671 | 0:70ce6da7a141 | 420 | |
mamont090671 | 0:70ce6da7a141 | 421 | /* Exchange header */ |
mamont090671 | 0:70ce6da7a141 | 422 | for (i = 0; i < 5; i++) |
mamont090671 | 0:70ce6da7a141 | 423 | { |
mamont090671 | 0:70ce6da7a141 | 424 | tmpreg = spi_.write(header_master[i]); |
mamont090671 | 0:70ce6da7a141 | 425 | header_slave[i] = tmpreg; |
mamont090671 | 0:70ce6da7a141 | 426 | } |
mamont090671 | 0:70ce6da7a141 | 427 | |
mamont090671 | 0:70ce6da7a141 | 428 | if (header_slave[0] == 0x02) { |
mamont090671 | 0:70ce6da7a141 | 429 | /* SPI is ready */ |
mamont090671 | 0:70ce6da7a141 | 430 | if (header_slave[1] >= (Nb_bytes1+Nb_bytes2)) { |
mamont090671 | 0:70ce6da7a141 | 431 | |
mamont090671 | 0:70ce6da7a141 | 432 | /* Buffer is big enough */ |
mamont090671 | 0:70ce6da7a141 | 433 | for (i = 0; i < Nb_bytes1; i++) { |
mamont090671 | 0:70ce6da7a141 | 434 | spi_.write(*(data1 + i)); |
mamont090671 | 0:70ce6da7a141 | 435 | } |
mamont090671 | 0:70ce6da7a141 | 436 | for (i = 0; i < Nb_bytes2; i++) { |
mamont090671 | 0:70ce6da7a141 | 437 | spi_.write(*(data2 + i)); |
mamont090671 | 0:70ce6da7a141 | 438 | } |
mamont090671 | 0:70ce6da7a141 | 439 | } else { |
mamont090671 | 0:70ce6da7a141 | 440 | /* Buffer is too small */ |
mamont090671 | 0:70ce6da7a141 | 441 | result = -2; |
mamont090671 | 0:70ce6da7a141 | 442 | } |
mamont090671 | 0:70ce6da7a141 | 443 | } else { |
mamont090671 | 0:70ce6da7a141 | 444 | /* SPI is not ready */ |
mamont090671 | 0:70ce6da7a141 | 445 | result = -1; |
mamont090671 | 0:70ce6da7a141 | 446 | } |
mamont090671 | 0:70ce6da7a141 | 447 | |
mamont090671 | 0:70ce6da7a141 | 448 | /* Release CS line */ |
mamont090671 | 0:70ce6da7a141 | 449 | //HAL_GPIO_WritePin(BNRG_SPI_CS_PORT, BNRG_SPI_CS_PIN, GPIO_PIN_SET); |
mamont090671 | 0:70ce6da7a141 | 450 | nCS_ = 1; |
mamont090671 | 0:70ce6da7a141 | 451 | |
mamont090671 | 0:70ce6da7a141 | 452 | enable_irq(); |
mamont090671 | 0:70ce6da7a141 | 453 | |
mamont090671 | 0:70ce6da7a141 | 454 | return result; |
mamont090671 | 0:70ce6da7a141 | 455 | } |
mamont090671 | 0:70ce6da7a141 | 456 | |
mamont090671 | 0:70ce6da7a141 | 457 | bool BlueNRGDevice::dataPresent() |
mamont090671 | 0:70ce6da7a141 | 458 | { |
mamont090671 | 0:70ce6da7a141 | 459 | return (irq_ == 1); |
mamont090671 | 0:70ce6da7a141 | 460 | } |
mamont090671 | 0:70ce6da7a141 | 461 | |
mamont090671 | 0:70ce6da7a141 | 462 | void BlueNRGDevice::disable_irq() |
mamont090671 | 0:70ce6da7a141 | 463 | { |
mamont090671 | 0:70ce6da7a141 | 464 | irq_.disable_irq(); |
mamont090671 | 0:70ce6da7a141 | 465 | } |
mamont090671 | 0:70ce6da7a141 | 466 | |
mamont090671 | 0:70ce6da7a141 | 467 | void BlueNRGDevice::enable_irq() |
mamont090671 | 0:70ce6da7a141 | 468 | { |
mamont090671 | 0:70ce6da7a141 | 469 | irq_.enable_irq(); |
mamont090671 | 0:70ce6da7a141 | 470 | } |
mamont090671 | 0:70ce6da7a141 | 471 | |
mamont090671 | 0:70ce6da7a141 | 472 | void BlueNRGDevice::processEvents() { |
mamont090671 | 0:70ce6da7a141 | 473 | btle_handler(); |
mamont090671 | 0:70ce6da7a141 | 474 | } |