RX sw for DoorBell - Based on SPIRIT1 & NUCLEO

Dependencies:   mbed

Committer:
emcu
Date:
Sun Jan 07 02:54:03 2018 +0000
Revision:
1:a21495aaedae
Parent:
0:62a6b1435357
Inserted link with description how to use this SW & KIT

Who changed what in which revision?

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