MAX20361 Demo with LoRa Module on LP0 mode

Dependencies:   SX1276GenericLib USBDevice

Fork of NonPingPong_PICO_LoRa_LP0 by Walter Luu

Committer:
walterluu
Date:
Fri Oct 16 06:50:42 2020 +0000
Revision:
7:c0872971aef4
Parent:
6:51f492ca61a2
MAX20361 Demo with LoRa Module on LP0 mode

Who changed what in which revision?

UserRevisionLine numberNew contents of line
walterluu 3:85fc843a9d7d 1
walterluu 0:664d9df11a00 2 #include "mbed.h"
walterluu 3:85fc843a9d7d 3
walterluu 3:85fc843a9d7d 4 // Board pins related
walterluu 0:664d9df11a00 5 #include "PinMap.h"
walterluu 0:664d9df11a00 6
walterluu 3:85fc843a9d7d 7 // LORA related
walterluu 3:85fc843a9d7d 8 #include "global_buffers.h"
walterluu 3:85fc843a9d7d 9 #include "GenericPingPong2.h"
walterluu 3:85fc843a9d7d 10 #define FEATURE_LORA
walterluu 3:85fc843a9d7d 11 #include "sx1276-mbed-hal.h"
walterluu 3:85fc843a9d7d 12
walterluu 3:85fc843a9d7d 13 #include "main.h"
walterluu 0:664d9df11a00 14
walterluu 3:85fc843a9d7d 15 // OT07 related
walterluu 3:85fc843a9d7d 16 #include "OT07_lib.h"
walterluu 3:85fc843a9d7d 17 #define CONVERT_T_DELAY 30
walterluu 3:85fc843a9d7d 18
walterluu 3:85fc843a9d7d 19 // MAX44009 related
walterluu 3:85fc843a9d7d 20 #include "MAX44009_lib.h"
walterluu 3:85fc843a9d7d 21
walterluu 3:85fc843a9d7d 22 // AO32 related
walterluu 3:85fc843a9d7d 23 #include "AO32_lib.h"
walterluu 3:85fc843a9d7d 24
walterluu 6:51f492ca61a2 25 // AO19 related
walterluu 6:51f492ca61a2 26 #include "AO19_lib.h"
walterluu 6:51f492ca61a2 27
walterluu 7:c0872971aef4 28 // Low Power Mode
walterluu 7:c0872971aef4 29 #include "lp.h"
walterluu 7:c0872971aef4 30 #include "mxc_config.h"
walterluu 7:c0872971aef4 31 #include "lp.h"
walterluu 7:c0872971aef4 32 #include "rtc.h"
walterluu 7:c0872971aef4 33 //#include "board.h" // Cannot find source?
walterluu 7:c0872971aef4 34 #define LP1_WakeTime 3 //seconds
walterluu 7:c0872971aef4 35 #define LP0_WakeTime 3 //seconds
walterluu 0:664d9df11a00 36
walterluu 3:85fc843a9d7d 37 // Virtual COM related
walterluu 3:85fc843a9d7d 38 #include "USBSerial.h" // for virtual COM
walterluu 0:664d9df11a00 39 #define BS 8 // ASCII Back Space
walterluu 0:664d9df11a00 40 #define CR 13 // ASCII Carriage Return
walterluu 0:664d9df11a00 41
walterluu 0:664d9df11a00 42 // Virtual serial port over USB TODO NEW VID PID NEEDED!!
walterluu 0:664d9df11a00 43 USBSerial pc(0x0B6A, 0x0042, 0x0001, false);
walterluu 0:664d9df11a00 44
walterluu 3:85fc843a9d7d 45 /***************************************************************************
walterluu 3:85fc843a9d7d 46 * LEDs Instantiation
walterluu 3:85fc843a9d7d 47 **************************************************************************/
walterluu 0:664d9df11a00 48 DigitalOut myled(LED2); // LED = LED2 green
walterluu 0:664d9df11a00 49 DigitalOut rLED(LED1); // red LED
walterluu 0:664d9df11a00 50 DigitalOut bLED(LED3); // blue LED
walterluu 3:85fc843a9d7d 51 //DigitalOut *led3;
walterluu 0:664d9df11a00 52
walterluu 3:85fc843a9d7d 53 /***************************************************************************
walterluu 3:85fc843a9d7d 54 * I2C Bus Instantiation
walterluu 3:85fc843a9d7d 55 **************************************************************************/
walterluu 3:85fc843a9d7d 56 I2C i2cBus0(P1_6, P1_7); // I2C of MAX32625PICO
walterluu 0:664d9df11a00 57
walterluu 0:664d9df11a00 58
walterluu 6:51f492ca61a2 59 bool get_data_flag = false; // used for data tramission frequency on the SENSOR side
walterluu 6:51f492ca61a2 60 bool print_data_flag = false; // used for data display on the GATEWAY side
walterluu 0:664d9df11a00 61
walterluu 3:85fc843a9d7d 62 //Timer setup
walterluu 6:51f492ca61a2 63 Ticker timer_1; // timer for data tramission frequency on the SENSOR side
walterluu 6:51f492ca61a2 64 Ticker timer_M; // timer for data print out on the GATEWAY side
walterluu 0:664d9df11a00 65
walterluu 3:85fc843a9d7d 66 void onTimerInterrupt(){
walterluu 3:85fc843a9d7d 67 get_data_flag = true;
walterluu 3:85fc843a9d7d 68 }
walterluu 0:664d9df11a00 69
walterluu 6:51f492ca61a2 70 void onGatewayInterrupt(){
walterluu 6:51f492ca61a2 71 print_data_flag = true;
walterluu 6:51f492ca61a2 72 }
walterluu 6:51f492ca61a2 73
walterluu 7:c0872971aef4 74 // *****************************************************************************
walterluu 7:c0872971aef4 75 void RTC_Setup()
walterluu 7:c0872971aef4 76 {
walterluu 7:c0872971aef4 77 rtc_cfg_t RTCconfig;
walterluu 7:c0872971aef4 78
walterluu 7:c0872971aef4 79 RTCconfig.compareCount[0] = LP0_WakeTime;//3 second timer
walterluu 7:c0872971aef4 80 // RTCconfig.compareCount[1] = LP1_WakeTime; //3 second timer
walterluu 7:c0872971aef4 81 RTCconfig.prescaler = RTC_PRESCALE_DIV_2_12; //1Hz clock
walterluu 7:c0872971aef4 82 RTCconfig.prescalerMask = RTC_PRESCALE_DIV_2_12;//used for prescaler compare
walterluu 7:c0872971aef4 83 RTCconfig.snoozeCount = 0;
walterluu 7:c0872971aef4 84 RTCconfig.snoozeMode = RTC_SNOOZE_DISABLE;
walterluu 7:c0872971aef4 85
walterluu 7:c0872971aef4 86 RTC_Init(&RTCconfig);
walterluu 7:c0872971aef4 87
walterluu 7:c0872971aef4 88 RTC_Start();
walterluu 7:c0872971aef4 89 }
walterluu 7:c0872971aef4 90
walterluu 7:c0872971aef4 91 //void Wakeup_LP1() {
walterluu 7:c0872971aef4 92 //
walterluu 7:c0872971aef4 93 // /***************************************************************************
walterluu 7:c0872971aef4 94 // * Temperature Sensor Data Measurement
walterluu 7:c0872971aef4 95 // **************************************************************************/
walterluu 7:c0872971aef4 96 // // obtain register hex values
walterluu 7:c0872971aef4 97 // convert_temperature(&i2cBus0, OT07_i2c_add); //send OW convert selected device
walterluu 7:c0872971aef4 98 // wait_ms(CONVERT_T_DELAY); //wait 20 ms for convert temperature to complete
walterluu 7:c0872971aef4 99 // int temp_error = OT07_read_register(&i2cBus0, OT07_i2c_add, OT07_FIFO_DATA, rawtempdata, 2);
walterluu 7:c0872971aef4 100 // double tempFinal = calc_temperature(rawtempdata);
walterluu 7:c0872971aef4 101 //
walterluu 7:c0872971aef4 102 // //fill raw temp data into the array
walterluu 7:c0872971aef4 103 // curr_raw_temp_to_master[0] = rawtempdata[0];
walterluu 7:c0872971aef4 104 // curr_raw_temp_to_master[1] = rawtempdata[1];
walterluu 7:c0872971aef4 105 //
walterluu 7:c0872971aef4 106 // /***************************************************************************
walterluu 7:c0872971aef4 107 // * Light Intensity Sensor Data Measurement
walterluu 7:c0872971aef4 108 // **************************************************************************/
walterluu 7:c0872971aef4 109 // // obtain register hex values
walterluu 7:c0872971aef4 110 // int lux_error = MAX44009_read_lux_register(&i2cBus0, MAX44009_i2c_add, MAX44009_LUX_HI, rawluxdata);
walterluu 7:c0872971aef4 111 // int luxFinal = (int) (calc_lux(rawluxdata));
walterluu 7:c0872971aef4 112 //
walterluu 7:c0872971aef4 113 // //fill raw lux data into the array
walterluu 7:c0872971aef4 114 // curr_raw_light_to_master[0] = rawluxdata[0];
walterluu 7:c0872971aef4 115 // curr_raw_light_to_master[1] = rawluxdata[1];
walterluu 7:c0872971aef4 116 //
walterluu 7:c0872971aef4 117 // /***************************************************************************
walterluu 7:c0872971aef4 118 // * Solar Harvester Data Measurement
walterluu 7:c0872971aef4 119 // **************************************************************************/
walterluu 7:c0872971aef4 120 //
walterluu 7:c0872971aef4 121 // int ocv_error = AO32_read_register(&i2cBus0, AO32_i2c_add, AO32_VOC, rawOCVdata);
walterluu 7:c0872971aef4 122 // int cnt_error = AO32_read_register(&i2cBus0, AO32_i2c_add, AO32_HARV_H, rawCntdata, 2); // burst read 2 bytes
walterluu 7:c0872971aef4 123 //
walterluu 7:c0872971aef4 124 // //calculate open circuit voltage from data
walterluu 7:c0872971aef4 125 // double OCVFinal = calc_OCV(rawOCVdata);
walterluu 7:c0872971aef4 126 //
walterluu 7:c0872971aef4 127 // //calculate harvesting counts from data
walterluu 7:c0872971aef4 128 // int countFinal = calc_Harvest(rawCntdata);
walterluu 7:c0872971aef4 129 //
walterluu 7:c0872971aef4 130 // //fill raw AO32 data into the array
walterluu 7:c0872971aef4 131 // curr_raw_AO32_to_master[0] = rawOCVdata[0]; // Fill OCV hex first
walterluu 7:c0872971aef4 132 // curr_raw_AO32_to_master[1] = rawCntdata[0]; // Fill Harvesting count high byte
walterluu 7:c0872971aef4 133 // curr_raw_AO32_to_master[2] = rawCntdata[1]; // Fill Harvesting count low byte
walterluu 7:c0872971aef4 134 //
walterluu 7:c0872971aef4 135 // // print out sensor data
walterluu 7:c0872971aef4 136 // pc.printf("SENSOR: [%.3f] [%d] [%.2f] [%d] [0]\r\n", tempFinal, luxFinal, OCVFinal, countFinal);
walterluu 7:c0872971aef4 137 //
walterluu 7:c0872971aef4 138 // /***************************************************************************
walterluu 7:c0872971aef4 139 // * Fill Payload Buffer With Data From Main Program Buffers for next LoRa Transmition
walterluu 7:c0872971aef4 140 // **************************************************************************/
walterluu 7:c0872971aef4 141 // memcpy(&BufferTx[tx_idx_signature], PongMsg, size_signature);
walterluu 7:c0872971aef4 142 // memcpy(&BufferTx[tx_idx_MAX30208], curr_raw_temp_to_master, size_of_MAX30208);
walterluu 7:c0872971aef4 143 // memcpy(&BufferTx[tx_idx_MAX44009], curr_raw_light_to_master, size_of_MAX44009);
walterluu 7:c0872971aef4 144 // memcpy(&BufferTx[tx_idx_MAX20361], curr_raw_AO32_to_master, size_of_MAX20361);
walterluu 7:c0872971aef4 145 //
walterluu 7:c0872971aef4 146 // /***************************************************************************
walterluu 7:c0872971aef4 147 // * LoRa Communication: Send Sensor Data
walterluu 7:c0872971aef4 148 // **************************************************************************/
walterluu 7:c0872971aef4 149 // SX1276SlaveSendData();
walterluu 7:c0872971aef4 150 //}
walterluu 7:c0872971aef4 151
walterluu 0:664d9df11a00 152 int main() {
walterluu 0:664d9df11a00 153
walterluu 3:85fc843a9d7d 154 /***************************************************************************
walterluu 3:85fc843a9d7d 155
walterluu 3:85fc843a9d7d 156 /* Setup begins here: */
walterluu 3:85fc843a9d7d 157 #if MASTER == 1 // Master Device
walterluu 7:c0872971aef4 158 rLED = LED_ON; // red LED on
walterluu 7:c0872971aef4 159 myled = LED_OFF;
walterluu 7:c0872971aef4 160 bLED = LED_OFF;
walterluu 3:85fc843a9d7d 161 #elif SLAVE == 1 // Slave Device
walterluu 7:c0872971aef4 162 rLED = LED_OFF;
walterluu 7:c0872971aef4 163 myled = LED_OFF;
walterluu 7:c0872971aef4 164 bLED = LED_ON; // blue LED on
walterluu 7:c0872971aef4 165
walterluu 7:c0872971aef4 166 //configure RTC and start for slave
walterluu 7:c0872971aef4 167 RTC_Setup();
walterluu 7:c0872971aef4 168
walterluu 7:c0872971aef4 169 // sleep(); // put slave in sleep mode LP2
walterluu 7:c0872971aef4 170
walterluu 3:85fc843a9d7d 171 #endif
walterluu 3:85fc843a9d7d 172
walterluu 3:85fc843a9d7d 173 /***************************************************************************
walterluu 3:85fc843a9d7d 174 * Combined Payload Buffers for LoRa Communications
walterluu 3:85fc843a9d7d 175 **************************************************************************/
walterluu 3:85fc843a9d7d 176 uint8_t BufferTx[BufferSizeTx]; // more info in global_buffers.h
walterluu 3:85fc843a9d7d 177 uint8_t BufferRx[BufferSizeRx]; // more info in global_buffers.h
walterluu 3:85fc843a9d7d 178
walterluu 3:85fc843a9d7d 179 /***************************************************************************
walterluu 3:85fc843a9d7d 180 * Identification Buffers
walterluu 3:85fc843a9d7d 181 **************************************************************************/
walterluu 3:85fc843a9d7d 182 #if MASTER == 1 // Master Device
walterluu 3:85fc843a9d7d 183 uint8_t ID_of_slave[size_signature];
walterluu 3:85fc843a9d7d 184 #elif SLAVE == 1 // Slave Device
walterluu 3:85fc843a9d7d 185 uint8_t ID_of_master[size_signature];
walterluu 3:85fc843a9d7d 186 #endif
walterluu 3:85fc843a9d7d 187
walterluu 3:85fc843a9d7d 188 /***************************************************************************
walterluu 3:85fc843a9d7d 189 * MAX30208 Data Buffers
walterluu 3:85fc843a9d7d 190 **************************************************************************/
walterluu 3:85fc843a9d7d 191 #if MASTER == 1 // Master Device
walterluu 5:9e751733a6f3 192 char curr_raw_temp_from_slave[size_of_MAX30208]; // to match data type
walterluu 6:51f492ca61a2 193 char prev_raw_temp_from_slave[size_of_MAX30208];
walterluu 3:85fc843a9d7d 194 #elif SLAVE == 1 // Slave Device
walterluu 3:85fc843a9d7d 195 uint8_t curr_raw_temp_to_master[size_of_MAX30208];
walterluu 3:85fc843a9d7d 196 #endif
walterluu 3:85fc843a9d7d 197
walterluu 0:664d9df11a00 198
walterluu 3:85fc843a9d7d 199 /***************************************************************************
walterluu 3:85fc843a9d7d 200 * MAX44009 Data Buffers
walterluu 3:85fc843a9d7d 201 **************************************************************************/
walterluu 3:85fc843a9d7d 202 #if MASTER == 1 // Master Device
walterluu 5:9e751733a6f3 203 char curr_raw_light_from_slave[size_of_MAX44009]; // to match data type
walterluu 6:51f492ca61a2 204 char prev_raw_light_from_slave[size_of_MAX44009];
walterluu 3:85fc843a9d7d 205 #elif SLAVE == 1 // Slave Device
walterluu 3:85fc843a9d7d 206 uint8_t curr_raw_light_to_master[size_of_MAX44009];
walterluu 3:85fc843a9d7d 207 #endif
walterluu 3:85fc843a9d7d 208
walterluu 3:85fc843a9d7d 209 /***************************************************************************
walterluu 3:85fc843a9d7d 210 * MAX20361 Data Buffers
walterluu 3:85fc843a9d7d 211 **************************************************************************/
walterluu 3:85fc843a9d7d 212 #if MASTER == 1 // Master Device
walterluu 5:9e751733a6f3 213 char curr_raw_AO32_from_slave[size_of_MAX20361]; // to match data type
walterluu 6:51f492ca61a2 214 char prev_raw_AO32_from_slave[size_of_MAX20361];
walterluu 3:85fc843a9d7d 215 #elif SLAVE == 1 // Slave Device
walterluu 3:85fc843a9d7d 216 uint8_t curr_raw_AO32_to_master[size_of_MAX20361];
walterluu 3:85fc843a9d7d 217 #endif
walterluu 3:85fc843a9d7d 218
walterluu 3:85fc843a9d7d 219 /***************************************************************************
walterluu 3:85fc843a9d7d 220 * Finish Setting up LoRa Radios: This passes in pointers to Buffers to send
walterluu 3:85fc843a9d7d 221 **************************************************************************/
walterluu 3:85fc843a9d7d 222 SX1276PingPongSetup(BufferTx, BufferRx, &pc);
walterluu 3:85fc843a9d7d 223
walterluu 7:c0872971aef4 224 // Pointer Tutorial
walterluu 3:85fc843a9d7d 225 // https://www.tutorialspoint.com/cprogramming/c_pointers.htm
walterluu 3:85fc843a9d7d 226
walterluu 3:85fc843a9d7d 227 /***************************************************************************
walterluu 3:85fc843a9d7d 228 * Create Dummy Data For Master and Slave
walterluu 3:85fc843a9d7d 229 **************************************************************************/
walterluu 0:664d9df11a00 230
walterluu 3:85fc843a9d7d 231 #if SLAVE == 1
walterluu 7:c0872971aef4 232 // curr_raw_temp_to_master[0] = 99;
walterluu 7:c0872971aef4 233 // curr_raw_temp_to_master[1] = 99;
walterluu 7:c0872971aef4 234 //
walterluu 7:c0872971aef4 235 // curr_raw_light_to_master[0] = 25;
walterluu 7:c0872971aef4 236 // curr_raw_light_to_master[1] = 26;
walterluu 7:c0872971aef4 237 //
walterluu 7:c0872971aef4 238 // curr_raw_AO32_to_master[0] = 99;
walterluu 7:c0872971aef4 239 // curr_raw_AO32_to_master[1] = 0;
walterluu 7:c0872971aef4 240 // curr_raw_AO32_to_master[2] = 101;
walterluu 7:c0872971aef4 241 // curr_raw_AO32_to_master[3] = 102;
walterluu 3:85fc843a9d7d 242 #endif
walterluu 3:85fc843a9d7d 243
walterluu 3:85fc843a9d7d 244 /***************************************************************************
walterluu 3:85fc843a9d7d 245 * Loop Counter
walterluu 3:85fc843a9d7d 246 **************************************************************************/
walterluu 3:85fc843a9d7d 247 int loopCnt = 0;
walterluu 3:85fc843a9d7d 248
walterluu 6:51f492ca61a2 249 #if MASTER == 1
walterluu 6:51f492ca61a2 250 //************* init ticker timer callbacks ****************
walterluu 6:51f492ca61a2 251 timer_M.attach(&onGatewayInterrupt, 3); //Gateway data print out frequency
walterluu 6:51f492ca61a2 252 #endif
walterluu 6:51f492ca61a2 253
walterluu 3:85fc843a9d7d 254 #if SLAVE == 1
walterluu 3:85fc843a9d7d 255 //************* init ticker timer callbacks ****************
walterluu 7:c0872971aef4 256 timer_1.attach(&onTimerInterrupt, 10); //Sensor data transmission frequency
walterluu 3:85fc843a9d7d 257
walterluu 6:51f492ca61a2 258 //************* OT07 Variables ****************
walterluu 3:85fc843a9d7d 259 char rawtempdata[2];
walterluu 3:85fc843a9d7d 260 char OT07_i2c_add = 0xA0;
walterluu 3:85fc843a9d7d 261
walterluu 3:85fc843a9d7d 262 //************* MAX44009 Variables ****************
walterluu 3:85fc843a9d7d 263 char rawluxdata[2];
walterluu 5:9e751733a6f3 264 char MAX44009_i2c_add = 0x94; // 0b1001 010x
walterluu 3:85fc843a9d7d 265
walterluu 3:85fc843a9d7d 266 //************* AO32 Variables ****************
walterluu 3:85fc843a9d7d 267 char rawOCVdata[2]; // only needs 1
walterluu 3:85fc843a9d7d 268 char rawCntdata[2];
walterluu 3:85fc843a9d7d 269 char AO32_i2c_add = 0x2A; // 0b0010 101x
walterluu 3:85fc843a9d7d 270
walterluu 6:51f492ca61a2 271 //************* AO19 Settings ****************
walterluu 6:51f492ca61a2 272 // Enable AO19
walterluu 6:51f492ca61a2 273 char AO19_i2c_add = 0xD0; //
walterluu 6:51f492ca61a2 274 char AO19_BB_CFG[2]; // store result of AO19's register 0x01 BBstCfg0
walterluu 6:51f492ca61a2 275 int AO19_read = AO19_read_register(&i2cBus0, AO19_i2c_add, AO19_BB_CFG0, AO19_BB_CFG);
walterluu 6:51f492ca61a2 276
walterluu 6:51f492ca61a2 277 int AO19_en = AO19_write_register(&i2cBus0, AO19_i2c_add, AO19_BB_CFG0, AO19_BB_CFG[0]|0x80);
walterluu 6:51f492ca61a2 278
walterluu 6:51f492ca61a2 279 // if (AO19_en) {
walterluu 6:51f492ca61a2 280 // pc.printf("AO19 Enabled fail!\r\n");
walterluu 6:51f492ca61a2 281 // }
walterluu 6:51f492ca61a2 282 // else {
walterluu 6:51f492ca61a2 283 // pc.printf("AO19 Enabledsuccess!\r\n");
walterluu 6:51f492ca61a2 284 // }
walterluu 6:51f492ca61a2 285
walterluu 6:51f492ca61a2 286
walterluu 0:664d9df11a00 287
walterluu 7:c0872971aef4 288 // LP0 Wake Up call
walterluu 7:c0872971aef4 289 if(LP_IsLP0WakeUp()){
walterluu 3:85fc843a9d7d 290
walterluu 7:c0872971aef4 291 /***************************************************************************
walterluu 3:85fc843a9d7d 292 * Temperature Sensor Data Measurement
walterluu 3:85fc843a9d7d 293 **************************************************************************/
walterluu 3:85fc843a9d7d 294 // obtain register hex values
walterluu 3:85fc843a9d7d 295 convert_temperature(&i2cBus0, OT07_i2c_add); //send OW convert selected device
walterluu 3:85fc843a9d7d 296 wait_ms(CONVERT_T_DELAY); //wait 20 ms for convert temperature to complete
walterluu 3:85fc843a9d7d 297 int temp_error = OT07_read_register(&i2cBus0, OT07_i2c_add, OT07_FIFO_DATA, rawtempdata, 2);
walterluu 5:9e751733a6f3 298 double tempFinal = calc_temperature(rawtempdata);
walterluu 3:85fc843a9d7d 299
walterluu 3:85fc843a9d7d 300 //fill raw temp data into the array
walterluu 3:85fc843a9d7d 301 curr_raw_temp_to_master[0] = rawtempdata[0];
walterluu 3:85fc843a9d7d 302 curr_raw_temp_to_master[1] = rawtempdata[1];
walterluu 0:664d9df11a00 303
walterluu 3:85fc843a9d7d 304 /***************************************************************************
walterluu 3:85fc843a9d7d 305 * Light Intensity Sensor Data Measurement
walterluu 3:85fc843a9d7d 306 **************************************************************************/
walterluu 3:85fc843a9d7d 307 // obtain register hex values
walterluu 5:9e751733a6f3 308 int lux_error = MAX44009_read_lux_register(&i2cBus0, MAX44009_i2c_add, MAX44009_LUX_HI, rawluxdata);
walterluu 6:51f492ca61a2 309 int luxFinal = (int) (calc_lux(rawluxdata));
walterluu 3:85fc843a9d7d 310
walterluu 3:85fc843a9d7d 311 //fill raw lux data into the array
walterluu 5:9e751733a6f3 312 curr_raw_light_to_master[0] = rawluxdata[0];
walterluu 5:9e751733a6f3 313 curr_raw_light_to_master[1] = rawluxdata[1];
walterluu 3:85fc843a9d7d 314
walterluu 3:85fc843a9d7d 315 /***************************************************************************
walterluu 3:85fc843a9d7d 316 * Solar Harvester Data Measurement
walterluu 3:85fc843a9d7d 317 **************************************************************************/
walterluu 3:85fc843a9d7d 318
walterluu 3:85fc843a9d7d 319 int ocv_error = AO32_read_register(&i2cBus0, AO32_i2c_add, AO32_VOC, rawOCVdata);
walterluu 3:85fc843a9d7d 320 int cnt_error = AO32_read_register(&i2cBus0, AO32_i2c_add, AO32_HARV_H, rawCntdata, 2); // burst read 2 bytes
walterluu 3:85fc843a9d7d 321
walterluu 3:85fc843a9d7d 322 //calculate open circuit voltage from data
walterluu 7:c0872971aef4 323 // double voltage = (double)(rawOCVdata[0]) / 100;
walterluu 5:9e751733a6f3 324 double OCVFinal = calc_OCV(rawOCVdata);
walterluu 3:85fc843a9d7d 325
walterluu 3:85fc843a9d7d 326 //calculate harvesting counts from data
walterluu 5:9e751733a6f3 327 int countFinal = calc_Harvest(rawCntdata);
walterluu 3:85fc843a9d7d 328
walterluu 3:85fc843a9d7d 329 //fill raw AO32 data into the array
walterluu 5:9e751733a6f3 330 curr_raw_AO32_to_master[0] = rawOCVdata[0]; // Fill OCV hex first
walterluu 5:9e751733a6f3 331 curr_raw_AO32_to_master[1] = rawCntdata[0]; // Fill Harvesting count high byte
walterluu 5:9e751733a6f3 332 curr_raw_AO32_to_master[2] = rawCntdata[1]; // Fill Harvesting count low byte
walterluu 0:664d9df11a00 333
walterluu 6:51f492ca61a2 334 // print out sensor data
walterluu 6:51f492ca61a2 335 pc.printf("SENSOR: [%.3f] [%d] [%.2f] [%d] [0]\r\n", tempFinal, luxFinal, OCVFinal, countFinal);
walterluu 6:51f492ca61a2 336
walterluu 3:85fc843a9d7d 337 /***************************************************************************
walterluu 3:85fc843a9d7d 338 * Fill Payload Buffer With Data From Main Program Buffers for next LoRa Transmition
walterluu 3:85fc843a9d7d 339 **************************************************************************/
walterluu 3:85fc843a9d7d 340 memcpy(&BufferTx[tx_idx_signature], PongMsg, size_signature);
walterluu 3:85fc843a9d7d 341 memcpy(&BufferTx[tx_idx_MAX30208], curr_raw_temp_to_master, size_of_MAX30208);
walterluu 3:85fc843a9d7d 342 memcpy(&BufferTx[tx_idx_MAX44009], curr_raw_light_to_master, size_of_MAX44009);
walterluu 3:85fc843a9d7d 343 memcpy(&BufferTx[tx_idx_MAX20361], curr_raw_AO32_to_master, size_of_MAX20361);
walterluu 7:c0872971aef4 344
walterluu 3:85fc843a9d7d 345 /***************************************************************************
walterluu 3:85fc843a9d7d 346 * LoRa Communication: Send Sensor Data
walterluu 3:85fc843a9d7d 347 **************************************************************************/
walterluu 7:c0872971aef4 348 SX1276SlaveSendData();
walterluu 3:85fc843a9d7d 349
walterluu 7:c0872971aef4 350 }
walterluu 7:c0872971aef4 351
walterluu 7:c0872971aef4 352
walterluu 7:c0872971aef4 353
walterluu 7:c0872971aef4 354 #endif
walterluu 7:c0872971aef4 355
walterluu 7:c0872971aef4 356 while (1) {
walterluu 7:c0872971aef4 357
walterluu 7:c0872971aef4 358 #if SLAVE == 1
walterluu 3:85fc843a9d7d 359
walterluu 3:85fc843a9d7d 360
walterluu 7:c0872971aef4 361 /***************************************************************************
walterluu 7:c0872971aef4 362 * LP0 Experiment
walterluu 7:c0872971aef4 363 **************************************************************************/
walterluu 7:c0872971aef4 364 //Clear existing wake-up config
walterluu 7:c0872971aef4 365 LP_ClearWakeUpConfig();
walterluu 7:c0872971aef4 366
walterluu 7:c0872971aef4 367 //Clear any event flags
walterluu 7:c0872971aef4 368 LP_ClearWakeUpFlags();
walterluu 7:c0872971aef4 369
walterluu 7:c0872971aef4 370 //configure wake-up on RTC compare 0
walterluu 7:c0872971aef4 371 LP_ConfigRTCWakeUp(1, 0, 0, 0);
walterluu 7:c0872971aef4 372
walterluu 7:c0872971aef4 373
walterluu 7:c0872971aef4 374 //set RTC compare 0 value
walterluu 7:c0872971aef4 375 uint32_t cmp = RTC_GetCount() + LP0_WakeTime;
walterluu 7:c0872971aef4 376 RTC_SetCompare(0,cmp);
walterluu 7:c0872971aef4 377 RTC_ClearFlags(MXC_F_RTC_FLAGS_COMP0);
walterluu 7:c0872971aef4 378
walterluu 7:c0872971aef4 379 //interrupts are disabled in LP_EnterLP0
walterluu 7:c0872971aef4 380 LP_EnterLP0();
walterluu 7:c0872971aef4 381
walterluu 7:c0872971aef4 382 //firmware will reset on wake-up
walterluu 7:c0872971aef4 383
walterluu 7:c0872971aef4 384 #endif // end define for Slave
walterluu 7:c0872971aef4 385
walterluu 7:c0872971aef4 386
walterluu 7:c0872971aef4 387 #if MASTER == 1 // only Receive Sensor Data
walterluu 3:85fc843a9d7d 388 /***************************************************************************
walterluu 3:85fc843a9d7d 389 * Fill Payload Buffer With Data From Main Program Buffers for next LoRa Transmition
walterluu 3:85fc843a9d7d 390 **************************************************************************/
walterluu 7:c0872971aef4 391 memcpy(&BufferTx[tx_idx_signature], PingMsg, size_signature); // no need
walterluu 3:85fc843a9d7d 392
walterluu 3:85fc843a9d7d 393 /***************************************************************************
walterluu 3:85fc843a9d7d 394 * LoRa Communication: Gateway Receive Sensor Data
walterluu 3:85fc843a9d7d 395 **************************************************************************/
walterluu 7:c0872971aef4 396 // SX1276PingPong();
walterluu 7:c0872971aef4 397 if(SX1276MasterCheckForNewData())
walterluu 7:c0872971aef4 398 {
walterluu 3:85fc843a9d7d 399
walterluu 3:85fc843a9d7d 400 /***************************************************************************
walterluu 3:85fc843a9d7d 401 * Fill Main Program Buffers With Data From Received Payload Buffer
walterluu 3:85fc843a9d7d 402 **************************************************************************/
walterluu 3:85fc843a9d7d 403 memcpy(ID_of_slave, &BufferRx[rx_idx_signature], size_signature);
walterluu 3:85fc843a9d7d 404 memcpy(curr_raw_temp_from_slave, &BufferRx[rx_idx_MAX30208], size_of_MAX30208);
walterluu 3:85fc843a9d7d 405 memcpy(curr_raw_light_from_slave, &BufferRx[rx_idx_MAX44009], size_of_MAX44009);
walterluu 3:85fc843a9d7d 406 memcpy(curr_raw_AO32_from_slave, &BufferRx[rx_idx_MAX20361], size_of_MAX20361);
walterluu 3:85fc843a9d7d 407
walterluu 3:85fc843a9d7d 408 /***************************************************************************
walterluu 3:85fc843a9d7d 409 * Master Device: Print out Slave Data
walterluu 3:85fc843a9d7d 410 **************************************************************************/
walterluu 3:85fc843a9d7d 411
walterluu 5:9e751733a6f3 412 double tempResult = calc_temperature(curr_raw_temp_from_slave);
walterluu 6:51f492ca61a2 413 int luxResult = (int) calc_lux(curr_raw_light_from_slave);
walterluu 5:9e751733a6f3 414
walterluu 5:9e751733a6f3 415 char OCVrawHex[2];
walterluu 5:9e751733a6f3 416 OCVrawHex[0] = curr_raw_AO32_from_slave[0];
walterluu 5:9e751733a6f3 417 OCVrawHex[1] = curr_raw_AO32_from_slave[1];
walterluu 5:9e751733a6f3 418 char CntrawHex[2];
walterluu 5:9e751733a6f3 419 CntrawHex[0] = curr_raw_AO32_from_slave[2];
walterluu 5:9e751733a6f3 420 CntrawHex[1] = curr_raw_AO32_from_slave[3];
walterluu 5:9e751733a6f3 421
walterluu 5:9e751733a6f3 422 double OCVResult = calc_OCV(OCVrawHex);
walterluu 5:9e751733a6f3 423 int CntResult = calc_Harvest(CntrawHex);
walterluu 7:c0872971aef4 424
walterluu 7:c0872971aef4 425 //reset the flag
walterluu 7:c0872971aef4 426 print_data_flag = false; // no need
walterluu 7:c0872971aef4 427 pc.printf("MSG: [%.3f] [%d] [%.2f] [%d] [0]\r\n", tempResult, luxResult, OCVResult, CntResult);
walterluu 3:85fc843a9d7d 428
walterluu 7:c0872971aef4 429 } // end of SX1276MasterCheckForNewData
walterluu 7:c0872971aef4 430
walterluu 3:85fc843a9d7d 431 loopCnt = loopCnt + 1;
walterluu 3:85fc843a9d7d 432
walterluu 7:c0872971aef4 433 #endif // end define for Master
walterluu 7:c0872971aef4 434
walterluu 7:c0872971aef4 435 // sleep(); // going back to sleep mode
walterluu 3:85fc843a9d7d 436
walterluu 3:85fc843a9d7d 437 } // end of while(1) loop
walterluu 3:85fc843a9d7d 438
walterluu 6:51f492ca61a2 439 } // end of main()