ver:init

Committer:
iv123
Date:
Sun Jun 18 16:10:28 2017 +0000
Revision:
0:88b85febcb45
Initial commit

Who changed what in which revision?

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