point-2-point demo

Dependencies:   sx12xx_hal

radio chip selection

Radio chip driver is not included, because these options are available.
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.

TX trigger selection

Edit main.h to define DIGITAL_TRIGGER or ANALOG_TRIGGER to chose whether transmit is initiated by digital pin (button/jumper) or analog pin(s) level change.

This project is intended to be used on two LoRa shields.

Each board sits in continuous RX mode, waiting for request packet.
If the received packet has good CRC, the packet is acknowledged along with read of ADC sample from the replying device.
The original request packet also contains instruction to set level of output pin.

Both sides of the link are running the same code, and each can initiate a transmission at any time.
No addressing is used, so only two nodes can operate on the radio channel.

main.cpp

Committer:
dudmuck
Date:
2017-11-10
Revision:
2:5131512b4eb9
Parent:
1:d95d135a4fb4
Child:
3:cd312fc32558

File content as of revision 2:5131512b4eb9:

#include "sx127x_lora.h"
#include "uart_cmds.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

InterruptIn user_button(USER_BUTTON);
Timer t;
Ticker ticker;
AnalogIn a1(A1);
DigitalOut pc6_out(PC_6);

DigitalOut jumper_out(PC_10);
InterruptIn jumper_in(PC_12);

void rfsw_callback()
{
    if (radio.RegOpMode.bits.Mode == RF_OPMODE_TRANSMITTER)
        rfsw = 1;
    else
        rfsw = 0;
}

/**********************************************************************/

uint8_t out_pin_state;


static uint16_t crc16( uint8_t *buffer, uint16_t length )
{
    uint16_t i;
    // The CRC calculation follows CCITT
    const uint16_t polynom = 0x1021;
    // CRC initial value
    uint16_t crc = 0x0000;

    if( buffer == NULL )
    {
        return 0;
    }

    for( i = 0; i < length; ++i )
    {
        uint16_t j;
        crc ^= ( uint16_t ) buffer[i] << 8;
        for( j = 0; j < 8; ++j )
        {
            crc = ( crc & 0x8000 ) ? ( crc << 1 ) ^ polynom : ( crc << 1 );
        }
    }

    return crc;
}

void tx_ack()
{
    uint8_t n = 0;
    uint16_t crc;

    if (radio.rx_buf[0] == CMD_OUT_PIN) {
        uint16_t ain = a1.read_u16();
        radio.tx_buf[n++] = CMD_OUT_PIN_ACK;
        /* TODO read analog pin and digital input pin */
        radio.tx_buf[n++] = ain;
        radio.tx_buf[n++] = ain >> 8;
    } else
        radio.tx_buf[n++] = 0xff;

    crc = crc16(radio.tx_buf, n);
    radio.tx_buf[n++] = crc;
    radio.tx_buf[n++] = crc >> 8;

    lora.RegPayloadLength = n;
    radio.write_reg(REG_LR_PAYLOADLENGTH, lora.RegPayloadLength);
        
    lora.start_tx(lora.RegPayloadLength);   /* begin transmission */
    
    while (lora.service() != SERVICE_TX_DONE)   /* wait for transmission to complete */
        ;

    lora.start_rx(RF_OPMODE_RECEIVER);
}

void radio_tx(uint8_t* payload, uint8_t payload_len)
{
    int i;
    uint8_t n = 0;
    uint16_t crc;

    while (payload_len > 0) {
        //printf("n%u, paylen%u\r\n", n, payload_len);
        payload_len--;
        radio.tx_buf[payload_len] = payload[payload_len];
        n++;
    }

    crc = crc16(radio.tx_buf, n);
    radio.tx_buf[n++] = crc;
    radio.tx_buf[n++] = crc >> 8;

    lora.RegPayloadLength = n;
    radio.write_reg(REG_LR_PAYLOADLENGTH, lora.RegPayloadLength);

    for (i = 0; i < 3; i++) {
        int rx_timeout_at;

        lora.start_tx(lora.RegPayloadLength);   /* begin transmission */
        
        while (lora.service() != SERVICE_TX_DONE)   /* wait for transmission to complete */
            ;
        rx_timeout_at = t.read_us() + 200000;

        lora.start_rx(RF_OPMODE_RECEIVER);

        while (t.read_us() < rx_timeout_at) {
            if (lora.service() == SERVICE_READ_FIFO) {
                uint16_t rx_crc;
                printf("rssi:%ddBm snr:%.1fdB ", lora.get_pkt_rssi(), lora.RegPktSnrValue / 4.0);
                rx_crc = radio.rx_buf[lora.RegRxNbBytes-2];
                rx_crc |= radio.rx_buf[lora.RegRxNbBytes-1] << 8;
                crc = crc16(radio.rx_buf, lora.RegRxNbBytes-2);
                if (crc == rx_crc) {
                    printf("crcOk %u\r\n", i);
                    if (radio.rx_buf[0] == CMD_OUT_PIN_ACK) {
                        uint16_t ain = radio.rx_buf[1];
                        ain |= radio.rx_buf[2] << 8;
                        printf("ain %u\r\n", ain);
                    }
                    return;
                } else
                    printf("crcFail %04x != %04x\r\n", crc, rx_crc);
            } 
        }
        printf("rx-timeout %u\r\n", i);
    } // ..for()
}

volatile bool start_tx;

void button_isr()
{
    if (!jumper_in.read())
        start_tx = true;
}

void auto_tx()
{
    if (jumper_in.read())
        start_tx = true;
    else
        ticker.detach();
}

int main()
{
    bool jin = false;
    printf("\r\n2reset\r\n");

    jumper_out = 1;
    jumper_in.mode(PullDown);

    while (!user_button) {
        printf("button-lo\r\n");
        wait(0.01);
    }
    user_button.fall(&button_isr);
    t.start();

    radio.rf_switch = rfsw_callback;
    
    radio.set_frf_MHz(917.3);
    lora.enable();
    lora.setBw_KHz(500);
    lora.setSf(9);
    
    /* 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 (jumper_in.read()) {
            if (!jin) {
                ticker.attach(auto_tx, 0.5);
                jin = true;
            }
        } else {
            jin = false;
        }

        if (start_tx) {
            start_tx = false;

            uint8_t buf[2];
            out_pin_state ^= 1;
            buf[0] = CMD_OUT_PIN;
            buf[1] = out_pin_state;
            printf("start_tx...\r\n");
            radio_tx(buf, 2);
        }
        if (lora.service() == SERVICE_READ_FIFO) {
            uint16_t crc, rx_crc;
            int i;
            for (i = 0; i < lora.RegRxNbBytes; i++) {
                printf("%02x ", radio.rx_buf[i]);
            }
            printf(" rssi:%ddBm, snr:%.1fdB\r\n", lora.get_pkt_rssi(), lora.RegPktSnrValue / 4.0);
            rx_crc = radio.rx_buf[lora.RegRxNbBytes-2];
            rx_crc |= radio.rx_buf[lora.RegRxNbBytes-1] << 8;
            crc = crc16(radio.rx_buf, lora.RegRxNbBytes-2);
            if (crc == rx_crc) {
                printf("crcOk\r\n");
                if (!parse_radio_rx(radio.rx_buf) && radio.rx_buf[0] == CMD_OUT_PIN) {
                    pc6_out.write(radio.rx_buf[1]);
                    printf("out pin state: %u", radio.rx_buf[1]);
                }
                printf("\r\n");
                tx_ack();
            } else
                printf("crc %04x != %04x\r\n", crc, rx_crc);
        } // ..if something received

        uart_service();
    } // ..for (;;)
}