Here it is ...

Dependencies:   libxDot-mbed5 TSL2561

Fork of Dot-Examples by MultiTech

Committer:
David Gutsch dgutsch@umail.iu.edu
Date:
Mon Jul 30 08:51:08 2018 -0400
Revision:
40:314e6b1c289f
Parent:
39:0d4362063352
refactored for boy scout demo. Gain is being set to high (16x) too.

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