BLE test
Fork of X_NUCLEO_IDB0XA1 by
source/BlueNRGDevice.cpp@167:abc42e7158a6, 2015-11-04 (annotated)
- Committer:
- Andrea Palmieri
- Date:
- Wed Nov 04 10:12:19 2015 +0100
- Revision:
- 167:abc42e7158a6
- Parent:
- 144:bdf5e8432131
- Child:
- 168:ad1dff5dca1f
Synch with BLE API 2.0.3
Signed-off-by: Andrea Palmieri <andrea.palmieri@st.com>
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Wolfgang Betz |
130:770ce14d3d15 | 1 | /* mbed Microcontroller Library |
Wolfgang Betz |
130:770ce14d3d15 | 2 | * Copyright (c) 2006-2013 ARM Limited |
Wolfgang Betz |
130:770ce14d3d15 | 3 | * |
Wolfgang Betz |
130:770ce14d3d15 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Wolfgang Betz |
130:770ce14d3d15 | 5 | * you may not use this file except in compliance with the License. |
Wolfgang Betz |
130:770ce14d3d15 | 6 | * You may obtain a copy of the License at |
Wolfgang Betz |
130:770ce14d3d15 | 7 | * |
Wolfgang Betz |
130:770ce14d3d15 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Wolfgang Betz |
130:770ce14d3d15 | 9 | * |
Wolfgang Betz |
130:770ce14d3d15 | 10 | * Unless required by applicable law or agreed to in writing, software |
Wolfgang Betz |
130:770ce14d3d15 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Wolfgang Betz |
130:770ce14d3d15 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Wolfgang Betz |
130:770ce14d3d15 | 13 | * See the License for the specific language governing permissions and |
Wolfgang Betz |
130:770ce14d3d15 | 14 | * limitations under the License. |
Wolfgang Betz |
130:770ce14d3d15 | 15 | */ |
Wolfgang Betz |
130:770ce14d3d15 | 16 | |
Wolfgang Betz |
130:770ce14d3d15 | 17 | /** |
Wolfgang Betz |
130:770ce14d3d15 | 18 | ****************************************************************************** |
Wolfgang Betz |
130:770ce14d3d15 | 19 | * @file BlueNRGDevice.cpp |
Wolfgang Betz |
130:770ce14d3d15 | 20 | * @author STMicroelectronics |
Wolfgang Betz |
130:770ce14d3d15 | 21 | * @brief Implementation of BLEDeviceInstanceBase |
Wolfgang Betz |
130:770ce14d3d15 | 22 | ****************************************************************************** |
Wolfgang Betz |
130:770ce14d3d15 | 23 | * @copy |
Wolfgang Betz |
130:770ce14d3d15 | 24 | * |
Wolfgang Betz |
130:770ce14d3d15 | 25 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS |
Wolfgang Betz |
130:770ce14d3d15 | 26 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE |
Wolfgang Betz |
130:770ce14d3d15 | 27 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY |
Wolfgang Betz |
130:770ce14d3d15 | 28 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING |
Wolfgang Betz |
130:770ce14d3d15 | 29 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE |
Wolfgang Betz |
130:770ce14d3d15 | 30 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. |
Wolfgang Betz |
130:770ce14d3d15 | 31 | * |
Wolfgang Betz |
130:770ce14d3d15 | 32 | * <h2><center>© COPYRIGHT 2013 STMicroelectronics</center></h2> |
Wolfgang Betz |
130:770ce14d3d15 | 33 | */ |
Wolfgang Betz |
130:770ce14d3d15 | 34 | |
Wolfgang Betz |
130:770ce14d3d15 | 35 | /** @defgroup BlueNRGDevice |
Wolfgang Betz |
130:770ce14d3d15 | 36 | * @brief BlueNRG BLE_API Device Adaptation |
Wolfgang Betz |
130:770ce14d3d15 | 37 | * @{ |
Wolfgang Betz |
130:770ce14d3d15 | 38 | */ |
Wolfgang Betz |
130:770ce14d3d15 | 39 | |
Wolfgang Betz |
144:bdf5e8432131 | 40 | #include "mbed-drivers/mbed.h" |
Wolfgang Betz |
130:770ce14d3d15 | 41 | #include "BlueNRGDevice.h" |
Wolfgang Betz |
130:770ce14d3d15 | 42 | #include "BlueNRGGap.h" |
Wolfgang Betz |
130:770ce14d3d15 | 43 | #include "BlueNRGGattServer.h" |
Wolfgang Betz |
130:770ce14d3d15 | 44 | |
Wolfgang Betz |
130:770ce14d3d15 | 45 | #include "btle.h" |
Wolfgang Betz |
130:770ce14d3d15 | 46 | #include "Utils.h" |
Wolfgang Betz |
130:770ce14d3d15 | 47 | #include "osal.h" |
Wolfgang Betz |
130:770ce14d3d15 | 48 | |
Wolfgang Betz |
130:770ce14d3d15 | 49 | #include "debug.h" |
Wolfgang Betz |
130:770ce14d3d15 | 50 | #include "stm32_bluenrg_ble.h" |
Wolfgang Betz |
130:770ce14d3d15 | 51 | |
Wolfgang Betz |
130:770ce14d3d15 | 52 | extern "C" { |
Wolfgang Betz |
130:770ce14d3d15 | 53 | #include "hci.h" |
Wolfgang Betz |
130:770ce14d3d15 | 54 | } |
Wolfgang Betz |
130:770ce14d3d15 | 55 | |
Wolfgang Betz |
130:770ce14d3d15 | 56 | #define HEADER_SIZE 5 |
Wolfgang Betz |
130:770ce14d3d15 | 57 | #define MAX_BUFFER_SIZE 255 |
Wolfgang Betz |
130:770ce14d3d15 | 58 | |
Wolfgang Betz |
130:770ce14d3d15 | 59 | /** |
Wolfgang Betz |
130:770ce14d3d15 | 60 | * The singleton which represents the BlueNRG transport for the BLEDevice. |
Wolfgang Betz |
130:770ce14d3d15 | 61 | * |
Wolfgang Betz |
130:770ce14d3d15 | 62 | * See file 'x_nucleo_idb0xa1_targets.h' for details regarding the peripheral pins used! |
Wolfgang Betz |
130:770ce14d3d15 | 63 | */ |
Wolfgang Betz |
130:770ce14d3d15 | 64 | #include "x_nucleo_idb0xa1_targets.h" |
Wolfgang Betz |
130:770ce14d3d15 | 65 | |
Wolfgang Betz |
130:770ce14d3d15 | 66 | BlueNRGDevice bluenrgDeviceInstance(IDB0XA1_PIN_SPI_MOSI, |
Wolfgang Betz |
130:770ce14d3d15 | 67 | IDB0XA1_PIN_SPI_MISO, |
Wolfgang Betz |
130:770ce14d3d15 | 68 | IDB0XA1_PIN_SPI_SCK, |
Wolfgang Betz |
130:770ce14d3d15 | 69 | IDB0XA1_PIN_SPI_nCS, |
Wolfgang Betz |
130:770ce14d3d15 | 70 | IDB0XA1_PIN_SPI_RESET, |
Wolfgang Betz |
130:770ce14d3d15 | 71 | IDB0XA1_PIN_SPI_IRQ); |
Wolfgang Betz |
130:770ce14d3d15 | 72 | |
Wolfgang Betz |
130:770ce14d3d15 | 73 | /** |
Wolfgang Betz |
130:770ce14d3d15 | 74 | * BLE-API requires an implementation of the following function in order to |
Wolfgang Betz |
130:770ce14d3d15 | 75 | * obtain its transport handle. |
Wolfgang Betz |
130:770ce14d3d15 | 76 | */ |
Wolfgang Betz |
130:770ce14d3d15 | 77 | BLEInstanceBase * |
Wolfgang Betz |
130:770ce14d3d15 | 78 | createBLEInstance(void) |
Wolfgang Betz |
130:770ce14d3d15 | 79 | { |
Wolfgang Betz |
130:770ce14d3d15 | 80 | return (&bluenrgDeviceInstance); |
Wolfgang Betz |
130:770ce14d3d15 | 81 | } |
Wolfgang Betz |
130:770ce14d3d15 | 82 | |
Wolfgang Betz |
130:770ce14d3d15 | 83 | /**************************************************************************/ |
Wolfgang Betz |
130:770ce14d3d15 | 84 | /** |
Wolfgang Betz |
130:770ce14d3d15 | 85 | @brief Constructor |
Wolfgang Betz |
130:770ce14d3d15 | 86 | * @param mosi mbed pin to use for MOSI line of SPI interface |
Wolfgang Betz |
130:770ce14d3d15 | 87 | * @param miso mbed pin to use for MISO line of SPI interface |
Wolfgang Betz |
130:770ce14d3d15 | 88 | * @param sck mbed pin to use for SCK line of SPI interface |
Wolfgang Betz |
130:770ce14d3d15 | 89 | * @param cs mbed pin to use for not chip select line of SPI interface |
Wolfgang Betz |
130:770ce14d3d15 | 90 | * @param rst mbed pin to use for BlueNRG reset |
Wolfgang Betz |
130:770ce14d3d15 | 91 | * @param irq mbed pin for BlueNRG IRQ |
Wolfgang Betz |
130:770ce14d3d15 | 92 | */ |
Wolfgang Betz |
130:770ce14d3d15 | 93 | /**************************************************************************/ |
Wolfgang Betz |
130:770ce14d3d15 | 94 | BlueNRGDevice::BlueNRGDevice(PinName mosi, |
Wolfgang Betz |
130:770ce14d3d15 | 95 | PinName miso, |
Wolfgang Betz |
130:770ce14d3d15 | 96 | PinName sck, |
Wolfgang Betz |
130:770ce14d3d15 | 97 | PinName cs, |
Wolfgang Betz |
130:770ce14d3d15 | 98 | PinName rst, |
Wolfgang Betz |
130:770ce14d3d15 | 99 | PinName irq) : spi_(mosi, miso, sck), nCS_(cs), rst_(rst), irq_(irq) |
Wolfgang Betz |
130:770ce14d3d15 | 100 | { |
Wolfgang Betz |
130:770ce14d3d15 | 101 | isInitialized = false; |
Wolfgang Betz |
130:770ce14d3d15 | 102 | |
Wolfgang Betz |
130:770ce14d3d15 | 103 | // Setup the spi for 8 bit data, low clock polarity, |
Wolfgang Betz |
130:770ce14d3d15 | 104 | // 1-edge phase, with an 8MHz clock rate |
Wolfgang Betz |
130:770ce14d3d15 | 105 | spi_.format(8, 0); |
Wolfgang Betz |
130:770ce14d3d15 | 106 | spi_.frequency(8000000); |
Wolfgang Betz |
130:770ce14d3d15 | 107 | |
Wolfgang Betz |
130:770ce14d3d15 | 108 | // Deselect the BlueNRG chip by keeping its nCS signal high |
Wolfgang Betz |
130:770ce14d3d15 | 109 | nCS_ = 1; |
Wolfgang Betz |
130:770ce14d3d15 | 110 | |
Wolfgang Betz |
130:770ce14d3d15 | 111 | wait_us(500); |
Wolfgang Betz |
130:770ce14d3d15 | 112 | } |
Wolfgang Betz |
130:770ce14d3d15 | 113 | |
Wolfgang Betz |
130:770ce14d3d15 | 114 | /**************************************************************************/ |
Wolfgang Betz |
130:770ce14d3d15 | 115 | /** |
Wolfgang Betz |
130:770ce14d3d15 | 116 | @brief Destructor |
Wolfgang Betz |
130:770ce14d3d15 | 117 | */ |
Wolfgang Betz |
130:770ce14d3d15 | 118 | /**************************************************************************/ |
Wolfgang Betz |
130:770ce14d3d15 | 119 | BlueNRGDevice::~BlueNRGDevice(void) |
Wolfgang Betz |
130:770ce14d3d15 | 120 | { |
Wolfgang Betz |
130:770ce14d3d15 | 121 | } |
Wolfgang Betz |
130:770ce14d3d15 | 122 | |
Wolfgang Betz |
130:770ce14d3d15 | 123 | |
Wolfgang Betz |
130:770ce14d3d15 | 124 | /** |
Wolfgang Betz |
130:770ce14d3d15 | 125 | @brief Initialises anything required to start using BLE |
Wolfgang Betz |
130:770ce14d3d15 | 126 | @param[in] void |
Wolfgang Betz |
130:770ce14d3d15 | 127 | @returns ble_error_t |
Wolfgang Betz |
130:770ce14d3d15 | 128 | */ |
Andrea Palmieri |
167:abc42e7158a6 | 129 | ble_error_t BlueNRGDevice::init(BLE::InstanceID_t instanceID, FunctionPointerWithContext<BLE::InitializationCompleteCallbackContext *> callback) |
Wolfgang Betz |
130:770ce14d3d15 | 130 | { |
Andrea Palmieri |
167:abc42e7158a6 | 131 | if (isInitialized) { |
Andrea Palmieri |
167:abc42e7158a6 | 132 | BLE::InitializationCompleteCallbackContext context = { |
Andrea Palmieri |
167:abc42e7158a6 | 133 | BLE::Instance(instanceID), |
Andrea Palmieri |
167:abc42e7158a6 | 134 | BLE_ERROR_ALREADY_INITIALIZED |
Andrea Palmieri |
167:abc42e7158a6 | 135 | }; |
Andrea Palmieri |
167:abc42e7158a6 | 136 | callback.call(&context); |
Andrea Palmieri |
167:abc42e7158a6 | 137 | return BLE_ERROR_ALREADY_INITIALIZED; |
Andrea Palmieri |
167:abc42e7158a6 | 138 | } |
Andrea Palmieri |
167:abc42e7158a6 | 139 | |
Wolfgang Betz |
130:770ce14d3d15 | 140 | // Set the interrupt handler for the device |
Wolfgang Betz |
130:770ce14d3d15 | 141 | irq_.mode(PullNone); // betzw: set irq mode |
Wolfgang Betz |
130:770ce14d3d15 | 142 | irq_.rise(&HCI_Isr); |
Wolfgang Betz |
130:770ce14d3d15 | 143 | |
Andrea Palmieri |
167:abc42e7158a6 | 144 | instanceID = instanceID; |
Andrea Palmieri |
167:abc42e7158a6 | 145 | |
Wolfgang Betz |
130:770ce14d3d15 | 146 | /* ToDo: Clear memory contents, reset the SD, etc. */ |
Wolfgang Betz |
132:51056160fa4a | 147 | // By default, we set the device GAP role to PERIPHERAL |
Wolfgang Betz |
132:51056160fa4a | 148 | btle_init(BlueNRGGap::getInstance().getIsSetAddress(), GAP_PERIPHERAL_ROLE_IDB04A1); |
Wolfgang Betz |
130:770ce14d3d15 | 149 | |
Wolfgang Betz |
130:770ce14d3d15 | 150 | isInitialized = true; |
Andrea Palmieri |
167:abc42e7158a6 | 151 | BLE::InitializationCompleteCallbackContext context = { |
Andrea Palmieri |
167:abc42e7158a6 | 152 | BLE::Instance(instanceID), |
Andrea Palmieri |
167:abc42e7158a6 | 153 | BLE_ERROR_NONE |
Andrea Palmieri |
167:abc42e7158a6 | 154 | }; |
Andrea Palmieri |
167:abc42e7158a6 | 155 | callback.call(&context); |
Wolfgang Betz |
130:770ce14d3d15 | 156 | |
Wolfgang Betz |
130:770ce14d3d15 | 157 | return BLE_ERROR_NONE; |
Wolfgang Betz |
130:770ce14d3d15 | 158 | } |
Wolfgang Betz |
130:770ce14d3d15 | 159 | |
Wolfgang Betz |
130:770ce14d3d15 | 160 | |
Wolfgang Betz |
130:770ce14d3d15 | 161 | /** |
Wolfgang Betz |
130:770ce14d3d15 | 162 | @brief Resets the BLE HW, removing any existing services and |
Wolfgang Betz |
130:770ce14d3d15 | 163 | characteristics |
Wolfgang Betz |
130:770ce14d3d15 | 164 | @param[in] void |
Wolfgang Betz |
130:770ce14d3d15 | 165 | @returns ble_error_t |
Wolfgang Betz |
130:770ce14d3d15 | 166 | */ |
Wolfgang Betz |
130:770ce14d3d15 | 167 | ble_error_t BlueNRGDevice::reset(void) |
Wolfgang Betz |
130:770ce14d3d15 | 168 | { |
Wolfgang Betz |
130:770ce14d3d15 | 169 | wait_us(500); |
Wolfgang Betz |
130:770ce14d3d15 | 170 | |
Wolfgang Betz |
130:770ce14d3d15 | 171 | /* Reset BlueNRG SPI interface */ |
Wolfgang Betz |
130:770ce14d3d15 | 172 | rst_ = 0; |
Wolfgang Betz |
130:770ce14d3d15 | 173 | wait_us(5); |
Wolfgang Betz |
130:770ce14d3d15 | 174 | rst_ = 1; |
Wolfgang Betz |
130:770ce14d3d15 | 175 | wait_us(5); |
Wolfgang Betz |
130:770ce14d3d15 | 176 | |
Wolfgang Betz |
130:770ce14d3d15 | 177 | /* Wait for the radio to come back up */ |
Wolfgang Betz |
130:770ce14d3d15 | 178 | wait_us(500); |
Wolfgang Betz |
130:770ce14d3d15 | 179 | |
Wolfgang Betz |
130:770ce14d3d15 | 180 | isInitialized = false; |
Wolfgang Betz |
130:770ce14d3d15 | 181 | |
Wolfgang Betz |
130:770ce14d3d15 | 182 | return BLE_ERROR_NONE; |
Wolfgang Betz |
130:770ce14d3d15 | 183 | } |
Wolfgang Betz |
130:770ce14d3d15 | 184 | |
Wolfgang Betz |
130:770ce14d3d15 | 185 | /*! |
Wolfgang Betz |
130:770ce14d3d15 | 186 | @brief Wait for any BLE Event like BLE Connection, Read Request etc. |
Wolfgang Betz |
130:770ce14d3d15 | 187 | @param[in] void |
Wolfgang Betz |
130:770ce14d3d15 | 188 | @returns char * |
Wolfgang Betz |
130:770ce14d3d15 | 189 | */ |
Wolfgang Betz |
130:770ce14d3d15 | 190 | void BlueNRGDevice::waitForEvent(void) |
Wolfgang Betz |
130:770ce14d3d15 | 191 | { |
Wolfgang Betz |
130:770ce14d3d15 | 192 | bool must_return = false; |
Wolfgang Betz |
130:770ce14d3d15 | 193 | |
Wolfgang Betz |
130:770ce14d3d15 | 194 | do { |
Wolfgang Betz |
130:770ce14d3d15 | 195 | BlueNRGGap::getInstance().Process(); |
Wolfgang Betz |
130:770ce14d3d15 | 196 | |
Wolfgang Betz |
130:770ce14d3d15 | 197 | HCI_Process(); |
Wolfgang Betz |
130:770ce14d3d15 | 198 | |
Wolfgang Betz |
130:770ce14d3d15 | 199 | if(must_return) return; |
Wolfgang Betz |
130:770ce14d3d15 | 200 | |
Wolfgang Betz |
130:770ce14d3d15 | 201 | __WFE(); /* it is recommended that SEVONPEND in the |
Wolfgang Betz |
130:770ce14d3d15 | 202 | System Control Register is NOT set */ |
Wolfgang Betz |
130:770ce14d3d15 | 203 | must_return = true; /* after returning from WFE we must guarantee |
Wolfgang Betz |
130:770ce14d3d15 | 204 | that conrol is given back to main loop before next WFE */ |
Wolfgang Betz |
130:770ce14d3d15 | 205 | } while(true); |
Wolfgang Betz |
130:770ce14d3d15 | 206 | |
Wolfgang Betz |
139:3a75965fd389 | 207 | } |
Wolfgang Betz |
130:770ce14d3d15 | 208 | |
Wolfgang Betz |
130:770ce14d3d15 | 209 | /*! |
Wolfgang Betz |
130:770ce14d3d15 | 210 | @brief get GAP version |
Wolfgang Betz |
130:770ce14d3d15 | 211 | @param[in] void |
Wolfgang Betz |
130:770ce14d3d15 | 212 | @returns char * |
Wolfgang Betz |
130:770ce14d3d15 | 213 | */ |
Wolfgang Betz |
130:770ce14d3d15 | 214 | const char *BlueNRGDevice::getVersion(void) |
Wolfgang Betz |
130:770ce14d3d15 | 215 | { |
Wolfgang Betz |
130:770ce14d3d15 | 216 | char *version = new char[6]; |
Wolfgang Betz |
130:770ce14d3d15 | 217 | memcpy((void *)version, "1.0.0", 5); |
Wolfgang Betz |
130:770ce14d3d15 | 218 | return version; |
Wolfgang Betz |
130:770ce14d3d15 | 219 | } |
Wolfgang Betz |
130:770ce14d3d15 | 220 | |
Wolfgang Betz |
130:770ce14d3d15 | 221 | /**************************************************************************/ |
Wolfgang Betz |
130:770ce14d3d15 | 222 | /*! |
Wolfgang Betz |
130:770ce14d3d15 | 223 | @brief get reference to GAP object |
Wolfgang Betz |
130:770ce14d3d15 | 224 | @param[in] void |
Wolfgang Betz |
130:770ce14d3d15 | 225 | @returns Gap& |
Wolfgang Betz |
130:770ce14d3d15 | 226 | */ |
Wolfgang Betz |
130:770ce14d3d15 | 227 | /**************************************************************************/ |
Wolfgang Betz |
130:770ce14d3d15 | 228 | Gap &BlueNRGDevice::getGap() |
Wolfgang Betz |
130:770ce14d3d15 | 229 | { |
Wolfgang Betz |
130:770ce14d3d15 | 230 | return BlueNRGGap::getInstance(); |
Wolfgang Betz |
130:770ce14d3d15 | 231 | } |
Wolfgang Betz |
130:770ce14d3d15 | 232 | |
Wolfgang Betz |
130:770ce14d3d15 | 233 | const Gap &BlueNRGDevice::getGap() const |
Wolfgang Betz |
130:770ce14d3d15 | 234 | { |
Wolfgang Betz |
130:770ce14d3d15 | 235 | return BlueNRGGap::getInstance(); |
Wolfgang Betz |
130:770ce14d3d15 | 236 | } |
Wolfgang Betz |
130:770ce14d3d15 | 237 | |
Wolfgang Betz |
130:770ce14d3d15 | 238 | /**************************************************************************/ |
Wolfgang Betz |
130:770ce14d3d15 | 239 | /*! |
Wolfgang Betz |
130:770ce14d3d15 | 240 | @brief get reference to GATT server object |
Wolfgang Betz |
130:770ce14d3d15 | 241 | @param[in] void |
Wolfgang Betz |
130:770ce14d3d15 | 242 | @returns GattServer& |
Wolfgang Betz |
130:770ce14d3d15 | 243 | */ |
Wolfgang Betz |
130:770ce14d3d15 | 244 | /**************************************************************************/ |
Wolfgang Betz |
130:770ce14d3d15 | 245 | GattServer &BlueNRGDevice::getGattServer() |
Wolfgang Betz |
130:770ce14d3d15 | 246 | { |
Wolfgang Betz |
130:770ce14d3d15 | 247 | return BlueNRGGattServer::getInstance(); |
Wolfgang Betz |
130:770ce14d3d15 | 248 | } |
Wolfgang Betz |
130:770ce14d3d15 | 249 | |
Wolfgang Betz |
130:770ce14d3d15 | 250 | const GattServer &BlueNRGDevice::getGattServer() const |
Wolfgang Betz |
130:770ce14d3d15 | 251 | { |
Wolfgang Betz |
130:770ce14d3d15 | 252 | return BlueNRGGattServer::getInstance(); |
Wolfgang Betz |
130:770ce14d3d15 | 253 | } |
Wolfgang Betz |
130:770ce14d3d15 | 254 | |
Wolfgang Betz |
130:770ce14d3d15 | 255 | /**************************************************************************/ |
Wolfgang Betz |
130:770ce14d3d15 | 256 | /*! |
Wolfgang Betz |
130:770ce14d3d15 | 257 | @brief shut down the the BLE device |
Wolfgang Betz |
130:770ce14d3d15 | 258 | @param[out] error if any |
Wolfgang Betz |
130:770ce14d3d15 | 259 | */ |
Wolfgang Betz |
130:770ce14d3d15 | 260 | /**************************************************************************/ |
Wolfgang Betz |
130:770ce14d3d15 | 261 | ble_error_t BlueNRGDevice::shutdown(void) { |
Wolfgang Betz |
130:770ce14d3d15 | 262 | return reset(); |
Wolfgang Betz |
130:770ce14d3d15 | 263 | } |
Wolfgang Betz |
130:770ce14d3d15 | 264 | |
Wolfgang Betz |
130:770ce14d3d15 | 265 | /** |
Wolfgang Betz |
130:770ce14d3d15 | 266 | * @brief Reads from BlueNRG SPI buffer and store data into local buffer. |
Wolfgang Betz |
130:770ce14d3d15 | 267 | * @param buffer : Buffer where data from SPI are stored |
Wolfgang Betz |
130:770ce14d3d15 | 268 | * @param buff_size: Buffer size |
Wolfgang Betz |
130:770ce14d3d15 | 269 | * @retval int32_t : Number of read bytes |
Wolfgang Betz |
130:770ce14d3d15 | 270 | */ |
Wolfgang Betz |
130:770ce14d3d15 | 271 | int32_t BlueNRGDevice::spiRead(uint8_t *buffer, uint8_t buff_size) |
Wolfgang Betz |
130:770ce14d3d15 | 272 | { |
Wolfgang Betz |
130:770ce14d3d15 | 273 | uint16_t byte_count; |
Wolfgang Betz |
130:770ce14d3d15 | 274 | uint8_t len = 0; |
Wolfgang Betz |
130:770ce14d3d15 | 275 | uint8_t char_ff = 0xff; |
Wolfgang Betz |
130:770ce14d3d15 | 276 | volatile uint8_t read_char; |
Wolfgang Betz |
130:770ce14d3d15 | 277 | |
Wolfgang Betz |
130:770ce14d3d15 | 278 | uint8_t i = 0; |
Wolfgang Betz |
130:770ce14d3d15 | 279 | volatile uint8_t tmpreg; |
Wolfgang Betz |
130:770ce14d3d15 | 280 | |
Wolfgang Betz |
130:770ce14d3d15 | 281 | uint8_t header_master[HEADER_SIZE] = {0x0b, 0x00, 0x00, 0x00, 0x00}; |
Wolfgang Betz |
130:770ce14d3d15 | 282 | uint8_t header_slave[HEADER_SIZE]; |
Wolfgang Betz |
130:770ce14d3d15 | 283 | |
Wolfgang Betz |
130:770ce14d3d15 | 284 | /* Select the chip */ |
Wolfgang Betz |
130:770ce14d3d15 | 285 | nCS_ = 0; |
Wolfgang Betz |
130:770ce14d3d15 | 286 | |
Wolfgang Betz |
130:770ce14d3d15 | 287 | /* Read the header */ |
Wolfgang Betz |
130:770ce14d3d15 | 288 | for (i = 0; i < 5; i++) |
Wolfgang Betz |
130:770ce14d3d15 | 289 | { |
Wolfgang Betz |
130:770ce14d3d15 | 290 | tmpreg = spi_.write(header_master[i]); |
Wolfgang Betz |
130:770ce14d3d15 | 291 | header_slave[i] = (uint8_t)(tmpreg); |
Wolfgang Betz |
130:770ce14d3d15 | 292 | } |
Wolfgang Betz |
130:770ce14d3d15 | 293 | |
Wolfgang Betz |
130:770ce14d3d15 | 294 | if (header_slave[0] == 0x02) { |
Wolfgang Betz |
130:770ce14d3d15 | 295 | /* device is ready */ |
Wolfgang Betz |
130:770ce14d3d15 | 296 | byte_count = (header_slave[4]<<8)|header_slave[3]; |
Wolfgang Betz |
130:770ce14d3d15 | 297 | |
Wolfgang Betz |
130:770ce14d3d15 | 298 | if (byte_count > 0) { |
Wolfgang Betz |
130:770ce14d3d15 | 299 | |
Wolfgang Betz |
130:770ce14d3d15 | 300 | /* avoid to read more data that size of the buffer */ |
Wolfgang Betz |
130:770ce14d3d15 | 301 | if (byte_count > buff_size){ |
Wolfgang Betz |
130:770ce14d3d15 | 302 | byte_count = buff_size; |
Wolfgang Betz |
130:770ce14d3d15 | 303 | } |
Wolfgang Betz |
130:770ce14d3d15 | 304 | |
Wolfgang Betz |
130:770ce14d3d15 | 305 | for (len = 0; len < byte_count; len++){ |
Wolfgang Betz |
130:770ce14d3d15 | 306 | read_char = spi_.write(char_ff); |
Wolfgang Betz |
130:770ce14d3d15 | 307 | buffer[len] = read_char; |
Wolfgang Betz |
130:770ce14d3d15 | 308 | } |
Wolfgang Betz |
130:770ce14d3d15 | 309 | } |
Wolfgang Betz |
130:770ce14d3d15 | 310 | } |
Wolfgang Betz |
130:770ce14d3d15 | 311 | /* Release CS line to deselect the chip */ |
Wolfgang Betz |
130:770ce14d3d15 | 312 | nCS_ = 1; |
Wolfgang Betz |
130:770ce14d3d15 | 313 | |
Wolfgang Betz |
130:770ce14d3d15 | 314 | // Add a small delay to give time to the BlueNRG to set the IRQ pin low |
Wolfgang Betz |
130:770ce14d3d15 | 315 | // to avoid a useless SPI read at the end of the transaction |
Wolfgang Betz |
130:770ce14d3d15 | 316 | for(volatile int i = 0; i < 2; i++)__NOP(); |
Wolfgang Betz |
130:770ce14d3d15 | 317 | |
Wolfgang Betz |
130:770ce14d3d15 | 318 | #ifdef PRINT_CSV_FORMAT |
Wolfgang Betz |
130:770ce14d3d15 | 319 | if (len > 0) { |
Wolfgang Betz |
130:770ce14d3d15 | 320 | print_csv_time(); |
Wolfgang Betz |
130:770ce14d3d15 | 321 | for (int i=0; i<len; i++) { |
Wolfgang Betz |
130:770ce14d3d15 | 322 | PRINT_CSV(" %02x", buffer[i]); |
Wolfgang Betz |
130:770ce14d3d15 | 323 | } |
Wolfgang Betz |
130:770ce14d3d15 | 324 | PRINT_CSV("\n"); |
Wolfgang Betz |
130:770ce14d3d15 | 325 | } |
Wolfgang Betz |
130:770ce14d3d15 | 326 | #endif |
Wolfgang Betz |
130:770ce14d3d15 | 327 | |
Wolfgang Betz |
130:770ce14d3d15 | 328 | return len; |
Wolfgang Betz |
130:770ce14d3d15 | 329 | } |
Wolfgang Betz |
130:770ce14d3d15 | 330 | |
Wolfgang Betz |
130:770ce14d3d15 | 331 | /** |
Wolfgang Betz |
130:770ce14d3d15 | 332 | * @brief Writes data from local buffer to SPI. |
Wolfgang Betz |
130:770ce14d3d15 | 333 | * @param data1 : First data buffer to be written |
Wolfgang Betz |
130:770ce14d3d15 | 334 | * @param data2 : Second data buffer to be written |
Wolfgang Betz |
130:770ce14d3d15 | 335 | * @param Nb_bytes1: Size of first data buffer to be written |
Wolfgang Betz |
130:770ce14d3d15 | 336 | * @param Nb_bytes2: Size of second data buffer to be written |
Wolfgang Betz |
130:770ce14d3d15 | 337 | * @retval Number of read bytes |
Wolfgang Betz |
130:770ce14d3d15 | 338 | */ |
Wolfgang Betz |
130:770ce14d3d15 | 339 | int32_t BlueNRGDevice::spiWrite(uint8_t* data1, |
Wolfgang Betz |
130:770ce14d3d15 | 340 | uint8_t* data2, uint8_t Nb_bytes1, uint8_t Nb_bytes2) |
Wolfgang Betz |
130:770ce14d3d15 | 341 | { |
Wolfgang Betz |
130:770ce14d3d15 | 342 | int32_t result = 0; |
Wolfgang Betz |
130:770ce14d3d15 | 343 | uint32_t i; |
Wolfgang Betz |
130:770ce14d3d15 | 344 | volatile uint8_t tmpreg; |
Wolfgang Betz |
130:770ce14d3d15 | 345 | |
Wolfgang Betz |
130:770ce14d3d15 | 346 | unsigned char header_master[HEADER_SIZE] = {0x0a, 0x00, 0x00, 0x00, 0x00}; |
Wolfgang Betz |
130:770ce14d3d15 | 347 | unsigned char header_slave[HEADER_SIZE] = {0xaa, 0x00, 0x00, 0x00, 0x00}; |
Wolfgang Betz |
130:770ce14d3d15 | 348 | |
Wolfgang Betz |
130:770ce14d3d15 | 349 | disable_irq(); |
Wolfgang Betz |
130:770ce14d3d15 | 350 | |
Wolfgang Betz |
130:770ce14d3d15 | 351 | /* CS reset */ |
Wolfgang Betz |
130:770ce14d3d15 | 352 | nCS_ = 0; |
Wolfgang Betz |
130:770ce14d3d15 | 353 | |
Wolfgang Betz |
130:770ce14d3d15 | 354 | /* Exchange header */ |
Wolfgang Betz |
130:770ce14d3d15 | 355 | for (i = 0; i < 5; i++) |
Wolfgang Betz |
130:770ce14d3d15 | 356 | { |
Wolfgang Betz |
130:770ce14d3d15 | 357 | tmpreg = spi_.write(header_master[i]); |
Wolfgang Betz |
130:770ce14d3d15 | 358 | header_slave[i] = tmpreg; |
Wolfgang Betz |
130:770ce14d3d15 | 359 | } |
Wolfgang Betz |
130:770ce14d3d15 | 360 | |
Wolfgang Betz |
130:770ce14d3d15 | 361 | if (header_slave[0] == 0x02) { |
Wolfgang Betz |
130:770ce14d3d15 | 362 | /* SPI is ready */ |
Wolfgang Betz |
130:770ce14d3d15 | 363 | if (header_slave[1] >= (Nb_bytes1+Nb_bytes2)) { |
Wolfgang Betz |
130:770ce14d3d15 | 364 | |
Wolfgang Betz |
130:770ce14d3d15 | 365 | /* Buffer is big enough */ |
Wolfgang Betz |
130:770ce14d3d15 | 366 | for (i = 0; i < Nb_bytes1; i++) { |
Wolfgang Betz |
130:770ce14d3d15 | 367 | spi_.write(*(data1 + i)); |
Wolfgang Betz |
130:770ce14d3d15 | 368 | } |
Wolfgang Betz |
130:770ce14d3d15 | 369 | for (i = 0; i < Nb_bytes2; i++) { |
Wolfgang Betz |
130:770ce14d3d15 | 370 | spi_.write(*(data2 + i)); |
Wolfgang Betz |
130:770ce14d3d15 | 371 | } |
Wolfgang Betz |
130:770ce14d3d15 | 372 | } else { |
Wolfgang Betz |
130:770ce14d3d15 | 373 | /* Buffer is too small */ |
Wolfgang Betz |
130:770ce14d3d15 | 374 | result = -2; |
Wolfgang Betz |
130:770ce14d3d15 | 375 | } |
Wolfgang Betz |
130:770ce14d3d15 | 376 | } else { |
Wolfgang Betz |
130:770ce14d3d15 | 377 | /* SPI is not ready */ |
Wolfgang Betz |
130:770ce14d3d15 | 378 | result = -1; |
Wolfgang Betz |
130:770ce14d3d15 | 379 | } |
Wolfgang Betz |
130:770ce14d3d15 | 380 | |
Wolfgang Betz |
130:770ce14d3d15 | 381 | /* Release CS line */ |
Wolfgang Betz |
130:770ce14d3d15 | 382 | //HAL_GPIO_WritePin(BNRG_SPI_CS_PORT, BNRG_SPI_CS_PIN, GPIO_PIN_SET); |
Wolfgang Betz |
130:770ce14d3d15 | 383 | nCS_ = 1; |
Wolfgang Betz |
130:770ce14d3d15 | 384 | |
Wolfgang Betz |
130:770ce14d3d15 | 385 | enable_irq(); |
Wolfgang Betz |
130:770ce14d3d15 | 386 | |
Wolfgang Betz |
130:770ce14d3d15 | 387 | return result; |
Wolfgang Betz |
130:770ce14d3d15 | 388 | } |
Wolfgang Betz |
130:770ce14d3d15 | 389 | |
Wolfgang Betz |
130:770ce14d3d15 | 390 | bool BlueNRGDevice::dataPresent() |
Wolfgang Betz |
130:770ce14d3d15 | 391 | { |
Wolfgang Betz |
130:770ce14d3d15 | 392 | return (irq_ == 1); |
Wolfgang Betz |
130:770ce14d3d15 | 393 | } |
Wolfgang Betz |
130:770ce14d3d15 | 394 | |
Wolfgang Betz |
130:770ce14d3d15 | 395 | void BlueNRGDevice::disable_irq() |
Wolfgang Betz |
130:770ce14d3d15 | 396 | { |
Wolfgang Betz |
130:770ce14d3d15 | 397 | irq_.disable_irq(); |
Wolfgang Betz |
130:770ce14d3d15 | 398 | } |
Wolfgang Betz |
130:770ce14d3d15 | 399 | |
Wolfgang Betz |
130:770ce14d3d15 | 400 | void BlueNRGDevice::enable_irq() |
Wolfgang Betz |
130:770ce14d3d15 | 401 | { |
Wolfgang Betz |
130:770ce14d3d15 | 402 | irq_.enable_irq(); |
Wolfgang Betz |
130:770ce14d3d15 | 403 | } |