L14-Click 1.0
STM32WLE5CC LoRaWAN Sensor Platform
Loading...
Searching...
No Matches
sys_sensors.c
Go to the documentation of this file.
1/* USER CODE BEGIN Header */
2/**
3 ******************************************************************************
4 * @file sys_sensors.c
5 * @author MCD Application Team
6 * @brief Manages the sensors on the application
7 ******************************************************************************
8 * @attention
9 *
10 * Copyright (c) 2026 STMicroelectronics.
11 * All rights reserved.
12 *
13 * This software is licensed under terms that can be found in the LICENSE file
14 * in the root directory of this software component.
15 * If no LICENSE file comes with this software, it is provided AS-IS.
16 *
17 ******************************************************************************
18 */
19/* USER CODE END Header */
20
21/* Includes ------------------------------------------------------------------*/
22#include "stdint.h"
23#include "platform.h"
24#include "sys_conf.h"
25#include "sys_sensors.h"
26#if defined (SENSOR_ENABLED) && (SENSOR_ENABLED == 0)
27#include "adc_if.h"
28#endif /* SENSOR_ENABLED */
29
30/* USER CODE BEGIN Includes */
31#if defined (SENSOR_ENABLED) && (SENSOR_ENABLED == 1)
32#if defined (X_NUCLEO_IKS01A2)
33#warning "IKS drivers are today available for several families but not stm32WL"
34#warning "up to the user adapt IKS low layer to map it on WL board driver"
35#warning "this code would work only if user provide necessary IKS and BSP layers"
36#include "iks01a2_env_sensors.h"
37#elif defined (X_NUCLEO_IKS01A3)
38
39/*
40## How to add IKS01A3 to STM32CubeWL
41 Note that LoRaWAN_End_Node Example is used as an example for steps below.
42 1. Open the LoRaWAN_End_Node CubeMX project by double-clicking on the LoRaWAN_End_Node.ioc under "STM32Cube_FW_WL_V1.x.x\Projects\NUCLEO-WL55JC\Applications\LoRaWAN\LoRaWAN_End_Node"
43 2. From the CubeMX project, click on "Software Packs"->"Manage Software Packs" to open the Embedded Software Packages Manager. Then, click on the "STMicroelectronics" tab, expand the X-CUBE-MEMS1, check the latest version of this pack (i.e. 9.0.0), and install. Then, close the Embedded Software Packages Manager.
44 3. From the CubeMX project, click on "Software Packs"->"Select Components" to open the Software Packs Component Selector, expand the X-CUBE-MEMS1 pack and select the "Board Extension IKS01A3" component by checking the respective box, and click OK.
45 4. From the CubeMX project, expand the "Connectivity" category and enable I2C2 on pins PA11 (I2C2_SDA) and PA12 (I2C2_SCK).
46 5. From the CubeMX project, expand the "Software Packs" category and enable the "Board Extension IKS01A3" by checking the box, and choose I2C2 under the "Found Solutions" menu.
47 6. From the CubeMX project, click the "Project Manager" section
48 - From the "Project Settings" section, select your Toolchain/IDE of choice (if CubeIDE, uncheck the "Generator Under Root" option).
49 - From the "Code Generator" section, select "Copy only the necessary library files".
50 7. Click "GENERATE CODE" to generate the code project with the MEMS drivers integrated.
51 8. From the code project, find and open the sys_conf.h and make the following edits
52 - Set the #define SENSOR_ENABLED to 1
53 - Set the #define LOW_POWER_DISABLE to 1 to prevent the device from entering low power mode. This is needed, since the I2C2 requires handling when exiting low power modes, so to prevent issues, best is to disable low power mode, however, if low power mode is desired, you'll have to re-initialize the I2C2 from PWR_ExitStopMode() in stm32_lpm_if.c, so you can just call HAL_I2C_Init() from there.
54 9. From the code project, find and open lora_app.h, and uncomment the following line
55 #define CAYENNE_LPP
56 10. From the code project properties, add X_NUCLEO_IKS01A3 Pre-processor Defined symbol.
57 11. Save all changes and build project
58 12. Connect the X-NUCLEO-IKS01A3 expansion board on the NUCLEO-WL55JC1
59 13. Load and run the code
60*/
61#warning "IKS drivers are today available for several families but not stm32WL, follow steps defined in sys_sensors.c"
62#include "iks01a3_env_sensors.h"
63#else /* not X_IKS01xx */
64#error "user to include its sensor drivers"
65#endif /* X_NUCLEO_IKS01xx */
66#elif !defined (SENSOR_ENABLED)
67#error SENSOR_ENABLED not defined
68#endif /* SENSOR_ENABLED */
69/* USER CODE END Includes */
70
71/* External variables ---------------------------------------------------------*/
72/* USER CODE BEGIN EV */
73
74/* USER CODE END EV */
75
76/* Private typedef -----------------------------------------------------------*/
77/* USER CODE BEGIN PTD */
78
79/* USER CODE END PTD */
80
81/* Private define ------------------------------------------------------------*/
82
83/* USER CODE BEGIN PD */
84#define STSOP_LATTITUDE ((float) 43.618622 ) /*!< default latitude position */
85#define STSOP_LONGITUDE ((float) 7.051415 ) /*!< default longitude position */
86#define MAX_GPS_POS ((int32_t) 8388607 ) /*!< 2^23 - 1 */
87#define HUMIDITY_DEFAULT_VAL 50.0f /*!< default humidity */
88#define TEMPERATURE_DEFAULT_VAL 18.0f /*!< default temperature */
89#define PRESSURE_DEFAULT_VAL 1000.0f /*!< default pressure */
90/* USER CODE END PD */
91
92/* Private macro -------------------------------------------------------------*/
93/* USER CODE BEGIN PM */
94
95/* USER CODE END PM */
96
97/* Private variables ---------------------------------------------------------*/
98
99/* USER CODE BEGIN PV */
100#if defined (SENSOR_ENABLED) && (SENSOR_ENABLED == 1)
101#if defined (X_NUCLEO_IKS01A2)
102#warning "IKS drivers are today available for several families but not stm32WL"
103#warning "up to the user adapt IKS low layer to map it on WL board driver"
104#warning "this code would work only if user provide necessary IKS and BSP layers"
105IKS01A2_ENV_SENSOR_Capabilities_t EnvCapabilities;
106#elif defined (X_NUCLEO_IKS01A3)
107IKS01A3_ENV_SENSOR_Capabilities_t EnvCapabilities;
108#else /* not X_IKS01Ax */
109#error "user to include its sensor drivers"
110#endif /* X_NUCLEO_IKS01 */
111#elif !defined (SENSOR_ENABLED)
112#error SENSOR_ENABLED not defined
113#endif /* SENSOR_ENABLED */
114/* USER CODE END PV */
115
116/* Private function prototypes -----------------------------------------------*/
117/* USER CODE BEGIN PFP */
118
119/* USER CODE END PFP */
120
121/* Exported functions --------------------------------------------------------*/
122int32_t EnvSensors_Read(sensor_t *sensor_data)
123{
124 /* USER CODE BEGIN EnvSensors_Read */
125 float HUMIDITY_Value = HUMIDITY_DEFAULT_VAL;
126 float TEMPERATURE_Value = TEMPERATURE_DEFAULT_VAL;
127 float PRESSURE_Value = PRESSURE_DEFAULT_VAL;
128
129#if defined (SENSOR_ENABLED) && (SENSOR_ENABLED == 1)
130#if (USE_IKS01A2_ENV_SENSOR_HTS221_0 == 1)
131 IKS01A2_ENV_SENSOR_GetValue(HTS221_0, ENV_HUMIDITY, &HUMIDITY_Value);
132 IKS01A2_ENV_SENSOR_GetValue(HTS221_0, ENV_TEMPERATURE, &TEMPERATURE_Value);
133#endif /* USE_IKS01A2_ENV_SENSOR_HTS221_0 */
134#if (USE_IKS01A2_ENV_SENSOR_LPS22HB_0 == 1)
135 IKS01A2_ENV_SENSOR_GetValue(LPS22HB_0, ENV_PRESSURE, &PRESSURE_Value);
136 IKS01A2_ENV_SENSOR_GetValue(LPS22HB_0, ENV_TEMPERATURE, &TEMPERATURE_Value);
137#endif /* USE_IKS01A2_ENV_SENSOR_LPS22HB_0 */
138#if (USE_IKS01A3_ENV_SENSOR_HTS221_0 == 1)
139 IKS01A3_ENV_SENSOR_GetValue(IKS01A3_HTS221_0, ENV_HUMIDITY, &HUMIDITY_Value);
140 IKS01A3_ENV_SENSOR_GetValue(IKS01A3_HTS221_0, ENV_TEMPERATURE, &TEMPERATURE_Value);
141#endif /* USE_IKS01A3_ENV_SENSOR_HTS221_0 */
142#if (USE_IKS01A3_ENV_SENSOR_LPS22HH_0 == 1)
143 IKS01A3_ENV_SENSOR_GetValue(IKS01A3_LPS22HH_0, ENV_PRESSURE, &PRESSURE_Value);
144 IKS01A3_ENV_SENSOR_GetValue(IKS01A3_LPS22HH_0, ENV_TEMPERATURE, &TEMPERATURE_Value);
145#endif /* USE_IKS01A3_ENV_SENSOR_LPS22HH_0 */
146#else
147 TEMPERATURE_Value = (SYS_GetTemperatureLevel() >> 8);
148#endif /* SENSOR_ENABLED */
149
150 sensor_data->humidity = HUMIDITY_Value;
151 sensor_data->temperature = TEMPERATURE_Value;
152 sensor_data->pressure = PRESSURE_Value;
153
154 sensor_data->latitude = (int32_t)((STSOP_LATTITUDE * MAX_GPS_POS) / 90);
155 sensor_data->longitude = (int32_t)((STSOP_LONGITUDE * MAX_GPS_POS) / 180);
156
157 return 0;
158 /* USER CODE END EnvSensors_Read */
159}
160
161int32_t EnvSensors_Init(void)
162{
163 int32_t ret = 0;
164 /* USER CODE BEGIN EnvSensors_Init */
165#if defined (SENSOR_ENABLED) && (SENSOR_ENABLED == 1)
166 /* Init */
167#if (USE_IKS01A2_ENV_SENSOR_HTS221_0 == 1)
168 ret = IKS01A2_ENV_SENSOR_Init(HTS221_0, ENV_TEMPERATURE | ENV_HUMIDITY);
169 if (ret != BSP_ERROR_NONE)
170 {
172 }
173#endif /* USE_IKS01A2_ENV_SENSOR_HTS221_0 */
174#if (USE_IKS01A2_ENV_SENSOR_LPS22HB_0 == 1)
175 ret = IKS01A2_ENV_SENSOR_Init(LPS22HB_0, ENV_TEMPERATURE | ENV_PRESSURE);
176 if (ret != BSP_ERROR_NONE)
177 {
179 }
180#endif /* USE_IKS01A2_ENV_SENSOR_LPS22HB_0 */
181#if (USE_IKS01A3_ENV_SENSOR_HTS221_0 == 1)
182 ret = IKS01A3_ENV_SENSOR_Init(IKS01A3_HTS221_0, ENV_TEMPERATURE | ENV_HUMIDITY);
183 if (ret != BSP_ERROR_NONE)
184 {
186 }
187#endif /* USE_IKS01A3_ENV_SENSOR_HTS221_0 */
188#if (USE_IKS01A3_ENV_SENSOR_LPS22HH_0 == 1)
189 ret = IKS01A3_ENV_SENSOR_Init(IKS01A3_LPS22HH_0, ENV_TEMPERATURE | ENV_PRESSURE);
190 if (ret != BSP_ERROR_NONE)
191 {
193 }
194#endif /* USE_IKS01A3_ENV_SENSOR_LPS22HH_0 */
195
196 /* Enable */
197#if (USE_IKS01A2_ENV_SENSOR_HTS221_0 == 1)
198 ret = IKS01A2_ENV_SENSOR_Enable(HTS221_0, ENV_HUMIDITY);
199 if (ret != BSP_ERROR_NONE)
200 {
202 }
203 ret = IKS01A2_ENV_SENSOR_Enable(HTS221_0, ENV_TEMPERATURE);
204 if (ret != BSP_ERROR_NONE)
205 {
207 }
208#endif /* USE_IKS01A2_ENV_SENSOR_HTS221_0 */
209#if (USE_IKS01A2_ENV_SENSOR_LPS22HB_0 == 1)
210 ret = IKS01A2_ENV_SENSOR_Enable(LPS22HB_0, ENV_PRESSURE);
211 if (ret != BSP_ERROR_NONE)
212 {
214 }
215 ret = IKS01A2_ENV_SENSOR_Enable(LPS22HB_0, ENV_TEMPERATURE);
216 if (ret != BSP_ERROR_NONE)
217 {
219 }
220#endif /* USE_IKS01A2_ENV_SENSOR_LPS22HB_0 */
221#if (USE_IKS01A3_ENV_SENSOR_HTS221_0 == 1)
222 ret = IKS01A3_ENV_SENSOR_Enable(IKS01A3_HTS221_0, ENV_HUMIDITY);
223 if (ret != BSP_ERROR_NONE)
224 {
226 }
227 ret = IKS01A3_ENV_SENSOR_Enable(IKS01A3_HTS221_0, ENV_TEMPERATURE);
228 if (ret != BSP_ERROR_NONE)
229 {
231 }
232#endif /* USE_IKS01A3_ENV_SENSOR_HTS221_0 */
233#if (USE_IKS01A3_ENV_SENSOR_LPS22HH_0 == 1)
234 ret = IKS01A3_ENV_SENSOR_Enable(IKS01A3_LPS22HH_0, ENV_PRESSURE);
235 if (ret != BSP_ERROR_NONE)
236 {
238 }
239 ret = IKS01A3_ENV_SENSOR_Enable(IKS01A3_LPS22HH_0, ENV_TEMPERATURE);
240 if (ret != BSP_ERROR_NONE)
241 {
243 }
244#endif /* USE_IKS01A3_ENV_SENSOR_LPS22HH_0 */
245
246 /* Get capabilities */
247#if (USE_IKS01A2_ENV_SENSOR_HTS221_0 == 1)
248 ret = IKS01A2_ENV_SENSOR_GetCapabilities(HTS221_0, &EnvCapabilities);
249 if (ret != BSP_ERROR_NONE)
250 {
252 }
253#endif /* USE_IKS01A2_ENV_SENSOR_HTS221_0 */
254#if (USE_IKS01A2_ENV_SENSOR_LPS22HB_0 == 1)
255 ret = IKS01A2_ENV_SENSOR_GetCapabilities(LPS22HB_0, &EnvCapabilities);
256 if (ret != BSP_ERROR_NONE)
257 {
259 }
260#endif /* USE_IKS01A2_ENV_SENSOR_LPS22HB_0 */
261#if (USE_IKS01A3_ENV_SENSOR_HTS221_0 == 1)
262 ret = IKS01A3_ENV_SENSOR_GetCapabilities(IKS01A3_HTS221_0, &EnvCapabilities);
263 if (ret != BSP_ERROR_NONE)
264 {
266 }
267#endif /* USE_IKS01A3_ENV_SENSOR_HTS221_0 */
268#if (USE_IKS01A3_ENV_SENSOR_LPS22HH_0 == 1)
269 ret = IKS01A3_ENV_SENSOR_GetCapabilities(IKS01A3_LPS22HH_0, &EnvCapabilities);
270 if (ret != BSP_ERROR_NONE)
271 {
273 }
274#endif /* USE_IKS01A3_ENV_SENSOR_LPS22HH_0 */
275
276#elif !defined (SENSOR_ENABLED)
277#error SENSOR_ENABLED not defined
278#endif /* SENSOR_ENABLED */
279 /* USER CODE END EnvSensors_Init */
280 return ret;
281}
282
283/* USER CODE BEGIN EF */
284
285/* USER CODE END EF */
286
287/* Private Functions Definition -----------------------------------------------*/
288/* USER CODE BEGIN PrFD */
289
290/* USER CODE END PrFD */
Header for ADC interface configuration.
int16_t SYS_GetTemperatureLevel(void)
Get the current temperature.
Definition adc_if.c:97
void Error_Handler(void)
Default error handler called by HAL on unrecoverable errors. Disables interrupts and enters an infini...
Definition main.c:505
Header for General HW instances configuration.
float temperature
Definition sys_sensors.h:41
float humidity
Definition sys_sensors.h:42
float pressure
Definition sys_sensors.h:40
int32_t longitude
Definition sys_sensors.h:44
int32_t latitude
Definition sys_sensors.h:43
Applicative configuration, e.g. : debug, trace, low power, sensors.
#define STSOP_LATTITUDE
Definition sys_sensors.c:84
int32_t EnvSensors_Read(sensor_t *sensor_data)
Environmental sensor read.
#define MAX_GPS_POS
Definition sys_sensors.c:86
int32_t EnvSensors_Init(void)
initialize the environmental sensor
#define HUMIDITY_DEFAULT_VAL
Definition sys_sensors.c:87
#define STSOP_LONGITUDE
Definition sys_sensors.c:85
#define PRESSURE_DEFAULT_VAL
Definition sys_sensors.c:89
#define TEMPERATURE_DEFAULT_VAL
Definition sys_sensors.c:88
Header for sensors application.