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