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:
Thu May 25 16:38:55 2017 +0000
Revision:
0:2ddc0583bcec
Child:
1:cf2e2f85bf8d
First draft version of HelloWorld_IDS01A5

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 0:2ddc0583bcec 6 * @date May 17th, 2017
rosarium 0:2ddc0583bcec 7 * @brief mbed HelloWorld P2P example 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 0:2ddc0583bcec 42 #define TEST_STR_LEN (32) //512
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 0:2ddc0583bcec 45
rosarium 0:2ddc0583bcec 46 #define PCKT_MARGIN (2) //32
rosarium 0:2ddc0583bcec 47
rosarium 0:2ddc0583bcec 48 //#define SIGNAL_BIT (0x1) //RPi //Removed OS dependency for mbed classic
rosarium 0:2ddc0583bcec 49 //static osThreadId main_thread_id; //RPi //Removed OS dependency for mbed classic
rosarium 0:2ddc0583bcec 50
rosarium 0:2ddc0583bcec 51
rosarium 0:2ddc0583bcec 52 static SimpleSpirit1 &myspirit = SimpleSpirit1::CreateInstance(D11, D12, D3, D9, D10, D2); //RPi
rosarium 0:2ddc0583bcec 53 bool SIGNAL_BIT = 0; //RPi
rosarium 0:2ddc0583bcec 54 static int curr_len = 0;
rosarium 0:2ddc0583bcec 55 unsigned char myValue = 0;
rosarium 0:2ddc0583bcec 56
rosarium 0:2ddc0583bcec 57 InterruptIn event(USER_BUTTON); //RPi //Added interrupt event to give userinterface to send pkt.
rosarium 0:2ddc0583bcec 58
rosarium 0:2ddc0583bcec 59 static void callback_func(int event)
rosarium 0:2ddc0583bcec 60 {
rosarium 0:2ddc0583bcec 61 if(event == SimpleSpirit1::RX_DONE) {
rosarium 0:2ddc0583bcec 62 // osSignalSet(main_thread_id, SIGNAL_BIT); //RPi //Removed OS dependency for mbed classic
rosarium 0:2ddc0583bcec 63 SIGNAL_BIT = 1;
rosarium 0:2ddc0583bcec 64 } else if (event != SimpleSpirit1::TX_DONE) {
rosarium 0:2ddc0583bcec 65 printf("Got unexpected event %d\r\n", event);
rosarium 0:2ddc0583bcec 66 } else {
rosarium 0:2ddc0583bcec 67 printf("TX done!\n\r\n");
rosarium 0:2ddc0583bcec 68 }
rosarium 0:2ddc0583bcec 69 }
rosarium 0:2ddc0583bcec 70
rosarium 0:2ddc0583bcec 71 void send_data(void)
rosarium 0:2ddc0583bcec 72 {
rosarium 0:2ddc0583bcec 73 int cnt = 0;
rosarium 0:2ddc0583bcec 74 cnt++;
rosarium 0:2ddc0583bcec 75 if(cnt >= MAX_PACKET_LEN) cnt = 0;
rosarium 0:2ddc0583bcec 76
rosarium 0:2ddc0583bcec 77 printf("\r\n***Sending a packet***\r\nSent string ='%s' (len=%d)\n\r", send_buf, strlen((const char*)send_buf) + 1);
rosarium 0:2ddc0583bcec 78
rosarium 0:2ddc0583bcec 79 uint32_t before_cca = us_ticker_read();
rosarium 0:2ddc0583bcec 80 while(myspirit.is_receiving()); // wait for ongoing RX ends
rosarium 0:2ddc0583bcec 81 uint32_t after_cca = us_ticker_read();
rosarium 0:2ddc0583bcec 82
rosarium 0:2ddc0583bcec 83 curr_len = strlen((const char*)send_buf) + 1;
rosarium 0:2ddc0583bcec 84 myspirit.send(send_buf, curr_len);
rosarium 0:2ddc0583bcec 85 uint32_t after_send = us_ticker_read();
rosarium 0:2ddc0583bcec 86
rosarium 0:2ddc0583bcec 87 printf("CCA time: %uus\n", (unsigned int)(after_cca - before_cca));
rosarium 0:2ddc0583bcec 88 printf("Send time: %uus\n\r", (unsigned int)(after_send - after_cca));
rosarium 0:2ddc0583bcec 89 }
rosarium 0:2ddc0583bcec 90
rosarium 0:2ddc0583bcec 91 int main()
rosarium 0:2ddc0583bcec 92 {
rosarium 0:2ddc0583bcec 93 // osStatus ret; //RPi //Removed OS dependency for mbed classic
rosarium 0:2ddc0583bcec 94
rosarium 0:2ddc0583bcec 95 unsigned int pckts_fails = 0;
rosarium 0:2ddc0583bcec 96 unsigned int first_failed_packet = 0;
rosarium 0:2ddc0583bcec 97 unsigned int last_failed_packet = 0;
rosarium 0:2ddc0583bcec 98 int validate_cnt = -1;
rosarium 0:2ddc0583bcec 99
rosarium 0:2ddc0583bcec 100 // main_thread_id = Thread::gettid(); //RPi //Removed OS dependency for mbed classic
rosarium 0:2ddc0583bcec 101
rosarium 0:2ddc0583bcec 102
rosarium 0:2ddc0583bcec 103 DigitalOut TestLED = D5; //RPi //LED of IDS01A4/5
rosarium 0:2ddc0583bcec 104 TestLED = 0; //RPi //LED off
rosarium 0:2ddc0583bcec 105
rosarium 0:2ddc0583bcec 106 myspirit.attach_irq_callback(callback_func);
rosarium 0:2ddc0583bcec 107
rosarium 0:2ddc0583bcec 108 myspirit.on();
rosarium 0:2ddc0583bcec 109
rosarium 0:2ddc0583bcec 110 printf("\n**************HelloWorld mbed demo for Spirit1 (X-NUCLEO-IDS01A4/5)**************\r\n");
rosarium 0:2ddc0583bcec 111 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 112
rosarium 0:2ddc0583bcec 113 while(1)
rosarium 0:2ddc0583bcec 114 {
rosarium 0:2ddc0583bcec 115
rosarium 0:2ddc0583bcec 116 event.rise(&send_data); //RPi // Added User button interrupt trigger to send data
rosarium 0:2ddc0583bcec 117
rosarium 0:2ddc0583bcec 118 if(SIGNAL_BIT) //RPi //Data pkt received
rosarium 0:2ddc0583bcec 119 {
rosarium 0:2ddc0583bcec 120 SIGNAL_BIT = 0;
rosarium 0:2ddc0583bcec 121
rosarium 0:2ddc0583bcec 122 for(unsigned int flush_count = 0; flush_count < TEST_STR_LEN; flush_count++) read_buf[flush_count] = 0 ;//RPi//clear the read buffer
rosarium 0:2ddc0583bcec 123
rosarium 0:2ddc0583bcec 124 int ret = myspirit.read(read_buf, sizeof(read_buf));
rosarium 0:2ddc0583bcec 125
rosarium 0:2ddc0583bcec 126 myValue = 1 - myValue; //RPi //Toggle LED at the receiver
rosarium 0:2ddc0583bcec 127 TestLED = myValue; //RPi //Toggle LED at the receiver
rosarium 0:2ddc0583bcec 128
rosarium 0:2ddc0583bcec 129 if(ret == 0) {
rosarium 0:2ddc0583bcec 130 printf("\nNothing to read\n\r");
rosarium 0:2ddc0583bcec 131 continue;
rosarium 0:2ddc0583bcec 132 }
rosarium 0:2ddc0583bcec 133 int len = strlen((const char*)read_buf) + 1;
rosarium 0:2ddc0583bcec 134 if(validate_cnt < 0) {
rosarium 0:2ddc0583bcec 135 validate_cnt = ret;
rosarium 0:2ddc0583bcec 136 } else {
rosarium 0:2ddc0583bcec 137 validate_cnt++;
rosarium 0:2ddc0583bcec 138 if(validate_cnt > MAX_PACKET_LEN) validate_cnt = 1;
rosarium 0:2ddc0583bcec 139 }
rosarium 0:2ddc0583bcec 140
rosarium 0:2ddc0583bcec 141 if(ret != len) {
rosarium 0:2ddc0583bcec 142 pckts_fails++;
rosarium 0:2ddc0583bcec 143 if((ret > (int)(first_failed_packet + PCKT_MARGIN) || (ret < (int)(first_failed_packet - PCKT_MARGIN)))) {
rosarium 0:2ddc0583bcec 144 first_failed_packet = ret;
rosarium 0:2ddc0583bcec 145 }
rosarium 0:2ddc0583bcec 146 last_failed_packet = ret;
rosarium 0:2ddc0583bcec 147 }
rosarium 0:2ddc0583bcec 148
rosarium 0:2ddc0583bcec 149 printf("\r\n***Received a packet***\r\n\rReceived string = '%s' (len=%d) \n\r", read_buf, ret);
rosarium 0:2ddc0583bcec 150 printf("RSSI: %f, LQI: %u\r\n", myspirit.get_last_rssi_dbm(), (unsigned int)myspirit.get_last_sqi());
rosarium 0:2ddc0583bcec 151 printf("Packets failed:\t%d\t(ff=%d, lf=%d)\r\n", pckts_fails, first_failed_packet, last_failed_packet);
rosarium 0:2ddc0583bcec 152
rosarium 0:2ddc0583bcec 153 while(myspirit.is_receiving()); // wait for ongoing RX ends
rosarium 0:2ddc0583bcec 154 }
rosarium 0:2ddc0583bcec 155 }
rosarium 0:2ddc0583bcec 156
rosarium 0:2ddc0583bcec 157 myspirit.off();
rosarium 0:2ddc0583bcec 158
rosarium 0:2ddc0583bcec 159 return 0;
rosarium 0:2ddc0583bcec 160 }
rosarium 0:2ddc0583bcec 161
rosarium 0:2ddc0583bcec 162
rosarium 0:2ddc0583bcec 163