May 2021 Commit

Dependencies:   sx128x sx12xx_hal

main.cpp

Committer:
dudmuck
Date:
2017-08-31
Revision:
1:3199506bc2e5
Parent:
0:cb38da4f4b04
Child:
2:0b7620bda2c9

File content as of revision 1:3199506bc2e5:

#include "sx127x_lora.h"
 
SPI spi(D11, D12, D13); // mosi, miso, sclk
//           dio0, dio1, nss, spi, rst
SX127x radio(  D2,   D3, D10, spi, A0); // sx1276 arduino shield
 
SX127x_lora lora(radio);
DigitalInOut rfsw(A4);    // for SX1276 arduino shield
 
void rfsw_callback()
{
    if (radio.RegOpMode.bits.Mode == RF_OPMODE_TRANSMITTER) {
        rfsw = 1;
    } else {
        rfsw = 0;
    }
}
/**********************************************************************/

DigitalIn pc3(PC_3);
DigitalIn pc2(PC_2);
DigitalIn pc6(PC_6);
DigitalIn pc8(PC_8);
Timer t;
#define CMD_PC2       0x02
#define CMD_PC3       0x03
#define CMD_PC6       0x06
#define CMD_PC8       0x08

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;

    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)
{
    unsigned t_diff;
    uint16_t crc;

    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;


    lora.start_tx(lora.RegPayloadLength);   /* begin transmission */

    while (lora.service() != SERVICE_TX_DONE) {  /* wait for transmission to complete */
    }

    printf("t_diff:%u crc:%04x\r\n", t_diff, crc);
}

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

    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())
            ;
    }
}
 
int main()
{
    printf("\r\nreset-tx\r\n");
    t.start();

    pc3.mode(PullUp);
    pc2.mode(PullUp);
    pc6.mode(PullUp);
    pc8.mode(PullUp);

    radio.rf_switch = rfsw_callback;
    
    radio.set_frf_MHz(910.8);
    lora.enable();
    lora.setBw_KHz(500);
    lora.setSf(11);
    
    /* RFO or PABOOST choice:
     * SX1276 shield: RFO if using 900MHz, or PA_BOOST if using 433MHz
     */
    rfsw.input();
    if (rfsw.read()) {
        printf("LAS\r\n");
        /* LAS HF=PA_BOOST  LF=RFO */
        if (radio.HF)
            radio.RegPaConfig.bits.PaSelect = 1;
        else
            radio.RegPaConfig.bits.PaSelect = 0;
    } else {
        /* MAS shield board, only RFO TX */
        radio.RegPaConfig.bits.PaSelect = 0;
        printf("MAS\r\n");
    }
    rfsw.output();
    radio.write_reg(REG_PACONFIG, radio.RegPaConfig.octet);
                
    /* constant payload length */
    lora.RegPayloadLength = 7;
    radio.write_reg(REG_LR_PAYLOADLENGTH, lora.RegPayloadLength);

    for (;;) {       
        debounce(&pc3, CMD_PC3);
        debounce(&pc2, CMD_PC2);
        debounce(&pc6, CMD_PC6);
        debounce(&pc8, CMD_PC8);
    } // ..for (;;)
}