Alarm generator, transmitter side

Dependencies:   sx12xx_hal

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 NAmote72 or Murata discovery, then you must import only sx127x driver.

This is transmitter project; Receiver side is Alarm Slave project.
This project generates alarm (transmits) from grounding certain pins.
To find the actual pins used, refer to the code where pin[A-D] is declared as DigitalIn.

Alarm message is sent N times with future timestamp indicating when alarm is to occur. This accommodates dropped packets, resulting in alarm occurring when as few as 1 packet is received.

Revision:
7:ccb3088ce5be
Parent:
6:59ba1113b3c6
--- a/main.cpp	Mon Feb 12 17:23:14 2018 -0800
+++ b/main.cpp	Wed Jul 18 18:52:35 2018 -0700
@@ -1,12 +1,50 @@
-#include "user_platform.h"
+//#include "user_platform.h"
+#include "radio.h"
+
+#if defined(SX127x_H) || defined(SX126x_H)
+    #define BW_KHZ              500
+    #define SPREADING_FACTOR    11
+    #define CF_HZ               910800000
+    #if defined(SX126x_H)
+        #define TX_DBM              (Radio::chipType == CHIP_TYPE_SX1262 ? 20 : 14) 
+    #else
+        #define TX_DBM              17
+    #endif
+#elif defined(SX128x_H)
+    #define BW_KHZ              200
+    #define SPREADING_FACTOR    11
+    #define CF_HZ               2487000000
+
+    #define TX_DBM              5
+#endif
+
+#ifdef TARGET_DISCO_L072CZ_LRWAN1
+    DigitalIn pinA(PB_12);
+    DigitalIn pinB(PB_13);
+    DigitalIn pinC(PB_14);
+    DigitalIn pinD(PB_15);
+#elif defined(TARGET_FF_MORPHO)
+    DigitalIn pinA(PC_3);
+    DigitalIn pinB(PC_2);
+    DigitalIn pinC(PC_6);
+    DigitalIn pinD(PC_8);
+#endif 
+
+volatile struct _f_ {
+    uint8_t enable_pin_A : 1;
+    uint8_t enable_pin_B : 1;
+    uint8_t enable_pin_C : 1;
+    uint8_t enable_pin_D : 1;
+} flags;
  
-SX127x_lora lora(radio);
 Timer t;
 #define CMD_PINA       0x02
 #define CMD_PINB       0x03
 #define CMD_PINC       0x06
 #define CMD_PIND       0x08
 
+volatile bool tx_done;
+
 static uint16_t crc_ccitt( uint8_t *buffer, uint16_t length )
 {
     // The CRC calculation follows CCITT
@@ -36,21 +74,20 @@
     unsigned t_diff;
     uint16_t crc;
 
-    radio.tx_buf[0] = cmd;
+    Radio::radio.tx_buf[0] = cmd;
     t_diff = target - t.read_us();
-    radio.tx_buf[1] = t_diff >> 24;
-    radio.tx_buf[2] = t_diff >> 16;
-    radio.tx_buf[3] = t_diff >> 8;
-    radio.tx_buf[4] = t_diff & 0xff;
-    crc = crc_ccitt(radio.tx_buf, 5);
-    radio.tx_buf[5] = crc >> 8;
-    radio.tx_buf[6] = crc & 0xff;
+    Radio::radio.tx_buf[1] = t_diff >> 24;
+    Radio::radio.tx_buf[2] = t_diff >> 16;
+    Radio::radio.tx_buf[3] = t_diff >> 8;
+    Radio::radio.tx_buf[4] = t_diff & 0xff;
+    crc = crc_ccitt(Radio::radio.tx_buf, 5);
+    Radio::radio.tx_buf[5] = crc >> 8;
+    Radio::radio.tx_buf[6] = crc & 0xff;
 
-
-    lora.start_tx(lora.RegPayloadLength);   /* begin transmission */
+    Radio::Send(7, 0, 0, 0);
 
-    while (lora.service() != SERVICE_TX_DONE) {  /* wait for transmission to complete */
-    }
+    for (tx_done = false; !tx_done; )
+        Radio::service();
 
     printf("t_diff:%u crc:%04x\r\n", t_diff, crc);
 }
@@ -87,55 +124,84 @@
     }
 }
 
-void cmd_ocp(uint8_t ma)
+void txDoneCB()
+{
+    tx_done = true;
+}
+
+void rxDoneCB(uint8_t size, float Rssi, float Snr)
 {
-    if (ma < 130)
-        radio.RegOcp.bits.OcpTrim = (ma - 45) / 5;
-    else
-        radio.RegOcp.bits.OcpTrim = (ma + 30) / 10;
-    radio.write_reg(REG_OCP, radio.RegOcp.octet);
-   
-    radio.RegOcp.octet = radio.read_reg(REG_OCP);
-    if (radio.RegOcp.bits.OcpTrim < 16)
-        ma = 45 + (5 * radio.RegOcp.bits.OcpTrim);
-    else if (radio.RegOcp.bits.OcpTrim < 28)
-        ma = (10 * radio.RegOcp.bits.OcpTrim) - 30;
-    else
-        ma = 240;
-    printf("Ocp: %dmA\r\n", ma); 
 }
 
+const RadioEvents_t rev = {
+    /* Dio0_top_half */     NULL,
+    /* TxDone_topHalf */    NULL,
+    /* TxDone_botHalf */    txDoneCB,
+    /* TxTimeout  */        NULL,
+    /* RxDone  */           rxDoneCB,
+    /* RxTimeout  */        NULL,
+    /* RxError  */          NULL,
+    /* FhssChangeChannel  */NULL,
+    /* CadDone  */          NULL
+};
+
 int main()
 {
     printf("\r\nreset-tx\r\n");
-    t.start();
 
     pinA.mode(PullUp);
     pinB.mode(PullUp);
     pinC.mode(PullUp);
     pinD.mode(PullUp);
 
-    radio.rf_switch = rfsw_callback;
-    
-    radio.set_frf_MHz(910.8);
-    lora.enable();
-    lora.setBw_KHz(500);
-    lora.setSf(11);
-    
-    board_init();
+    wait(0.05);
+
+    if (pinA.read() == 0) {
+        printf("pinA-disabled\r\n");
+        flags.enable_pin_A = 0;
+    } else
+        flags.enable_pin_A = 1;
+
+    if (pinB.read() == 0) {
+        printf("pinB-disabled\r\n");
+        flags.enable_pin_B = 0;
+    } else
+        flags.enable_pin_B = 1;
+
+    if (pinC.read() == 0) {
+        printf("pinC-disabled\r\n");
+        flags.enable_pin_C = 0;
+    } else
+        flags.enable_pin_C = 1;
+
+    if (pinD.read() == 0) {
+        printf("pinD-disabled\r\n");
+        flags.enable_pin_D = 0;
+    } else
+        flags.enable_pin_D = 1;
+
+    t.start();
+
+    Radio::Init(&rev);
+
+    Radio::Standby();
+    Radio::LoRaModemConfig(BW_KHZ, SPREADING_FACTOR, 1);
+    Radio::LoRaPacketConfig(8, false, true, false);  // preambleLen, fixLen, crcOn, invIQ
+    Radio::SetChannel(CF_HZ);
+
+    Radio::set_tx_dbm(TX_DBM);
                 
-    /* constant payload length */
-    lora.RegPayloadLength = 7;
-    radio.write_reg(REG_LR_PAYLOADLENGTH, lora.RegPayloadLength);
-    
-/*    lora.RegModemConfig2.octet = radio.read_reg(REG_LR_MODEMCONFIG2);
-    lora.RegModemConfig2.sx1276bits.TxContinuousMode = 1;   
-    radio.write_reg(REG_LR_MODEMCONFIG2, lora.RegModemConfig2.octet);      
-*/
     for (;;) {       
-        debounce(&pinA, CMD_PINA);
-        debounce(&pinB, CMD_PINB);
-        debounce(&pinC, CMD_PINC);
-        debounce(&pinD, CMD_PIND);
+        if (flags.enable_pin_A)
+            debounce(&pinA, CMD_PINA);
+
+        if (flags.enable_pin_B)
+            debounce(&pinB, CMD_PINB);
+
+        if (flags.enable_pin_C)
+            debounce(&pinC, CMD_PINC);
+
+        if (flags.enable_pin_D)
+            debounce(&pinD, CMD_PIND);
     } // ..for (;;)
 }