This node was designed to map the range of Multitech Conduit gateways, It uses a Multitech mDot and Adafruit GPS breakout.

Dependencies:   MBed_Adafruit-GPS-Library libmDot-mbed5

Fork of mDot_LoRa_Connect_ABPA_DHT22_sleep by Brendan Kelly

Committer:
kellybs1
Date:
Mon Aug 21 05:01:01 2017 +0000
Revision:
10:02615da7a9fe
Working version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kellybs1 10:02615da7a9fe 1 #include "dot_util.h"
kellybs1 10:02615da7a9fe 2 #if defined(TARGET_XDOT_L151CC)
kellybs1 10:02615da7a9fe 3 #include "xdot_low_power.h"
kellybs1 10:02615da7a9fe 4 #endif
kellybs1 10:02615da7a9fe 5
kellybs1 10:02615da7a9fe 6 #if defined(TARGET_MTS_MDOT_F411RE)
kellybs1 10:02615da7a9fe 7 uint32_t portA[6];
kellybs1 10:02615da7a9fe 8 uint32_t portB[6];
kellybs1 10:02615da7a9fe 9 uint32_t portC[6];
kellybs1 10:02615da7a9fe 10 uint32_t portD[6];
kellybs1 10:02615da7a9fe 11 uint32_t portH[6];
kellybs1 10:02615da7a9fe 12 #endif
kellybs1 10:02615da7a9fe 13
kellybs1 10:02615da7a9fe 14
kellybs1 10:02615da7a9fe 15 void display_config() {
kellybs1 10:02615da7a9fe 16 // display configuration and library version information
kellybs1 10:02615da7a9fe 17 logInfo("=====================");
kellybs1 10:02615da7a9fe 18 logInfo("general configuration");
kellybs1 10:02615da7a9fe 19 logInfo("=====================");
kellybs1 10:02615da7a9fe 20 logInfo("version ------------------ %s", dot->getId().c_str());
kellybs1 10:02615da7a9fe 21 logInfo("device ID/EUI ------------ %s", mts::Text::bin2hexString(dot->getDeviceId()).c_str());
kellybs1 10:02615da7a9fe 22 logInfo("default channel plan ----- %s", mDot::FrequencyBandStr(dot->getDefaultFrequencyBand()).c_str());
kellybs1 10:02615da7a9fe 23 logInfo("current channel plan ----- %s", mDot::FrequencyBandStr(dot->getFrequencyBand()).c_str());
kellybs1 10:02615da7a9fe 24 if (lora::ChannelPlan::IsPlanFixed(dot->getFrequencyBand())) {
kellybs1 10:02615da7a9fe 25 logInfo("frequency sub band ------- %u", dot->getFrequencySubBand());
kellybs1 10:02615da7a9fe 26 }
kellybs1 10:02615da7a9fe 27 logInfo("public network ----------- %s", dot->getPublicNetwork() ? "on" : "off");
kellybs1 10:02615da7a9fe 28 logInfo("=========================");
kellybs1 10:02615da7a9fe 29 logInfo("credentials configuration");
kellybs1 10:02615da7a9fe 30 logInfo("=========================");
kellybs1 10:02615da7a9fe 31 logInfo("device class ------------- %s", dot->getClass().c_str());
kellybs1 10:02615da7a9fe 32 logInfo("network join mode -------- %s", mDot::JoinModeStr(dot->getJoinMode()).c_str());
kellybs1 10:02615da7a9fe 33 if (dot->getJoinMode() == mDot::MANUAL || dot->getJoinMode() == mDot::PEER_TO_PEER) {
kellybs1 10:02615da7a9fe 34 logInfo("network address ---------- %s", mts::Text::bin2hexString(dot->getNetworkAddress()).c_str());
kellybs1 10:02615da7a9fe 35 logInfo("network session key------- %s", mts::Text::bin2hexString(dot->getNetworkSessionKey()).c_str());
kellybs1 10:02615da7a9fe 36 logInfo("data session key---------- %s", mts::Text::bin2hexString(dot->getDataSessionKey()).c_str());
kellybs1 10:02615da7a9fe 37 } else {
kellybs1 10:02615da7a9fe 38 logInfo("network name ------------- %s", dot->getNetworkName().c_str());
kellybs1 10:02615da7a9fe 39 logInfo("network phrase ----------- %s", dot->getNetworkPassphrase().c_str());
kellybs1 10:02615da7a9fe 40 logInfo("network EUI -------------- %s", mts::Text::bin2hexString(dot->getNetworkId()).c_str());
kellybs1 10:02615da7a9fe 41 logInfo("network KEY -------------- %s", mts::Text::bin2hexString(dot->getNetworkKey()).c_str());
kellybs1 10:02615da7a9fe 42 }
kellybs1 10:02615da7a9fe 43 logInfo("========================");
kellybs1 10:02615da7a9fe 44 logInfo("communication parameters");
kellybs1 10:02615da7a9fe 45 logInfo("========================");
kellybs1 10:02615da7a9fe 46 if (dot->getJoinMode() == mDot::PEER_TO_PEER) {
kellybs1 10:02615da7a9fe 47 logInfo("TX frequency ------------- %lu", dot->getTxFrequency());
kellybs1 10:02615da7a9fe 48 } else {
kellybs1 10:02615da7a9fe 49 logInfo("acks --------------------- %s, %u attempts", dot->getAck() > 0 ? "on" : "off", dot->getAck());
kellybs1 10:02615da7a9fe 50 }
kellybs1 10:02615da7a9fe 51 logInfo("TX datarate -------------- %s", mDot::DataRateStr(dot->getTxDataRate()).c_str());
kellybs1 10:02615da7a9fe 52 logInfo("TX power ----------------- %lu dBm", dot->getTxPower());
kellybs1 10:02615da7a9fe 53 logInfo("antenna gain ------------- %u dBm", dot->getAntennaGain());
kellybs1 10:02615da7a9fe 54 logInfo("LBT ---------------------- %s", dot->getLbtTimeUs() ? "on" : "off");
kellybs1 10:02615da7a9fe 55 if (dot->getLbtTimeUs()) {
kellybs1 10:02615da7a9fe 56 logInfo("LBT time ----------------- %lu us", dot->getLbtTimeUs());
kellybs1 10:02615da7a9fe 57 logInfo("LBT threshold ------------ %d dBm", dot->getLbtThreshold());
kellybs1 10:02615da7a9fe 58 }
kellybs1 10:02615da7a9fe 59 }
kellybs1 10:02615da7a9fe 60
kellybs1 10:02615da7a9fe 61 void update_ota_config_name_phrase(std::string network_name, std::string network_passphrase, uint8_t frequency_sub_band, bool public_network, uint8_t ack) {
kellybs1 10:02615da7a9fe 62 std::string current_network_name = dot->getNetworkName();
kellybs1 10:02615da7a9fe 63 std::string current_network_passphrase = dot->getNetworkPassphrase();
kellybs1 10:02615da7a9fe 64 uint8_t current_frequency_sub_band = dot->getFrequencySubBand();
kellybs1 10:02615da7a9fe 65 bool current_public_network = dot->getPublicNetwork();
kellybs1 10:02615da7a9fe 66 uint8_t current_ack = dot->getAck();
kellybs1 10:02615da7a9fe 67
kellybs1 10:02615da7a9fe 68 if (current_network_name != network_name) {
kellybs1 10:02615da7a9fe 69 logInfo("changing network name from \"%s\" to \"%s\"", current_network_name.c_str(), network_name.c_str());
kellybs1 10:02615da7a9fe 70 if (dot->setNetworkName(network_name) != mDot::MDOT_OK) {
kellybs1 10:02615da7a9fe 71 logError("failed to set network name to \"%s\"", network_name.c_str());
kellybs1 10:02615da7a9fe 72 }
kellybs1 10:02615da7a9fe 73 }
kellybs1 10:02615da7a9fe 74
kellybs1 10:02615da7a9fe 75 if (current_network_passphrase != network_passphrase) {
kellybs1 10:02615da7a9fe 76 logInfo("changing network passphrase from \"%s\" to \"%s\"", current_network_passphrase.c_str(), network_passphrase.c_str());
kellybs1 10:02615da7a9fe 77 if (dot->setNetworkPassphrase(network_passphrase) != mDot::MDOT_OK) {
kellybs1 10:02615da7a9fe 78 logError("failed to set network passphrase to \"%s\"", network_passphrase.c_str());
kellybs1 10:02615da7a9fe 79 }
kellybs1 10:02615da7a9fe 80 }
kellybs1 10:02615da7a9fe 81
kellybs1 10:02615da7a9fe 82 if (lora::ChannelPlan::IsPlanFixed(dot->getFrequencyBand())) {
kellybs1 10:02615da7a9fe 83 if (current_frequency_sub_band != frequency_sub_band) {
kellybs1 10:02615da7a9fe 84 logInfo("changing frequency sub band from %u to %u", current_frequency_sub_band, frequency_sub_band);
kellybs1 10:02615da7a9fe 85 if (dot->setFrequencySubBand(frequency_sub_band) != mDot::MDOT_OK) {
kellybs1 10:02615da7a9fe 86 logError("failed to set frequency sub band to %u", frequency_sub_band);
kellybs1 10:02615da7a9fe 87 }
kellybs1 10:02615da7a9fe 88 }
kellybs1 10:02615da7a9fe 89 }
kellybs1 10:02615da7a9fe 90
kellybs1 10:02615da7a9fe 91 if (current_public_network != public_network) {
kellybs1 10:02615da7a9fe 92 logInfo("changing public network from %s to %s", current_public_network ? "on" : "off", public_network ? "on" : "off");
kellybs1 10:02615da7a9fe 93 if (dot->setPublicNetwork(public_network) != mDot::MDOT_OK) {
kellybs1 10:02615da7a9fe 94 logError("failed to set public network to %s", public_network ? "on" : "off");
kellybs1 10:02615da7a9fe 95 }
kellybs1 10:02615da7a9fe 96 }
kellybs1 10:02615da7a9fe 97
kellybs1 10:02615da7a9fe 98 if (current_ack != ack) {
kellybs1 10:02615da7a9fe 99 logInfo("changing acks from %u to %u", current_ack, ack);
kellybs1 10:02615da7a9fe 100 if (dot->setAck(ack) != mDot::MDOT_OK) {
kellybs1 10:02615da7a9fe 101 logError("failed to set acks to %u", ack);
kellybs1 10:02615da7a9fe 102 }
kellybs1 10:02615da7a9fe 103 }
kellybs1 10:02615da7a9fe 104 }
kellybs1 10:02615da7a9fe 105
kellybs1 10:02615da7a9fe 106 void update_ota_config_id_key(uint8_t *network_id, uint8_t *network_key, uint8_t frequency_sub_band, bool public_network, uint8_t ack) {
kellybs1 10:02615da7a9fe 107 std::vector<uint8_t> current_network_id = dot->getNetworkId();
kellybs1 10:02615da7a9fe 108 std::vector<uint8_t> current_network_key = dot->getNetworkKey();
kellybs1 10:02615da7a9fe 109 uint8_t current_frequency_sub_band = dot->getFrequencySubBand();
kellybs1 10:02615da7a9fe 110 bool current_public_network = dot->getPublicNetwork();
kellybs1 10:02615da7a9fe 111 uint8_t current_ack = dot->getAck();
kellybs1 10:02615da7a9fe 112
kellybs1 10:02615da7a9fe 113 std::vector<uint8_t> network_id_vector(network_id, network_id + 8);
kellybs1 10:02615da7a9fe 114 std::vector<uint8_t> network_key_vector(network_key, network_key + 16);
kellybs1 10:02615da7a9fe 115
kellybs1 10:02615da7a9fe 116 if (current_network_id != network_id_vector) {
kellybs1 10:02615da7a9fe 117 logInfo("changing network ID from \"%s\" to \"%s\"", mts::Text::bin2hexString(current_network_id).c_str(), mts::Text::bin2hexString(network_id_vector).c_str());
kellybs1 10:02615da7a9fe 118 if (dot->setNetworkId(network_id_vector) != mDot::MDOT_OK) {
kellybs1 10:02615da7a9fe 119 logError("failed to set network ID to \"%s\"", mts::Text::bin2hexString(network_id_vector).c_str());
kellybs1 10:02615da7a9fe 120 }
kellybs1 10:02615da7a9fe 121 }
kellybs1 10:02615da7a9fe 122
kellybs1 10:02615da7a9fe 123 if (current_network_key != network_key_vector) {
kellybs1 10:02615da7a9fe 124 logInfo("changing network KEY from \"%s\" to \"%s\"", mts::Text::bin2hexString(current_network_key).c_str(), mts::Text::bin2hexString(network_key_vector).c_str());
kellybs1 10:02615da7a9fe 125 if (dot->setNetworkKey(network_key_vector) != mDot::MDOT_OK) {
kellybs1 10:02615da7a9fe 126 logError("failed to set network KEY to \"%s\"", mts::Text::bin2hexString(network_key_vector).c_str());
kellybs1 10:02615da7a9fe 127 }
kellybs1 10:02615da7a9fe 128 }
kellybs1 10:02615da7a9fe 129
kellybs1 10:02615da7a9fe 130 if (lora::ChannelPlan::IsPlanFixed(dot->getFrequencyBand())) {
kellybs1 10:02615da7a9fe 131 if (current_frequency_sub_band != frequency_sub_band) {
kellybs1 10:02615da7a9fe 132 logInfo("changing frequency sub band from %u to %u", current_frequency_sub_band, frequency_sub_band);
kellybs1 10:02615da7a9fe 133 if (dot->setFrequencySubBand(frequency_sub_band) != mDot::MDOT_OK) {
kellybs1 10:02615da7a9fe 134 logError("failed to set frequency sub band to %u", frequency_sub_band);
kellybs1 10:02615da7a9fe 135 }
kellybs1 10:02615da7a9fe 136 }
kellybs1 10:02615da7a9fe 137 }
kellybs1 10:02615da7a9fe 138
kellybs1 10:02615da7a9fe 139 if (current_public_network != public_network) {
kellybs1 10:02615da7a9fe 140 logInfo("changing public network from %s to %s", current_public_network ? "on" : "off", public_network ? "on" : "off");
kellybs1 10:02615da7a9fe 141 if (dot->setPublicNetwork(public_network) != mDot::MDOT_OK) {
kellybs1 10:02615da7a9fe 142 logError("failed to set public network to %s", public_network ? "on" : "off");
kellybs1 10:02615da7a9fe 143 }
kellybs1 10:02615da7a9fe 144 }
kellybs1 10:02615da7a9fe 145
kellybs1 10:02615da7a9fe 146 if (current_ack != ack) {
kellybs1 10:02615da7a9fe 147 logInfo("changing acks from %u to %u", current_ack, ack);
kellybs1 10:02615da7a9fe 148 if (dot->setAck(ack) != mDot::MDOT_OK) {
kellybs1 10:02615da7a9fe 149 logError("failed to set acks to %u", ack);
kellybs1 10:02615da7a9fe 150 }
kellybs1 10:02615da7a9fe 151 }
kellybs1 10:02615da7a9fe 152 }
kellybs1 10:02615da7a9fe 153
kellybs1 10:02615da7a9fe 154 void update_manual_config(uint8_t *network_address, uint8_t *network_session_key, uint8_t *data_session_key, uint8_t frequency_sub_band, bool public_network, uint8_t ack) {
kellybs1 10:02615da7a9fe 155 std::vector<uint8_t> current_network_address = dot->getNetworkAddress();
kellybs1 10:02615da7a9fe 156 std::vector<uint8_t> current_network_session_key = dot->getNetworkSessionKey();
kellybs1 10:02615da7a9fe 157 std::vector<uint8_t> current_data_session_key = dot->getDataSessionKey();
kellybs1 10:02615da7a9fe 158 uint8_t current_frequency_sub_band = dot->getFrequencySubBand();
kellybs1 10:02615da7a9fe 159 bool current_public_network = dot->getPublicNetwork();
kellybs1 10:02615da7a9fe 160 uint8_t current_ack = dot->getAck();
kellybs1 10:02615da7a9fe 161
kellybs1 10:02615da7a9fe 162 std::vector<uint8_t> network_address_vector(network_address, network_address + 4);
kellybs1 10:02615da7a9fe 163 std::vector<uint8_t> network_session_key_vector(network_session_key, network_session_key + 16);
kellybs1 10:02615da7a9fe 164 std::vector<uint8_t> data_session_key_vector(data_session_key, data_session_key + 16);
kellybs1 10:02615da7a9fe 165
kellybs1 10:02615da7a9fe 166 if (current_network_address != network_address_vector) {
kellybs1 10:02615da7a9fe 167 logInfo("changing network address from \"%s\" to \"%s\"", mts::Text::bin2hexString(current_network_address).c_str(), mts::Text::bin2hexString(network_address_vector).c_str());
kellybs1 10:02615da7a9fe 168 if (dot->setNetworkAddress(network_address_vector) != mDot::MDOT_OK) {
kellybs1 10:02615da7a9fe 169 logError("failed to set network address to \"%s\"", mts::Text::bin2hexString(network_address_vector).c_str());
kellybs1 10:02615da7a9fe 170 }
kellybs1 10:02615da7a9fe 171 }
kellybs1 10:02615da7a9fe 172
kellybs1 10:02615da7a9fe 173 if (current_network_session_key != network_session_key_vector) {
kellybs1 10:02615da7a9fe 174 logInfo("changing network session key from \"%s\" to \"%s\"", mts::Text::bin2hexString(current_network_session_key).c_str(), mts::Text::bin2hexString(network_session_key_vector).c_str());
kellybs1 10:02615da7a9fe 175 if (dot->setNetworkSessionKey(network_session_key_vector) != mDot::MDOT_OK) {
kellybs1 10:02615da7a9fe 176 logError("failed to set network session key to \"%s\"", mts::Text::bin2hexString(network_session_key_vector).c_str());
kellybs1 10:02615da7a9fe 177 }
kellybs1 10:02615da7a9fe 178 }
kellybs1 10:02615da7a9fe 179
kellybs1 10:02615da7a9fe 180 if (current_data_session_key != data_session_key_vector) {
kellybs1 10:02615da7a9fe 181 logInfo("changing data session key from \"%s\" to \"%s\"", mts::Text::bin2hexString(current_data_session_key).c_str(), mts::Text::bin2hexString(data_session_key_vector).c_str());
kellybs1 10:02615da7a9fe 182 if (dot->setDataSessionKey(data_session_key_vector) != mDot::MDOT_OK) {
kellybs1 10:02615da7a9fe 183 logError("failed to set data session key to \"%s\"", mts::Text::bin2hexString(data_session_key_vector).c_str());
kellybs1 10:02615da7a9fe 184 }
kellybs1 10:02615da7a9fe 185 }
kellybs1 10:02615da7a9fe 186
kellybs1 10:02615da7a9fe 187 if (current_frequency_sub_band != frequency_sub_band) {
kellybs1 10:02615da7a9fe 188 logInfo("changing frequency sub band from %u to %u", current_frequency_sub_band, frequency_sub_band);
kellybs1 10:02615da7a9fe 189 if (dot->setFrequencySubBand(frequency_sub_band) != mDot::MDOT_OK) {
kellybs1 10:02615da7a9fe 190 logError("failed to set frequency sub band to %u", frequency_sub_band);
kellybs1 10:02615da7a9fe 191 }
kellybs1 10:02615da7a9fe 192 }
kellybs1 10:02615da7a9fe 193
kellybs1 10:02615da7a9fe 194 if (current_public_network != public_network) {
kellybs1 10:02615da7a9fe 195 logInfo("changing public network from %s to %s", current_public_network ? "on" : "off", public_network ? "on" : "off");
kellybs1 10:02615da7a9fe 196 if (dot->setPublicNetwork(public_network) != mDot::MDOT_OK) {
kellybs1 10:02615da7a9fe 197 logError("failed to set public network to %s", public_network ? "on" : "off");
kellybs1 10:02615da7a9fe 198 }
kellybs1 10:02615da7a9fe 199 }
kellybs1 10:02615da7a9fe 200
kellybs1 10:02615da7a9fe 201 if (current_ack != ack) {
kellybs1 10:02615da7a9fe 202 logInfo("changing acks from %u to %u", current_ack, ack);
kellybs1 10:02615da7a9fe 203 if (dot->setAck(ack) != mDot::MDOT_OK) {
kellybs1 10:02615da7a9fe 204 logError("failed to set acks to %u", ack);
kellybs1 10:02615da7a9fe 205 }
kellybs1 10:02615da7a9fe 206 }
kellybs1 10:02615da7a9fe 207 }
kellybs1 10:02615da7a9fe 208
kellybs1 10:02615da7a9fe 209 void update_peer_to_peer_config(uint8_t *network_address, uint8_t *network_session_key, uint8_t *data_session_key, uint32_t tx_frequency, uint8_t tx_datarate, uint8_t tx_power) {
kellybs1 10:02615da7a9fe 210 std::vector<uint8_t> current_network_address = dot->getNetworkAddress();
kellybs1 10:02615da7a9fe 211 std::vector<uint8_t> current_network_session_key = dot->getNetworkSessionKey();
kellybs1 10:02615da7a9fe 212 std::vector<uint8_t> current_data_session_key = dot->getDataSessionKey();
kellybs1 10:02615da7a9fe 213 uint32_t current_tx_frequency = dot->getTxFrequency();
kellybs1 10:02615da7a9fe 214 uint8_t current_tx_datarate = dot->getTxDataRate();
kellybs1 10:02615da7a9fe 215 uint8_t current_tx_power = dot->getTxPower();
kellybs1 10:02615da7a9fe 216
kellybs1 10:02615da7a9fe 217 std::vector<uint8_t> network_address_vector(network_address, network_address + 4);
kellybs1 10:02615da7a9fe 218 std::vector<uint8_t> network_session_key_vector(network_session_key, network_session_key + 16);
kellybs1 10:02615da7a9fe 219 std::vector<uint8_t> data_session_key_vector(data_session_key, data_session_key + 16);
kellybs1 10:02615da7a9fe 220
kellybs1 10:02615da7a9fe 221 if (current_network_address != network_address_vector) {
kellybs1 10:02615da7a9fe 222 logInfo("changing network address from \"%s\" to \"%s\"", mts::Text::bin2hexString(current_network_address).c_str(), mts::Text::bin2hexString(network_address_vector).c_str());
kellybs1 10:02615da7a9fe 223 if (dot->setNetworkAddress(network_address_vector) != mDot::MDOT_OK) {
kellybs1 10:02615da7a9fe 224 logError("failed to set network address to \"%s\"", mts::Text::bin2hexString(network_address_vector).c_str());
kellybs1 10:02615da7a9fe 225 }
kellybs1 10:02615da7a9fe 226 }
kellybs1 10:02615da7a9fe 227
kellybs1 10:02615da7a9fe 228 if (current_network_session_key != network_session_key_vector) {
kellybs1 10:02615da7a9fe 229 logInfo("changing network session key from \"%s\" to \"%s\"", mts::Text::bin2hexString(current_network_session_key).c_str(), mts::Text::bin2hexString(network_session_key_vector).c_str());
kellybs1 10:02615da7a9fe 230 if (dot->setNetworkSessionKey(network_session_key_vector) != mDot::MDOT_OK) {
kellybs1 10:02615da7a9fe 231 logError("failed to set network session key to \"%s\"", mts::Text::bin2hexString(network_session_key_vector).c_str());
kellybs1 10:02615da7a9fe 232 }
kellybs1 10:02615da7a9fe 233 }
kellybs1 10:02615da7a9fe 234
kellybs1 10:02615da7a9fe 235 if (current_data_session_key != data_session_key_vector) {
kellybs1 10:02615da7a9fe 236 logInfo("changing data session key from \"%s\" to \"%s\"", mts::Text::bin2hexString(current_data_session_key).c_str(), mts::Text::bin2hexString(data_session_key_vector).c_str());
kellybs1 10:02615da7a9fe 237 if (dot->setDataSessionKey(data_session_key_vector) != mDot::MDOT_OK) {
kellybs1 10:02615da7a9fe 238 logError("failed to set data session key to \"%s\"", mts::Text::bin2hexString(data_session_key_vector).c_str());
kellybs1 10:02615da7a9fe 239 }
kellybs1 10:02615da7a9fe 240 }
kellybs1 10:02615da7a9fe 241
kellybs1 10:02615da7a9fe 242 if (current_tx_frequency != tx_frequency) {
kellybs1 10:02615da7a9fe 243 logInfo("changing TX frequency from %lu to %lu", current_tx_frequency, tx_frequency);
kellybs1 10:02615da7a9fe 244 if (dot->setTxFrequency(tx_frequency) != mDot::MDOT_OK) {
kellybs1 10:02615da7a9fe 245 logError("failed to set TX frequency to %lu", tx_frequency);
kellybs1 10:02615da7a9fe 246 }
kellybs1 10:02615da7a9fe 247 }
kellybs1 10:02615da7a9fe 248
kellybs1 10:02615da7a9fe 249 if (current_tx_datarate != tx_datarate) {
kellybs1 10:02615da7a9fe 250 logInfo("changing TX datarate from %u to %u", current_tx_datarate, tx_datarate);
kellybs1 10:02615da7a9fe 251 if (dot->setTxDataRate(tx_datarate) != mDot::MDOT_OK) {
kellybs1 10:02615da7a9fe 252 logError("failed to set TX datarate to %u", tx_datarate);
kellybs1 10:02615da7a9fe 253 }
kellybs1 10:02615da7a9fe 254 }
kellybs1 10:02615da7a9fe 255
kellybs1 10:02615da7a9fe 256 if (current_tx_power != tx_power) {
kellybs1 10:02615da7a9fe 257 logInfo("changing TX power from %u to %u", current_tx_power, tx_power);
kellybs1 10:02615da7a9fe 258 if (dot->setTxPower(tx_power) != mDot::MDOT_OK) {
kellybs1 10:02615da7a9fe 259 logError("failed to set TX power to %u", tx_power);
kellybs1 10:02615da7a9fe 260 }
kellybs1 10:02615da7a9fe 261 }
kellybs1 10:02615da7a9fe 262 }
kellybs1 10:02615da7a9fe 263
kellybs1 10:02615da7a9fe 264 void update_network_link_check_config(uint8_t link_check_count, uint8_t link_check_threshold) {
kellybs1 10:02615da7a9fe 265 uint8_t current_link_check_count = dot->getLinkCheckCount();
kellybs1 10:02615da7a9fe 266 uint8_t current_link_check_threshold = dot->getLinkCheckThreshold();
kellybs1 10:02615da7a9fe 267
kellybs1 10:02615da7a9fe 268 if (current_link_check_count != link_check_count) {
kellybs1 10:02615da7a9fe 269 logInfo("changing link check count from %u to %u", current_link_check_count, link_check_count);
kellybs1 10:02615da7a9fe 270 if (dot->setLinkCheckCount(link_check_count) != mDot::MDOT_OK) {
kellybs1 10:02615da7a9fe 271 logError("failed to set link check count to %u", link_check_count);
kellybs1 10:02615da7a9fe 272 }
kellybs1 10:02615da7a9fe 273 }
kellybs1 10:02615da7a9fe 274
kellybs1 10:02615da7a9fe 275 if (current_link_check_threshold != link_check_threshold) {
kellybs1 10:02615da7a9fe 276 logInfo("changing link check threshold from %u to %u", current_link_check_threshold, link_check_threshold);
kellybs1 10:02615da7a9fe 277 if (dot->setLinkCheckThreshold(link_check_threshold) != mDot::MDOT_OK) {
kellybs1 10:02615da7a9fe 278 logError("failed to set link check threshold to %u", link_check_threshold);
kellybs1 10:02615da7a9fe 279 }
kellybs1 10:02615da7a9fe 280 }
kellybs1 10:02615da7a9fe 281 }
kellybs1 10:02615da7a9fe 282
kellybs1 10:02615da7a9fe 283 void join_network() {
kellybs1 10:02615da7a9fe 284 int32_t j_attempts = 0;
kellybs1 10:02615da7a9fe 285 int32_t ret = mDot::MDOT_ERROR;
kellybs1 10:02615da7a9fe 286
kellybs1 10:02615da7a9fe 287 // attempt to join the network
kellybs1 10:02615da7a9fe 288 while (ret != mDot::MDOT_OK) {
kellybs1 10:02615da7a9fe 289 logInfo("attempt %d to join network", ++j_attempts);
kellybs1 10:02615da7a9fe 290 ret = dot->joinNetwork();
kellybs1 10:02615da7a9fe 291 if (ret != mDot::MDOT_OK) {
kellybs1 10:02615da7a9fe 292 logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
kellybs1 10:02615da7a9fe 293 // in some frequency bands we need to wait until another channel is available before transmitting again
kellybs1 10:02615da7a9fe 294 uint32_t delay_s = (dot->getNextTxMs() / 1000) + 1;
kellybs1 10:02615da7a9fe 295 if (delay_s < 2) {
kellybs1 10:02615da7a9fe 296 logInfo("waiting %lu s until next free channel", delay_s);
kellybs1 10:02615da7a9fe 297 wait(delay_s);
kellybs1 10:02615da7a9fe 298 } else {
kellybs1 10:02615da7a9fe 299 logInfo("sleeping %lu s until next free channel", delay_s);
kellybs1 10:02615da7a9fe 300 dot->sleep(delay_s, mDot::RTC_ALARM, false);
kellybs1 10:02615da7a9fe 301 }
kellybs1 10:02615da7a9fe 302 }
kellybs1 10:02615da7a9fe 303 }
kellybs1 10:02615da7a9fe 304 }
kellybs1 10:02615da7a9fe 305
kellybs1 10:02615da7a9fe 306 void sleep_wake_rtc_only(bool deepsleep) {
kellybs1 10:02615da7a9fe 307 // in some frequency bands we need to wait until another channel is available before transmitting again
kellybs1 10:02615da7a9fe 308 // wait at least 10s between transmissions
kellybs1 10:02615da7a9fe 309 uint32_t delay_s = dot->getNextTxMs() / 1000;
kellybs1 10:02615da7a9fe 310 if (delay_s < 10) {
kellybs1 10:02615da7a9fe 311 delay_s = 10;
kellybs1 10:02615da7a9fe 312 }
kellybs1 10:02615da7a9fe 313
kellybs1 10:02615da7a9fe 314 logInfo("%ssleeping %lus", deepsleep ? "deep" : "", delay_s);
kellybs1 10:02615da7a9fe 315 logInfo("application will %s after waking up", deepsleep ? "execute from beginning" : "resume");
kellybs1 10:02615da7a9fe 316
kellybs1 10:02615da7a9fe 317 // lowest current consumption in sleep mode can only be achieved by configuring IOs as analog inputs with no pull resistors
kellybs1 10:02615da7a9fe 318 // the library handles all internal IOs automatically, but the external IOs are the application's responsibility
kellybs1 10:02615da7a9fe 319 // certain IOs may require internal pullup or pulldown resistors because leaving them floating would cause extra current consumption
kellybs1 10:02615da7a9fe 320 // for xDot: UART_*, I2C_*, SPI_*, GPIO*, WAKE
kellybs1 10:02615da7a9fe 321 // for mDot: XBEE_*, USBTX, USBRX, PB_0, PB_1
kellybs1 10:02615da7a9fe 322 // steps are:
kellybs1 10:02615da7a9fe 323 // * save IO configuration
kellybs1 10:02615da7a9fe 324 // * configure IOs to reduce current consumption
kellybs1 10:02615da7a9fe 325 // * sleep
kellybs1 10:02615da7a9fe 326 // * restore IO configuration
kellybs1 10:02615da7a9fe 327 if (! deepsleep) {
kellybs1 10:02615da7a9fe 328 // save the GPIO state.
kellybs1 10:02615da7a9fe 329 sleep_save_io();
kellybs1 10:02615da7a9fe 330
kellybs1 10:02615da7a9fe 331 // configure GPIOs for lowest current
kellybs1 10:02615da7a9fe 332 sleep_configure_io();
kellybs1 10:02615da7a9fe 333 }
kellybs1 10:02615da7a9fe 334
kellybs1 10:02615da7a9fe 335 // go to sleep/deepsleep for delay_s seconds and wake using the RTC alarm
kellybs1 10:02615da7a9fe 336 dot->sleep(delay_s, mDot::RTC_ALARM, deepsleep);
kellybs1 10:02615da7a9fe 337
kellybs1 10:02615da7a9fe 338 if (! deepsleep) {
kellybs1 10:02615da7a9fe 339 // restore the GPIO state.
kellybs1 10:02615da7a9fe 340 sleep_restore_io();
kellybs1 10:02615da7a9fe 341 }
kellybs1 10:02615da7a9fe 342 }
kellybs1 10:02615da7a9fe 343
kellybs1 10:02615da7a9fe 344 void sleep_wake_interrupt_only(bool deepsleep) {
kellybs1 10:02615da7a9fe 345 #if defined (TARGET_XDOT_L151CC)
kellybs1 10:02615da7a9fe 346 if (deepsleep) {
kellybs1 10:02615da7a9fe 347 // for xDot, WAKE pin (connected to S2 on xDot-DK) is the only pin that can wake the processor from deepsleep
kellybs1 10:02615da7a9fe 348 // it is automatically configured when INTERRUPT or RTC_ALARM_OR_INTERRUPT is the wakeup source and deepsleep is true in the mDot::sleep call
kellybs1 10:02615da7a9fe 349 } else {
kellybs1 10:02615da7a9fe 350 // configure WAKE pin (connected to S2 on xDot-DK) as the pin that will wake the xDot from low power modes
kellybs1 10:02615da7a9fe 351 // other pins can be confgured instead: GPIO0-3 or UART_RX
kellybs1 10:02615da7a9fe 352 dot->setWakePin(WAKE);
kellybs1 10:02615da7a9fe 353 }
kellybs1 10:02615da7a9fe 354
kellybs1 10:02615da7a9fe 355 logInfo("%ssleeping until interrupt on %s pin", deepsleep ? "deep" : "", deepsleep ? "WAKE" : mDot::pinName2Str(dot->getWakePin()).c_str());
kellybs1 10:02615da7a9fe 356 #else
kellybs1 10:02615da7a9fe 357
kellybs1 10:02615da7a9fe 358 if (deepsleep) {
kellybs1 10:02615da7a9fe 359 // for mDot, XBEE_DIO7 pin is the only pin that can wake the processor from deepsleep
kellybs1 10:02615da7a9fe 360 // it is automatically configured when INTERRUPT or RTC_ALARM_OR_INTERRUPT is the wakeup source and deepsleep is true in the mDot::sleep call
kellybs1 10:02615da7a9fe 361 } else {
kellybs1 10:02615da7a9fe 362 // configure XBEE_DIO7 pin as the pin that will wake the mDot from low power modes
kellybs1 10:02615da7a9fe 363 // other pins can be confgured instead: XBEE_DIO2-6, XBEE_DI8, XBEE_DIN
kellybs1 10:02615da7a9fe 364 dot->setWakePin(XBEE_DIO7);
kellybs1 10:02615da7a9fe 365 }
kellybs1 10:02615da7a9fe 366
kellybs1 10:02615da7a9fe 367 logInfo("%ssleeping until interrupt on %s pin", deepsleep ? "deep" : "", deepsleep ? "DIO7" : mDot::pinName2Str(dot->getWakePin()).c_str());
kellybs1 10:02615da7a9fe 368 #endif
kellybs1 10:02615da7a9fe 369
kellybs1 10:02615da7a9fe 370 logInfo("application will %s after waking up", deepsleep ? "execute from beginning" : "resume");
kellybs1 10:02615da7a9fe 371
kellybs1 10:02615da7a9fe 372 // lowest current consumption in sleep mode can only be achieved by configuring IOs as analog inputs with no pull resistors
kellybs1 10:02615da7a9fe 373 // the library handles all internal IOs automatically, but the external IOs are the application's responsibility
kellybs1 10:02615da7a9fe 374 // certain IOs may require internal pullup or pulldown resistors because leaving them floating would cause extra current consumption
kellybs1 10:02615da7a9fe 375 // for xDot: UART_*, I2C_*, SPI_*, GPIO*, WAKE
kellybs1 10:02615da7a9fe 376 // for mDot: XBEE_*, USBTX, USBRX, PB_0, PB_1
kellybs1 10:02615da7a9fe 377 // steps are:
kellybs1 10:02615da7a9fe 378 // * save IO configuration
kellybs1 10:02615da7a9fe 379 // * configure IOs to reduce current consumption
kellybs1 10:02615da7a9fe 380 // * sleep
kellybs1 10:02615da7a9fe 381 // * restore IO configuration
kellybs1 10:02615da7a9fe 382 if (! deepsleep) {
kellybs1 10:02615da7a9fe 383 // save the GPIO state.
kellybs1 10:02615da7a9fe 384 sleep_save_io();
kellybs1 10:02615da7a9fe 385
kellybs1 10:02615da7a9fe 386 // configure GPIOs for lowest current
kellybs1 10:02615da7a9fe 387 sleep_configure_io();
kellybs1 10:02615da7a9fe 388 }
kellybs1 10:02615da7a9fe 389
kellybs1 10:02615da7a9fe 390 // go to sleep/deepsleep and wake on rising edge of configured wake pin (only the WAKE pin in deepsleep)
kellybs1 10:02615da7a9fe 391 // since we're not waking on the RTC alarm, the interval is ignored
kellybs1 10:02615da7a9fe 392 dot->sleep(0, mDot::INTERRUPT, deepsleep);
kellybs1 10:02615da7a9fe 393
kellybs1 10:02615da7a9fe 394 if (! deepsleep) {
kellybs1 10:02615da7a9fe 395 // restore the GPIO state.
kellybs1 10:02615da7a9fe 396 sleep_restore_io();
kellybs1 10:02615da7a9fe 397 }
kellybs1 10:02615da7a9fe 398 }
kellybs1 10:02615da7a9fe 399
kellybs1 10:02615da7a9fe 400 void sleep_wake_rtc_or_interrupt(bool deepsleep) {
kellybs1 10:02615da7a9fe 401 // in some frequency bands we need to wait until another channel is available before transmitting again
kellybs1 10:02615da7a9fe 402 // wait at least 10s between transmissions
kellybs1 10:02615da7a9fe 403 uint32_t delay_s = dot->getNextTxMs() / 1000;
kellybs1 10:02615da7a9fe 404 if (delay_s < 10) {
kellybs1 10:02615da7a9fe 405 delay_s = 10;
kellybs1 10:02615da7a9fe 406 }
kellybs1 10:02615da7a9fe 407
kellybs1 10:02615da7a9fe 408 #if defined (TARGET_XDOT_L151CC)
kellybs1 10:02615da7a9fe 409 if (deepsleep) {
kellybs1 10:02615da7a9fe 410 // for xDot, WAKE pin (connected to S2 on xDot-DK) is the only pin that can wake the processor from deepsleep
kellybs1 10:02615da7a9fe 411 // it is automatically configured when INTERRUPT or RTC_ALARM_OR_INTERRUPT is the wakeup source and deepsleep is true in the mDot::sleep call
kellybs1 10:02615da7a9fe 412 } else {
kellybs1 10:02615da7a9fe 413 // configure WAKE pin (connected to S2 on xDot-DK) as the pin that will wake the xDot from low power modes
kellybs1 10:02615da7a9fe 414 // other pins can be confgured instead: GPIO0-3 or UART_RX
kellybs1 10:02615da7a9fe 415 dot->setWakePin(WAKE);
kellybs1 10:02615da7a9fe 416 }
kellybs1 10:02615da7a9fe 417
kellybs1 10:02615da7a9fe 418 logInfo("%ssleeping %lus or until interrupt on %s pin", deepsleep ? "deep" : "", delay_s, deepsleep ? "WAKE" : mDot::pinName2Str(dot->getWakePin()).c_str());
kellybs1 10:02615da7a9fe 419 #else
kellybs1 10:02615da7a9fe 420 if (deepsleep) {
kellybs1 10:02615da7a9fe 421 // for mDot, XBEE_DIO7 pin is the only pin that can wake the processor from deepsleep
kellybs1 10:02615da7a9fe 422 // it is automatically configured when INTERRUPT or RTC_ALARM_OR_INTERRUPT is the wakeup source and deepsleep is true in the mDot::sleep call
kellybs1 10:02615da7a9fe 423 } else {
kellybs1 10:02615da7a9fe 424 // configure XBEE_DIO7 pin as the pin that will wake the mDot from low power modes
kellybs1 10:02615da7a9fe 425 // other pins can be confgured instead: XBEE_DIO2-6, XBEE_DI8, XBEE_DIN
kellybs1 10:02615da7a9fe 426 dot->setWakePin(XBEE_DIO7);
kellybs1 10:02615da7a9fe 427 }
kellybs1 10:02615da7a9fe 428
kellybs1 10:02615da7a9fe 429 logInfo("%ssleeping %lus or until interrupt on %s pin", deepsleep ? "deep" : "", delay_s, deepsleep ? "DIO7" : mDot::pinName2Str(dot->getWakePin()).c_str());
kellybs1 10:02615da7a9fe 430 #endif
kellybs1 10:02615da7a9fe 431
kellybs1 10:02615da7a9fe 432 logInfo("application will %s after waking up", deepsleep ? "execute from beginning" : "resume");
kellybs1 10:02615da7a9fe 433
kellybs1 10:02615da7a9fe 434 // lowest current consumption in sleep mode can only be achieved by configuring IOs as analog inputs with no pull resistors
kellybs1 10:02615da7a9fe 435 // the library handles all internal IOs automatically, but the external IOs are the application's responsibility
kellybs1 10:02615da7a9fe 436 // certain IOs may require internal pullup or pulldown resistors because leaving them floating would cause extra current consumption
kellybs1 10:02615da7a9fe 437 // for xDot: UART_*, I2C_*, SPI_*, GPIO*, WAKE
kellybs1 10:02615da7a9fe 438 // for mDot: XBEE_*, USBTX, USBRX, PB_0, PB_1
kellybs1 10:02615da7a9fe 439 // steps are:
kellybs1 10:02615da7a9fe 440 // * save IO configuration
kellybs1 10:02615da7a9fe 441 // * configure IOs to reduce current consumption
kellybs1 10:02615da7a9fe 442 // * sleep
kellybs1 10:02615da7a9fe 443 // * restore IO configuration
kellybs1 10:02615da7a9fe 444 if (! deepsleep) {
kellybs1 10:02615da7a9fe 445 // save the GPIO state.
kellybs1 10:02615da7a9fe 446 sleep_save_io();
kellybs1 10:02615da7a9fe 447
kellybs1 10:02615da7a9fe 448 // configure GPIOs for lowest current
kellybs1 10:02615da7a9fe 449 sleep_configure_io();
kellybs1 10:02615da7a9fe 450 }
kellybs1 10:02615da7a9fe 451
kellybs1 10:02615da7a9fe 452 // go to sleep/deepsleep and wake using the RTC alarm after delay_s seconds or rising edge of configured wake pin (only the WAKE pin in deepsleep)
kellybs1 10:02615da7a9fe 453 // whichever comes first will wake the xDot
kellybs1 10:02615da7a9fe 454 dot->sleep(delay_s, mDot::RTC_ALARM_OR_INTERRUPT, deepsleep);
kellybs1 10:02615da7a9fe 455
kellybs1 10:02615da7a9fe 456 if (! deepsleep) {
kellybs1 10:02615da7a9fe 457 // restore the GPIO state.
kellybs1 10:02615da7a9fe 458 sleep_restore_io();
kellybs1 10:02615da7a9fe 459 }
kellybs1 10:02615da7a9fe 460 }
kellybs1 10:02615da7a9fe 461
kellybs1 10:02615da7a9fe 462 void sleep_save_io() {
kellybs1 10:02615da7a9fe 463 #if defined(TARGET_XDOT_L151CC)
kellybs1 10:02615da7a9fe 464 xdot_save_gpio_state();
kellybs1 10:02615da7a9fe 465 #else
kellybs1 10:02615da7a9fe 466 portA[0] = GPIOA->MODER;
kellybs1 10:02615da7a9fe 467 portA[1] = GPIOA->OTYPER;
kellybs1 10:02615da7a9fe 468 portA[2] = GPIOA->OSPEEDR;
kellybs1 10:02615da7a9fe 469 portA[3] = GPIOA->PUPDR;
kellybs1 10:02615da7a9fe 470 portA[4] = GPIOA->AFR[0];
kellybs1 10:02615da7a9fe 471 portA[5] = GPIOA->AFR[1];
kellybs1 10:02615da7a9fe 472
kellybs1 10:02615da7a9fe 473 portB[0] = GPIOB->MODER;
kellybs1 10:02615da7a9fe 474 portB[1] = GPIOB->OTYPER;
kellybs1 10:02615da7a9fe 475 portB[2] = GPIOB->OSPEEDR;
kellybs1 10:02615da7a9fe 476 portB[3] = GPIOB->PUPDR;
kellybs1 10:02615da7a9fe 477 portB[4] = GPIOB->AFR[0];
kellybs1 10:02615da7a9fe 478 portB[5] = GPIOB->AFR[1];
kellybs1 10:02615da7a9fe 479
kellybs1 10:02615da7a9fe 480 portC[0] = GPIOC->MODER;
kellybs1 10:02615da7a9fe 481 portC[1] = GPIOC->OTYPER;
kellybs1 10:02615da7a9fe 482 portC[2] = GPIOC->OSPEEDR;
kellybs1 10:02615da7a9fe 483 portC[3] = GPIOC->PUPDR;
kellybs1 10:02615da7a9fe 484 portC[4] = GPIOC->AFR[0];
kellybs1 10:02615da7a9fe 485 portC[5] = GPIOC->AFR[1];
kellybs1 10:02615da7a9fe 486
kellybs1 10:02615da7a9fe 487 portD[0] = GPIOD->MODER;
kellybs1 10:02615da7a9fe 488 portD[1] = GPIOD->OTYPER;
kellybs1 10:02615da7a9fe 489 portD[2] = GPIOD->OSPEEDR;
kellybs1 10:02615da7a9fe 490 portD[3] = GPIOD->PUPDR;
kellybs1 10:02615da7a9fe 491 portD[4] = GPIOD->AFR[0];
kellybs1 10:02615da7a9fe 492 portD[5] = GPIOD->AFR[1];
kellybs1 10:02615da7a9fe 493
kellybs1 10:02615da7a9fe 494 portH[0] = GPIOH->MODER;
kellybs1 10:02615da7a9fe 495 portH[1] = GPIOH->OTYPER;
kellybs1 10:02615da7a9fe 496 portH[2] = GPIOH->OSPEEDR;
kellybs1 10:02615da7a9fe 497 portH[3] = GPIOH->PUPDR;
kellybs1 10:02615da7a9fe 498 portH[4] = GPIOH->AFR[0];
kellybs1 10:02615da7a9fe 499 portH[5] = GPIOH->AFR[1];
kellybs1 10:02615da7a9fe 500 #endif
kellybs1 10:02615da7a9fe 501 }
kellybs1 10:02615da7a9fe 502
kellybs1 10:02615da7a9fe 503 void sleep_configure_io() {
kellybs1 10:02615da7a9fe 504 #if defined(TARGET_XDOT_L151CC)
kellybs1 10:02615da7a9fe 505 // GPIO Ports Clock Enable
kellybs1 10:02615da7a9fe 506 __GPIOA_CLK_ENABLE();
kellybs1 10:02615da7a9fe 507 __GPIOB_CLK_ENABLE();
kellybs1 10:02615da7a9fe 508 __GPIOC_CLK_ENABLE();
kellybs1 10:02615da7a9fe 509 __GPIOH_CLK_ENABLE();
kellybs1 10:02615da7a9fe 510
kellybs1 10:02615da7a9fe 511 GPIO_InitTypeDef GPIO_InitStruct;
kellybs1 10:02615da7a9fe 512
kellybs1 10:02615da7a9fe 513 // UART1_TX, UART1_RTS & UART1_CTS to analog nopull - RX could be a wakeup source
kellybs1 10:02615da7a9fe 514 GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_11 | GPIO_PIN_12;
kellybs1 10:02615da7a9fe 515 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
kellybs1 10:02615da7a9fe 516 GPIO_InitStruct.Pull = GPIO_NOPULL;
kellybs1 10:02615da7a9fe 517 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
kellybs1 10:02615da7a9fe 518
kellybs1 10:02615da7a9fe 519 // I2C_SDA & I2C_SCL to analog nopull
kellybs1 10:02615da7a9fe 520 GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9;
kellybs1 10:02615da7a9fe 521 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
kellybs1 10:02615da7a9fe 522 GPIO_InitStruct.Pull = GPIO_NOPULL;
kellybs1 10:02615da7a9fe 523 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
kellybs1 10:02615da7a9fe 524
kellybs1 10:02615da7a9fe 525 // SPI_MOSI, SPI_MISO, SPI_SCK, & SPI_NSS to analog nopull
kellybs1 10:02615da7a9fe 526 GPIO_InitStruct.Pin = GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
kellybs1 10:02615da7a9fe 527 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
kellybs1 10:02615da7a9fe 528 GPIO_InitStruct.Pull = GPIO_NOPULL;
kellybs1 10:02615da7a9fe 529 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
kellybs1 10:02615da7a9fe 530
kellybs1 10:02615da7a9fe 531 // iterate through potential wake pins - leave the configured wake pin alone if one is needed
kellybs1 10:02615da7a9fe 532 if (dot->getWakePin() != WAKE || dot->getWakeMode() == mDot::RTC_ALARM) {
kellybs1 10:02615da7a9fe 533 GPIO_InitStruct.Pin = GPIO_PIN_0;
kellybs1 10:02615da7a9fe 534 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
kellybs1 10:02615da7a9fe 535 GPIO_InitStruct.Pull = GPIO_NOPULL;
kellybs1 10:02615da7a9fe 536 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
kellybs1 10:02615da7a9fe 537 }
kellybs1 10:02615da7a9fe 538 if (dot->getWakePin() != GPIO0 || dot->getWakeMode() == mDot::RTC_ALARM) {
kellybs1 10:02615da7a9fe 539 GPIO_InitStruct.Pin = GPIO_PIN_4;
kellybs1 10:02615da7a9fe 540 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
kellybs1 10:02615da7a9fe 541 GPIO_InitStruct.Pull = GPIO_NOPULL;
kellybs1 10:02615da7a9fe 542 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
kellybs1 10:02615da7a9fe 543 }
kellybs1 10:02615da7a9fe 544 if (dot->getWakePin() != GPIO1 || dot->getWakeMode() == mDot::RTC_ALARM) {
kellybs1 10:02615da7a9fe 545 GPIO_InitStruct.Pin = GPIO_PIN_5;
kellybs1 10:02615da7a9fe 546 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
kellybs1 10:02615da7a9fe 547 GPIO_InitStruct.Pull = GPIO_NOPULL;
kellybs1 10:02615da7a9fe 548 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
kellybs1 10:02615da7a9fe 549 }
kellybs1 10:02615da7a9fe 550 if (dot->getWakePin() != GPIO2 || dot->getWakeMode() == mDot::RTC_ALARM) {
kellybs1 10:02615da7a9fe 551 GPIO_InitStruct.Pin = GPIO_PIN_0;
kellybs1 10:02615da7a9fe 552 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
kellybs1 10:02615da7a9fe 553 GPIO_InitStruct.Pull = GPIO_NOPULL;
kellybs1 10:02615da7a9fe 554 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
kellybs1 10:02615da7a9fe 555 }
kellybs1 10:02615da7a9fe 556 if (dot->getWakePin() != GPIO3 || dot->getWakeMode() == mDot::RTC_ALARM) {
kellybs1 10:02615da7a9fe 557 GPIO_InitStruct.Pin = GPIO_PIN_2;
kellybs1 10:02615da7a9fe 558 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
kellybs1 10:02615da7a9fe 559 GPIO_InitStruct.Pull = GPIO_NOPULL;
kellybs1 10:02615da7a9fe 560 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
kellybs1 10:02615da7a9fe 561 }
kellybs1 10:02615da7a9fe 562 if (dot->getWakePin() != UART1_RX || dot->getWakeMode() == mDot::RTC_ALARM) {
kellybs1 10:02615da7a9fe 563 GPIO_InitStruct.Pin = GPIO_PIN_10;
kellybs1 10:02615da7a9fe 564 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
kellybs1 10:02615da7a9fe 565 GPIO_InitStruct.Pull = GPIO_NOPULL;
kellybs1 10:02615da7a9fe 566 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
kellybs1 10:02615da7a9fe 567 }
kellybs1 10:02615da7a9fe 568 #else
kellybs1 10:02615da7a9fe 569 /* GPIO Ports Clock Enable */
kellybs1 10:02615da7a9fe 570 __GPIOA_CLK_ENABLE();
kellybs1 10:02615da7a9fe 571 __GPIOB_CLK_ENABLE();
kellybs1 10:02615da7a9fe 572 __GPIOC_CLK_ENABLE();
kellybs1 10:02615da7a9fe 573
kellybs1 10:02615da7a9fe 574 GPIO_InitTypeDef GPIO_InitStruct;
kellybs1 10:02615da7a9fe 575
kellybs1 10:02615da7a9fe 576 // XBEE_DOUT, XBEE_DIN, XBEE_DO8, XBEE_RSSI, USBTX, USBRX, PA_12, PA_13, PA_14 & PA_15 to analog nopull
kellybs1 10:02615da7a9fe 577 GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_6 | GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10
kellybs1 10:02615da7a9fe 578 | GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
kellybs1 10:02615da7a9fe 579 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
kellybs1 10:02615da7a9fe 580 GPIO_InitStruct.Pull = GPIO_NOPULL;
kellybs1 10:02615da7a9fe 581 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
kellybs1 10:02615da7a9fe 582
kellybs1 10:02615da7a9fe 583 // PB_0, PB_1, PB_3 & PB_4 to analog nopull
kellybs1 10:02615da7a9fe 584 GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_3 | GPIO_PIN_4;
kellybs1 10:02615da7a9fe 585 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
kellybs1 10:02615da7a9fe 586 GPIO_InitStruct.Pull = GPIO_NOPULL;
kellybs1 10:02615da7a9fe 587 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
kellybs1 10:02615da7a9fe 588
kellybs1 10:02615da7a9fe 589 // PC_9 & PC_13 to analog nopull
kellybs1 10:02615da7a9fe 590 GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_13;
kellybs1 10:02615da7a9fe 591 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
kellybs1 10:02615da7a9fe 592 GPIO_InitStruct.Pull = GPIO_NOPULL;
kellybs1 10:02615da7a9fe 593 HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
kellybs1 10:02615da7a9fe 594
kellybs1 10:02615da7a9fe 595 // iterate through potential wake pins - leave the configured wake pin alone if one is needed
kellybs1 10:02615da7a9fe 596 // XBEE_DIN - PA3
kellybs1 10:02615da7a9fe 597 // XBEE_DIO2 - PA5
kellybs1 10:02615da7a9fe 598 // XBEE_DIO3 - PA4
kellybs1 10:02615da7a9fe 599 // XBEE_DIO4 - PA7
kellybs1 10:02615da7a9fe 600 // XBEE_DIO5 - PC1
kellybs1 10:02615da7a9fe 601 // XBEE_DIO6 - PA1
kellybs1 10:02615da7a9fe 602 // XBEE_DIO7 - PA0
kellybs1 10:02615da7a9fe 603 // XBEE_SLEEPRQ - PA11
kellybs1 10:02615da7a9fe 604
kellybs1 10:02615da7a9fe 605 if (dot->getWakePin() != XBEE_DIN || dot->getWakeMode() == mDot::RTC_ALARM) {
kellybs1 10:02615da7a9fe 606 GPIO_InitStruct.Pin = GPIO_PIN_3;
kellybs1 10:02615da7a9fe 607 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
kellybs1 10:02615da7a9fe 608 GPIO_InitStruct.Pull = GPIO_NOPULL;
kellybs1 10:02615da7a9fe 609 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
kellybs1 10:02615da7a9fe 610 }
kellybs1 10:02615da7a9fe 611
kellybs1 10:02615da7a9fe 612 if (dot->getWakePin() != XBEE_DIO2 || dot->getWakeMode() == mDot::RTC_ALARM) {
kellybs1 10:02615da7a9fe 613 GPIO_InitStruct.Pin = GPIO_PIN_5;
kellybs1 10:02615da7a9fe 614 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
kellybs1 10:02615da7a9fe 615 GPIO_InitStruct.Pull = GPIO_NOPULL;
kellybs1 10:02615da7a9fe 616 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
kellybs1 10:02615da7a9fe 617 }
kellybs1 10:02615da7a9fe 618
kellybs1 10:02615da7a9fe 619 if (dot->getWakePin() != XBEE_DIO3 || dot->getWakeMode() == mDot::RTC_ALARM) {
kellybs1 10:02615da7a9fe 620 GPIO_InitStruct.Pin = GPIO_PIN_4;
kellybs1 10:02615da7a9fe 621 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
kellybs1 10:02615da7a9fe 622 GPIO_InitStruct.Pull = GPIO_NOPULL;
kellybs1 10:02615da7a9fe 623 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
kellybs1 10:02615da7a9fe 624 }
kellybs1 10:02615da7a9fe 625
kellybs1 10:02615da7a9fe 626 if (dot->getWakePin() != XBEE_DIO4 || dot->getWakeMode() == mDot::RTC_ALARM) {
kellybs1 10:02615da7a9fe 627 GPIO_InitStruct.Pin = GPIO_PIN_7;
kellybs1 10:02615da7a9fe 628 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
kellybs1 10:02615da7a9fe 629 GPIO_InitStruct.Pull = GPIO_NOPULL;
kellybs1 10:02615da7a9fe 630 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
kellybs1 10:02615da7a9fe 631 }
kellybs1 10:02615da7a9fe 632
kellybs1 10:02615da7a9fe 633 if (dot->getWakePin() != XBEE_DIO5 || dot->getWakeMode() == mDot::RTC_ALARM) {
kellybs1 10:02615da7a9fe 634 GPIO_InitStruct.Pin = GPIO_PIN_1;
kellybs1 10:02615da7a9fe 635 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
kellybs1 10:02615da7a9fe 636 GPIO_InitStruct.Pull = GPIO_NOPULL;
kellybs1 10:02615da7a9fe 637 HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
kellybs1 10:02615da7a9fe 638 }
kellybs1 10:02615da7a9fe 639
kellybs1 10:02615da7a9fe 640 if (dot->getWakePin() != XBEE_DIO6 || dot->getWakeMode() == mDot::RTC_ALARM) {
kellybs1 10:02615da7a9fe 641 GPIO_InitStruct.Pin = GPIO_PIN_1;
kellybs1 10:02615da7a9fe 642 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
kellybs1 10:02615da7a9fe 643 GPIO_InitStruct.Pull = GPIO_NOPULL;
kellybs1 10:02615da7a9fe 644 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
kellybs1 10:02615da7a9fe 645 }
kellybs1 10:02615da7a9fe 646
kellybs1 10:02615da7a9fe 647 if (dot->getWakePin() != XBEE_DIO7 || dot->getWakeMode() == mDot::RTC_ALARM) {
kellybs1 10:02615da7a9fe 648 GPIO_InitStruct.Pin = GPIO_PIN_0;
kellybs1 10:02615da7a9fe 649 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
kellybs1 10:02615da7a9fe 650 GPIO_InitStruct.Pull = GPIO_NOPULL;
kellybs1 10:02615da7a9fe 651 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
kellybs1 10:02615da7a9fe 652 }
kellybs1 10:02615da7a9fe 653
kellybs1 10:02615da7a9fe 654 if (dot->getWakePin() != XBEE_SLEEPRQ|| dot->getWakeMode() == mDot::RTC_ALARM) {
kellybs1 10:02615da7a9fe 655 GPIO_InitStruct.Pin = GPIO_PIN_11;
kellybs1 10:02615da7a9fe 656 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
kellybs1 10:02615da7a9fe 657 GPIO_InitStruct.Pull = GPIO_NOPULL;
kellybs1 10:02615da7a9fe 658 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
kellybs1 10:02615da7a9fe 659 }
kellybs1 10:02615da7a9fe 660 #endif
kellybs1 10:02615da7a9fe 661 }
kellybs1 10:02615da7a9fe 662
kellybs1 10:02615da7a9fe 663 void sleep_restore_io() {
kellybs1 10:02615da7a9fe 664 #if defined(TARGET_XDOT_L151CC)
kellybs1 10:02615da7a9fe 665 xdot_restore_gpio_state();
kellybs1 10:02615da7a9fe 666 #else
kellybs1 10:02615da7a9fe 667 GPIOA->MODER = portA[0];
kellybs1 10:02615da7a9fe 668 GPIOA->OTYPER = portA[1];
kellybs1 10:02615da7a9fe 669 GPIOA->OSPEEDR = portA[2];
kellybs1 10:02615da7a9fe 670 GPIOA->PUPDR = portA[3];
kellybs1 10:02615da7a9fe 671 GPIOA->AFR[0] = portA[4];
kellybs1 10:02615da7a9fe 672 GPIOA->AFR[1] = portA[5];
kellybs1 10:02615da7a9fe 673
kellybs1 10:02615da7a9fe 674 GPIOB->MODER = portB[0];
kellybs1 10:02615da7a9fe 675 GPIOB->OTYPER = portB[1];
kellybs1 10:02615da7a9fe 676 GPIOB->OSPEEDR = portB[2];
kellybs1 10:02615da7a9fe 677 GPIOB->PUPDR = portB[3];
kellybs1 10:02615da7a9fe 678 GPIOB->AFR[0] = portB[4];
kellybs1 10:02615da7a9fe 679 GPIOB->AFR[1] = portB[5];
kellybs1 10:02615da7a9fe 680
kellybs1 10:02615da7a9fe 681 GPIOC->MODER = portC[0];
kellybs1 10:02615da7a9fe 682 GPIOC->OTYPER = portC[1];
kellybs1 10:02615da7a9fe 683 GPIOC->OSPEEDR = portC[2];
kellybs1 10:02615da7a9fe 684 GPIOC->PUPDR = portC[3];
kellybs1 10:02615da7a9fe 685 GPIOC->AFR[0] = portC[4];
kellybs1 10:02615da7a9fe 686 GPIOC->AFR[1] = portC[5];
kellybs1 10:02615da7a9fe 687
kellybs1 10:02615da7a9fe 688 GPIOD->MODER = portD[0];
kellybs1 10:02615da7a9fe 689 GPIOD->OTYPER = portD[1];
kellybs1 10:02615da7a9fe 690 GPIOD->OSPEEDR = portD[2];
kellybs1 10:02615da7a9fe 691 GPIOD->PUPDR = portD[3];
kellybs1 10:02615da7a9fe 692 GPIOD->AFR[0] = portD[4];
kellybs1 10:02615da7a9fe 693 GPIOD->AFR[1] = portD[5];
kellybs1 10:02615da7a9fe 694
kellybs1 10:02615da7a9fe 695 GPIOH->MODER = portH[0];
kellybs1 10:02615da7a9fe 696 GPIOH->OTYPER = portH[1];
kellybs1 10:02615da7a9fe 697 GPIOH->OSPEEDR = portH[2];
kellybs1 10:02615da7a9fe 698 GPIOH->PUPDR = portH[3];
kellybs1 10:02615da7a9fe 699 GPIOH->AFR[0] = portH[4];
kellybs1 10:02615da7a9fe 700 GPIOH->AFR[1] = portH[5];
kellybs1 10:02615da7a9fe 701 #endif
kellybs1 10:02615da7a9fe 702 }
kellybs1 10:02615da7a9fe 703
kellybs1 10:02615da7a9fe 704 void send_data(std::vector<uint8_t> data) {
kellybs1 10:02615da7a9fe 705 int32_t ret;
kellybs1 10:02615da7a9fe 706
kellybs1 10:02615da7a9fe 707 ret = dot->send(data);
kellybs1 10:02615da7a9fe 708 if (ret != mDot::MDOT_OK) {
kellybs1 10:02615da7a9fe 709 logError("failed to send data to %s [%d][%s]", dot->getJoinMode() == mDot::PEER_TO_PEER ? "peer" : "gateway", ret, mDot::getReturnCodeString(ret).c_str());
kellybs1 10:02615da7a9fe 710 } else {
kellybs1 10:02615da7a9fe 711 logInfo("successfully sent data to %s", dot->getJoinMode() == mDot::PEER_TO_PEER ? "peer" : "gateway");
kellybs1 10:02615da7a9fe 712 }
kellybs1 10:02615da7a9fe 713 }
kellybs1 10:02615da7a9fe 714