zcbvsDG

Dependencies:   ISL29011

Committer:
deepak15
Date:
Mon Sep 25 09:41:00 2017 +0000
Revision:
0:aa8df9825311
test _1;

Who changed what in which revision?

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