Fork to see if I can get working

Dependencies:   BufferedSerial OneWire WinbondSPIFlash libxDot-dev-mbed5-deprecated

Fork of xDotBridge_update_test20180823 by Matt Briggs

Committer:
Matt Briggs
Date:
Wed Jan 04 17:23:12 2017 -0700
Revision:
28:b14b2926e916
Parent:
27:6b68ff715ae1
Child:
29:e05e35976cfe
Fixed sleep current

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Matt Briggs 22:9453658b8d4b 1 #include "config.h"
Mike Fiore 11:d2e31743433a 2 #include "dot_util.h"
Mike Fiore 14:19fae4509473 3 #include "RadioEvent.h"
Matt Briggs 26:9411b26a5084 4 #include <math.h>
Mike Fiore 11:d2e31743433a 5
Matt Briggs 22:9453658b8d4b 6
mfiore 17:d4f82e16de5f 7 /////////////////////////////////////////////////////////////////////////////
mfiore 17:d4f82e16de5f 8 // -------------------- DOT LIBRARY REQUIRED ------------------------------//
mfiore 17:d4f82e16de5f 9 // * Because these example programs can be used for both mDot and xDot //
mfiore 17:d4f82e16de5f 10 // devices, the LoRa stack is not included. The libmDot library should //
mfiore 17:d4f82e16de5f 11 // be imported if building for mDot devices. The libxDot library //
mfiore 17:d4f82e16de5f 12 // should be imported if building for xDot devices. //
mfiore 17:d4f82e16de5f 13 // * https://developer.mbed.org/teams/MultiTech/code/libmDot-dev-mbed5/ //
mfiore 17:d4f82e16de5f 14 // * https://developer.mbed.org/teams/MultiTech/code/libmDot-mbed5/ //
mfiore 17:d4f82e16de5f 15 // * https://developer.mbed.org/teams/MultiTech/code/libxDot-dev-mbed5/ //
mfiore 17:d4f82e16de5f 16 // * https://developer.mbed.org/teams/MultiTech/code/libxDot-mbed5/ //
mfiore 17:d4f82e16de5f 17 /////////////////////////////////////////////////////////////////////////////
mfiore 17:d4f82e16de5f 18
Mike Fiore 11:d2e31743433a 19 /////////////////////////////////////////////////////////////
Mike Fiore 11:d2e31743433a 20 // * these options must match between the two devices in //
Mike Fiore 11:d2e31743433a 21 // order for communication to be successful
Mike Fiore 11:d2e31743433a 22 /////////////////////////////////////////////////////////////
Mike Fiore 11:d2e31743433a 23 static uint8_t network_address[] = { 0x01, 0x02, 0x03, 0x04 };
Mike Fiore 11:d2e31743433a 24 static uint8_t network_session_key[] = { 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04 };
mbriggs_vortex 19:75b28b4117cc 25 static uint8_t data_session_key[] = { 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04 };
Mike Fiore 11:d2e31743433a 26
Matt Briggs 26:9411b26a5084 27 // wireless bridge protocol
Matt Briggs 26:9411b26a5084 28 //const float dutyCycle = 0.01; // 1%
Matt Briggs 27:6b68ff715ae1 29 const float rxSleepTime = 2000; // ms (one second resolution, min 2 seconds)
Matt Briggs 26:9411b26a5084 30 const uint8_t maxPayloadSize = 10; // Number of bytes (used for toa calcultion)
Matt Briggs 26:9411b26a5084 31
Matt Briggs 25:fea776239709 32 DigitalOut led1(LED1);
Mike Fiore 11:d2e31743433a 33 mDot* dot = NULL;
Mike Fiore 11:d2e31743433a 34
Mike Fiore 11:d2e31743433a 35 Serial pc(USBTX, USBRX);
Mike Fiore 11:d2e31743433a 36
Matt Briggs 27:6b68ff715ae1 37 //Ticker t;
Matt Briggs 27:6b68ff715ae1 38 //
Matt Briggs 27:6b68ff715ae1 39 //class RxHandler {
Matt Briggs 27:6b68ff715ae1 40 // private:
Matt Briggs 27:6b68ff715ae1 41 // uint m_dwnLink;
Matt Briggs 27:6b68ff715ae1 42 // mDot *m_dot;
Matt Briggs 27:6b68ff715ae1 43 // public:
Matt Briggs 27:6b68ff715ae1 44 // RxHandler (mDot *dot) {
Matt Briggs 27:6b68ff715ae1 45 // m_dot = dot;
Matt Briggs 27:6b68ff715ae1 46 // m_dwnLink = dot->getDownLinkCounter();
Matt Briggs 27:6b68ff715ae1 47 // }
Matt Briggs 27:6b68ff715ae1 48 // void listen() {
Matt Briggs 27:6b68ff715ae1 49 // std::vector<uint8_t> data;
Matt Briggs 27:6b68ff715ae1 50 // led1 = 1; // FIXME
Matt Briggs 27:6b68ff715ae1 51 // logInfo("Listening for new message current DLC: %d, time: %lu", m_dwnLink, us_ticker_read());
Matt Briggs 27:6b68ff715ae1 52 // wait(0.060); // Wait twice the time on air
Matt Briggs 27:6b68ff715ae1 53 // if (m_dwnLink < dot->getDownLinkCounter()) {
Matt Briggs 27:6b68ff715ae1 54 // m_dwnLink = dot->getDownLinkCounter();
Matt Briggs 27:6b68ff715ae1 55 // m_dot->recv(data);
Matt Briggs 27:6b68ff715ae1 56 // std::string dataStr(data.begin(), data.end());
Matt Briggs 27:6b68ff715ae1 57 // logInfo("Got msg num: -, payload: %s", dataStr.c_str());
Matt Briggs 27:6b68ff715ae1 58 // wait(0.5);
Matt Briggs 27:6b68ff715ae1 59 // }
Matt Briggs 27:6b68ff715ae1 60 // led1=0;
Matt Briggs 27:6b68ff715ae1 61 // }
Matt Briggs 27:6b68ff715ae1 62 //};
Matt Briggs 27:6b68ff715ae1 63
Mike Fiore 11:d2e31743433a 64 int main() {
Mike Fiore 14:19fae4509473 65 // Custom event handler for automatically displaying RX data
Mike Fiore 11:d2e31743433a 66 RadioEvent events;
Mike Fiore 11:d2e31743433a 67 uint32_t tx_frequency;
Mike Fiore 11:d2e31743433a 68 uint8_t tx_datarate;
Mike Fiore 11:d2e31743433a 69 uint8_t tx_power;
Mike Fiore 11:d2e31743433a 70 uint8_t frequency_band;
Mike Fiore 11:d2e31743433a 71
Mike Fiore 11:d2e31743433a 72 pc.baud(115200);
Mike Fiore 11:d2e31743433a 73
Matt Briggs 27:6b68ff715ae1 74 mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
Mike Fiore 11:d2e31743433a 75
Mike Fiore 11:d2e31743433a 76 dot = mDot::getInstance();
Mike Fiore 11:d2e31743433a 77
Mike Fiore 16:a3832552dfe1 78 logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
Mike Fiore 16:a3832552dfe1 79
Mike Fiore 12:ec9768677cea 80 // start from a well-known state
Mike Fiore 12:ec9768677cea 81 logInfo("defaulting Dot configuration");
Mike Fiore 12:ec9768677cea 82 dot->resetConfig();
Mike Fiore 12:ec9768677cea 83
Mike Fiore 11:d2e31743433a 84 // make sure library logging is turned on
Matt Briggs 27:6b68ff715ae1 85 dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
Mike Fiore 11:d2e31743433a 86
Mike Fiore 11:d2e31743433a 87 // attach the custom events handler
Mike Fiore 11:d2e31743433a 88 dot->setEvents(&events);
Mike Fiore 11:d2e31743433a 89
Mike Fiore 11:d2e31743433a 90 // update configuration if necessary
Mike Fiore 11:d2e31743433a 91 if (dot->getJoinMode() != mDot::PEER_TO_PEER) {
Mike Fiore 11:d2e31743433a 92 logInfo("changing network join mode to PEER_TO_PEER");
Mike Fiore 11:d2e31743433a 93 if (dot->setJoinMode(mDot::PEER_TO_PEER) != mDot::MDOT_OK) {
Mike Fiore 11:d2e31743433a 94 logError("failed to set network join mode to PEER_TO_PEER");
Mike Fiore 11:d2e31743433a 95 }
Mike Fiore 11:d2e31743433a 96 }
Mike Fiore 11:d2e31743433a 97 frequency_band = dot->getFrequencyBand();
Mike Fiore 11:d2e31743433a 98 switch (frequency_band) {
Mike Fiore 11:d2e31743433a 99 case mDot::FB_EU868:
Mike Fiore 11:d2e31743433a 100 // 250kHz channels achieve higher throughput
Mike Fiore 11:d2e31743433a 101 // DR6 : SF7 @ 250kHz
Mike Fiore 11:d2e31743433a 102 // DR0 - DR5 (125kHz channels) available but much slower
Mike Fiore 11:d2e31743433a 103 tx_frequency = 869850000;
Mike Fiore 11:d2e31743433a 104 tx_datarate = mDot::DR6;
Mike Fiore 11:d2e31743433a 105 // the 869850000 frequency is 100% duty cycle if the total power is under 7 dBm - tx power 4 + antenna gain 3 = 7
Mike Fiore 11:d2e31743433a 106 tx_power = 4;
Mike Fiore 11:d2e31743433a 107 break;
Mike Fiore 11:d2e31743433a 108 case mDot::FB_US915:
Mike Fiore 11:d2e31743433a 109 case mDot::FB_AU915:
Mike Fiore 11:d2e31743433a 110 default:
Mike Fiore 11:d2e31743433a 111 // 500kHz channels achieve highest throughput
Mike Fiore 11:d2e31743433a 112 // DR8 : SF12 @ 500kHz
Mike Fiore 11:d2e31743433a 113 // DR9 : SF11 @ 500kHz
Mike Fiore 11:d2e31743433a 114 // DR10 : SF10 @ 500kHz
Mike Fiore 11:d2e31743433a 115 // DR11 : SF9 @ 500kHz
Mike Fiore 11:d2e31743433a 116 // DR12 : SF8 @ 500kHz
Mike Fiore 11:d2e31743433a 117 // DR13 : SF7 @ 500kHz
Mike Fiore 11:d2e31743433a 118 // DR0 - DR3 (125kHz channels) available but much slower
Mike Fiore 11:d2e31743433a 119 tx_frequency = 915500000;
Mike Fiore 11:d2e31743433a 120 tx_datarate = mDot::DR13;
Mike Fiore 11:d2e31743433a 121 // 915 bands have no duty cycle restrictions, set tx power to max
Mike Fiore 11:d2e31743433a 122 tx_power = 20;
Mike Fiore 11:d2e31743433a 123 break;
Mike Fiore 11:d2e31743433a 124 }
Mike Fiore 11:d2e31743433a 125 // in PEER_TO_PEER mode there is no join request/response transaction
Mike Fiore 11:d2e31743433a 126 // as long as both Dots are configured correctly, they should be able to communicate
Mike Fiore 11:d2e31743433a 127 update_peer_to_peer_config(network_address, network_session_key, data_session_key, tx_frequency, tx_datarate, tx_power);
Mike Fiore 11:d2e31743433a 128
Matt Briggs 24:fdf87e4b72e5 129
Matt Briggs 24:fdf87e4b72e5 130 unsigned int wakeMode;
Matt Briggs 26:9411b26a5084 131
Matt Briggs 24:fdf87e4b72e5 132 ///////////////////////////////
Matt Briggs 24:fdf87e4b72e5 133 // Transmitter Configuration //
Matt Briggs 24:fdf87e4b72e5 134 ///////////////////////////////
Matt Briggs 27:6b68ff715ae1 135 #if BRIDGE_TX_BRUTE
Matt Briggs 24:fdf87e4b72e5 136 wakeMode = mDot::INTERRUPT;
Matt Briggs 24:fdf87e4b72e5 137 #endif
Matt Briggs 24:fdf87e4b72e5 138
Matt Briggs 24:fdf87e4b72e5 139 ////////////////////////////
Matt Briggs 24:fdf87e4b72e5 140 // Receiver Configuration //
Matt Briggs 24:fdf87e4b72e5 141 ////////////////////////////
Matt Briggs 27:6b68ff715ae1 142 #if BRIDGE_RX_BRUTE
Matt Briggs 24:fdf87e4b72e5 143 wakeMode = mDot::RTC_ALARM_OR_INTERRUPT;
Matt Briggs 24:fdf87e4b72e5 144 #endif
Matt Briggs 24:fdf87e4b72e5 145
Matt Briggs 24:fdf87e4b72e5 146 // Common Configuration
Matt Briggs 25:fea776239709 147 dot->setWakePin(WAKE); // Use the wake pin as sleep interrupt
Matt Briggs 25:fea776239709 148 dot->setClass("C"); // Set class C
Matt Briggs 25:fea776239709 149 dot->setAck(0); // Disable Ack
Matt Briggs 28:b14b2926e916 150 dot->setTxPower(20); // 20 dBm
Matt Briggs 24:fdf87e4b72e5 151
Mike Fiore 11:d2e31743433a 152 // save changes to configuration
Mike Fiore 11:d2e31743433a 153 logInfo("saving configuration");
Mike Fiore 11:d2e31743433a 154 if (!dot->saveConfig()) {
Mike Fiore 11:d2e31743433a 155 logError("failed to save configuration");
Mike Fiore 11:d2e31743433a 156 }
Mike Fiore 11:d2e31743433a 157
Mike Fiore 11:d2e31743433a 158 // display configuration
Mike Fiore 11:d2e31743433a 159 display_config();
Mike Fiore 11:d2e31743433a 160
Matt Briggs 26:9411b26a5084 161
Matt Briggs 27:6b68ff715ae1 162 uint32_t txTime = 30; // in ms
Matt Briggs 27:6b68ff715ae1 163 unsigned int nTimesToTx = ceil(rxSleepTime / ((float)txTime));
Matt Briggs 27:6b68ff715ae1 164 logInfo("rxSleepTime %f, timeOnAir %lu, nTimesToTx %lu", rxSleepTime, txTime, nTimesToTx);
Matt Briggs 26:9411b26a5084 165
mbriggs_vortex 23:4ed894108882 166 uint16_t seqNum=0;
Matt Briggs 26:9411b26a5084 167 uint32_t cDwnLink = dot->getDownLinkCounter();
Mike Fiore 11:d2e31743433a 168 while (true) {
Matt Briggs 25:fea776239709 169 std::vector<uint8_t> data;
Matt Briggs 25:fea776239709 170 led1=0;
Mike Fiore 11:d2e31743433a 171
Mike Fiore 11:d2e31743433a 172 // join network if not joined
Mike Fiore 11:d2e31743433a 173 if (!dot->getNetworkJoinStatus()) {
Mike Fiore 11:d2e31743433a 174 join_network();
Mike Fiore 11:d2e31743433a 175 }
Mike Fiore 11:d2e31743433a 176
Matt Briggs 27:6b68ff715ae1 177 //////////////////////////////////////////
Matt Briggs 27:6b68ff715ae1 178 // Brute Protocol Transmitter main loop //
Matt Briggs 27:6b68ff715ae1 179 //////////////////////////////////////////
Matt Briggs 27:6b68ff715ae1 180 #if BRIDGE_TX_BRUTE
Matt Briggs 26:9411b26a5084 181 led1=1;
Matt Briggs 25:fea776239709 182 data.push_back((seqNum >> 8) & 0xFF);
Matt Briggs 25:fea776239709 183 data.push_back(seqNum & 0xFF);
Matt Briggs 27:6b68ff715ae1 184 logInfo("Starting TX. Time: %lu, seqNum: %lu", us_ticker_read(), seqNum);
Matt Briggs 26:9411b26a5084 185 for(uint i=0;i<nTimesToTx;++i) {
Matt Briggs 27:6b68ff715ae1 186 dot->send(data);
Matt Briggs 26:9411b26a5084 187 }
mbriggs_vortex 23:4ed894108882 188 seqNum++;
Matt Briggs 25:fea776239709 189 led1=0;
Matt Briggs 27:6b68ff715ae1 190 logInfo("Finished TX. Time: %lu", us_ticker_read());
mbriggs_vortex 23:4ed894108882 191
Matt Briggs 28:b14b2926e916 192 sleep_save_io();
Matt Briggs 28:b14b2926e916 193 sleep_configure_io();
Matt Briggs 24:fdf87e4b72e5 194 dot->sleep(0, wakeMode, false); // Go to sleep until wake button
Matt Briggs 28:b14b2926e916 195 sleep_restore_io();
Matt Briggs 24:fdf87e4b72e5 196 #endif
Matt Briggs 27:6b68ff715ae1 197
Matt Briggs 27:6b68ff715ae1 198 ///////////////////////////////////////
Matt Briggs 27:6b68ff715ae1 199 // Brute Protocol Receiver main loop //
Matt Briggs 27:6b68ff715ae1 200 ///////////////////////////////////////
Matt Briggs 27:6b68ff715ae1 201
Matt Briggs 27:6b68ff715ae1 202 #if BRIDGE_RX_BRUTE
Matt Briggs 27:6b68ff715ae1 203 logInfo("Waiting for new message current DLC: %d, Time %d", cDwnLink, us_ticker_read());
Matt Briggs 27:6b68ff715ae1 204 wait(txTime*2.0/1000.0); // Wait twice the txTime
Matt Briggs 26:9411b26a5084 205 if (cDwnLink < dot->getDownLinkCounter()) {
Matt Briggs 26:9411b26a5084 206 led1 = 1;
Matt Briggs 26:9411b26a5084 207 cDwnLink = dot->getDownLinkCounter();
Matt Briggs 26:9411b26a5084 208 dot->recv(data);
Matt Briggs 26:9411b26a5084 209 std::string dataStr(data.begin(), data.end());
Matt Briggs 26:9411b26a5084 210 logInfo("Got msg num: %d, payload: %s", seqNum, dataStr.c_str());
Matt Briggs 26:9411b26a5084 211 seqNum++;
Matt Briggs 26:9411b26a5084 212 wait(0.5);
Matt Briggs 25:fea776239709 213 }
Matt Briggs 26:9411b26a5084 214 led1=0;
Matt Briggs 27:6b68ff715ae1 215 logInfo("Sleeping. Time %d", us_ticker_read());
Matt Briggs 28:b14b2926e916 216 sleep_save_io();
Matt Briggs 28:b14b2926e916 217 sleep_configure_io();
Matt Briggs 26:9411b26a5084 218 dot->sleep(2, wakeMode, false); // Go to sleep until wake button
Matt Briggs 28:b14b2926e916 219 sleep_restore_io();
Matt Briggs 25:fea776239709 220 #endif
Matt Briggs 27:6b68ff715ae1 221 // // Idea to Setup Ticker
Matt Briggs 27:6b68ff715ae1 222 // RxHandler rxHandler(dot);
Matt Briggs 27:6b68ff715ae1 223 // t.attach(&rxHandler, &RxHandler::listen, 1.5);
Matt Briggs 27:6b68ff715ae1 224 // wait(5.0);
Matt Briggs 27:6b68ff715ae1 225 // while (true) {
Matt Briggs 27:6b68ff715ae1 226 // dot->sleep(0, wakeMode, false); // Go to sleep until wake button
Matt Briggs 27:6b68ff715ae1 227 // //sleep();
Matt Briggs 27:6b68ff715ae1 228 // }
Mike Fiore 11:d2e31743433a 229 }
Mike Fiore 11:d2e31743433a 230
Mike Fiore 11:d2e31743433a 231 return 0;
Mike Fiore 11:d2e31743433a 232 }
Mike Fiore 11:d2e31743433a 233
Matt Briggs 27:6b68ff715ae1 234