SCPI interface to SX1272 and SX1276

Dependencies:   SX127x lib_gps lib_mma8451q lib_mpl3115a2 lib_sx9500 libscpi mbed

Description

This program implements a SCPI command parser. When connected via the debug virtual comm port, you can command the radio and peripherals to perform specific tasks or to set specific parameters. The serial port defaults to 9600bps N-8-1.

Example

The following exampling causes the NA Mote to transmit a continuous LoRa signal.

Example SCPI Commands

*IDN?
RA:MOD LORA
RA:FREQ 903
RA:PAS PA_BOOST
RA:OCP 170
RA:BGR 7
RA:POW 15
RA:LORA:TXContinuous ON
RA:FI "test"

Commands

SCPI is case-insensitive. Commands can be entered lower case.

  • RAdio
    • :FIfo "text" write to fifo, and transmit packet
    • :FIfo? read from fifo (last received packet)
    • :REGister { address }, {value} write radio register (SCPI takes #H for hex prefix)
    • :REGister? { address } read radio register (SCPI takes #H for hex prefix)
    • :MODulation { FSK | OOK | LORa } set modem type
    • :MODulation? get modem type
    • :OPmode { SLE | STB | FST | TX | FSR | RXC | RXS | CAD } set mode
    • :OPmode? get mode
    • :DIOMap { 0 - 5 }, { 0 - 3 } set DIO pin function {pin number}, {map value}
    • :DIOMap? { 0 - 5 } get DIO pin function {pin number}
    • :DIO? { 0 - 5 } read DIO pin level
    • :RSSI? read received signal strength (if in RXC mode)
    • :PASelect { RFO | PA_BOOST } set RF output pin
    • :PASelect? get RF output pin selected
    • :OCP {mA} set over current protection
    • :OCP? read current limit
    • :POWer {0 to 15} set OutputPower
    • :POWer? get OutputPower
    • :BGR { 0 - 7 } set PA ref current
    • :BGR? get PA ref current
    • :FREQuency {MHz} set FRF
    • :FREQuency? get FRF
    • :LNABoost { OFF | ON } set LNA boost
    • :LNABoost? get LNA boost
    • :LORa
      • :BW {KHz} set lora bandwidth
      • :BW? get lora bandwidth (in KHz)
      • :SF {7 to 12} set spreading factor
      • :SF? get spreading factor
      • :TXContinuous { OFF | ON } set continuous TX (end transmit by turning off)
      • :TXContinuous? get continuous TX
      • :PRELen {n-symbols} set preamble length
      • :PRELen? get preamble length
      • :CR {0-4} set coding rate
      • :CR? get coding rate
      • :LDRO { OFF | ON } set LowDataRateOptimize bit
      • :LDRO? get LowDataRateOptimize bit
      • :CRC { OFF | ON} enable CRC in transmitted packet
      • :CRC? read CRC enabled state
      • :INVRx { OFF | ON } enable receiver spectral invert
      • :INVRx? get receiver inversion state
      • :INVTx { OFF | ON } enable transmitter spectral invert
      • :INVTx? get transmitter inversion state
      • :FEI? get frequency error of last received packet
      • :PKTSnr? get S/N (dB) of last received packet
    • :FSK
      • :DATAMode { CONT | PKT } select continuous/packet mode
      • :DATAMode? get packet mode / continuous
      • :FDev {Hz} set TX frequency deviation
      • :FDev? get TX frequency deviation
      • :BITRate {bps} set bit rate
      • :BITRate? get bit rate
      • :BT { 1.0 | 0.5 | 0.3 | 0.0 } set transmit shaping
      • :BT? get transmit shaping
      • :PRELen {n} set preamble size
      • :PRELen? get preamble size
      • :RXBW {Hz} set receive bandwidth
      • :RXBW? set receive bandwidth
      • :AFCBW {Hz} set receive bandwidth (during preamble)
      • :AFCBW? set receive bandwidth (during preamble)
      • :DCFree { OFF | MAN | WHIT } set DC-free encoding
      • :DCFree? get DC-free encoding
      • :RXTrigger { OFF | RSSI | PRE | BOTH } set RX trigger mode
      • :RXTrigger? get RX trigger selection

radio events

Radio events are reported into the Questionable Data Status Event Register, which is summarized in Bit 3 (QSB) of Status Byte Register.

enabling event and reading event

RA:MOD?
"LORa"
RA:OP?
"RXC"
STAT:QUES:ENAB 512
*STB?
8
STAT:QUES:EVEN?
512
RA:FI?
"4747474747474747"
*STB?
0

Bit 9 of QUEStionable event register indicates DIO0 event occurred upon radio packet reception.

NA Mote-72 specific commands

  • PD2 { OFF | ON } set power amplifier voltage state
  • PD2? get power amplifier voltage state
  • VBAT? read battery voltage
  • GPS
    • :EN { OFF | ON } enable GPS
    • :EN? get GPS enabled state
    • :NUMCoords {n} set count of coordinates received (use to clear count)
    • :NUMCoords? get count of coordinates received
    • :LOngitude {n} set longitude
    • :LOngitude? get longitude
    • :LAtitude {n} set latitude
    • :LAtitude? get latitude
  • MMA MMA8451Q
    • :ID? read ID register (WHO_AM_I == 0x1a)
  • MPL MPL3115A2
    • :ID? read ID register (WHO_AM_I == 0xc4)
  • SX9500
    • :RST reset SX9500
    • :REGister {addr}, {value} write SX9500 register
    • :REGister? {addr} read SX9500 register
Revision:
5:0a62140cefa4
Parent:
4:23f467ca9934
Child:
6:074e01954263
--- a/scpi-def.cpp	Tue Aug 18 17:12:26 2015 +0000
+++ b/scpi-def.cpp	Thu Aug 20 02:01:45 2015 +0000
@@ -9,6 +9,10 @@
 
 /******************************************************************************/
 #ifdef TARGET_MOTE_L152RC
+#include "gps.h"
+#include "mma8451q.h"
+#include "mpl3115a2.h"
+#include "sx9500.h"
 
 #define RFSW1                    PC_4 //NorAm_Mote RFSwitch_CNTR_1
 #define RFSW2                    PC_13 //NorAm_Mote RFSwitch_CNTR_2
@@ -60,6 +64,20 @@
 DigitalIn dio4_pin(RADIO_DIO_4);
 DigitalIn dio5_pin(RADIO_DIO_5);
 
+AnalogIn ain_bat(PA_1);
+#define AIN_VREF        3.3     // stm32 internal refernce
+#define AIN_VBAT_DIV    2       // resistor divider
+
+/*  gps(tx, rx, en); */
+GPS gps(PB_6, PB_7, PB_11);
+uint32_t gps_coord_cnt;
+
+DigitalIn i2c_int_pin(PB_4);
+I2C i2c(I2C_SDA, I2C_SCL);
+MMA8451Q mma8451q(i2c, i2c_int_pin);
+MPL3115A2 mpl3115a2(i2c, i2c_int_pin);
+SX9500 sx9500(i2c, PA_9, PA_10);
+
 #else // sx1276 shield...
 //  pin:       3     8     1      7    10    12     5
 //           mosi, miso, sclk,   cs,  rst,  dio0, dio1
@@ -125,6 +143,15 @@
                 break;
         } // ...switch (act)           
     }
+    
+#ifdef TARGET_MOTE_L152RC
+    gps.service();
+    if (gps.LatitudeBinary != 0) {
+        gps.LatitudeBinary = 0;
+        //printf("gps long:%f, lat:%f  Vbat:%.2fV\r\n", gps.Longitude, gps.Latitude, ain_bat->read()*AIN_VREF*AIN_VBAT_DIV);
+        gps_coord_cnt++;
+    }    
+#endif /* TARGET_MOTE_L152RC */
 }
 
 scpi_result_t tx_busyQ(scpi_t * context)
@@ -645,6 +672,7 @@
     for (i = 0; i < len; i++)
         radio.tx_buf[i] = buffer[i];
     
+    radio.RegPaConfig.octet = radio.read_reg(REG_PACONFIG);    
     if (is_lora()) {
         lora.RegPayloadLength = len;
         radio.write_reg(REG_LR_PAYLOADLENGTH, lora.RegPayloadLength);
@@ -1290,6 +1318,120 @@
     SCPI_ResultBool(context, rfsw2.read());
     return SCPI_RES_OK;
 }
+
+scpi_result_t vbatQ(scpi_t* context)
+{
+    SCPI_ResultDouble(context, ain_bat.read()*AIN_VREF*AIN_VBAT_DIV);
+    return SCPI_RES_OK;      
+}
+
+scpi_result_t gps_enable(scpi_t* context)
+{
+    scpi_bool_t param1;
+    
+    if (!SCPI_ParamBool(context, &param1, TRUE))
+        return SCPI_RES_ERR;    
+        
+    gps.enable(param1);
+    return SCPI_RES_OK;
+}
+
+scpi_result_t gps_enableQ(scpi_t* context)
+{
+    SCPI_ResultBool(context, gps.enabled());
+    
+    return SCPI_RES_OK;      
+}
+
+scpi_result_t gps_numCords(scpi_t* context)
+{
+    int32_t i;
+    
+    if (!SCPI_ParamInt(context, &i, TRUE))
+        return SCPI_RES_ERR;   
+        
+    gps_coord_cnt = i; 
+    return SCPI_RES_OK;
+}
+
+scpi_result_t gps_numCordsQ(scpi_t* context)
+{
+    SCPI_ResultInt(context, gps_coord_cnt);    
+    return SCPI_RES_OK;    
+}
+
+scpi_result_t gps_longitude(scpi_t* context)
+{
+    if (!SCPI_ParamDouble(context, &gps.Longitude, TRUE))
+        return SCPI_RES_ERR;   
+        
+    return SCPI_RES_OK;         
+}
+
+scpi_result_t gps_longitudeQ(scpi_t* context)
+{
+    SCPI_ResultDouble(context, gps.Longitude);
+    return SCPI_RES_OK;     
+}
+
+scpi_result_t gps_latitude(scpi_t* context)
+{
+    if (!SCPI_ParamDouble(context, &gps.Latitude, TRUE))
+        return SCPI_RES_ERR;   
+        
+    return SCPI_RES_OK;           
+}
+
+scpi_result_t gps_latitudeQ(scpi_t* context)
+{
+    SCPI_ResultDouble(context, gps.Latitude);
+    return SCPI_RES_OK; 
+} 
+
+scpi_result_t mma_idQ(scpi_t* context)
+{
+    SCPI_ResultIntBase(context, mma8451q.read_single(MMA8451_ID), 16);
+    return SCPI_RES_OK;      
+}
+
+scpi_result_t mpl_idQ(scpi_t* context)
+{
+    SCPI_ResultIntBase(context, mpl3115a2.read(MPL3115_ID), 16);
+    return SCPI_RES_OK;       
+}
+
+scpi_result_t sx9500_reset(scpi_t* context)
+{
+    sx9500.reset();
+    return SCPI_RES_OK;
+}
+
+scpi_result_t sx9500_reg(scpi_t* context)
+{
+    int32_t addr, data;
+
+    if (!SCPI_ParamInt(context, &addr, TRUE))
+        return SCPI_RES_ERR;  
+        
+    if (!SCPI_ParamInt(context, &data, TRUE))
+        return SCPI_RES_ERR;  
+
+    sx9500.write(addr, data);
+        
+    return SCPI_RES_OK;
+}
+
+scpi_result_t sx9500_regQ(scpi_t* context)
+{
+    int32_t addr;
+    
+    if (!SCPI_ParamInt(context, &addr, TRUE))
+        return SCPI_RES_ERR; 
+     
+    SCPI_ResultIntBase(context, sx9500.read_single(addr), 16);
+    return SCPI_RES_OK;     
+}
+
 #endif /* TARGET_MOTE_L152RC */
 
 static const scpi_command_t scpi_commands[] = {
@@ -1414,6 +1556,25 @@
     {.pattern = "PD2", .callback = pd2_set,},
     {.pattern = "PD2?", .callback = pd2_get,},
     {.pattern = "RFSW?", .callback = rfswQ,},
+    {.pattern = "VBAT?", .callback = vbatQ,},
+    
+    {.pattern = "GPS:ENable", .callback = gps_enable,},
+    {.pattern = "GPS:ENable?", .callback = gps_enableQ,},
+    {.pattern = "GPS:NUMCoords", .callback = gps_numCords,},
+    {.pattern = "GPS:NUMCoords?", .callback = gps_numCordsQ,},   
+    {.pattern = "GPS:LOngitude", .callback = gps_longitude,},
+    {.pattern = "GPS:LOngitude?", .callback = gps_longitudeQ,},  
+    {.pattern = "GPS:LAtitude", .callback = gps_latitude,},
+    {.pattern = "GPS:LAtitude?", .callback = gps_latitudeQ,},  
+    
+    {.pattern = "MMA:ID?", .callback = mma_idQ,}, 
+    
+    {.pattern = "MPL:ID?", .callback = mpl_idQ,},
+    
+    {.pattern = "SX9500:RST", .callback = sx9500_reset,},
+    {.pattern = "SX9500:REGister", .callback = sx9500_reg,},
+    {.pattern = "SX9500:REGister?", .callback = sx9500_regQ,},
+    
 #endif /* TARGET_MOTE_L152RC */
     
     SCPI_CMD_LIST_END
@@ -1447,4 +1608,10 @@
 {
     radio.rf_switch.attach(rfsw_callback);
     radio.get_frf_MHz();    // get HF bit
+    
+#ifdef TARGET_MOTE_L152RC
+    /* V3 mote */
+    gps.init();
+    gps.en_invert = false;
+#endif /* TARGET_MOTE_L152RC */
 }