Thiago Lima / Mbed 2 deprecated Telemetria_RX

Dependencies:   mbed BufferedSerial SX1276GenericLib2

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* Includes */
00002 #include "mbed.h" /* Mbed include */
00003 
00004 /* Lora includes */
00005 #include "PinMap.h"
00006 #include "sx1276-mbed-hal.h"
00007 
00008 /* Serial communication include */
00009 #include "BufferedSerial.h"
00010 
00011 /* Set this flag to '1' to display debug messages on the console */
00012 #define DEBUG_MESSAGE   1
00013 
00014 /* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */
00015 #define USE_MODEM_LORA  1
00016 #define USE_MODEM_FSK   !USE_MODEM_LORA
00017 #define RF_FREQUENCY            RF_FREQUENCY_915_0 // Hz
00018 #define TX_OUTPUT_POWER         14                  // 14 dBm
00019 
00020 #if USE_MODEM_LORA == 1
00021 
00022 #define LORA_BANDWIDTH          125000  // LoRa default, details in SX1276::BandwidthMap
00023 #define LORA_SPREADING_FACTOR   LORA_SF7
00024 #define LORA_CODINGRATE         LORA_ERROR_CODING_RATE_4_5
00025 
00026 #define LORA_PREAMBLE_LENGTH    8       // Same for Tx and Rx
00027 #define LORA_SYMBOL_TIMEOUT     5       // Symbols
00028 #define LORA_FIX_LENGTH_PAYLOAD_ON  false
00029 #define LORA_FHSS_ENABLED       false  
00030 #define LORA_NB_SYMB_HOP        4     
00031 #define LORA_IQ_INVERSION_ON    false
00032 #define LORA_CRC_ENABLED        true
00033     
00034 #endif 
00035 
00036 #define RX_TIMEOUT_VALUE    0       // In ms
00037 #define TX_TIMEOUT_VALUE    1000000 // In ms
00038 
00039 //#define BUFFER_SIZE       32        // Define the payload size here
00040 #define BUFFER_SIZE         64      // Define the payload size here
00041 
00042 typedef struct {
00043             float p; // Pressure
00044             float temperatureHTS221; // Temperature from HTS221
00045             float humidity; // Humidity
00046             float temperatureLPS22HB; // Temperature from LPS22HB
00047             int32_t w[3]; // Angular velocity
00048             int32_t a[3]; // Acceleration of the accelerometer LSM303AGR
00049             int32_t ag[3]; // Acceleration of the accelerometer and gyroscope LSM6DSL 
00050             int32_t m [3]; // Heading 
00051 }Dados; // Data struct
00052         
00053 Dados dados;
00054 
00055 /* LoRa modem instances and configurations */
00056 
00057 static RadioEvents_t RadioEvents; // Calback functions struct
00058 
00059 SX1276Generic *Radio; // Definition of a Radio object
00060 
00061 bool received = false; // Flag to indicate the end of reception
00062 
00063 /* Configuration function */
00064 void SystemClock_Config(void);
00065 
00066 /* Callback functions prototypes */
00067 
00068 // Brief Function to be executed on Radio Tx Done event
00069 void OnTxDone(void *radio, void *userThisPtr, void *userData);
00070 
00071 // Brief Function to be executed on Radio Rx Done event
00072 void OnRxDone(void *radio, void *userThisPtr, void *userData, uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr );
00073 
00074 // Brief Function executed on Radio Tx Timeout event
00075 void OnTxTimeout(void *radio, void *userThisPtr, void *userData);
00076 
00077 // Brief Function executed on Radio Rx Timeout event
00078 void OnRxTimeout(void *radio, void *userThisPtr, void *userData);
00079 
00080 // Brief Function executed on Radio Rx Error event
00081 void OnRxError(void *radio, void *userThisPtr, void *userData);
00082 
00083 // Brief Function executed on Radio Fhss Change Channel event
00084 void OnFhssChangeChannel(void *radio, void *userThisPtr, void *userData, uint8_t channelIndex);
00085 
00086 // Brief Function executed on CAD Done event
00087 void OnCadDone(void *radio, void *userThisPtr, void *userData);
00088 
00089 /* Serial communication to debug program */
00090 BufferedSerial *ser;
00091 
00092 int main() {
00093     SystemClock_Config(); /* Synchronize clock for TX and RX boards */
00094     
00095     /* Serial configuration */
00096     if (DEBUG_MESSAGE) {
00097         ser = new BufferedSerial(USBTX, USBRX);
00098         ser->baud(115200);
00099         ser->format(8);
00100     }
00101     
00102     /* General Header*/
00103     if (DEBUG_MESSAGE)
00104         ser->printf("Telemetry Rx inicial version program\r\n\r\n");
00105     
00106     Radio = new SX1276Generic(NULL, MURATA_SX1276,
00107             LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET,
00108             LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5,
00109             LORA_ANT_RX, LORA_ANT_TX, LORA_ANT_BOOST, LORA_TCXO);
00110     
00111     if (DEBUG_MESSAGE) {        
00112         ser->printf("SX1276 Simple receiver aplication\r\n" );
00113         ser->printf("Frequency: %.1f\r\n", (double)RF_FREQUENCY/1000000.0);
00114         ser->printf("TXPower: %d dBm\r\n",  TX_OUTPUT_POWER);
00115         ser->printf("Bandwidth: %d Hz\r\n", LORA_BANDWIDTH);
00116         ser->printf("Spreading factor: SF%d\r\n", LORA_SPREADING_FACTOR);
00117     }
00118     
00119     // Initialize Radio driver
00120     RadioEvents.TxDone = OnTxDone;
00121     RadioEvents.RxDone = OnRxDone;
00122     RadioEvents.RxError = OnRxError;
00123     RadioEvents.TxTimeout = OnTxTimeout;
00124     RadioEvents.RxTimeout = OnRxTimeout; 
00125     
00126     // Initializes the radio    
00127     while (Radio->Init( &RadioEvents ) == false) {
00128         if (DEBUG_MESSAGE)
00129             ser->printf("Radio could not be detected!\r\n");
00130         wait( 1 );
00131     }
00132     
00133     // Display the board type
00134     switch(Radio->DetectBoardType()) {
00135         case SX1276MB1LAS:
00136             if (DEBUG_MESSAGE)
00137                 ser->printf(" > Board Type: SX1276MB1LAS <\r\n");
00138             break;
00139         case SX1276MB1MAS:
00140             if (DEBUG_MESSAGE)
00141                 ser->printf(" > Board Type: SX1276MB1LAS <\r\n");
00142         case MURATA_SX1276:
00143             if (DEBUG_MESSAGE)
00144                 ser->printf(" > Board Type: MURATA_SX1276_STM32L0 <\r\n");
00145             break;
00146         case RFM95_SX1276:
00147             if (DEBUG_MESSAGE) 
00148                 ser->printf(" > HopeRF RFM95xx <\r\n");
00149             break;
00150         default:
00151             if (DEBUG_MESSAGE)
00152                 ser->printf(" > Board Type: unknown <\r\n");
00153     }
00154     
00155     Radio->SetChannel(RF_FREQUENCY ); // Sets the frequency of the communication
00156     
00157     // Debug message of the state of fhss
00158     if (LORA_FHSS_ENABLED) {
00159         if (DEBUG_MESSAGE)
00160             ser->printf("             > LORA FHSS Mode <\r\n");
00161     }    
00162     if (!LORA_FHSS_ENABLED) {
00163         if (DEBUG_MESSAGE)
00164             ser->printf("             > LORA Mode <\r\n");
00165     }
00166     // Sets the configuration of the transmission     
00167     Radio->SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
00168                          LORA_SPREADING_FACTOR, LORA_CODINGRATE,
00169                          LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
00170                          LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, 
00171                          LORA_IQ_INVERSION_ON, 2000 );
00172     
00173     // Sets the configuration of the reception
00174     Radio->SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
00175                          LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
00176                          LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0,
00177                          LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, 
00178                          LORA_IQ_INVERSION_ON, true );
00179     if (DEBUG_MESSAGE)
00180         ser->printf("Starting Receive loop\r\n"); 
00181         
00182     Radio->Rx(RX_TIMEOUT_VALUE); // Puts the device in reception mode continuously
00183     
00184     while( 1 )
00185     {   
00186         //After the receiving, puts the device again in receive mode 
00187         if (received == true) {
00188             received = false;
00189             Radio->Rx(RX_TIMEOUT_VALUE); 
00190         }
00191     }
00192     
00193 }
00194 
00195 
00196 void SystemClock_Config(void)
00197 {
00198 #ifdef B_L072Z_LRWAN1_LORA
00199     /* 
00200      * The L072Z_LRWAN1_LORA clock setup is somewhat differnt from the Nucleo board.
00201      * It has no LSE.
00202      */
00203     RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
00204     RCC_OscInitTypeDef RCC_OscInitStruct = {0};
00205 
00206     /* Enable HSE Oscillator and Activate PLL with HSE as source */
00207     RCC_OscInitStruct.OscillatorType      = RCC_OSCILLATORTYPE_HSI;
00208     RCC_OscInitStruct.HSEState            = RCC_HSE_OFF;
00209     RCC_OscInitStruct.HSIState            = RCC_HSI_ON;
00210     RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
00211     RCC_OscInitStruct.PLL.PLLState        = RCC_PLL_ON;
00212     RCC_OscInitStruct.PLL.PLLSource       = RCC_PLLSOURCE_HSI;
00213     RCC_OscInitStruct.PLL.PLLMUL          = RCC_PLLMUL_6;
00214     RCC_OscInitStruct.PLL.PLLDIV          = RCC_PLLDIV_3;
00215 
00216     if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
00217         // Error_Handler();
00218     }
00219 
00220     /* Set Voltage scale1 as MCU will run at 32MHz */
00221     __HAL_RCC_PWR_CLK_ENABLE();
00222     __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
00223 
00224     /* Poll VOSF bit of in PWR_CSR. Wait until it is reset to 0 */
00225     while (__HAL_PWR_GET_FLAG(PWR_FLAG_VOS) != RESET) {};
00226 
00227     /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
00228     clocks dividers */
00229     RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
00230     RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
00231     RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
00232     RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
00233     RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
00234     if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) {
00235         // Error_Handler();
00236     }
00237 #endif
00238 }
00239 
00240 void OnTxDone(void *radio, void *userThisPtr, void *userData)
00241 {
00242     Radio->Sleep( );
00243     if (DEBUG_MESSAGE)
00244         ser->printf("> OnTxDone\r\n");
00245 }
00246 
00247 void OnRxDone(void *radio, void *userThisPtr, void *userData, uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
00248 {
00249     Radio->Sleep( );
00250     received = true;
00251     memcpy(&dados, payload, sizeof(dados));
00252     if (DEBUG_MESSAGE) {
00253         ser->printf("> OnRxDone: RssiValue=%d dBm, SnrValue=%d\r\n", rssi, snr);
00254         ser->printf("I  received %d mg, %d mg, %d mg, %d mg, %d mg, %d mg, %d mdps, %d mdps, %d mdps\r\n", dados.a[0], dados.a[1], dados.a[2], dados.ag[0], dados.ag[1], dados.ag[2], dados.w[0], dados.w[1], dados.w[2]);
00255         ser->printf("and %d mG, %d mG, %d mG, %g %%, %g C, %g C, %g mBar\r\n", dados.m[0], dados.m[1], dados.m[2], dados.humidity, dados.temperatureHTS221, dados.temperatureLPS22HB, dados.p);
00256     }    
00257 
00258 }
00259 
00260 void OnTxTimeout(void *radio, void *userThisPtr, void *userData)
00261 {
00262     Radio->Sleep( );
00263     if(DEBUG_MESSAGE)
00264         ser->printf("> OnTxTimeout\r\n");
00265 }
00266 
00267 void OnRxTimeout(void *radio, void *userThisPtr, void *userData)
00268 {
00269     Radio->Sleep( );
00270     if (DEBUG_MESSAGE)
00271         ser->printf("> OnRxTimeout\r\n");
00272 }
00273 
00274 void OnRxError(void *radio, void *userThisPtr, void *userData)
00275 {
00276     Radio->Sleep( );
00277     received = true;
00278     if (DEBUG_MESSAGE)
00279         ser->printf("> OnRxError\r\n");
00280 }