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