gjhn

Dependents:   BLE_GENERALE

Fork of X_NUCLEO_IDB0XA1 by ST

Branch:
117ed4c55629877d7cb3d79c4bcb24e7da5bcab0
Revision:
278:a5209d8cfd61
Parent:
273:00205952d841
Child:
293:5a1f577bf92f
--- a/source/BlueNRGDevice.cpp	Thu Sep 15 10:51:41 2016 +0100
+++ b/source/BlueNRGDevice.cpp	Thu Sep 15 10:51:43 2016 +0100
@@ -16,7 +16,7 @@
 
 /**
   ******************************************************************************
-  * @file    BlueNRGDevice.cpp 
+  * @file    BlueNRGDevice.cpp
   * @author  STMicroelectronics
   * @brief   Implementation of BLEDeviceInstanceBase
   ******************************************************************************
@@ -30,14 +30,18 @@
   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
   *
   * <h2><center>&copy; COPYRIGHT 2013 STMicroelectronics</center></h2>
-  */ 
-  
+  */
+
 /** @defgroup BlueNRGDevice
  *  @brief BlueNRG BLE_API Device Adaptation
  *  @{
  */
  
-#include "mbed-drivers/mbed.h"
+#ifdef YOTTA_CFG_MBED_OS
+    #include "mbed-drivers/mbed.h"
+#else
+    #include "mbed.h"
+#endif
 #include "BlueNRGDevice.h"
 #include "BlueNRGGap.h"
 #include "BlueNRGGattServer.h"
@@ -97,14 +101,14 @@
                              PinName sck,
                              PinName cs,
                              PinName rst,
-                             PinName irq) : 
+                             PinName irq) :
 	isInitialized(false), spi_(mosi, miso, sck), nCS_(cs), rst_(rst), irq_(irq)
 {
     // Setup the spi for 8 bit data, low clock polarity,
     // 1-edge phase, with an 8MHz clock rate
     spi_.format(8, 0);
     spi_.frequency(8000000);
-	
+
     // Deselect the BlueNRG chip by keeping its nCS signal high
     nCS_ = 1;
 
@@ -154,7 +158,7 @@
 int BlueNRGDevice::updateFirmware(const uint8_t *fw_image, uint32_t fw_size)
 {
 	int status = program_device(fw_image, fw_size);
-	
+
 	return (status);
 }
 
@@ -184,14 +188,14 @@
 
 	// Init the BlueNRG/BlueNRG-MS stack
 	btleInit();
-	
+
 	isInitialized = true;
 	BLE::InitializationCompleteCallbackContext context = {
 	        BLE::Instance(instanceID),
 	        BLE_ERROR_NONE
 	};
 	callback.call(&context);
-    
+
 	return BLE_ERROR_NONE;
 }
 
@@ -218,9 +222,9 @@
 }
 
 /*!
-  @brief  Wait for any BLE Event like BLE Connection, Read Request etc.    
+  @brief  Wait for any BLE Event like BLE Connection, Read Request etc.
   @param[in] void
-  @returns    char *      
+  @returns    char *
 */
 void BlueNRGDevice::waitForEvent(void)
 {
@@ -228,7 +232,7 @@
 
 	do {
         bluenrgDeviceInstance.processEvents();
-		
+
 		if(must_return) return;
 
 		__WFE(); /* it is recommended that SEVONPEND in the
@@ -237,8 +241,8 @@
 				       that conrol is given back to main loop before next WFE */
 	} while(true);
 
-} 
- 
+}
+
 /*!
     @brief  get GAP version
     @brief Get the BLE stack version information
@@ -255,15 +259,15 @@
 /*!
     @brief  get reference to GAP object
     @param[in] void
-    @returns    Gap&      
+    @returns    Gap&
 */
 /**************************************************************************/
-Gap        &BlueNRGDevice::getGap()        
+Gap        &BlueNRGDevice::getGap()
 {
     return BlueNRGGap::getInstance();
 }
 
-const Gap  &BlueNRGDevice::getGap() const        
+const Gap  &BlueNRGDevice::getGap() const
 {
     return BlueNRGGap::getInstance();
 }
@@ -272,10 +276,10 @@
 /*!
     @brief  get reference to GATT server object
     @param[in] void
-    @returns    GattServer&    
+    @returns    GattServer&
 */
 /**************************************************************************/
-GattServer &BlueNRGDevice::getGattServer() 
+GattServer &BlueNRGDevice::getGattServer()
 {
     return BlueNRGGattServer::getInstance();
 }
@@ -284,7 +288,7 @@
 {
     return BlueNRGGattServer::getInstance();
 }
- 
+
 /**************************************************************************/
 /*!
     @brief  shut down the BLE device
@@ -325,7 +329,7 @@
     return BLE_ERROR_NONE;
 
 }
-																							
+
 /**
  * @brief  Reads from BlueNRG SPI buffer and store data into local buffer.
  * @param  buffer   : Buffer where data from SPI are stored
@@ -338,7 +342,7 @@
   uint8_t len = 0;
   uint8_t char_ff = 0xff;
   volatile uint8_t read_char;
-	
+
 	uint8_t i = 0;
 	volatile uint8_t tmpreg;
 
@@ -347,38 +351,38 @@
 
   /* Select the chip */
   nCS_ = 0;
-	
-  /* Read the header */  
+
+  /* Read the header */
   for (i = 0; i < 5; i++)
-  { 
+  {
 		tmpreg = spi_.write(header_master[i]);
 		header_slave[i] = (uint8_t)(tmpreg);
-  } 
-	
+  }
+
   if (header_slave[0] == 0x02) {
     /* device is ready */
     byte_count = (header_slave[4]<<8)|header_slave[3];
-  
+
     if (byte_count > 0) {
-  
+
       /* avoid to read more data that size of the buffer */
       if (byte_count > buff_size){
         byte_count = buff_size;
       }
-  
+
       for (len = 0; len < byte_count; len++){
         read_char = spi_.write(char_ff);
 				buffer[len] = read_char;
       }
-    }    
+    }
   }
   /* Release CS line to deselect the chip */
   nCS_ = 1;
-	
+
   // Add a small delay to give time to the BlueNRG to set the IRQ pin low
   // to avoid a useless SPI read at the end of the transaction
   for(volatile int i = 0; i < 2; i++)__NOP();
-  
+
 #ifdef PRINT_CSV_FORMAT
   if (len > 0) {
     print_csv_time();
@@ -388,8 +392,8 @@
     PRINT_CSV("\n");
   }
 #endif
-  
-  return len;   
+
+  return len;
 }
 
 /**
@@ -402,11 +406,11 @@
  */
 int32_t BlueNRGDevice::spiWrite(uint8_t* data1,
 				uint8_t* data2, uint8_t Nb_bytes1, uint8_t Nb_bytes2)
-{  
+{
 	int32_t result = 0;
 	uint32_t i;
 	volatile uint8_t tmpreg;
-    
+
   unsigned char header_master[HEADER_SIZE] = {0x0a, 0x00, 0x00, 0x00, 0x00};
   unsigned char header_slave[HEADER_SIZE]  = {0xaa, 0x00, 0x00, 0x00, 0x00};
 
@@ -415,24 +419,24 @@
   /* CS reset */
   nCS_ = 0;
 
-  /* Exchange header */  
+  /* Exchange header */
   for (i = 0; i < 5; i++)
-  { 
+  {
 		tmpreg = spi_.write(header_master[i]);
 		header_slave[i] = tmpreg;
-  } 
-	
+  }
+
   if (header_slave[0] == 0x02) {
     /* SPI is ready */
     if (header_slave[1] >= (Nb_bytes1+Nb_bytes2)) {
-  
+
       /*  Buffer is big enough */
 			for (i = 0; i < Nb_bytes1; i++) {
 				spi_.write(*(data1 + i));
       }
       for (i = 0; i < Nb_bytes2; i++) {
 				spi_.write(*(data2 + i));
-      }			
+      }
     } else {
       /* Buffer is too small */
       result = -2;
@@ -441,13 +445,13 @@
     /* SPI is not ready */
     result = -1;
   }
-    
+
   /* Release CS line */
   //HAL_GPIO_WritePin(BNRG_SPI_CS_PORT, BNRG_SPI_CS_PIN, GPIO_PIN_SET);
   nCS_ = 1;
-			
+
   enable_irq();
-    
+
   return result;
 }