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.
Fork of DISCO-L072CZ-LRWAN1_LoRa_PingPong by
Transmitter.cpp
00001 #include "mbed.h" 00002 #include "PinMap.h" 00003 #include "main.h" 00004 #include "sx1276-mbed-hal.h" 00005 00006 00007 #ifdef FEATURE_LORA 00008 00009 /* Set this flag to '1' to display debug messages on the console */ 00010 #define DEBUG_MESSAGE 1 00011 00012 /* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */ 00013 #define USE_MODEM_LORA 1 00014 #define USE_MODEM_FSK !USE_MODEM_LORA 00015 #define RF_FREQUENCY RF_FREQUENCY_868_1 // Hz 00016 #define TX_OUTPUT_POWER 14 // 14 dBm 00017 00018 #if USE_MODEM_LORA == 1 00019 00020 #define LORA_BANDWIDTH 125000 // LoRa default, details in SX1276::BandwidthMap 00021 #define LORA_SPREADING_FACTOR LORA_SF7 00022 #define LORA_CODINGRATE LORA_ERROR_CODING_RATE_4_5 00023 00024 #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx 00025 #define LORA_SYMBOL_TIMEOUT 5 // Symbols 00026 #define LORA_FIX_LENGTH_PAYLOAD_ON false 00027 #define LORA_FHSS_ENABLED false 00028 #define LORA_NB_SYMB_HOP 4 00029 #define LORA_IQ_INVERSION_ON false 00030 #define LORA_CRC_ENABLED true 00031 00032 #elif USE_MODEM_FSK == 1 00033 00034 #define FSK_FDEV 25000 // Hz 00035 #define FSK_DATARATE 19200 // bps 00036 #define FSK_BANDWIDTH 50000 // Hz 00037 #define FSK_AFC_BANDWIDTH 83333 // Hz 00038 #define FSK_PREAMBLE_LENGTH 5 // Same for Tx and Rx 00039 #define FSK_FIX_LENGTH_PAYLOAD_ON false 00040 #define FSK_CRC_ENABLED true 00041 00042 #else 00043 #error "Please define a modem in the compiler options." 00044 #endif 00045 00046 00047 #define BUFFER_SIZE 2048 00048 00049 /* 00050 * Global variables declarations 00051 */ 00052 typedef enum { 00053 LOWPOWER = 0, 00054 IDLE, 00055 00056 RX, 00057 RX_TIMEOUT, 00058 RX_ERROR, 00059 00060 TX, 00061 TX_TIMEOUT, 00062 00063 CAD, 00064 CAD_DONE 00065 } AppStates_t; 00066 00067 volatile AppStates_t State = LOWPOWER; 00068 00069 /*! 00070 * Radio events function pointer 00071 */ 00072 static RadioEvents_t RadioEvents; 00073 00074 /* 00075 * Global variables declarations 00076 */ 00077 SX1276Generic *Radio ; 00078 00079 uint16_t BufferSize = BUFFER_SIZE; 00080 uint8_t Buffer[BUFFER_SIZE]; 00081 00082 DigitalOut *led3; 00083 00084 BufferedSerial *serial; 00085 int Transmitter() 00086 { 00087 #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) ) 00088 DigitalOut *led = new DigitalOut(LED2); 00089 #elif defined(TARGET_NUCLEO_L073RZ) || defined(TARGET_DISCO_L072CZ_LRWAN1) 00090 DigitalOut *led = new DigitalOut(LED4); // RX red 00091 led3 = new DigitalOut(LED3); // TX blue 00092 #else 00093 DigitalOut *led = new DigitalOut(LED1); 00094 led3 = led; 00095 #endif 00096 00097 //////////////////////////////////////////////////////////////// 00098 *led3 = 1; 00099 00100 serial = new BufferedSerial(USBTX,USBRX); 00101 serial->baud(115200*2); 00102 serial->format(8); 00103 00104 //////////////////////////////////////////////////////////////// 00105 Radio = new SX1276Generic (NULL, MURATA_SX1276, 00106 LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET, 00107 LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5, 00108 LORA_ANT_RX, LORA_ANT_TX, LORA_ANT_BOOST, LORA_TCXO); 00109 00110 // Initialize Radio driver 00111 RadioEvents.TxDone = OnTxDone; 00112 RadioEvents.RxDone = OnRxDone; 00113 RadioEvents.RxError = OnRxError; 00114 RadioEvents.TxTimeout = OnTxTimeout; 00115 RadioEvents.RxTimeout = OnRxTimeout; 00116 if (Radio->Init( &RadioEvents ) == false) { 00117 while(Radio->Init( &RadioEvents ) == false) { 00118 serial->printf("Radio could not be detected!"); 00119 wait( 1 ); 00120 } 00121 } 00122 00123 00124 00125 00126 Radio->SetChannel(RF_FREQUENCY ); 00127 00128 00129 Radio->SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, 00130 LORA_SPREADING_FACTOR, LORA_CODINGRATE, 00131 LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, 00132 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, 00133 LORA_IQ_INVERSION_ON, 2000 ); 00134 00135 while( 1 ) { 00136 #ifdef TARGET_STM32L4 00137 WatchDogUpdate(); 00138 #endif 00139 00140 switch( State ) { 00141 case TX: 00142 State = LOWPOWER; 00143 break; 00144 case RX_TIMEOUT: 00145 State = LOWPOWER; 00146 break; 00147 case RX_ERROR: 00148 State = LOWPOWER; 00149 break; 00150 case TX_TIMEOUT: 00151 State = LOWPOWER; 00152 break; 00153 case LOWPOWER: 00154 // going to send" ); 00155 int sendSize = 0; 00156 serial->printf("reached lowpower\n"); 00157 int i = 0; 00158 while(i < BufferSize) { 00159 char temp; 00160 while(!serial->readable()) ; 00161 temp = serial->getc(); 00162 if(temp == '\n') break; 00163 Buffer[i++] = temp; 00164 sendSize++; 00165 } 00166 00167 Buffer[i++] = '\n'; 00168 Buffer[i++] = 0; 00169 00170 sendSize++; 00171 sendSize++; 00172 00173 serial->printf("Sending: %s", Buffer); 00174 Radio->Send( Buffer, sendSize); 00175 wait(0.5); 00176 *led3 = !*led3 ; 00177 break; 00178 default: 00179 State = LOWPOWER; 00180 break; 00181 } 00182 } 00183 } 00184 00185 void OnTxDone(void *radio, void *userThisPtr, void *userData) 00186 { 00187 Radio->Sleep( ); 00188 State = TX; 00189 if (DEBUG_MESSAGE) 00190 serial->printf("> OnTxDone [7]"); 00191 } 00192 00193 void OnRxDone(void *radio, void *userThisPtr, void *userData, uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) 00194 { 00195 Radio->Sleep( ); 00196 BufferSize = size; 00197 memcpy( Buffer, payload, BufferSize ); 00198 State = LOWPOWER; 00199 //if (DEBUG_MESSAGE) 00200 // dprintf("> OnRxDone: RssiValue=%d dBm, SnrValue=%d", rssi, snr); 00201 //dump("Data:", payload, size); 00202 } 00203 00204 void OnTxTimeout(void *radio, void *userThisPtr, void *userData) 00205 { 00206 *led3 = 0; 00207 Radio->Sleep( ); 00208 State = LOWPOWER; 00209 //if(DEBUG_MESSAGE) 00210 // serial->printf("> OnTxTimeout [8] "); 00211 } 00212 00213 void OnRxTimeout(void *radio, void *userThisPtr, void *userData) 00214 { 00215 *led3 = 0; 00216 Radio->Sleep( ); 00217 State = LOWPOWER; 00218 if (DEBUG_MESSAGE) 00219 dprintf("> OnRxTimeout [9] "); 00220 } 00221 00222 void OnRxError(void *radio, void *userThisPtr, void *userData) 00223 { 00224 Radio->Sleep( ); 00225 State = LOWPOWER; 00226 if (DEBUG_MESSAGE) 00227 dprintf("> OnRxError [100] "); 00228 } 00229 00230 #endif
Generated on Wed Jul 13 2022 21:19:47 by
1.7.2
