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