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