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:
wobetz
Date:
Tue Jun 20 09:07:56 2017 +0000
Revision:
5:a65ed26b0f98
Parent:
4:cae1fa054b86
Cosmetic changes & usage of link to github for Spirit1

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
wobetz 5:a65ed26b0f98 48 static volatile bool rx_done_flag = false;
wobetz 5:a65ed26b0f98 49 static volatile bool tx_done_flag = false;
wobetz 5:a65ed26b0f98 50 static volatile bool send_data_flag = false;
rosarium 0:2ddc0583bcec 51
wobetz 5:a65ed26b0f98 52 static DigitalOut TestLED(D5); /* LED of IDS01A4/5 */
wobetz 5:a65ed26b0f98 53 static InterruptIn event(USER_BUTTON); /* Interrupt event to give userinterface to send pkt. */
rosarium 0:2ddc0583bcec 54
rosarium 1:cf2e2f85bf8d 55 /**
rosarium 1:cf2e2f85bf8d 56 * @brief callback_func
rosarium 1:cf2e2f85bf8d 57 * @param callback event
rosarium 1:cf2e2f85bf8d 58 * @retval None.
rosarium 1:cf2e2f85bf8d 59 */
rosarium 0:2ddc0583bcec 60 static void callback_func(int event)
rosarium 0:2ddc0583bcec 61 {
rosarium 1:cf2e2f85bf8d 62 if(event == SimpleSpirit1::RX_DONE)
rosarium 1:cf2e2f85bf8d 63 {
rosarium 1:cf2e2f85bf8d 64 rx_done_flag = 1;
rosarium 1:cf2e2f85bf8d 65 }
rosarium 1:cf2e2f85bf8d 66 else if (event == SimpleSpirit1::TX_DONE)
rosarium 1:cf2e2f85bf8d 67 {
rosarium 1:cf2e2f85bf8d 68 tx_done_flag = 1;
rosarium 1:cf2e2f85bf8d 69 }
rosarium 0:2ddc0583bcec 70 }
rosarium 0:2ddc0583bcec 71
rosarium 4:cae1fa054b86 72 /**
rosarium 4:cae1fa054b86 73 * @brief set_send_data_flag
rosarium 4:cae1fa054b86 74 * @param None
rosarium 4:cae1fa054b86 75 * @retval None
rosarium 4:cae1fa054b86 76 */
wobetz 5:a65ed26b0f98 77 static void set_send_data_flag(void)
rosarium 4:cae1fa054b86 78 {
rosarium 4:cae1fa054b86 79 send_data_flag = 1 ;
rosarium 4:cae1fa054b86 80 }
rosarium 1:cf2e2f85bf8d 81
rosarium 1:cf2e2f85bf8d 82 /**
rosarium 1:cf2e2f85bf8d 83 * @brief send_data
rosarium 1:cf2e2f85bf8d 84 * @param None
rosarium 1:cf2e2f85bf8d 85 * @retval None
rosarium 1:cf2e2f85bf8d 86 */
wobetz 5:a65ed26b0f98 87 static void send_data(void)
wobetz 5:a65ed26b0f98 88 {
rosarium 1:cf2e2f85bf8d 89 printf("\r\n***Sending a packet***\r\n");
rosarium 0:2ddc0583bcec 90
rosarium 4:cae1fa054b86 91 while(myspirit.is_receiving()); /* wait for ongoing RX ends */
rosarium 1:cf2e2f85bf8d 92
wobetz 5:a65ed26b0f98 93 size_t curr_len = strlen((const char*)send_buf) + 1;
rosarium 0:2ddc0583bcec 94 myspirit.send(send_buf, curr_len);
rosarium 0:2ddc0583bcec 95 }
rosarium 4:cae1fa054b86 96
rosarium 4:cae1fa054b86 97 /**
rosarium 4:cae1fa054b86 98 * @brief read_rcvd_data
rosarium 4:cae1fa054b86 99 * @param None
rosarium 4:cae1fa054b86 100 * @retval None
rosarium 4:cae1fa054b86 101 */
wobetz 5:a65ed26b0f98 102 static void read_rcvd_data(void)
rosarium 4:cae1fa054b86 103 {
rosarium 4:cae1fa054b86 104
rosarium 4:cae1fa054b86 105 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 106
rosarium 4:cae1fa054b86 107 int ret = myspirit.read(read_buf, sizeof(read_buf));
rosarium 4:cae1fa054b86 108
wobetz 5:a65ed26b0f98 109 TestLED = !TestLED; /* Toggle LED at the receiver */
rosarium 4:cae1fa054b86 110
rosarium 4:cae1fa054b86 111 if(ret == 0)
rosarium 4:cae1fa054b86 112 {
rosarium 4:cae1fa054b86 113 printf("\nNothing to read\n\r");
rosarium 4:cae1fa054b86 114 return;
rosarium 4:cae1fa054b86 115 }
rosarium 4:cae1fa054b86 116 printf("\r\n***Received a packet***\r\n\rReceived string = '%s' (len=%d) \n\r", read_buf, ret);
rosarium 4:cae1fa054b86 117 }
rosarium 4:cae1fa054b86 118
rosarium 4:cae1fa054b86 119
rosarium 1:cf2e2f85bf8d 120 /**
rosarium 1:cf2e2f85bf8d 121 * @brief main routine
rosarium 1:cf2e2f85bf8d 122 * @param None
rosarium 1:cf2e2f85bf8d 123 * @retval int
rosarium 1:cf2e2f85bf8d 124 */
rosarium 0:2ddc0583bcec 125 int main()
rosarium 1:cf2e2f85bf8d 126 {
rosarium 4:cae1fa054b86 127 TestLED = 0; /* LED off */
rosarium 0:2ddc0583bcec 128
rosarium 0:2ddc0583bcec 129 myspirit.attach_irq_callback(callback_func);
rosarium 0:2ddc0583bcec 130
rosarium 0:2ddc0583bcec 131 myspirit.on();
rosarium 0:2ddc0583bcec 132
rosarium 0:2ddc0583bcec 133 printf("\n**************HelloWorld mbed demo for Spirit1 (X-NUCLEO-IDS01A4/5)**************\r\n");
rosarium 0:2ddc0583bcec 134 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 135
rosarium 4:cae1fa054b86 136 event.rise(&set_send_data_flag); /*User button interrupt trigger to set send data flag */
rosarium 1:cf2e2f85bf8d 137
rosarium 0:2ddc0583bcec 138 while(1)
rosarium 0:2ddc0583bcec 139 {
rosarium 4:cae1fa054b86 140 __WFE(); /* low power in idle condition., waiting for an event */
rosarium 0:2ddc0583bcec 141
rosarium 1:cf2e2f85bf8d 142 if(rx_done_flag)
rosarium 0:2ddc0583bcec 143 {
wobetz 5:a65ed26b0f98 144 rx_done_flag = false;
rosarium 4:cae1fa054b86 145 read_rcvd_data();
rosarium 4:cae1fa054b86 146 }
rosarium 4:cae1fa054b86 147
rosarium 4:cae1fa054b86 148 else if (send_data_flag)
rosarium 4:cae1fa054b86 149 {
wobetz 5:a65ed26b0f98 150 send_data_flag = false;
rosarium 4:cae1fa054b86 151 send_data();
rosarium 1:cf2e2f85bf8d 152 }
rosarium 1:cf2e2f85bf8d 153
rosarium 1:cf2e2f85bf8d 154 else if (tx_done_flag)
rosarium 1:cf2e2f85bf8d 155 {
wobetz 5:a65ed26b0f98 156 tx_done_flag = false;
rosarium 4:cae1fa054b86 157 printf("\r\n***Packet sent ***\r\nSent string ='%s' (len=%d)\n\r", send_buf, strlen((const char*)send_buf) + 1);
rosarium 1:cf2e2f85bf8d 158 }
rosarium 0:2ddc0583bcec 159 }
rosarium 0:2ddc0583bcec 160
wobetz 5:a65ed26b0f98 161 /* unreachable */
wobetz 5:a65ed26b0f98 162 // myspirit.off();
wobetz 5:a65ed26b0f98 163 // return 0;
rosarium 0:2ddc0583bcec 164 }