raises pin at time instructed by received LoRa packet.
Dependencies: sx126x sx12xx_hal
main.cpp
00001 #include "radio.h" 00002 00003 DigitalOut myled(LED1); 00004 00005 #if defined(SX127x_H) || defined(SX126x_H) 00006 #define BW_KHZ 500 00007 #define SPREADING_FACTOR 11 00008 #define CF_HZ 910800000 00009 #elif defined(SX128x_H) 00010 #define BW_KHZ 200 00011 #define SPREADING_FACTOR 11 00012 #define CF_HZ 2487000000 00013 #endif 00014 00015 #ifdef TARGET_DISCO_L072CZ_LRWAN1 00016 DigitalOut pinA(PB_12); 00017 DigitalOut pinB(PB_13); 00018 DigitalOut pinC(PB_14); 00019 DigitalOut pinD(PB_15); 00020 #else 00021 DigitalOut pinA(PC_3); 00022 DigitalOut pinB(PC_2); 00023 DigitalOut pinC(PC_6); 00024 DigitalOut pinD(PC_8); 00025 #endif /* !TARGET_DISCO_L072CZ_LRWAN1 */ 00026 00027 DigitalOut* pin; 00028 Timeout to; 00029 00030 #define PIN_ASSERT_us 500000 00031 00032 #define CMD_PINA 0x02 00033 #define CMD_PINB 0x03 00034 #define CMD_PINC 0x06 00035 #define CMD_PIND 0x08 00036 00037 /**********************************************************************/ 00038 00039 void alarm_pin_clr() 00040 { 00041 pin->write(0); 00042 } 00043 00044 void alarm_pin_set() 00045 { 00046 pin->write(1); 00047 to.attach_us(&alarm_pin_clr, PIN_ASSERT_us); 00048 } 00049 00050 static uint16_t crc_ccitt( uint8_t *buffer, uint16_t length ) 00051 { 00052 // The CRC calculation follows CCITT 00053 const uint16_t polynom = 0x1021; 00054 // CRC initial value 00055 uint16_t crc = 0x0000; 00056 00057 if( buffer == NULL ) 00058 { 00059 return 0; 00060 } 00061 00062 for( uint16_t i = 0; i < length; ++i ) 00063 { 00064 crc ^= ( uint16_t ) buffer[i] << 8; 00065 for( uint16_t j = 0; j < 8; ++j ) 00066 { 00067 crc = ( crc & 0x8000 ) ? ( crc << 1 ) ^ polynom : ( crc << 1 ); 00068 } 00069 } 00070 00071 return crc; 00072 } 00073 00074 void get_alarm() 00075 { 00076 uint16_t rx_crc, crc = crc_ccitt(Radio::radio.rx_buf, 5); 00077 rx_crc = Radio::radio.rx_buf[5]; 00078 rx_crc <<= 8; 00079 rx_crc += Radio::radio.rx_buf[6]; 00080 //printf("%u) crc rx:%04x, calc:%04x\r\n", lora.RegRxNbBytes, rx_crc, crc); 00081 if (crc == rx_crc) { 00082 uint8_t c = Radio::radio.rx_buf[0]; 00083 if (c == CMD_PINA || c == CMD_PINB || c == CMD_PINC || c == CMD_PIND) { 00084 unsigned delay; 00085 delay = Radio::radio.rx_buf[1]; 00086 delay <<= 8; 00087 delay += Radio::radio.rx_buf[2]; 00088 delay <<= 8; 00089 delay += Radio::radio.rx_buf[3]; 00090 delay <<= 8; 00091 delay += Radio::radio.rx_buf[4]; 00092 switch (c) { 00093 case CMD_PINA: pin = &pinA; break; 00094 case CMD_PINB: pin = &pinB; break; 00095 case CMD_PINC: pin = &pinC; break; 00096 case CMD_PIND: pin = &pinD; break; 00097 } 00098 to.attach_us(&alarm_pin_set, delay); 00099 printf("delay:%u\r\n", delay); 00100 } else 00101 printf("cmd? %02x\r\n", Radio::radio.rx_buf[0]); 00102 } else 00103 printf("crc fail %04x, %04x\r\n", rx_crc, crc); 00104 } 00105 00106 void txDoneCB() 00107 { 00108 } 00109 00110 void rxDoneCB(uint8_t size, float Rssi, float Snr) 00111 { 00112 get_alarm(); 00113 printf("%.1fdBm snr:%.1fdB ", Rssi, Snr); 00114 } 00115 00116 const RadioEvents_t rev = { 00117 /* Dio0_top_half */ NULL, 00118 /* TxDone_topHalf */ NULL, 00119 /* TxDone_botHalf */ txDoneCB, 00120 /* TxTimeout */ NULL, 00121 /* RxDone */ rxDoneCB, 00122 /* RxTimeout */ NULL, 00123 /* RxError */ NULL, 00124 /* FhssChangeChannel */NULL, 00125 /* CadDone */ NULL 00126 }; 00127 00128 00129 int main() 00130 { 00131 printf("\r\nreset-rx\r\n"); 00132 00133 00134 Radio::Init(&rev); 00135 00136 Radio::Standby(); 00137 Radio::LoRaModemConfig(BW_KHZ, SPREADING_FACTOR, 1); 00138 Radio::LoRaPacketConfig(8, false, true, false); // preambleLen, fixLen, crcOn, invIQ 00139 Radio::SetChannel(CF_HZ); 00140 00141 Radio::Rx(0); 00142 00143 for (;;) { 00144 Radio::service(); 00145 } 00146 }
Generated on Tue Jul 19 2022 22:05:28 by 1.7.2