May 2021 Commit

Dependencies:   sx128x sx12xx_hal

Revision:
8:efd5ceb87878
Parent:
7:ccb3088ce5be
Child:
9:28f69b9b6a4c
--- a/main.cpp	Wed Jul 18 18:52:35 2018 -0700
+++ b/main.cpp	Mon Aug 19 01:28:02 2019 +0000
@@ -1,6 +1,10 @@
-//#include "user_platform.h"
+#include "mbed.h"
 #include "radio.h"
 
+#include "mbed-client-cli/ns_cmdline.h"
+#include "mbed-trace/mbed_trace.h"
+#define TRACE_GROUP   "mast"
+
 #if defined(SX127x_H) || defined(SX126x_H)
     #define BW_KHZ              500
     #define SPREADING_FACTOR    11
@@ -17,45 +21,17 @@
 
     #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;
  
 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
-    const uint16_t polynom = 0x1021;
-    // CRC initial value
-    uint16_t crc = 0x0000;
+    tr_debug(__func__);
+    const uint16_t polynom = 0x1021;    // The CRC calculation follows CCITT
+    uint16_t crc = 0x0000;              // CRC initial value
 
-    if( buffer == NULL )
-    {
-        return 0;
-    }
+    if( buffer == NULL ) return 0;
 
     for( uint16_t i = 0; i < length; ++i )
     {
@@ -65,12 +41,12 @@
             crc = ( crc & 0x8000 ) ? ( crc << 1 ) ^ polynom : ( crc << 1 );
         }
     }
-
     return crc;
 }
 
 void transmit(unsigned target, uint8_t cmd)
 {
+    tr_debug(__func__);
     unsigned t_diff;
     uint16_t crc;
 
@@ -83,47 +59,30 @@
     crc = crc_ccitt(Radio::radio.tx_buf, 5);
     Radio::radio.tx_buf[5] = crc >> 8;
     Radio::radio.tx_buf[6] = crc & 0xff;
-
     Radio::Send(7, 0, 0, 0);
 
-    for (tx_done = false; !tx_done; )
+    for (tx_done=false;!tx_done;) 
+    {
         Radio::service();
-
-    printf("t_diff:%u crc:%04x\r\n", t_diff, crc);
+    }
+    tr_debug("t_diff:%u crc:%04x\r\n", t_diff, crc);
 }
 
 #define TARGET_LATENCY      2000000
-void send_alarm(uint8_t cmd)
+static void send_alarm(uint8_t cmd)
 {
+    tr_debug(__func__);
     int i;
     unsigned target = t.read_us() + TARGET_LATENCY;
-    printf("send_alarm() %u\n", target);
+    tr_debug("send_alarm() %u\n", target);
 
-    for (i = 0; i < 5; i++) {
+    for (i = 0; i < 5; i++)
+    {
         transmit(target, cmd);
         wait(0.1);
     }
 }
 
-void debounce(DigitalIn* pin, uint8_t cmd)
-{
-    if (!pin->read()) {
-        int i;
-        for (i = 0; i < 5; i++) {
-            wait(0.01);
-            if (pin->read()) {
-                printf("trans\r\n");
-                break;
-            }
-        }
-        if (i == 5)
-            send_alarm(cmd);
-
-        while (!pin->read())
-            ;
-    }
-}
-
 void txDoneCB()
 {
     tx_done = true;
@@ -133,7 +92,8 @@
 {
 }
 
-const RadioEvents_t rev = {
+const RadioEvents_t rev =
+{
     /* Dio0_top_half */     NULL,
     /* TxDone_topHalf */    NULL,
     /* TxDone_botHalf */    txDoneCB,
@@ -145,63 +105,62 @@
     /* CadDone  */          NULL
 };
 
+
+static Mutex serialOutMutex;
+static void serial_out_mutex_wait()
+{
+    serialOutMutex.lock();
+}
+
+static void serial_out_mutex_release()
+{
+    osStatus s = serialOutMutex.unlock();
+    MBED_ASSERT(s == osOK);
+}
+
+static int cmd_transmitByte(int argc, char *argv[]) 
+{
+    tr_debug("%s args: %d",__FUNCTION__,argc);
+    if(argc != 2)
+    {
+        tr_debug("Invalid argument count");
+        return CMDLINE_RETCODE_INVALID_PARAMETERS;
+    }
+    uint8_t byte = atoi(argv[1]);
+    tr_debug("sending byte: %d",byte);
+    send_alarm(byte);
+    return CMDLINE_RETCODE_SUCCESS;
+}
+
 int main()
 {
-    printf("\r\nreset-tx\r\n");
-
-    pinA.mode(PullUp);
-    pinB.mode(PullUp);
-    pinC.mode(PullUp);
-    pinD.mode(PullUp);
+    mbed_trace_mutex_wait_function_set( serial_out_mutex_wait );
+    mbed_trace_mutex_release_function_set( serial_out_mutex_release );
+    mbed_trace_init();
+    tr_debug(__FUNCTION__);
 
     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);
-                
-    for (;;) {       
-        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 (;;)
+    tr_debug("Initializing Command Line");
+    cmd_init(0);
+    cmd_mutex_wait_func( serial_out_mutex_wait );
+    cmd_mutex_release_func( serial_out_mutex_release );
+    cmd_add("transmit-byte", cmd_transmitByte,"Transmit Byte","Transmit a decimal byte over LoRa radio");
+    
+    while(true)
+    {
+        int c = getchar();
+        if (c != EOF)
+        {
+            cmd_char_input(c);
+        }
+    }
 }