Alarm generator, transmitter side

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

This is transmitter project; Receiver side is Alarm Slave project.
This project generates alarm (transmits) from grounding certain pins.
To find the actual pins used, refer to the code where pin[A-D] is declared as DigitalIn.

Alarm message is sent N times with future timestamp indicating when alarm is to occur. This accommodates dropped packets, resulting in alarm occurring when as few as 1 packet is received.

main.cpp

Committer:
Wayne Roberts
Date:
2018-02-05
Revision:
5:1652e04809fb
Parent:
4:19056d9707ef
Child:
6:59ba1113b3c6

File content as of revision 5:1652e04809fb:

#include "platform.h"
 
SX127x_lora lora(radio);
Timer t;
#define CMD_PINA       0x02
#define CMD_PINB       0x03
#define CMD_PINC       0x06
#define CMD_PIND       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())
            ;
    }
}

void cmd_ocp(uint8_t ma)
{
    if (ma < 130)
        radio.RegOcp.bits.OcpTrim = (ma - 45) / 5;
    else
        radio.RegOcp.bits.OcpTrim = (ma + 30) / 10;
    radio.write_reg(REG_OCP, radio.RegOcp.octet);
   
    radio.RegOcp.octet = radio.read_reg(REG_OCP);
    if (radio.RegOcp.bits.OcpTrim < 16)
        ma = 45 + (5 * radio.RegOcp.bits.OcpTrim);
    else if (radio.RegOcp.bits.OcpTrim < 28)
        ma = (10 * radio.RegOcp.bits.OcpTrim) - 30;
    else
        ma = 240;
    printf("Ocp: %dmA\r\n", ma); 
}

int main()
{
    printf("\r\nreset-tx\r\n");
    t.start();

    pinA.mode(PullUp);
    pinB.mode(PullUp);
    pinC.mode(PullUp);
    pinD.mode(PullUp);

    radio.rf_switch = rfsw_callback;
    
    radio.set_frf_MHz(910.8);
    lora.enable();
    lora.setBw_KHz(500);
    lora.setSf(11);
    
    board_init();
                
    /* constant payload length */
    lora.RegPayloadLength = 7;
    radio.write_reg(REG_LR_PAYLOADLENGTH, lora.RegPayloadLength);
    
/*    lora.RegModemConfig2.octet = radio.read_reg(REG_LR_MODEMCONFIG2);
    lora.RegModemConfig2.sx1276bits.TxContinuousMode = 1;   
    radio.write_reg(REG_LR_MODEMCONFIG2, lora.RegModemConfig2.octet);      
*/
    for (;;) {       
        debounce(&pinA, CMD_PINA);
        debounce(&pinB, CMD_PINB);
        debounce(&pinC, CMD_PINC);
        debounce(&pinD, CMD_PIND);
    } // ..for (;;)
}