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