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: X_NUCLEO_COMMON ST_INTERFACES
Dependents: X_NUCLEO_IKS01A3 X_NUCLEO_IKS01A3
LPS22HHSensor.cpp
00001 /** 00002 ****************************************************************************** 00003 * @file LPS22HHSensor.cpp 00004 * @author SRA 00005 * @version V1.0.0 00006 * @date February 2019 00007 * @brief Implementation of a LPS22HH pressure sensor. 00008 ****************************************************************************** 00009 * @attention 00010 * 00011 * <h2><center>© COPYRIGHT(c) 2019 STMicroelectronics</center></h2> 00012 * 00013 * Redistribution and use in source and binary forms, with or without modification, 00014 * are permitted provided that the following conditions are met: 00015 * 1. Redistributions of source code must retain the above copyright notice, 00016 * this list of conditions and the following disclaimer. 00017 * 2. Redistributions in binary form must reproduce the above copyright notice, 00018 * this list of conditions and the following disclaimer in the documentation 00019 * and/or other materials provided with the distribution. 00020 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00021 * may be used to endorse or promote products derived from this software 00022 * without specific prior written permission. 00023 * 00024 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00025 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00026 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00027 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00028 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00029 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00030 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00031 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00032 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00033 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00034 * 00035 ****************************************************************************** 00036 */ 00037 00038 00039 /* Includes ------------------------------------------------------------------*/ 00040 00041 #include "LPS22HHSensor.h" 00042 00043 00044 /* Class Implementation ------------------------------------------------------*/ 00045 00046 /** Constructor 00047 * @param spi object of an helper class which handles the SPI peripheral 00048 * @param cs_pin the chip select pin 00049 * @param int_pin the interrupt pin 00050 * @param spi_type the SPI type 00051 */ 00052 LPS22HHSensor::LPS22HHSensor(SPI *spi, PinName cs_pin, PinName int_pin, SPI_type_t spi_type) : _dev_spi(spi), _cs_pin(cs_pin), _int_irq(int_pin), _spi_type(spi_type) 00053 { 00054 assert(spi); 00055 if (cs_pin == NC) { 00056 printf("ERROR LPS22HH CS MUST NOT BE NC\n\r"); 00057 _dev_spi = NULL; 00058 _dev_i2c = NULL; 00059 return; 00060 } 00061 00062 _reg_ctx.write_reg = LPS22HH_io_write; 00063 _reg_ctx.read_reg = LPS22HH_io_read; 00064 _reg_ctx.handle = (void *)this; 00065 _cs_pin = 1; 00066 _dev_i2c = NULL; 00067 _address = 0; 00068 00069 if (_spi_type == SPI3W) { 00070 /* Enable SPI 3-Wires on the component */ 00071 uint8_t data = 0x01; 00072 lps22hh_write_reg(&_reg_ctx, LPS22HH_CTRL_REG1, &data, 1); 00073 } 00074 } 00075 00076 /** Constructor 00077 * @param i2c object of an helper class which handles the I2C peripheral 00078 * @param address the address of the component's instance 00079 * @param int_pin the interrupt pin 00080 */ 00081 LPS22HHSensor::LPS22HHSensor(DevI2C *i2c, uint8_t address, PinName int_pin) : _dev_i2c(i2c), _address(address), _cs_pin(NC), _int_irq(int_pin) 00082 { 00083 assert(i2c); 00084 _dev_spi = NULL; 00085 _reg_ctx.write_reg = LPS22HH_io_write; 00086 _reg_ctx.read_reg = LPS22HH_io_read; 00087 _reg_ctx.handle = (void *)this; 00088 } 00089 00090 00091 /** 00092 * @brief Initializing the component 00093 * @param init pointer to device specific initalization structure 00094 * @retval 0 in case of success, an error code otherwise 00095 */ 00096 int LPS22HHSensor::init(void *init) 00097 { 00098 /* Disable MIPI I3C(SM) interface */ 00099 if (lps22hh_i3c_interface_set(&_reg_ctx, LPS22HH_I3C_DISABLE) != 0) { 00100 return 1; 00101 } 00102 00103 /* Power down the device, set Low Noise Enable (bit 5), clear One Shot (bit 4) */ 00104 if (lps22hh_data_rate_set(&_reg_ctx, (lps22hh_odr_t)(LPS22HH_POWER_DOWN | 0x10)) != 0) { 00105 return 1; 00106 } 00107 00108 /* Disable low-pass filter on LPS22HH pressure data */ 00109 if (lps22hh_lp_bandwidth_set(&_reg_ctx, LPS22HH_LPF_ODR_DIV_2) != 0) { 00110 return 1; 00111 } 00112 00113 /* Set block data update mode */ 00114 if (lps22hh_block_data_update_set(&_reg_ctx, PROPERTY_ENABLE) != 0) { 00115 return 1; 00116 } 00117 00118 /* Set autoincrement for multi-byte read/write */ 00119 if (lps22hh_auto_increment_set(&_reg_ctx, PROPERTY_ENABLE) != 0) { 00120 return 1; 00121 } 00122 00123 _last_odr = LPS22HH_25_Hz; 00124 _is_enabled = 0; 00125 00126 return 0; 00127 } 00128 00129 /** 00130 * @brief Get WHO_AM_I value 00131 * @param id the WHO_AM_I value 00132 * @retval 0 in case of success, an error code otherwise 00133 */ 00134 int LPS22HHSensor::read_id(uint8_t *id) 00135 { 00136 if (lps22hh_device_id_get(&_reg_ctx, id) != 0) { 00137 return 1; 00138 } 00139 00140 return 0; 00141 } 00142 00143 /** 00144 * @brief Enable the LPS22HH pressure sensor 00145 * @retval 0 in case of success, an error code otherwise 00146 */ 00147 int LPS22HHSensor::enable() 00148 { 00149 /* Check if the component is already _is_enabled */ 00150 if (_is_enabled == 1U) { 00151 return 0; 00152 } 00153 00154 /* Output data rate selection. */ 00155 if (lps22hh_data_rate_set(&_reg_ctx, _last_odr) != 0) { 00156 return 1; 00157 } 00158 00159 _is_enabled = 1; 00160 00161 return 0; 00162 } 00163 00164 /** 00165 * @brief Disable the LPS22HH pressure sensor 00166 * @retval 0 in case of success, an error code otherwise 00167 */ 00168 int LPS22HHSensor::disable() 00169 { 00170 /* Check if the component is already disabled */ 00171 if (_is_enabled == 0U) { 00172 return 0; 00173 } 00174 00175 /* Get current output data rate. */ 00176 if (lps22hh_data_rate_get(&_reg_ctx, &_last_odr) != 0) { 00177 return 1; 00178 } 00179 /* Output data rate selection - power down. */ 00180 if (lps22hh_data_rate_set(&_reg_ctx, LPS22HH_POWER_DOWN) != 0) { 00181 return 1; 00182 } 00183 00184 00185 _is_enabled = 0; 00186 00187 return 0; 00188 } 00189 00190 00191 /** 00192 * @brief Get output data rate 00193 * @param odr the output data rate value 00194 * @retval 0 in case of success, an error code otherwise 00195 */ 00196 int LPS22HHSensor::get_odr(float *odr) 00197 { 00198 int ret = 0; 00199 lps22hh_odr_t odr_low_level; 00200 00201 if (lps22hh_data_rate_get(&_reg_ctx, &odr_low_level) != 0) { 00202 return 1; 00203 } 00204 00205 switch (odr_low_level) { 00206 case LPS22HH_POWER_DOWN: 00207 *odr = 0.0f; 00208 break; 00209 00210 case LPS22HH_1_Hz: 00211 *odr = 1.0f; 00212 break; 00213 00214 case LPS22HH_10_Hz: 00215 *odr = 10.0f; 00216 break; 00217 00218 case LPS22HH_25_Hz: 00219 *odr = 25.0f; 00220 break; 00221 00222 case LPS22HH_50_Hz: 00223 *odr = 50.0f; 00224 break; 00225 00226 case LPS22HH_75_Hz: 00227 *odr = 75.0f; 00228 break; 00229 00230 case LPS22HH_100_Hz: 00231 *odr = 100.0f; 00232 break; 00233 00234 case LPS22HH_200_Hz: 00235 *odr = 200.0f; 00236 break; 00237 00238 default: 00239 ret = 1; 00240 break; 00241 } 00242 00243 return ret; 00244 } 00245 00246 /** 00247 * @brief Set the LPS22HH pressure sensor output data rate 00248 * @param odr the output data rate value to be set 00249 * @retval 0 in case of success, an error code otherwise 00250 */ 00251 int LPS22HHSensor::set_odr(float odr) 00252 { 00253 /* Check if the component is _is_enabled */ 00254 if (_is_enabled == 1U) { 00255 return set_odr_when_enabled(odr); 00256 } else { 00257 return set_odr_when_disabled(odr); 00258 } 00259 } 00260 00261 00262 /** 00263 * @brief Set output data rate 00264 * @param odr the output data rate value to be set 00265 * @retval 0 in case of success, an error code otherwise 00266 */ 00267 int LPS22HHSensor::set_odr_when_enabled(float odr) 00268 { 00269 lps22hh_odr_t new_odr; 00270 00271 new_odr = (odr <= 1.0f) ? LPS22HH_1_Hz 00272 : (odr <= 10.0f) ? LPS22HH_10_Hz 00273 : (odr <= 25.0f) ? LPS22HH_25_Hz 00274 : (odr <= 50.0f) ? LPS22HH_50_Hz 00275 : (odr <= 75.0f) ? LPS22HH_75_Hz 00276 : (odr <= 100.0f) ? LPS22HH_100_Hz 00277 : LPS22HH_200_Hz; 00278 00279 if (lps22hh_data_rate_set(&_reg_ctx, new_odr) != 0) { 00280 return 1; 00281 } 00282 00283 if (lps22hh_data_rate_get(&_reg_ctx, &_last_odr) != 0) { 00284 return 1; 00285 } 00286 00287 return 0; 00288 } 00289 00290 /** 00291 * @brief Set output data rate when disabled 00292 * @param odr the output data rate value to be set 00293 * @retval 0 in case of success, an error code otherwise 00294 */ 00295 int LPS22HHSensor::set_odr_when_disabled(float odr) 00296 { 00297 _last_odr = (odr <= 1.0f) ? LPS22HH_1_Hz 00298 : (odr <= 10.0f) ? LPS22HH_10_Hz 00299 : (odr <= 25.0f) ? LPS22HH_25_Hz 00300 : (odr <= 50.0f) ? LPS22HH_50_Hz 00301 : (odr <= 75.0f) ? LPS22HH_75_Hz 00302 : (odr <= 100.0f) ? LPS22HH_100_Hz 00303 : LPS22HH_200_Hz; 00304 00305 return 0; 00306 } 00307 00308 /** 00309 * @brief Get the LPS22HH pressure value 00310 * @param value pointer where the pressure value is written 00311 * @retval 0 in case of success, an error code otherwise 00312 */ 00313 int LPS22HHSensor::get_pressure(float *value) 00314 { 00315 axis1bit32_t data_raw_pressure; 00316 00317 (void)memset(data_raw_pressure.u8bit, 0x00, sizeof(int32_t)); 00318 if (lps22hh_pressure_raw_get(&_reg_ctx, data_raw_pressure.u8bit) != 0) { 00319 return 1; 00320 } 00321 00322 *value = LPS22HH_FROM_LSB_TO_hPa((float)(data_raw_pressure.i32bit)); 00323 00324 return 0; 00325 } 00326 00327 /** 00328 * @brief Get the LPS22HH pressure data ready bit value 00329 * @param status the status of data ready bit 00330 * @retval 0 in case of success, an error code otherwise 00331 */ 00332 int LPS22HHSensor::get_press_drdy_status(uint8_t *status) 00333 { 00334 if (lps22hh_press_flag_data_ready_get(&_reg_ctx, status) != 0) { 00335 return 1; 00336 } 00337 00338 return 0; 00339 } 00340 00341 /** 00342 * @brief Get the LPS22HH temperature value 00343 * @param value pointer where the temperature value is written 00344 * @retval 0 in case of success, an error code otherwise 00345 */ 00346 int LPS22HHSensor::get_temperature(float *value) 00347 { 00348 axis1bit16_t data_raw_temperature; 00349 00350 (void)memset(data_raw_temperature.u8bit, 0x00, sizeof(int16_t)); 00351 if (lps22hh_temperature_raw_get(&_reg_ctx, data_raw_temperature.u8bit) != 0) { 00352 return 1; 00353 } 00354 00355 *value = LPS22HH_FROM_LSB_TO_degC((float)(data_raw_temperature.i16bit)); 00356 00357 return 0; 00358 } 00359 00360 /** 00361 * @brief Get the LPS22HH temperature data ready bit value 00362 * @param status the status of data ready bit 00363 * @retval 0 in case of success, an error code otherwise 00364 */ 00365 int LPS22HHSensor::get_temp_drdy_status(uint8_t *status) 00366 { 00367 if (lps22hh_temp_flag_data_ready_get(&_reg_ctx, status) != 0) { 00368 return 1; 00369 } 00370 00371 return 0; 00372 } 00373 00374 /** 00375 * @brief Get the LPS22HH register value 00376 * @param reg address to be written 00377 * @param data value to be written 00378 * @retval 0 in case of success, an error code otherwise 00379 */ 00380 int LPS22HHSensor::read_reg(uint8_t reg, uint8_t *data) 00381 { 00382 if (lps22hh_read_reg(&_reg_ctx, reg, data, 1) != 0) { 00383 return 1; 00384 } 00385 00386 return 0; 00387 } 00388 00389 /** 00390 * @brief Set the LPS22HH register value 00391 * @param reg address to be written 00392 * @param data value to be written 00393 * @retval 0 in case of success, an error code otherwise 00394 */ 00395 int LPS22HHSensor::write_reg(uint8_t reg, uint8_t data) 00396 { 00397 if (lps22hh_write_reg(&_reg_ctx, reg, &data, 1) != 0) { 00398 return 1; 00399 } 00400 00401 return 0; 00402 } 00403 00404 /** 00405 * @brief Get the LPS22HH FIFO data 00406 * @param press the pointer where FIFO pressure value is stored 00407 * @param temp the pointer where FIFO temperature value is stored 00408 * @retval 0 in case of success, an error code otherwise 00409 */ 00410 int LPS22HHSensor::get_fifo_data(float *press, float *temp) 00411 { 00412 axis1bit32_t data_raw_pressure; 00413 axis1bit16_t data_raw_temperature; 00414 00415 (void)memset(data_raw_pressure.u8bit, 0x00, sizeof(int32_t)); 00416 if (lps22hh_fifo_pressure_raw_get(&_reg_ctx, data_raw_pressure.u8bit) != 0) { 00417 return 1; 00418 } 00419 00420 *press = LPS22HH_FROM_LSB_TO_hPa((float)(data_raw_pressure.i32bit)); 00421 00422 (void)memset(data_raw_temperature.u8bit, 0x00, sizeof(int16_t)); 00423 if (lps22hh_fifo_temperature_raw_get(&_reg_ctx, data_raw_temperature.u8bit) != 0) { 00424 return 1; 00425 } 00426 00427 *temp = LPS22HH_FROM_LSB_TO_degC((float)(data_raw_temperature.i16bit)); 00428 00429 return 0; 00430 } 00431 00432 /** 00433 * @brief Get the LPS22HH FIFO threshold 00434 * @param status the status of FIFO threshold 00435 * @retval 0 in case of success, an error code otherwise 00436 */ 00437 int LPS22HHSensor::get_fifo_fth_status(uint8_t *status) 00438 { 00439 if (lps22hh_fifo_wtm_flag_get(&_reg_ctx, status) != 0) { 00440 return 1; 00441 } 00442 00443 return 0; 00444 } 00445 00446 /** 00447 * @brief Get the LPS22HH FIFO full status 00448 * @param status the status of FIFO full status 00449 * @retval 0 in case of success, an error code otherwise 00450 */ 00451 int LPS22HHSensor::get_fifo_full_status(uint8_t *status) 00452 { 00453 if (lps22hh_fifo_full_flag_get(&_reg_ctx, status) != 0) { 00454 return 1; 00455 } 00456 00457 return 0; 00458 } 00459 00460 /** 00461 * @brief Get the LPS22HH FIFO OVR status 00462 * @param status the status of FIFO OVR status 00463 * @retval 0 in case of success, an error code otherwise 00464 */ 00465 int LPS22HHSensor::get_fifo_ovr_status(uint8_t *status) 00466 { 00467 if (lps22hh_fifo_ovr_flag_get(&_reg_ctx, status) != 0) { 00468 return 1; 00469 } 00470 00471 return 0; 00472 } 00473 00474 /** 00475 * @brief Get the LPS22HH FIFO data level 00476 * @param status the status of FIFO data level 00477 * @retval 0 in case of success, an error code otherwise 00478 */ 00479 int LPS22HHSensor::get_fifo_level(uint8_t *status) 00480 { 00481 if (lps22hh_fifo_data_level_get(&_reg_ctx, status) != 0) { 00482 return 1; 00483 } 00484 00485 return 0; 00486 } 00487 00488 /** 00489 * @brief Reset the FIFO interrupt 00490 * @param interrupt The FIFO interrupt to be reset; values: 0 = FTH; 1 = FULL; 2 = OVR 00491 * @retval 0 in case of success, an error code otherwise 00492 */ 00493 int LPS22HHSensor::reset_fifo_interrupt(uint8_t interrupt) 00494 { 00495 switch (interrupt) { 00496 case 0: 00497 if (lps22hh_fifo_threshold_on_int_set(&_reg_ctx, PROPERTY_DISABLE) != 0) { 00498 return 1; 00499 } 00500 break; 00501 case 1: 00502 if (lps22hh_fifo_full_on_int_set(&_reg_ctx, PROPERTY_DISABLE) != 0) { 00503 return 1; 00504 } 00505 break; 00506 case 2: 00507 if (lps22hh_fifo_ovr_on_int_set(&_reg_ctx, PROPERTY_DISABLE) != 0) { 00508 return 1; 00509 } 00510 break; 00511 default: 00512 return 1; 00513 } 00514 00515 return 0; 00516 } 00517 00518 /** 00519 * @brief Set the FIFO interrupt 00520 * @param interrupt The FIFO interrupt to be set; values: 0 = FTH; 1 = FULL; 2 = OVR 00521 * @retval 0 in case of success, an error code otherwise 00522 */ 00523 int LPS22HHSensor::set_fifo_interrupt(uint8_t interrupt) 00524 { 00525 switch (interrupt) { 00526 case 0: 00527 if (lps22hh_fifo_threshold_on_int_set(&_reg_ctx, PROPERTY_ENABLE) != 0) { 00528 return 1; 00529 } 00530 break; 00531 case 1: 00532 if (lps22hh_fifo_full_on_int_set(&_reg_ctx, PROPERTY_ENABLE) != 0) { 00533 return 1; 00534 } 00535 break; 00536 case 2: 00537 if (lps22hh_fifo_ovr_on_int_set(&_reg_ctx, PROPERTY_ENABLE) != 0) { 00538 return 1; 00539 } 00540 break; 00541 default: 00542 return 1; 00543 } 00544 00545 return 0; 00546 } 00547 00548 /** 00549 * @brief Set the FIFO mode 00550 * @param Mode the FIFO mode to be set 00551 * @retval 0 in case of success, an error code otherwise 00552 */ 00553 int LPS22HHSensor::set_fifo_mode(uint8_t mode) 00554 { 00555 /* Verify that the passed parameter contains one of the valid values */ 00556 switch ((lps22hh_f_mode_t)mode) { 00557 case LPS22HH_BYPASS_MODE: 00558 case LPS22HH_FIFO_MODE: 00559 case LPS22HH_STREAM_MODE: 00560 case LPS22HH_STREAM_TO_FIFO_MODE: 00561 case LPS22HH_BYPASS_TO_STREAM_MODE: 00562 case LPS22HH_BYPASS_TO_FIFO_MODE: 00563 break; 00564 default: 00565 return 1; 00566 } 00567 00568 if (lps22hh_fifo_mode_set(&_reg_ctx, (lps22hh_f_mode_t)mode) != 0) { 00569 return 1; 00570 } 00571 00572 return 0; 00573 } 00574 00575 /** 00576 * @brief Set the LPS22HH FIFO watermark level 00577 * @param watermark the FIFO watermark level to be set 00578 * @retval 0 in case of success, an error code otherwise 00579 */ 00580 int LPS22HHSensor::set_fifo_watermark_level(uint8_t watermark) 00581 { 00582 if (lps22hh_fifo_watermark_set(&_reg_ctx, watermark) != 0) { 00583 return 1; 00584 } 00585 00586 return 0; 00587 } 00588 00589 /** 00590 * @brief Set the LPS22HH stop on watermark function 00591 * @param stop the state of stop on watermark function 00592 * @retval 0 in case of success, an error code otherwise 00593 */ 00594 int LPS22HHSensor::stop_fifo_on_watermark(uint8_t stop) 00595 { 00596 if (lps22hh_fifo_stop_on_wtm_set(&_reg_ctx, stop) != 0) { 00597 return 1; 00598 } 00599 00600 return 0; 00601 } 00602 00603 /** 00604 * @brief Set the LPS22HH One Shot Mode 00605 * @retval 0 in case of success, an error code otherwise 00606 */ 00607 int LPS22HHSensor::set_one_shot() 00608 { 00609 /* Start One Shot Measurement */ 00610 if (lps22hh_data_rate_set(&_reg_ctx, LPS22HH_ONE_SHOOT) != 0) { 00611 return 1; 00612 } 00613 00614 return 0; 00615 } 00616 00617 /** 00618 * @brief Get the LPS22HH One Shot Status 00619 * @param status pointer to the one shot status (1 means measurements available, 0 means measurements not available yet) 00620 * @retval 0 in case of success, an error code otherwise 00621 */ 00622 int LPS22HHSensor::get_one_shot_status(uint8_t *status) 00623 { 00624 uint8_t p_da; 00625 uint8_t t_da; 00626 00627 /* Get DataReady for pressure */ 00628 if (lps22hh_press_flag_data_ready_get(&_reg_ctx, &p_da) != 0) { 00629 return 1; 00630 } 00631 00632 /* Get DataReady for temperature */ 00633 if (lps22hh_temp_flag_data_ready_get(&_reg_ctx, &t_da) != 0) { 00634 return 1; 00635 } 00636 00637 if (p_da && t_da) { 00638 *status = 1; 00639 } else { 00640 *status = 0; 00641 } 00642 00643 return 0; 00644 } 00645 00646 int32_t LPS22HH_io_write(void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite) 00647 { 00648 return ((LPS22HHSensor *)handle)->io_write(pBuffer, WriteAddr, nBytesToWrite); 00649 } 00650 00651 int32_t LPS22HH_io_read(void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead) 00652 { 00653 return ((LPS22HHSensor *)handle)->io_read(pBuffer, ReadAddr, nBytesToRead); 00654 }
Generated on Wed Jul 13 2022 04:53:54 by
