alan broad
/
carbon_v5_arm_studio
arm studio build
src/wbit_util.cpp@7:fba1e8fc7693, 2018-08-10 (annotated)
- Committer:
- alan1974
- Date:
- Fri Aug 10 19:17:43 2018 +0000
- Revision:
- 7:fba1e8fc7693
- Parent:
- 5:abfe25f0de33
- Child:
- 8:a5316708e51d
works
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:0d25d9ddbe9f | 86 | return dot->nvmWrite(0,pNvm,sizeof(nvm)); |
alan1974 | 4:d87f8dcf40ef | 87 | |
alan1974 | 1:0d25d9ddbe9f | 88 | } |
alan1974 | 1:0d25d9ddbe9f | 89 | //============================================================================== |
alan1974 | 1:0d25d9ddbe9f | 90 | //nvmRead |
alan1974 | 1:0d25d9ddbe9f | 91 | //- read nvmData struc from nvm memory |
alan1974 | 1:0d25d9ddbe9f | 92 | //- if bad chksum then default to hard code network keys |
alan1974 | 1:0d25d9ddbe9f | 93 | //============================================================================== |
alan1974 | 1:0d25d9ddbe9f | 94 | bool nvmRead(nvm *pNvm){ |
alan1974 | 1:0d25d9ddbe9f | 95 | uint8_t i; |
alan1974 | 1:0d25d9ddbe9f | 96 | dot->nvmRead(0,pNvm,sizeof(nvm)); |
alan1974 | 2:0af50f386eb2 | 97 | uint8_t chksum = getNvmChkSum(pNvm); |
alan1974 | 7:fba1e8fc7693 | 98 | |
alan1974 | 1:0d25d9ddbe9f | 99 | if (chksum == pNvm->chksum){ |
alan1974 | 7:fba1e8fc7693 | 100 | logInfo("nvmRead: chksum ok"); |
alan1974 | 1:0d25d9ddbe9f | 101 | return true; |
alan1974 | 1:0d25d9ddbe9f | 102 | } |
alan1974 | 7:fba1e8fc7693 | 103 | |
alan1974 | 1:0d25d9ddbe9f | 104 | //bad chksum, resort to old hard coded keys |
alan1974 | 5:abfe25f0de33 | 105 | if(verbose)pc.printf("nvmRead: bad chksum, using default values"); |
alan1974 | 1:0d25d9ddbe9f | 106 | for (i = 0; i < sizeof(network_id);i++){ |
alan1974 | 1:0d25d9ddbe9f | 107 | pNvm->network_id[i] = network_id[i]; |
alan1974 | 1:0d25d9ddbe9f | 108 | } |
alan1974 | 1:0d25d9ddbe9f | 109 | for (i = 0; i < sizeof(network_key);i++){ |
alan1974 | 1:0d25d9ddbe9f | 110 | pNvm->network_key[i] = network_key[i]; |
alan1974 | 1:0d25d9ddbe9f | 111 | } |
alan1974 | 7:fba1e8fc7693 | 112 | pNvm->bLogOutputOn = 0; //disable log output |
alan1974 | 5:abfe25f0de33 | 113 | // printNmvData(pNvm); |
alan1974 | 1:0d25d9ddbe9f | 114 | return false; |
alan1974 | 1:0d25d9ddbe9f | 115 | } |
alan1974 | 1:0d25d9ddbe9f | 116 | //============================================================================ |
alan1974 | 1:0d25d9ddbe9f | 117 | |
alan1974 | 0:a91cd1b08360 | 118 | uint8_t join_network_attempts_wbit() { |
alan1974 | 0:a91cd1b08360 | 119 | return j_attempts; |
alan1974 | 0:a91cd1b08360 | 120 | } |
alan1974 | 0:a91cd1b08360 | 121 | |
alan1974 | 0:a91cd1b08360 | 122 | bool join_network_wbit(uint8_t nmbAttempts) { |
alan1974 | 0:a91cd1b08360 | 123 | //int32_t j_attempts = 0; |
alan1974 | 0:a91cd1b08360 | 124 | j_attempts = 0; |
alan1974 | 0:a91cd1b08360 | 125 | int32_t ret = mDot::MDOT_ERROR; |
alan1974 | 0:a91cd1b08360 | 126 | |
alan1974 | 0:a91cd1b08360 | 127 | // attempt to join the network |
alan1974 | 0:a91cd1b08360 | 128 | while (ret != mDot::MDOT_OK) { |
alan1974 | 0:a91cd1b08360 | 129 | j_attempts++; |
alan1974 | 0:a91cd1b08360 | 130 | logInfo("attempt %d to join network",j_attempts); |
alan1974 | 0:a91cd1b08360 | 131 | ret = dot->joinNetwork(); |
alan1974 | 0:a91cd1b08360 | 132 | if (ret == mDot::MDOT_OK) return true; |
alan1974 | 0:a91cd1b08360 | 133 | |
alan1974 | 0:a91cd1b08360 | 134 | logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
alan1974 | 0:a91cd1b08360 | 135 | if (j_attempts >= nmbAttempts) |
alan1974 | 0:a91cd1b08360 | 136 | { |
alan1974 | 0:a91cd1b08360 | 137 | logInfo("attempts %d to join network exceeds specified attempts $d ",j_attempts,nmbAttempts); |
alan1974 | 0:a91cd1b08360 | 138 | return false; |
alan1974 | 0:a91cd1b08360 | 139 | } |
alan1974 | 0:a91cd1b08360 | 140 | // in some frequency bands we need to wait until another channel is available before transmitting again |
alan1974 | 0:a91cd1b08360 | 141 | uint32_t delay_s = (dot->getNextTxMs() / 1000) + 1; |
alan1974 | 0:a91cd1b08360 | 142 | if (delay_s < 2) { |
alan1974 | 0:a91cd1b08360 | 143 | logInfo("waiting %lu s until next free channel", delay_s); |
alan1974 | 0:a91cd1b08360 | 144 | wait(delay_s); |
alan1974 | 0:a91cd1b08360 | 145 | } else { |
alan1974 | 0:a91cd1b08360 | 146 | logInfo("sleeping %lu s until next free channel", delay_s); |
alan1974 | 0:a91cd1b08360 | 147 | dot->sleep(delay_s, mDot::RTC_ALARM, false); |
alan1974 | 0:a91cd1b08360 | 148 | } |
alan1974 | 0:a91cd1b08360 | 149 | }//while |
alan1974 | 0:a91cd1b08360 | 150 | return false; |
alan1974 | 0:a91cd1b08360 | 151 | } |