Minor maintenance updates

Committer:
cgoyette
Date:
Mon Jul 24 11:30:40 2017 +0000
Revision:
0:469d03d84986
Child:
1:24a27dd7f48f
.

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