34

Dependencies:   BSP libNDEF libxDot-mbed5 ST25DV

Committer:
umickelsen
Date:
Sat Sep 30 22:01:56 2017 +0000
Revision:
0:b15aed49e54b
Importacion inicial

Who changed what in which revision?

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