Basic xdot code to check how many memory is available for user APP after initializing libxdot lorawan stack

Committer:
jose_23991
Date:
Wed Jul 07 11:34:06 2021 +0000
Revision:
43:97fd5b4de956
Basic xDot Bootlaoder+ABP+FUOTA code to check available memory for user app

Who changed what in which revision?

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