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