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