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