Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: libxDot-mbed5 TSL2561
Fork of Dot-Examples by
dot_util.cpp
00001 #include "dot_util.h" 00002 #if defined(TARGET_XDOT_L151CC) 00003 #include "xdot_low_power.h" 00004 00005 #endif 00006 00007 #if defined(TARGET_MTS_MDOT_F411RE) 00008 uint32_t portA[6]; 00009 uint32_t portB[6]; 00010 uint32_t portC[6]; 00011 uint32_t portD[6]; 00012 uint32_t portH[6]; 00013 #endif 00014 00015 00016 void display_config() { 00017 // display configuration and library version information 00018 logInfo("====================="); 00019 logInfo("general configuration"); 00020 logInfo("====================="); 00021 logInfo("version ------------------ %s", dot->getId().c_str()); 00022 logInfo("device ID/EUI ------------ %s", mts::Text::bin2hexString(dot->getDeviceId()).c_str()); 00023 logInfo("default channel plan ----- %s", mDot::FrequencyBandStr(dot->getDefaultFrequencyBand()).c_str()); 00024 logInfo("current channel plan ----- %s", mDot::FrequencyBandStr(dot->getFrequencyBand()).c_str()); 00025 if (lora::ChannelPlan::IsPlanFixed(dot->getFrequencyBand())) { 00026 logInfo("frequency sub band ------- %u", dot->getFrequencySubBand()); 00027 } 00028 00029 std::string network_mode_str("Undefined"); 00030 uint8_t network_mode = dot->getPublicNetwork(); 00031 // if (network_mode == lora::PRIVATE_MTS) 00032 // network_mode_str = "Private MTS"; 00033 // else if (network_mode == lora::PUBLIC_LORAWAN) 00034 // network_mode_str = "Public LoRaWAN"; 00035 // else if (network_mode == lora::PRIVATE_LORAWAN) 00036 // network_mode_str = "Private LoRaWAN"; 00037 logInfo("public network ----------- %s", network_mode_str.c_str()); 00038 00039 logInfo("========================="); 00040 logInfo("credentials configuration"); 00041 logInfo("========================="); 00042 logInfo("device class ------------- %s", dot->getClass().c_str()); 00043 logInfo("network join mode -------- %s", mDot::JoinModeStr(dot->getJoinMode()).c_str()); 00044 if (dot->getJoinMode() == mDot::MANUAL || dot->getJoinMode() == mDot::PEER_TO_PEER) { 00045 logInfo("network address ---------- %s", mts::Text::bin2hexString(dot->getNetworkAddress()).c_str()); 00046 logInfo("network session key------- %s", mts::Text::bin2hexString(dot->getNetworkSessionKey()).c_str()); 00047 logInfo("data session key---------- %s", mts::Text::bin2hexString(dot->getDataSessionKey()).c_str()); 00048 } else { 00049 logInfo("network name ------------- %s", dot->getNetworkName().c_str()); 00050 logInfo("network phrase ----------- %s", dot->getNetworkPassphrase().c_str()); 00051 logInfo("network EUI -------------- %s", mts::Text::bin2hexString(dot->getNetworkId()).c_str()); 00052 logInfo("network KEY -------------- %s", mts::Text::bin2hexString(dot->getNetworkKey()).c_str()); 00053 } 00054 logInfo("========================"); 00055 logInfo("communication parameters"); 00056 logInfo("========================"); 00057 if (dot->getJoinMode() == mDot::PEER_TO_PEER) { 00058 logInfo("TX frequency ------------- %lu", dot->getTxFrequency()); 00059 } else { 00060 logInfo("acks --------------------- %s, %u attempts", dot->getAck() > 0 ? "on" : "off", dot->getAck()); 00061 } 00062 logInfo("TX datarate -------------- %s", mDot::DataRateStr(dot->getTxDataRate()).c_str()); 00063 logInfo("TX power ----------------- %lu dBm", dot->getTxPower()); 00064 logInfo("antenna gain ------------- %u dBm", dot->getAntennaGain()); 00065 logInfo("LBT ---------------------- %s", dot->getLbtTimeUs() ? "on" : "off"); 00066 if (dot->getLbtTimeUs()) { 00067 logInfo("LBT time ----------------- %lu us", dot->getLbtTimeUs()); 00068 logInfo("LBT threshold ------------ %d dBm", dot->getLbtThreshold()); 00069 } 00070 } 00071 00072 void update_ota_config_name_phrase(std::string network_name, std::string network_passphrase, uint8_t frequency_sub_band, lora::NetworkType network_type, uint8_t ack) { 00073 std::string current_network_name = dot->getNetworkName(); 00074 std::string current_network_passphrase = dot->getNetworkPassphrase(); 00075 uint8_t current_frequency_sub_band = dot->getFrequencySubBand(); 00076 uint8_t current_network_type = dot->getPublicNetwork(); 00077 uint8_t current_ack = dot->getAck(); 00078 00079 if (current_network_name != network_name) { 00080 logInfo("changing network name from \"%s\" to \"%s\"", current_network_name.c_str(), network_name.c_str()); 00081 if (dot->setNetworkName(network_name) != mDot::MDOT_OK) { 00082 logError("failed to set network name to \"%s\"", network_name.c_str()); 00083 } 00084 } 00085 00086 if (current_network_passphrase != network_passphrase) { 00087 logInfo("changing network passphrase from \"%s\" to \"%s\"", current_network_passphrase.c_str(), network_passphrase.c_str()); 00088 if (dot->setNetworkPassphrase(network_passphrase) != mDot::MDOT_OK) { 00089 logError("failed to set network passphrase to \"%s\"", network_passphrase.c_str()); 00090 } 00091 } 00092 00093 if (lora::ChannelPlan::IsPlanFixed(dot->getFrequencyBand())) { 00094 if (current_frequency_sub_band != frequency_sub_band) { 00095 logInfo("changing frequency sub band from %u to %u", current_frequency_sub_band, frequency_sub_band); 00096 if (dot->setFrequencySubBand(frequency_sub_band) != mDot::MDOT_OK) { 00097 logError("failed to set frequency sub band to %u", frequency_sub_band); 00098 } 00099 } 00100 } 00101 00102 if (current_network_type != network_type) { 00103 logInfo("changing public network from %d to %d", current_network_type, network_type); 00104 if (dot->setPublicNetwork(network_type) != mDot::MDOT_OK) { 00105 logError("failed to set public network to %d", network_type); 00106 } 00107 } 00108 00109 if (current_ack != ack) { 00110 logInfo("changing acks from %u to %u", current_ack, ack); 00111 if (dot->setAck(ack) != mDot::MDOT_OK) { 00112 logError("failed to set acks to %u", ack); 00113 } 00114 } 00115 } 00116 00117 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) { 00118 std::vector<uint8_t> current_network_id = dot->getNetworkId(); 00119 std::vector<uint8_t> current_network_key = dot->getNetworkKey(); 00120 uint8_t current_frequency_sub_band = dot->getFrequencySubBand(); 00121 bool current_public_network = dot->getPublicNetwork(); 00122 uint8_t current_ack = dot->getAck(); 00123 00124 std::vector<uint8_t> network_id_vector(network_id, network_id + 8); 00125 std::vector<uint8_t> network_key_vector(network_key, network_key + 16); 00126 00127 if (current_network_id != network_id_vector) { 00128 logInfo("changing network ID from \"%s\" to \"%s\"", mts::Text::bin2hexString(current_network_id).c_str(), mts::Text::bin2hexString(network_id_vector).c_str()); 00129 if (dot->setNetworkId(network_id_vector) != mDot::MDOT_OK) { 00130 logError("failed to set network ID to \"%s\"", mts::Text::bin2hexString(network_id_vector).c_str()); 00131 } 00132 } 00133 00134 if (current_network_key != network_key_vector) { 00135 logInfo("changing network KEY from \"%s\" to \"%s\"", mts::Text::bin2hexString(current_network_key).c_str(), mts::Text::bin2hexString(network_key_vector).c_str()); 00136 if (dot->setNetworkKey(network_key_vector) != mDot::MDOT_OK) { 00137 logError("failed to set network KEY to \"%s\"", mts::Text::bin2hexString(network_key_vector).c_str()); 00138 } 00139 } 00140 00141 if (lora::ChannelPlan::IsPlanFixed(dot->getFrequencyBand())) { 00142 if (current_frequency_sub_band != frequency_sub_band) { 00143 logInfo("changing frequency sub band from %u to %u", current_frequency_sub_band, frequency_sub_band); 00144 if (dot->setFrequencySubBand(frequency_sub_band) != mDot::MDOT_OK) { 00145 logError("failed to set frequency sub band to %u", frequency_sub_band); 00146 } 00147 } 00148 } 00149 00150 if (current_public_network != public_network) { 00151 logInfo("changing public network from %s to %s", current_public_network ? "on" : "off", public_network ? "on" : "off"); 00152 if (dot->setPublicNetwork(public_network) != mDot::MDOT_OK) { 00153 logError("failed to set public network to %s", public_network ? "on" : "off"); 00154 } 00155 } 00156 00157 if (current_ack != ack) { 00158 logInfo("changing acks from %u to %u", current_ack, ack); 00159 if (dot->setAck(ack) != mDot::MDOT_OK) { 00160 logError("failed to set acks to %u", ack); 00161 } 00162 } 00163 } 00164 00165 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) { 00166 std::vector<uint8_t> current_network_address = dot->getNetworkAddress(); 00167 std::vector<uint8_t> current_network_session_key = dot->getNetworkSessionKey(); 00168 std::vector<uint8_t> current_data_session_key = dot->getDataSessionKey(); 00169 uint8_t current_frequency_sub_band = dot->getFrequencySubBand(); 00170 bool current_public_network = dot->getPublicNetwork(); 00171 uint8_t current_ack = dot->getAck(); 00172 00173 std::vector<uint8_t> network_address_vector(network_address, network_address + 4); 00174 std::vector<uint8_t> network_session_key_vector(network_session_key, network_session_key + 16); 00175 std::vector<uint8_t> data_session_key_vector(data_session_key, data_session_key + 16); 00176 00177 if (current_network_address != network_address_vector) { 00178 logInfo("changing network address from \"%s\" to \"%s\"", mts::Text::bin2hexString(current_network_address).c_str(), mts::Text::bin2hexString(network_address_vector).c_str()); 00179 if (dot->setNetworkAddress(network_address_vector) != mDot::MDOT_OK) { 00180 logError("failed to set network address to \"%s\"", mts::Text::bin2hexString(network_address_vector).c_str()); 00181 } 00182 } 00183 00184 if (current_network_session_key != network_session_key_vector) { 00185 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()); 00186 if (dot->setNetworkSessionKey(network_session_key_vector) != mDot::MDOT_OK) { 00187 logError("failed to set network session key to \"%s\"", mts::Text::bin2hexString(network_session_key_vector).c_str()); 00188 } 00189 } 00190 00191 if (current_data_session_key != data_session_key_vector) { 00192 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()); 00193 if (dot->setDataSessionKey(data_session_key_vector) != mDot::MDOT_OK) { 00194 logError("failed to set data session key to \"%s\"", mts::Text::bin2hexString(data_session_key_vector).c_str()); 00195 } 00196 } 00197 00198 if (current_frequency_sub_band != frequency_sub_band) { 00199 logInfo("changing frequency sub band from %u to %u", current_frequency_sub_band, frequency_sub_band); 00200 if (dot->setFrequencySubBand(frequency_sub_band) != mDot::MDOT_OK) { 00201 logError("failed to set frequency sub band to %u", frequency_sub_band); 00202 } 00203 } 00204 00205 if (current_public_network != public_network) { 00206 logInfo("changing public network from %s to %s", current_public_network ? "on" : "off", public_network ? "on" : "off"); 00207 if (dot->setPublicNetwork(public_network) != mDot::MDOT_OK) { 00208 logError("failed to set public network to %s", public_network ? "on" : "off"); 00209 } 00210 } 00211 00212 if (current_ack != ack) { 00213 logInfo("changing acks from %u to %u", current_ack, ack); 00214 if (dot->setAck(ack) != mDot::MDOT_OK) { 00215 logError("failed to set acks to %u", ack); 00216 } 00217 } 00218 } 00219 00220 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) { 00221 std::vector<uint8_t> current_network_address = dot->getNetworkAddress(); 00222 std::vector<uint8_t> current_network_session_key = dot->getNetworkSessionKey(); 00223 std::vector<uint8_t> current_data_session_key = dot->getDataSessionKey(); 00224 uint32_t current_tx_frequency = dot->getTxFrequency(); 00225 uint8_t current_tx_datarate = dot->getTxDataRate(); 00226 uint8_t current_tx_power = dot->getTxPower(); 00227 00228 std::vector<uint8_t> network_address_vector(network_address, network_address + 4); 00229 std::vector<uint8_t> network_session_key_vector(network_session_key, network_session_key + 16); 00230 std::vector<uint8_t> data_session_key_vector(data_session_key, data_session_key + 16); 00231 00232 if (current_network_address != network_address_vector) { 00233 logInfo("changing network address from \"%s\" to \"%s\"", mts::Text::bin2hexString(current_network_address).c_str(), mts::Text::bin2hexString(network_address_vector).c_str()); 00234 if (dot->setNetworkAddress(network_address_vector) != mDot::MDOT_OK) { 00235 logError("failed to set network address to \"%s\"", mts::Text::bin2hexString(network_address_vector).c_str()); 00236 } 00237 } 00238 00239 if (current_network_session_key != network_session_key_vector) { 00240 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()); 00241 if (dot->setNetworkSessionKey(network_session_key_vector) != mDot::MDOT_OK) { 00242 logError("failed to set network session key to \"%s\"", mts::Text::bin2hexString(network_session_key_vector).c_str()); 00243 } 00244 } 00245 00246 if (current_data_session_key != data_session_key_vector) { 00247 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()); 00248 if (dot->setDataSessionKey(data_session_key_vector) != mDot::MDOT_OK) { 00249 logError("failed to set data session key to \"%s\"", mts::Text::bin2hexString(data_session_key_vector).c_str()); 00250 } 00251 } 00252 00253 if (current_tx_frequency != tx_frequency) { 00254 logInfo("changing TX frequency from %lu to %lu", current_tx_frequency, tx_frequency); 00255 if (dot->setTxFrequency(tx_frequency) != mDot::MDOT_OK) { 00256 logError("failed to set TX frequency to %lu", tx_frequency); 00257 } 00258 } 00259 00260 if (current_tx_datarate != tx_datarate) { 00261 logInfo("changing TX datarate from %u to %u", current_tx_datarate, tx_datarate); 00262 if (dot->setTxDataRate(tx_datarate) != mDot::MDOT_OK) { 00263 logError("failed to set TX datarate to %u", tx_datarate); 00264 } 00265 } 00266 00267 if (current_tx_power != tx_power) { 00268 logInfo("changing TX power from %u to %u", current_tx_power, tx_power); 00269 if (dot->setTxPower(tx_power) != mDot::MDOT_OK) { 00270 logError("failed to set TX power to %u", tx_power); 00271 } 00272 } 00273 } 00274 00275 void update_network_link_check_config(uint8_t link_check_count, uint8_t link_check_threshold) { 00276 uint8_t current_link_check_count = dot->getLinkCheckCount(); 00277 uint8_t current_link_check_threshold = dot->getLinkCheckThreshold(); 00278 00279 if (current_link_check_count != link_check_count) { 00280 logInfo("changing link check count from %u to %u", current_link_check_count, link_check_count); 00281 if (dot->setLinkCheckCount(link_check_count) != mDot::MDOT_OK) { 00282 logError("failed to set link check count to %u", link_check_count); 00283 } 00284 } 00285 00286 if (current_link_check_threshold != link_check_threshold) { 00287 logInfo("changing link check threshold from %u to %u", current_link_check_threshold, link_check_threshold); 00288 if (dot->setLinkCheckThreshold(link_check_threshold) != mDot::MDOT_OK) { 00289 logError("failed to set link check threshold to %u", link_check_threshold); 00290 } 00291 } 00292 } 00293 00294 void join_network(Serial *pc) { 00295 int32_t j_attempts = 0; 00296 int32_t ret = mDot::MDOT_ERROR; 00297 00298 // attempt to join the network 00299 while (ret != mDot::MDOT_OK) { 00300 logInfo("attempt %d to join network", ++j_attempts); 00301 (*pc).printf("attempt %d to join network\r\n", ++j_attempts); 00302 ret = dot->joinNetwork(); 00303 if (ret != mDot::MDOT_OK) { 00304 logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); 00305 (*pc).printf("failed to join network %d:%s\r\n", ret, mDot::getReturnCodeString(ret).c_str()); 00306 // in some frequency bands we need to wait until another channel is available before transmitting again 00307 uint32_t delay_s = (dot->getNextTxMs() / 1000) + 1; 00308 if (delay_s < 5) { 00309 logInfo("waiting %lu s until next free channel", delay_s); 00310 wait(delay_s); 00311 } else { 00312 logInfo("sleeping %lu s until next free channel", delay_s); 00313 dot->sleep(delay_s, mDot::RTC_ALARM, false); 00314 } 00315 } 00316 } 00317 (*pc).printf("j_n(): network join SUCCESSFUL\r\n"); 00318 } 00319 00320 void sleep_wake_rtc_only(bool deepsleep, int delay, Serial *pc) { 00321 // in some frequency bands we need to wait until another channel is available before transmitting again 00322 // wait at least 10s between transmissions 00323 uint32_t delay_s;// = dot->getNextTxMs() / 1000; 00324 /*if (delay_s < 10) { 00325 delay_s = 10; 00326 }*/ 00327 delay_s = delay; 00328 00329 pc->printf("%ssleeping %lus ", deepsleep ? "deep " : " ", delay_s); 00330 pc->printf("application will %s after waking up\r\n", deepsleep ? "execute from beginning" : "resume"); 00331 00332 // lowest current consumption in sleep mode can only be achieved by configuring IOs as analog inputs with no pull resistors 00333 // the library handles all internal IOs automatically, but the external IOs are the application's responsibility 00334 // certain IOs may require internal pullup or pulldown resistors because leaving them floating would cause extra current consumption 00335 // for xDot: UART_*, I2C_*, SPI_*, GPIO*, WAKE 00336 // for mDot: XBEE_*, USBTX, USBRX, PB_0, PB_1 00337 // steps are: 00338 // * save IO configuration 00339 // * configure IOs to reduce current consumption 00340 // * sleep 00341 // * restore IO configuration 00342 if (! deepsleep) { 00343 // save the GPIO state. 00344 sleep_save_io(); 00345 00346 // configure GPIOs for lowest current 00347 sleep_configure_io(); 00348 } 00349 00350 // go to sleep/deepsleep for delay_s seconds and wake using the RTC alarm 00351 dot->sleep(delay_s, mDot::RTC_ALARM, deepsleep); 00352 00353 if (! deepsleep) { 00354 // restore the GPIO state. 00355 sleep_restore_io(); 00356 } 00357 } 00358 00359 void sleep_wake_interrupt_only(bool deepsleep) { 00360 #if defined (TARGET_XDOT_L151CC) 00361 if (deepsleep) { 00362 // for xDot, WAKE pin (connected to S2 on xDot-DK) is the only pin that can wake the processor from deepsleep 00363 // it is automatically configured when INTERRUPT or RTC_ALARM_OR_INTERRUPT is the wakeup source and deepsleep is true in the mDot::sleep call 00364 } else { 00365 // configure WAKE pin (connected to S2 on xDot-DK) as the pin that will wake the xDot from low power modes 00366 // other pins can be confgured instead: GPIO0-3 or UART_RX 00367 dot->setWakePin(WAKE); 00368 } 00369 00370 logInfo("%ssleeping until interrupt on %s pin", deepsleep ? "deep" : "", deepsleep ? "WAKE" : mDot::pinName2Str(dot->getWakePin()).c_str()); 00371 #else 00372 00373 if (deepsleep) { 00374 // for mDot, XBEE_DIO7 pin is the only pin that can wake the processor from deepsleep 00375 // it is automatically configured when INTERRUPT or RTC_ALARM_OR_INTERRUPT is the wakeup source and deepsleep is true in the mDot::sleep call 00376 } else { 00377 // configure XBEE_DIO7 pin as the pin that will wake the mDot from low power modes 00378 // other pins can be confgured instead: XBEE_DIO2-6, XBEE_DI8, XBEE_DIN 00379 dot->setWakePin(XBEE_DIO7); 00380 } 00381 00382 logInfo("%ssleeping until interrupt on %s pin", deepsleep ? "deep" : "", deepsleep ? "DIO7" : mDot::pinName2Str(dot->getWakePin()).c_str()); 00383 #endif 00384 00385 logInfo("application will %s after waking up", deepsleep ? "execute from beginning" : "resume"); 00386 00387 // lowest current consumption in sleep mode can only be achieved by configuring IOs as analog inputs with no pull resistors 00388 // the library handles all internal IOs automatically, but the external IOs are the application's responsibility 00389 // certain IOs may require internal pullup or pulldown resistors because leaving them floating would cause extra current consumption 00390 // for xDot: UART_*, I2C_*, SPI_*, GPIO*, WAKE 00391 // for mDot: XBEE_*, USBTX, USBRX, PB_0, PB_1 00392 // steps are: 00393 // * save IO configuration 00394 // * configure IOs to reduce current consumption 00395 // * sleep 00396 // * restore IO configuration 00397 if (! deepsleep) { 00398 // save the GPIO state. 00399 sleep_save_io(); 00400 00401 // configure GPIOs for lowest current 00402 sleep_configure_io(); 00403 } 00404 00405 // go to sleep/deepsleep and wake on rising edge of configured wake pin (only the WAKE pin in deepsleep) 00406 // since we're not waking on the RTC alarm, the interval is ignored 00407 dot->sleep(0, mDot::INTERRUPT, deepsleep); 00408 00409 if (! deepsleep) { 00410 // restore the GPIO state. 00411 sleep_restore_io(); 00412 } 00413 } 00414 00415 void sleep_wake_rtc_or_interrupt(bool deepsleep) { 00416 // in some frequency bands we need to wait until another channel is available before transmitting again 00417 // wait at least 10s between transmissions 00418 uint32_t delay_s = dot->getNextTxMs() / 1000; 00419 if (delay_s < 10) { 00420 delay_s = 10; 00421 } 00422 00423 #if defined (TARGET_XDOT_L151CC) 00424 if (deepsleep) { 00425 // for xDot, WAKE pin (connected to S2 on xDot-DK) is the only pin that can wake the processor from deepsleep 00426 // it is automatically configured when INTERRUPT or RTC_ALARM_OR_INTERRUPT is the wakeup source and deepsleep is true in the mDot::sleep call 00427 } else { 00428 // configure WAKE pin (connected to S2 on xDot-DK) as the pin that will wake the xDot from low power modes 00429 // other pins can be confgured instead: GPIO0-3 or UART_RX 00430 dot->setWakePin(WAKE); 00431 } 00432 00433 logInfo("%ssleeping %lus or until interrupt on %s pin", deepsleep ? "deep" : "", delay_s, deepsleep ? "WAKE" : mDot::pinName2Str(dot->getWakePin()).c_str()); 00434 #else 00435 if (deepsleep) { 00436 // for mDot, XBEE_DIO7 pin is the only pin that can wake the processor from deepsleep 00437 // it is automatically configured when INTERRUPT or RTC_ALARM_OR_INTERRUPT is the wakeup source and deepsleep is true in the mDot::sleep call 00438 } else { 00439 // configure XBEE_DIO7 pin as the pin that will wake the mDot from low power modes 00440 // other pins can be confgured instead: XBEE_DIO2-6, XBEE_DI8, XBEE_DIN 00441 dot->setWakePin(XBEE_DIO7); 00442 } 00443 00444 logInfo("%ssleeping %lus or until interrupt on %s pin", deepsleep ? "deep" : "", delay_s, deepsleep ? "DIO7" : mDot::pinName2Str(dot->getWakePin()).c_str()); 00445 #endif 00446 00447 logInfo("application will %s after waking up", deepsleep ? "execute from beginning" : "resume"); 00448 00449 // lowest current consumption in sleep mode can only be achieved by configuring IOs as analog inputs with no pull resistors 00450 // the library handles all internal IOs automatically, but the external IOs are the application's responsibility 00451 // certain IOs may require internal pullup or pulldown resistors because leaving them floating would cause extra current consumption 00452 // for xDot: UART_*, I2C_*, SPI_*, GPIO*, WAKE 00453 // for mDot: XBEE_*, USBTX, USBRX, PB_0, PB_1 00454 // steps are: 00455 // * save IO configuration 00456 // * configure IOs to reduce current consumption 00457 // * sleep 00458 // * restore IO configuration 00459 if (! deepsleep) { 00460 // save the GPIO state. 00461 sleep_save_io(); 00462 00463 // configure GPIOs for lowest current 00464 sleep_configure_io(); 00465 } 00466 00467 // 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) 00468 // whichever comes first will wake the xDot 00469 dot->sleep(delay_s, mDot::RTC_ALARM_OR_INTERRUPT, deepsleep); 00470 00471 if (! deepsleep) { 00472 // restore the GPIO state. 00473 sleep_restore_io(); 00474 } 00475 } 00476 00477 void sleep_save_io() { 00478 #if defined(TARGET_XDOT_L151CC) 00479 xdot_save_gpio_state(); 00480 #else 00481 portA[0] = GPIOA->MODER; 00482 portA[1] = GPIOA->OTYPER; 00483 portA[2] = GPIOA->OSPEEDR; 00484 portA[3] = GPIOA->PUPDR; 00485 portA[4] = GPIOA->AFR[0]; 00486 portA[5] = GPIOA->AFR[1]; 00487 00488 portB[0] = GPIOB->MODER; 00489 portB[1] = GPIOB->OTYPER; 00490 portB[2] = GPIOB->OSPEEDR; 00491 portB[3] = GPIOB->PUPDR; 00492 portB[4] = GPIOB->AFR[0]; 00493 portB[5] = GPIOB->AFR[1]; 00494 00495 portC[0] = GPIOC->MODER; 00496 portC[1] = GPIOC->OTYPER; 00497 portC[2] = GPIOC->OSPEEDR; 00498 portC[3] = GPIOC->PUPDR; 00499 portC[4] = GPIOC->AFR[0]; 00500 portC[5] = GPIOC->AFR[1]; 00501 00502 portD[0] = GPIOD->MODER; 00503 portD[1] = GPIOD->OTYPER; 00504 portD[2] = GPIOD->OSPEEDR; 00505 portD[3] = GPIOD->PUPDR; 00506 portD[4] = GPIOD->AFR[0]; 00507 portD[5] = GPIOD->AFR[1]; 00508 00509 portH[0] = GPIOH->MODER; 00510 portH[1] = GPIOH->OTYPER; 00511 portH[2] = GPIOH->OSPEEDR; 00512 portH[3] = GPIOH->PUPDR; 00513 portH[4] = GPIOH->AFR[0]; 00514 portH[5] = GPIOH->AFR[1]; 00515 #endif 00516 } 00517 00518 void sleep_configure_io() { 00519 #if defined(TARGET_XDOT_L151CC) 00520 // GPIO Ports Clock Enable 00521 __GPIOA_CLK_ENABLE(); 00522 __GPIOB_CLK_ENABLE(); 00523 __GPIOC_CLK_ENABLE(); 00524 __GPIOH_CLK_ENABLE(); 00525 00526 GPIO_InitTypeDef GPIO_InitStruct; 00527 00528 // UART1_TX, UART1_RTS & UART1_CTS to analog nopull - RX could be a wakeup source 00529 GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_11 | GPIO_PIN_12; 00530 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; 00531 GPIO_InitStruct.Pull = GPIO_NOPULL; 00532 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 00533 00534 // I2C_SDA & I2C_SCL to analog nopull 00535 GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9; 00536 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; 00537 GPIO_InitStruct.Pull = GPIO_NOPULL; 00538 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); 00539 00540 // SPI_MOSI, SPI_MISO, SPI_SCK, & SPI_NSS to analog nopull 00541 GPIO_InitStruct.Pin = GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15; 00542 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; 00543 GPIO_InitStruct.Pull = GPIO_NOPULL; 00544 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); 00545 00546 // iterate through potential wake pins - leave the configured wake pin alone if one is needed 00547 if (dot->getWakePin() != WAKE || dot->getWakeMode() == mDot::RTC_ALARM) { 00548 GPIO_InitStruct.Pin = GPIO_PIN_0; 00549 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; 00550 GPIO_InitStruct.Pull = GPIO_NOPULL; 00551 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 00552 } 00553 if (dot->getWakePin() != GPIO0 || dot->getWakeMode() == mDot::RTC_ALARM) { 00554 GPIO_InitStruct.Pin = GPIO_PIN_4; 00555 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; 00556 GPIO_InitStruct.Pull = GPIO_NOPULL; 00557 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 00558 } 00559 if (dot->getWakePin() != GPIO1 || dot->getWakeMode() == mDot::RTC_ALARM) { 00560 GPIO_InitStruct.Pin = GPIO_PIN_5; 00561 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; 00562 GPIO_InitStruct.Pull = GPIO_NOPULL; 00563 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 00564 } 00565 if (dot->getWakePin() != GPIO2 || dot->getWakeMode() == mDot::RTC_ALARM) { 00566 GPIO_InitStruct.Pin = GPIO_PIN_0; 00567 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; 00568 GPIO_InitStruct.Pull = GPIO_NOPULL; 00569 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); 00570 } 00571 if (dot->getWakePin() != GPIO3 || dot->getWakeMode() == mDot::RTC_ALARM) { 00572 GPIO_InitStruct.Pin = GPIO_PIN_2; 00573 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; 00574 GPIO_InitStruct.Pull = GPIO_NOPULL; 00575 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); 00576 } 00577 if (dot->getWakePin() != UART1_RX || dot->getWakeMode() == mDot::RTC_ALARM) { 00578 GPIO_InitStruct.Pin = GPIO_PIN_10; 00579 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; 00580 GPIO_InitStruct.Pull = GPIO_NOPULL; 00581 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 00582 } 00583 #else 00584 /* GPIO Ports Clock Enable */ 00585 __GPIOA_CLK_ENABLE(); 00586 __GPIOB_CLK_ENABLE(); 00587 __GPIOC_CLK_ENABLE(); 00588 00589 GPIO_InitTypeDef GPIO_InitStruct; 00590 00591 // XBEE_DOUT, XBEE_DIN, XBEE_DO8, XBEE_RSSI, USBTX, USBRX, PA_12, PA_13, PA_14 & PA_15 to analog nopull 00592 GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_6 | GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 00593 | GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15; 00594 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; 00595 GPIO_InitStruct.Pull = GPIO_NOPULL; 00596 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 00597 00598 // PB_0, PB_1, PB_3 & PB_4 to analog nopull 00599 GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_3 | GPIO_PIN_4; 00600 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; 00601 GPIO_InitStruct.Pull = GPIO_NOPULL; 00602 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); 00603 00604 // PC_9 & PC_13 to analog nopull 00605 GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_13; 00606 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; 00607 GPIO_InitStruct.Pull = GPIO_NOPULL; 00608 HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); 00609 00610 // iterate through potential wake pins - leave the configured wake pin alone if one is needed 00611 // XBEE_DIN - PA3 00612 // XBEE_DIO2 - PA5 00613 // XBEE_DIO3 - PA4 00614 // XBEE_DIO4 - PA7 00615 // XBEE_DIO5 - PC1 00616 // XBEE_DIO6 - PA1 00617 // XBEE_DIO7 - PA0 00618 // XBEE_SLEEPRQ - PA11 00619 00620 if (dot->getWakePin() != XBEE_DIN || dot->getWakeMode() == mDot::RTC_ALARM) { 00621 GPIO_InitStruct.Pin = GPIO_PIN_3; 00622 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; 00623 GPIO_InitStruct.Pull = GPIO_NOPULL; 00624 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 00625 } 00626 00627 if (dot->getWakePin() != XBEE_DIO2 || dot->getWakeMode() == mDot::RTC_ALARM) { 00628 GPIO_InitStruct.Pin = GPIO_PIN_5; 00629 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; 00630 GPIO_InitStruct.Pull = GPIO_NOPULL; 00631 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 00632 } 00633 00634 if (dot->getWakePin() != XBEE_DIO3 || dot->getWakeMode() == mDot::RTC_ALARM) { 00635 GPIO_InitStruct.Pin = GPIO_PIN_4; 00636 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; 00637 GPIO_InitStruct.Pull = GPIO_NOPULL; 00638 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 00639 } 00640 00641 if (dot->getWakePin() != XBEE_DIO4 || dot->getWakeMode() == mDot::RTC_ALARM) { 00642 GPIO_InitStruct.Pin = GPIO_PIN_7; 00643 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; 00644 GPIO_InitStruct.Pull = GPIO_NOPULL; 00645 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 00646 } 00647 00648 if (dot->getWakePin() != XBEE_DIO5 || dot->getWakeMode() == mDot::RTC_ALARM) { 00649 GPIO_InitStruct.Pin = GPIO_PIN_1; 00650 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; 00651 GPIO_InitStruct.Pull = GPIO_NOPULL; 00652 HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); 00653 } 00654 00655 if (dot->getWakePin() != XBEE_DIO6 || dot->getWakeMode() == mDot::RTC_ALARM) { 00656 GPIO_InitStruct.Pin = GPIO_PIN_1; 00657 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; 00658 GPIO_InitStruct.Pull = GPIO_NOPULL; 00659 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 00660 } 00661 00662 if (dot->getWakePin() != XBEE_DIO7 || dot->getWakeMode() == mDot::RTC_ALARM) { 00663 GPIO_InitStruct.Pin = GPIO_PIN_0; 00664 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; 00665 GPIO_InitStruct.Pull = GPIO_NOPULL; 00666 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 00667 } 00668 00669 if (dot->getWakePin() != XBEE_SLEEPRQ|| dot->getWakeMode() == mDot::RTC_ALARM) { 00670 GPIO_InitStruct.Pin = GPIO_PIN_11; 00671 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; 00672 GPIO_InitStruct.Pull = GPIO_NOPULL; 00673 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 00674 } 00675 #endif 00676 } 00677 00678 void sleep_restore_io() { 00679 #if defined(TARGET_XDOT_L151CC) 00680 xdot_restore_gpio_state(); 00681 #else 00682 GPIOA->MODER = portA[0]; 00683 GPIOA->OTYPER = portA[1]; 00684 GPIOA->OSPEEDR = portA[2]; 00685 GPIOA->PUPDR = portA[3]; 00686 GPIOA->AFR[0] = portA[4]; 00687 GPIOA->AFR[1] = portA[5]; 00688 00689 GPIOB->MODER = portB[0]; 00690 GPIOB->OTYPER = portB[1]; 00691 GPIOB->OSPEEDR = portB[2]; 00692 GPIOB->PUPDR = portB[3]; 00693 GPIOB->AFR[0] = portB[4]; 00694 GPIOB->AFR[1] = portB[5]; 00695 00696 GPIOC->MODER = portC[0]; 00697 GPIOC->OTYPER = portC[1]; 00698 GPIOC->OSPEEDR = portC[2]; 00699 GPIOC->PUPDR = portC[3]; 00700 GPIOC->AFR[0] = portC[4]; 00701 GPIOC->AFR[1] = portC[5]; 00702 00703 GPIOD->MODER = portD[0]; 00704 GPIOD->OTYPER = portD[1]; 00705 GPIOD->OSPEEDR = portD[2]; 00706 GPIOD->PUPDR = portD[3]; 00707 GPIOD->AFR[0] = portD[4]; 00708 GPIOD->AFR[1] = portD[5]; 00709 00710 GPIOH->MODER = portH[0]; 00711 GPIOH->OTYPER = portH[1]; 00712 GPIOH->OSPEEDR = portH[2]; 00713 GPIOH->PUPDR = portH[3]; 00714 GPIOH->AFR[0] = portH[4]; 00715 GPIOH->AFR[1] = portH[5]; 00716 #endif 00717 } 00718 00719 int send_data(std::vector<uint8_t> data, Serial *pc) { 00720 int32_t ret; 00721 00722 ret = dot->send(data); 00723 if (ret != mDot::MDOT_OK) { 00724 (*pc).printf("s_d(): failed to send data to %s [%d][%s]\r\n", dot->getJoinMode() == mDot::PEER_TO_PEER ? "peer\r\n" : "gateway\r\n", ret, mDot::getReturnCodeString(ret).c_str()); 00725 00726 return 1; 00727 } else { 00728 (*pc).printf("successfully sent data to %s", dot->getJoinMode() == mDot::PEER_TO_PEER ? "peer\r\n" : "gateway\r\n"); 00729 00730 return 0; 00731 } 00732 } 00733 00734 uint8_t get_light_data(TSL2561 *sensor, Serial *pc) { 00735 00736 if(sensor->who_am_i()) 00737 pc->printf("\r\nI'm a light sensor!\r\n"); 00738 else 00739 pc->printf("\r\nI'm not a light sensor.\r\n"); 00740 00741 00742 float raw_data = sensor->lux(); 00743 pc->printf("raw data: %f\r\n", raw_data); 00744 00745 return (uint8_t)raw_data; 00746 00747 //return (uint8_t)(rand() % 100); 00748 } 00749 00750 void ascii_converter(char *ret, uint8_t numData, std::vector<uint8_t> *tx_data, Serial *pc) { 00751 00752 sprintf(ret, "%d", numData); 00753 //pc->printf("char buffer: %s\r\n", ret); 00754 00755 int i = 0; 00756 while(ret[i] != '\0' || ret[i] == 'X') { 00757 //pc->printf("%c ", ret[i]); 00758 tx_data->push_back(ret[i++]); 00759 //pc->printf("%c", ret[i - 1]); 00760 } 00761 00762 //pc->printf("\r\n"); 00763 }
Generated on Tue Jul 12 2022 18:10:22 by
