TX part for SPIRIT1, P2P application.

Dependencies:   mbed

Committer:
emcu
Date:
Fri Jan 05 19:13:24 2018 +0000
Revision:
0:94f11a1db3fa
Child:
1:4910fc32a71f
TX part for SPIRIT1, P2P application.

Who changed what in which revision?

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