LP Transmitter Wakeup
Dependencies: max32630fthr USBDevice
main.cpp@2:33b3b46a9c0d, 2020-09-04 (annotated)
- Committer:
- tlyp
- Date:
- Fri Sep 04 16:59:21 2020 +0000
- Revision:
- 2:33b3b46a9c0d
- Parent:
- 1:8834bc22c2e7
- Child:
- 3:b0579a51fd46
initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tlyp | 2:33b3b46a9c0d | 1 | /******************************************************************************* |
tlyp | 2:33b3b46a9c0d | 2 | * Copyright (C) Maxim Integrated Products, Inc., All Rights Reserved. |
tlyp | 2:33b3b46a9c0d | 3 | * |
tlyp | 2:33b3b46a9c0d | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
tlyp | 2:33b3b46a9c0d | 5 | * copy of this software and associated documentation files (the "Software"), |
tlyp | 2:33b3b46a9c0d | 6 | * to deal in the Software without restriction, including without limitation |
tlyp | 2:33b3b46a9c0d | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
tlyp | 2:33b3b46a9c0d | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
tlyp | 2:33b3b46a9c0d | 9 | * Software is furnished to do so, subject to the following conditions: |
tlyp | 2:33b3b46a9c0d | 10 | * |
tlyp | 2:33b3b46a9c0d | 11 | * The above copyright notice and this permission notice shall be included |
tlyp | 2:33b3b46a9c0d | 12 | * in all copies or substantial portions of the Software. |
tlyp | 2:33b3b46a9c0d | 13 | * |
tlyp | 2:33b3b46a9c0d | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
tlyp | 2:33b3b46a9c0d | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
tlyp | 2:33b3b46a9c0d | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
tlyp | 2:33b3b46a9c0d | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
tlyp | 2:33b3b46a9c0d | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
tlyp | 2:33b3b46a9c0d | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
tlyp | 2:33b3b46a9c0d | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
tlyp | 2:33b3b46a9c0d | 21 | * |
tlyp | 2:33b3b46a9c0d | 22 | * Except as contained in this notice, the name of Maxim Integrated |
tlyp | 2:33b3b46a9c0d | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
tlyp | 2:33b3b46a9c0d | 24 | * Products, Inc. Branding Policy. |
tlyp | 2:33b3b46a9c0d | 25 | * |
tlyp | 2:33b3b46a9c0d | 26 | * The mere transfer of this software does not imply any licenses |
tlyp | 2:33b3b46a9c0d | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
tlyp | 2:33b3b46a9c0d | 28 | * trademarks, maskwork rights, or any other form of intellectual |
tlyp | 2:33b3b46a9c0d | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
tlyp | 2:33b3b46a9c0d | 30 | * ownership rights. |
tlyp | 2:33b3b46a9c0d | 31 | ******************************************************************************* |
tlyp | 2:33b3b46a9c0d | 32 | |
tlyp | 2:33b3b46a9c0d | 33 | This is code for the MAX1472 RF Transmitter EV-Kit. This program will record a |
tlyp | 2:33b3b46a9c0d | 34 | temperature fom the MAX30208 EV-Kit, encrypt the data using the user-generated |
tlyp | 2:33b3b46a9c0d | 35 | Symmetric key, apply forward error correction (FEC) encoding, and then send the |
tlyp | 2:33b3b46a9c0d | 36 | message via the MAX1472 transmitter. When the MAX1472 is not sending data, it will |
tlyp | 2:33b3b46a9c0d | 37 | go into a power saving sleep mode. |
tlyp | 2:33b3b46a9c0d | 38 | |
tlyp | 2:33b3b46a9c0d | 39 | Hardware Setup and connections: |
tlyp | 2:33b3b46a9c0d | 40 | |
tlyp | 2:33b3b46a9c0d | 41 | MAX32630FTHR-> MAX1472 Ev-Kit |
tlyp | 2:33b3b46a9c0d | 42 | |
tlyp | 2:33b3b46a9c0d | 43 | 3.3V -> VDD |
tlyp | 2:33b3b46a9c0d | 44 | GND -> VSS |
tlyp | 2:33b3b46a9c0d | 45 | P3_1 -> Data_In |
tlyp | 2:33b3b46a9c0d | 46 | P5_1 -> ENABLE |
tlyp | 2:33b3b46a9c0d | 47 | |
tlyp | 2:33b3b46a9c0d | 48 | ******************************************************************************* |
tlyp | 2:33b3b46a9c0d | 49 | MAX32630FTHR-> MAX30208 Ev-Kit |
tlyp | 2:33b3b46a9c0d | 50 | |
tlyp | 2:33b3b46a9c0d | 51 | 1.8V -> VIN |
tlyp | 2:33b3b46a9c0d | 52 | GND -> GND |
tlyp | 2:33b3b46a9c0d | 53 | P3_4 -> SDA |
tlyp | 2:33b3b46a9c0d | 54 | P3_5 -> SCL |
tlyp | 2:33b3b46a9c0d | 55 | |
tlyp | 2:33b3b46a9c0d | 56 | ******************************************************************************* |
tlyp | 2:33b3b46a9c0d | 57 | */ |
tlyp | 2:33b3b46a9c0d | 58 | |
MI | 0:41ed595f83f5 | 59 | #include "mbed.h" |
MI | 0:41ed595f83f5 | 60 | #include "max32630fthr.h" |
MI | 0:41ed595f83f5 | 61 | #include "mxc_config.h" |
MI | 0:41ed595f83f5 | 62 | #include "lp.h" |
MI | 0:41ed595f83f5 | 63 | #include "gpio.h" |
MI | 1:8834bc22c2e7 | 64 | #include "rtc.h" |
MI | 1:8834bc22c2e7 | 65 | #include "MAX14690.h" |
tlyp | 2:33b3b46a9c0d | 66 | #include "USBSerial.h" |
tlyp | 2:33b3b46a9c0d | 67 | #include "MAX30208.h" |
tlyp | 2:33b3b46a9c0d | 68 | #include "ForwardErrCorr.h" |
tlyp | 2:33b3b46a9c0d | 69 | |
tlyp | 2:33b3b46a9c0d | 70 | MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3); |
tlyp | 2:33b3b46a9c0d | 71 | |
tlyp | 2:33b3b46a9c0d | 72 | I2C i2c(P3_4, P3_5); //sda,scl |
tlyp | 2:33b3b46a9c0d | 73 | |
tlyp | 2:33b3b46a9c0d | 74 | RawSerial uart(P3_1,P3_0);//tx,rx |
tlyp | 2:33b3b46a9c0d | 75 | MAX30208 TempSensor(i2c, 0x50); //Constructor takes 7-bit slave adrs |
tlyp | 2:33b3b46a9c0d | 76 | |
tlyp | 2:33b3b46a9c0d | 77 | #define SymmetricKey "RfIsCoOl" //Set Private Key here -- Make sure it is identical to receiver |
tlyp | 2:33b3b46a9c0d | 78 | char TransTable[] = {0x1F,0x18,0x06,0x01}; //Used to translate data for FEC -- Make sure it is identical to receiver |
tlyp | 2:33b3b46a9c0d | 79 | Translator transTx(SymmetricKey, TransTable); //Initialize Encoder |
tlyp | 2:33b3b46a9c0d | 80 | |
tlyp | 2:33b3b46a9c0d | 81 | #define SendDelay 5 //Number of seconds between sending data |
tlyp | 2:33b3b46a9c0d | 82 | char DEVICETYPE = 'T'; //'T' for Temperature Sensor |
tlyp | 2:33b3b46a9c0d | 83 | char DEVICEID = 0x01; //Set the Device ID |
tlyp | 2:33b3b46a9c0d | 84 | |
tlyp | 2:33b3b46a9c0d | 85 | USBSerial microUSB; //microUSB connection that can be used for printing |
MI | 0:41ed595f83f5 | 86 | |
MI | 0:41ed595f83f5 | 87 | DigitalOut rLED(LED1); |
MI | 0:41ed595f83f5 | 88 | DigitalOut gLED(LED2); |
MI | 0:41ed595f83f5 | 89 | DigitalOut bLED(LED3); |
MI | 0:41ed595f83f5 | 90 | |
tlyp | 2:33b3b46a9c0d | 91 | DigitalIn sw1(SW1); //Used on start-up to stay in active mode for re-programming |
tlyp | 2:33b3b46a9c0d | 92 | DigitalOut TXEnable(P5_1); //Used to Enable Transmitter |
MI | 0:41ed595f83f5 | 93 | |
MI | 0:41ed595f83f5 | 94 | // ***************************************************************************** |
tlyp | 2:33b3b46a9c0d | 95 | /* |
tlyp | 2:33b3b46a9c0d | 96 | * @brief Configures the RTC to tick every second so it can be compared to during sleep intterupt |
tlyp | 2:33b3b46a9c0d | 97 | */ |
MI | 1:8834bc22c2e7 | 98 | void RTC_Setup() |
MI | 0:41ed595f83f5 | 99 | { |
MI | 1:8834bc22c2e7 | 100 | rtc_cfg_t RTCconfig; |
tlyp | 2:33b3b46a9c0d | 101 | RTCconfig.prescaler = RTC_PRESCALE_DIV_2_12; //Set tick frequency to 1 second |
tlyp | 2:33b3b46a9c0d | 102 | RTCconfig.prescalerMask = RTC_PRESCALE_DIV_2_12; //1 second frequency |
MI | 1:8834bc22c2e7 | 103 | RTCconfig.snoozeCount = 0; |
MI | 1:8834bc22c2e7 | 104 | RTCconfig.snoozeMode = RTC_SNOOZE_DISABLE; |
MI | 1:8834bc22c2e7 | 105 | RTC_Init(&RTCconfig); |
MI | 1:8834bc22c2e7 | 106 | RTC_Start(); |
MI | 0:41ed595f83f5 | 107 | } |
MI | 0:41ed595f83f5 | 108 | |
tlyp | 2:33b3b46a9c0d | 109 | //**************************************************************************** |
tlyp | 2:33b3b46a9c0d | 110 | /** |
tlyp | 2:33b3b46a9c0d | 111 | * @brief Record and read temperature from MAX30208 |
tlyp | 2:33b3b46a9c0d | 112 | * @param TempSensor - Refrence to MAX30208 temp sensor object |
tlyp | 2:33b3b46a9c0d | 113 | * @param &value[OUT]- Address to store read temperature at |
tlyp | 2:33b3b46a9c0d | 114 | * @return 0 on success, 1 on failure due to improper data record or read |
tlyp | 2:33b3b46a9c0d | 115 | */ |
tlyp | 2:33b3b46a9c0d | 116 | uint16_t ReadData(MAX30208 TempSensor, uint16_t &value){ |
tlyp | 2:33b3b46a9c0d | 117 | if (TempSensor.takeDataMeasurment() != 0){ |
tlyp | 2:33b3b46a9c0d | 118 | microUSB.printf("Error Taking data Meaurment\r\n"); |
tlyp | 2:33b3b46a9c0d | 119 | return(1); |
tlyp | 2:33b3b46a9c0d | 120 | } |
tlyp | 2:33b3b46a9c0d | 121 | wait_ms(50); //max temp read is 50ms |
tlyp | 2:33b3b46a9c0d | 122 | if (TempSensor.readData(value) !=0){ |
tlyp | 2:33b3b46a9c0d | 123 | microUSB.printf("Error reading temperature Data\r\n"); |
tlyp | 2:33b3b46a9c0d | 124 | return(1); |
tlyp | 2:33b3b46a9c0d | 125 | } |
tlyp | 2:33b3b46a9c0d | 126 | return(0); |
tlyp | 2:33b3b46a9c0d | 127 | } |
tlyp | 2:33b3b46a9c0d | 128 | |
tlyp | 2:33b3b46a9c0d | 129 | //***************************************************************************** |
tlyp | 2:33b3b46a9c0d | 130 | /* |
tlyp | 2:33b3b46a9c0d | 131 | * @brief Begin Communication with warm up bytes and device type/id |
tlyp | 2:33b3b46a9c0d | 132 | */ |
tlyp | 2:33b3b46a9c0d | 133 | void comInit(){ |
tlyp | 2:33b3b46a9c0d | 134 | uart.putc(0xFF); |
tlyp | 2:33b3b46a9c0d | 135 | uart.putc(0xFF); |
tlyp | 2:33b3b46a9c0d | 136 | uart.putc(0x00); |
tlyp | 2:33b3b46a9c0d | 137 | uart.putc('b'); |
tlyp | 2:33b3b46a9c0d | 138 | uart.putc(DEVICETYPE); |
tlyp | 2:33b3b46a9c0d | 139 | uart.putc(DEVICEID); |
tlyp | 2:33b3b46a9c0d | 140 | } |
tlyp | 2:33b3b46a9c0d | 141 | |
tlyp | 2:33b3b46a9c0d | 142 | //***************************************************************************** |
tlyp | 2:33b3b46a9c0d | 143 | /** |
tlyp | 2:33b3b46a9c0d | 144 | * @brief Send data and end transmission |
tlyp | 2:33b3b46a9c0d | 145 | * @param EncryptedData[IN] - 8 bytes of encryted data to send via UART connection |
tlyp | 2:33b3b46a9c0d | 146 | */ |
tlyp | 2:33b3b46a9c0d | 147 | void comData(char *EncryptedData){ |
tlyp | 2:33b3b46a9c0d | 148 | for(int i=0;i<8;i++){ |
tlyp | 2:33b3b46a9c0d | 149 | uart.putc(EncryptedData[i]); //Send all of the encrypted data |
tlyp | 2:33b3b46a9c0d | 150 | } |
tlyp | 2:33b3b46a9c0d | 151 | uart.putc('c'); //End of packet character |
tlyp | 2:33b3b46a9c0d | 152 | uart.send_break(); |
tlyp | 2:33b3b46a9c0d | 153 | } |
tlyp | 2:33b3b46a9c0d | 154 | |
tlyp | 2:33b3b46a9c0d | 155 | //***************************************************************************** |
tlyp | 2:33b3b46a9c0d | 156 | |
MI | 0:41ed595f83f5 | 157 | int main(void) |
MI | 0:41ed595f83f5 | 158 | { |
tlyp | 2:33b3b46a9c0d | 159 | //initialize PMIC |
MI | 1:8834bc22c2e7 | 160 | MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3); |
tlyp | 2:33b3b46a9c0d | 161 | uart.baud(9600); //Set baud rate for 9600 |
MI | 1:8834bc22c2e7 | 162 | |
MI | 0:41ed595f83f5 | 163 | //check if starting at main because of LP0 wake-up |
MI | 1:8834bc22c2e7 | 164 | if(LP_IsLP0WakeUp()) { |
tlyp | 2:33b3b46a9c0d | 165 | //empty |
MI | 1:8834bc22c2e7 | 166 | } |
tlyp | 2:33b3b46a9c0d | 167 | |
MI | 0:41ed595f83f5 | 168 | else { |
MI | 0:41ed595f83f5 | 169 | //We did not wake up from sleep and this is first power-on |
MI | 1:8834bc22c2e7 | 170 | //Only configure RTC the first time around |
MI | 1:8834bc22c2e7 | 171 | RTC_Setup(); |
MI | 0:41ed595f83f5 | 172 | } |
tlyp | 2:33b3b46a9c0d | 173 | |
tlyp | 2:33b3b46a9c0d | 174 | //Set LEDs on to show device is awake |
MI | 0:41ed595f83f5 | 175 | gLED = LED_ON; |
MI | 1:8834bc22c2e7 | 176 | rLED = LED_ON; |
MI | 1:8834bc22c2e7 | 177 | bLED = LED_ON; |
tlyp | 2:33b3b46a9c0d | 178 | |
tlyp | 2:33b3b46a9c0d | 179 | //Main loop |
MI | 0:41ed595f83f5 | 180 | while(1) { |
MI | 1:8834bc22c2e7 | 181 | //hold down switch 1 to prevent the microcontroller from going into LP0 |
MI | 1:8834bc22c2e7 | 182 | while(sw1 == 0); |
MI | 1:8834bc22c2e7 | 183 | |
tlyp | 2:33b3b46a9c0d | 184 | uint16_t tempData; |
tlyp | 2:33b3b46a9c0d | 185 | if(ReadData(TempSensor,tempData) !=0){ |
tlyp | 2:33b3b46a9c0d | 186 | printf("Error reading data!\r\n"); |
tlyp | 2:33b3b46a9c0d | 187 | } |
tlyp | 2:33b3b46a9c0d | 188 | |
tlyp | 2:33b3b46a9c0d | 189 | //Encrypt Temperature Data |
tlyp | 2:33b3b46a9c0d | 190 | char EncryptedData[8]; |
tlyp | 2:33b3b46a9c0d | 191 | transTx.Encrypt(tempData,EncryptedData); |
tlyp | 2:33b3b46a9c0d | 192 | |
tlyp | 2:33b3b46a9c0d | 193 | TXEnable = 1; //Turn on Transmitter |
tlyp | 2:33b3b46a9c0d | 194 | wait_us(450); //Wake-up time for transmitter |
MI | 1:8834bc22c2e7 | 195 | |
tlyp | 2:33b3b46a9c0d | 196 | //Start a timer for sending data |
tlyp | 2:33b3b46a9c0d | 197 | Timer timer; |
tlyp | 2:33b3b46a9c0d | 198 | timer.start(); |
tlyp | 2:33b3b46a9c0d | 199 | |
tlyp | 2:33b3b46a9c0d | 200 | //Send the data for 1 sec |
tlyp | 2:33b3b46a9c0d | 201 | while(timer.read_ms() < 1000) { |
tlyp | 2:33b3b46a9c0d | 202 | |
tlyp | 2:33b3b46a9c0d | 203 | //Send Data continuously |
tlyp | 2:33b3b46a9c0d | 204 | comInit(); |
tlyp | 2:33b3b46a9c0d | 205 | comData(EncryptedData); |
tlyp | 2:33b3b46a9c0d | 206 | }//while |
tlyp | 2:33b3b46a9c0d | 207 | |
tlyp | 2:33b3b46a9c0d | 208 | TXEnable = 0; //Turn off Transmitter |
tlyp | 2:33b3b46a9c0d | 209 | timer.stop(); //Stop send timer |
tlyp | 2:33b3b46a9c0d | 210 | |
MI | 1:8834bc22c2e7 | 211 | //Clear existing wake-up config |
MI | 1:8834bc22c2e7 | 212 | LP_ClearWakeUpConfig(); |
MI | 0:41ed595f83f5 | 213 | |
MI | 1:8834bc22c2e7 | 214 | //Clear any event flags |
MI | 1:8834bc22c2e7 | 215 | LP_ClearWakeUpFlags(); |
MI | 0:41ed595f83f5 | 216 | |
MI | 1:8834bc22c2e7 | 217 | //configure wake-up on RTC compare 0 |
tlyp | 2:33b3b46a9c0d | 218 | //LP_ConfigRTCWakeUp(enable compare0, enable compare1, set prescale, set rollover) |
MI | 1:8834bc22c2e7 | 219 | LP_ConfigRTCWakeUp(1, 0, 0, 0); |
MI | 1:8834bc22c2e7 | 220 | |
tlyp | 2:33b3b46a9c0d | 221 | //disable unused PMIC rails to minimize power consumption |
tlyp | 2:33b3b46a9c0d | 222 | pegasus.max14690.ldo2SetMode(MAX14690::LDO_DISABLED); |
tlyp | 2:33b3b46a9c0d | 223 | pegasus.max14690.ldo3SetMode(MAX14690::LDO_DISABLED); |
MI | 1:8834bc22c2e7 | 224 | |
tlyp | 2:33b3b46a9c0d | 225 | //Turn off LEDs to show low power mode |
tlyp | 2:33b3b46a9c0d | 226 | gLED = LED_OFF; |
tlyp | 2:33b3b46a9c0d | 227 | rLED = LED_OFF; |
tlyp | 2:33b3b46a9c0d | 228 | bLED = LED_OFF; |
tlyp | 2:33b3b46a9c0d | 229 | |
tlyp | 2:33b3b46a9c0d | 230 | //Reset the RTC back to 0 ticks |
tlyp | 2:33b3b46a9c0d | 231 | RTC_SetCount(0); |
tlyp | 2:33b3b46a9c0d | 232 | |
tlyp | 2:33b3b46a9c0d | 233 | //set RTC to generate an interrupt every SendDelay seconds (User defined value at top of program) |
tlyp | 2:33b3b46a9c0d | 234 | RTC_SetCompare(0,SendDelay); //(Compare index, Compare Value in ticks) |
MI | 1:8834bc22c2e7 | 235 | |
MI | 1:8834bc22c2e7 | 236 | //clear comparison flag in the RTC registers |
MI | 1:8834bc22c2e7 | 237 | RTC_ClearFlags(MXC_F_RTC_FLAGS_COMP0); |
MI | 0:41ed595f83f5 | 238 | |
tlyp | 2:33b3b46a9c0d | 239 | //Enter Deep Sleep Mode |
MI | 1:8834bc22c2e7 | 240 | LP_EnterLP0(); |
MI | 1:8834bc22c2e7 | 241 | |
MI | 1:8834bc22c2e7 | 242 | //firmware will reset with no prior knowledge on wake-up |
MI | 0:41ed595f83f5 | 243 | } |
MI | 0:41ed595f83f5 | 244 | } |