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

Committer:
rosarium
Date:
Mon Jun 19 10:25:09 2017 +0000
Revision:
4:cae1fa054b86
Parent:
3:29bbb772ee62
Child:
5:a65ed26b0f98
updated main.cpp with new flag for sending data

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rosarium 0:2ddc0583bcec 1 /**
rosarium 0:2ddc0583bcec 2 ******************************************************************************
rosarium 0:2ddc0583bcec 3 * @file main.cpp
rosarium 0:2ddc0583bcec 4 * @author Rosarium PILA, STMicroelectronics
rosarium 0:2ddc0583bcec 5 * @version V1.0.0
rosarium 3:29bbb772ee62 6 * @date June 19th, 2017
rosarium 4:cae1fa054b86 7 * @brief mbed test application for the STMicroelectronics X-NUCLEO-IDB01A4/5
rosarium 0:2ddc0583bcec 8 * Spirit1 Expansion Board
rosarium 0:2ddc0583bcec 9 ******************************************************************************
rosarium 0:2ddc0583bcec 10 * @attention
rosarium 0:2ddc0583bcec 11 *
rosarium 0:2ddc0583bcec 12 * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
rosarium 0:2ddc0583bcec 13 *
rosarium 0:2ddc0583bcec 14 * Redistribution and use in source and binary forms, with or without modification,
rosarium 0:2ddc0583bcec 15 * are permitted provided that the following conditions are met:
rosarium 0:2ddc0583bcec 16 * 1. Redistributions of source code must retain the above copyright notice,
rosarium 0:2ddc0583bcec 17 * this list of conditions and the following disclaimer.
rosarium 0:2ddc0583bcec 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
rosarium 0:2ddc0583bcec 19 * this list of conditions and the following disclaimer in the documentation
rosarium 0:2ddc0583bcec 20 * and/or other materials provided with the distribution.
rosarium 0:2ddc0583bcec 21 * 3. Neither the name of STMicroelectronics nor the names of its contributors
rosarium 0:2ddc0583bcec 22 * may be used to endorse or promote products derived from this software
rosarium 0:2ddc0583bcec 23 * without specific prior written permission.
rosarium 0:2ddc0583bcec 24 *
rosarium 0:2ddc0583bcec 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
rosarium 0:2ddc0583bcec 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
rosarium 0:2ddc0583bcec 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
rosarium 0:2ddc0583bcec 28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
rosarium 0:2ddc0583bcec 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
rosarium 0:2ddc0583bcec 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
rosarium 0:2ddc0583bcec 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
rosarium 0:2ddc0583bcec 32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
rosarium 0:2ddc0583bcec 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
rosarium 0:2ddc0583bcec 34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
rosarium 0:2ddc0583bcec 35 *
rosarium 0:2ddc0583bcec 36 ******************************************************************************
rosarium 0:2ddc0583bcec 37 */
rosarium 0:2ddc0583bcec 38
rosarium 0:2ddc0583bcec 39 #include "mbed.h"
rosarium 0:2ddc0583bcec 40 #include "SimpleSpirit1.h"
rosarium 0:2ddc0583bcec 41
rosarium 1:cf2e2f85bf8d 42 #define TEST_STR_LEN (32)
rosarium 0:2ddc0583bcec 43 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'};
rosarium 0:2ddc0583bcec 44 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'};
rosarium 1:cf2e2f85bf8d 45
rosarium 4:cae1fa054b86 46 static SimpleSpirit1 &myspirit = SimpleSpirit1::CreateInstance(D11, D12, D3, D9, D10, D2); /* (SPI_CLK) = (D13:PA5:resistorR7 --> D3:PB3:resistorR4)*/
rosarium 0:2ddc0583bcec 47
rosarium 1:cf2e2f85bf8d 48 volatile bool rx_done_flag = 0;
rosarium 1:cf2e2f85bf8d 49 volatile bool tx_done_flag = 0;
rosarium 4:cae1fa054b86 50 volatile bool send_data_flag = 0;
rosarium 0:2ddc0583bcec 51
rosarium 0:2ddc0583bcec 52 static int curr_len = 0;
rosarium 0:2ddc0583bcec 53
rosarium 4:cae1fa054b86 54 DigitalOut TestLED = D5; /* LED of IDS01A4/5 */
rosarium 4:cae1fa054b86 55 unsigned char led_toggle = 0;
rosarium 4:cae1fa054b86 56
rosarium 4:cae1fa054b86 57 InterruptIn event(USER_BUTTON); /* Interrupt event to give userinterface to send pkt. */
rosarium 0:2ddc0583bcec 58
rosarium 1:cf2e2f85bf8d 59 /**
rosarium 1:cf2e2f85bf8d 60 * @brief callback_func
rosarium 1:cf2e2f85bf8d 61 * @param callback event
rosarium 1:cf2e2f85bf8d 62 * @retval None.
rosarium 1:cf2e2f85bf8d 63 */
rosarium 0:2ddc0583bcec 64 static void callback_func(int event)
rosarium 0:2ddc0583bcec 65 {
rosarium 1:cf2e2f85bf8d 66 if(event == SimpleSpirit1::RX_DONE)
rosarium 1:cf2e2f85bf8d 67 {
rosarium 1:cf2e2f85bf8d 68 rx_done_flag = 1;
rosarium 1:cf2e2f85bf8d 69 }
rosarium 1:cf2e2f85bf8d 70 else if (event == SimpleSpirit1::TX_DONE)
rosarium 1:cf2e2f85bf8d 71 {
rosarium 1:cf2e2f85bf8d 72 tx_done_flag = 1;
rosarium 1:cf2e2f85bf8d 73 }
rosarium 0:2ddc0583bcec 74 }
rosarium 0:2ddc0583bcec 75
rosarium 4:cae1fa054b86 76 /**
rosarium 4:cae1fa054b86 77 * @brief set_send_data_flag
rosarium 4:cae1fa054b86 78 * @param None
rosarium 4:cae1fa054b86 79 * @retval None
rosarium 4:cae1fa054b86 80 */
rosarium 4:cae1fa054b86 81 void set_send_data_flag(void)
rosarium 4:cae1fa054b86 82 {
rosarium 4:cae1fa054b86 83 send_data_flag = 1 ;
rosarium 4:cae1fa054b86 84 }
rosarium 1:cf2e2f85bf8d 85
rosarium 1:cf2e2f85bf8d 86 /**
rosarium 1:cf2e2f85bf8d 87 * @brief send_data
rosarium 1:cf2e2f85bf8d 88 * @param None
rosarium 1:cf2e2f85bf8d 89 * @retval None
rosarium 1:cf2e2f85bf8d 90 */
rosarium 0:2ddc0583bcec 91 void send_data(void)
rosarium 1:cf2e2f85bf8d 92 {
rosarium 1:cf2e2f85bf8d 93 printf("\r\n***Sending a packet***\r\n");
rosarium 0:2ddc0583bcec 94
rosarium 4:cae1fa054b86 95 while(myspirit.is_receiving()); /* wait for ongoing RX ends */
rosarium 1:cf2e2f85bf8d 96
rosarium 0:2ddc0583bcec 97 curr_len = strlen((const char*)send_buf) + 1;
rosarium 0:2ddc0583bcec 98 myspirit.send(send_buf, curr_len);
rosarium 0:2ddc0583bcec 99 }
rosarium 4:cae1fa054b86 100
rosarium 4:cae1fa054b86 101 /**
rosarium 4:cae1fa054b86 102 * @brief read_rcvd_data
rosarium 4:cae1fa054b86 103 * @param None
rosarium 4:cae1fa054b86 104 * @retval None
rosarium 4:cae1fa054b86 105 */
rosarium 4:cae1fa054b86 106 void read_rcvd_data(void)
rosarium 4:cae1fa054b86 107 {
rosarium 4:cae1fa054b86 108
rosarium 4:cae1fa054b86 109 for(unsigned int flush_count = 0; flush_count < TEST_STR_LEN; flush_count++) read_buf[flush_count] = 0 ;/* clear the read buffer */
rosarium 4:cae1fa054b86 110
rosarium 4:cae1fa054b86 111 int ret = myspirit.read(read_buf, sizeof(read_buf));
rosarium 4:cae1fa054b86 112
rosarium 4:cae1fa054b86 113 led_toggle = 1 - led_toggle;
rosarium 4:cae1fa054b86 114 TestLED = led_toggle; /* Toggle LED at the receiver */
rosarium 4:cae1fa054b86 115
rosarium 4:cae1fa054b86 116 if(ret == 0)
rosarium 4:cae1fa054b86 117 {
rosarium 4:cae1fa054b86 118 printf("\nNothing to read\n\r");
rosarium 4:cae1fa054b86 119 return;
rosarium 4:cae1fa054b86 120 }
rosarium 4:cae1fa054b86 121 printf("\r\n***Received a packet***\r\n\rReceived string = '%s' (len=%d) \n\r", read_buf, ret);
rosarium 4:cae1fa054b86 122 }
rosarium 4:cae1fa054b86 123
rosarium 4:cae1fa054b86 124
rosarium 1:cf2e2f85bf8d 125 /**
rosarium 1:cf2e2f85bf8d 126 * @brief main routine
rosarium 1:cf2e2f85bf8d 127 * @param None
rosarium 1:cf2e2f85bf8d 128 * @retval int
rosarium 1:cf2e2f85bf8d 129 */
rosarium 0:2ddc0583bcec 130 int main()
rosarium 1:cf2e2f85bf8d 131 {
rosarium 4:cae1fa054b86 132 TestLED = 0; /* LED off */
rosarium 0:2ddc0583bcec 133
rosarium 0:2ddc0583bcec 134 myspirit.attach_irq_callback(callback_func);
rosarium 0:2ddc0583bcec 135
rosarium 0:2ddc0583bcec 136 myspirit.on();
rosarium 0:2ddc0583bcec 137
rosarium 0:2ddc0583bcec 138 printf("\n**************HelloWorld mbed demo for Spirit1 (X-NUCLEO-IDS01A4/5)**************\r\n");
rosarium 0:2ddc0583bcec 139 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");
rosarium 0:2ddc0583bcec 140
rosarium 4:cae1fa054b86 141 event.rise(&set_send_data_flag); /*User button interrupt trigger to set send data flag */
rosarium 1:cf2e2f85bf8d 142
rosarium 0:2ddc0583bcec 143 while(1)
rosarium 0:2ddc0583bcec 144 {
rosarium 4:cae1fa054b86 145 __WFE(); /* low power in idle condition., waiting for an event */
rosarium 0:2ddc0583bcec 146
rosarium 1:cf2e2f85bf8d 147 if(rx_done_flag)
rosarium 0:2ddc0583bcec 148 {
rosarium 1:cf2e2f85bf8d 149 rx_done_flag = 0;
rosarium 4:cae1fa054b86 150 read_rcvd_data();
rosarium 4:cae1fa054b86 151 }
rosarium 4:cae1fa054b86 152
rosarium 4:cae1fa054b86 153 else if (send_data_flag)
rosarium 4:cae1fa054b86 154 {
rosarium 4:cae1fa054b86 155 send_data_flag = 0;
rosarium 4:cae1fa054b86 156 send_data();
rosarium 1:cf2e2f85bf8d 157 }
rosarium 1:cf2e2f85bf8d 158
rosarium 1:cf2e2f85bf8d 159 else if (tx_done_flag)
rosarium 1:cf2e2f85bf8d 160 {
rosarium 1:cf2e2f85bf8d 161 tx_done_flag = 0;
rosarium 4:cae1fa054b86 162 printf("\r\n***Packet sent ***\r\nSent string ='%s' (len=%d)\n\r", send_buf, strlen((const char*)send_buf) + 1);
rosarium 1:cf2e2f85bf8d 163 }
rosarium 0:2ddc0583bcec 164 }
rosarium 0:2ddc0583bcec 165
rosarium 0:2ddc0583bcec 166 myspirit.off();
rosarium 0:2ddc0583bcec 167
rosarium 0:2ddc0583bcec 168 return 0;
rosarium 0:2ddc0583bcec 169 }
rosarium 0:2ddc0583bcec 170
rosarium 0:2ddc0583bcec 171
rosarium 0:2ddc0583bcec 172