Here it is ...

Dependencies:   libxDot-mbed5 TSL2561

Fork of Dot-Examples by MultiTech

Committer:
David Gutsch dgutsch@umail.iu.edu
Date:
Thu Jul 12 12:54:47 2018 -0400
Revision:
32:cc05a2e80969
Child:
33:79e4c812d91d
it finally works! swapped the config function, changed join_delay from 5 to 1, and made small changes to pass serial address to different function

Who changed what in which revision?

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