Enrico Marinoni / Mbed 2 deprecated Spirit1_TX_DoorBell

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /**
00002 
00003  Wireless Door Bell based on SPIRIT1 & NUCLEO boards
00004  TX TX TX
00005  
00006  Date:  Jan 2018
00007  Version: 1.0
00008  More info are here: 
00009  http://www.emcu.eu/wireless-doorbell-based-on-spirit1-nucleo-boards/
00010  
00011  
00012  ******************************************************************************
00013  * @file    main.cpp
00014  * @author  Rosarium PILA, STMicroelectronics
00015  * @version V1.0.0
00016  * @date    June 19th, 2017
00017  * @brief   mbed test application for the STMicroelectronics X-NUCLEO-IDB01A4/5
00018  *          Spirit1 Expansion Board
00019  ******************************************************************************
00020  * @attention
00021  *
00022  * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
00023  *
00024  * Redistribution and use in source and binary forms, with or without modification,
00025  * are permitted provided that the following conditions are met:
00026  *   1. Redistributions of source code must retain the above copyright notice,
00027  *      this list of conditions and the following disclaimer.
00028  *   2. Redistributions in binary form must reproduce the above copyright notice,
00029  *      this list of conditions and the following disclaimer in the documentation
00030  *      and/or other materials provided with the distribution.
00031  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00032  *      may be used to endorse or promote products derived from this software
00033  *      without specific prior written permission.
00034  *
00035  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00036  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00037  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00038  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00039  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00040  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00041  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00042  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00043  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00044  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00045  *
00046  ******************************************************************************
00047  */
00048 
00049 #include "mbed.h"
00050 #include "SimpleSpirit1.h"
00051 
00052 #include <stdio.h>
00053 #include <string.h>
00054 
00055 #define TEST_STR_LEN (32) 
00056 static char 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'};
00057 static char 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'};
00058           
00059 static SimpleSpirit1 &myspirit = SimpleSpirit1::CreateInstance(D11, D12, D3, D9, D10, D2); /* (SPI_CLK) = (D13:PA5:resistorR7 --> D3:PB3:resistorR4)*/
00060 
00061 static volatile bool rx_done_flag = false; 
00062 static volatile bool tx_done_flag = false; 
00063 static volatile bool send_data_flag = false;
00064 
00065 static DigitalOut TestLED(D5);         /* LED of IDS01A4/5 */
00066 static InterruptIn event(USER_BUTTON); /* Interrupt event to give userinterface to send pkt. */
00067 
00068 /**
00069  * @brief  callback_func
00070  * @param  callback event
00071  * @retval None.
00072  */
00073 static void callback_func(int event) 
00074 {
00075   // printf("\n\r\n\r Interrupt \n\r\n\r");
00076   
00077   if(event == SimpleSpirit1::RX_DONE) 
00078   {              
00079     rx_done_flag = 1;
00080   }
00081   else if (event == SimpleSpirit1::TX_DONE) 
00082   {
00083     tx_done_flag = 1;
00084   }
00085   wait_ms(60);  // Anti debounce on Blue Button
00086 }
00087 
00088 /**
00089  * @brief  set_send_data_flag
00090  * @param  None
00091  * @retval None
00092  */
00093 static void set_send_data_flag(void)
00094 {  
00095   send_data_flag = 1 ;
00096 }
00097 
00098 /**
00099  * @brief  send_data
00100  * @param  None
00101  * @retval None
00102  */
00103 static void send_data(void)
00104 {
00105   printf("\r\n***Sending a packet***");  
00106   
00107   while(myspirit.is_receiving()); /* wait for ongoing RX ends */
00108  
00109   size_t curr_len = strlen((const char*)send_buf) + 1;
00110   myspirit.send(send_buf, curr_len);
00111 }
00112 
00113 /**
00114  * @brief  read_rcvd_data
00115  * @param  None
00116  * @retval None
00117  */
00118 static void read_rcvd_data(void)
00119 {  
00120  
00121   for(unsigned int flush_count = 0; flush_count < TEST_STR_LEN; flush_count++) read_buf[flush_count] = 0 ;/* clear the read buffer */
00122       
00123       int ret = myspirit.read(read_buf, sizeof(read_buf));
00124       
00125       // TestLED = !TestLED;     /* Toggle LED at the receiver */
00126       
00127       if(ret == 0) 
00128       {
00129         printf("\nNothing to read\n\r");
00130         return;
00131       }
00132       printf("\r\n***Received a packet***\r\n\rReceived string = '%s' (len=%d) \n\r", read_buf, ret); 
00133       
00134       wait_ms(100);
00135 }
00136 
00137 
00138 /**
00139  * @brief  main routine
00140  * @param  None
00141  * @retval int
00142  */
00143 int main() 
00144 {  
00145   TestLED = 0;                  /* LED off */
00146   
00147   myspirit.attach_irq_callback(callback_func);
00148   
00149   myspirit.on();
00150   
00151   printf("\n\r************** TX mode Spirit1 (X-NUCLEO-IDS01A4/5) ************** \r\n");
00152   printf("\nPress User Button on one of the TX kit and on the RX kit the LED D1 must toggle\n\r\n");
00153   
00154   event.rise(&set_send_data_flag); /*User button interrupt trigger to set send data flag */
00155   
00156   while(1) 
00157   {
00158     __WFE(); /* low power in idle condition., waiting for an event */
00159     
00160     if(rx_done_flag) 
00161     {
00162       rx_done_flag = false; 
00163       read_rcvd_data();      
00164       
00165       // Test Test if the TX string and RX string are equals
00166       if(strcmp(send_buf,read_buf) == 0)
00167       {
00168         printf("RxTx OK \n\r");
00169         TestLED = !TestLED;     /* Toggle LED at the receiver */
00170       }
00171       else
00172         printf("RxTx FAIL \n\r");          
00173     }
00174     
00175     else if (send_data_flag)
00176     {
00177       send_data_flag = false;
00178       send_data();     
00179     }
00180     
00181     else if (tx_done_flag)
00182     {
00183       tx_done_flag = false;
00184       printf("\r\n***Packet sent ***\r\nSent string ='%s' (len=%d)\n\r", send_buf, strlen((const char*)send_buf) + 1);
00185     }
00186     
00187     wait_ms(200);  // Anti debounce on Blue Button
00188   }
00189   
00190   /* unreachable */
00191   // myspirit.off();
00192   // return 0;
00193 }