Code to talk to the mdot

Dependencies:   libmDot-mbed5 ISL29011

Committer:
bhimebau
Date:
Thu Feb 08 18:45:41 2018 +0000
Revision:
0:34da43c761f8
Initial push;

Who changed what in which revision?

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