Sensor Code By Jorge Troncoso

Dependencies:   Sensor2 MAX31855-1 libmDot-1

Fork of 0A101_mDot_Sensor_Rev3_AUS by Jorge Troncoso

Committer:
jortronm2
Date:
Wed Jun 13 11:31:15 2018 +0000
Revision:
2:0417c5cdceaf
Parent:
0:1441b10e38a6
Sensor Code by Jorge Troncoso

Who changed what in which revision?

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