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