added other files

Dependencies:   lib_gps lib_mpl3115a2 lmic_MOTE_L152RC mbed

Fork of lmic_NAmote_GPS_tjm by Timothy Mulrooney

Committer:
jlcolemanmbed
Date:
Thu Feb 18 01:41:02 2016 +0000
Revision:
10:a4c6c9b33dce
Parent:
9:f4d52e9625f7
updated config.h

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dudmuck 0:a5e3347bad61 1 /*
dudmuck 0:a5e3347bad61 2 / _____) _ | |
dudmuck 0:a5e3347bad61 3 ( (____ _____ ____ _| |_ _____ ____| |__
dudmuck 0:a5e3347bad61 4 \____ \| ___ | (_ _) ___ |/ ___) _ \
dudmuck 0:a5e3347bad61 5 _____) ) ____| | | || |_| ____( (___| | | |
dudmuck 0:a5e3347bad61 6 (______/|_____)_|_|_| \__)_____)\____)_| |_|
dudmuck 0:a5e3347bad61 7 (C)2013 Semtech
dudmuck 0:a5e3347bad61 8
dudmuck 0:a5e3347bad61 9 Description: MBED example application
dudmuck 0:a5e3347bad61 10
dudmuck 0:a5e3347bad61 11 License: Revised BSD License, see LICENSE.TXT file include in the project
dudmuck 0:a5e3347bad61 12
dudmuck 0:a5e3347bad61 13 Maintainer: Miguel Luis and Gregory Cristian
dudmuck 0:a5e3347bad61 14 */
dudmuck 0:a5e3347bad61 15 #include "mbed.h"
dudmuck 0:a5e3347bad61 16 #include "lmic.h"
dudmuck 0:a5e3347bad61 17 #include "mpl3115a2.h"
dudmuck 0:a5e3347bad61 18 #include "gps.h"
dudmuck 0:a5e3347bad61 19 #include "debug.h"
dudmuck 0:a5e3347bad61 20
jlcolemanmbed 9:f4d52e9625f7 21 #define LORA_IT
jlcolemanmbed 9:f4d52e9625f7 22 //#define SENET_F
dudmuck 6:0b2b2d196ddd 23 //#define SMTC
dudmuck 0:a5e3347bad61 24
dudmuck 2:a8f00db60fdb 25 typedef enum {
dudmuck 2:a8f00db60fdb 26 MOTE_NONE = 0,
dudmuck 2:a8f00db60fdb 27 MOTE_V2,
dudmuck 2:a8f00db60fdb 28 MOTE_V3
dudmuck 2:a8f00db60fdb 29 } mote_version_e;
dudmuck 2:a8f00db60fdb 30 mote_version_e mote_version = MOTE_NONE;
dudmuck 2:a8f00db60fdb 31
tmulrooney 8:1069ed1d4464 32 //DigitalOut pc_7(PTC1); /* *** TODO *** */
tmulrooney 8:1069ed1d4464 33 //DigitalIn pc_1(PTC1); /* *** TODO *** */
dudmuck 2:a8f00db60fdb 34
dudmuck 2:a8f00db60fdb 35
dudmuck 0:a5e3347bad61 36 /* ****************************************** */
dudmuck 0:a5e3347bad61 37 /* ***** Basic App and Network Parameters *** */
dudmuck 0:a5e3347bad61 38 /* ****************************************** */
dudmuck 0:a5e3347bad61 39 // Hybrid Mode must be defined in lmic.h //
dudmuck 0:a5e3347bad61 40 // DevEUI and Keys defined by Activation type //
dudmuck 0:a5e3347bad61 41 #define APP_DATA_SIZE 11
dudmuck 0:a5e3347bad61 42 #define APP_ACK 0
dudmuck 4:f9aaf0f82c00 43 #define MS_DELAY_NEXT_TX 400
dudmuck 0:a5e3347bad61 44 #ifdef SMTC
dudmuck 4:f9aaf0f82c00 45 #define OVER_THE_AIR_ACTIVATION 1 //0
dudmuck 0:a5e3347bad61 46 #else
dudmuck 0:a5e3347bad61 47 #define OVER_THE_AIR_ACTIVATION 1
dudmuck 0:a5e3347bad61 48 #endif /* SMTC */
dudmuck 0:a5e3347bad61 49
dudmuck 0:a5e3347bad61 50 /* ***************************************** */
dudmuck 0:a5e3347bad61 51
tmulrooney 8:1069ed1d4464 52 #define LED_RED PTA1
tmulrooney 8:1069ed1d4464 53 #define LED_YEL PTA2
dudmuck 0:a5e3347bad61 54
dudmuck 0:a5e3347bad61 55 static DigitalOut led1(LED_RED);
dudmuck 0:a5e3347bad61 56 static DigitalOut led2(LED_YEL);
dudmuck 0:a5e3347bad61 57
dudmuck 0:a5e3347bad61 58 /* gps(tx, rx, en); */
tmulrooney 8:1069ed1d4464 59 //GPS gps(PTC1, PTC1, PTC1); /* *** TODO *** */
dudmuck 0:a5e3347bad61 60
tmulrooney 8:1069ed1d4464 61 I2C i2c(PTB3,PTB2);
tmulrooney 8:1069ed1d4464 62 //DigitalIn i2c_int_pin(PTC1);
tmulrooney 8:1069ed1d4464 63 //MPL3115A2 mpl3115a2(i2c, i2c_int_pin); /* *** TODO *** */
dudmuck 3:d81e4a63fb2e 64 AnalogIn *bat;
dudmuck 3:d81e4a63fb2e 65 #define LOW_BAT_THRESHOLD 3.45
dudmuck 0:a5e3347bad61 66
dudmuck 0:a5e3347bad61 67 //////////////////////////////////////////////////
dudmuck 0:a5e3347bad61 68 // CONFIGURATION (FOR APPLICATION CALLBACKS BELOW)
dudmuck 0:a5e3347bad61 69 //////////////////////////////////////////////////
jlcolemanmbed 9:f4d52e9625f7 70 #ifdef LORA_IT
jlcolemanmbed 9:f4d52e9625f7 71 // application router ID (LSBF)
jlcolemanmbed 9:f4d52e9625f7 72 //static const u1_t APPEUI[8] = { 0x01, 0x00, 0x01, 0x00, 0x00, 0x0c, 0x25, 0x00 };
jlcolemanmbed 9:f4d52e9625f7 73 static const u1_t reverse_APPEUI[8] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 };
jlcolemanmbed 9:f4d52e9625f7 74
jlcolemanmbed 9:f4d52e9625f7 75 // unique device ID (LSBF)
jlcolemanmbed 9:f4d52e9625f7 76 //static const u1_t DEVEUI[8] = { 0x0f, 0x00, 0x00, 0x00, 0x01, 0x0c, 0x25, 0x00 };
jlcolemanmbed 9:f4d52e9625f7 77 static const u1_t reverse_DEVEUI[8] = { 0x02, 0x00, 0x00, 0x00, 0x00, 0xee, 0xff, 0xc0 };
jlcolemanmbed 9:f4d52e9625f7 78
jlcolemanmbed 9:f4d52e9625f7 79 // device-specific AES key (derived from device EUI)
jlcolemanmbed 9:f4d52e9625f7 80 static const u1_t DEVKEY[16] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, };
jlcolemanmbed 9:f4d52e9625f7 81 // E4 -72 -71 -C5 -F5 -30 -A9 -9F -CF -C4 -0E -AB -EA -D7 -19 -42
jlcolemanmbed 9:f4d52e9625f7 82 #endif /* LORA_IT */
jlcolemanmbed 9:f4d52e9625f7 83
dudmuck 0:a5e3347bad61 84 #ifdef SENET_F
dudmuck 0:a5e3347bad61 85 // application router ID (LSBF)
dudmuck 6:0b2b2d196ddd 86 //static const u1_t APPEUI[8] = { 0x01, 0x00, 0x01, 0x00, 0x00, 0x0c, 0x25, 0x00 };
dudmuck 6:0b2b2d196ddd 87 static const u1_t reverse_APPEUI[8] = { 0x00, 0x25, 0x0C, 0x00, 0x00, 0x01, 0x00, 0x01 };
dudmuck 0:a5e3347bad61 88
dudmuck 0:a5e3347bad61 89 // unique device ID (LSBF)
dudmuck 6:0b2b2d196ddd 90 //static const u1_t DEVEUI[8] = { 0x0f, 0x00, 0x00, 0x00, 0x01, 0x0c, 0x25, 0x00 };
dudmuck 6:0b2b2d196ddd 91 static const u1_t reverse_DEVEUI[8] = { 0x00, 0x25, 0x0C, 0x01, 0x00, 0x00, 0x00, 0x0F };
dudmuck 0:a5e3347bad61 92
dudmuck 0:a5e3347bad61 93 // device-specific AES key (derived from device EUI)
dudmuck 0:a5e3347bad61 94 static const u1_t DEVKEY[16] = { 0xe4, 0x72, 0x71, 0xc5, 0xf5, 0x30, 0xa9, 0x9f, 0xcf, 0xc4, 0x0e, 0xab, 0xea, 0xd7, 0x19, 0x42, };
dudmuck 0:a5e3347bad61 95 // E4 -72 -71 -C5 -F5 -30 -A9 -9F -CF -C4 -0E -AB -EA -D7 -19 -42
dudmuck 0:a5e3347bad61 96 #endif /* SENET_F */
dudmuck 0:a5e3347bad61 97
dudmuck 0:a5e3347bad61 98 #ifdef SMTC
dudmuck 0:a5e3347bad61 99 // Semtech Activation (v1.x server)
dudmuck 0:a5e3347bad61 100 // application router ID (LSBF)
dudmuck 6:0b2b2d196ddd 101 static const u1_t reverse_APPEUI[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
dudmuck 6:0b2b2d196ddd 102 //AA-AA-AA-AA-FF-FF-FF-FF
dudmuck 6:0b2b2d196ddd 103 //static const u1_t APPEUI[8] = { 0xff, 0xff, 0xff, 0xff, 0xaa, 0xaa, 0xaa, 0xaa };
dudmuck 6:0b2b2d196ddd 104 //static const u1_t reverse_APPEUI[8] = { 0xaa, 0xaa, 0xaa, 0xaa, 0xff, 0xff, 0xff, 0xff };
dudmuck 0:a5e3347bad61 105
dudmuck 0:a5e3347bad61 106 // unique device ID (LSBF)
dudmuck 6:0b2b2d196ddd 107 //static const u1_t DEVEUI[8] = { 0x21, 0x00, 0x00, 0x00, 0x01, 0x0c, 0x25, 0x00 };
dudmuck 6:0b2b2d196ddd 108 static const u1_t reverse_DEVEUI[8] = { 0x00, 0x25, 0x0C, 0x01, 0x00, 0x00, 0x00, 0x20 };
dudmuck 6:0b2b2d196ddd 109 //static const u1_t DEVEUI[8] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
dudmuck 6:0b2b2d196ddd 110 //static const u1_t reverse_DEVEUI[8] = { 0xEF, 0xCD, 0xAB, 0x89, 0x67, 0x45, 0x23, 0x01 };
dudmuck 0:a5e3347bad61 111
dudmuck 0:a5e3347bad61 112 // device-specific AES key (derived from device EUI)
dudmuck 4:f9aaf0f82c00 113 static const u1_t DEVKEY[16] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
dudmuck 6:0b2b2d196ddd 114 //static const u1_t DEVKEY[16] = { 0xab, 0x89, 0xef, 0xcd, 0x23, 0x01, 0x67, 0x45, 0x54, 0x76, 0x10, 0x32, 0xdc, 0xfe, 0x98, 0xba };
dudmuck 0:a5e3347bad61 115
dudmuck 4:f9aaf0f82c00 116 /*static uint8_t NwkSKey[] =
dudmuck 0:a5e3347bad61 117 {
dudmuck 0:a5e3347bad61 118 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
dudmuck 0:a5e3347bad61 119 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
dudmuck 0:a5e3347bad61 120 };
dudmuck 0:a5e3347bad61 121
dudmuck 0:a5e3347bad61 122 static uint8_t ArtSKey[] =
dudmuck 0:a5e3347bad61 123 {
dudmuck 0:a5e3347bad61 124 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
dudmuck 0:a5e3347bad61 125 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
dudmuck 4:f9aaf0f82c00 126 };*/
dudmuck 0:a5e3347bad61 127
dudmuck 0:a5e3347bad61 128 #endif /* SMTC */
dudmuck 0:a5e3347bad61 129
dudmuck 0:a5e3347bad61 130 //////////////////////////////////////////////////
dudmuck 0:a5e3347bad61 131 // APPLICATION CALLBACKS
dudmuck 0:a5e3347bad61 132 //////////////////////////////////////////////////
dudmuck 0:a5e3347bad61 133
dudmuck 0:a5e3347bad61 134 // provide application router ID (8 bytes, LSBF)
tmulrooney 8:1069ed1d4464 135 void os_getArtEui (u1_t* buf)
tmulrooney 8:1069ed1d4464 136 {
tmulrooney 8:1069ed1d4464 137 debug("os_getArtEui Enter\r\n");
tmulrooney 8:1069ed1d4464 138 for(int i=0; i < 8; i++)
tmulrooney 8:1069ed1d4464 139 debug("%02X ",reverse_APPEUI[i]);
tmulrooney 8:1069ed1d4464 140 debug("\r\n");
dudmuck 6:0b2b2d196ddd 141 //memcpy(buf, APPEUI, 8);
dudmuck 6:0b2b2d196ddd 142 LMIC_reverse_memcpy(buf, reverse_APPEUI, 8);
dudmuck 0:a5e3347bad61 143 }
dudmuck 0:a5e3347bad61 144
dudmuck 0:a5e3347bad61 145 // provide device ID (8 bytes, LSBF)
dudmuck 0:a5e3347bad61 146 void os_getDevEui (u1_t* buf) {
tmulrooney 8:1069ed1d4464 147 debug("os_getDevEui Enter\r\n");
tmulrooney 8:1069ed1d4464 148 for(int i=0; i < 8; i++)
tmulrooney 8:1069ed1d4464 149 debug("%02X ",reverse_DEVEUI[i]);
tmulrooney 8:1069ed1d4464 150 debug("\r\n");
dudmuck 6:0b2b2d196ddd 151 //memcpy(buf, DEVEUI, 8);
dudmuck 6:0b2b2d196ddd 152 LMIC_reverse_memcpy(buf, reverse_DEVEUI, 8);
dudmuck 0:a5e3347bad61 153 }
dudmuck 0:a5e3347bad61 154
dudmuck 0:a5e3347bad61 155 // provide device key (16 bytes)
dudmuck 0:a5e3347bad61 156 void os_getDevKey (u1_t* buf) {
tmulrooney 8:1069ed1d4464 157 debug("os_getDevKey Enter\r\n");
tmulrooney 8:1069ed1d4464 158 for(int i=0; i < 16; i++)
tmulrooney 8:1069ed1d4464 159 debug("%02X ",DEVKEY[i]);
tmulrooney 8:1069ed1d4464 160 debug("\r\n");
tmulrooney 8:1069ed1d4464 161 for(int i=0; i < 16; i++)
tmulrooney 8:1069ed1d4464 162 debug("%c ",DEVKEY[i]);
tmulrooney 8:1069ed1d4464 163 debug("\r\n");
tmulrooney 8:1069ed1d4464 164 memcpy(buf, DEVKEY, 16);
dudmuck 0:a5e3347bad61 165 }
dudmuck 0:a5e3347bad61 166
dudmuck 0:a5e3347bad61 167 //////////////////////////////////////////////////
dudmuck 0:a5e3347bad61 168 // MAIN - INITIALIZATION AND STARTUP
dudmuck 0:a5e3347bad61 169 //////////////////////////////////////////////////
dudmuck 2:a8f00db60fdb 170 void get_mote_version()
dudmuck 2:a8f00db60fdb 171 {
tmulrooney 8:1069ed1d4464 172 // char first;
dudmuck 2:a8f00db60fdb 173
tmulrooney 8:1069ed1d4464 174 // pc_7 = 1;
tmulrooney 8:1069ed1d4464 175 // first = pc_1;
tmulrooney 8:1069ed1d4464 176 // pc_7 = 0;
tmulrooney 8:1069ed1d4464 177 // if (first && !pc_1) {
tmulrooney 8:1069ed1d4464 178 // mote_version = MOTE_V2;
tmulrooney 8:1069ed1d4464 179 // printf("v2\r\n");
tmulrooney 8:1069ed1d4464 180 // bat = new AnalogIn(PA_0);
tmulrooney 8:1069ed1d4464 181 // } else {
tmulrooney 8:1069ed1d4464 182 // mote_version = MOTE_V3;
tmulrooney 8:1069ed1d4464 183 // printf("v3\r\n");
tmulrooney 8:1069ed1d4464 184 // bat = new AnalogIn(PA_1);
tmulrooney 8:1069ed1d4464 185 // }
dudmuck 2:a8f00db60fdb 186 }
dudmuck 0:a5e3347bad61 187
jlcolemanmbed 9:f4d52e9625f7 188 #if 1
dudmuck 0:a5e3347bad61 189 // initial job
dudmuck 0:a5e3347bad61 190 static void initfunc (osjob_t* j) {
dudmuck 0:a5e3347bad61 191 debug_str("B: INITFUNC\n");
dudmuck 0:a5e3347bad61 192 // reset MAC state
dudmuck 0:a5e3347bad61 193 LMIC_reset();
dudmuck 0:a5e3347bad61 194 // start joining
dudmuck 0:a5e3347bad61 195 #if( OVER_THE_AIR_ACTIVATION != 0 )
dudmuck 0:a5e3347bad61 196 LMIC_startJoining();
dudmuck 0:a5e3347bad61 197 #else
dudmuck 0:a5e3347bad61 198 devaddr_t *serial_id = (devaddr_t *) 0x1FF800d0; // cat3 device stm32l152rc
dudmuck 0:a5e3347bad61 199 LMIC_setSession( 0, *serial_id, NwkSKey, ArtSKey );
dudmuck 0:a5e3347bad61 200 debug_val("SN = ", *serial_id);
dudmuck 0:a5e3347bad61 201 #endif
tmulrooney 8:1069ed1d4464 202 //init done - onEvent() callback will be invoked...
dudmuck 0:a5e3347bad61 203
dudmuck 0:a5e3347bad61 204 //DEBUG_STR("E: INITFUNC");
dudmuck 2:a8f00db60fdb 205 get_mote_version();
tmulrooney 8:1069ed1d4464 206 // if (mote_version == MOTE_V3)
tmulrooney 8:1069ed1d4464 207 // gps.en_invert = false;
tmulrooney 8:1069ed1d4464 208 // else
tmulrooney 8:1069ed1d4464 209 // gps.en_invert = true;
dudmuck 2:a8f00db60fdb 210
tmulrooney 8:1069ed1d4464 211 // gps.init();
tmulrooney 8:1069ed1d4464 212 // gps.enable(1);
dudmuck 0:a5e3347bad61 213 //gps.verbose = 1;
dudmuck 0:a5e3347bad61 214
tmulrooney 8:1069ed1d4464 215 // mpl3115a2.init();
dudmuck 0:a5e3347bad61 216 }
tmulrooney 8:1069ed1d4464 217 #endif
tmulrooney 8:1069ed1d4464 218
jlcolemanmbed 9:f4d52e9625f7 219 #if 0
tmulrooney 8:1069ed1d4464 220 // counter static
tmulrooney 8:1069ed1d4464 221 int cnt = 0;
tmulrooney 8:1069ed1d4464 222 // log text to USART and toggle LED
tmulrooney 8:1069ed1d4464 223 static void initfunc (osjob_t* job)
tmulrooney 8:1069ed1d4464 224 {
tmulrooney 8:1069ed1d4464 225 // say hello
tmulrooney 8:1069ed1d4464 226 debug_str("Hello World!\r\n");
tmulrooney 8:1069ed1d4464 227 // log counter
tmulrooney 8:1069ed1d4464 228 debug_val("cnt = ", cnt);
tmulrooney 8:1069ed1d4464 229 // toggle LED
tmulrooney 8:1069ed1d4464 230 debug_led(++cnt & 1);
tmulrooney 8:1069ed1d4464 231 // reschedule job every second
tmulrooney 8:1069ed1d4464 232 os_setTimedCallback(job, os_getTime()+sec2osticks(1), initfunc);
tmulrooney 8:1069ed1d4464 233 }
tmulrooney 8:1069ed1d4464 234 #endif
tmulrooney 8:1069ed1d4464 235
dudmuck 0:a5e3347bad61 236
dudmuck 0:a5e3347bad61 237 int main(void)
dudmuck 0:a5e3347bad61 238 {
dudmuck 0:a5e3347bad61 239 osjob_t initjob;
tmulrooney 8:1069ed1d4464 240
dudmuck 0:a5e3347bad61 241 // initialize runtime env
tmulrooney 8:1069ed1d4464 242 debug_init();
dudmuck 0:a5e3347bad61 243 os_init();
dudmuck 0:a5e3347bad61 244 // setup initial job
dudmuck 0:a5e3347bad61 245 os_setCallback(&initjob, initfunc);
dudmuck 0:a5e3347bad61 246 // execute scheduled jobs and events
dudmuck 0:a5e3347bad61 247 os_runloop();
dudmuck 0:a5e3347bad61 248 // (not reached)
dudmuck 0:a5e3347bad61 249 }
dudmuck 0:a5e3347bad61 250
dudmuck 0:a5e3347bad61 251 //////////////////////////////////////////////////
dudmuck 0:a5e3347bad61 252 // LMIC EVENT CALLBACK
dudmuck 0:a5e3347bad61 253 //////////////////////////////////////////////////
dudmuck 0:a5e3347bad61 254 osjob_t rxLedJob;
dudmuck 0:a5e3347bad61 255 osjob_t txLedJob;
dudmuck 0:a5e3347bad61 256 osjob_t sendFrameJob;
dudmuck 0:a5e3347bad61 257
dudmuck 0:a5e3347bad61 258 static void onRxLed (osjob_t* j) {
dudmuck 0:a5e3347bad61 259 debug_val("LED2 = ", 1 );
dudmuck 0:a5e3347bad61 260 led2 = 1;
dudmuck 0:a5e3347bad61 261 }
dudmuck 0:a5e3347bad61 262
dudmuck 0:a5e3347bad61 263 static void onTxLed (osjob_t* j) {
dudmuck 0:a5e3347bad61 264 debug_val("LED1 = ", 1 );
dudmuck 0:a5e3347bad61 265 led1 = 1;
dudmuck 0:a5e3347bad61 266 }
dudmuck 0:a5e3347bad61 267
dudmuck 3:d81e4a63fb2e 268 static void
dudmuck 3:d81e4a63fb2e 269 restore_hsi()
dudmuck 3:d81e4a63fb2e 270 {
tmulrooney 8:1069ed1d4464 271 // RCC_OscInitTypeDef osc_init;
dudmuck 3:d81e4a63fb2e 272 /* if HSI was shut off in deep sleep (needed for AnalogIn) */
tmulrooney 8:1069ed1d4464 273 // HAL_RCC_GetOscConfig(&osc_init);
tmulrooney 8:1069ed1d4464 274 // if (osc_init.HSIState != RCC_HSI_ON) {
tmulrooney 8:1069ed1d4464 275 // // Enable the HSI (to clock the ADC)
tmulrooney 8:1069ed1d4464 276 // osc_init.OscillatorType = RCC_OSCILLATORTYPE_HSI;
tmulrooney 8:1069ed1d4464 277 // osc_init.HSIState = RCC_HSI_ON;
tmulrooney 8:1069ed1d4464 278 // osc_init.PLL.PLLState = RCC_PLL_NONE;
tmulrooney 8:1069ed1d4464 279 // HAL_RCC_OscConfig(&osc_init);
tmulrooney 8:1069ed1d4464 280 // }
dudmuck 3:d81e4a63fb2e 281 }
dudmuck 3:d81e4a63fb2e 282
dudmuck 0:a5e3347bad61 283 static bool AppLedStateOn = false;
dudmuck 0:a5e3347bad61 284
dudmuck 3:d81e4a63fb2e 285 #define AIN_VREF 3.3 // stm32 internal refernce
dudmuck 3:d81e4a63fb2e 286 #define AIN_VBAT_DIV 2 // resistor divider
dudmuck 0:a5e3347bad61 287 static void PrepareDataFrame( void )
dudmuck 0:a5e3347bad61 288 {
dudmuck 0:a5e3347bad61 289 uint16_t altitudeGps;
dudmuck 3:d81e4a63fb2e 290
dudmuck 3:d81e4a63fb2e 291 restore_hsi();
dudmuck 0:a5e3347bad61 292
tmulrooney 8:1069ed1d4464 293 // gps.service();
dudmuck 6:0b2b2d196ddd 294 //printf("lat:%f long:%f\r\n", gps.Latitude, gps.Longitude);
tmulrooney 8:1069ed1d4464 295 // mpl3115a2.ReadTemperature();
dudmuck 0:a5e3347bad61 296
dudmuck 0:a5e3347bad61 297 // immediately prepare next transmission
dudmuck 0:a5e3347bad61 298 //LMIC.frame[0] = LMIC.rxq.snr;
dudmuck 0:a5e3347bad61 299 LMIC.frame[0] = AppLedStateOn; // (bit 0 == 1) => LED on
tmulrooney 8:1069ed1d4464 300 // LMIC.frame[1] = (int)mpl3115a2.Temperature; // Signed degrees Celcius in half degree units. So, +/-63 C
dudmuck 3:d81e4a63fb2e 301 LMIC.frame[2] = (bat->read_u16() >> 8) + (bat->read_u16() >> 9) ; // per LoRaMAC spec; 0=Charging; 1...254 = level, 255 = N/A
dudmuck 3:d81e4a63fb2e 302
tmulrooney 8:1069ed1d4464 303 // LMIC.frame[3] = ( gps.LatitudeBinary >> 16 ) & 0xFF;
tmulrooney 8:1069ed1d4464 304 // LMIC.frame[4] = ( gps.LatitudeBinary >> 8 ) & 0xFF;
tmulrooney 8:1069ed1d4464 305 // LMIC.frame[5] = gps.LatitudeBinary & 0xFF;
tmulrooney 8:1069ed1d4464 306 // LMIC.frame[6] = ( gps.LongitudeBinary >> 16 ) & 0xFF;
tmulrooney 8:1069ed1d4464 307 // LMIC.frame[7] = ( gps.LongitudeBinary >> 8 ) & 0xFF;
tmulrooney 8:1069ed1d4464 308 // LMIC.frame[8] = gps.LongitudeBinary & 0xFF;
dudmuck 0:a5e3347bad61 309
tmulrooney 8:1069ed1d4464 310 // altitudeGps = atoi(gps.NmeaGpsData.NmeaAltitude);
dudmuck 3:d81e4a63fb2e 311 //printf("alt:%d\r\n", altitudeGps);
dudmuck 0:a5e3347bad61 312 LMIC.frame[9] = ( altitudeGps >> 8 ) & 0xFF;
dudmuck 0:a5e3347bad61 313 LMIC.frame[10] = altitudeGps & 0xFF;
dudmuck 3:d81e4a63fb2e 314
dudmuck 6:0b2b2d196ddd 315
dudmuck 6:0b2b2d196ddd 316 //printf("bat:%.2f\r\n", volts);
dudmuck 6:0b2b2d196ddd 317 #ifndef CHNL_HYBRID
dudmuck 6:0b2b2d196ddd 318 float volts = bat->read()*AIN_VREF*AIN_VBAT_DIV;
dudmuck 3:d81e4a63fb2e 319 if (volts < LOW_BAT_THRESHOLD)
dudmuck 3:d81e4a63fb2e 320 LMIC.txpow_limit = 20;
dudmuck 3:d81e4a63fb2e 321 else
dudmuck 3:d81e4a63fb2e 322 LMIC.txpow_limit = 30;
dudmuck 6:0b2b2d196ddd 323 #endif /* !CHNL_HYBRID */
dudmuck 0:a5e3347bad61 324 }
dudmuck 0:a5e3347bad61 325
dudmuck 6:0b2b2d196ddd 326 static void onSendFrame (osjob_t* j)
dudmuck 6:0b2b2d196ddd 327 {
dudmuck 6:0b2b2d196ddd 328 if (LMIC.opmode & OP_TXRXPEND)
dudmuck 6:0b2b2d196ddd 329 return;
dudmuck 6:0b2b2d196ddd 330
dudmuck 0:a5e3347bad61 331 PrepareDataFrame( );
dudmuck 6:0b2b2d196ddd 332 // schedule transmission (port 1, data[], datalen 1, ACK requested)
dudmuck 6:0b2b2d196ddd 333 // (will be sent as soon as duty cycle permits)
dudmuck 6:0b2b2d196ddd 334
dudmuck 0:a5e3347bad61 335 LMIC_setTxData2(5, LMIC.frame, APP_DATA_SIZE, APP_ACK);
dudmuck 0:a5e3347bad61 336 }
dudmuck 0:a5e3347bad61 337
dudmuck 0:a5e3347bad61 338 void onEvent (ev_t ev) {
dudmuck 0:a5e3347bad61 339
dudmuck 0:a5e3347bad61 340 debug_event(ev);
dudmuck 0:a5e3347bad61 341
tmulrooney 8:1069ed1d4464 342 // gps.service();
dudmuck 0:a5e3347bad61 343
dudmuck 0:a5e3347bad61 344 switch(ev)
dudmuck 0:a5e3347bad61 345 {
dudmuck 0:a5e3347bad61 346 // network joined, session established
dudmuck 0:a5e3347bad61 347 case EV_JOINED:
dudmuck 0:a5e3347bad61 348 debug_val("Net ID = ", LMIC.netid);
dudmuck 0:a5e3347bad61 349 goto tx;
dudmuck 0:a5e3347bad61 350 // scheduled data sent (optionally data received)
dudmuck 0:a5e3347bad61 351 case EV_TXCOMPLETE:
dudmuck 0:a5e3347bad61 352 if(LMIC.dataLen)
dudmuck 0:a5e3347bad61 353 { // data received in rx slot after tx
dudmuck 0:a5e3347bad61 354 debug_buf(LMIC.frame+LMIC.dataBeg, LMIC.dataLen);
dudmuck 0:a5e3347bad61 355 if(LMIC.dataLen == 1) { // set LED state if exactly one byte is received
dudmuck 0:a5e3347bad61 356 AppLedStateOn = LMIC.frame[LMIC.dataBeg] & 0x01;
dudmuck 0:a5e3347bad61 357 debug_val("LED3 = ", AppLedStateOn ? 0 : 1 );
dudmuck 0:a5e3347bad61 358 }
dudmuck 0:a5e3347bad61 359 }
dudmuck 0:a5e3347bad61 360 if((LMIC.txrxFlags & (TXRX_DNW1|TXRX_DNW2) )!= 0 )
dudmuck 0:a5e3347bad61 361 {
dudmuck 0:a5e3347bad61 362 debug_val("LED2 = ", 0 );
dudmuck 0:a5e3347bad61 363 led2 = 0;
dudmuck 0:a5e3347bad61 364 os_setTimedCallback( &rxLedJob, os_getTime() + ms2osticks(15), onRxLed );
dudmuck 0:a5e3347bad61 365 }
dudmuck 0:a5e3347bad61 366 tx:
dudmuck 4:f9aaf0f82c00 367 os_setTimedCallback( &sendFrameJob, os_getTime() + ms2osticks(MS_DELAY_NEXT_TX), onSendFrame ); // Change the Tx periodicity
dudmuck 0:a5e3347bad61 368 //onSendFrame(NULL);
dudmuck 0:a5e3347bad61 369
dudmuck 0:a5e3347bad61 370 // Blink Tx LED
dudmuck 0:a5e3347bad61 371 debug_val("LED1 = ", 0 );
dudmuck 0:a5e3347bad61 372 led1 = 0;
dudmuck 0:a5e3347bad61 373
dudmuck 0:a5e3347bad61 374 os_setTimedCallback( &txLedJob, os_getTime() + ms2osticks(25), onTxLed );
dudmuck 0:a5e3347bad61 375 break;
dudmuck 0:a5e3347bad61 376 default:
jlcolemanmbed 9:f4d52e9625f7 377 debug("event: %d/n",ev);
dudmuck 0:a5e3347bad61 378 break;
dudmuck 0:a5e3347bad61 379 }
dudmuck 0:a5e3347bad61 380 }