TX sw for DoorBell - Based on SPIRIT1 & NUCLEO

Dependencies:   mbed

Committer:
emcu
Date:
Sun Jan 07 02:53:42 2018 +0000
Revision:
1:d88d2aa4bb36
Parent:
0:2a97a480f5c4
Inserted link with description how to use this SW & KIT

Who changed what in which revision?

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