Polytech school project. RICM4 students, see http://air.imag.fr/index.php/Projets-2016-2017-Station_de_pompage_connect%C3%A9e for more information
Fork of SX1276PingPong by
main.cpp
00001 #include "mbed.h" 00002 #include "main.h" 00003 #include "sx1276-hal.h" 00004 #include "debug.h" 00005 #include "ordre.h" 00006 #include "trame.h" 00007 00008 /* Set this flag to '1' to display debug messages on the console 00009 Must be 0 in order to work with the projet 00010 */ 00011 #define DEBUG_MESSAGE 0 00012 00013 #define ID_DEVICE 0x24 00014 #define ID_STATION 0x55 00015 #define TEMPS_ATTENTE_COM 20 00016 00017 /* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */ 00018 #define USE_MODEM_LORA 1 00019 #define USE_MODEM_FSK !USE_MODEM_LORA 00020 00021 #define RF_FREQUENCY 865000000 // Hz 00022 #define TX_OUTPUT_POWER 14 // 14 dBm 00023 00024 #if USE_MODEM_LORA == 1 00025 00026 #define LORA_BANDWIDTH 2 // [0: 125 kHz, 00027 // 1: 250 kHz, 00028 // 2: 500 kHz, 00029 // 3: Reserved] 00030 #define LORA_SPREADING_FACTOR 7 // [SF7..SF12] 00031 #define LORA_CODINGRATE 1 // [1: 4/5, 00032 // 2: 4/6, 00033 // 3: 4/7, 00034 // 4: 4/8] 00035 #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx 00036 #define LORA_SYMBOL_TIMEOUT 5 // Symbols 00037 #define LORA_FIX_LENGTH_PAYLOAD_ON false 00038 #define LORA_FHSS_ENABLED false 00039 #define LORA_NB_SYMB_HOP 4 00040 #define LORA_IQ_INVERSION_ON false 00041 #define LORA_CRC_ENABLED true 00042 00043 #elif USE_MODEM_FSK == 1 00044 00045 #define FSK_FDEV 25000 // Hz 00046 #define FSK_DATARATE 19200 // bps 00047 #define FSK_BANDWIDTH 50000 // Hz 00048 #define FSK_AFC_BANDWIDTH 83333 // Hz 00049 #define FSK_PREAMBLE_LENGTH 5 // Same for Tx and Rx 00050 #define FSK_FIX_LENGTH_PAYLOAD_ON false 00051 #define FSK_CRC_ENABLED true 00052 00053 #else 00054 #error "Please define a modem in the compiler options." 00055 #endif 00056 00057 #define RX_TIMEOUT_VALUE 3500000 // in us 00058 #define BUFFER_SIZE 32 // Define the payload size here 00059 00060 #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) ) 00061 DigitalOut led(LED2); 00062 #else 00063 DigitalOut led(LED1); 00064 #endif 00065 00066 /* 00067 * Global variables declarations 00068 */ 00069 typedef enum 00070 { 00071 LOWPOWER = 0, 00072 IDLE, 00073 00074 RX, 00075 RX_TIMEOUT, 00076 RX_ERROR, 00077 00078 TX, 00079 TX_TIMEOUT, 00080 00081 CAD, 00082 CAD_DONE 00083 }AppStates_t; 00084 00085 volatile AppStates_t State = LOWPOWER; 00086 00087 /*! 00088 * Radio events function pointer 00089 */ 00090 static RadioEvents_t RadioEvents; 00091 00092 /* 00093 * Global variables declarations 00094 */ 00095 SX1276MB1xAS Radio( NULL ); 00096 00097 uint16_t BufferSize = BUFFER_SIZE; 00098 uint8_t Buffer[BUFFER_SIZE]; 00099 00100 int16_t RssiValue = 0.0; 00101 int8_t SnrValue = 0.0; 00102 00103 //Connection serie avec le PC 00104 Serial pc(USBTX, USBRX); 00105 00106 char portCom[3]; 00107 bool ordreRecuPortCom = false; 00108 00109 00110 int main() 00111 { 00112 uint8_t i; 00113 bool isMaster = false; 00114 00115 debug_if( DEBUG_MESSAGE, "\n\n\r SX1276 Server Demo Application \n\n\r" ); 00116 00117 // Initialize Radio driver 00118 RadioEvents.TxDone = OnTxDone; 00119 RadioEvents.RxDone = OnRxDone; 00120 RadioEvents.RxError = OnRxError; 00121 RadioEvents.TxTimeout = OnTxTimeout; 00122 RadioEvents.RxTimeout = OnRxTimeout; 00123 Radio.Init( &RadioEvents ); 00124 00125 // verify the connection with the board 00126 while( Radio.Read( REG_VERSION ) == 0x00 ) 00127 { 00128 debug_if( DEBUG_MESSAGE, "Radio could not be detected!\n\r", NULL ); 00129 wait( 1 ); 00130 } 00131 00132 debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1LAS ) ) , "\n\r > Board Type: SX1276MB1LAS < \n\r" ); 00133 debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1MAS ) ) , "\n\r > Board Type: SX1276MB1MAS < \n\r" ); 00134 00135 Radio.SetChannel( RF_FREQUENCY ); 00136 00137 #if USE_MODEM_LORA == 1 00138 00139 debug_if( DEBUG_MESSAGE & LORA_FHSS_ENABLED, "\n\n\r > LORA FHSS Mode < \n\n\r"); 00140 debug_if( DEBUG_MESSAGE & !LORA_FHSS_ENABLED, "\n\n\r > LORA Mode < \n\n\r"); 00141 00142 Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, 00143 LORA_SPREADING_FACTOR, LORA_CODINGRATE, 00144 LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, 00145 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, 00146 LORA_IQ_INVERSION_ON, 2000000 ); 00147 00148 Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, 00149 LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, 00150 LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0, 00151 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, 00152 LORA_IQ_INVERSION_ON, true ); 00153 00154 #elif USE_MODEM_FSK == 1 00155 00156 debug_if( DEBUG_MESSAGE, "\n\n\r > FSK Mode < \n\n\r"); 00157 Radio.SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0, 00158 FSK_DATARATE, 0, 00159 FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON, 00160 FSK_CRC_ENABLED, 0, 0, 0, 2000000 ); 00161 00162 Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE, 00163 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH, 00164 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED, 00165 0, 0, false, true ); 00166 00167 #else 00168 00169 #error "Please define a modem in the compiler options." 00170 00171 #endif 00172 00173 debug_if( DEBUG_MESSAGE, "Starting Server loop\r\n" ); 00174 00175 led = 0; 00176 00177 //ACK Message Par defaut 00178 char AckMsg[] = {0x24, 0x55, 0x00}; 00179 00180 Radio.Rx( RX_TIMEOUT_VALUE ); 00181 00182 //Varialbe de controle 00183 bool ackOrData = true; //true = ack, false = Data 00184 00185 while( 1 ) 00186 { 00187 switch( State ) 00188 { 00189 case RX: 00190 if( BufferSize > 0 ) 00191 { 00192 TrameData trameRecue((char*)Buffer); 00193 if( trameRecue.getIdStation() == (char) ID_STATION ) 00194 { 00195 00196 led = !led; 00197 debug_if( DEBUG_MESSAGE, "...DATA\r\n" ); 00198 00199 //Envoit sur le port COM 00200 pc.printf(trameRecue.creerTrame()); 00201 00202 // Send the ACK 00203 AckMsg[0] = trameRecue.getIdEmetteur(); 00204 00205 strcpy( ( char* )Buffer, ( char* )AckMsg ); 00206 // We fill the buffer with numbers for the payload 00207 for( i = 3; i < BufferSize; i++ ) 00208 { 00209 Buffer[i] = i - 4; 00210 } 00211 wait( 5.0); 00212 ackOrData = true; 00213 Radio.Send( Buffer, BufferSize ); 00214 00215 } 00216 else // valid reception but not expected 00217 { 00218 Radio.Rx( RX_TIMEOUT_VALUE ); 00219 } 00220 } 00221 State = LOWPOWER; 00222 break; 00223 case TX: 00224 led = !led; 00225 if(ackOrData){ 00226 debug_if( DEBUG_MESSAGE, "ACK...\r\n" ); 00227 //Après Envoie du ACK, on ecoute la réponse du PC (pour l'ordre) 00228 led = 1; 00229 pc.scanf("%s",portCom); 00230 led = 0; 00231 //Mise a jour des variables de controle 00232 ordreRecuPortCom = true; 00233 ackOrData = false; 00234 } 00235 else{ 00236 debug_if( DEBUG_MESSAGE, "ORDRE...\r\n" ); 00237 Radio.Rx( RX_TIMEOUT_VALUE ); 00238 } 00239 State = LOWPOWER; 00240 break; 00241 case RX_TIMEOUT: 00242 Radio.Rx( RX_TIMEOUT_VALUE ); 00243 State = LOWPOWER; 00244 break; 00245 case RX_ERROR: 00246 // We have received a Packet with a CRC error, send reply as if packet was correct 00247 //Do nothing 00248 State = LOWPOWER; 00249 break; 00250 case TX_TIMEOUT: 00251 Radio.Rx( RX_TIMEOUT_VALUE ); 00252 State = LOWPOWER; 00253 break; 00254 case LOWPOWER: 00255 if(ordreRecuPortCom){ 00256 Ordre o((char *)portCom); 00257 debug_if( DEBUG_MESSAGE, "Ordre recu sur le port COM\r\n" ); 00258 led=!led; 00259 if(o.getOrdreAFaire() == 1){ 00260 //Envoie de l'ordre. 00261 strcpy( ( char* )Buffer, ( char* )portCom ); 00262 for( i = 3; i < BufferSize; i++ ) 00263 { 00264 Buffer[i] = i - 4; 00265 } 00266 wait_ms( 10 ); 00267 ordreRecuPortCom = false; 00268 Radio.Send( Buffer, BufferSize ); 00269 }else{ 00270 debug_if( DEBUG_MESSAGE, "Pas d'activation de la pompe\r\n" ); 00271 Radio.Rx( RX_TIMEOUT_VALUE ); 00272 } 00273 ordreRecuPortCom = false; 00274 00275 break; 00276 default: 00277 State = LOWPOWER; 00278 break; 00279 } 00280 } 00281 } 00282 00283 void OnTxDone( void ) 00284 { 00285 Radio.Sleep( ); 00286 State = TX; 00287 debug_if( DEBUG_MESSAGE, "> OnTxDone\n\r" ); 00288 } 00289 00290 void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) 00291 { 00292 Radio.Sleep( ); 00293 BufferSize = size; 00294 memcpy( Buffer, payload, BufferSize ); 00295 RssiValue = rssi; 00296 SnrValue = snr; 00297 State = RX; 00298 debug_if( DEBUG_MESSAGE, "> OnRxDone\n\r" ); 00299 } 00300 00301 void OnTxTimeout( void ) 00302 { 00303 Radio.Sleep( ); 00304 State = TX_TIMEOUT; 00305 debug_if( DEBUG_MESSAGE, "> OnTxTimeout\n\r" ); 00306 } 00307 00308 void OnRxTimeout( void ) 00309 { 00310 Radio.Sleep( ); 00311 Buffer[ BufferSize ] = 0; 00312 State = RX_TIMEOUT; 00313 debug_if( DEBUG_MESSAGE, "> OnRxTimeout\n\r" ); 00314 } 00315 00316 void OnRxError( void ) 00317 { 00318 Radio.Sleep( ); 00319 State = RX_ERROR; 00320 debug_if( DEBUG_MESSAGE, "> OnRxError\n\r" ); 00321 } 00322
Generated on Thu Jul 14 2022 10:24:05 by 1.7.2