ST / Mbed 2 deprecated HelloWorld_IDS01A4

Dependencies:   mbed

Fork of HelloWorld_IDS01A5_Program by rosarium pila

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