lora's drivers taken out of examples repo to make a smaller binary

Dependencies:   libxDot-mbed5

Committer:
candre97
Date:
Sun Dec 08 01:56:17 2019 +0000
Revision:
1:80bc4501abc5
working on lower levels of radio driver. Still have a binary that is much too large. ;

Who changed what in which revision?

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