raises pin at time instructed by received LoRa packet.

Dependencies:   sx126x 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.

Transmitter side is at Alarm Slave project page.
Output is observed on nucleo morpho connector pins pin[A-D]: see code for actual pins used.

main.cpp

Committer:
dudmuck
Date:
2017-08-31
Revision:
1:6a3a48d657a9
Parent:
0:b6ec8db2edbf
Child:
2:bf201940a9db

File content as of revision 1:6a3a48d657a9:

#include "sx127x_lora.h"
 
DigitalOut myled(LED1);
 
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;
}
/**********************************************************************/
DigitalOut pc3(PC_3);
DigitalOut pc2(PC_2);
DigitalOut pc6(PC_6);
DigitalOut pc8(PC_8);
DigitalOut* pin; 
Timeout to;

#define PIN_ASSERT_us       500000

#define CMD_PC2       0x02
#define CMD_PC3       0x03
#define CMD_PC6       0x06
#define CMD_PC8       0x08

void alarm_pin_clr()
{
    pin->write(0);
}

void alarm_pin_set()
{
    pin->write(1);
    to.attach_us(&alarm_pin_clr, PIN_ASSERT_us);
}

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 get_alarm()
{
    uint16_t rx_crc, crc = crc_ccitt(radio.rx_buf, 5);
    rx_crc = radio.rx_buf[5];
    rx_crc <<= 8;
    rx_crc += radio.rx_buf[6];
    //printf("%u) crc rx:%04x, calc:%04x\r\n", lora.RegRxNbBytes, rx_crc, crc);
    if (crc == rx_crc) {
        uint8_t c = radio.rx_buf[0];
        //if (radio.rx_buf[0] == CMD_ALARM)
        if (c == CMD_PC2 || c == CMD_PC3 || c == CMD_PC6 || c == CMD_PC8) {
            unsigned delay;
            delay = radio.rx_buf[1];
            delay <<= 8;
            delay += radio.rx_buf[2];
            delay <<= 8;
            delay += radio.rx_buf[3];
            delay <<= 8;
            delay += radio.rx_buf[4];
            switch (c) {
                case CMD_PC2: pin = &pc2; break;
                case CMD_PC3: pin = &pc3; break;
                case CMD_PC6: pin = &pc6; break;
                case CMD_PC8: pin = &pc8; break;
            }
            to.attach_us(&alarm_pin_set, delay);
            printf("delay:%u\r\n", delay);
        } else
            printf("cmd? %02x\r\n", radio.rx_buf[0]);
    } else
        printf("crc fail %04x, %04x\r\n", rx_crc, crc);
}
 
int main()
{   
    printf("\r\nreset-rx\r\n");
    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);
                
    lora.start_rx(RF_OPMODE_RECEIVER);
    
    for (;;) {     
        if (lora.service() == SERVICE_READ_FIFO) {
            /*int i;
            for (i = 0; i < lora.RegRxNbBytes; i++) {
                printf("%02x ", radio.rx_buf[i]);
            }
            printf("\r\n");*/
            get_alarm();
        }
    }
}