HelloWorld_IDS01A4 mbed application for Spirit1 P2P demo.

Dependencies:   mbed

Fork of HelloWorld_IDS01A5_Program by rosarium pila

mbed 2.0 (a.k.a classic) HelloWorld example for Spirit1 to transfer data from one node to another.
To send the predefined data packet from the sender to the receiver, user needs to press User Button on the board. The LED on the receiver expansion board will toggle on reception of data successfully.
Data packets sent and received can be seen on USB serial terminal.

EVALUATION BOARDS RF FREQUENCY DESCRIPTION :
X-NUCLEO-IDS01A4 868 MHz 868 MHz RF expansion board based on SPIRIT1

Revision:
4:cae1fa054b86
Parent:
3:29bbb772ee62
Child:
5:a65ed26b0f98
--- a/main.cpp	Mon Jun 19 08:52:28 2017 +0000
+++ b/main.cpp	Mon Jun 19 10:25:09 2017 +0000
@@ -4,7 +4,7 @@
  * @author  Rosarium PILA, STMicroelectronics
  * @version V1.0.0
  * @date    June 19th, 2017
- * @brief   mbed Hello World test application for the STMicroelectronics X-NUCLEO-IDB01A4/5
+ * @brief   mbed test application for the STMicroelectronics X-NUCLEO-IDB01A4/5
  *          Spirit1 Expansion Board
  ******************************************************************************
  * @attention
@@ -43,15 +43,18 @@
 static uint8_t send_buf[TEST_STR_LEN] ={'S','P','I','R','I','T','1',' ','H','E','L','L','O',' ','W','O','R','L','D',' ','P','2','P',' ','D','E','M','O'};
 static uint8_t read_buf[TEST_STR_LEN] ={'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
           
-static SimpleSpirit1 &myspirit = SimpleSpirit1::CreateInstance(D11, D12, D3, D9, D10, D2); //RPi: (SPI_CLK) = (D13:PA5:resistorR7 --> D3:PB3:resistorR4)
+static SimpleSpirit1 &myspirit = SimpleSpirit1::CreateInstance(D11, D12, D3, D9, D10, D2); /* (SPI_CLK) = (D13:PA5:resistorR7 --> D3:PB3:resistorR4)*/
 
 volatile bool rx_done_flag = 0; 
 volatile bool tx_done_flag = 0; 
+volatile bool send_data_flag = 0;
 
 static int curr_len = 0;
-unsigned char myValue = 0;
 
-InterruptIn event(USER_BUTTON); //RPi : //Added interrupt event to give userinterface to send pkt.
+DigitalOut TestLED = D5;      /* LED of IDS01A4/5 */
+unsigned char led_toggle = 0;
+
+InterruptIn event(USER_BUTTON); /* Interrupt event to give userinterface to send pkt. */
 
 /**
  * @brief  callback_func
@@ -70,6 +73,15 @@
   }
 }
 
+/**
+ * @brief  set_send_data_flag
+ * @param  None
+ * @retval None
+ */
+void set_send_data_flag(void)
+{  
+  send_data_flag = 1 ;
+}
 
 /**
  * @brief  send_data
@@ -80,11 +92,36 @@
 {  
   printf("\r\n***Sending a packet***\r\n");  
   
-  while(myspirit.is_receiving()); // wait for ongoing RX ends
+  while(myspirit.is_receiving()); /* wait for ongoing RX ends */
  
   curr_len = strlen((const char*)send_buf) + 1;
   myspirit.send(send_buf, curr_len);
 }
+
+/**
+ * @brief  read_rcvd_data
+ * @param  None
+ * @retval None
+ */
+void read_rcvd_data(void)
+{  
+ 
+  for(unsigned int flush_count = 0; flush_count < TEST_STR_LEN; flush_count++) read_buf[flush_count] = 0 ;/* clear the read buffer */
+      
+      int ret = myspirit.read(read_buf, sizeof(read_buf));
+      
+      led_toggle = 1 - led_toggle; 
+      TestLED = led_toggle;     /* Toggle LED at the receiver */
+      
+      if(ret == 0) 
+      {
+        printf("\nNothing to read\n\r");
+        return;
+      }
+      printf("\r\n***Received a packet***\r\n\rReceived string = '%s' (len=%d) \n\r", read_buf, ret); 
+}
+
+
 /**
  * @brief  main routine
  * @param  None
@@ -92,8 +129,7 @@
  */
 int main() 
 {  
-  DigitalOut TestLED = D5;      //RPi : LED of IDS01A4/5
-  TestLED = 0;                  //RPi : LED off
+  TestLED = 0;                  /* LED off */
   
   myspirit.attach_irq_callback(callback_func);
   
@@ -102,34 +138,28 @@
   printf("\n**************HelloWorld mbed demo for Spirit1 (X-NUCLEO-IDS01A4/5)**************\r\n");
   printf("\nPress User Button on one of the two boards to send a packaet to the other and the LED D1 on the receiver X-NUCLEO-IDS01A4/5 should toggle \n\r\n");
   
-  event.rise(&send_data); //RPi: User button interrupt trigger to send data
+  event.rise(&set_send_data_flag); /*User button interrupt trigger to set send data flag */
   
   while(1) 
   {
-    __WFE(); // low power in idle condition., waiting for an event
+    __WFE(); /* low power in idle condition., waiting for an event */
     
     if(rx_done_flag) 
     {
       rx_done_flag = 0; 
-     
-      for(unsigned int flush_count = 0; flush_count < TEST_STR_LEN; flush_count++) read_buf[flush_count] = 0 ;//RPi:clear the read buffer
-      
-      int ret = myspirit.read(read_buf, sizeof(read_buf));
-      
-      myValue = 1 - myValue; 
-      TestLED = myValue;     //RPi: Toggle LED at the receiver
-      
-      if(ret == 0) {
-        printf("\nNothing to read\n\r");
-        continue;
-      }
-      printf("\r\n***Received a packet***\r\n\rReceived string = '%s' (len=%d) \n\r", read_buf, ret); 
+      read_rcvd_data();
+    }
+    
+    else if (send_data_flag)
+    {
+      send_data_flag = 0;
+      send_data();
     }
     
     else if (tx_done_flag)
     {
       tx_done_flag = 0;
-      printf("\r\n***Packet sent***\r\nSent string ='%s' (len=%d)\n\r", send_buf, strlen((const char*)send_buf) + 1);
+      printf("\r\n***Packet sent ***\r\nSent string ='%s' (len=%d)\n\r", send_buf, strlen((const char*)send_buf) + 1);
     }
   }