Semtech / mDot_test

Dependencies:   libmDot mbed-rtos mbed-src

Fork of mDot_test_rx by Joe Knapp

Committer:
jknapp_smtc
Date:
Thu Jul 30 19:02:09 2015 +0000
Revision:
3:7cc5dfd22b54
Parent:
2:aadbdfb6d517
Child:
4:ee6bf074135c
Checking for SMTC System

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mfiore 0:e17e5a07892d 1 #include "mbed.h"
mfiore 0:e17e5a07892d 2 #include "mDot.h"
mfiore 0:e17e5a07892d 3 #include "MTSLog.h"
mfiore 0:e17e5a07892d 4 #include "MTSText.h"
mfiore 0:e17e5a07892d 5 #include <string>
mfiore 0:e17e5a07892d 6 #include <vector>
mfiore 0:e17e5a07892d 7
mfiore 0:e17e5a07892d 8 using namespace mts;
mfiore 0:e17e5a07892d 9
jknapp_smtc 3:7cc5dfd22b54 10 static const uint8_t DEVKEY[16] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF }; // Normal
jknapp_smtc 3:7cc5dfd22b54 11 static const uint8_t APPEUI[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // SMTC AppEUI
jknapp_smtc 3:7cc5dfd22b54 12
jknapp_smtc 3:7cc5dfd22b54 13 //static const uint8_t DEVKEY[16] = { 0x03, 0x24, 0x22, 0x60, 0xA6, 0xC6, 0x98, 0x5F, 0xD5, 0x02, 0x23, 0xD0, 0x53, 0x00, 0x49, 0xF6 }; // Normal
jknapp_smtc 3:7cc5dfd22b54 14 //static const uint8_t APPEUI[8] = { 0x00, 0x25, 0x0C, 0x00, 0x00, 0x01, 0x00, 0x01 }; // Senet AppEUI
jknapp_smtc 3:7cc5dfd22b54 15 //static const uint8_t APPEUI[8] = { 0x01, 0x00, 0x01, 0x00, 0x00, 0x0C, 0x25, 0x00 }; // Senet AppEUI Swapped
jknapp_smtc 3:7cc5dfd22b54 16
jknapp_smtc 3:7cc5dfd22b54 17 static std::vector<uint8_t> AppKey (DEVKEY, DEVKEY + sizeof(DEVKEY)/sizeof(uint8_t) );
jknapp_smtc 3:7cc5dfd22b54 18 static std::vector<uint8_t> AppEUI (APPEUI, APPEUI + sizeof(APPEUI)/sizeof(uint8_t) );
jknapp_smtc 3:7cc5dfd22b54 19 static uint8_t config_frequency_sub_band = 0; // 0 = Enable all channels, 1 = 1st eight channels
jknapp_smtc 3:7cc5dfd22b54 20
jknapp_smtc 3:7cc5dfd22b54 21 InterruptIn button(PB_1);
jknapp_smtc 3:7cc5dfd22b54 22 DigitalOut drive_high(PB_0);
jknapp_smtc 3:7cc5dfd22b54 23 DigitalIn door_open(PA_7);
jknapp_smtc 3:7cc5dfd22b54 24
jknapp_smtc 3:7cc5dfd22b54 25 volatile bool msg_rdy;
jknapp_smtc 3:7cc5dfd22b54 26
jknapp_smtc 3:7cc5dfd22b54 27 void queue_message(){
jknapp_smtc 3:7cc5dfd22b54 28 msg_rdy = true;
jknapp_smtc 3:7cc5dfd22b54 29 }
jknapp_smtc 3:7cc5dfd22b54 30
mfiore 0:e17e5a07892d 31
mfiore 0:e17e5a07892d 32 int main() {
mfiore 0:e17e5a07892d 33 Serial debug(USBTX, USBRX);
mfiore 0:e17e5a07892d 34 debug.baud(460800);
jknapp_smtc 3:7cc5dfd22b54 35 drive_high = 1;
mfiore 0:e17e5a07892d 36
jknapp_smtc 3:7cc5dfd22b54 37 msg_rdy = false;
mfiore 0:e17e5a07892d 38 int32_t ret;
mfiore 0:e17e5a07892d 39 int32_t next_tx;
mfiore 2:aadbdfb6d517 40 int32_t wait_time = 2;
mfiore 0:e17e5a07892d 41 mDot* dot;
mfiore 0:e17e5a07892d 42 std::vector<uint8_t> send_data;
mfiore 0:e17e5a07892d 43 std::vector<uint8_t> recv_data;
mfiore 0:e17e5a07892d 44 uint8_t recv = 0;
mfiore 0:e17e5a07892d 45 uint8_t recv_mismatch = 0;
mfiore 0:e17e5a07892d 46 uint8_t send_failure = 0;
mfiore 0:e17e5a07892d 47 uint8_t iterations = 50;
jknapp_smtc 3:7cc5dfd22b54 48 button.rise(&queue_message);
mfiore 0:e17e5a07892d 49
jknapp_smtc 3:7cc5dfd22b54 50 send_data.push_back(0x01);
jknapp_smtc 3:7cc5dfd22b54 51 send_data.push_back(0x02);
mfiore 0:e17e5a07892d 52
mfiore 0:e17e5a07892d 53 dot = mDot::getInstance();
mfiore 0:e17e5a07892d 54
mfiore 0:e17e5a07892d 55 dot->resetConfig();
mfiore 1:8295b8c0d802 56
mfiore 1:8295b8c0d802 57 dot->setLogLevel(MTSLog::TRACE_LEVEL);
jknapp_smtc 3:7cc5dfd22b54 58 //dot->setJoinByteOrder(1); // MSB
jknapp_smtc 3:7cc5dfd22b54 59 dot->setTxDataRate(mDot::SF_10);
jknapp_smtc 3:7cc5dfd22b54 60 //dot->setTxPower(20);
jknapp_smtc 3:7cc5dfd22b54 61
mfiore 0:e17e5a07892d 62 while ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
mfiore 0:e17e5a07892d 63 logError("failed to set frequency sub band: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 0:e17e5a07892d 64 }
jknapp_smtc 3:7cc5dfd22b54 65 while ((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) {
jknapp_smtc 3:7cc5dfd22b54 66 logError("failed to set Public Network: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 0:e17e5a07892d 67 }
jknapp_smtc 3:7cc5dfd22b54 68 /** Set network ID
jknapp_smtc 3:7cc5dfd22b54 69 * for use with OTA & AUTO_OTA network join modes
jknapp_smtc 3:7cc5dfd22b54 70 * setting network ID via this function sets network name to empty
jknapp_smtc 3:7cc5dfd22b54 71 * @param id a vector of 8 bytes
jknapp_smtc 3:7cc5dfd22b54 72 * @returns MDOT_OK if success
jknapp_smtc 3:7cc5dfd22b54 73 */
jknapp_smtc 3:7cc5dfd22b54 74 while ((ret = dot->setNetworkId(AppEUI)) != mDot::MDOT_OK) {
jknapp_smtc 3:7cc5dfd22b54 75 logError("failed to set AppEUI: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
jknapp_smtc 3:7cc5dfd22b54 76 }
jknapp_smtc 3:7cc5dfd22b54 77 while ((ret = dot->setNetworkKey(AppKey)) != mDot::MDOT_OK) {
jknapp_smtc 3:7cc5dfd22b54 78 logError("failed to set AppEUI: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 0:e17e5a07892d 79 }
mfiore 0:e17e5a07892d 80
mfiore 2:aadbdfb6d517 81 logInfo("enabling activity LED");
mfiore 2:aadbdfb6d517 82 dot->setActivityLedEnable(true);
mfiore 2:aadbdfb6d517 83
mfiore 0:e17e5a07892d 84 logInfo("joining network");
mfiore 0:e17e5a07892d 85 while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
mfiore 0:e17e5a07892d 86 logError("failed to join network: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 0:e17e5a07892d 87 wait_ms(dot->getNextTxMs() + 1);
mfiore 0:e17e5a07892d 88 }
mfiore 0:e17e5a07892d 89 logInfo("joined");
mfiore 0:e17e5a07892d 90
jknapp_smtc 3:7cc5dfd22b54 91 dot->setAck(3); // Use Confirmed frames and try three times
jknapp_smtc 3:7cc5dfd22b54 92
jknapp_smtc 3:7cc5dfd22b54 93 while (1) {
jknapp_smtc 3:7cc5dfd22b54 94
jknapp_smtc 3:7cc5dfd22b54 95 send_data[0] = 192; //door_open;
mfiore 0:e17e5a07892d 96 if ((ret = dot->send(send_data)) != mDot::MDOT_OK) {
mfiore 0:e17e5a07892d 97 logError("failed to send: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 0:e17e5a07892d 98 send_failure++;
mfiore 0:e17e5a07892d 99 } else {
mfiore 0:e17e5a07892d 100 logInfo("send data: %s", Text::bin2hexString(send_data).c_str());
mfiore 0:e17e5a07892d 101 if ((ret = dot->recv(recv_data)) != mDot::MDOT_OK) {
mfiore 0:e17e5a07892d 102 logError("failed to recv: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 0:e17e5a07892d 103 } else {
mfiore 0:e17e5a07892d 104 logInfo("recv data: %s", Text::bin2hexString(recv_data).c_str());
jknapp_smtc 3:7cc5dfd22b54 105 if (recv_data[0] == 0x80) {
mfiore 0:e17e5a07892d 106 recv++;
jknapp_smtc 3:7cc5dfd22b54 107 } else if (recv_data[0] == 0xFF) {
jknapp_smtc 3:7cc5dfd22b54 108 goto END;
mfiore 0:e17e5a07892d 109 } else {
mfiore 0:e17e5a07892d 110 recv_mismatch++;
mfiore 0:e17e5a07892d 111 }
mfiore 0:e17e5a07892d 112 }
mfiore 0:e17e5a07892d 113 recv_data.clear();
mfiore 0:e17e5a07892d 114 }
jknapp_smtc 3:7cc5dfd22b54 115 msg_rdy = false;
jknapp_smtc 3:7cc5dfd22b54 116 while (!msg_rdy) wait(wait_time);
mfiore 0:e17e5a07892d 117 }
jknapp_smtc 3:7cc5dfd22b54 118 END:
mfiore 0:e17e5a07892d 119 logInfo("Version: %s", dot->getId().c_str());
mfiore 0:e17e5a07892d 120 logInfo("Recv: %d/%d", recv, iterations);
mfiore 0:e17e5a07892d 121 logInfo("Recv Mismatch: %d/%d", recv_mismatch, iterations);
mfiore 0:e17e5a07892d 122 logInfo("Send Failure: %d/%d", send_failure, iterations);
mfiore 0:e17e5a07892d 123 logInfo("Dropped: %d/%d", iterations - (recv + recv_mismatch + send_failure), iterations);
mfiore 0:e17e5a07892d 124
mfiore 0:e17e5a07892d 125 return 0;
mfiore 0:e17e5a07892d 126 }