point-2-point demo
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:
- Wayne Roberts
- Date:
- 2018-05-30
- Revision:
- 3:cd312fc32558
- Parent:
- 2:5131512b4eb9
- Child:
- 4:b5dd459ac390
File content as of revision 3:cd312fc32558:
#include "sx127x_lora.h" #include "uart_cmds.h" //#define ANALOG_TX_TRIGGER 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); #define AIN_REST_THRESHOLD 96 // 12bit left justified DigitalOut pc6_out(PC_6); #ifndef ANALOG_TX_TRIGGER DigitalOut jumper_out(PC_10); InterruptIn jumper_in(PC_12); volatile bool start_tx; #endif /* !ANALOG_TX_TRIGGER */ 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() } #ifndef ANALOG_TX_TRIGGER void button_isr() { if (!jumper_in.read()) start_tx = true; } void auto_tx() { if (jumper_in.read()) start_tx = true; else ticker.detach(); } #endif /* !ANALOG_TX_TRIGGER */ int main() { #ifdef ANALOG_TX_TRIGGER uint16_t prev_ain; int8_t ain_movement; bool ain_sent; #else bool jin = false; jumper_out = 1; jumper_in.mode(PullDown); while (!user_button) { printf("button-lo\r\n"); wait(0.01); } user_button.fall(&button_isr); #endif /* !ANALOG_TX_TRIGGER */ printf("\r\n2reset\r\n"); 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); #ifdef ANALOG_TX_TRIGGER prev_ain = a1.read_u16(); ain_movement = 0; ain_sent = false; #endif /* ANALOG_TX_TRIGGER */ for (;;) { #ifdef ANALOG_TX_TRIGGER uint16_t ain = a1.read_u16(); uint16_t diff = abs(ain-prev_ain); if (diff > AIN_REST_THRESHOLD) { ain_sent = false; if (ain_movement < 1) ain_movement = 1; else { if (++ain_movement > 16) ain_movement = 16; } } else { /* steady state */ if (ain_movement > 0) ain_movement = 0; else { if (--ain_movement < -16) { ain_movement = -16; if (!ain_sent) { uint8_t buf[4]; printf("## %02x ##\r\n", ain >> 8); buf[0] = CMD_PWM; buf[1] = 120; // Hz buf[2] = ain >> 8; // duty radio_tx(buf, 3); ain_sent = true; } } } } //printf("%05u diff:%04u move:%d\r\n", ain, diff, ain_movement); prev_ain = ain; wait_us(5000); #else 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); } #endif /* !ANALOG_TX_TRIGGER */ 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 (;;) }