Modified version to include updated libmdot, mbedos, and code includes channel plan setting

Dependencies:   libmDot-mbed5

Fork of mDot_LoRa_Connect_Example_ABPA by MultiTech

Committer:
kellybs1
Date:
Sun Sep 10 05:30:38 2017 +0000
Revision:
15:abf182f4ebdc
Parent:
12:41882e27ee79
tidying up repos;

Who changed what in which revision?

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