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 "user_platform.h" 00002 #include "radio.h" 00003 00004 #if defined(SX127x_H) || defined(SX126x_H) 00005 #define BW_KHZ 500 00006 #define SPREADING_FACTOR 11 00007 #define CF_HZ 910800000 00008 #if defined(SX126x_H) 00009 #define TX_DBM (Radio::chipType == CHIP_TYPE_SX1262 ? 20 : 14) 00010 #else 00011 #define TX_DBM 17 00012 #endif 00013 #elif defined(SX128x_H) 00014 #define BW_KHZ 200 00015 #define SPREADING_FACTOR 11 00016 #define CF_HZ 2487000000 00017 00018 #define TX_DBM 5 00019 #endif 00020 00021 #ifdef TARGET_DISCO_L072CZ_LRWAN1 00022 DigitalIn pinA(PB_12); 00023 DigitalIn pinB(PB_13); 00024 DigitalIn pinC(PB_14); 00025 DigitalIn pinD(PB_15); 00026 #elif defined(TARGET_FF_MORPHO) 00027 DigitalIn pinA(PC_3); 00028 DigitalIn pinB(PC_2); 00029 DigitalIn pinC(PC_6); 00030 DigitalIn pinD(PC_8); 00031 #endif 00032 00033 volatile struct _f_ { 00034 uint8_t enable_pin_A : 1; 00035 uint8_t enable_pin_B : 1; 00036 uint8_t enable_pin_C : 1; 00037 uint8_t enable_pin_D : 1; 00038 } flags; 00039 00040 Timer t; 00041 #define CMD_PINA 0x02 00042 #define CMD_PINB 0x03 00043 #define CMD_PINC 0x06 00044 #define CMD_PIND 0x08 00045 00046 volatile bool tx_done; 00047 00048 static uint16_t crc_ccitt( uint8_t *buffer, uint16_t length ) 00049 { 00050 // The CRC calculation follows CCITT 00051 const uint16_t polynom = 0x1021; 00052 // CRC initial value 00053 uint16_t crc = 0x0000; 00054 00055 if( buffer == NULL ) 00056 { 00057 return 0; 00058 } 00059 00060 for( uint16_t i = 0; i < length; ++i ) 00061 { 00062 crc ^= ( uint16_t ) buffer[i] << 8; 00063 for( uint16_t j = 0; j < 8; ++j ) 00064 { 00065 crc = ( crc & 0x8000 ) ? ( crc << 1 ) ^ polynom : ( crc << 1 ); 00066 } 00067 } 00068 00069 return crc; 00070 } 00071 00072 void transmit(unsigned target, uint8_t cmd) 00073 { 00074 unsigned t_diff; 00075 uint16_t crc; 00076 00077 Radio::radio.tx_buf[0] = cmd; 00078 t_diff = target - t.read_us(); 00079 Radio::radio.tx_buf[1] = t_diff >> 24; 00080 Radio::radio.tx_buf[2] = t_diff >> 16; 00081 Radio::radio.tx_buf[3] = t_diff >> 8; 00082 Radio::radio.tx_buf[4] = t_diff & 0xff; 00083 crc = crc_ccitt(Radio::radio.tx_buf, 5); 00084 Radio::radio.tx_buf[5] = crc >> 8; 00085 Radio::radio.tx_buf[6] = crc & 0xff; 00086 00087 Radio::Send(7, 0, 0, 0); 00088 00089 for (tx_done = false; !tx_done; ) 00090 Radio::service(); 00091 00092 printf("t_diff:%u crc:%04x\r\n", t_diff, crc); 00093 } 00094 00095 #define TARGET_LATENCY 2000000 00096 void send_alarm(uint8_t cmd) 00097 { 00098 int i; 00099 unsigned target = t.read_us() + TARGET_LATENCY; 00100 printf("send_alarm() %u\n", target); 00101 00102 for (i = 0; i < 5; i++) { 00103 transmit(target, cmd); 00104 wait(0.1); 00105 } 00106 } 00107 00108 void debounce(DigitalIn* pin, uint8_t cmd) 00109 { 00110 if (!pin->read()) { 00111 int i; 00112 for (i = 0; i < 5; i++) { 00113 wait(0.01); 00114 if (pin->read()) { 00115 printf("trans\r\n"); 00116 break; 00117 } 00118 } 00119 if (i == 5) 00120 send_alarm(cmd); 00121 00122 while (!pin->read()) 00123 ; 00124 } 00125 } 00126 00127 void txDoneCB() 00128 { 00129 tx_done = true; 00130 } 00131 00132 void rxDoneCB(uint8_t size, float Rssi, float Snr) 00133 { 00134 } 00135 00136 const RadioEvents_t rev = { 00137 /* Dio0_top_half */ NULL, 00138 /* TxDone_topHalf */ NULL, 00139 /* TxDone_botHalf */ txDoneCB, 00140 /* TxTimeout */ NULL, 00141 /* RxDone */ rxDoneCB, 00142 /* RxTimeout */ NULL, 00143 /* RxError */ NULL, 00144 /* FhssChangeChannel */NULL, 00145 /* CadDone */ NULL 00146 }; 00147 00148 int main() 00149 { 00150 printf("\r\nreset-tx\r\n"); 00151 00152 pinA.mode(PullUp); 00153 pinB.mode(PullUp); 00154 pinC.mode(PullUp); 00155 pinD.mode(PullUp); 00156 00157 wait(0.05); 00158 00159 if (pinA.read() == 0) { 00160 printf("pinA-disabled\r\n"); 00161 flags.enable_pin_A = 0; 00162 } else 00163 flags.enable_pin_A = 1; 00164 00165 if (pinB.read() == 0) { 00166 printf("pinB-disabled\r\n"); 00167 flags.enable_pin_B = 0; 00168 } else 00169 flags.enable_pin_B = 1; 00170 00171 if (pinC.read() == 0) { 00172 printf("pinC-disabled\r\n"); 00173 flags.enable_pin_C = 0; 00174 } else 00175 flags.enable_pin_C = 1; 00176 00177 if (pinD.read() == 0) { 00178 printf("pinD-disabled\r\n"); 00179 flags.enable_pin_D = 0; 00180 } else 00181 flags.enable_pin_D = 1; 00182 00183 t.start(); 00184 00185 Radio::Init(&rev); 00186 00187 Radio::Standby(); 00188 Radio::LoRaModemConfig(BW_KHZ, SPREADING_FACTOR, 1); 00189 Radio::LoRaPacketConfig(8, false, true, false); // preambleLen, fixLen, crcOn, invIQ 00190 Radio::SetChannel(CF_HZ); 00191 00192 Radio::set_tx_dbm(TX_DBM); 00193 00194 for (;;) { 00195 if (flags.enable_pin_A) 00196 debounce(&pinA, CMD_PINA); 00197 00198 if (flags.enable_pin_B) 00199 debounce(&pinB, CMD_PINB); 00200 00201 if (flags.enable_pin_C) 00202 debounce(&pinC, CMD_PINC); 00203 00204 if (flags.enable_pin_D) 00205 debounce(&pinD, CMD_PIND); 00206 } // ..for (;;) 00207 }
Generated on Thu Jul 21 2022 15:44:25 by
