arm studio build

Dependencies:   libxDot-mbed5

Committer:
alan1974
Date:
Sat Aug 04 19:56:33 2018 +0000
Revision:
2:0af50f386eb2
Parent:
1:0d25d9ddbe9f
Child:
5:abfe25f0de33
update

Who changed what in which revision?

UserRevisionLine numberNew contents of line
alan1974 0:a91cd1b08360 1 #include "mbed.h"
alan1974 1:0d25d9ddbe9f 2 #include "global.h"
alan1974 0:a91cd1b08360 3 #include "commI2C.h"
alan1974 0:a91cd1b08360 4 #include "dot_util.h"
alan1974 0:a91cd1b08360 5 #include "wbit_util.h"
alan1974 0:a91cd1b08360 6 #include "mDot.h"
alan1974 0:a91cd1b08360 7 #include "RadioEvent.h"
alan1974 0:a91cd1b08360 8 #include "Lora.h"
alan1974 0:a91cd1b08360 9
alan1974 1:0d25d9ddbe9f 10 //nvm storage params
alan1974 1:0d25d9ddbe9f 11 nvm nvmData;
alan1974 1:0d25d9ddbe9f 12 nvm *pNvmData = &nvmData;
alan1974 1:0d25d9ddbe9f 13
alan1974 0:a91cd1b08360 14 //=======================================================================================================
alan1974 0:a91cd1b08360 15 // enable some GPIO for scope trigger or led
alan1974 0:a91cd1b08360 16 //=======================================================================================================
alan1974 0:a91cd1b08360 17 //#define GPIO_ENABLE
alan1974 0:a91cd1b08360 18 //=======================================================================================================
alan1974 0:a91cd1b08360 19 // configure for either private (test only) or public network (standard)
alan1974 0:a91cd1b08360 20 //=======================================================================================================
alan1974 0:a91cd1b08360 21 //#define MT_PRIVATE_NETWORK //ENABLE THIS FOR PRIVATE NETWORK
alan1974 0:a91cd1b08360 22
alan1974 0:a91cd1b08360 23 //=======================================================================================================
alan1974 0:a91cd1b08360 24 //code api level and version
alan1974 0:a91cd1b08360 25 //api_level: proc code will not run if api level (last two bytess) greater than what it expects
alan1974 0:a91cd1b08360 26 //=======================================================================================================
alan1974 0:a91cd1b08360 27 uint8_t api_level[4] = { 0x00, 0x00, 0x00, 0x03 }; //api-level, determines if xdot code works with proc code
alan1974 0:a91cd1b08360 28 uint8_t ver_level[4] = { 0x00, 0x00, 0x00, 0x07 }; //updated for every code check-in
alan1974 0:a91cd1b08360 29
alan1974 0:a91cd1b08360 30 //=======================================================================================================
alan1974 0:a91cd1b08360 31 //configuring mbed pinsa; https://docs.mbed.com/docs/mbed-os-api-reference/en/latest/APIs/io/DigitalInOut/
alan1974 0:a91cd1b08360 32 // * these options must match the settings on your gateway //
alan1974 0:a91cd1b08360 33 // * edit their values to match your configuration //
alan1974 0:a91cd1b08360 34 // * frequency sub band is only relevant for the 915 bands //
alan1974 0:a91cd1b08360 35 // * either the network name and passphrase can be used or //
alan1974 0:a91cd1b08360 36 // the network ID (8 bytes) and KEY (16 bytes) //
alan1974 0:a91cd1b08360 37 //=======================================================================================================
alan1974 1:0d25d9ddbe9f 38 static std::string network_name = "asdfqwer"; //not used
alan1974 1:0d25d9ddbe9f 39 static std::string network_passphrase = "zxcvasdf"; //not used
alan1974 1:0d25d9ddbe9f 40 /*
alan1974 1:0d25d9ddbe9f 41 uint8_t network_id[] = { 0x90, 0xF1, 0x47, 0x90, 0x6C, 0x48, 0x1D, 0x29 }; //static id not used anymore but don't comment out
alan1974 1:0d25d9ddbe9f 42 //OTAA keys
alan1974 1:0d25d9ddbe9f 43 uint8_t network_key[] = { 0x0F, 0xF9, 0xA2, 0x90, 0x2E, 0xAA, 0x6B, 0x8C, 0x6A, 0x4E, 0xFD, 0x67, 0xF9, 0xA6, 0xF3, 0xD3 }; //appkey
alan1974 1:0d25d9ddbe9f 44 */
alan1974 0:a91cd1b08360 45
alan1974 0:a91cd1b08360 46 #ifdef MT_PRIVATE_NETWORK
alan1974 0:a91cd1b08360 47 static uint8_t frequency_sub_band = 4;
alan1974 0:a91cd1b08360 48 static bool public_network = false;
alan1974 0:a91cd1b08360 49 static uint8_t ack = 1; //0;
alan1974 0:a91cd1b08360 50 #else
alan1974 1:0d25d9ddbe9f 51 uint8_t frequency_sub_band = 1;
alan1974 1:0d25d9ddbe9f 52 bool public_network = true; //false
alan1974 1:0d25d9ddbe9f 53 uint8_t ack = 1; //0;
alan1974 1:0d25d9ddbe9f 54 uint8_t link_check_treshold = 100; //5;
alan1974 0:a91cd1b08360 55 #endif
alan1974 0:a91cd1b08360 56
alan1974 1:0d25d9ddbe9f 57 bool adr = false; //set adaptive data rate
alan1974 1:0d25d9ddbe9f 58
alan1974 1:0d25d9ddbe9f 59
alan1974 1:0d25d9ddbe9f 60
alan1974 1:0d25d9ddbe9f 61
alan1974 0:a91cd1b08360 62
alan1974 0:a91cd1b08360 63 //=======================================================================================================
alan1974 0:a91cd1b08360 64 // deepsleep consumes slightly less current than sleep
alan1974 0:a91cd1b08360 65 // in sleep mode, IO state is maintained, RAM is retained, and application will resume after waking up
alan1974 0:a91cd1b08360 66 // in deepsleep mode, IOs float, RAM is lost, and application will start from beginning after waking up
alan1974 0:a91cd1b08360 67 // if deep_sleep == true, device will enter deepsleep mode
alan1974 0:a91cd1b08360 68 //=======================================================================================================
alan1974 0:a91cd1b08360 69 static bool deep_sleep = false; //false;
alan1974 0:a91cd1b08360 70
alan1974 0:a91cd1b08360 71 uint32_t packets_sent = 0;
alan1974 0:a91cd1b08360 72 uint32_t acks_rcvd = 0;
alan1974 0:a91cd1b08360 73 int8_t rssi =0; //rssi of last rcvd rx1/rx2
alan1974 0:a91cd1b08360 74 std::string eui = " ";
alan1974 0:a91cd1b08360 75 static bool led_enabled = false; //true; //false;
alan1974 0:a91cd1b08360 76 mDot* dot = NULL;
alan1974 0:a91cd1b08360 77 lora::ChannelPlan* plan = NULL;
alan1974 0:a91cd1b08360 78 uint8_t buf_xmt[BUFFER_SIZE_I2C]; //outgoing data
alan1974 0:a91cd1b08360 79 uint8_t buf_rcv[BUFFER_SIZE_I2C]; //incoming data
alan1974 0:a91cd1b08360 80 std::vector<uint8_t> upstream_packet;
alan1974 0:a91cd1b08360 81
alan1974 0:a91cd1b08360 82 #ifdef GPIO_ENABLE
alan1974 0:a91cd1b08360 83 DigitalOut led1(PB_0); //LED1);
alan1974 0:a91cd1b08360 84 DigitalInOut gpio1(PA_5); //scope debug PA-5 is connected to SW1 pads on Loren v04 and can be used for scope debug
alan1974 0:a91cd1b08360 85 #endif
alan1974 0:a91cd1b08360 86
alan1974 0:a91cd1b08360 87 Serial pc(USBTX, USBRX); //serial port output
alan1974 0:a91cd1b08360 88
alan1974 0:a91cd1b08360 89 //=================================================================================================
alan1974 0:a91cd1b08360 90 //LED_test
alan1974 0:a91cd1b08360 91 //=================================================================================================
alan1974 0:a91cd1b08360 92 void LED_test(int num)
alan1974 0:a91cd1b08360 93 {
alan1974 0:a91cd1b08360 94 #ifdef GPIO_ENABLE
alan1974 0:a91cd1b08360 95 if (led_enabled)
alan1974 0:a91cd1b08360 96 {
alan1974 0:a91cd1b08360 97 pc.printf("LED_test()\r\n");
alan1974 0:a91cd1b08360 98 int test;
alan1974 0:a91cd1b08360 99 for (test=0; test<num; test++)
alan1974 0:a91cd1b08360 100 {
alan1974 0:a91cd1b08360 101 led1 = 0;
alan1974 0:a91cd1b08360 102 wait_ms(500);
alan1974 0:a91cd1b08360 103 led1 = 1;
alan1974 0:a91cd1b08360 104 wait_ms(500);
alan1974 0:a91cd1b08360 105 }
alan1974 0:a91cd1b08360 106 }
alan1974 0:a91cd1b08360 107 #endif
alan1974 0:a91cd1b08360 108 }
alan1974 0:a91cd1b08360 109 //==================================================================================
alan1974 0:a91cd1b08360 110 //chksum
alan1974 0:a91cd1b08360 111 //compute checksum over i2c buffer except for last byte (chksum byte)
alan1974 0:a91cd1b08360 112 //==================================================================================
alan1974 0:a91cd1b08360 113 uint8_t chksum_proc(uint8_t *bfr_xdot){
alan1974 0:a91cd1b08360 114 uint8_t i;
alan1974 0:a91cd1b08360 115 uint8_t chksum = 0;
alan1974 0:a91cd1b08360 116 for (i=0; i < BUFFER_SIZE_I2C-1;i++)chksum += bfr_xdot[i];//good code
alan1974 0:a91cd1b08360 117 return chksum;
alan1974 0:a91cd1b08360 118 }
alan1974 0:a91cd1b08360 119 //=================================================================================================
alan1974 0:a91cd1b08360 120 //cfg_network:
alan1974 0:a91cd1b08360 121 // configure the network public/private and sub-band
alan1974 0:a91cd1b08360 122 // bForceCfg : true => force a network cfg
alan1974 0:a91cd1b08360 123 // false => only change if bPublic or sub_band have changed
alan1974 0:a91cd1b08360 124 // bPublic : true if network is public, else private
alan1974 0:a91cd1b08360 125 // sub_band: sub band number (1..8)
alan1974 0:a91cd1b08360 126
alan1974 0:a91cd1b08360 127 // NOTE: THIS ONLY WORKS WHEN THE XDOT BOOTS UP. IT DOES NOT WORK WHEN TRYING TO CHANGE AFTER.
alan1974 0:a91cd1b08360 128 // NOT SURE WHY...WHEN THE XDOT REJOINS IT SHOULD USE NEW SESSION SETTINGS... asb
alan1974 0:a91cd1b08360 129 //
alan1974 0:a91cd1b08360 130 //asb:dec 2017: try this later and see if it works:mdot.h:
alan1974 0:a91cd1b08360 131 // int32_t setPublicNetwork(const bool& on);
alan1974 0:a91cd1b08360 132 // bool getPublicNetwork();
alan1974 0:a91cd1b08360 133 // int32_t setFrequencySubBand(const uint8_t& band);
alan1974 0:a91cd1b08360 134 //=================================================================================================
alan1974 0:a91cd1b08360 135 bool cfg_network(bool bForceCfg,bool bPublic,uint8_t sub_band){
alan1974 0:a91cd1b08360 136 // if bForceCfg false and network parameters haven't changed then just exit
alan1974 0:a91cd1b08360 137 if (!bForceCfg){
alan1974 0:a91cd1b08360 138 pc.printf("no configuration change needed subband the same");
alan1974 0:a91cd1b08360 139 if ((public_network ==bPublic) && (frequency_sub_band ==sub_band))return true;
alan1974 0:a91cd1b08360 140 }
alan1974 0:a91cd1b08360 141 pc.printf("changing to subband: %d\r\n",sub_band);
alan1974 0:a91cd1b08360 142 // update network settings
alan1974 0:a91cd1b08360 143 public_network = bPublic;
alan1974 0:a91cd1b08360 144 frequency_sub_band =sub_band;
alan1974 0:a91cd1b08360 145 // start from a well-known state
alan1974 0:a91cd1b08360 146 logInfo("defaulting Dot configuration");
alan1974 0:a91cd1b08360 147 //dot->resetConfig(); //reset config to factory default not used for v0307
alan1974 0:a91cd1b08360 148 dot->resetNetworkSession(); //Reset current network session, essentially disconnecting from the network
alan1974 0:a91cd1b08360 149
alan1974 0:a91cd1b08360 150 if (dot->getJoinMode() != mDot::OTA) { // update configuration if necessary
alan1974 0:a91cd1b08360 151 logInfo("changing network join mode to OTA");
alan1974 0:a91cd1b08360 152 if (dot->setJoinMode(mDot::OTA) != mDot::MDOT_OK) {
alan1974 0:a91cd1b08360 153 logError("failed to set network join mode to OTA");
alan1974 0:a91cd1b08360 154 return false;
alan1974 0:a91cd1b08360 155 }
alan1974 0:a91cd1b08360 156 }
alan1974 0:a91cd1b08360 157 // in OTA and AUTO_OTA join modes, the credentials can be passed to the library as a name and passphrase or an ID and KEY
alan1974 0:a91cd1b08360 158 // only one method or the other should be used!
alan1974 0:a91cd1b08360 159 if (public_network){
alan1974 1:0d25d9ddbe9f 160 // update_ota_config_id_key(network_id, network_key, frequency_sub_band, public_network, ack);
alan1974 1:0d25d9ddbe9f 161 update_ota_config_id_key(nvmData.network_id,nvmData.network_key, frequency_sub_band, public_network, ack);
alan1974 1:0d25d9ddbe9f 162
alan1974 1:0d25d9ddbe9f 163
alan1974 1:0d25d9ddbe9f 164
alan1974 0:a91cd1b08360 165 logInfo("-------------- network configured for public access -----------------------------");
alan1974 0:a91cd1b08360 166 }
alan1974 0:a91cd1b08360 167 else{
alan1974 0:a91cd1b08360 168 update_ota_config_name_phrase(network_name, network_passphrase, frequency_sub_band, public_network, ack);
alan1974 0:a91cd1b08360 169 logInfo("-------------- network configured for private access -------------------------------");
alan1974 1:0d25d9ddbe9f 170 }
alan1974 1:0d25d9ddbe9f 171 // configure network link check count
alan1974 1:0d25d9ddbe9f 172 // declares the Dot disconnected if no acks received within link_check_treshold transmits
alan1974 1:0d25d9ddbe9f 173 //update_network_link_check_config(3, 5);
alan1974 1:0d25d9ddbe9f 174 dot->setLinkCheckThreshold(link_check_treshold);
alan1974 1:0d25d9ddbe9f 175
alan1974 1:0d25d9ddbe9f 176
alan1974 0:a91cd1b08360 177
alan1974 0:a91cd1b08360 178 /* done thru radio cmds
alan1974 0:a91cd1b08360 179 // save changes to configuration
alan1974 0:a91cd1b08360 180 logInfo("saving configuration");
alan1974 0:a91cd1b08360 181 if (!dot->saveConfig()) {
alan1974 0:a91cd1b08360 182 logError("failed to save configuration");
alan1974 0:a91cd1b08360 183 return false;
alan1974 0:a91cd1b08360 184 }
alan1974 0:a91cd1b08360 185 display_config();
alan1974 0:a91cd1b08360 186 */
alan1974 0:a91cd1b08360 187 return true;
alan1974 0:a91cd1b08360 188 }
alan1974 0:a91cd1b08360 189
alan1974 0:a91cd1b08360 190 //=================================================================================================
alan1974 0:a91cd1b08360 191 //main()
alan1974 0:a91cd1b08360 192 // main() runs in its own thread in the OS
alan1974 0:a91cd1b08360 193 // (note the calls to wait below for delays)
alan1974 0:a91cd1b08360 194 //=================================================================================================
alan1974 0:a91cd1b08360 195 int main() {
alan1974 1:0d25d9ddbe9f 196 uint8_t i;
alan1974 0:a91cd1b08360 197 RadioEvent events;
alan1974 0:a91cd1b08360 198
alan1974 0:a91cd1b08360 199 mDotEvent mdotevent; //used to get ping info????
alan1974 0:a91cd1b08360 200 plan = new lora::ChannelPlan_US915();
alan1974 0:a91cd1b08360 201 #ifdef GPIO_ENABLE
alan1974 0:a91cd1b08360 202 gpio1.output();
alan1974 0:a91cd1b08360 203 gpio1 =0;
alan1974 0:a91cd1b08360 204 led1 = 1;
alan1974 0:a91cd1b08360 205 #endif
alan1974 0:a91cd1b08360 206 pc.baud(115200);
alan1974 0:a91cd1b08360 207 pc.printf("\r\n**********************************************************\r\n");
alan1974 0:a91cd1b08360 208 pc.printf("\r\n XDOT BOOT\r\n");
alan1974 0:a91cd1b08360 209 pc.printf("COMM api_level = <HEX> %x.%x.%x.%x\r\n",api_level[0],api_level[1],api_level[2],api_level[3]);
alan1974 0:a91cd1b08360 210 pc.printf("COMM version = %x.%x.%x.%x\r\n",ver_level[0],ver_level[1],ver_level[2],ver_level[3]);
alan1974 1:0d25d9ddbe9f 211 pc.printf("**********************************************************\r\n");
alan1974 0:a91cd1b08360 212 assert(plan);
alan1974 0:a91cd1b08360 213 dot = mDot::getInstance(plan);
alan1974 1:0d25d9ddbe9f 214 assert(dot);
alan1974 1:0d25d9ddbe9f 215
alan1974 1:0d25d9ddbe9f 216 //dot->setLogLevel((true) ? mts::MTSLog::TRACE_LEVEL : mts::MTSLog::TRACE_LEVEL); // TRACE_LEVEL , INFO_LEVEL
alan1974 1:0d25d9ddbe9f 217 bool bLogOutput = true;
alan1974 1:0d25d9ddbe9f 218 dot->setLogLevel((bLogOutput) ? mts::MTSLog::TRACE_LEVEL : mts::MTSLog::TRACE_LEVEL); // TRACE_LEVEL , INFO_LEVEL
alan1974 0:a91cd1b08360 219 dot->setEvents(&events);
alan1974 1:0d25d9ddbe9f 220
alan1974 1:0d25d9ddbe9f 221 //nvm nvmData;
alan1974 1:0d25d9ddbe9f 222 //nvm *pNvmData = &nvmData;
alan1974 1:0d25d9ddbe9f 223
alan1974 1:0d25d9ddbe9f 224
alan1974 1:0d25d9ddbe9f 225 nvmRead(pNvmData);
alan1974 1:0d25d9ddbe9f 226 /*
alan1974 1:0d25d9ddbe9f 227 pc.printf("****NON-VOLATILE MEMORY TEST*********\r\n");
alan1974 1:0d25d9ddbe9f 228
alan1974 1:0d25d9ddbe9f 229
alan1974 1:0d25d9ddbe9f 230 for (i = 0; i < sizeof(network_id);i++){
alan1974 1:0d25d9ddbe9f 231 nvmData.network_id[i] = network_id[i];
alan1974 1:0d25d9ddbe9f 232 }
alan1974 1:0d25d9ddbe9f 233 for (i = 0; i < sizeof(network_key);i++){
alan1974 1:0d25d9ddbe9f 234 nvmData.network_key[i] = network_key[i];
alan1974 1:0d25d9ddbe9f 235 }
alan1974 1:0d25d9ddbe9f 236
alan1974 1:0d25d9ddbe9f 237 uint8_t databytesread[128];
alan1974 1:0d25d9ddbe9f 238 dot->nvmWrite(0,pData,128);
alan1974 1:0d25d9ddbe9f 239 dot->nvmRead(0, databytesread,128);
alan1974 1:0d25d9ddbe9f 240 for (i = 0; i < 64; i++)
alan1974 1:0d25d9ddbe9f 241 pc.printf("databyte %d: %x\r\n",i,databytesread[i]);
alan1974 1:0d25d9ddbe9f 242
alan1974 1:0d25d9ddbe9f 243 pc.printf("****NON-VOLATILE MEMORY TEST*********\r\n");
alan1974 1:0d25d9ddbe9f 244 */
alan1974 0:a91cd1b08360 245
alan1974 0:a91cd1b08360 246
alan1974 0:a91cd1b08360 247 // getStandbyFlag() should return the state of the standby flag directly from the processor
alan1974 0:a91cd1b08360 248 // Standby flag: This bit is set by hardware and cleared only by a POR/PDR (power on reset/power down reset) or by setting the CSBF bit in the PWR power control register (PWR_CR)
alan1974 0:a91cd1b08360 249 // 0: Device has not been in Standby mode
alan1974 0:a91cd1b08360 250 // 1: Device has been in Standby mode
alan1974 0:a91cd1b08360 251 // The xDot should enter standby mode when deep sleep in invoked. So you should see the standby flag set if it came out of deep sleep.
alan1974 1:0d25d9ddbe9f 252 if (!dot->getStandbyFlag()) { //if 0 => power-up/reset which should always be the case at this point
alan1974 0:a91cd1b08360 253 logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
alan1974 0:a91cd1b08360 254
alan1974 1:0d25d9ddbe9f 255 pc.printf("\r\n saved configuration: ");
alan1974 0:a91cd1b08360 256 frequency_sub_band = dot->getFrequencySubBand();
alan1974 0:a91cd1b08360 257 pc.printf("\r\n subband: %d ",frequency_sub_band);
alan1974 0:a91cd1b08360 258 pc.printf("\r\n ADR: %d ",dot->getAdr());
alan1974 0:a91cd1b08360 259 pc.printf("\r\n antenna gain: %d ",dot->getAntennaGain());
alan1974 0:a91cd1b08360 260 pc.printf("\r\n transmit max pwr: %d ",dot->getMaxTxPower());
alan1974 0:a91cd1b08360 261 pc.printf("\r\n transmit min pwr: %d ",dot->getMinTxPower());
alan1974 0:a91cd1b08360 262 pc.printf("\r\n transmit pwr: %d ",dot->getTxPower());
alan1974 0:a91cd1b08360 263 pc.printf("\r\n TxDataRate: %d ",dot->getTxDataRate());
alan1974 0:a91cd1b08360 264 pc.printf("\r\n maxPktLen: %d ",dot->getMaxPacketLength());
alan1974 1:0d25d9ddbe9f 265 // pc.printf("\r\n port nmb: %d ",dot->getAppPort());
alan1974 1:0d25d9ddbe9f 266 pc.printf("\r\n link check count: %d ",dot->getLinkCheckThreshold());
alan1974 1:0d25d9ddbe9f 267 pc.printf("\r\n");
alan1974 0:a91cd1b08360 268 cfg_network(true,public_network,frequency_sub_band); //force network cfg,
alan1974 0:a91cd1b08360 269
alan1974 0:a91cd1b08360 270 } else {
alan1974 0:a91cd1b08360 271 // restore the saved session (join OTAA info) if the dot woke from deepsleep mode
alan1974 0:a91cd1b08360 272 // useful to use with deepsleep because session info is otherwise lost when the dot enters deepsleep
alan1974 0:a91cd1b08360 273 logInfo("restoring network session from NVM");
alan1974 0:a91cd1b08360 274 dot->restoreNetworkSession();
alan1974 0:a91cd1b08360 275 }
alan1974 0:a91cd1b08360 276 //--------------------------------------------------------------------------------------------------------------------------------------------
alan1974 1:0d25d9ddbe9f 277 // configure network link check count
alan1974 1:0d25d9ddbe9f 278 // declares the Dot disconnected if no acks received within link_check_treshold transmits
alan1974 1:0d25d9ddbe9f 279 //update_network_link_check_config(3, 5);
alan1974 1:0d25d9ddbe9f 280 dot->setLinkCheckThreshold(link_check_treshold);
alan1974 0:a91cd1b08360 281 //----------------------------------------------------------------------------------------------------------------------------------------------
alan1974 0:a91cd1b08360 282
alan1974 0:a91cd1b08360 283 // save changes to configuration
alan1974 0:a91cd1b08360 284 // logInfo("saving configuration");
alan1974 0:a91cd1b08360 285
alan1974 1:0d25d9ddbe9f 286 eui = mts::Text::bin2hexString(dot->getDeviceId()).c_str();
alan1974 1:0d25d9ddbe9f 287 pc.printf("\r\nEUI: %s\r\n",eui);
alan1974 1:0d25d9ddbe9f 288 bool joined = false;
alan1974 1:0d25d9ddbe9f 289
alan1974 0:a91cd1b08360 290
alan1974 1:0d25d9ddbe9f 291 i2c_proc_init(); //init i2c comm
alan1974 1:0d25d9ddbe9f 292 sleep_wake_interrupt_only(deep_sleep);
alan1974 0:a91cd1b08360 293
alan1974 0:a91cd1b08360 294 //scope test
alan1974 0:a91cd1b08360 295 // gpio1 =1;
alan1974 0:a91cd1b08360 296 //==============================================================================
alan1974 0:a91cd1b08360 297 // -loop here forever
alan1974 0:a91cd1b08360 298 // -sleep until LORA_WAKE goes hi => proc ready to send i2c cmd
alan1974 0:a91cd1b08360 299 // -start polling incoming i2c bus for proc cmd
alan1974 0:a91cd1b08360 300 // -execute cmd
alan1974 0:a91cd1b08360 301 // -take control of LORA_WAKE and toggle it hi to signal proc that xdot
alan1974 0:a91cd1b08360 302 // ready to send i2c ack message
alan1974 0:a91cd1b08360 303 // -go back to sleep
alan1974 0:a91cd1b08360 304 //==============================================================================
alan1974 0:a91cd1b08360 305 bool bPulseLoraWake = false;
alan1974 0:a91cd1b08360 306 while(1)
alan1974 0:a91cd1b08360 307 {
alan1974 0:a91cd1b08360 308 pc.printf("\n\r ***************************** ");
alan1974 0:a91cd1b08360 309
alan1974 0:a91cd1b08360 310 switch (i2c_proc_comm()){
alan1974 0:a91cd1b08360 311 case I2C_WRITE: //xdot ack ->proc
alan1974 0:a91cd1b08360 312 pc.printf("\n\r xdot ack -> proc done,going to sleep\n\r ");
alan1974 0:a91cd1b08360 313 bPulseLoraWake = false;
alan1974 0:a91cd1b08360 314 // gpio1 =0;
alan1974 0:a91cd1b08360 315 sleep_wake_interrupt_only(deep_sleep); //wait for wake
alan1974 0:a91cd1b08360 316 // gpio1 =1;
alan1974 0:a91cd1b08360 317 pc.printf("\n\r lora wake detected -> monitoring i2c bus\n\r ");
alan1974 0:a91cd1b08360 318 break;
alan1974 0:a91cd1b08360 319 case I2C_READ: //xdot <- proc
alan1974 0:a91cd1b08360 320 bPulseLoraWake = true;
alan1974 0:a91cd1b08360 321 switch (buf_rcv[0])
alan1974 0:a91cd1b08360 322 {
alan1974 1:0d25d9ddbe9f 323 case XDOT_CMD_XMIT_PKT:
alan1974 0:a91cd1b08360 324 pkt_upstrm *pUp= (pkt_upstrm*)&buf_rcv[0];
alan1974 0:a91cd1b08360 325 pkt_ack *pAck = (pkt_ack*)&buf_xmt[0];
alan1974 0:a91cd1b08360 326 pAck->ack = I2C_ACK_PROC;
alan1974 0:a91cd1b08360 327 pAck->cmd = XDOT_CMD_XMIT_PKT;
alan1974 0:a91cd1b08360 328 //pAck->dataLen = buf_rcv[1];
alan1974 0:a91cd1b08360 329 pAck->dataLen = pUp->dataLen; //data len of xmitted pkt
alan1974 0:a91cd1b08360 330 pc.printf("\r\npkt to xmit data len: %d\r\n",pAck->dataLen);
alan1974 0:a91cd1b08360 331 uint8_t chksum = chksum_proc(buf_rcv);
alan1974 0:a91cd1b08360 332 //pc.printf("\r\chksum rcvd: %d, chksum computed: %d\r\n",chksum,pkt_upstrm->chksum);
alan1974 0:a91cd1b08360 333 pc.printf("\r\nI2C chksum rcvd: %d",chksum);
alan1974 0:a91cd1b08360 334 pc.printf(" chksum computed: %d",pUp->chksum);
alan1974 0:a91cd1b08360 335 pAck->bXmitAttempted = 1;
alan1974 0:a91cd1b08360 336 pAck->chksum_err = 0;
alan1974 0:a91cd1b08360 337 if(pUp->chksum != chksum){
alan1974 0:a91cd1b08360 338 pc.printf(" chksum err, aborting xmit");
alan1974 0:a91cd1b08360 339 pAck->bXmitAttempted = 0;
alan1974 0:a91cd1b08360 340 pAck->mdot_ret = -2048; //wbit rtn code?
alan1974 0:a91cd1b08360 341 pAck->chksum_err = 1;
alan1974 0:a91cd1b08360 342 break;
alan1974 0:a91cd1b08360 343 }
alan1974 1:0d25d9ddbe9f 344 //rev 0307 parameters
alan1974 1:0d25d9ddbe9f 345 pc.printf("\n\r setting application port %d ",pUp->appPort); //appPort not used in rev < 0307
alan1974 1:0d25d9ddbe9f 346 dot->setAppPort(pUp->appPort);
alan1974 1:0d25d9ddbe9f 347 uint8_t linkFailcnt = dot->getLinkFailCount();
alan1974 1:0d25d9ddbe9f 348 pc.printf("\r\n lINK fail count %d\r\n",linkFailcnt);
alan1974 1:0d25d9ddbe9f 349 if (pUp->bResetLinkCCounter){
alan1974 1:0d25d9ddbe9f 350 dot->setLinkFailCount(0);
alan1974 1:0d25d9ddbe9f 351 pc.printf("\r\n reset link fail threshold to zero");
alan1974 1:0d25d9ddbe9f 352 }
alan1974 0:a91cd1b08360 353 if (pUp->dataLen == 0){ //datalen non zero?
alan1974 0:a91cd1b08360 354 pAck->bXmitAttempted = 0;
alan1974 0:a91cd1b08360 355 break;
alan1974 0:a91cd1b08360 356 }
alan1974 0:a91cd1b08360 357 upstream_packet.clear(); //xfr data from incoming bfr to xmit bfr
alan1974 0:a91cd1b08360 358 for (i=0; i< pUp->dataLen;i++) upstream_packet.push_back(pUp->txData[i]);
alan1974 0:a91cd1b08360 359 pc.printf("\r\n[TEST],Upstream Packet Received"); // no \r\n because it comes below
alan1974 0:a91cd1b08360 360 for(std::vector<uint8_t>::iterator it = upstream_packet.begin(); it != upstream_packet.end(); ++it) pc.printf(",0x%x", *it);
alan1974 0:a91cd1b08360 361 pc.printf("\r\n"); // see i told you.
alan1974 0:a91cd1b08360 362
alan1974 0:a91cd1b08360 363 joined = dot->getNetworkJoinStatus(); //are we joined to Lorawan?
alan1974 0:a91cd1b08360 364 pAck->joinAttempts = 0; //no attempts made yet to join
alan1974 1:0d25d9ddbe9f 365 pAck->bAck = 0; //won't know if we receive a lorawan ack until after xmit
alan1974 1:0d25d9ddbe9f 366 pAck->bAckdata = 0; //won't know if we receive a lorawan ack downstream data until after xmit
alan1974 1:0d25d9ddbe9f 367 pAck->rssi = 0; //if not rx1/rx2 then no RSSI value
alan1974 1:0d25d9ddbe9f 368 if(!joined) { //if not previously joined, then need to join now
alan1974 0:a91cd1b08360 369 pAck->bJoined = 0;
alan1974 0:a91cd1b08360 370 pc.printf("\r\n----------- NETWORK NOT JOINED YET, WILL TRY TO JOIN %d TIMES\r\n",pUp->joinAttemps);
alan1974 0:a91cd1b08360 371 joined = join_network_wbit(pUp->joinAttemps);
alan1974 0:a91cd1b08360 372 pAck->joinAttempts = join_network_attempts_wbit();
alan1974 1:0d25d9ddbe9f 373 pAck->mdot_ret = dot->send(upstream_packet);
alan1974 1:0d25d9ddbe9f 374 if (!joined)pc.printf("\r\n----------- FAILED TO JOIN...GIVING UP\r\n"); // join network if not joined
alan1974 0:a91cd1b08360 375 }
alan1974 0:a91cd1b08360 376 if (joined){
alan1974 1:0d25d9ddbe9f 377 pAck->bJoined = 1; //we are joined to the network
alan1974 0:a91cd1b08360 378 packets_sent++;
alan1974 0:a91cd1b08360 379 //send packet
alan1974 0:a91cd1b08360 380 //return code indicates results, send return code back to proc Dec14,2017
alan1974 0:a91cd1b08360 381 pAck->mdot_ret = dot->send(upstream_packet);
alan1974 0:a91cd1b08360 382 printf("\n\rdata->send() return code: %d\r\n",pAck->mdot_ret);
alan1974 0:a91cd1b08360 383 if (pAck->mdot_ret == mDot::MDOT_OK){ //xmit the pkt in blocking mode, return false if no ack
alan1974 0:a91cd1b08360 384 //if (dot->send(upstream_packet) == mDot::MDOT_OK){ //xmit the pkt in blocking mode, return false if no ack
alan1974 0:a91cd1b08360 385 acks_rcvd++;
alan1974 0:a91cd1b08360 386 pAck->bAck = 1; //we got a Rx1 or Rx2 ack
alan1974 0:a91cd1b08360 387 mDot::rssi_stats rssiStats = dot->getRssiStats(); //rssi stat
alan1974 0:a91cd1b08360 388 pAck->rssi = (int8_t)rssiStats.last;
alan1974 1:0d25d9ddbe9f 389 printf("\n\rdata->send()= true => ack rcvd :ack=: %d, rssi=: %d\r\n",pAck->bAck,pAck->rssi);
alan1974 0:a91cd1b08360 390 if (events.is_packet_received()){ //any downstream data from the Rx1/Rx2 pkt?
alan1974 0:a91cd1b08360 391 printf("\n\revents.is_packet_received = true\r\n");
alan1974 0:a91cd1b08360 392 pAck->bAckdata = 1;
alan1974 0:a91cd1b08360 393 upstream_packet.clear();
alan1974 0:a91cd1b08360 394 upstream_packet = events.get_downstream_packet();
alan1974 0:a91cd1b08360 395 pAck->rxLen = upstream_packet.size();
alan1974 0:a91cd1b08360 396 if (pAck->rxLen > I2C_MAX_ACK_DATA){
alan1974 0:a91cd1b08360 397 pc.printf("\r\n got ack with pkt data too large.. rejected\r\n");
alan1974 0:a91cd1b08360 398 break;
alan1974 0:a91cd1b08360 399 }
alan1974 0:a91cd1b08360 400 pc.printf("\r\n pkt data: ");
alan1974 0:a91cd1b08360 401 for (i=0; i< pAck->rxLen;i++) {
alan1974 0:a91cd1b08360 402 pAck->rxData[i]= upstream_packet[i];
alan1974 0:a91cd1b08360 403 pc.printf(" %x",pAck->rxData[i]);
alan1974 0:a91cd1b08360 404 }
alan1974 0:a91cd1b08360 405 } //if downstream data rcvd
alan1974 0:a91cd1b08360 406 else{
alan1974 0:a91cd1b08360 407 printf("\n\revents.is_packet_received ()= false => ack rcvd but no data\r\n");
alan1974 0:a91cd1b08360 408 }
alan1974 0:a91cd1b08360 409 } //send() returns K
alan1974 0:a91cd1b08360 410 else{
alan1974 0:a91cd1b08360 411 printf("\n\rdata->send()= false => no ack\r\n"); //could be some other error
alan1974 0:a91cd1b08360 412 }
alan1974 0:a91cd1b08360 413 }//if joined
alan1974 0:a91cd1b08360 414 break;
alan1974 0:a91cd1b08360 415 case XDOT_CMD_SET_RADIO:
alan1974 0:a91cd1b08360 416 pc.printf("\n\r proc cmd: CMD_SET_RADIO");
alan1974 0:a91cd1b08360 417 pkt_setradiodwn *pDwnRadio= (pkt_setradiodwn*)&buf_xmt[0];
alan1974 0:a91cd1b08360 418 pkt_setradioup *pUpRadio = (pkt_setradioup*)&buf_rcv[0];
alan1974 0:a91cd1b08360 419 pDwnRadio->ack = I2C_ACK_PROC;
alan1974 0:a91cd1b08360 420 pDwnRadio->cmd = XDOT_CMD_SET_RADIO;
alan1974 0:a91cd1b08360 421
alan1974 0:a91cd1b08360 422 if (pUpRadio->bSetParams){
alan1974 2:0af50f386eb2 423 pc.printf("\n\r setting subband to %d ",pUpRadio->params.sub_band);
alan1974 2:0af50f386eb2 424 cfg_network(false,true,(uint8_t)pUpRadio->params.sub_band);
alan1974 2:0af50f386eb2 425 pc.printf("\n\r setting adr to %d ",pUpRadio->params.aDR);
alan1974 2:0af50f386eb2 426 dot->setAdr((uint8_t)pUpRadio->params.aDR); // enable or disable Adaptive Data Rate
alan1974 2:0af50f386eb2 427 pc.printf("\n\r setting antenna gain to %d ",pUpRadio->params.antennaGaindBi);
alan1974 2:0af50f386eb2 428 dot->setAntennaGain(pUpRadio->params.antennaGaindBi);
alan1974 2:0af50f386eb2 429 pc.printf("\n\r setting radio tx power to %d ",pUpRadio->params.txPowerdBm);
alan1974 2:0af50f386eb2 430 dot->setTxPower(pUpRadio->params.txPowerdBm);
alan1974 2:0af50f386eb2 431 pc.printf("\n\r setting tx datarate to %d ",pUpRadio->params.dataRate);
alan1974 2:0af50f386eb2 432 dot->setTxDataRate(pUpRadio->params.dataRate);
alan1974 1:0d25d9ddbe9f 433 // pc.printf("\n\r setting application port %d ",pUpRadio->appPort);
alan1974 1:0d25d9ddbe9f 434 // dot->setAppPort(pUpRadio->appPort);
alan1974 2:0af50f386eb2 435 pc.printf("\n\r setting link check threshold %d ",pUpRadio->params.linkChkCount);
alan1974 2:0af50f386eb2 436 dot->setLinkCheckThreshold(pUpRadio->params.linkChkCount);
alan1974 0:a91cd1b08360 437 pc.printf("\n\r saving configuration");
alan1974 0:a91cd1b08360 438 if (!dot->saveConfig())logError("failed to save configuration");
alan1974 0:a91cd1b08360 439 display_config();
alan1974 0:a91cd1b08360 440 }
alan1974 0:a91cd1b08360 441
alan1974 2:0af50f386eb2 442 pDwnRadio->params.public_network = public_network;
alan1974 2:0af50f386eb2 443 pDwnRadio->params.sub_band = dot->getFrequencySubBand();
alan1974 2:0af50f386eb2 444 pDwnRadio->params.linkChkCount = dot->getLinkCheckThreshold();
alan1974 2:0af50f386eb2 445 pDwnRadio->params.maxDataLen = dot->getMaxPacketLength();
alan1974 2:0af50f386eb2 446 pDwnRadio->params.maxTxPowerdBm = dot->getMaxTxPower();
alan1974 2:0af50f386eb2 447 pDwnRadio->params.minTxPowerdBm = dot->getMinTxPower();
alan1974 2:0af50f386eb2 448 pDwnRadio->params.aDR = dot->getAdr();
alan1974 2:0af50f386eb2 449 pDwnRadio->params.antennaGaindBi = dot->getAntennaGain();
alan1974 2:0af50f386eb2 450 pDwnRadio->params.txPowerdBm = dot->getTxPower();
alan1974 2:0af50f386eb2 451 pDwnRadio->params.dataRate = dot->getTxDataRate();
alan1974 1:0d25d9ddbe9f 452 // pDwnRadio->appPort = dot->getAppPort();
alan1974 0:a91cd1b08360 453 break;
alan1974 0:a91cd1b08360 454 case XDOT_CMD_GET_EUI: //0307: modified to include radio parameter settings
alan1974 0:a91cd1b08360 455 pc.printf("\n\r proc cmd: get EUI");
alan1974 0:a91cd1b08360 456 pkt_eui *peui = (pkt_eui*)&buf_xmt[0];
alan1974 0:a91cd1b08360 457 peui->ack = I2C_ACK_PROC;
alan1974 0:a91cd1b08360 458 peui->cmd = XDOT_CMD_GET_EUI;
alan1974 0:a91cd1b08360 459 upstream_packet.clear();
alan1974 0:a91cd1b08360 460 upstream_packet = dot->getDeviceId();
alan1974 0:a91cd1b08360 461 peui->dataLen = upstream_packet.size();
alan1974 0:a91cd1b08360 462 for (i=0; i< peui->dataLen;i++) peui->euiData[i] = upstream_packet[i];
alan1974 0:a91cd1b08360 463 for (i=0; i< 4;i++) peui->apiLvlData[i] = api_level[i];
alan1974 0:a91cd1b08360 464 for (i=0; i< 4;i++) peui->verLvlData[i] = ver_level[i];
alan1974 0:a91cd1b08360 465 peui->dataLen = sizeof(pkt_eui)-3; //size of struc minus first 3 bytes
alan1974 0:a91cd1b08360 466 pc.printf("\n\r eui data length: %d",peui->dataLen);
alan1974 0:a91cd1b08360 467 break;
alan1974 1:0d25d9ddbe9f 468 case XDOT_CMD_SET_NTWKSESS: //read or write network seesion to xdot flash
alan1974 1:0d25d9ddbe9f 469 bool bWriteSession = (bool)buf_rcv[1];
alan1974 1:0d25d9ddbe9f 470 if (bWriteSession){
alan1974 1:0d25d9ddbe9f 471 pc.printf("\n\r proc cmd writing network sesion to flash");
alan1974 1:0d25d9ddbe9f 472 dot->saveNetworkSession();
alan1974 1:0d25d9ddbe9f 473 }
alan1974 1:0d25d9ddbe9f 474 else{
alan1974 1:0d25d9ddbe9f 475 pc.printf("\n\r reading network session from flash");
alan1974 1:0d25d9ddbe9f 476 dot->restoreNetworkSession();
alan1974 1:0d25d9ddbe9f 477 }
alan1974 1:0d25d9ddbe9f 478 pkt_ntwrk *pktwrk = (pkt_ntwrk*)&buf_xmt[0];
alan1974 1:0d25d9ddbe9f 479 pktwrk->ack = I2C_ACK_PROC;
alan1974 1:0d25d9ddbe9f 480 pktwrk->cmd = XDOT_CMD_SET_NTWKSESS;
alan1974 1:0d25d9ddbe9f 481 pktwrk->bSetNetwrk = (uint8_t)bWriteSession;
alan1974 1:0d25d9ddbe9f 482 break;
alan1974 1:0d25d9ddbe9f 483 case XDOT_CMD_SET_NVM:
alan1974 2:0af50f386eb2 484 pkt_setnvmdwn *pDwnNvm= (pkt_setnvmdwn*)&buf_xmt[0];
alan1974 2:0af50f386eb2 485 pkt_setnvmup *pUpNvm = (pkt_setnvmup*)&buf_rcv[0];
alan1974 2:0af50f386eb2 486 pDwnNvm->ack = I2C_ACK_PROC;
alan1974 2:0af50f386eb2 487 pDwnNvm->cmd = XDOT_CMD_SET_RADIO;
alan1974 2:0af50f386eb2 488 nvmWrite(&pUpNvm->nvmData);
alan1974 2:0af50f386eb2 489 nvmRead(&pDwnNvm->nvmData);
alan1974 1:0d25d9ddbe9f 490 break;
alan1974 0:a91cd1b08360 491 case XDOT_CMD_SET_KEY_X:
alan1974 0:a91cd1b08360 492 pc.printf("\n\r proc cmd: set a key, simulating minm delay before wake pulse\r\n");
alan1974 0:a91cd1b08360 493 wait_ms(I2C_MIN_WAIT_DELAY);
alan1974 0:a91cd1b08360 494 buf_xmt[0] = I2C_ACK_PROC;
alan1974 0:a91cd1b08360 495 buf_xmt[1] = XDOT_CMD_SET_KEY_X;
alan1974 0:a91cd1b08360 496 break;
alan1974 0:a91cd1b08360 497 case XDOT_CMD_GATEWAY_PING:
alan1974 0:a91cd1b08360 498 pc.printf("\n\r proc cmd: xmit gateway ping\r\n");
alan1974 0:a91cd1b08360 499 pkt_ping *pPing = (pkt_ping*)&buf_xmt[0];
alan1974 0:a91cd1b08360 500 pPing->ack = I2C_ACK_PROC;
alan1974 0:a91cd1b08360 501 pPing->cmd = XDOT_CMD_GATEWAY_PING;
alan1974 0:a91cd1b08360 502 pPing->dataLen = 3; //only 3 bytes returned
alan1974 0:a91cd1b08360 503 pc.printf("\r\n----------- SENDING GATEWAY PING \r\n");
alan1974 0:a91cd1b08360 504 mDot::ping_response ping_res;
alan1974 0:a91cd1b08360 505 ping_res = dot->ping();
alan1974 0:a91cd1b08360 506 pPing->status = (int8_t)ping_res.status;
alan1974 0:a91cd1b08360 507 pPing->rssi = (int8_t)ping_res.rssi;
alan1974 0:a91cd1b08360 508 pPing->snr = (int8_t)ping_res.snr;
alan1974 0:a91cd1b08360 509 if (ping_res.status == 0)
alan1974 0:a91cd1b08360 510 pc.printf("\r\n----------- GATEWAY PING SUCCEEDED \r\n");
alan1974 0:a91cd1b08360 511 else
alan1974 0:a91cd1b08360 512 pc.printf("\r\n----------- GATEWAY PING FAIL \r\n");
alan1974 0:a91cd1b08360 513 break;
alan1974 0:a91cd1b08360 514 default:
alan1974 0:a91cd1b08360 515 pc.printf("\n\r proc cmd not recognized:%x",buf_rcv[0]);
alan1974 0:a91cd1b08360 516 wait_ms(I2C_MIN_WAIT_DELAY);
alan1974 0:a91cd1b08360 517 buf_xmt[0] = I2C_ACK_PROC;
alan1974 0:a91cd1b08360 518 buf_xmt[1] = XDOT_CMD_UNDEFINED;
alan1974 0:a91cd1b08360 519 } //switch buf_rcv[0]
alan1974 0:a91cd1b08360 520 //gpio1 =1; //test
alan1974 0:a91cd1b08360 521 if (bPulseLoraWake) i2c_pulse_wake(); //pulse wake-up lo->hi->lo to signal proc that xdot ready to send ack
alan1974 0:a91cd1b08360 522
alan1974 0:a91cd1b08360 523 } //switch i2c_proc_comm
alan1974 0:a91cd1b08360 524 } //while
alan1974 0:a91cd1b08360 525
alan1974 0:a91cd1b08360 526 } //main
alan1974 0:a91cd1b08360 527