aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRocco Marco Guglielmi <roccomarco.guglielmi@gmail.com>2018-03-09 23:45:17 +0000
committerRocco Marco Guglielmi <roccomarco.guglielmi@gmail.com>2018-03-09 23:45:17 +0000
commit8887dbbde2e35d3ed21a8b5cee9be2a68bfe900c (patch)
tree74977e8385b1b05467df40b0c7fa5a9f2c435118
parent6f6cad8190e851bd31d8b0e8d9a92d0207939e73 (diff)
downloadChibiOS-8887dbbde2e35d3ed21a8b5cee9be2a68bfe900c.tar.gz
ChibiOS-8887dbbde2e35d3ed21a8b5cee9be2a68bfe900c.tar.bz2
ChibiOS-8887dbbde2e35d3ed21a8b5cee9be2a68bfe900c.zip
Updated HTS221 Driver and related demo
git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@11678 110e8d01-0319-4d1e-a829-52ad28d1bb01
-rw-r--r--os/ex/ST/hts221.c693
-rw-r--r--os/ex/ST/hts221.h398
-rw-r--r--testex/STM32/STM32F4xx/I2C-HTS221/Makefile34
-rw-r--r--testex/STM32/STM32F4xx/I2C-HTS221/debug/STM32F4xx-I2C-HTS221 (OpenOCD, Flash and Run).launch2
-rw-r--r--testex/STM32/STM32F4xx/I2C-HTS221/main.c172
5 files changed, 820 insertions, 479 deletions
diff --git a/os/ex/ST/hts221.c b/os/ex/ST/hts221.c
index 570726248..a426b2c9a 100644
--- a/os/ex/ST/hts221.c
+++ b/os/ex/ST/hts221.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.
@@ -53,7 +53,6 @@
/* Driver local functions. */
/*===========================================================================*/
-#if (HTS221_USE_I2C) || defined(__DOXYGEN__)
/**
* @brief Reads registers value using I2C.
* @pre The I2C interface must be initialized and the driver started.
@@ -63,9 +62,10 @@
* @param[out] rxbuf pointer to an output buffer
* @param[in] n number of consecutive register to read
* @return the operation status.
+ *
* @notapi
*/
-msg_t hts221I2CReadRegister(I2CDriver *i2cp, uint8_t reg, uint8_t* rxbuf,
+static msg_t hts221I2CReadRegister(I2CDriver *i2cp, uint8_t reg, uint8_t* rxbuf,
size_t n) {
uint8_t txbuf = reg;
if (n > 1)
@@ -85,31 +85,32 @@ msg_t hts221I2CReadRegister(I2CDriver *i2cp, uint8_t reg, uint8_t* rxbuf,
* @param[in] n size of txbuf less one (not considering the first
* element)
* @return the operation status.
+ *
* @notapi
*/
-msg_t hts221I2CWriteRegister(I2CDriver *i2cp, uint8_t* txbuf, size_t n) {
+static msg_t hts221I2CWriteRegister(I2CDriver *i2cp, uint8_t* txbuf, size_t n) {
if (n > 1)
(*txbuf) |= HTS221_SUB_MS;
return i2cMasterTransmitTimeout(i2cp, HTS221_SAD, txbuf, n + 1, NULL, 0,
TIME_INFINITE);
}
-#endif /* HTS221_USE_I2C */
/**
- * @brief Compute biases and sensitivities starting from data stored in
+ * @brief Computes biases and sensitivities starting from data stored in
* calibration registers.
- * @notapi
+ * @note Factory bias and sensitivity values are stored into the driver
+ * structure.
*
* @param[in] devp pointer to the HTS221 interface
- * @param[in] flag flag to select parameters
* @return the operation status.
+ *
+ * @notapi
*/
-msg_t hts221Calibrate(HTS221Driver *devp, uint8_t flag) {
+static msg_t hts221Calibrate(HTS221Driver *devp) {
msg_t msg;
uint8_t calib[16], H0_rH_x2, H1_rH_x2, msb;
int16_t H0_T0_OUT, H1_T0_OUT, T0_degC_x8, T1_degC_x8, T0_OUT, T1_OUT;
- float sens;
#if HTS221_SHARED_I2C
i2cAcquireBus(devp->config->i2cp);
@@ -132,6 +133,7 @@ msg_t hts221Calibrate(HTS221Driver *devp, uint8_t flag) {
H1_T0_OUT += calib[11] << 8;
T0_degC_x8 = calib[2];
+
/* Completing T0_degC_x8 value */
msb = (calib[5] & HTS221_SEL(0x03, 0));
if (msb & HTS221_SEL(0x01, 1)) {
@@ -152,277 +154,465 @@ msg_t hts221Calibrate(HTS221Driver *devp, uint8_t flag) {
T1_OUT = calib[14];
T1_OUT += calib[15] << 8;
- sens = ((float)H1_rH_x2 - (float)H0_rH_x2) /
- (2.0f * ((float)H1_T0_OUT - (float)H0_T0_OUT));
-
- if (flag & HTS221_FLAG_HYGRO_SENS)
- devp->sensitivity[0] = sens;
-
- if (flag & HTS221_FLAG_HYGRO_BIAS)
- devp->bias[0] = (sens * (float)H0_T0_OUT) -
- ((float)H0_rH_x2 / 2.0f);
-
- sens = ((float)T1_degC_x8 - (float)T0_degC_x8) /
- (8.0f * ((float)T1_OUT - (float)T0_OUT));
-
- if (flag & HTS221_FLAG_THERMO_SENS)
- devp->sensitivity[1] = sens;
-
- if (flag & HTS221_FLAG_THERMO_BIAS)
- devp->bias[1] = (sens * (float)T0_OUT) -
- ((float)T0_degC_x8 / 8.0f);
+ devp->hygrofactorysensitivity = ((float)H1_rH_x2 - (float)H0_rH_x2) /
+ (((float)H1_T0_OUT - (float)H0_T0_OUT) * 2.0f);
+
+
+ devp->hygrofactorybias = (devp->hygrofactorysensitivity * (float)H0_T0_OUT) -
+ ((float)H0_rH_x2 / 2.0f);
+
+ devp->thermofactorysensitivity = ((float)T1_degC_x8 - (float)T0_degC_x8) /
+ (((float)T1_OUT - (float)T0_OUT) * 8.0f);
+
+ devp->thermofactorybias = (devp->thermofactorysensitivity * (float)T0_OUT) -
+ ((float)T0_degC_x8 / 8.0f);
return msg;
}
-/*
- * Interface implementation.
+/**
+ * @brief Return the number of axes of the BaseHygrometer.
+ *
+ * @param[in] ip pointer to @p BaseHygrometer interface.
+ *
+ * @return the number of axes.
*/
static size_t hygro_get_axes_number(void *ip) {
-
- osalDbgCheck(ip != NULL);
+ (void)ip;
+
return HTS221_HYGRO_NUMBER_OF_AXES;
}
-static size_t thermo_get_axes_number(void *ip) {
-
- osalDbgCheck(ip != NULL);
- return HTS221_THERMO_NUMBER_OF_AXES;
-}
-
-static size_t sens_get_axes_number(void *ip) {
-
- osalDbgCheck(ip != NULL);
- return (thermo_get_axes_number(ip) + hygro_get_axes_number(ip));
-}
-
-static msg_t hygro_read_raw(void *ip, int32_t* axis) {
+/**
+ * @brief Retrieves raw data from the BaseHygrometer.
+ * @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
+ * BaseHygrometer axes number.
+ *
+ * @param[in] ip pointer to @p BaseHygrometer 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 hygro_read_raw(void *ip, int32_t axes[]) {
+ HTS221Driver* devp;
+ uint8_t buff[2];
int16_t tmp;
- uint8_t buff[2];
- msg_t msg = MSG_OK;
+ msg_t msg;
+
+ osalDbgCheck((ip != NULL) && (axes != NULL));
- *axis = 0;
+ /* Getting parent instance pointer.*/
+ devp = objGetInstance(HTS221Driver*, (BaseHygrometer*)ip);
- osalDbgCheck((ip != NULL) && (axis != NULL));
- osalDbgAssert((((HTS221Driver *)ip)->state == HTS221_READY),
+ osalDbgAssert((devp->state == HTS221_READY),
"hygro_read_raw(), invalid state");
-#if HTS221_USE_I2C
- osalDbgAssert((((HTS221Driver *)ip)->config->i2cp->state == I2C_READY),
+
+ osalDbgAssert((devp->config->i2cp->state == I2C_READY),
"hygro_read_raw(), channel not ready");
#if HTS221_SHARED_I2C
- i2cAcquireBus(((HTS221Driver *)ip)->config->i2cp);
- i2cStart(((HTS221Driver *)ip)->config->i2cp,
- ((HTS221Driver *)ip)->config->i2ccfg);
+ i2cAcquireBus(devp->config->i2cp);
+ i2cStart(devp->config->i2cp,
+ devp->config->i2ccfg);
#endif /* HTS221_SHARED_I2C */
- msg = hts221I2CReadRegister(((HTS221Driver *)ip)->config->i2cp,
- HTS221_AD_HUMIDITY_OUT_L, buff, 2);
+ msg = hts221I2CReadRegister(devp->config->i2cp, HTS221_AD_HUMIDITY_OUT_L,
+ buff, 2);
#if HTS221_SHARED_I2C
- i2cReleaseBus(((HTS221Driver *)ip)->config->i2cp);
+ i2cReleaseBus(devp->config->i2cp);
#endif /* HTS221_SHARED_I2C */
-#endif /* HTS221_USE_I2C */
if (msg == MSG_OK) {
tmp = buff[0] + (buff[1] << 8);
- *axis = (int32_t)tmp;
+ *axes = (int32_t)tmp;
}
return msg;
}
-static msg_t thermo_read_raw(void *ip, int32_t axis[]) {
- int16_t tmp;
- uint8_t buff[2];
- msg_t msg = MSG_OK;
+/**
+ * @brief Retrieves cooked data from the BaseHygrometer.
+ * @note This data is manipulated according to the formula
+ * cooked = (raw * sensitivity) - bias.
+ * @note Final data is expressed as %rH.
+ * @note The axes array must be at least the same size of the
+ * BaseHygrometer axes number.
+ *
+ * @param[in] ip pointer to @p BaseHygrometer 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 hygro_read_cooked(void *ip, float axes[]) {
+ HTS221Driver* devp;
+ int32_t raw;
+ msg_t msg;
- *axis = 0.0f;
+ osalDbgCheck((ip != NULL) && (axes != NULL));
- osalDbgCheck((ip != NULL) && (axis != NULL));
- osalDbgAssert((((HTS221Driver *)ip)->state == HTS221_READY),
- "thermo_read_raw(), invalid state");
+ /* Getting parent instance pointer.*/
+ devp = objGetInstance(HTS221Driver*, (BaseHygrometer*)ip);
-#if HTS221_USE_I2C
- osalDbgAssert((((HTS221Driver *)ip)->config->i2cp->state == I2C_READY),
- "thermo_read_raw(), channel not ready");
-
-#if HTS221_SHARED_I2C
- i2cAcquireBus(((HTS221Driver *)ip)->config->i2cp);
- i2cStart(((HTS221Driver *)ip)->config->i2cp,
- ((HTS221Driver *)ip)->config->i2ccfg);
-#endif /* HTS221_SHARED_I2C */
+ osalDbgAssert((devp->state == HTS221_READY),
+ "hygro_read_cooked(), invalid state");
- msg = hts221I2CReadRegister(((HTS221Driver *)ip)->config->i2cp,
- HTS221_AD_TEMP_OUT_L, buff, 2);
-
-#if HTS221_SHARED_I2C
- i2cReleaseBus(((HTS221Driver *)ip)->config->i2cp);
-#endif /* HTS221_SHARED_I2C */
-#endif /* HTS221_USE_I2C */
+ msg = hygro_read_raw(ip, &raw);
- if (msg == MSG_OK) {
- tmp = buff[0] + (buff[1] << 8);
- *axis = (int32_t)tmp;
- }
+ *axes = (raw * devp->hygrosensitivity) - devp->hygrobias;
+
return msg;
}
-static msg_t sens_read_raw(void *ip, int32_t axes[]) {
- int32_t* bp = axes;
- msg_t msg;
- msg = hygro_read_raw(ip, bp);
- if (msg != MSG_OK){
- return msg;
- }
- bp += HTS221_HYGRO_NUMBER_OF_AXES;
- return thermo_read_raw(ip, bp);
-}
+/**
+ * @brief Set bias values for the BaseHygrometer.
+ * @note Bias must be expressed as %rH.
+ * @note The bias buffer must be at least the same size of the
+ * BaseHygrometer axes number.
+ *
+ * @param[in] ip pointer to @p BaseHygrometer 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 hygro_set_bias(void *ip, float *bp) {
+ HTS221Driver* devp;
+ msg_t msg = MSG_OK;
+
+ osalDbgCheck((ip != NULL) && (bp != NULL));
-static msg_t hygro_read_cooked(void *ip, float* axis) {
- int32_t raw;
- msg_t msg;
+ /* Getting parent instance pointer.*/
+ devp = objGetInstance(HTS221Driver*, (BaseHygrometer*)ip);
+
+ osalDbgAssert((devp->state == HTS221_READY),
+ "hygro_set_bias(), invalid state");
- osalDbgCheck((ip != NULL) && (axis != NULL));
- osalDbgAssert((((HTS221Driver *)ip)->state == HTS221_READY),
- "hygro_read_cooked(), invalid state");
+ devp->hygrobias = *bp;
+ return msg;
+}
- msg = hygro_read_raw(ip, &raw);
+/**
+ * @brief Reset bias values for the BaseHygrometer.
+ * @note Default biases value are obtained from device datasheet when
+ * available otherwise they are considered zero.
+ *
+ * @param[in] ip pointer to @p BaseHygrometer interface.
+ *
+ * @return The operation status.
+ * @retval MSG_OK if the function succeeded.
+ */
+static msg_t hygro_reset_bias(void *ip) {
+ HTS221Driver* devp;
+ msg_t msg = MSG_OK;
+
+ osalDbgCheck(ip != NULL);
- *axis = raw * ((HTS221Driver *)ip)->sensitivity[0];
- *axis -= ((HTS221Driver *)ip)->bias[0];
+ /* Getting parent instance pointer.*/
+ devp = objGetInstance(HTS221Driver*, (BaseHygrometer*)ip);
+
+ osalDbgAssert((devp->state == HTS221_READY),
+ "hygro_reset_bias(), invalid state");
+
+ devp->hygrobias = devp->hygrofactorybias;
return msg;
}
-static msg_t thermo_read_cooked(void *ip, float* axis) {
- int32_t raw;
- msg_t msg;
+/**
+ * @brief Set sensitivity values for the BaseHygrometer.
+ * @note Sensitivity must be expressed as %rH/LSB.
+ * @note The sensitivity buffer must be at least the same size of the
+ * BaseHygrometer axes number.
+ *
+ * @param[in] ip pointer to @p BaseHygrometer interface.
+ * @param[in] sp a buffer which contains sensitivities.
+ *
+ * @return The operation status.
+ * @retval MSG_OK if the function succeeded.
+ */
+static msg_t hygro_set_sensitivity(void *ip, float *sp) {
+ HTS221Driver* devp;
+ msg_t msg = MSG_OK;
+
+ osalDbgCheck((ip != NULL) && (sp != NULL));
- osalDbgCheck((ip != NULL) && (axis != NULL));
+ /* Getting parent instance pointer.*/
+ devp = objGetInstance(HTS221Driver*, (BaseHygrometer*)ip);
+
+ osalDbgAssert((devp->state == HTS221_READY),
+ "hygro_set_sensitivity(), invalid state");
- osalDbgAssert((((HTS221Driver *)ip)->state == HTS221_READY),
- "thermo_read_cooked(), invalid state");
+ devp->hygrosensitivity = *sp;
+ return msg;
+}
- msg = thermo_read_raw(ip, &raw);
+/**
+ * @brief Reset sensitivity values for the BaseHygrometer.
+ * @note Default sensitivities value are obtained from device datasheet.
+ *
+ * @param[in] ip pointer to @p BaseHygrometer interface.
+ *
+ * @return The operation status.
+ * @retval MSG_OK if the function succeeded.
+ */
+static msg_t hygro_reset_sensitivity(void *ip) {
+ HTS221Driver* devp;
+ msg_t msg = MSG_OK;
+
+ osalDbgCheck(ip != NULL);
- *axis = raw * ((HTS221Driver *)ip)->sensitivity[1];
- *axis -= ((HTS221Driver *)ip)->bias[1];
+ /* Getting parent instance pointer.*/
+ devp = objGetInstance(HTS221Driver*, (BaseHygrometer*)ip);
+
+ osalDbgAssert((devp->state == HTS221_READY),
+ "hygro_reset_sensitivity(), invalid state");
+
+ devp->hygrosensitivity = devp->hygrofactorysensitivity;
return msg;
}
-static msg_t sens_read_cooked(void *ip, float axes[]) {
- float *dp = axes;
- msg_t 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 HTS221_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[]) {
+ HTS221Driver* devp;
+ int16_t tmp;
+ uint8_t buff[2];
+ msg_t msg;
+
osalDbgCheck((ip != NULL) && (axes != NULL));
- osalDbgAssert((((HTS221Driver *)ip)->state == HTS221_READY),
- "sens_read_cooked(), invalid state");
+ /* Getting parent instance pointer.*/
+ devp = objGetInstance(HTS221Driver*, (BaseThermometer*)ip);
+
+ osalDbgAssert((devp->state == HTS221_READY),
+ "thermo_read_raw(), invalid state");
+
+ osalDbgAssert((devp->config->i2cp->state == I2C_READY),
+ "thermo_read_raw(), channel not ready");
+
+#if HTS221_SHARED_I2C
+ i2cAcquireBus(devp->config->i2cp);
+ i2cStart(devp->config->i2cp,
+ devp->config->i2ccfg);
+#endif /* HTS221_SHARED_I2C */
- msg = hygro_read_cooked(ip, dp);
- if (msg != MSG_OK)
- return msg;
- dp += HTS221_THERMO_NUMBER_OF_AXES;
- return thermo_read_cooked(ip, dp);
+ msg = hts221I2CReadRegister(devp->config->i2cp, HTS221_AD_TEMP_OUT_L,
+ buff, 2);
+
+#if HTS221_SHARED_I2C
+ i2cReleaseBus(devp->config->i2cp);
+#endif /* HTS221_SHARED_I2C */
+
+ if (msg == MSG_OK) {
+ tmp = buff[0] + (buff[1] << 8);
+ *axes = (int32_t)tmp;
+ }
+ return msg;
}
-static msg_t hygro_set_bias(void *ip, float *bp) {
- osalDbgCheck((ip != NULL) && (bp != NULL));
+/**
+ * @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) {
+ HTS221Driver* devp;
+ int32_t raw;
+ msg_t msg;
- osalDbgAssert((((HTS221Driver *)ip)->state == HTS221_READY) ||
- (((HTS221Driver *)ip)->state == HTS221_STOP),
- "hygro_set_bias(), invalid state");
+ osalDbgCheck((ip != NULL) && (axis != NULL));
- ((HTS221Driver *)ip)->bias[0] = *bp;
- return MSG_OK;
-}
+ /* Getting parent instance pointer.*/
+ devp = objGetInstance(HTS221Driver*, (BaseThermometer*)ip);
+
+ osalDbgAssert((devp->state == HTS221_READY),
+ "thermo_read_cooked(), invalid state");
-static msg_t thermo_set_bias(void *ip, float *bp) {
- osalDbgCheck((ip != NULL) && (bp != NULL));
+ msg = thermo_read_raw(devp, &raw);
- osalDbgAssert((((HTS221Driver *)ip)->state == HTS221_READY) ||
- (((HTS221Driver *)ip)->state == HTS221_STOP),
- "thermo_set_bias(), invalid state");
+ *axis = (raw * devp->thermosensitivity) - devp->thermobias;
- ((HTS221Driver *)ip)->bias[1] = *bp;
- return MSG_OK;
+ return msg;
}
-static msg_t hygro_reset_bias(void *ip) {
- osalDbgCheck(ip != NULL);
-
- osalDbgAssert((((HTS221Driver *)ip)->state == HTS221_READY) ||
- (((HTS221Driver *)ip)->state == HTS221_STOP),
- "hygro_reset_bias(), invalid state");
+/**
+ * @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) {
+ HTS221Driver* devp;
+ msg_t msg = MSG_OK;
+
+ osalDbgCheck((ip != NULL) && (bp != NULL));
- return hts221Calibrate(ip, HTS221_FLAG_HYGRO_BIAS);
+ /* Getting parent instance pointer.*/
+ devp = objGetInstance(HTS221Driver*, (BaseThermometer*)ip);
+
+ osalDbgAssert((devp->state == HTS221_READY),
+ "thermo_set_bias(), invalid state");
+
+ devp->thermobias = *bp;
+
+ return msg;
}
+/**
+ * @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) {
+ HTS221Driver* devp;
+ msg_t msg = MSG_OK;
+
osalDbgCheck(ip != NULL);
- osalDbgAssert((((HTS221Driver *)ip)->state == HTS221_READY) ||
- (((HTS221Driver *)ip)->state == HTS221_STOP),
+ /* Getting parent instance pointer.*/
+ devp = objGetInstance(HTS221Driver*, (BaseThermometer*)ip);
+
+ osalDbgAssert((devp->state == HTS221_READY),
"thermo_reset_bias(), invalid state");
- return hts221Calibrate(ip, HTS221_FLAG_THERMO_BIAS);
-}
-
-static msg_t hygro_set_sensitivity(void *ip, float *sp) {
- osalDbgCheck((ip != NULL) && (sp != NULL));
-
- osalDbgAssert((((HTS221Driver *)ip)->state == HTS221_READY) ||
- (((HTS221Driver *)ip)->state == HTS221_STOP),
- "hygro_set_sensitivity(), invalid state");
-
- ((HTS221Driver *)ip)->sensitivity[0] = *sp;
- return MSG_OK;
+ devp->thermobias = devp->thermofactorybias;
+
+ 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) {
+ HTS221Driver* devp;
+ msg_t msg = MSG_OK;
+
osalDbgCheck((ip != NULL) && (sp != NULL));
- osalDbgAssert((((HTS221Driver *)ip)->state == HTS221_READY) ||
- (((HTS221Driver *)ip)->state == HTS221_STOP),
+ /* Getting parent instance pointer.*/
+ devp = objGetInstance(HTS221Driver*, (BaseThermometer*)ip);
+
+ osalDbgAssert((devp->state == HTS221_READY),
"thermo_set_sensitivity(), invalid state");
-
- return hts221Calibrate(ip, HTS221_FLAG_THERMO_SENS);
-}
-
-static msg_t hygro_reset_sensitivity(void *ip) {
- osalDbgCheck(ip != NULL);
-
- osalDbgAssert((((HTS221Driver *)ip)->state == HTS221_READY) ||
- (((HTS221Driver *)ip)->state == HTS221_STOP),
- "hygro_reset_sensitivity(), invalid state");
-
- return hts221Calibrate(ip, HTS221_FLAG_HYGRO_SENS);
+
+ 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) {
+ HTS221Driver* devp;
+ msg_t msg = MSG_OK;
+
osalDbgCheck(ip != NULL);
- osalDbgAssert((((HTS221Driver *)ip)->state == HTS221_READY) ||
- (((HTS221Driver *)ip)->state == HTS221_STOP),
+ /* Getting parent instance pointer.*/
+ devp = objGetInstance(HTS221Driver*, (BaseThermometer*)ip);
+
+ osalDbgAssert((devp->state == HTS221_READY),
"thermo_reset_sensitivity(), invalid state");
- ((HTS221Driver *)ip)->sensitivity[1] = HTS221_THERMO_SENS;
- return MSG_OK;
+ devp->thermosensitivity = devp->thermofactorysensitivity;
+
+ return msg;
}
-static const struct BaseSensorVMT vmt_sensor = {
- sens_get_axes_number, sens_read_raw, sens_read_cooked
+static const struct HTS221VMT vmt_device = {
+ (size_t)0
};
-static const struct HTS221HygrometerVMT vmt_hygrometer = {
+static const struct BaseHygrometerVMT vmt_hygrometer = {
+ sizeof(struct HTS221VMT*),
hygro_get_axes_number, hygro_read_raw, hygro_read_cooked,
- hygro_set_bias, hygro_reset_bias,
- hygro_set_sensitivity, hygro_reset_sensitivity
+ hygro_set_bias, hygro_reset_bias, hygro_set_sensitivity,
+ hygro_reset_sensitivity
};
-static const struct HTS221ThermometerVMT vmt_thermometer = {
+static const struct BaseThermometerVMT vmt_thermometer = {
+ sizeof(struct HTS221VMT*) + sizeof(BaseHygrometer),
thermo_get_axes_number, thermo_read_raw, thermo_read_cooked,
- thermo_set_bias, thermo_reset_bias,
- thermo_set_sensitivity, thermo_reset_sensitivity
+ thermo_set_bias, thermo_reset_bias, thermo_set_sensitivity,
+ thermo_reset_sensitivity
};
/*===========================================================================*/
@@ -438,15 +628,19 @@ static const struct HTS221ThermometerVMT vmt_thermometer = {
*/
void hts221ObjectInit(HTS221Driver *devp) {
- devp->vmt_sensor = &vmt_sensor;
- devp->vmt_hygrometer = &vmt_hygrometer;
- devp->vmt_thermometer = &vmt_thermometer;
+ devp->vmt = &vmt_device;
+ devp->hygro_if.vmt = &vmt_hygrometer;
+ devp->thermo_if.vmt = &vmt_thermometer;
+
devp->config = NULL;
- devp->state = HTS221_STOP;
- devp->bias[0] = 0.0;
- devp->bias[1] = 0.0;
- devp->sensitivity[0] = 0.0;
- devp->sensitivity[1] = 0.0;
+
+ devp->hygroaxes = HTS221_HYGRO_NUMBER_OF_AXES;
+ devp->thermoaxes = HTS221_THERMO_NUMBER_OF_AXES;
+
+ devp->hygrobias = 0.0f;
+ devp->thermobias = 0.0f;
+
+ devp->state = HTS221_STOP;
}
/**
@@ -462,11 +656,53 @@ void hts221Start(HTS221Driver *devp, const HTS221Config *config) {
osalDbgCheck((devp != NULL) && (config != NULL));
osalDbgAssert((devp->state == HTS221_STOP) || (devp->state == HTS221_READY),
- "hts221Start(), invalid state");
+ "hts221Start(), invalid state");
+
devp->config = config;
+
+#if HTS221_SHARED_I2C
+ i2cAcquireBus(devp->config->i2cp);
+#endif /* HTS221_SHARED_I2C */
+
+ i2cStart(devp->config->i2cp, devp->config->i2ccfg);
+ hts221Calibrate(devp);
+
+#if HTS221_SHARED_I2C
+ i2cReleaseBus(devp->config->i2cp);
+#endif /* HTS221_SHARED_I2C */
+
+ if(devp->config->hygrosensitivity == NULL) {
+ devp->hygrosensitivity = devp->hygrofactorysensitivity;
+ }
+ else{
+ /* Taking hygrometer sensitivity from user configurations */
+ devp->hygrosensitivity = *(devp->config->hygrosensitivity);
+ }
+
+ if(devp->config->hygrobias == NULL) {
+ devp->hygrobias = devp->hygrofactorybias;
+ }
+ else{
+ /* Taking hygrometer bias from user configurations */
+ devp->hygrobias = *(devp->config->hygrobias);
+ }
-#if HTS221_USE_I2C
+ if(devp->config->thermosensitivity == NULL) {
+ devp->thermosensitivity = devp->thermofactorysensitivity;
+ }
+ else{
+ /* Taking thermometer sensitivity from user configurations */
+ devp->thermosensitivity = *(devp->config->thermosensitivity);
+ }
+ if(devp->config->thermobias == NULL) {
+ devp->thermobias = devp->thermofactorybias;
+ }
+ else{
+ /* Taking thermometer bias from user configurations */
+ devp->thermobias = *(devp->config->thermobias);
+ }
+
/* Control register 1 configuration block.*/
{
cr[0] = HTS221_AD_CTRL_REG1;
@@ -475,16 +711,15 @@ void hts221Start(HTS221Driver *devp, const HTS221Config *config) {
cr[1] |= devp->config->blockdataupdate;
#endif
-#if HTS221_SHARED_I2C
- i2cAcquireBus((devp)->config->i2cp);
+#if HTS221_SHARED_I2C
+ i2cAcquireBus(devp->config->i2cp);
+ i2cStart(devp->config->i2cp, devp->config->i2ccfg);
#endif /* HTS221_SHARED_I2C */
- i2cStart((devp)->config->i2cp,
- (devp)->config->i2ccfg);
hts221I2CWriteRegister(devp->config->i2cp, cr, 1);
-#if HTS221_SHARED_I2C
- i2cReleaseBus((devp)->config->i2cp);
+#if HTS221_SHARED_I2C
+ i2cReleaseBus(devp->config->i2cp);
#endif /* HTS221_SHARED_I2C */
}
@@ -493,39 +728,20 @@ void hts221Start(HTS221Driver *devp, const HTS221Config *config) {
cr[0] = HTS221_AD_AV_CONF;
cr[1] = 0x05;
#if HTS221_USE_ADVANCED || defined(__DOXYGEN__)
- cr[1] = devp->config->reshumidity | devp->config->restemperature;
+ cr[1] = devp->config->hygroresolution | devp->config->thermoresolution;
#endif
-#if HTS221_SHARED_I2C
- i2cAcquireBus((devp)->config->i2cp);
- i2cStart((devp)->config->i2cp,
- (devp)->config->i2ccfg);
+
+#if HTS221_SHARED_I2C
+ i2cAcquireBus(devp->config->i2cp);
+ i2cStart(devp->config->i2cp, devp->config->i2ccfg);
#endif /* HTS221_SHARED_I2C */
hts221I2CWriteRegister(devp->config->i2cp, cr, 1);
-#if HTS221_SHARED_I2C
- i2cReleaseBus((devp)->config->i2cp);
+#if HTS221_SHARED_I2C
+ i2cReleaseBus(devp->config->i2cp);
#endif /* HTS221_SHARED_I2C */
}
-#endif /* HTS221_USE_I2C */
-
- if (devp->config->sensitivity == NULL) {
- hts221Calibrate(devp, HTS221_FLAG_HYGRO_SENS | HTS221_FLAG_THERMO_SENS);
- }
- else{
- /* Taking Sensitivity from user configurations */
- devp->sensitivity[0] = devp->config->sensitivity[0];
- devp->sensitivity[1] = devp->config->sensitivity[1];
- }
-
- if (devp->config->bias == NULL) {
- hts221Calibrate(devp, HTS221_FLAG_HYGRO_BIAS | HTS221_FLAG_THERMO_BIAS);
- }
- else {
- /* Taking Bias from user configurations */
- devp->bias[0] = devp->config->bias[0];
- devp->bias[1] = devp->config->bias[1];
- }
/* This is the MEMS transient recovery time */
osalThreadSleepMilliseconds(5);
@@ -549,21 +765,20 @@ void hts221Stop(HTS221Driver *devp) {
"hts221Stop(), invalid state");
if (devp->state == HTS221_READY) {
-#if (HTS221_USE_I2C)
-#if HTS221_SHARED_I2C
- i2cAcquireBus((devp)->config->i2cp);
- i2cStart((devp)->config->i2cp, (devp)->config->i2ccfg);
+
+#if HTS221_SHARED_I2C
+ i2cAcquireBus(devp->config->i2cp);
+ i2cStart(devp->config->i2cp, devp->config->i2ccfg);
#endif /* HTS221_SHARED_I2C */
cr[0] = HTS221_AD_CTRL_REG1;
cr[1] = 0;
hts221I2CWriteRegister(devp->config->i2cp, cr, 1);
- i2cStop((devp)->config->i2cp);
-#if HTS221_SHARED_I2C
- i2cReleaseBus((devp)->config->i2cp);
+ i2cStop(devp->config->i2cp);
+#if HTS221_SHARED_I2C
+ i2cReleaseBus(devp->config->i2cp);
#endif /* HTS221_SHARED_I2C */
-#endif /* HTS221_USE_I2C */
}
devp->state = HTS221_STOP;
}
diff --git a/os/ex/ST/hts221.h b/os/ex/ST/hts221.h
index 9f47aaea4..c6e595fb7 100644
--- a/os/ex/ST/hts221.h
+++ b/os/ex/ST/hts221.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.
@@ -44,7 +44,7 @@
/**
* @brief HTS221 driver version string.
*/
-#define EX_HTS221_VERSION "1.0.1"
+#define EX_HTS221_VERSION "1.0.2"
/**
* @brief HTS221 driver version major number.
@@ -59,22 +59,33 @@
/**
* @brief HTS221 driver version patch number.
*/
-#define EX_HTS221_PATCH 1
+#define EX_HTS221_PATCH 2
/** @} */
/**
- * @brief HTS221 characteristics.
+ * @brief HTS221 hygrometer subsystem characteristics.
+ * @note Sensitivity is expressed as %rH/LSB whereas %rH stand for percentage
+ * of relative humidity.
*
* @{
*/
#define HTS221_HYGRO_NUMBER_OF_AXES 1U
-#define HTS221_THERMO_NUMBER_OF_AXES 1U
-#define HTS221_HYGRO_SENS 256.0f /**< LSB/%rH */
-#define HTS221_THERMO_SENS 64.0f /**< LSB/°C */
+#define HTS221_HYGRO_SENS 0.00390625f
/** @} */
/**
+ * @brief HTS221 thermometer subsystem characteristics.
+ * @note Sensitivity is expressed as °C/LSB whereas.
+ *
+ * @{
+ */
+#define HTS221_THERMO_NUMBER_OF_AXES 1U
+
+#define HTS221_THERMO_SENS 00015625f
+/** @} */
+
+/**
* @name HTS221 communication interfaces related bit masks
* @{
*/
@@ -154,7 +165,7 @@
/** @} */
/*===========================================================================*/
-/* Driver pre-compile time settings. */
+/* Driver pre-thermoile time settings. */
/*===========================================================================*/
/**
@@ -194,7 +205,7 @@
* on each transaction.
* @note The default is @p FALSE. Requires I2C_USE_MUTUAL_EXCLUSION
*/
-#if !defined(HTS221_SHARED_SPI) || defined(__DOXYGEN__)
+#if !defined(HTS221_SHARED_I2C) || defined(__DOXYGEN__)
#define HTS221_SHARED_I2C FALSE
#endif
/** @} */
@@ -223,10 +234,6 @@
#error "HTS221_SHARED_I2C requires I2C_USE_MUTUAL_EXCLUSION"
#endif
-/*
- * TODO: Add SPI support.
- */
-
/*===========================================================================*/
/* Driver data structures and types. */
/*===========================================================================*/
@@ -236,6 +243,11 @@
* @{
*/
/**
+ * @brief Structure representing a HTS221 driver.
+ */
+typedef struct HTS221Driver HTS221Driver;
+
+/**
* @brief HTS221 output data rate and bandwidth.
*/
typedef enum {
@@ -316,17 +328,21 @@ typedef struct {
const I2CConfig *i2ccfg;
#endif /* HTS221_USE_I2C */
/**
- * @brief HTS221 initial sensitivity.
- * @note Value are respectively related to hygrometer
- * and thermometer.
+ * @brief HTS221 hygrometer subsystem initial sensitivity.
+ */
+ float *hygrosensitivity;
+ /**
+ * @brief HTS221 hygrometer subsystem initial bias.
+ */
+ float *hygrobias;
+ /**
+ * @brief HTS221 thermometer subsystem initial sensitivity.
*/
- float* sensitivity;
+ float *thermosensitivity;
/**
- * @brief HTS221 initial bias.
- * @note Value are respectively related to hygrometer
- * and thermometer.
+ * @brief HTS221 thermometer subsystem initial bias.
*/
- float* bias;
+ float *thermobias;
/**
* @brief HTS221 output data rate selection.
*/
@@ -337,104 +353,320 @@ typedef struct {
*/
hts221_bdu_t blockdataupdate;
/**
- * @brief HTS221 humidity resolution.
+ * @brief HTS221 hygrometer subsystem resolution.
*/
- hts221_avgh_t reshumidity;
+ hts221_avgh_t hygroresolution;
/**
- * @brief HTS221 temperature resolution.
+ * @brief HTS221 thermometer subsystem resolution.
*/
- hts221_avgt_t restemperature;
+ hts221_avgt_t thermoresolution;
#endif
} HTS221Config;
/**
- * @brief @p HTS221 hygrometer subsystem specific methods.
+ * @brief @p HTS221 specific methods.
* @note No methods so far, just a common ancestor interface.
*/
-#define _hts221_hygrometer_methods_alone
+#define _hts221_methods_alone
/**
- * @brief @p HTS221 hygrometer subsystem specific methods with inherited ones.
+ * @brief @p HTS221 specific methods with inherited ones.
*/
-#define _hts221_hygrometer_methods \
- _base_hygrometer_methods \
- _hts221_hygrometer_methods_alone
+#define _hts221_methods \
+ _base_object_methods \
+ _hts221_methods_alone
/**
- * @brief @p HTS221 thermometer subsystem specific methods.
- * @note No methods so far, just a common ancestor interface.
+ * @extends BaseObjectVMT
+ *
+ * @brief @p HTS221 virtual methods table.
*/
-#define _hts221_thermometer_methods_alone
-
+struct HTS221VMT {
+ _hts221_methods
+};
+
/**
- * @brief @p HTS221 compass subsystem specific methods with inherited ones.
+ * @brief @p HTS221Driver specific data.
+ */
+#define _hts221_data \
+ /* Driver state.*/ \
+ hts221_state_t state; \
+ /* Current configuration data.*/ \
+ const HTS221Config *config; \
+ /* Hygrometer subsystem axes number.*/ \
+ size_t hygroaxes; \
+ /* Hygrometer subsystem current sensitivity.*/ \
+ float hygrosensitivity; \
+ /* Hygrometer subsystem current bias .*/ \
+ float hygrobias; \
+ /* Hygrometer subsystem factory sensitivity.*/ \
+ float hygrofactorysensitivity; \
+ /* Hygrometer subsystem factory bias .*/ \
+ float hygrofactorybias; \
+ /* Thermometer subsystem axes number.*/ \
+ size_t thermoaxes; \
+ /* Thermometer subsystem current sensitivity.*/ \
+ float thermosensitivity; \
+ /* Thermometer subsystem current bias.*/ \
+ float thermobias; \
+ /* Thermometer subsystem factory sensitivity.*/ \
+ float thermofactorysensitivity; \
+ /* Thermometer subsystem factory bias.*/ \
+ float thermofactorybias;
+
+/**
+ * @brief HTS221 2-axis hygrometer/thermometer class.
*/
-#define _hts221_thermometer_methods \
- _base_thermometer_methods \
- _hts221_thermometer_methods_alone
+struct HTS221Driver {
+ /** @brief Virtual Methods Table.*/
+ const struct HTS221VMT *vmt;
+ /** @brief Base hygrometer interface.*/
+ BaseHygrometer hygro_if;
+ /** @brief Base thermometer interface.*/
+ BaseThermometer thermo_if;
+ _hts221_data
+};
+/** @} */
+
+/*===========================================================================*/
+/* Driver macros. */
+/*===========================================================================*/
/**
- * @extends BaseHygrometerVMT
+ * @brief Return the number of axes of the BaseHygrometer.
+ *
+ * @param[in] devp pointer to @p HTS221Driver.
+ *
+ * @return the number of axes.
*
- * @brief @p HTS221 hygrometer virtual methods table.
+ * @api
*/
-struct HTS221HygrometerVMT {
- _hts221_hygrometer_methods
-};
+#define hts221HygrometerGetAxesNumber(devp) \
+ hygrometerGetAxesNumber(&((devp)->hygro_if))
/**
- * @extends BaseThermometerVMT
+ * @brief Retrieves raw data from the BaseHygrometer.
+ * @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
+ * BaseHygrometer axes number.
+ *
+ * @param[in] devp pointer to @p HTS221Driver.
+ * @param[out] axes a buffer which would be filled with raw data.
*
- * @brief @p HTS221 thermometer virtual methods table.
+ * @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
*/
-struct HTS221ThermometerVMT {
- _hts221_thermometer_methods
-};
+#define hts221HygrometerReadRaw(devp, axes) \
+ hygrometerReadRaw(&((devp)->hygro_if), axes)
/**
- * @brief @p HTS221Driver specific data.
+ * @brief Retrieves cooked data from the BaseHygrometer.
+ * @note This data is manipulated according to the formula
+ * cooked = (raw * sensitivity) - bias.
+ * @note Final data is expressed as %rH.
+ * @note The axes array must be at least the same size of the
+ * BaseHygrometer axes number.
+ *
+ * @param[in] devp pointer to @p HTS221Driver.
+ * @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 _hts221_data \
- /* Driver state.*/ \
- hts221_state_t state; \
- /* Current configuration data.*/ \
- const HTS221Config *config; \
- /* Current sensitivity data.*/ \
- float sensitivity[HTS221_HYGRO_NUMBER_OF_AXES + \
- HTS221_THERMO_NUMBER_OF_AXES]; \
- /* Current Bias data.*/ \
- float bias[HTS221_HYGRO_NUMBER_OF_AXES + \
- HTS221_THERMO_NUMBER_OF_AXES];
+#define hts221HygrometerReadCooked(devp, axes) \
+ hygrometerReadCooked(&((devp)->hygro_if), axes)
/**
- * @extends BaseHygrometer
+ * @brief Set bias values for the BaseHygrometer.
+ * @note Bias must be expressed as %rH.
+ * @note The bias buffer must be at least the same size of the
+ * BaseHygrometer axes number.
+ *
+ * @param[in] devp pointer to @p HTS221Driver.
+ * @param[in] bp a buffer which contains biases.
*
- * @brief HTS221 3-axis barometer class.
- * @details This class extends @p BaseHygrometer by adding physical
- * driver implementation.
+ * @return The operation status.
+ * @retval MSG_OK if the function succeeded.
+ *
+ * @api
*/
-struct HTS221Driver {
- /** @brief BaseSensor Virtual Methods Table. */
- const struct BaseSensorVMT *vmt_sensor;
- _base_sensor_data
- /** @brief HTS221 Hygrometer Virtual Methods Table. */
- const struct HTS221HygrometerVMT *vmt_hygrometer;
- _base_hygrometer_data
- /** @brief HTS221 Thermometer Virtual Methods Table. */
- const struct HTS221ThermometerVMT *vmt_thermometer;
- _base_thermometer_data
- _hts221_data
-};
+#define hts221HygrometerSetBias(devp, bp) \
+ hygrometerSetBias(&((devp)->hygro_if), bp)
/**
- * @brief Structure representing a HTS221 driver.
+ * @brief Reset bias values for the BaseHygrometer.
+ * @note Default biases value are obtained from device datasheet when
+ * available otherwise they are considered zero.
+ *
+ * @param[in] devp pointer to @p HTS221Driver.
+ *
+ * @return The operation status.
+ * @retval MSG_OK if the function succeeded.
+ *
+ * @api
*/
-typedef struct HTS221Driver HTS221Driver;
-/** @} */
+#define hts221HygrometerResetBias(devp) \
+ hygrometerResetBias(&((devp)->hygro_if))
-/*===========================================================================*/
-/* Driver macros. */
-/*===========================================================================*/
+/**
+ * @brief Set sensitivity values for the BaseHygrometer.
+ * @note Sensitivity must be expressed as %rH/LSB.
+ * @note The sensitivity buffer must be at least the same size of the
+ * BaseHygrometer axes number.
+ *
+ * @param[in] devp pointer to @p HTS221Driver.
+ * @param[in] sp a buffer which contains sensitivities.
+ *
+ * @return The operation status.
+ * @retval MSG_OK if the function succeeded.
+ *
+ * @api
+ */
+#define hts221HygrometerSetSensitivity(devp, sp) \
+ hygrometerSetSensitivity(&((devp)->hygro_if), sp)
+
+/**
+ * @brief Reset sensitivity values for the BaseHygrometer.
+ * @note Default sensitivities value are obtained from device datasheet.
+ *
+ * @param[in] devp pointer to @p HTS221Driver.
+ *
+ * @return The operation status.
+ * @retval MSG_OK if the function succeeded.
+ *
+ * @api
+ */
+#define hts221HygrometerResetSensitivity(devp) \
+ hygrometerResetSensitivity(&((devp)->hygro_if))
+
+/**
+ * @brief Return the number of axes of the BaseThermometer.
+ *
+ * @param[in] devp pointer to @p HTS221Driver.
+ *
+ * @return the number of axes.
+ *
+ * @api
+ */
+#define hts221ThermometerGetAxesNumber(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 HTS221Driver.
+ * @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 hts221ThermometerReadRaw(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 HTS221Driver.
+ * @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 hts221ThermometerReadCooked(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 HTS221Driver.
+ * @param[in] bp a buffer which contains biases.
+ *
+ * @return The operation status.
+ * @retval MSG_OK if the function succeeded.
+ *
+ * @api
+ */
+#define hts221ThermometerSetBias(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 HTS221Driver.
+ *
+ * @return The operation status.
+ * @retval MSG_OK if the function succeeded.
+ *
+ * @api
+ */
+#define hts221ThermometerResetBias(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 HTS221Driver.
+ * @param[in] sp a buffer which contains sensitivities.
+ *
+ * @return The operation status.
+ * @retval MSG_OK if the function succeeded.
+ *
+ * @api
+ */
+#define hts221ThermometerSetSensitivity(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 HTS221Driver.
+ *
+ * @return The operation status.
+ * @retval MSG_OK if the function succeeded.
+ *
+ * @api
+ */
+#define hts221ThermometerResetSensitivity(devp) \
+ thermometerResetSensitivity(&((devp)->thermo_if))
/*===========================================================================*/
/* External declarations. */
diff --git a/testex/STM32/STM32F4xx/I2C-HTS221/Makefile b/testex/STM32/STM32F4xx/I2C-HTS221/Makefile
index 7beed88eb..09b7c1d4e 100644
--- a/testex/STM32/STM32F4xx/I2C-HTS221/Makefile
+++ b/testex/STM32/STM32F4xx/I2C-HTS221/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/hts221.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) \
- $(HTS221SRC) \
- $(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) $(HTS221INC) \
- $(STREAMSINC) $(SHELLINC)
+# List ASM source files here
+ASMSRC = $(ALLASMSRC)
+ASMXSRC = $(ALLXASMSRC)
+
+INCDIR = $(ALLINC) $(TESTINC)
#
# Project, sources and paths
@@ -200,8 +196,8 @@ CPPWARN = -Wall -Wextra -Wundef
#
# List all user C define here, like -D_DEBUG=1
-UDEFS = -DCHPRINTF_USE_FLOAT=1 -DSHELL_CMD_TEST_ENABLED=0 \
- -DHTS221_USE_ADVANCED=0 -DLPS25H_SHARED_I2C=0
+UDEFS = -DCHPRINTF_USE_FLOAT=1 \
+ -DHTS221_USE_ADVANCED=0 -DHTS221_SHARED_I2C=0
# Define ASM defines here
UADEFS =
diff --git a/testex/STM32/STM32F4xx/I2C-HTS221/debug/STM32F4xx-I2C-HTS221 (OpenOCD, Flash and Run).launch b/testex/STM32/STM32F4xx/I2C-HTS221/debug/STM32F4xx-I2C-HTS221 (OpenOCD, Flash and Run).launch
index 83de3fdfd..e1ea50989 100644
--- a/testex/STM32/STM32F4xx/I2C-HTS221/debug/STM32F4xx-I2C-HTS221 (OpenOCD, Flash and Run).launch
+++ b/testex/STM32/STM32F4xx/I2C-HTS221/debug/STM32F4xx-I2C-HTS221 (OpenOCD, Flash and Run).launch
@@ -33,7 +33,7 @@
<intAttribute key="org.eclipse.cdt.launch.ATTR_BUILD_BEFORE_LAUNCH_ATTR" value="2"/>
<stringAttribute key="org.eclipse.cdt.launch.COREFILE_PATH" value=""/>
<stringAttribute key="org.eclipse.cdt.launch.DEBUGGER_REGISTER_GROUPS" value=""/>
-<stringAttribute key="org.eclipse.cdt.launch.FORMAT" value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&lt;contentList&gt;&lt;content id=&quot;rxbuf-hts221I2CReadRegister-(format)&quot; val=&quot;2&quot;/&gt;&lt;content id=&quot;reg-hts221I2CReadRegister-(format)&quot; val=&quot;2&quot;/&gt;&lt;content id=&quot;cr-hts221Start-(format)&quot; val=&quot;2&quot;/&gt;&lt;content id=&quot;tmp-read_raw.lto_priv.21-(format)&quot; val=&quot;0&quot;/&gt;&lt;content id=&quot;null-lps25hStart-(format)&quot; val=&quot;4&quot;/&gt;&lt;content id=&quot;null-read_raw-(format)&quot; val=&quot;1&quot;/&gt;&lt;content id=&quot;null-lps25hI2CReadRegister-(format)&quot; val=&quot;4&quot;/&gt;&lt;content id=&quot;rxbuf-lps25hI2CReadRegister-(format)&quot; val=&quot;4&quot;/&gt;&lt;content id=&quot;tmp-hygro_read_raw.lto_priv.26-(format)&quot; val=&quot;2&quot;/&gt;&lt;content id=&quot;H0_rH_x2-hts221Calibrate-(format)&quot; val=&quot;1&quot;/&gt;&lt;/contentList&gt;"/>
+<stringAttribute key="org.eclipse.cdt.launch.FORMAT" value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&lt;contentList&gt;&lt;content id=&quot;H0_rH_x2-hts221Calibrate-(format)&quot; val=&quot;1&quot;/&gt;&lt;content id=&quot;tmp-hygro_read_raw.lto_priv.26-(format)&quot; val=&quot;2&quot;/&gt;&lt;content id=&quot;rxbuf-lps25hI2CReadRegister-(format)&quot; val=&quot;4&quot;/&gt;&lt;content id=&quot;null-lps25hI2CReadRegister-(format)&quot; val=&quot;4&quot;/&gt;&lt;content id=&quot;null-read_raw-(format)&quot; val=&quot;1&quot;/&gt;&lt;content id=&quot;null-lps25hStart-(format)&quot; val=&quot;4&quot;/&gt;&lt;content id=&quot;tmp-read_raw.lto_priv.21-(format)&quot; val=&quot;0&quot;/&gt;&lt;content id=&quot;cr-hts221Start-(format)&quot; val=&quot;2&quot;/&gt;&lt;content id=&quot;reg-hts221I2CReadRegister-(format)&quot; val=&quot;2&quot;/&gt;&lt;content id=&quot;rxbuf-hts221I2CReadRegister-(format)&quot; val=&quot;2&quot;/&gt;&lt;/contentList&gt;"/>
<stringAttribute key="org.eclipse.cdt.launch.GLOBAL_VARIABLES" value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&#13;&#10;&lt;globalVariableList/&gt;&#13;&#10;"/>
<stringAttribute key="org.eclipse.cdt.launch.MEMORY_BLOCKS" value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&#13;&#10;&lt;memoryBlockExpressionList/&gt;&#13;&#10;"/>
<stringAttribute key="org.eclipse.cdt.launch.PROGRAM_NAME" value="./build/ch.elf"/>
diff --git a/testex/STM32/STM32F4xx/I2C-HTS221/main.c b/testex/STM32/STM32F4xx/I2C-HTS221/main.c
index 891073dd8..dc1cb5cd8 100644
--- a/testex/STM32/STM32F4xx/I2C-HTS221/main.c
+++ b/testex/STM32/STM32F4xx/I2C-HTS221/main.c
@@ -17,12 +17,11 @@
#include "ch.h"
#include "hal.h"
-#include "string.h"
-#include "shell.h"
#include "chprintf.h"
-
#include "hts221.h"
+#define cls(chp) chprintf(chp, "\033[2J\033[1;1H")
+
/*===========================================================================*/
/* HTS221 related. */
/*===========================================================================*/
@@ -30,10 +29,11 @@
/* HTS221 Driver: This object represent an HTS221 instance */
static HTS221Driver HTS221D1;
-static int32_t rawdata[HTS221_HYGRO_NUMBER_OF_AXES +
- HTS221_THERMO_NUMBER_OF_AXES];
-static float cookeddata[HTS221_HYGRO_NUMBER_OF_AXES +
- HTS221_THERMO_NUMBER_OF_AXES];
+static int32_t hygroraw;
+static int32_t thermoraw;
+
+static float hygrocooked;
+static float thermocooked;
static const I2CConfig i2ccfg = {
OPMODE_I2C,
@@ -46,6 +46,8 @@ static const HTS221Config hts221cfg = {
&i2ccfg,
NULL,
NULL,
+ NULL,
+ NULL,
HTS221_ODR_7HZ,
#if HTS221_USE_ADVANCED || defined(__DOXYGEN__)
HTS221_BDU_CONTINUOUS,
@@ -55,113 +57,10 @@ static const HTS221Config hts221cfg = {
};
/*===========================================================================*/
-/* 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[]) {
- (void)argv;
- if (argc != 2) {
- chprintf(chp, "Usage: read [hygro|thermo|both] [raw|cooked]\r\n");
- return;
- }
-
- while (chnGetTimeout((BaseChannel *)chp, 150) == Q_TIMEOUT) {
- if (!strcmp (argv[0], "hygro")) {
- if (!strcmp (argv[1], "raw")) {
-#if CHPRINTF_USE_ANSI_CODE
- chprintf(chp, "\033[2J\033[1;1H");
-#endif
- hygrometerReadRaw(&HTS221D1, rawdata);
- chprintf(chp, "HTS221 Hygrometer raw data...\r\n");
- chprintf(chp, "Raw humidity: %d\r\n", *rawdata);
- }
- else if (!strcmp (argv[1], "cooked")) {
-#if CHPRINTF_USE_ANSI_CODE
- chprintf(chp, "\033[2J\033[1;1H");
-#endif
- hygrometerReadCooked(&HTS221D1, cookeddata);
- chprintf(chp, "HTS221 Hygrometer cooked data...\r\n");
- chprintf(chp, "Cooked humidity: %.2f %%\r\n", *cookeddata);
- }
- else {
- chprintf(chp, "Usage: read [hygro|thermo|both] [raw|cooked]\r\n");
- return;
- }
- }
- else if (!strcmp (argv[0], "thermo")) {
- if (!strcmp (argv[1], "raw")) {
-#if CHPRINTF_USE_ANSI_CODE
- chprintf(chp, "\033[2J\033[1;1H");
-#endif
- thermometerReadRaw(&HTS221D1, rawdata);
- chprintf(chp, "HTS221 Thermometer raw data...\r\n");
- chprintf(chp, "Raw temperature: %d\r\n", *rawdata);
- }
- else if (!strcmp (argv[1], "cooked")) {
-#if CHPRINTF_USE_ANSI_CODE
- chprintf(chp, "\033[2J\033[1;1H");
-#endif
- thermometerReadCooked(&HTS221D1, cookeddata);
- chprintf(chp, "HTS221 Thermometer cooked data...\r\n");
- chprintf(chp, "Cooked temperature: %.2f °C\r\n", *cookeddata);
- }
- else {
- chprintf(chp, "Usage: read [hygro|thermo|both] [raw|cooked]\r\n");
- return;
- }
- }
- else if (!strcmp (argv[0], "both")) {
- if (!strcmp (argv[1], "raw")) {
-#if CHPRINTF_USE_ANSI_CODE
- chprintf(chp, "\033[2J\033[1;1H");
-#endif
- sensorReadRaw(&HTS221D1, rawdata);
- chprintf(chp, "HTS221 Hygrometer raw data...\r\n");
- chprintf(chp, "Raw humidity: %d\r\n", rawdata[0]);
- chprintf(chp, "HTS221 Thermometer raw data...\r\n");
- chprintf(chp, "Raw temperature: %d\r\n", rawdata[1]);
- }
- else if (!strcmp (argv[1], "cooked")) {
-#if CHPRINTF_USE_ANSI_CODE
- chprintf(chp, "\033[2J\033[1;1H");
-#endif
- sensorReadCooked(&HTS221D1, cookeddata);
- chprintf(chp, "HTS221 Hygrometer cooked data...\r\n");
- chprintf(chp, "Cooked humidity: %.2f %%\r\n", cookeddata[0]);
- chprintf(chp, "HTS221 Thermometer cooked data...\r\n");
- chprintf(chp, "Cooked temperature: %.2f °C\r\n", cookeddata[1]);
- }
- else {
- chprintf(chp, "Usage: read [hygro|thermo|both] [raw|cooked]\r\n");
- return;
- }
- }
- else {
- chprintf(chp, "Usage: read [hygro|thermo|both] [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.
*/
@@ -171,9 +70,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);
}
}
@@ -193,43 +90,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);
- /*
- * HTS221 Object Initialization
- */
+ /* HTS221 Object Initialization.*/
hts221ObjectInit(&HTS221D1);
- /*
- * Activates the HTS221 driver.
- */
+ /* Activates the HTS221 driver.*/
hts221Start(&HTS221D1, &hts221cfg);
- /*
- * Shell manager initialization.
- */
- shellInit();
+ /* Normal main() thread activity, printing MEMS data on the SD1. */
+ while (true) {
+ hts221HygrometerReadRaw(&HTS221D1, &hygroraw);
+ chprintf(chp, "HTS221D1 Hygrometer raw data...\r\n");
+ chprintf(chp, "Hum: %d\r\n", hygroraw);
+
+ hts221ThermometerReadRaw(&HTS221D1, &thermoraw);
+ chprintf(chp, "HTS221D1 Thermometer raw data...\r\n");
+ chprintf(chp, "Temp: %d\r\n", &thermoraw);
+
+ hts221HygrometerReadCooked(&HTS221D1, &hygrocooked);
+ chprintf(chp, "HTS221D1 Hygrometer cooked data...\r\n");
+ chprintf(chp, "Hum: %.2f\r\n", hygrocooked);
- while(TRUE) {
+ hts221ThermometerReadCooked(&HTS221D1, &thermocooked);
+ chprintf(chp, "HTS221D1 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);
}
hts221Stop(&HTS221D1);
}