Echo example of class C devices receiving downlink and sending as next uplink

Dependencies:   libmDot-mbed5 ISL29011

Committer:
Mike Fiore
Date:
Tue Oct 11 11:49:56 2016 -0500
Revision:
11:d2e31743433a
Parent:
10:4d0b765f7b9e
Child:
15:364df461110f
add peer to peer example

Who changed what in which revision?

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