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