Utility for reading temperature on DHT22 and sending to LoRa network

Dependencies:   DHT22 libmDot-mbed5

Committer:
kellybs1
Date:
Tue Oct 31 03:12:37 2017 +0000
Revision:
0:e0037d88baa2
initial commit - with junk keys

Who changed what in which revision?

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