May 2021 Commit

Dependencies:   sx128x sx12xx_hal

main.cpp

Committer:
grit
Date:
2019-08-19
Revision:
8:efd5ceb87878
Parent:
7:ccb3088ce5be
Child:
9:28f69b9b6a4c

File content as of revision 8:efd5ceb87878:

#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
    #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
 
Timer t;
volatile bool tx_done;

static uint16_t crc_ccitt( uint8_t *buffer, uint16_t length )
{
    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;

    for( uint16_t i = 0; i < length; ++i )
    {
        crc ^= ( uint16_t ) buffer[i] << 8;
        for( uint16_t j = 0; j < 8; ++j )
        {
            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;

    Radio::radio.tx_buf[0] = cmd;
    t_diff = target - t.read_us();
    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;
    Radio::Send(7, 0, 0, 0);

    for (tx_done=false;!tx_done;) 
    {
        Radio::service();
    }
    tr_debug("t_diff:%u crc:%04x\r\n", t_diff, crc);
}

#define TARGET_LATENCY      2000000
static void send_alarm(uint8_t cmd)
{
    tr_debug(__func__);
    int i;
    unsigned target = t.read_us() + TARGET_LATENCY;
    tr_debug("send_alarm() %u\n", target);

    for (i = 0; i < 5; i++)
    {
        transmit(target, cmd);
        wait(0.1);
    }
}

void txDoneCB()
{
    tx_done = true;
}

void rxDoneCB(uint8_t size, float Rssi, float Snr)
{
}

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
};


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()
{
    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);
    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);

    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);
        }
    }
}