Text menu driven ANSI/VT100 console test utility for LoRa transceivers

radio chip selection

Radio chip driver is not included, allowing choice of radio device.
If you're using SX1272 or SX1276, then import sx127x driver into your program.
if you're using SX1261 or SX1262, then import sx126x driver into your program.
if you're using SX1280, then import sx1280 driver into your program.
if you're using LR1110, then import LR1110 driver into your program.
If you're using NAmote72 or Murata discovery, then you must import only sx127x driver.
If you're using Type1SJ select target DISCO_L072CZ_LRWAN1 and import sx126x driver into your program.

This is VT100 text-based menu driven test program for SX12xx transceiver devices.
Serial console is divided into horizontally into top half and bottom half.
The bottom half serves as scrolling area to log activity.
The top half serves as menu, to configure the radio.
For all devices, the serial console operates at 115200 8N1, and requires terminal with ANSI-VT100 capability, such as putty/teraterm/minicom etc.
Use program only with keyboard up/down/left/right keys. Enter to change an item, or number for value item. Some items are single bit, requiring only enter key to toggle. Others with fixed choices give a drop-down menu.

Revision:
2:ea9245bb1c53
Parent:
1:0817a150122b
Child:
3:56fc764dee0a
--- a/radio_sx126x.cpp	Mon Aug 20 18:13:09 2018 -0700
+++ b/radio_sx126x.cpp	Wed Aug 22 09:50:32 2018 -0700
@@ -44,6 +44,7 @@
 LowPowerTimer Radio::lpt;
 uint8_t Radio::pktType;
 uint8_t Radio::bw_idx;
+uint8_t Radio::cadParams[7];
 
 const char* opModes[] = {
     "SLEEP    ", // 0
@@ -158,6 +159,14 @@
         pa_config_buf[3] = 1;   // paLut
     }
 
+    {
+        cadParams[0] = radio.readReg(REG_ADDR_LORA_CONFIG9, 1);
+        cadParams[0] >>= 5;
+
+        cadParams[1] = radio.readReg(REG_ADDR_LORA_CAD_PN_RATIO, 1);
+        cadParams[2] = radio.readReg(REG_ADDR_LORA_CAD_MINPEAK, 1);
+    }
+
 }
 
 void Radio::hw_reset()
@@ -209,7 +218,7 @@
     NULL
 };
 
-unsigned Radio::opmode_read_cb(bool forWriting)
+unsigned Radio::opmode_read(bool forWriting)
 {
     status_t status;
     radio.xfer(OPCODE_GET_STATUS, 0, 1, &status.octet);
@@ -228,7 +237,7 @@
         return status.bits.chipMode;
 }
 
-menuMode_e Radio::opmode_write_cb(unsigned sel)
+menuMode_e Radio::opmode_write(unsigned sel)
 {
     switch (sel) {
         case 0:
@@ -277,12 +286,12 @@
     NULL
 };
 
-unsigned Radio::pktType_read_cb(bool fw)
+unsigned Radio::pktType_read(bool fw)
 {
     return radio.getPacketType();
 }
 
-menuMode_e Radio::pktType_write_cb(unsigned idx)
+menuMode_e Radio::pktType_write(unsigned idx)
 {
     radio.setPacketType(idx);
     return MENUMODE_REINIT_MENU;
@@ -368,7 +377,7 @@
     NULL
 };
 
-unsigned Radio::tx_ramp_read_cb(bool fw)
+unsigned Radio::tx_ramp_read(bool fw)
 {
     PwrCtrl_t PwrCtrl;
     PwrCtrl.octet = radio.readReg(REG_ADDR_PWR_CTRL, 1);
@@ -376,7 +385,7 @@
     return PwrCtrl.bits.ramp_time;
 }
 
-menuMode_e Radio::tx_ramp_write_cb(unsigned sidx)
+menuMode_e Radio::tx_ramp_write(unsigned sidx)
 {
     tx_param_buf[1] = sidx;
     radio.xfer(OPCODE_SET_TX_PARAMS, 2, 0, tx_param_buf);
@@ -543,6 +552,15 @@
         RadioEvents->TxDone_botHalf();
 }
 
+void Radio::cadDone(bool det)
+{
+    log_printf("cadDone ");
+    if (det)
+        pc.printf("CadDetected");
+
+    pc.printf("\r\n");
+}
+
 uint8_t ana_regs[128];
 
 void Radio::boardInit(const RadioEvents_t* e)
@@ -551,6 +569,7 @@
 
     radio.txDone = txDoneBottom;
     radio.rxDone = rxDone;
+    radio.cadDone = cadDone;
 
     radio.chipModeChange = chipModeChange;
 
@@ -858,6 +877,141 @@
 
 const toggle_item_t Radio::lora_inviq_item = { _ITEM_TOGGLE, "InvertIQ", NULL, lora_inviq_read, lora_inviq_push};
 
+void Radio::cad_push()
+{
+    {
+        uint8_t buf[8];
+        IrqFlags_t irqEnable;
+        irqEnable.word = 0;
+        irqEnable.bits.RxDone = 1;
+        irqEnable.bits.Timeout = 1;
+        irqEnable.bits.CadDetected = 1;
+        irqEnable.bits.CadDone = 1;
+
+        buf[0] = 3;//irqEnable.word >> 8;    // enable bits
+        buf[1] = 0xff;//irqEnable.word; // enable bits
+        buf[2] = irqEnable.word >> 8;     // dio1
+        buf[3] = irqEnable.word;  // dio1
+        buf[4] = 0; // dio2
+        buf[5] = 0; // dio2
+        buf[6] = 0; // dio3
+        buf[7] = 0; // dio3
+        radio.xfer(OPCODE_SET_DIO_IRQ_PARAMS, 8, 0, buf);
+    }
+
+    radio.setCAD();
+}
+
+const button_item_t Radio::lora_cad_item = { _ITEM_BUTTON, "CAD", cad_push };
+
+static const char* lora_cadsymbs[] = {
+    " 1",
+    " 2",
+    " 4",
+    " 8",
+    "16",
+    NULL
+};
+
+unsigned Radio::lora_cadsymbs_read(bool forWriting)
+{
+    cadParams[0] = radio.readReg(REG_ADDR_LORA_CONFIG9, 1);
+    cadParams[0] >>= 5;
+    return cadParams[0];
+}
+
+menuMode_e Radio::lora_cadsymbs_write(unsigned sidx)
+{
+    cadParams[0] = sidx;
+    radio.xfer(OPCODE_SET_CAD_PARAM, 7, 0, cadParams);
+    return MENUMODE_REDRAW;
+}
+
+const dropdown_item_t Radio::lora_cadsymbs_item = { _ITEM_DROPDOWN, lora_cadsymbs, lora_cadsymbs, lora_cadsymbs_read, lora_cadsymbs_write};
+
+void Radio::lora_cadpnratio_print()
+{
+    cadParams[1] = radio.readReg(REG_ADDR_LORA_CAD_PN_RATIO, 1);
+    pc.printf("%u", cadParams[1]);
+}
+
+bool Radio::lora_cadpnratio_write(const char* txt)
+{
+    unsigned n;
+    sscanf(txt, "%u", &n);
+    cadParams[1] = n;
+    radio.xfer(OPCODE_SET_CAD_PARAM, 7, 0, cadParams);
+    return false;
+}
+
+const value_item_t Radio::lora_cadpnratio_item = { _ITEM_VALUE, 4, lora_cadpnratio_print, lora_cadpnratio_write};
+
+void Radio::lora_cadmin_print()
+{
+    cadParams[2] = radio.readReg(REG_ADDR_LORA_CAD_MINPEAK, 1);
+    pc.printf("%u", cadParams[2]);
+}
+
+bool Radio::lora_cadmin_write(const char* txt)
+{
+    unsigned n;
+    sscanf(txt, "%u", &n);
+    cadParams[2] = n;
+    radio.xfer(OPCODE_SET_CAD_PARAM, 7, 0, cadParams);
+    return false;
+}
+
+const value_item_t Radio::lora_cadmin_item = { _ITEM_VALUE, 4, lora_cadmin_print, lora_cadmin_write};
+
+bool Radio::lora_cadexit_read()
+{
+    return cadParams[3];
+}
+
+bool Radio::lora_cadexit_push()
+{
+    if (cadParams[3])
+        cadParams[3] = 0;
+    else
+        cadParams[3] = 1;
+
+    radio.xfer(OPCODE_SET_CAD_PARAM, 7, 0, cadParams);
+
+    return cadParams[3];
+}
+
+const toggle_item_t Radio::lora_cadexit_item = { _ITEM_TOGGLE, "CAD_ONLY", "CAD_RX  ", lora_cadexit_read, lora_cadexit_push};
+
+void Radio::lora_cadtimeout_print(void)
+{
+    unsigned n;
+
+    n = cadParams[4];
+    n <<= 8;
+    n += cadParams[5];
+    n <<= 8;
+    n += cadParams[6];
+    pc.printf("%u", n);
+}
+
+bool Radio::lora_cadtimeout_write(const char* txt)
+{
+    unsigned n;
+    float ticks;
+
+    sscanf(txt, "%u", &n);
+    ticks = n / 15.625;
+    n = ticks;
+
+    cadParams[4] = n >> 16;
+    cadParams[5] = n >> 8;
+    cadParams[6] = n;
+
+    return false;
+}
+
+const value_item_t Radio::lora_cadtimeout_item = { _ITEM_VALUE, 4, lora_cadtimeout_print, lora_cadtimeout_write};
+
 const menu_t Radio::lora_menu[] = {
     { {FIRST_CHIP_MENU_ROW+1,  1},   NULL,      &lora_bw_item, FLAG_MSGTYPE_ALL },
     { {FIRST_CHIP_MENU_ROW+1, 12},  "sf:",      &lora_sf_item, FLAG_MSGTYPE_ALL },
@@ -868,6 +1022,14 @@
     { {FIRST_CHIP_MENU_ROW+2, 22},              NULL, &lora_headerType_item, FLAG_MSGTYPE_ALL },
     { {FIRST_CHIP_MENU_ROW+2, 32},              NULL, &lora_crcon_item, FLAG_MSGTYPE_ALL },
     { {FIRST_CHIP_MENU_ROW+2, 39},              NULL, &lora_inviq_item, FLAG_MSGTYPE_ALL },
+
+    { {FIRST_CHIP_MENU_ROW+3,  1},          NULL,        &lora_cad_item, FLAG_MSGTYPE_ALL },
+    { {FIRST_CHIP_MENU_ROW+3,  5},    "symbols:",   &lora_cadsymbs_item, FLAG_MSGTYPE_ALL },
+    { {FIRST_CHIP_MENU_ROW+3, 20}, "peak/noise:", &lora_cadpnratio_item, FLAG_MSGTYPE_ALL },
+    { {FIRST_CHIP_MENU_ROW+3, 35},        "min:",     &lora_cadmin_item, FLAG_MSGTYPE_ALL },
+    { {FIRST_CHIP_MENU_ROW+3, 45},       "exit:",    &lora_cadexit_item, FLAG_MSGTYPE_ALL },
+    { {FIRST_CHIP_MENU_ROW+3, 62}, "timeout us:", &lora_cadtimeout_item, FLAG_MSGTYPE_ALL },
+
     { {0, 0}, NULL, NULL }
 };