arm studio build

Dependencies:   libxDot-mbed5

Committer:
alan1974
Date:
Sat Aug 11 17:34:42 2018 +0000
Revision:
8:a5316708e51d
Parent:
7:fba1e8fc7693
Child:
9:cc23b2049639
fixed failure due to display turned off

Who changed what in which revision?

UserRevisionLine numberNew contents of line
alan1974 1:0d25d9ddbe9f 1 #include "global.h"
alan1974 0:a91cd1b08360 2 #include "wbit_util.h"
alan1974 0:a91cd1b08360 3 #include "dot_util.h"
alan1974 1:0d25d9ddbe9f 4 #include "commI2C.h"
alan1974 0:a91cd1b08360 5 #include "mbed.h"
alan1974 0:a91cd1b08360 6 #include "mDot.h"
alan1974 2:0af50f386eb2 7 extern Serial pc;
alan1974 5:abfe25f0de33 8 extern bool verbose;
alan1974 1:0d25d9ddbe9f 9 uint8_t j_attempts = 0; //return number of attempts it took to join the network
alan1974 1:0d25d9ddbe9f 10
alan1974 1:0d25d9ddbe9f 11
alan1974 1:0d25d9ddbe9f 12 //network keys
alan1974 1:0d25d9ddbe9f 13 //these are used as backup keys in case nvm memory is corrupted and we can't read the keys correctly
alan1974 1:0d25d9ddbe9f 14 uint8_t network_id[] = { 0x90, 0xF1, 0x47, 0x90, 0x6C, 0x48, 0x1D, 0x29 }; //static id not used anymore but don't comment out
alan1974 2:0af50f386eb2 15 uint8_t network_key[] = { 0x0F, 0xF9, 0xA2, 0x90, 0x2E, 0xAA, 0x6B, 0x8C, 0x6A, 0x4E, 0xFD, 0x67, 0xF9, 0xA6, 0xF3, 0xD3 }; //OTAA appkey
alan1974 5:abfe25f0de33 16
alan1974 5:abfe25f0de33 17 //============================================================================
alan1974 5:abfe25f0de33 18 // printRadioCfg
alan1974 5:abfe25f0de33 19
alan1974 5:abfe25f0de33 20 //============================================================================
alan1974 5:abfe25f0de33 21 void printRadioCfg(){
alan1974 5:abfe25f0de33 22 if(verbose){
alan1974 5:abfe25f0de33 23 pc.printf("\r\n************************************************************");
alan1974 5:abfe25f0de33 24 pc.printf("\r\n saved radio configuration: ");
alan1974 5:abfe25f0de33 25 pc.printf("\r\n public network: %d ",dot->getPublicNetwork());
alan1974 5:abfe25f0de33 26 pc.printf("\r\n subband: %d ",dot->getFrequencySubBand());
alan1974 5:abfe25f0de33 27 pc.printf("\n\r join delay %d ",dot->getJoinDelay());
alan1974 5:abfe25f0de33 28 pc.printf("\r\n ADR: %d ",dot->getAdr());
alan1974 5:abfe25f0de33 29 pc.printf("\r\n antenna gain: %d ",dot->getAntennaGain());
alan1974 5:abfe25f0de33 30 pc.printf("\r\n transmit max pwr: %d ",dot->getMaxTxPower()); //not saved in cfg
alan1974 5:abfe25f0de33 31 pc.printf("\r\n transmit min pwr: %d ",dot->getMinTxPower()); //not saved in cfg
alan1974 5:abfe25f0de33 32 pc.printf("\r\n transmit pwr: %d ",dot->getTxPower());
alan1974 5:abfe25f0de33 33 pc.printf("\n\r tx inverted %d ",dot->getTxInverted());
alan1974 5:abfe25f0de33 34 pc.printf("\n\r rx inverted %d ",dot->getRxInverted());
alan1974 5:abfe25f0de33 35 pc.printf("\n\r rx delay %d ",dot->getRxDelay());
alan1974 5:abfe25f0de33 36 pc.printf("\r\n TxDataRate: %d ",dot->getTxDataRate());
alan1974 5:abfe25f0de33 37 pc.printf("\r\n maxPktLen: %d ",dot->getMaxPacketLength()); //not saved in cfg
alan1974 5:abfe25f0de33 38 pc.printf("\r\n************************************************************\r\n");
alan1974 5:abfe25f0de33 39 }
alan1974 5:abfe25f0de33 40 }
alan1974 2:0af50f386eb2 41 //==============================================================================
alan1974 2:0af50f386eb2 42 //printNmvData
alan1974 1:0d25d9ddbe9f 43 //==============================================================================
alan1974 2:0af50f386eb2 44 void printNmvData(nvm *pNvm){
alan1974 4:d87f8dcf40ef 45 uint8_t i;
alan1974 7:fba1e8fc7693 46 if(verbose){
alan1974 7:fba1e8fc7693 47 pc.printf("\r\n************************************************************");
alan1974 7:fba1e8fc7693 48 pc.printf("\r\nnon-volatile memory (nvm):");
alan1974 7:fba1e8fc7693 49 pc.printf("\r\nnetwork_id:");
alan1974 7:fba1e8fc7693 50 for (i = 0; i < sizeof(network_id);i++)
alan1974 7:fba1e8fc7693 51 pc.printf(" %x",pNvm->network_id[i]);
alan1974 7:fba1e8fc7693 52 pc.printf("\r\nnetwork_key:");
alan1974 7:fba1e8fc7693 53 for (i = 0 ; i < sizeof(network_key);i++)
alan1974 7:fba1e8fc7693 54 pc.printf(" %x",pNvm->network_key[i]);
alan1974 7:fba1e8fc7693 55 pc.printf("\r\nbLogOutputOn: %d ",pNvm->bLogOutputOn);
alan1974 7:fba1e8fc7693 56
alan1974 7:fba1e8fc7693 57 pc.printf("\r\n static backup keys:");
alan1974 7:fba1e8fc7693 58 pc.printf("\r\nnetwork_id:");
alan1974 7:fba1e8fc7693 59 for (i = 0; i < sizeof(network_id);i++)
alan1974 7:fba1e8fc7693 60 pc.printf(" %x",network_id[i]);
alan1974 7:fba1e8fc7693 61 pc.printf("\r\nnetwork_key:");
alan1974 7:fba1e8fc7693 62 for (i = 0 ; i < sizeof(network_key);i++)
alan1974 7:fba1e8fc7693 63 pc.printf(" %x",network_key[i]);
alan1974 7:fba1e8fc7693 64 }
alan1974 5:abfe25f0de33 65 if(verbose)pc.printf("\r\n************************************************************\r\n");
alan1974 2:0af50f386eb2 66 }
alan1974 2:0af50f386eb2 67 //==============================================================================
alan1974 2:0af50f386eb2 68 //getNvmChkSum
alan1974 1:0d25d9ddbe9f 69 //compute chksum for nvm data; don't include chksum byte in nvm struc
alan1974 1:0d25d9ddbe9f 70 //==============================================================================
alan1974 2:0af50f386eb2 71 uint8_t getNvmChkSum(nvm *pNvm){
alan1974 2:0af50f386eb2 72 uint8_t i;
alan1974 1:0d25d9ddbe9f 73 uint8_t chksum= 0;
alan1974 1:0d25d9ddbe9f 74 uint8_t *pData = (uint8_t *)pNvm;
alan1974 1:0d25d9ddbe9f 75
alan1974 1:0d25d9ddbe9f 76 for (i = 0 ; i < sizeof(nvm)-1;i++)chksum += pData[i];
alan1974 1:0d25d9ddbe9f 77 return chksum;
alan1974 1:0d25d9ddbe9f 78 }
alan1974 1:0d25d9ddbe9f 79 //==============================================================================
alan1974 1:0d25d9ddbe9f 80 //nvmWrite
alan1974 1:0d25d9ddbe9f 81 //write nvmData struc to nvm memory
alan1974 1:0d25d9ddbe9f 82 //==============================================================================
alan1974 1:0d25d9ddbe9f 83 bool nvmWrite(nvm *pNvm){
alan1974 2:0af50f386eb2 84 pNvm->chksum = getNvmChkSum(pNvm);
alan1974 4:d87f8dcf40ef 85 printNmvData(pNvm);
alan1974 8:a5316708e51d 86 return dot->nvmWrite(0,pNvm,sizeof(nvm));
alan1974 1:0d25d9ddbe9f 87 }
alan1974 1:0d25d9ddbe9f 88 //==============================================================================
alan1974 8:a5316708e51d 89 //nvmRestorekeys
alan1974 8:a5316708e51d 90 //restore hard coded keys
alan1974 8:a5316708e51d 91 //==============================================================================
alan1974 8:a5316708e51d 92 bool nvmRestoreKeys(nvm *pNvm){
alan1974 8:a5316708e51d 93 uint8_t i;
alan1974 8:a5316708e51d 94 for (i = 0; i < sizeof(network_id);i++){
alan1974 8:a5316708e51d 95 pNvm->network_id[i] = network_id[i];
alan1974 8:a5316708e51d 96 }
alan1974 8:a5316708e51d 97 for (i = 0; i < sizeof(network_key);i++){
alan1974 8:a5316708e51d 98 pNvm->network_key[i] = network_key[i];
alan1974 8:a5316708e51d 99 }
alan1974 8:a5316708e51d 100 pNvm->bLogOutputOn = 0; //disable log output
alan1974 8:a5316708e51d 101 pNvm->chksum = getNvmChkSum(pNvm);
alan1974 8:a5316708e51d 102 printNmvData(pNvm);
alan1974 8:a5316708e51d 103 return dot->nvmWrite(0,pNvm,sizeof(nvm));
alan1974 8:a5316708e51d 104 }
alan1974 8:a5316708e51d 105 //==============================================================================
alan1974 1:0d25d9ddbe9f 106 //nvmRead
alan1974 1:0d25d9ddbe9f 107 //- read nvmData struc from nvm memory
alan1974 1:0d25d9ddbe9f 108 //- if bad chksum then default to hard code network keys
alan1974 1:0d25d9ddbe9f 109 //==============================================================================
alan1974 1:0d25d9ddbe9f 110 bool nvmRead(nvm *pNvm){
alan1974 1:0d25d9ddbe9f 111 uint8_t i;
alan1974 1:0d25d9ddbe9f 112 dot->nvmRead(0,pNvm,sizeof(nvm));
alan1974 2:0af50f386eb2 113 uint8_t chksum = getNvmChkSum(pNvm);
alan1974 7:fba1e8fc7693 114
alan1974 1:0d25d9ddbe9f 115 if (chksum == pNvm->chksum){
alan1974 7:fba1e8fc7693 116 logInfo("nvmRead: chksum ok");
alan1974 1:0d25d9ddbe9f 117 return true;
alan1974 1:0d25d9ddbe9f 118 }
alan1974 1:0d25d9ddbe9f 119 //bad chksum, resort to old hard coded keys
alan1974 8:a5316708e51d 120 if(verbose)pc.printf("nvmRead: bad chksum, using default values");
alan1974 8:a5316708e51d 121 nvmRestoreKeys(pNvm);
alan1974 8:a5316708e51d 122 return false;
alan1974 1:0d25d9ddbe9f 123 }
alan1974 1:0d25d9ddbe9f 124 //============================================================================
alan1974 1:0d25d9ddbe9f 125
alan1974 0:a91cd1b08360 126 uint8_t join_network_attempts_wbit() {
alan1974 0:a91cd1b08360 127 return j_attempts;
alan1974 0:a91cd1b08360 128 }
alan1974 0:a91cd1b08360 129
alan1974 0:a91cd1b08360 130 bool join_network_wbit(uint8_t nmbAttempts) {
alan1974 0:a91cd1b08360 131 //int32_t j_attempts = 0;
alan1974 0:a91cd1b08360 132 j_attempts = 0;
alan1974 0:a91cd1b08360 133 int32_t ret = mDot::MDOT_ERROR;
alan1974 0:a91cd1b08360 134
alan1974 0:a91cd1b08360 135 // attempt to join the network
alan1974 0:a91cd1b08360 136 while (ret != mDot::MDOT_OK) {
alan1974 0:a91cd1b08360 137 j_attempts++;
alan1974 0:a91cd1b08360 138 logInfo("attempt %d to join network",j_attempts);
alan1974 0:a91cd1b08360 139 ret = dot->joinNetwork();
alan1974 0:a91cd1b08360 140 if (ret == mDot::MDOT_OK) return true;
alan1974 0:a91cd1b08360 141
alan1974 0:a91cd1b08360 142 logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
alan1974 0:a91cd1b08360 143 if (j_attempts >= nmbAttempts)
alan1974 0:a91cd1b08360 144 {
alan1974 0:a91cd1b08360 145 logInfo("attempts %d to join network exceeds specified attempts $d ",j_attempts,nmbAttempts);
alan1974 0:a91cd1b08360 146 return false;
alan1974 0:a91cd1b08360 147 }
alan1974 0:a91cd1b08360 148 // in some frequency bands we need to wait until another channel is available before transmitting again
alan1974 0:a91cd1b08360 149 uint32_t delay_s = (dot->getNextTxMs() / 1000) + 1;
alan1974 0:a91cd1b08360 150 if (delay_s < 2) {
alan1974 0:a91cd1b08360 151 logInfo("waiting %lu s until next free channel", delay_s);
alan1974 0:a91cd1b08360 152 wait(delay_s);
alan1974 0:a91cd1b08360 153 } else {
alan1974 0:a91cd1b08360 154 logInfo("sleeping %lu s until next free channel", delay_s);
alan1974 0:a91cd1b08360 155 dot->sleep(delay_s, mDot::RTC_ALARM, false);
alan1974 0:a91cd1b08360 156 }
alan1974 0:a91cd1b08360 157 }//while
alan1974 0:a91cd1b08360 158 return false;
alan1974 0:a91cd1b08360 159 }