Dependencies:   BLE_API mbed nRF51822

Fork of eco_Labs_ble_Client by Happiest

Revision:
11:b8e687d97537
Child:
12:c7a8a7fe76fd
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/spi_slave.cpp	Thu Oct 06 09:42:38 2016 +0000
@@ -0,0 +1,167 @@
+/**
+  ******************************************************************************
+  * @file    ble_uart.cpp
+  * @author  Happiesstminds Firmware Team
+  * @version v1.0
+  * @date    4-Oct-2016
+  * @brief   
+  *
+  ******************************************************************************
+  * @attention
+  *  
+  *
+  ******************************************************************************
+  */
+
+/******************************************************************************/
+/* Include Files*/
+/******************************************************************************/
+#include "mbed.h"
+#include "spi_slave.h"
+#include "ble_types.h"
+#include "ble_msg_handler.h"
+
+
+/******************************************************************************/
+/* Local Defines                                                              */
+/******************************************************************************/
+#define SPI_DUMMY_BYTE      0x00
+
+#define SPI_TX_BUF_SIZE     64
+#define SPI_TX_EMPTY_DATA   0xFF
+
+/******************************************************************************/
+/* Extern Variable/functions                                                  */
+/******************************************************************************/
+extern bool toggle_led_flag;
+extern void spi_rx_Data_Callback(uint8_t *rx_data, uint8_t len);
+
+/******************************************************************************/
+/* Static Variables                                                           */
+/******************************************************************************/
+DigitalOut led2(LED1); 
+SPISlave device(P0_9, P0_11,P0_8, P0_10); // mosi, miso, sclk, ssel
+DigitalOut  spi_rqst(P0_28);
+DigitalOut  spi_rdy(P0_29); 
+
+static spi_data_ready_callback_t  spi_data_ready_cb; 
+
+uint8_t spi_tx_buf[SPI_TX_BUF_SIZE];
+uint8_t spi_tx_ptr;
+uint8_t spi_bytes_to_send;
+
+/******************************************************************************/
+/* Global Functions                                                              */
+/******************************************************************************/
+/**
+ * @brief  Enable Interrupt line
+ * @param  None        
+ * @retval None
+ */
+void enable_interrupt_line(bool status )
+{
+    if(status)
+    {
+        spi_rqst =1;
+        spi_rdy =1;
+    }
+    else
+    {
+        spi_rqst =0;
+        spi_rdy =0;
+    }
+        
+}
+/**
+ * @brief  Initializes the Nano BLE SPI slave buffer init
+ * @param  None        
+ * @retval None
+ */
+void spi_buf_init(void)
+{   
+    spi_tx_ptr = 0;
+    spi_bytes_to_send = 0;
+    memset(spi_tx_buf, SPI_TX_EMPTY_DATA, SPI_TX_BUF_SIZE);
+}
+
+/**
+ * @brief  Receive data from Master
+ * @param  
+ * @retval 
+ */
+void spi_slave_rx_data(void)
+{    
+    uint8_t rx_buf[20];
+    uint8_t byte_cnt = 0;
+    uint8_t send_ptr = 0;
+    
+    device.reply(0xFF);
+    while(1)
+    {
+        if(device.receive()) 
+        {            
+            rx_buf[byte_cnt] = device.read();
+            
+            if (spi_bytes_to_send > 0) {
+                //device.reply(rx_buf[byte_cnt]);
+                device.reply(spi_tx_buf[send_ptr]);                
+                if (spi_tx_ptr++ > SPI_TX_BUF_SIZE) {
+                    spi_tx_ptr= 0;
+                }
+                spi_bytes_to_send--;
+            } else {
+                device.reply(rx_buf[byte_cnt]);
+            } 
+            
+            if ((byte_cnt >= 4) && (rx_buf[byte_cnt] == BLE_EOT_CMD)) {                
+                /* Check for End of Frame to make sure complete packet has been received */
+                spi_data_ready_cb(rx_buf, byte_cnt);
+                byte_cnt = 0;
+            }
+            byte_cnt++;
+        }
+    } 
+}
+
+/**
+ * @brief  Send data to Master
+ * @param  
+ * @retval 
+ */
+void spi_slave_tx_data(uint8_t *tx_buf, uint8_t len)
+{
+#if 0    
+    /* TODO: make the GPIO line high */
+ //   enable_interrupt_line();    
+    
+    /* Copy the buffer into local buffer */
+    memcpy(&spi_tx_buf[spi_tx_ptr], tx_buf, len);
+    spi_bytes_to_send += len; 
+    spi_tx_ptr += len; 
+    if (spi_tx_ptr > SPI_TX_BUF_SIZE) {
+        spi_tx_ptr = 0;    
+    }
+#endif    
+}
+
+/**
+ * @brief  Function used to register spi data receive callback 
+ * @param  none
+ * @retval none
+ */
+void spi_data_rx_cb_register(spi_data_ready_callback_t data_rx_callback)
+{
+    spi_data_ready_cb = data_rx_callback;
+}
+
+
+void init_interrupt_line(void )
+{
+    spi_rqst =0;
+    spi_rdy =0;
+}
+
+
+/******************************************************************************/
+/* END OF FILE                                                                */
+/******************************************************************************/