May 2021 test

Dependencies:   sx128x sx12xx_hal

Committer:
dudmuck
Date:
Tue Aug 22 10:16:06 2017 -0700
Revision:
0:b6ec8db2edbf
Child:
1:6a3a48d657a9
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dudmuck 0:b6ec8db2edbf 1 #include "sx127x_lora.h"
dudmuck 0:b6ec8db2edbf 2
dudmuck 0:b6ec8db2edbf 3 DigitalOut myled(LED1);
dudmuck 0:b6ec8db2edbf 4
dudmuck 0:b6ec8db2edbf 5 SPI spi(D11, D12, D13); // mosi, miso, sclk
dudmuck 0:b6ec8db2edbf 6 // dio0, dio1, nss, spi, rst
dudmuck 0:b6ec8db2edbf 7 SX127x radio( D2, D3, D10, spi, A0); // sx1276 arduino shield
dudmuck 0:b6ec8db2edbf 8
dudmuck 0:b6ec8db2edbf 9 SX127x_lora lora(radio);
dudmuck 0:b6ec8db2edbf 10 DigitalInOut rfsw(A4); // for SX1276 arduino shield
dudmuck 0:b6ec8db2edbf 11
dudmuck 0:b6ec8db2edbf 12 void rfsw_callback()
dudmuck 0:b6ec8db2edbf 13 {
dudmuck 0:b6ec8db2edbf 14 if (radio.RegOpMode.bits.Mode == RF_OPMODE_TRANSMITTER)
dudmuck 0:b6ec8db2edbf 15 rfsw = 1;
dudmuck 0:b6ec8db2edbf 16 else
dudmuck 0:b6ec8db2edbf 17 rfsw = 0;
dudmuck 0:b6ec8db2edbf 18 }
dudmuck 0:b6ec8db2edbf 19 /**********************************************************************/
dudmuck 0:b6ec8db2edbf 20 DigitalOut pc_3(PC_3);
dudmuck 0:b6ec8db2edbf 21 Timeout to;
dudmuck 0:b6ec8db2edbf 22
dudmuck 0:b6ec8db2edbf 23 #define PIN_ASSERT_us 200000
dudmuck 0:b6ec8db2edbf 24 #define CMD_ALARM 0x01
dudmuck 0:b6ec8db2edbf 25
dudmuck 0:b6ec8db2edbf 26 void alarm_pin_clr()
dudmuck 0:b6ec8db2edbf 27 {
dudmuck 0:b6ec8db2edbf 28 pc_3 = 0;
dudmuck 0:b6ec8db2edbf 29 }
dudmuck 0:b6ec8db2edbf 30
dudmuck 0:b6ec8db2edbf 31 void alarm_pin_set()
dudmuck 0:b6ec8db2edbf 32 {
dudmuck 0:b6ec8db2edbf 33 pc_3 = 1;
dudmuck 0:b6ec8db2edbf 34 to.attach_us(&alarm_pin_clr, PIN_ASSERT_us);
dudmuck 0:b6ec8db2edbf 35 }
dudmuck 0:b6ec8db2edbf 36
dudmuck 0:b6ec8db2edbf 37 static uint16_t crc_ccitt( uint8_t *buffer, uint16_t length )
dudmuck 0:b6ec8db2edbf 38 {
dudmuck 0:b6ec8db2edbf 39 // The CRC calculation follows CCITT
dudmuck 0:b6ec8db2edbf 40 const uint16_t polynom = 0x1021;
dudmuck 0:b6ec8db2edbf 41 // CRC initial value
dudmuck 0:b6ec8db2edbf 42 uint16_t crc = 0x0000;
dudmuck 0:b6ec8db2edbf 43
dudmuck 0:b6ec8db2edbf 44 if( buffer == NULL )
dudmuck 0:b6ec8db2edbf 45 {
dudmuck 0:b6ec8db2edbf 46 return 0;
dudmuck 0:b6ec8db2edbf 47 }
dudmuck 0:b6ec8db2edbf 48
dudmuck 0:b6ec8db2edbf 49 for( uint16_t i = 0; i < length; ++i )
dudmuck 0:b6ec8db2edbf 50 {
dudmuck 0:b6ec8db2edbf 51 crc ^= ( uint16_t ) buffer[i] << 8;
dudmuck 0:b6ec8db2edbf 52 for( uint16_t j = 0; j < 8; ++j )
dudmuck 0:b6ec8db2edbf 53 {
dudmuck 0:b6ec8db2edbf 54 crc = ( crc & 0x8000 ) ? ( crc << 1 ) ^ polynom : ( crc << 1 );
dudmuck 0:b6ec8db2edbf 55 }
dudmuck 0:b6ec8db2edbf 56 }
dudmuck 0:b6ec8db2edbf 57
dudmuck 0:b6ec8db2edbf 58 return crc;
dudmuck 0:b6ec8db2edbf 59 }
dudmuck 0:b6ec8db2edbf 60
dudmuck 0:b6ec8db2edbf 61 void get_alarm()
dudmuck 0:b6ec8db2edbf 62 {
dudmuck 0:b6ec8db2edbf 63 uint16_t rx_crc, crc = crc_ccitt(radio.rx_buf, 5);
dudmuck 0:b6ec8db2edbf 64 rx_crc = radio.rx_buf[5];
dudmuck 0:b6ec8db2edbf 65 rx_crc <<= 8;
dudmuck 0:b6ec8db2edbf 66 rx_crc += radio.rx_buf[6];
dudmuck 0:b6ec8db2edbf 67 //printf("%u) crc rx:%04x, calc:%04x\r\n", lora.RegRxNbBytes, rx_crc, crc);
dudmuck 0:b6ec8db2edbf 68 if (crc == rx_crc) {
dudmuck 0:b6ec8db2edbf 69 if (radio.rx_buf[0] == CMD_ALARM) {
dudmuck 0:b6ec8db2edbf 70 unsigned delay;
dudmuck 0:b6ec8db2edbf 71 delay = radio.rx_buf[1];
dudmuck 0:b6ec8db2edbf 72 delay <<= 8;
dudmuck 0:b6ec8db2edbf 73 delay += radio.rx_buf[2];
dudmuck 0:b6ec8db2edbf 74 delay <<= 8;
dudmuck 0:b6ec8db2edbf 75 delay += radio.rx_buf[3];
dudmuck 0:b6ec8db2edbf 76 delay <<= 8;
dudmuck 0:b6ec8db2edbf 77 delay += radio.rx_buf[4];
dudmuck 0:b6ec8db2edbf 78 to.attach_us(&alarm_pin_set, delay);
dudmuck 0:b6ec8db2edbf 79 printf("delay:%u\r\n", delay);
dudmuck 0:b6ec8db2edbf 80 } else
dudmuck 0:b6ec8db2edbf 81 printf("cmd? %02x\r\n", radio.rx_buf[0]);
dudmuck 0:b6ec8db2edbf 82 } else
dudmuck 0:b6ec8db2edbf 83 printf("crc fail %04x, %04x\r\n", rx_crc, crc);
dudmuck 0:b6ec8db2edbf 84 }
dudmuck 0:b6ec8db2edbf 85
dudmuck 0:b6ec8db2edbf 86 int main()
dudmuck 0:b6ec8db2edbf 87 {
dudmuck 0:b6ec8db2edbf 88 printf("\r\nreset-rx\r\n");
dudmuck 0:b6ec8db2edbf 89 radio.rf_switch = rfsw_callback;
dudmuck 0:b6ec8db2edbf 90
dudmuck 0:b6ec8db2edbf 91 radio.set_frf_MHz(910.8);
dudmuck 0:b6ec8db2edbf 92 lora.enable();
dudmuck 0:b6ec8db2edbf 93 lora.setBw_KHz(500);
dudmuck 0:b6ec8db2edbf 94 lora.setSf(11);
dudmuck 0:b6ec8db2edbf 95
dudmuck 0:b6ec8db2edbf 96 /* RFO or PABOOST choice:
dudmuck 0:b6ec8db2edbf 97 * SX1276 shield: RFO if using 900MHz, or PA_BOOST if using 433MHz
dudmuck 0:b6ec8db2edbf 98 */
dudmuck 0:b6ec8db2edbf 99 rfsw.input();
dudmuck 0:b6ec8db2edbf 100 if (rfsw.read()) {
dudmuck 0:b6ec8db2edbf 101 printf("LAS\r\n");
dudmuck 0:b6ec8db2edbf 102 /* LAS HF=PA_BOOST LF=RFO */
dudmuck 0:b6ec8db2edbf 103 if (radio.HF)
dudmuck 0:b6ec8db2edbf 104 radio.RegPaConfig.bits.PaSelect = 1;
dudmuck 0:b6ec8db2edbf 105 else
dudmuck 0:b6ec8db2edbf 106 radio.RegPaConfig.bits.PaSelect = 0;
dudmuck 0:b6ec8db2edbf 107 } else {
dudmuck 0:b6ec8db2edbf 108 /* MAS shield board, only RFO TX */
dudmuck 0:b6ec8db2edbf 109 radio.RegPaConfig.bits.PaSelect = 0;
dudmuck 0:b6ec8db2edbf 110 printf("MAS\r\n");
dudmuck 0:b6ec8db2edbf 111 }
dudmuck 0:b6ec8db2edbf 112 rfsw.output();
dudmuck 0:b6ec8db2edbf 113 radio.write_reg(REG_PACONFIG, radio.RegPaConfig.octet);
dudmuck 0:b6ec8db2edbf 114
dudmuck 0:b6ec8db2edbf 115 lora.start_rx(RF_OPMODE_RECEIVER);
dudmuck 0:b6ec8db2edbf 116
dudmuck 0:b6ec8db2edbf 117 for (;;) {
dudmuck 0:b6ec8db2edbf 118 if (lora.service() == SERVICE_READ_FIFO) {
dudmuck 0:b6ec8db2edbf 119 /*int i;
dudmuck 0:b6ec8db2edbf 120 for (i = 0; i < lora.RegRxNbBytes; i++) {
dudmuck 0:b6ec8db2edbf 121 printf("%02x ", radio.rx_buf[i]);
dudmuck 0:b6ec8db2edbf 122 }
dudmuck 0:b6ec8db2edbf 123 printf("\r\n");*/
dudmuck 0:b6ec8db2edbf 124 get_alarm();
dudmuck 0:b6ec8db2edbf 125 }
dudmuck 0:b6ec8db2edbf 126 }
dudmuck 0:b6ec8db2edbf 127 }