From d4c4f24cffc301e2f578c81b65a60ccaabe997d7 Mon Sep 17 00:00:00 2001 From: Rocco Marco Guglielmi Date: Sat, 10 Mar 2018 13:54:21 +0000 Subject: Updated LPS25H and added a thermometer interface to the driver git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@11686 110e8d01-0319-4d1e-a829-52ad28d1bb01 --- os/ex/ST/lps25h.c | 513 ++++++++++++++++++++++++----- os/ex/ST/lps25h.h | 370 ++++++++++++++++++--- testex/STM32/STM32F4xx/I2C-LPS25H/Makefile | 32 +- testex/STM32/STM32F4xx/I2C-LPS25H/main.c | 118 ++----- 4 files changed, 797 insertions(+), 236 deletions(-) diff --git a/os/ex/ST/lps25h.c b/os/ex/ST/lps25h.c index ad5b197c9..2957ca87b 100644 --- a/os/ex/ST/lps25h.c +++ b/os/ex/ST/lps25h.c @@ -1,5 +1,5 @@ /* - ChibiOS - Copyright (C) 2016 Rocco Marco Guglielmi + ChibiOS - Copyright (C) 2016-2018 Rocco Marco Guglielmi This file is part of ChibiOS. @@ -57,10 +57,11 @@ * @param[out] rxbuf pointer to an output buffer * @param[in] n number of consecutive register to read * @return the operation status. + * * @notapi */ -msg_t lps25hI2CReadRegister(I2CDriver *i2cp, lps25h_sad_t sad, uint8_t reg, - uint8_t* rxbuf, size_t n) { +static msg_t lps25hI2CReadRegister(I2CDriver *i2cp, lps25h_sad_t sad, + uint8_t reg, uint8_t* rxbuf, size_t n) { uint8_t txbuf = reg; if(n > 1) txbuf |= LPS25H_SUB_MS; @@ -80,11 +81,11 @@ msg_t lps25hI2CReadRegister(I2CDriver *i2cp, lps25h_sad_t sad, uint8_t reg, * @param[in] n size of txbuf less one (not considering the first * element) * @return the operation status. + * * @notapi - * @return the operation status. */ -msg_t lps25hI2CWriteRegister(I2CDriver *i2cp, lps25h_sad_t sad, uint8_t* txbuf, - size_t n) { +static msg_t lps25hI2CWriteRegister(I2CDriver *i2cp, lps25h_sad_t sad, + uint8_t* txbuf, size_t n) { if (n > 1) (*txbuf) |= LPS25H_SUB_MS; return i2cMasterTransmitTimeout(i2cp, sad, txbuf, n + 1, NULL, 0, @@ -92,118 +93,448 @@ msg_t lps25hI2CWriteRegister(I2CDriver *i2cp, lps25h_sad_t sad, uint8_t* txbuf, } #endif /* LPS25H_USE_I2C */ -/* - * Interface implementation. +/** + * @brief Return the number of axes of the BaseBarometer. + * + * @param[in] ip pointer to @p BaseBarometer interface. + * + * @return the number of axes. */ -static size_t get_axes_number(void *ip) { +static size_t baro_get_axes_number(void *ip) { + (void)ip; - osalDbgCheck(ip != NULL); - return LPS25H_NUMBER_OF_AXES; + return LPS25H_BARO_NUMBER_OF_AXES; } -static msg_t read_raw(void *ip, int32_t* axis) { +/** + * @brief Retrieves raw data from the BaseBarometer. + * @note This data is retrieved from MEMS register without any algebraical + * manipulation. + * @note The axes array must be at least the same size of the + * BaseBarometer axes number. + * + * @param[in] ip pointer to @p BaseBarometer interface. + * @param[out] axes a buffer which would be filled with raw data. + * + * @return The operation status. + * @retval MSG_OK if the function succeeded. + * @retval MSG_RESET if one or more I2C errors occurred, the errors can + * be retrieved using @p i2cGetErrors(). + * @retval MSG_TIMEOUT if a timeout occurred before operation end. + */ +static msg_t baro_read_raw(void *ip, int32_t axes[]) { + LPS25HDriver* devp; uint8_t buff[3]; - msg_t msg = MSG_OK; + msg_t msg; + + osalDbgCheck((ip != NULL) && (axes != NULL)); + + /* Getting parent instance pointer.*/ + devp = objGetInstance(LPS25HDriver*, (BaseBarometer*)ip); - *axis = 0; - - osalDbgCheck((ip != NULL) && (axis != NULL)); - osalDbgAssert((((LPS25HDriver *)ip)->state == LPS25H_READY), - "read_raw(), invalid state"); + osalDbgAssert((devp->state == LPS25H_READY), + "baro_read_raw(), invalid state"); -#if LPS25H_USE_I2C - osalDbgAssert((((LPS25HDriver *)ip)->config->i2cp->state == I2C_READY), - "read_raw(), channel not ready"); + osalDbgAssert((devp->config->i2cp->state == I2C_READY), + "baro_read_raw(), channel not ready"); + #if LPS25H_SHARED_I2C - i2cAcquireBus(((LPS25HDriver *)ip)->config->i2cp); - i2cStart(((LPS25HDriver *)ip)->config->i2cp, - ((LPS25HDriver *)ip)->config->i2ccfg); + i2cAcquireBus(devp->config->i2cp); + i2cStart(devp->config->i2cp, + devp->config->i2ccfg); #endif /* LPS25H_SHARED_I2C */ - msg = lps25hI2CReadRegister(((LPS25HDriver *)ip)->config->i2cp, - ((LPS25HDriver *)ip)->config->slaveaddress, - LPS25H_AD_PRESS_OUT_XL, buff, 3); + msg = lps25hI2CReadRegister(devp->config->i2cp, devp->config->slaveaddress, + LPS25H_AD_PRESS_OUT_XL, buff, 3); #if LPS25H_SHARED_I2C - i2cReleaseBus(((LPS25HDriver *)ip)->config->i2cp); + i2cReleaseBus(devp->config->i2cp); #endif /* LPS25H_SHARED_I2C */ -#endif /* LPS25H_USE_I2C */ if(msg == MSG_OK) { - *axis = buff[0] + (buff[1] << 8) + (buff[2] << 16); + *axes = buff[0] + (buff[1] << 8) + (buff[2] << 16); } return msg; } -static msg_t read_cooked(void *ip, float* axis) { +/** + * @brief Retrieves cooked data from the BaseBarometer. + * @note This data is manipulated according to the formula + * cooked = (raw * sensitivity) - bias. + * @note Final data is expressed as hPa. + * @note The axes array must be at least the same size of the + * BaseBarometer axes number. + * + * @param[in] ip pointer to @p BaseBarometer interface. + * @param[out] axes a buffer which would be filled with cooked data. + * + * @return The operation status. + * @retval MSG_OK if the function succeeded. + * @retval MSG_RESET if one or more I2C errors occurred, the errors can + * be retrieved using @p i2cGetErrors(). + * @retval MSG_TIMEOUT if a timeout occurred before operation end. + */ +static msg_t baro_read_cooked(void *ip, float axes[]) { + LPS25HDriver* devp; int32_t raw; msg_t msg; - osalDbgCheck((ip != NULL) && (axis != NULL)); + osalDbgCheck((ip != NULL) && (axes != NULL)); - osalDbgAssert((((LPS25HDriver *)ip)->state == LPS25H_READY), - "read_cooked(), invalid state"); + /* Getting parent instance pointer.*/ + devp = objGetInstance(LPS25HDriver*, (BaseBarometer*)ip); + + osalDbgAssert((devp->state == LPS25H_READY), + "baro_read_cooked(), invalid state"); - msg = read_raw(ip, &raw); + msg = baro_read_raw(ip, &raw); - *axis = raw * ((LPS25HDriver *)ip)->sensitivity; - *axis -= ((LPS25HDriver *)ip)->bias; + *axes = (raw * devp->barosensitivity) - devp->barobias; + return msg; } -static msg_t set_bias(void *ip, float *bp) { +/** + * @brief Set bias values for the BaseBarometer. + * @note Bias must be expressed as hPa. + * @note The bias buffer must be at least the same size of the + * BaseBarometer axes number. + * + * @param[in] ip pointer to @p BaseBarometer interface. + * @param[in] bp a buffer which contains biases. + * + * @return The operation status. + * @retval MSG_OK if the function succeeded. + * @retval MSG_RESET if one or more I2C errors occurred, the errors can + * be retrieved using @p i2cGetErrors(). + * @retval MSG_TIMEOUT if a timeout occurred before operation end. + */ +static msg_t baro_set_bias(void *ip, float *bp) { + LPS25HDriver* devp; + msg_t msg = MSG_OK; + osalDbgCheck((ip != NULL) && (bp != NULL)); - osalDbgAssert((((LPS25HDriver *)ip)->state == LPS25H_READY) || - (((LPS25HDriver *)ip)->state == LPS25H_STOP), - "set_bias(), invalid state"); + /* Getting parent instance pointer.*/ + devp = objGetInstance(LPS25HDriver*, (BaseBarometer*)ip); - ((LPS25HDriver *)ip)->bias = *bp; - return MSG_OK; + osalDbgAssert((devp->state == LPS25H_READY), + "baro_set_bias(), invalid state"); + + devp->barobias = *bp; + return msg; } -static msg_t reset_bias(void *ip) { +/** + * @brief Reset bias values for the BaseBarometer. + * @note Default biases value are obtained from device datasheet when + * available otherwise they are considered zero. + * + * @param[in] ip pointer to @p BaseBarometer interface. + * + * @return The operation status. + * @retval MSG_OK if the function succeeded. + */ +static msg_t baro_reset_bias(void *ip) { + LPS25HDriver* devp; + msg_t msg = MSG_OK; + osalDbgCheck(ip != NULL); - osalDbgAssert((((LPS25HDriver *)ip)->state == LPS25H_READY) || - (((LPS25HDriver *)ip)->state == LPS25H_STOP), - "reset_bias(), invalid state"); + /* Getting parent instance pointer.*/ + devp = objGetInstance(LPS25HDriver*, (BaseBarometer*)ip); + + osalDbgAssert((devp->state == LPS25H_READY), + "baro_reset_bias(), invalid state"); + + devp->barobias = LPS25H_BARO_SENS; + return msg; +} + +/** + * @brief Set sensitivity values for the BaseBarometer. + * @note Sensitivity must be expressed as hPa/LSB. + * @note The sensitivity buffer must be at least the same size of the + * BaseBarometer axes number. + * + * @param[in] ip pointer to @p BaseBarometer interface. + * @param[in] sp a buffer which contains sensitivities. + * + * @return The operation status. + * @retval MSG_OK if the function succeeded. + */ +static msg_t baro_set_sensitivity(void *ip, float *sp) { + LPS25HDriver* devp; + msg_t msg = MSG_OK; + + osalDbgCheck((ip != NULL) && (sp != NULL)); + + /* Getting parent instance pointer.*/ + devp = objGetInstance(LPS25HDriver*, (BaseBarometer*)ip); + + osalDbgAssert((devp->state == LPS25H_READY), + "baro_set_sensitivity(), invalid state"); + + devp->barosensitivity = *sp; + return msg; +} + +/** + * @brief Reset sensitivity values for the BaseBarometer. + * @note Default sensitivities value are obtained from device datasheet. + * + * @param[in] ip pointer to @p BaseBarometer interface. + * + * @return The operation status. + * @retval MSG_OK if the function succeeded. + */ +static msg_t baro_reset_sensitivity(void *ip) { + LPS25HDriver* devp; + msg_t msg = MSG_OK; + + osalDbgCheck(ip != NULL); + + /* Getting parent instance pointer.*/ + devp = objGetInstance(LPS25HDriver*, (BaseBarometer*)ip); + + osalDbgAssert((devp->state == LPS25H_READY), + "baro_reset_sensitivity(), invalid state"); + + devp->barosensitivity = LPS25H_BARO_SENS; + return msg; +} + +/** + * @brief Return the number of axes of the BaseThermometer. + * + * @param[in] ip pointer to @p BaseThermometer interface. + * + * @return the number of axes. + */ +static size_t thermo_get_axes_number(void *ip) { + (void)ip; + + return LPS25H_THERMO_NUMBER_OF_AXES; +} + +/** + * @brief Retrieves raw data from the BaseThermometer. + * @note This data is retrieved from MEMS register without any algebraical + * manipulation. + * @note The axes array must be at least the same size of the + * BaseThermometer axes number. + * + * @param[in] ip pointer to @p BaseThermometer interface. + * @param[out] axes a buffer which would be filled with raw data. + * + * @return The operation status. + * @retval MSG_OK if the function succeeded. + * @retval MSG_RESET if one or more I2C errors occurred, the errors can + * be retrieved using @p i2cGetErrors(). + * @retval MSG_TIMEOUT if a timeout occurred before operation end. + */ +static msg_t thermo_read_raw(void *ip, int32_t axes[]) { + LPS25HDriver* devp; + int16_t tmp; + uint8_t buff[2]; + msg_t msg; + + osalDbgCheck((ip != NULL) && (axes != NULL)); + + /* Getting parent instance pointer.*/ + devp = objGetInstance(LPS25HDriver*, (BaseThermometer*)ip); + + osalDbgAssert((devp->state == LPS25H_READY), + "thermo_read_raw(), invalid state"); + + osalDbgAssert((devp->config->i2cp->state == I2C_READY), + "thermo_read_raw(), channel not ready"); + +#if LPS25H_SHARED_I2C + i2cAcquireBus(devp->config->i2cp); + i2cStart(devp->config->i2cp, + devp->config->i2ccfg); +#endif /* LPS25H_SHARED_I2C */ - ((LPS25HDriver *)ip)->bias = 0.0f; - return MSG_OK; + msg = lps25hI2CReadRegister(devp->config->i2cp, devp->config->slaveaddress, + LPS25H_AD_TEMP_OUT_L, buff, 2); + +#if LPS25H_SHARED_I2C + i2cReleaseBus(devp->config->i2cp); +#endif /* LPS25H_SHARED_I2C */ + + if (msg == MSG_OK) { + tmp = buff[0] + (buff[1] << 8); + *axes = (int32_t)tmp; + } + return msg; } +/** + * @brief Retrieves cooked data from the BaseThermometer. + * @note This data is manipulated according to the formula + * cooked = (raw * sensitivity) - bias. + * @note Final data is expressed as °C. + * @note The axes array must be at least the same size of the + * BaseThermometer axes number. + * + * @param[in] ip pointer to @p BaseThermometer interface. + * @param[out] axes a buffer which would be filled with cooked data. + * + * @return The operation status. + * @retval MSG_OK if the function succeeded. + * @retval MSG_RESET if one or more I2C errors occurred, the errors can + * be retrieved using @p i2cGetErrors(). + * @retval MSG_TIMEOUT if a timeout occurred before operation end. + */ +static msg_t thermo_read_cooked(void *ip, float* axis) { + LPS25HDriver* devp; + int32_t raw; + msg_t msg; -static msg_t set_sensivity(void *ip, float *sp) { + osalDbgCheck((ip != NULL) && (axis != NULL)); + + /* Getting parent instance pointer.*/ + devp = objGetInstance(LPS25HDriver*, (BaseThermometer*)ip); - osalDbgCheck((ip != NULL) && (sp !=NULL)); + osalDbgAssert((devp->state == LPS25H_READY), + "thermo_read_cooked(), invalid state"); + + msg = thermo_read_raw(devp, &raw); - osalDbgAssert((((LPS25HDriver *)ip)->state == LPS25H_READY), - "set_sensivity(), invalid state"); + *axis = (raw * devp->thermosensitivity) - devp->thermobias; + + return msg; +} + +/** + * @brief Set bias values for the BaseThermometer. + * @note Bias must be expressed as °C. + * @note The bias buffer must be at least the same size of the + * BaseThermometer axes number. + * + * @param[in] ip pointer to @p BaseThermometer interface. + * @param[in] bp a buffer which contains biases. + * + * @return The operation status. + * @retval MSG_OK if the function succeeded. + */ +static msg_t thermo_set_bias(void *ip, float *bp) { + LPS25HDriver* devp; + msg_t msg = MSG_OK; + + osalDbgCheck((ip != NULL) && (bp != NULL)); + + /* Getting parent instance pointer.*/ + devp = objGetInstance(LPS25HDriver*, (BaseThermometer*)ip); - ((LPS25HDriver *)ip)->sensitivity = *sp; - return MSG_OK; + osalDbgAssert((devp->state == LPS25H_READY), + "thermo_set_bias(), invalid state"); + + devp->thermobias = *bp; + + return msg; } -static msg_t reset_sensivity(void *ip) { +/** + * @brief Reset bias values for the BaseThermometer. + * @note Default biases value are obtained from device datasheet when + * available otherwise they are considered zero. + * + * @param[in] ip pointer to @p BaseThermometer interface. + * + * @return The operation status. + * @retval MSG_OK if the function succeeded. + */ +static msg_t thermo_reset_bias(void *ip) { + LPS25HDriver* devp; + msg_t msg = MSG_OK; + + osalDbgCheck(ip != NULL); + + /* Getting parent instance pointer.*/ + devp = objGetInstance(LPS25HDriver*, (BaseThermometer*)ip); + + osalDbgAssert((devp->state == LPS25H_READY), + "thermo_reset_bias(), invalid state"); + devp->thermobias = LPS25H_THERMO_BIAS; + + return msg; +} + +/** + * @brief Set sensitivity values for the BaseThermometer. + * @note Sensitivity must be expressed as °C/LSB. + * @note The sensitivity buffer must be at least the same size of the + * BaseThermometer axes number. + * + * @param[in] ip pointer to @p BaseThermometer interface. + * @param[in] sp a buffer which contains sensitivities. + * + * @return The operation status. + * @retval MSG_OK if the function succeeded. + */ +static msg_t thermo_set_sensitivity(void *ip, float *sp) { + LPS25HDriver* devp; + msg_t msg = MSG_OK; + + osalDbgCheck((ip != NULL) && (sp != NULL)); + + /* Getting parent instance pointer.*/ + devp = objGetInstance(LPS25HDriver*, (BaseThermometer*)ip); + + osalDbgAssert((devp->state == LPS25H_READY), + "thermo_set_sensitivity(), invalid state"); + + devp->thermosensitivity = *sp; + + return msg; +} + +/** + * @brief Reset sensitivity values for the BaseThermometer. + * @note Default sensitivities value are obtained from device datasheet. + * + * @param[in] ip pointer to @p BaseThermometer interface. + * + * @return The operation status. + * @retval MSG_OK if the function succeeded. + */ +static msg_t thermo_reset_sensitivity(void *ip) { + LPS25HDriver* devp; + msg_t msg = MSG_OK; + osalDbgCheck(ip != NULL); - osalDbgAssert((((LPS25HDriver *)ip)->state == LPS25H_READY), - "reset_sensivity(), invalid state"); + /* Getting parent instance pointer.*/ + devp = objGetInstance(LPS25HDriver*, (BaseThermometer*)ip); + + osalDbgAssert((devp->state == LPS25H_READY), + "thermo_reset_sensitivity(), invalid state"); - ((LPS25HDriver *)ip)->sensitivity = LPS25H_SENS; - return MSG_OK; + devp->thermosensitivity = LPS25H_THERMO_SENS; + + return msg; } -static const struct BaseSensorVMT vmt_sensor = { - get_axes_number, read_raw, read_cooked +static const struct LPS25HVMT vmt_device = { + (size_t)0 +}; + +static const struct BaseBarometerVMT vmt_barometer = { + sizeof(struct LPS25HVMT*), + baro_get_axes_number, baro_read_raw, baro_read_cooked, + baro_set_bias, baro_reset_bias, baro_set_sensitivity, + baro_reset_sensitivity }; -static const struct LPS25HBarometerVMT vmt_barometer = { - get_axes_number, read_raw, read_cooked, - set_bias, reset_bias, set_sensivity, reset_sensivity +static const struct BaseThermometerVMT vmt_thermometer = { + sizeof(struct LPS25HVMT*) + sizeof(BaseBarometer), + thermo_get_axes_number, thermo_read_raw, thermo_read_cooked, + thermo_set_bias, thermo_reset_bias, thermo_set_sensitivity, + thermo_reset_sensitivity }; /*===========================================================================*/ @@ -219,10 +550,15 @@ static const struct LPS25HBarometerVMT vmt_barometer = { */ void lps25hObjectInit(LPS25HDriver *devp) { - devp->vmt_sensor = &vmt_sensor; - devp->vmt_barometer = &vmt_barometer; + devp->vmt = &vmt_device; + devp->baro_if.vmt = &vmt_barometer; + devp->thermo_if.vmt = &vmt_thermometer; + devp->config = NULL; - devp->bias = 0; + + devp->baroaxes = LPS25H_BARO_NUMBER_OF_AXES; + devp->thermoaxes = LPS25H_THERMO_NUMBER_OF_AXES; + devp->state = LPS25H_STOP; } @@ -239,10 +575,10 @@ void lps25hStart(LPS25HDriver *devp, const LPS25HConfig *config) { osalDbgCheck((devp != NULL) && (config != NULL)); osalDbgAssert((devp->state == LPS25H_STOP) || (devp->state == LPS25H_READY), - "lps25hStart(), invalid state"); + "lps25hStart(), invalid state"); devp->config = config; -#if LPS25H_USE_I2C + /* Control register 1 configuration block.*/ { cr[0] = LPS25H_AD_CTRL_REG1; @@ -269,7 +605,7 @@ void lps25hStart(LPS25HDriver *devp, const LPS25HConfig *config) { cr[0] = LPS25H_AD_RES_CONF; cr[1] = 0x05; #if LPS25H_USE_ADVANCED || defined(__DOXYGEN__) - cr[1] = devp->config->respressure | devp->config->restemperature; + cr[1] = devp->config->baroresolution | devp->config->thermoresolution; #endif } @@ -285,22 +621,37 @@ void lps25hStart(LPS25HDriver *devp, const LPS25HConfig *config) { #if LPS25H_SHARED_I2C i2cReleaseBus((devp)->config->i2cp); #endif /* LPS25H_SHARED_I2C */ -#endif /* LPS25H_USE_I2C */ - if(devp->config->sensitivity == NULL) { - devp->sensitivity = LPS25H_SENS; + if(devp->config->barosensitivity == NULL) { + devp->barosensitivity = LPS25H_BARO_SENS; + } + else{ + /* Taking barometer sensitivity from user configurations */ + devp->barosensitivity = *(devp->config->barosensitivity); + } + + if(devp->config->barobias == NULL) { + devp->barobias = LPS25H_BARO_BIAS; } else{ - /* Taking Sensitivity from user configurations */ - devp->sensitivity = *devp->config->sensitivity; + /* Taking barometer bias from user configurations */ + devp->barobias = *(devp->config->barobias); } - if(devp->config->bias == NULL) { - devp->bias = 0.0f; + if(devp->config->thermosensitivity == NULL) { + devp->thermosensitivity = LPS25H_THERMO_SENS; } else{ - /* Taking Bias from user configurations */ - devp->bias = *devp->config->bias; + /* Taking thermometer sensitivity from user configurations */ + devp->thermosensitivity = *(devp->config->thermosensitivity); + } + + if(devp->config->thermobias == NULL) { + devp->thermobias = LPS25H_THERMO_BIAS; + } + else{ + /* Taking thermometer bias from user configurations */ + devp->thermobias = *(devp->config->thermobias); } /* This is the Barometer transient recovery time */ @@ -325,7 +676,6 @@ void lps25hStop(LPS25HDriver *devp) { "lps25hStop(), invalid state"); if (devp->state == LPS25H_READY) { -#if (LPS25H_USE_I2C) #if LPS25H_SHARED_I2C i2cAcquireBus((devp)->config->i2cp); i2cStart((devp)->config->i2cp, @@ -341,7 +691,6 @@ void lps25hStop(LPS25HDriver *devp) { #if LPS25H_SHARED_I2C i2cReleaseBus((devp)->config->i2cp); #endif /* LPS25H_SHARED_I2C */ -#endif /* LPS25H_USE_I2C */ } devp->state = LPS25H_STOP; } diff --git a/os/ex/ST/lps25h.h b/os/ex/ST/lps25h.h index 519d8e846..54d96635c 100644 --- a/os/ex/ST/lps25h.h +++ b/os/ex/ST/lps25h.h @@ -1,5 +1,5 @@ /* - ChibiOS - Copyright (C) 2016 Rocco Marco Guglielmi + ChibiOS - Copyright (C) 2016-2018 Rocco Marco Guglielmi This file is part of ChibiOS. @@ -30,6 +30,7 @@ #define _LPS25H_H_ #include "hal_barometer.h" +#include "hal_thermometer.h" /*===========================================================================*/ /* Driver constants. */ @@ -42,7 +43,7 @@ /** * @brief LPS25H driver version string. */ -#define EX_LPS25H_VERSION "1.0.4" +#define EX_LPS25H_VERSION "1.0.5" /** * @brief LPS25H driver version major number. @@ -57,18 +58,35 @@ /** * @brief LPS25H driver version patch number. */ -#define EX_LPS25H_PATCH 4 +#define EX_LPS25H_PATCH 5 /** @} */ /** - * @brief LPS25H characteristics. + * @brief LPS25H baromether subsystem characteristics. + * @note Sensitivity is expressed as hPa/LSB whereas hPa stand for + * hectopascal. + * @note Bias is expressed as hPa. * * @{ */ -#define LPS25H_NUMBER_OF_AXES 1U +#define LPS25H_BARO_NUMBER_OF_AXES 1U -#define LPS25H_SENS 0.00024414f +#define LPS25H_BARO_SENS 0.00024414f +#define LPS25H_BARO_BIAS 0.0f /** @} */ + +/** + * @brief LPS25H thermometer subsystem characteristics. + * @note Sensitivity is expressed as °C/LSB. + * @note Bias is expressed as °C. + * + * @{ + */ +#define LPS25H_THERMO_NUMBER_OF_AXES 1U + +#define LPS25H_THERMO_SENS 0.00208333f +#define LPS25H_THERMO_BIAS -42.5f +/** @} */ /** * @name LPS25H communication interfaces related bit masks @@ -254,10 +272,6 @@ #error "LPS25H_SHARED_I2C requires I2C_USE_MUTUAL_EXCLUSION" #endif -/* - * TODO: Add SPI support. - */ - /*===========================================================================*/ /* Driver data structures and types. */ /*===========================================================================*/ @@ -266,6 +280,11 @@ * @name LPS25H data structures and types. * @{ */ +/** + * @brief Structure representing a LPS25H driver. + */ +typedef struct LPS25HDriver LPS25HDriver; + /** * @brief LPS25H slave address */ @@ -345,20 +364,28 @@ typedef struct { /** * @brief I2C configuration associated to this LPS25H. */ - const I2CConfig *i2ccfg; + const I2CConfig *i2ccfg; + /** + * @brief LPS25H slave address + */ + lps25h_sad_t slaveaddress; #endif /* LPS25H_USE_I2C */ /** - * @brief LPS25H initial sensitivity. + * @brief LPS25H barometer subsystem initial sensitivity. */ - float* sensitivity; + float *barosensitivity; /** - * @brief LPS25H initial bias. + * @brief LPS25H barometer subsystem initial bias. */ - float* bias; + float *barobias; /** - * @brief LPS25H slave address + * @brief LPS25H thermometer subsystem initial sensitivity. */ - lps25h_sad_t slaveaddress; + float *thermosensitivity; + /** + * @brief LPS25H thermometer subsystem initial bias. + */ + float *thermobias; /** * @brief LPS25H output data rate selection. */ @@ -369,36 +396,36 @@ typedef struct { */ lps25h_bdu_t blockdataupdate; /** - * @brief LPS25H pressure resolution. + * @brief LPS25H barometer subsystem resolution. */ - lps25h_avgp_t respressure; + lps25h_avgp_t baroresolution; /** - * @brief LPS25H temperature resolution. + * @brief LPS25H thermometer subsystem resolution. */ - lps25h_avgt_t restemperature; + lps25h_avgt_t thermoresolution; #endif } LPS25HConfig; /** - * @brief @p LPS25H barometer subsystem specific methods. + * @brief @p LPS25H specific methods. * @note No methods so far, just a common ancestor interface. */ -#define _lps25h_barometer_methods_alone +#define _lps25h_methods_alone /** - * @brief @p LPS25H barometer subsystem specific methods with inherited ones. + * @brief @p LPS25H specific methods with inherited ones. */ -#define _lps25h_barometer_methods \ - _base_barometer_methods \ - _lps25h_barometer_methods_alone +#define _lps25h_methods \ + _base_object_methods \ + _lps25h_methods_alone /** - * @extends BaseBarometerVMT + * @extends BaseObjectVMT * - * @brief @p LPS25H barometer virtual methods table. + * @brief @p LPS25H virtual methods table. */ -struct LPS25HBarometerVMT { - _lps25h_barometer_methods +struct LPS25HVMT { + _lps25h_methods }; /** @@ -409,38 +436,273 @@ struct LPS25HBarometerVMT { lps25h_state_t state; \ /* Current configuration data.*/ \ const LPS25HConfig *config; \ - /* Current sensitivity data.*/ \ - float sensitivity; \ - /* Current Bias data.*/ \ - float bias; + /* Barometer subsystem axes number.*/ \ + size_t baroaxes; \ + /* Barometer subsystem current sensitivity.*/ \ + float barosensitivity; \ + /* Barometer subsystem current bias .*/ \ + float barobias; \ + /* Thermometer subsystem axes number.*/ \ + size_t thermoaxes; \ + /* Thermometer subsystem current sensitivity.*/ \ + float thermosensitivity; \ + /* Thermometer subsystem current bias.*/ \ + float thermobias; -/** - * @extends BaseGyroscope - * - * @brief LPS25H 3-axis barometer class. - * @details This class extends @p BaseGyroscope by adding physical - * driver implementation. +/** + * @brief LPS25H 2-axis barometer/thermometer class. */ struct LPS25HDriver { - /** @brief BaseSensor Virtual Methods Table. */ - const struct BaseSensorVMT *vmt_sensor; - _base_sensor_data - /** @brief LPS25H Barometer Virtual Methods Table. */ - const struct LPS25HBarometerVMT *vmt_barometer; - _base_barometer_data + /** @brief Virtual Methods Table.*/ + const struct LPS25HVMT *vmt; + /** @brief Base barometer interface.*/ + BaseBarometer baro_if; + /** @brief Base thermometer interface.*/ + BaseThermometer thermo_if; _lps25h_data -}; - -/** - * @brief Structure representing a LPS25H driver. - */ -typedef struct LPS25HDriver LPS25HDriver; +}; /** @} */ /*===========================================================================*/ /* Driver macros. */ /*===========================================================================*/ - + +/** + * @brief Return the number of axes of the BaseBarometer. + * + * @param[in] devp pointer to @p LPS25HDriver. + * + * @return the number of axes. + * + * @api + */ +#define lps25hBarometerGetAxesNumber(devp) \ + barometerGetAxesNumber(&((devp)->baro_if)) + +/** + * @brief Retrieves raw data from the BaseBarometer. + * @note This data is retrieved from MEMS register without any algebraical + * manipulation. + * @note The axes array must be at least the same size of the + * BaseBarometer axes number. + * + * @param[in] devp pointer to @p LPS25HDriver. + * @param[out] axes a buffer which would be filled with raw data. + * + * @return The operation status. + * @retval MSG_OK if the function succeeded. + * @retval MSG_RESET if one or more I2C errors occurred, the errors can + * be retrieved using @p i2cGetErrors(). + * @retval MSG_TIMEOUT if a timeout occurred before operation end. + * + * @api + */ +#define lps25hBarometerReadRaw(devp, axes) \ + barometerReadRaw(&((devp)->baro_if), axes) + +/** + * @brief Retrieves cooked data from the BaseBarometer. + * @note This data is manipulated according to the formula + * cooked = (raw * sensitivity) - bias. + * @note Final data is expressed as hPa. + * @note The axes array must be at least the same size of the + * BaseBarometer axes number. + * + * @param[in] devp pointer to @p LPS25HDriver. + * @param[out] axes a buffer which would be filled with cooked data. + * + * @return The operation status. + * @retval MSG_OK if the function succeeded. + * @retval MSG_RESET if one or more I2C errors occurred, the errors can + * be retrieved using @p i2cGetErrors(). + * @retval MSG_TIMEOUT if a timeout occurred before operation end. + * + * @api + */ +#define lps25hBarometerReadCooked(devp, axes) \ + barometerReadCooked(&((devp)->baro_if), axes) + +/** + * @brief Set bias values for the BaseBarometer. + * @note Bias must be expressed as hPa. + * @note The bias buffer must be at least the same size of the + * BaseBarometer axes number. + * + * @param[in] devp pointer to @p LPS25HDriver. + * @param[in] bp a buffer which contains biases. + * + * @return The operation status. + * @retval MSG_OK if the function succeeded. + * + * @api + */ +#define lps25hBarometerSetBias(devp, bp) \ + barometerSetBias(&((devp)->baro_if), bp) + +/** + * @brief Reset bias values for the BaseBarometer. + * @note Default biases value are obtained from device datasheet when + * available otherwise they are considered zero. + * + * @param[in] devp pointer to @p LPS25HDriver. + * + * @return The operation status. + * @retval MSG_OK if the function succeeded. + * + * @api + */ +#define lps25hBarometerResetBias(devp) \ + barometerResetBias(&((devp)->baro_if)) + +/** + * @brief Set sensitivity values for the BaseBarometer. + * @note Sensitivity must be expressed as hPa/LSB. + * @note The sensitivity buffer must be at least the same size of the + * BaseBarometer axes number. + * + * @param[in] devp pointer to @p LPS25HDriver. + * @param[in] sp a buffer which contains sensitivities. + * + * @return The operation status. + * @retval MSG_OK if the function succeeded. + * + * @api + */ +#define lps25hBarometerSetSensitivity(devp, sp) \ + barometerSetSensitivity(&((devp)->baro_if), sp) + +/** + * @brief Reset sensitivity values for the BaseBarometer. + * @note Default sensitivities value are obtained from device datasheet. + * + * @param[in] devp pointer to @p LPS25HDriver. + * + * @return The operation status. + * @retval MSG_OK if the function succeeded. + * + * @api + */ +#define lps25hBarometerResetSensitivity(devp) \ + barometerResetSensitivity(&((devp)->baro_if)) + +/** + * @brief Return the number of axes of the BaseThermometer. + * + * @param[in] devp pointer to @p LPS25HDriver. + * + * @return the number of axes. + * + * @api + */ +#define lps25hThermometerGetAxesNumber(devp) \ + thermometerGetAxesNumber(&((devp)->thermo_if)) + +/** + * @brief Retrieves raw data from the BaseThermometer. + * @note This data is retrieved from MEMS register without any algebraical + * manipulation. + * @note The axes array must be at least the same size of the + * BaseThermometer axes number. + * + * @param[in] devp pointer to @p LPS25HDriver. + * @param[out] axes a buffer which would be filled with raw data. + * + * @return The operation status. + * @retval MSG_OK if the function succeeded. + * @retval MSG_RESET if one or more I2C errors occurred, the errors can + * be retrieved using @p i2cGetErrors(). + * @retval MSG_TIMEOUT if a timeout occurred before operation end. + * + * @api + */ +#define lps25hThermometerReadRaw(devp, axes) \ + thermometerReadRaw(&((devp)->thermo_if), axes) + +/** + * @brief Retrieves cooked data from the BaseThermometer. + * @note This data is manipulated according to the formula + * cooked = (raw * sensitivity) - bias. + * @note Final data is expressed as °C. + * @note The axes array must be at least the same size of the + * BaseThermometer axes number. + * + * @param[in] devp pointer to @p LPS25HDriver. + * @param[out] axes a buffer which would be filled with cooked data. + * + * @return The operation status. + * @retval MSG_OK if the function succeeded. + * @retval MSG_RESET if one or more I2C errors occurred, the errors can + * be retrieved using @p i2cGetErrors(). + * @retval MSG_TIMEOUT if a timeout occurred before operation end. + * + * @api + */ +#define lps25hThermometerReadCooked(devp, axes) \ + thermometerReadCooked(&((devp)->thermo_if), axes) + +/** + * @brief Set bias values for the BaseThermometer. + * @note Bias must be expressed as °C. + * @note The bias buffer must be at least the same size of the + * BaseThermometer axes number. + * + * @param[in] devp pointer to @p LPS25HDriver. + * @param[in] bp a buffer which contains biases. + * + * @return The operation status. + * @retval MSG_OK if the function succeeded. + * + * @api + */ +#define lps25hThermometerSetBias(devp, bp) \ + thermometerSetBias(&((devp)->thermo_if), bp) + +/** + * @brief Reset bias values for the BaseThermometer. + * @note Default biases value are obtained from device datasheet when + * available otherwise they are considered zero. + * + * @param[in] devp pointer to @p LPS25HDriver. + * + * @return The operation status. + * @retval MSG_OK if the function succeeded. + * + * @api + */ +#define lps25hThermometerResetBias(devp) \ + thermometerResetBias(&((devp)->thermo_if)) + +/** + * @brief Set sensitivity values for the BaseThermometer. + * @note Sensitivity must be expressed as °C/LSB. + * @note The sensitivity buffer must be at least the same size of the + * BaseThermometer axes number. + * + * @param[in] devp pointer to @p LPS25HDriver. + * @param[in] sp a buffer which contains sensitivities. + * + * @return The operation status. + * @retval MSG_OK if the function succeeded. + * + * @api + */ +#define lps25hThermometerSetSensitivity(devp, sp) \ + thermometerSetSensitivity(&((devp)->thermo_if), sp) + +/** + * @brief Reset sensitivity values for the BaseThermometer. + * @note Default sensitivities value are obtained from device datasheet. + * + * @param[in] devp pointer to @p LPS25HDriver. + * + * @return The operation status. + * @retval MSG_OK if the function succeeded. + * + * @api + */ +#define lps25hThermometerResetSensitivity(devp) \ + thermometerResetSensitivity(&((devp)->thermo_if)) + /*===========================================================================*/ /* External declarations. */ /*===========================================================================*/ diff --git a/testex/STM32/STM32F4xx/I2C-LPS25H/Makefile b/testex/STM32/STM32F4xx/I2C-LPS25H/Makefile index 3719f5377..5e16f5197 100644 --- a/testex/STM32/STM32F4xx/I2C-LPS25H/Makefile +++ b/testex/STM32/STM32F4xx/I2C-LPS25H/Makefile @@ -87,6 +87,9 @@ PROJECT = ch # Imported source files and paths CHIBIOS = ../../../.. + +# Licensing files. +include $(CHIBIOS)/os/license/license.mk # Startup files. include $(CHIBIOS)/os/common/startup/ARMCMx/compilers/GCC/mk/startup_stm32f4xx.mk # HAL-OSAL files (optional). @@ -97,31 +100,23 @@ include $(CHIBIOS)/os/hal/osal/rt/osal.mk # RTOS files (optional). include $(CHIBIOS)/os/rt/rt.mk include $(CHIBIOS)/os/common/ports/ARMCMx/compilers/GCC/mk/port_v7m.mk -# Other files (optional). +# EX files (optional). include $(CHIBIOS)/os/ex/ST/lps25h.mk +# Other files (optional). include $(CHIBIOS)/os/hal/lib/streams/streams.mk -include $(CHIBIOS)/os/various/shell/shell.mk # Define linker script file here LDSCRIPT= $(STARTUPLD)/STM32F401xE.ld # C sources that can be compiled in ARM or THUMB mode depending on the global # setting. -CSRC = $(STARTUPSRC) \ - $(KERNSRC) \ - $(PORTSRC) \ - $(OSALSRC) \ - $(HALSRC) \ - $(PLATFORMSRC) \ - $(BOARDSRC) \ - $(LPS25HSRC) \ - $(STREAMSSRC) \ - $(SHELLSRC) \ +CSRC = $(ALLCSRC) \ + $(TESTSRC) \ main.c # C++ sources that can be compiled in ARM or THUMB mode depending on the global # setting. -CPPSRC = +CPPSRC = $(ALLCPPSRC) # C sources to be compiled in ARM mode regardless of the global setting. # NOTE: Mixing ARM and THUMB mode enables the -mthumb-interwork compiler @@ -147,10 +142,11 @@ TCPPSRC = ASMSRC = ASMXSRC = $(STARTUPASM) $(PORTASM) $(OSALASM) -INCDIR = $(CHIBIOS)/os/license \ - $(STARTUPINC) $(KERNINC) $(PORTINC) $(OSALINC) \ - $(HALINC) $(PLATFORMINC) $(BOARDINC) $(LPS25HINC) \ - $(STREAMSINC) $(SHELLINC) +# List ASM source files here +ASMSRC = $(ALLASMSRC) +ASMXSRC = $(ALLXASMSRC) + +INCDIR = $(ALLINC) $(TESTINC) # # Project, sources and paths @@ -200,7 +196,7 @@ CPPWARN = -Wall -Wextra -Wundef # # List all user C define here, like -D_DEBUG=1 -UDEFS = -DCHPRINTF_USE_FLOAT=1 -DSHELL_CMD_TEST_ENABLED=0 \ +UDEFS = -DCHPRINTF_USE_FLOAT=1 \ -DLPS25H_USE_ADVANCED=0 -DLPS25H_SHARED_I2C=0 # Define ASM defines here diff --git a/testex/STM32/STM32F4xx/I2C-LPS25H/main.c b/testex/STM32/STM32F4xx/I2C-LPS25H/main.c index 8aafdc19a..640a82c91 100644 --- a/testex/STM32/STM32F4xx/I2C-LPS25H/main.c +++ b/testex/STM32/STM32F4xx/I2C-LPS25H/main.c @@ -17,12 +17,11 @@ #include "ch.h" #include "hal.h" -#include "string.h" -#include "shell.h" #include "chprintf.h" - #include "lps25h.h" +#define cls(chp) chprintf(chp, "\033[2J\033[1;1H") + /*===========================================================================*/ /* LPS25H related. */ /*===========================================================================*/ @@ -30,8 +29,11 @@ /* LPS25H Driver: This object represent an LPS25H instance */ static LPS25HDriver LPS25HD1; -static int32_t rawdata; -static float cookeddata; +static int32_t baroraw; +static int32_t thermoraw; + +static float barocooked; +static float thermocooked; static const I2CConfig i2ccfg = { OPMODE_I2C, @@ -42,9 +44,11 @@ static const I2CConfig i2ccfg = { static const LPS25HConfig lps25hcfg = { &I2CD1, &i2ccfg, + LPS25H_SAD_VCC, + NULL, + NULL, NULL, NULL, - LPS25H_SAD_VCC, LPS25H_ODR_7HZ, #if LPS25H_USE_ADVANCED LPS25H_BDU_CONTINUOUS, @@ -54,59 +58,10 @@ static const LPS25HConfig lps25hcfg = { }; /*===========================================================================*/ -/* Command line related. */ -/*===========================================================================*/ - -/* Enable use of special ANSI escape sequences */ -#define CHPRINTF_USE_ANSI_CODE TRUE -#define SHELL_WA_SIZE THD_WORKING_AREA_SIZE(2048) - -static void cmd_read(BaseSequentialStream *chp, int argc, char *argv[]) { - - if (argc != 1) { - chprintf(chp, "Usage: read [raw|cooked]\r\n"); - return; - } - - while (chnGetTimeout((BaseChannel *)chp, 150) == Q_TIMEOUT) { - if (!strcmp (argv[0], "raw")) { -#if CHPRINTF_USE_ANSI_CODE - chprintf(chp, "\033[2J\033[1;1H"); -#endif - barometerReadRaw(&LPS25HD1, &rawdata); - chprintf(chp, "LPS25H Barometer raw data...\r\n"); - chprintf(chp, "RAW: %d\r\n", rawdata); - } - else if (!strcmp (argv[0], "cooked")) { -#if CHPRINTF_USE_ANSI_CODE - chprintf(chp, "\033[2J\033[1;1H"); -#endif - barometerReadCooked(&LPS25HD1, &cookeddata); - chprintf(chp, "LPS25H Barometer cooked data...\r\n"); - chprintf(chp, "COOCKED: %.4f hPa\r\n", cookeddata); - } - else { - chprintf(chp, "Usage: read [raw|cooked]\r\n"); - return; - } - } - chprintf(chp, "Stopped\r\n"); -} - -static const ShellCommand commands[] = { - {"read", cmd_read}, - {NULL, NULL} -}; - -static const ShellConfig shell_cfg1 = { - (BaseSequentialStream *)&SD2, - commands -}; - -/*===========================================================================*/ -/* Main code. */ +/* Generic code. */ /*===========================================================================*/ +static BaseSequentialStream* chp = (BaseSequentialStream*)&SD2; /* * LED blinker thread, times are in milliseconds. */ @@ -116,9 +71,7 @@ static THD_FUNCTION(Thread1, arg) { (void)arg; chRegSetThreadName("blinker"); while (true) { - palClearPad(GPIOA, GPIOA_LED_GREEN); - chThdSleepMilliseconds(500); - palSetPad(GPIOA, GPIOA_LED_GREEN); + palToggleLine(LINE_LED_GREEN); chThdSleepMilliseconds(500); } } @@ -138,43 +91,44 @@ int main(void) { halInit(); chSysInit(); + /* Configuring I2C SCK and I2C SDA related GPIOs .*/ palSetLineMode(LINE_ARD_D15, PAL_MODE_ALTERNATE(4) | PAL_STM32_OSPEED_HIGHEST | PAL_STM32_OTYPE_OPENDRAIN); palSetLineMode(LINE_ARD_D14, PAL_MODE_ALTERNATE(4) | PAL_STM32_OSPEED_HIGHEST | PAL_STM32_OTYPE_OPENDRAIN); - /* - * Activates the serial driver 2 using the driver default configuration. - */ + /* Activates the serial driver 1 using the driver default configuration.*/ sdStart(&SD2, NULL); - /* - * Creates the blinker thread. - */ + /* Creates the blinker thread.*/ chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); - /* - * LPS25H Object Initialization - */ + /* LPS25H Object Initialization.*/ lps25hObjectInit(&LPS25HD1); - /* - * Activates the LPS25H driver. - */ + /* Activates the LPS25H driver.*/ lps25hStart(&LPS25HD1, &lps25hcfg); - /* - * Shell manager initialization. - */ - shellInit(); + /* Normal main() thread activity, printing MEMS data on the SD2. */ + while (true) { + lps25hBarometerReadRaw(&LPS25HD1, &baroraw); + chprintf(chp, "LPS25HD1 Barometer raw data...\r\n"); + chprintf(chp, "Pres: %d\r\n", baroraw); + + lps25hThermometerReadRaw(&LPS25HD1, &thermoraw); + chprintf(chp, "LPS25HD1 Thermometer raw data...\r\n"); + chprintf(chp, "Temp: %d\r\n", &thermoraw); + + lps25hBarometerReadCooked(&LPS25HD1, &barocooked); + chprintf(chp, "LPS25HD1 Barometer cooked data...\r\n"); + chprintf(chp, "Pres: %.2f\r\n", barocooked); - while(TRUE) { + lps25hThermometerReadCooked(&LPS25HD1, &thermocooked); + chprintf(chp, "LPS25HD1 Thermometer cooked data...\r\n"); + chprintf(chp, "Temp: %.2f\r\n", thermocooked); - thread_t *shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE, - "shell", NORMALPRIO + 1, - shellThread, (void *)&shell_cfg1); - chThdWait(shelltp); /* Waiting termination. */ - chThdSleepMilliseconds(1000); + chThdSleepMilliseconds(100); + cls(chp); } lps25hStop(&LPS25HD1); } -- cgit v1.2.3