Minor maintenance updates

Committer:
cgoyette
Date:
Wed Sep 20 14:12:54 2017 +0000
Revision:
1:24a27dd7f48f
Parent:
0:469d03d84986
updated library to match multitechs latest

Who changed what in which revision?

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