Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
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 PwmOut pwmA(PB_11); /* CN10-18 */ 00016 PwmOut pwmB(PA_15); /* CN7-17 */ 00017 PwmOut pwmC(PB_1); /* CN10-24 */ 00018 PwmOut pwmD(PC_6); /* CN10-4 */ 00019 00020 PwmOut* pwmPtr; 00021 uint8_t pwm_hz; 00022 uint8_t pwm_duty; 00023 Timeout to; 00024 00025 #define PIN_ASSERT_us 500000 00026 00027 #define CMD_PWM_A 0x02 00028 #define CMD_PWM_B 0x03 00029 #define CMD_PWM_C 0x04 00030 #define CMD_PWM_D 0x05 00031 00032 /**********************************************************************/ 00033 00034 void alarm_pin_clr() 00035 { 00036 pwmPtr->write(0); 00037 } 00038 00039 void alarm_pin_set() 00040 { 00041 pwmPtr->period(1.0 / pwm_hz); 00042 pwmPtr->write(pwm_duty / 255.0); 00043 to.attach_us(&alarm_pin_clr, PIN_ASSERT_us); 00044 printf("%uHz, duty:%.2f\r\n", pwm_hz, pwm_duty/255.0); 00045 } 00046 00047 static uint16_t crc_ccitt( uint8_t *buffer, uint16_t length ) 00048 { 00049 // The CRC calculation follows CCITT 00050 const uint16_t polynom = 0x1021; 00051 // CRC initial value 00052 uint16_t crc = 0x0000; 00053 00054 if( buffer == NULL ) 00055 { 00056 return 0; 00057 } 00058 00059 for( uint16_t i = 0; i < length; ++i ) 00060 { 00061 crc ^= ( uint16_t ) buffer[i] << 8; 00062 for( uint16_t j = 0; j < 8; ++j ) 00063 { 00064 crc = ( crc & 0x8000 ) ? ( crc << 1 ) ^ polynom : ( crc << 1 ); 00065 } 00066 } 00067 00068 return crc; 00069 } 00070 00071 void get_alarm() 00072 { 00073 uint16_t rx_crc, crc = crc_ccitt(Radio::radio.rx_buf, 7); 00074 rx_crc = Radio::radio.rx_buf[7]; 00075 rx_crc <<= 8; 00076 rx_crc += Radio::radio.rx_buf[8]; 00077 //printf("%u) crc rx:%04x, calc:%04x\r\n", lora.RegRxNbBytes, rx_crc, crc); 00078 if (crc == rx_crc) { 00079 uint8_t c = Radio::radio.rx_buf[0]; 00080 if (c == CMD_PWM_A || c == CMD_PWM_B || c == CMD_PWM_C || c == CMD_PWM_D) { 00081 pwm_hz = Radio::radio.rx_buf[1]; 00082 pwm_duty = Radio::radio.rx_buf[2]; 00083 unsigned delay; 00084 delay = Radio::radio.rx_buf[3]; 00085 delay <<= 8; 00086 delay += Radio::radio.rx_buf[4]; 00087 delay <<= 8; 00088 delay += Radio::radio.rx_buf[5]; 00089 delay <<= 8; 00090 delay += Radio::radio.rx_buf[6]; 00091 switch (c) { 00092 case CMD_PWM_A: pwmPtr = &pwmA; break; 00093 case CMD_PWM_B: pwmPtr = &pwmB; break; 00094 case CMD_PWM_C: pwmPtr = &pwmC; break; 00095 case CMD_PWM_D: pwmPtr = &pwmD; break; 00096 } 00097 to.attach_us(&alarm_pin_set, delay); 00098 printf("delay:%u\r\n", delay); 00099 } else 00100 printf("cmd? %02x\r\n", Radio::radio.rx_buf[0]); 00101 } else 00102 printf("crc fail %04x, %04x\r\n", rx_crc, crc); 00103 } 00104 00105 void txDoneCB() 00106 { 00107 } 00108 00109 void rxDoneCB(uint8_t size, float Rssi, float Snr) 00110 { 00111 get_alarm(); 00112 printf("%.1fdBm snr:%.1fdB ", Rssi, Snr); 00113 } 00114 00115 const RadioEvents_t rev = { 00116 /* Dio0_top_half */ NULL, 00117 /* TxDone_topHalf */ NULL, 00118 /* TxDone_botHalf */ txDoneCB, 00119 /* TxTimeout */ NULL, 00120 /* RxDone */ rxDoneCB, 00121 /* RxTimeout */ NULL, 00122 /* RxError */ NULL, 00123 /* FhssChangeChannel */NULL, 00124 /* CadDone */ NULL 00125 }; 00126 00127 00128 int main() 00129 { 00130 printf("\r\nreset-rx\r\n"); 00131 00132 00133 Radio::Init(&rev); 00134 00135 Radio::Standby(); 00136 Radio::LoRaModemConfig(BW_KHZ, SPREADING_FACTOR, 1); 00137 Radio::LoRaPacketConfig(8, false, true, false); // preambleLen, fixLen, crcOn, invIQ 00138 Radio::SetChannel(CF_HZ); 00139 00140 Radio::Rx(0); 00141 00142 for (;;) { 00143 Radio::service(); 00144 } 00145 }
Generated on Fri Jul 15 2022 01:56:02 by
1.7.2