From 29c0a99e9bcacdaaf8a45093ae5806f7d109d3d8 Mon Sep 17 00:00:00 2001 From: Rocco Marco Guglielmi Date: Fri, 20 Mar 2020 13:53:11 +0000 Subject: [PATCH] EX: Added support for the ADXL355 git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@13463 27425a3e-05d8-49a3-a47f-9c15f0e5edd8 --- doc/ex/src/main.dox | 10 +- os/ex/devices/ADI/adxl355.c | 605 +++++++++++++++++++++++++++++++++ os/ex/devices/ADI/adxl355.h | 633 +++++++++++++++++++++++++++++++++++ os/ex/devices/ADI/adxl355.mk | 10 + os/ex/dox/main.dox | 11 + readme.txt | 4 +- 6 files changed, 1268 insertions(+), 5 deletions(-) create mode 100644 os/ex/devices/ADI/adxl355.c create mode 100644 os/ex/devices/ADI/adxl355.h create mode 100644 os/ex/devices/ADI/adxl355.mk diff --git a/doc/ex/src/main.dox b/doc/ex/src/main.dox index c9964175e..5e39f058e 100644 --- a/doc/ex/src/main.dox +++ b/doc/ex/src/main.dox @@ -29,6 +29,12 @@ * - EX resides on top of HAL which offers, among others, a set of abstract * interfaces like Generic Sensor, Gyroscope, Magnetometer, Accelerometer, * Barometer, Thermometer: EX offers a phisical implementation of them. + * - Currently Analog Devices supported devices are: + * - @b ADXL355: Low Noise, Low Drift, Low Power, 3-Axis Accelerometers; + * - Currently Bosch supported devices are: + * - @b BMP085: Digital pressure sensor; + * - Currently Micron Technology supported devices are: + * - @b M25Q: Serial NOR Flash; * - Currently STMicroelectronics supported devices are: * - @b HTS221: Capacitive digital humidity sensor; * - @b L3GD20: 3-axis digital gyroscope; @@ -38,8 +44,4 @@ * - @b LPS25H: Piezoresistive 260-1260 hPa pressure sensor; * - @b LSM6DS0: 6-axis iNEMO inertial module; * - @b LSM303DLHC: Ultra compact high performance e-compass; - * - Currently Micron Technology supported devices are: - * - @b M25Q: Serial NOR Flash; - * - Currently Bosch supported devices are: - * - @b BMP085: Digital pressure sensor; */ diff --git a/os/ex/devices/ADI/adxl355.c b/os/ex/devices/ADI/adxl355.c new file mode 100644 index 000000000..de9be045e --- /dev/null +++ b/os/ex/devices/ADI/adxl355.c @@ -0,0 +1,605 @@ +/* + ChibiOS - Copyright (C) 2016..2019 Rocco Marco Guglielmi + + This file is part of ChibiOS. + + ChibiOS is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + ChibiOS is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +*/ + +/** + * @file adxl355.c + * @brief ADXL355 MEMS interface module code. + * + * @addtogroup ADXL355 + * @ingroup EX_ADI + * @{ + */ + +#include "hal.h" +#include "adxl355.h" + +/*===========================================================================*/ +/* Driver local definitions. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver exported variables. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver local variables and types. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver local functions. */ +/*===========================================================================*/ + +#if (ADXL355_USE_SPI) || defined(__DOXYGEN__) +/** + * @brief Reads a generic register value using SPI. + * @pre The SPI interface must be initialized and the driver started. + * + * @param[in] spip pointer to the SPI interface + * @param[in] reg starting register address + * @param[in] n number of consecutive registers to read + * @param[in] b pointer to an output buffer. + */ +static void adxl355SPIReadRegister(SPIDriver *spip, uint8_t reg, size_t n, + uint8_t* b) { + uint8_t cmd; + cmd = (reg << 1) | ADXL355_RW; + spiSelect(spip); + spiSend(spip, 1, &cmd); + spiReceive(spip, n, b); + spiUnselect(spip); +} + +/** + * @brief Writes a value into a generic register using SPI. + * @pre The SPI interface must be initialized and the driver started. + * + * @param[in] spip pointer to the SPI interface + * @param[in] reg starting register address + * @param[in] n number of adjacent registers to write + * @param[in] b pointer to a buffer of values. + */ +static void adxl355SPIWriteRegister(SPIDriver *spip, uint8_t reg, size_t n, + uint8_t* b) { + uint8_t cmd; + cmd = (reg << 1); + spiSelect(spip); + spiSend(spip, 1, &cmd); + spiSend(spip, n, b); + spiUnselect(spip); +} +#endif /* ADXL355_USE_SPI */ + +/** + * @brief Return the number of axes of the BaseAccelerometer. + * + * @param[in] ip pointer to @p BaseAccelerometer interface. + * + * @return the number of axes. + */ +static size_t acc_get_axes_number(void *ip) { + (void)ip; + + return ADXL355_ACC_NUMBER_OF_AXES; +} + +/** + * @brief Retrieves raw data from the BaseAccelerometer. + * @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 + * BaseAccelerometer axes number. + * + * @param[in] ip pointer to @p BaseAccelerometer 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 acc_read_raw(void *ip, int32_t axes[]) { + ADXL355Driver* devp; + uint8_t buff [ADXL355_ACC_NUMBER_OF_AXES * 3], i; + int32_t tmp; + msg_t msg = MSG_OK; + + osalDbgCheck((ip != NULL) && (axes != NULL)); + + /* Getting parent instance pointer.*/ + devp = objGetInstance(ADXL355Driver*, (BaseAccelerometer*)ip); + + osalDbgAssert((devp->state == ADXL355_READY), + "acc_read_raw(), invalid state"); + +#if ADXL355_USE_SPI +#if ADXL355_SHARED_SPI + osalDbgAssert((devp->config->spip->state == SPI_READY), + "acc_read_raw(), channel not ready"); + + spiAcquireBus(devp->config->spip); + spiStart(devp->config->spip, + devp->config->spicfg); +#endif /* ADXL355_SHARED_SPI */ + + adxl355SPIReadRegister(devp->config->spip, ADXL355_AD_XDATA3, + ADXL355_ACC_NUMBER_OF_AXES * 3, buff); + +#if ADXL355_SHARED_SPI + spiReleaseBus(devp->config->spip); +#endif /* ADXL355_SHARED_SPI */ +#endif /* ADXL355_USE_SPI */ + + for(i = 0; i < ADXL355_ACC_NUMBER_OF_AXES; i++) { + tmp = (buff[3 * i] << 12) | (buff[3 * i + 1] << 4) | (buff[3 * i + 2] >> 4); + if(tmp & 0x80000) { + tmp |= 0xFFF00000U; + } + axes[i] = tmp; + } + return msg; +} + +/** + * @brief Retrieves cooked data from the BaseAccelerometer. + * @note This data is manipulated according to the formula + * cooked = (raw * sensitivity) - bias. + * @note Final data is expressed as milli-G. + * @note The axes array must be at least the same size of the + * BaseAccelerometer axes number. + * + * @param[in] ip pointer to @p BaseAccelerometer 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 acc_read_cooked(void *ip, float axes[]) { + ADXL355Driver* devp; + uint32_t i; + int32_t raw[ADXL355_ACC_NUMBER_OF_AXES]; + msg_t msg; + + osalDbgCheck((ip != NULL) && (axes != NULL)); + + /* Getting parent instance pointer.*/ + devp = objGetInstance(ADXL355Driver*, (BaseAccelerometer*)ip); + + osalDbgAssert((devp->state == ADXL355_READY), + "acc_read_cooked(), invalid state"); + + msg = acc_read_raw(ip, raw); + for(i = 0; i < ADXL355_ACC_NUMBER_OF_AXES; i++) { + axes[i] = (raw[i] * devp->accsensitivity[i]) - devp->accbias[i]; + } + return msg; +} + +/** + * @brief Set bias values for the BaseAccelerometer. + * @note Bias must be expressed as milli-G. + * @note The bias buffer must be at least the same size of the + * BaseAccelerometer axes number. + * + * @param[in] ip pointer to @p BaseAccelerometer interface. + * @param[in] bp a buffer which contains biases. + * + * @return The operation status. + * @retval MSG_OK if the function succeeded. + */ +static msg_t acc_set_bias(void *ip, float *bp) { + ADXL355Driver* devp; + uint32_t i; + msg_t msg = MSG_OK; + + osalDbgCheck((ip != NULL) && (bp != NULL)); + + /* Getting parent instance pointer.*/ + devp = objGetInstance(ADXL355Driver*, (BaseAccelerometer*)ip); + + osalDbgAssert((devp->state == ADXL355_READY), + "acc_set_bias(), invalid state"); + + for(i = 0; i < ADXL355_ACC_NUMBER_OF_AXES; i++) { + devp->accbias[i] = bp[i]; + } + return msg; +} + +/** + * @brief Reset bias values for the BaseAccelerometer. + * @note Default biases value are obtained from device datasheet when + * available otherwise they are considered zero. + * + * @param[in] ip pointer to @p BaseAccelerometer interface. + * + * @return The operation status. + * @retval MSG_OK if the function succeeded. + */ +static msg_t acc_reset_bias(void *ip) { + ADXL355Driver* devp; + uint32_t i; + msg_t msg = MSG_OK; + + osalDbgCheck(ip != NULL); + + /* Getting parent instance pointer.*/ + devp = objGetInstance(ADXL355Driver*, (BaseAccelerometer*)ip); + + osalDbgAssert((devp->state == ADXL355_READY), + "acc_reset_bias(), invalid state"); + + for(i = 0; i < ADXL355_ACC_NUMBER_OF_AXES; i++) + devp->accbias[i] = ADXL355_ACC_BIAS; + return msg; +} + +/** + * @brief Set sensitivity values for the BaseAccelerometer. + * @note Sensitivity must be expressed as milli-G/LSB. + * @note The sensitivity buffer must be at least the same size of the + * BaseAccelerometer axes number. + * + * @param[in] ip pointer to @p BaseAccelerometer interface. + * @param[in] sp a buffer which contains sensitivities. + * + * @return The operation status. + * @retval MSG_OK if the function succeeded. + */ +static msg_t acc_set_sensivity(void *ip, float *sp) { + ADXL355Driver* devp; + uint32_t i; + msg_t msg = MSG_OK; + + /* Getting parent instance pointer.*/ + devp = objGetInstance(ADXL355Driver*, (BaseAccelerometer*)ip); + + osalDbgCheck((ip != NULL) && (sp != NULL)); + + osalDbgAssert((devp->state == ADXL355_READY), + "acc_set_sensivity(), invalid state"); + + for(i = 0; i < ADXL355_ACC_NUMBER_OF_AXES; i++) { + devp->accsensitivity[i] = sp[i]; + } + return msg; +} + +/** + * @brief Reset sensitivity values for the BaseAccelerometer. + * @note Default sensitivities value are obtained from device datasheet. + * + * @param[in] ip pointer to @p BaseAccelerometer interface. + * + * @return The operation status. + * @retval MSG_OK if the function succeeded. + * @retval MSG_RESET otherwise. + */ +static msg_t acc_reset_sensivity(void *ip) { + ADXL355Driver* devp; + uint32_t i; + msg_t msg = MSG_OK; + + osalDbgCheck(ip != NULL); + + /* Getting parent instance pointer.*/ + devp = objGetInstance(ADXL355Driver*, (BaseAccelerometer*)ip); + + osalDbgAssert((devp->state == ADXL355_READY), + "acc_reset_sensivity(), invalid state"); + + if(devp->config->accfullscale == ADXL355_ACC_FS_2G) + for(i = 0; i < ADXL355_ACC_NUMBER_OF_AXES; i++) + devp->accsensitivity[i] = ADXL355_ACC_SENS_2G; + else if(devp->config->accfullscale == ADXL355_ACC_FS_4G) + for(i = 0; i < ADXL355_ACC_NUMBER_OF_AXES; i++) + devp->accsensitivity[i] = ADXL355_ACC_SENS_4G; + else if(devp->config->accfullscale == ADXL355_ACC_FS_8G) + for(i = 0; i < ADXL355_ACC_NUMBER_OF_AXES; i++) + devp->accsensitivity[i] = ADXL355_ACC_SENS_8G; + else { + osalDbgAssert(FALSE, + "acc_reset_sensivity(), accelerometer full scale issue"); + return MSG_RESET; + } + return msg; +} + +/** + * @brief Changes the ADXL355Driver accelerometer fullscale value. + * @note This function also rescale sensitivities and biases based on + * previous and next fullscale value. + * @note A recalibration is highly suggested after calling this function. + * + * @param[in] devp pointer to @p ADXL355Driver interface. + * @param[in] fs new fullscale value. + * + * @return The operation status. + * @retval MSG_OK if the function succeeded. + * @retval MSG_RESET otherwise. + */ +static msg_t acc_set_full_scale(ADXL355Driver *devp, adxl355_acc_fs_t fs) { + float newfs, scale; + uint8_t i, reg_val; + msg_t msg; + + osalDbgCheck(devp != NULL); + + osalDbgAssert((devp->state == ADXL355_READY), + "acc_set_full_scale(), invalid state"); + osalDbgAssert((devp->config->spip->state == SPI_READY), + "acc_set_full_scale(), channel not ready"); + + /* Computing new fullscale value.*/ + if(fs == ADXL355_ACC_FS_2G) { + newfs = ADXL355_ACC_2G; + } + else if(fs == ADXL355_ACC_FS_4G) { + newfs = ADXL355_ACC_4G; + } + else if(fs == ADXL355_ACC_FS_8G) { + newfs = ADXL355_ACC_8G; + } + else { + msg = MSG_RESET; + return msg; + } + + if(newfs != devp->accfullscale) { + /* Computing scale value.*/ + scale = newfs / devp->accfullscale; + devp->accfullscale = newfs; + +#if ADXL355_USE_SPI +#if ADXL355_SHARED_SPI + spiAcquireBus(devp->config->spip); + spiStart(devp->config->spip, + devp->config->spicfg); +#endif /* ADXL355_SHARED_SPI */ + + /* Getting data from register.*/ + adxl355SPIReadRegister(devp->config->spip, ADXL355_AD_RANGE, 1, ®_val); + +#if ADXL355_SHARED_SPI + spiReleaseBus(devp->config->spip); +#endif /* ADXL355_SHARED_SPI */ +#endif /* ADXL355_USE_SPI */ + + reg_val &= ~(ADXL355_RANGE_RANGE_MASK); + reg_val |= fs; + +#if ADXL355_USE_SPI +#if ADXL355_SHARED_SPI + spiAcquireBus(devp->config->spip); + spiStart(devp->config->spip, + devp->config->spicfg); +#endif /* ADXL355_SHARED_SPI */ + + /* Getting data from register.*/ + adxl355SPIWriteRegister(devp->config->spip, ADXL355_AD_RANGE, 1, ®_val); + +#if ADXL355_SHARED_SPI + spiReleaseBus(devp->config->spip); +#endif /* ADXL355_SHARED_SPI */ +#endif /* ADXL355_USE_SPI */ + + /* Scaling sensitivity and bias. Re-calibration is suggested anyway. */ + for(i = 0; i < ADXL355_ACC_NUMBER_OF_AXES; i++) { + devp->accsensitivity[i] *= scale; + devp->accbias[i] *= scale; + } + } + return msg; +} + +static const struct ADXL355VMT vmt_device = { + (size_t)0, + acc_set_full_scale +}; + +static const struct BaseAccelerometerVMT vmt_accelerometer = { + sizeof(struct ADXL355VMT*), + acc_get_axes_number, acc_read_raw, acc_read_cooked, + acc_set_bias, acc_reset_bias, acc_set_sensivity, acc_reset_sensivity +}; + +/*===========================================================================*/ +/* Driver exported functions. */ +/*===========================================================================*/ + +/** + * @brief Initializes an instance. + * + * @param[out] devp pointer to the @p ADXL355Driver object + * + * @init + */ +void adxl355ObjectInit(ADXL355Driver *devp) { + devp->vmt = &vmt_device; + devp->acc_if.vmt = &vmt_accelerometer; + + devp->config = NULL; + + devp->accaxes = ADXL355_ACC_NUMBER_OF_AXES; + + devp->state = ADXL355_STOP; +} + +/** + * @brief Configures and activates ADXL355 Complex Driver peripheral. + * + * @param[in] devp pointer to the @p ADXL355Driver object + * @param[in] config pointer to the @p ADXL355Config object + * + * @api + */ +void adxl355Start(ADXL355Driver *devp, const ADXL355Config *config) { + uint32_t i; + uint8_t reg_val; + osalDbgCheck((devp != NULL) && (config != NULL)); + + osalDbgAssert((devp->state == ADXL355_STOP) || + (devp->state == ADXL355_READY), + "adxl355Start(), invalid state"); + + devp->config = config; + + /* Range register configuration block.*/ + { + reg_val = ADXL355_RANGE_I2C_HS | devp->config->accfullscale; + +#if ADXL355_USE_SPI +#if ADXL355_SHARED_SPI + spiAcquireBus(devp->config->spip); +#endif /* ADXL355_SHARED_SPI */ + spiStart(devp->config->spip, devp->config->spicfg); + + adxl355SPIWriteRegister(devp->config->spip, ADXL355_AD_RANGE, 1, ®_val); + +#if ADXL355_SHARED_SPI + spiReleaseBus(devp->config->spip); +#endif /* ADXL355_SHARED_SPI */ +#endif /* ADXL355_USE_SPI */ + } + + /* Filter register configuration block.*/ + { + reg_val = devp->config->accoutputdatarate; +#if ADXL355_USE_ADVANCED || defined(__DOXYGEN__) + reg_val |= devp->config->acchighpass; +#endif + +#if ADXL355_USE_SPI +#if ADXL355_SHARED_SPI + spiAcquireBus(devp->config->spip); + spiStart(devp->config->spip, devp->config->spicfg); +#endif /* ADXL355_SHARED_SPI */ + + adxl355SPIWriteRegister(devp->config->spip, ADXL355_AD_FILTER, 1, ®_val); + +#if ADXL355_SHARED_SPI + spiReleaseBus(devp->config->spip); +#endif /* ADXL355_SHARED_SPI */ +#endif /* ADXL355_USE_SPI */ + } + + /* Power control configuration block.*/ + { + reg_val = 0; + +#if ADXL355_USE_SPI +#if ADXL355_SHARED_SPI + spiAcquireBus(devp->config->spip); + spiStart(devp->config->spip, devp->config->spicfg); +#endif /* ADXL355_SHARED_SPI */ + + adxl355SPIWriteRegister(devp->config->spip, ADXL355_AD_POWER_CTL, 1, ®_val); + +#if ADXL355_SHARED_SPI + spiReleaseBus(devp->config->spip); +#endif /* ADXL355_SHARED_SPI */ +#endif /* ADXL355_USE_SPI */ + } + + /* Storing sensitivity information according to user setting */ + if(devp->config->accfullscale == ADXL355_ACC_FS_2G) { + devp->accfullscale = ADXL355_ACC_2G; + if(devp->config->accsensitivity == NULL) + for(i = 0; i < ADXL355_ACC_NUMBER_OF_AXES; i++) + devp->accsensitivity[i] = ADXL355_ACC_SENS_2G; + else + for(i = 0; i < ADXL355_ACC_NUMBER_OF_AXES; i++) + devp->accsensitivity[i] = devp->config->accsensitivity[i]; + } + else if(devp->config->accfullscale == ADXL355_ACC_FS_4G) { + devp->accfullscale = ADXL355_ACC_4G; + if(devp->config->accsensitivity == NULL) + for(i = 0; i < ADXL355_ACC_NUMBER_OF_AXES; i++) + devp->accsensitivity[i] = ADXL355_ACC_SENS_4G; + else + for(i = 0; i < ADXL355_ACC_NUMBER_OF_AXES; i++) + devp->accsensitivity[i] = devp->config->accsensitivity[i]; + } + else if(devp->config->accfullscale == ADXL355_ACC_FS_8G) { + devp->accfullscale = ADXL355_ACC_8G; + if(devp->config->accsensitivity == NULL) + for(i = 0; i < ADXL355_ACC_NUMBER_OF_AXES; i++) + devp->accsensitivity[i] = ADXL355_ACC_SENS_8G; + else + for(i = 0; i < ADXL355_ACC_NUMBER_OF_AXES; i++) + devp->accsensitivity[i] = devp->config->accsensitivity[i]; + } + else { + osalDbgAssert(FALSE, "adxl355Start(), accelerometer full scale issue"); + } + + /* Storing bias information according to user setting */ + if(devp->config->accbias != NULL) + for(i = 0; i < ADXL355_ACC_NUMBER_OF_AXES; i++) + devp->accbias[i] = devp->config->accbias[i]; + else + for(i = 0; i < ADXL355_ACC_NUMBER_OF_AXES; i++) + devp->accbias[i] = ADXL355_ACC_BIAS; + + /* This is the Accelerometer transient recovery time */ + osalThreadSleepMilliseconds(10); + + devp->state = ADXL355_READY; +} + +/** + * @brief Deactivates the ADXL355 Complex Driver peripheral. + * + * @param[in] devp pointer to the @p ADXL355Driver object + * + * @api + */ +void adxl355Stop(ADXL355Driver *devp) { + uint8_t reg_val; + osalDbgCheck(devp != NULL); + + osalDbgAssert((devp->state == ADXL355_STOP) || + (devp->state == ADXL355_READY), + "adxl355Stop(), invalid state"); + + if (devp->state == ADXL355_READY) { +#if (ADXL355_USE_SPI) +#if ADXL355_SHARED_SPI + spiAcquireBus(devp->config->spip); + spiStart(devp->config->spip, + devp->config->spicfg); +#endif /* ADXL355_SHARED_SPI */ + /* Disabling all axes and enabling power down mode.*/ + reg_val = 1; + adxl355SPIWriteRegister(devp->config->spip, ADXL355_AD_POWER_CTL, + 1, ®_val); + + spiStop(devp->config->spip); +#if ADXL355_SHARED_SPI + spiReleaseBus(devp->config->spip); +#endif /* ADXL355_SHARED_SPI */ +#endif /* ADXL355_USE_SPI */ + } + devp->state = ADXL355_STOP; +} +/** @} */ diff --git a/os/ex/devices/ADI/adxl355.h b/os/ex/devices/ADI/adxl355.h new file mode 100644 index 000000000..c6a2056c5 --- /dev/null +++ b/os/ex/devices/ADI/adxl355.h @@ -0,0 +1,633 @@ +/* + ChibiOS - Copyright (C) 2016..2019 Rocco Marco Guglielmi + + This file is part of ChibiOS. + + ChibiOS is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + ChibiOS is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +*/ + +/** + * @file adxl355.h + * @brief ADXL355 MEMS interface module header. + * + * @addtogroup ADXL355 + * @ingroup EX_ADI + * @{ + */ + +#ifndef _ADXL355_H_ +#define _ADXL355_H_ + +#include "ex_accelerometer.h" + +/*===========================================================================*/ +/* Driver constants. */ +/*===========================================================================*/ + +/** + * @name Version identification + * @{ + */ +/** + * @brief ADXL355 driver version string. + */ +#define EX_ADXL355_VERSION "1.0.0" + +/** + * @brief ADXL355 driver version major number. + */ +#define EX_ADXL355_MAJOR 1 + +/** + * @brief ADXL355 driver version minor number. + */ +#define EX_ADXL355_MINOR 0 + +/** + * @brief ADXL355 driver version patch number. + */ +#define EX_ADXL355_PATCH 0 +/** @} */ + +/** + * @brief ADXL355 accelerometer subsystem characteristics. + * @note Sensitivity is expressed as milli-G/LSB whereas + * 1 milli-G = 0.00980665 m/s^2. + * @note Bias is expressed as milli-G. + * + * @{ + */ +#define ADXL355_ACC_NUMBER_OF_AXES 3U + +#define ADXL355_ACC_2G 2.048f +#define ADXL355_ACC_4G 4.096f +#define ADXL355_ACC_8G 8.0192f + +#define ADXL355_ACC_SENS_2G 0.003906f +#define ADXL355_ACC_SENS_4G 0.007813f +#define ADXL355_ACC_SENS_8G 0.015625f + +#define ADXL355_ACC_BIAS 0.0f +/** @} */ + +/** + * @name ADXL355 communication interfaces related bit masks + * @{ + */ +#define ADXL355_DI_MASK 0xFF +#define ADXL355_DI(n) (1 << n) +#define ADXL355_AD_MASK 0xFE +#define ADXL355_RW (1 << 0) +#define ADXL355_AD(n) (1 << (n + 1)) + +/** @} */ + +/** + * @name ADXL355 register addresses + * @{ + */ +#define ADXL355_AD_DEVID_AD 0x00 +#define ADXL355_AD_DEVID_MST 0x01 +#define ADXL355_AD_PARTID 0x02 +#define ADXL355_AD_REVID 0x03 +#define ADXL355_AD_STATUS 0x04 +#define ADXL355_AD_FIFO_ENTRIES 0x05 +#define ADXL355_AD_TEMP2 0x06 +#define ADXL355_AD_TEMP1 0x07 +#define ADXL355_AD_XDATA3 0x08 +#define ADXL355_AD_XDATA2 0x09 +#define ADXL355_AD_XDATA1 0x0A +#define ADXL355_AD_YDATA3 0x0B +#define ADXL355_AD_YDATA2 0x0C +#define ADXL355_AD_YDATA1 0x0D +#define ADXL355_AD_ZDATA3 0x0E +#define ADXL355_AD_ZDATA2 0x0F +#define ADXL355_AD_ZDATA1 0x10 +#define ADXL355_AD_FIFO_DATA 0x11 +#define ADXL355_AD_OFFSET_X_H 0x1E +#define ADXL355_AD_OFFSET_X_L 0x1F +#define ADXL355_AD_OFFSET_Y_H 0x20 +#define ADXL355_AD_OFFSET_Y_L 0x21 +#define ADXL355_AD_OFFSET_Z_H 0x22 +#define ADXL355_AD_OFFSET_Z_L 0x23 +#define ADXL355_AD_ACT_EN 0x24 +#define ADXL355_AD_ACT_THRES_L 0x25 +#define ADXL355_AD_ACT_THRES_H 0x26 +#define ADXL355_AD_ACT_COUNTER 0x27 +#define ADXL355_AD_FILTER 0x28 +#define ADXL355_AD_FIFO_SAMPLES 0x29 +#define ADXL355_AD_INT_MAP 0x2A +#define ADXL355_AD_SYNC 0x2B +#define ADXL355_AD_RANGE 0x2C +#define ADXL355_AD_POWER_CTL 0x2D +#define ADXL355_AD_SELF_TEST 0x2E +#define ADXL355_AD_RESET 0x2F +/** @} */ + +/** + * @name ADXL355_FILTER register bits definitions + * @{ + */ +#define ADXL355_FILTER_MASK 0x7F +#define ADXL355_FILTER_ORD_LPF_0 (1 << 0) +#define ADXL355_FILTER_ORD_LPF_1 (1 << 1) +#define ADXL355_FILTER_ORD_LPF_2 (1 << 2) +#define ADXL355_FILTER_ORD_LPF_3 (1 << 3) +#define ADXL355_FILTER_HPF_CORNER_0 (1 << 4) +#define ADXL355_FILTER_HPF_CORNER_1 (1 << 5) +#define ADXL355_FILTER_HPF_CORNER_2 (1 << 6) +/** @} */ + +/** + * @name ADXL355_FIFO_SAMPLES register bits definitions + * @{ + */ +#define ADXL355_FIFO_SAMPLES_MASK 0x7F +#define ADXL355_FIFO_SAMPLES_BIT_0 (1 << 0) +#define ADXL355_FIFO_SAMPLES_BIT_1 (1 << 1) +#define ADXL355_FIFO_SAMPLES_BIT_2 (1 << 2) +#define ADXL355_FIFO_SAMPLES_BIT_3 (1 << 3) +#define ADXL355_FIFO_SAMPLES_BIT_4 (1 << 4) +#define ADXL355_FIFO_SAMPLES_BIT_5 (1 << 5) +#define ADXL355_FIFO_SAMPLES_BIT_6 (1 << 6) +/** @} */ + +/** + * @name ADXL355_INT_MAP register bits definitions + * @{ + */ +#define ADXL355_INT_MAP_MASK 0xFF +#define ADXL355_INT_MAP_RDY_EN1 (1 << 0) +#define ADXL355_INT_MAP_FULL_EN1 (1 << 1) +#define ADXL355_INT_MAP_OVR_EN1 (1 << 2) +#define ADXL355_INT_MAP_ACT_EN1 (1 << 3) +#define ADXL355_INT_MAP_RDY_EN2 (1 << 4) +#define ADXL355_INT_MAP_FULL_EN2 (1 << 5) +#define ADXL355_INT_MAP_OVR_EN2 (1 << 6) +#define ADXL355_INT_MAP_ACT_EN2 (1 << 7) +/** @} */ + +/** + * @name ADXL355_SYNC register bits definitions + * @{ + */ +#define ADXL355_SYNC_MASK 0x07 +#define ADXL355_SYNC_EXT_SYNC_0 (1 << 0) +#define ADXL355_SYNC_EXT_SYNC_1 (1 << 1) +#define ADXL355_SYNC_EXT_CLK (1 << 2) +/** @} */ + +/** + * @name ADXL355_RANGE register bits definitions + * @{ + */ +#define ADXL355_RANGE_MASK 0xC3 +#define ADXL355_RANGE_RANGE_MASK 0x03 +#define ADXL355_RANGE_RANGE_1 (1 << 1) +#define ADXL355_RANGE_RANGE_0 (1 << 0) +#define ADXL355_RANGE_RANGE_1 (1 << 1) +#define ADXL355_RANGE_INT_POL (1 << 6) +#define ADXL355_RANGE_I2C_HS (1 << 7) +/** @} */ + +/** + * @name ADXL355_POWER_CTL register bits definitions + * @{ + */ +#define ADXL355_POWER_CTL_MASK 0x07 +#define ADXL355_POWER_CTL_STANDBY (1 << 0) +#define ADXL355_POWER_CTL_TEMP_OFF (1 << 1) +#define ADXL355_POWER_CTL_DRDY_OFF (1 << 2) +/** @} */ + +/** + * @name ADXL355_SELT_TEST register bits definitions + * @{ + */ +#define ADXL355_SELF_TEST_MASK 0x03 +#define ADXL355_SELF_TEST_ST1 (1 << 0) +#define ADXL355_SELF_TEST_ST2 (1 << 1) +/** @} */ + +/*===========================================================================*/ +/* Driver pre-compile time settings. */ +/*===========================================================================*/ + +/** + * @name Configuration options + * @{ + */ +/** + * @brief ADXL355 SPI interface switch. + * @details If set to @p TRUE the support for SPI is included. + * @note The default is @p TRUE. + */ +#if !defined(ADXL355_USE_SPI) || defined(__DOXYGEN__) +#define ADXL355_USE_SPI TRUE +#endif + +/** + * @brief ADXL355 shared SPI switch. + * @details If set to @p TRUE the device acquires SPI bus ownership + * on each transaction. + * @note The default is @p FALSE. Requires SPI_USE_MUTUAL_EXCLUSION. + */ +#if !defined(ADXL355_SHARED_SPI) || defined(__DOXYGEN__) +#define ADXL355_SHARED_SPI FALSE +#endif + +/** + * @brief ADXL355 I2C interface switch. + * @details If set to @p TRUE the support for I2C is included. + * @note The default is @p FALSE. + */ +#if !defined(ADXL355_USE_I2C) || defined(__DOXYGEN__) +#define ADXL355_USE_I2C FALSE +#endif + +/** + * @brief ADXL355 shared I2C switch. + * @details If set to @p TRUE the device acquires I2C bus ownership + * on each transaction. + * @note The default is @p FALSE. Requires I2C_USE_MUTUAL_EXCLUSION. + */ +#if !defined(ADXL355_SHARED_I2C) || defined(__DOXYGEN__) +#define ADXL355_SHARED_I2C FALSE +#endif + +/** + * @brief ADXL355 advanced configurations switch. + * @details If set to @p TRUE more configurations are available. + * @note The default is @p FALSE. + */ +#if !defined(ADXL355_USE_ADVANCED) || defined(__DOXYGEN__) +#define ADXL355_USE_ADVANCED FALSE +#endif +/** @} */ + +/*===========================================================================*/ +/* Derived constants and error checks. */ +/*===========================================================================*/ + +#if !(ADXL355_USE_SPI ^ ADXL355_USE_I2C) +#error "ADXL355_USE_SPI and ADXL355_USE_I2C cannot be both true or both false" +#endif + +#if ADXL355_USE_SPI && !HAL_USE_SPI +#error "ADXL355_USE_SPI requires HAL_USE_SPI" +#endif + +#if ADXL355_SHARED_SPI && !SPI_USE_MUTUAL_EXCLUSION +#error "ADXL355_SHARED_SPI requires SPI_USE_MUTUAL_EXCLUSION" +#endif + +#if ADXL355_USE_I2C && !HAL_USE_I2C +#error "ADXL355_USE_I2C requires HAL_USE_I2C" +#endif + +#if ADXL355_SHARED_I2C && !I2C_USE_MUTUAL_EXCLUSION +#error "ADXL355_SHARED_I2C requires I2C_USE_MUTUAL_EXCLUSION" +#endif + +/* + * CHTODO: Add support for ADXL355 over I2C. + */ +#if ADXL355_USE_I2C +#error "ADXL355 over I2C still not supported" +#endif + +/*===========================================================================*/ +/* Driver data structures and types. */ +/*===========================================================================*/ + +/** + * @name ADXL355 data structures and types + * @{ + */ +/** + * @brief Structure representing a ADXL355 driver. + */ +typedef struct ADXL355Driver ADXL355Driver; + +/** + * @brief ADXL355 full scale. + */ +typedef enum { + ADXL355_ACC_FS_2G = 0x01, /**< Full scale ±2g. */ + ADXL355_ACC_FS_4G = 0x02, /**< Full scale ±4g. */ + ADXL355_ACC_FS_8G = 0x03, /**< Full scale ±8g. */ +}adxl355_acc_fs_t; + +/** + * @brief ADXL355 output data rate. + */ +typedef enum { + ADXL355_ACC_ODR_4000HZ = 0x00, /**< ODR 4000 Hz, LP cut-off ODR/4. */ + ADXL355_ACC_ODR_2000HZ = 0x01, /**< ODR 2000 Hz, LP cut-off ODR/4. */ + ADXL355_ACC_ODR_1000HZ = 0x02, /**< ODR 1000 Hz, LP cut-off ODR/4. */ + ADXL355_ACC_ODR_500HZ = 0x03, /**< ODR 500 Hz, LP cut-off ODR/4. */ + ADXL355_ACC_ODR_250HZ = 0x04, /**< ODR 250 Hz, LP cut-off ODR/4. */ + ADXL355_ACC_ODR_125HZ = 0x06, /**< ODR 125 Hz, LP cut-off ODR/4. */ + ADXL355_ACC_ODR_62P5HZ = 0x06, /**< ODR 62.5 Hz, LP cut-off ODR/4. */ + ADXL355_ACC_ODR_31P25HZ = 0x07, /**< ODR 31.25 Hz, LP cut-off ODR/4. */ + ADXL355_ACC_ODR_15P625HZ = 0x08, /**< ODR 15.625 Hz, LP cut-off ODR/4. */ + ADXL355_ACC_ODR_7P913HZ = 0x09, /**< ODR 7.913 Hz, LP cut-off ODR/4. */ + ADXL355_ACC_ODR_3P906HZ = 0x0A /**< ODR 3.906 Hz, LP cut-off ODR/4. */ +}adxl355_acc_odr_t; + +/** + * @brief ADXL355 HP filter. + */ +typedef enum { + ADXL355_ACC_HP_DISABLED = 0x00, /**< HP disabled. */ + ADXL355_ACC_HP_LEV_1 = 0x10, /**< HP cut-off 247 x 10^-3 x ODR. */ + ADXL355_ACC_HP_LEV_2 = 0x20, /**< HP cut-off 62.084 x 10^-3 x ODR. */ + ADXL355_ACC_HP_LEV_3 = 0x30, /**< HP cut-off 15.545 x 10^-3 x ODR. */ + ADXL355_ACC_HP_LEV_4 = 0x40, /**< HP cut-off 3.892 x 10^-3 x ODR. */ + ADXL355_ACC_HP_LEV_5 = 0x50, /**< HP cut-off 0.954 x 10^-3 x ODR. */ + ADXL355_ACC_HP_LEV_6 = 0x60, /**< HP cut-off 0.238 x 10^-3 x ODR. */ +}adxl355_acc_hp_t; + +/** + * @brief Driver state machine possible states. + */ +typedef enum { + ADXL355_UNINIT = 0, /**< Not initialized. */ + ADXL355_STOP = 1, /**< Stopped. */ + ADXL355_READY = 2, /**< Ready. */ +} adxl355_state_t; + +/** + * @brief ADXL355 configuration structure. + */ +typedef struct { + +#if (ADXL355_USE_SPI) || defined(__DOXYGEN__) + /** + * @brief SPI driver associated to this ADXL355. + */ + SPIDriver *spip; + /** + * @brief SPI configuration associated to this ADXL355. + */ + const SPIConfig *spicfg; +#endif /* ADXL355_USE_SPI */ +#if (ADXL355_USE_I2C) || defined(__DOXYGEN__) + /** + * @brief I2C driver associated to this ADXL355. + */ + I2CDriver *i2cp; + /** + * @brief I2C configuration associated to this ADXL355. + */ + const I2CConfig *i2ccfg; +#endif /* ADXL355_USE_I2C */ + /** + * @brief ADXL355 accelerometer subsystem initial sensitivity. + */ + float *accsensitivity; + /** + * @brief ADXL355 accelerometer subsystem initial bias. + */ + float *accbias; + /** + * @brief ADXL355 accelerometer subsystem initial full scale. + */ + adxl355_acc_fs_t accfullscale; + /** + * @brief ADXL355 output data rate selection. + */ + adxl355_acc_odr_t accoutputdatarate; +#if ADXL355_USE_ADVANCED || defined(__DOXYGEN__) + /** + * @brief ADXL355 HP filter bandwidth. + */ + adxl355_acc_hp_t acchighpass; +#endif +} ADXL355Config; + +/** + * @brief @p ADXL355 specific methods. + */ +#define _adxl355_methods_alone \ + /* Change full scale value of ADXL355 accelerometer subsystem.*/ \ + msg_t (*acc_set_full_scale)(ADXL355Driver *devp, adxl355_acc_fs_t fs); + + +/** + * @brief @p ADXL355 specific methods with inherited ones. + */ +#define _adxl355_methods \ + _base_object_methods \ + _adxl355_methods_alone + +/** + * @extends BaseObjectVMT + * + * @brief @p ADXL355 virtual methods table. + */ +struct ADXL355VMT { + _adxl355_methods +}; + +/** + * @brief @p ADXL355Driver specific data. + */ +#define _adxl355_data \ + _base_sensor_data \ + /* Driver state.*/ \ + adxl355_state_t state; \ + /* Current configuration data.*/ \ + const ADXL355Config *config; \ + /* Accelerometer subsystem axes number.*/ \ + size_t accaxes; \ + /* Accelerometer subsystem current sensitivity.*/ \ + float accsensitivity[ADXL355_ACC_NUMBER_OF_AXES]; \ + /* Accelerometer subsystem current bias .*/ \ + float accbias[ADXL355_ACC_NUMBER_OF_AXES]; \ + /* Accelerometer subsystem current full scale value.*/ \ + float accfullscale; + +/** + * @brief ADXL355 3-axis accelerometer class. + */ +struct ADXL355Driver { + /** @brief Virtual Methods Table.*/ + const struct ADXL355VMT *vmt; + /** @brief Base accelerometer interface.*/ + BaseAccelerometer acc_if; + _adxl355_data +}; +/** @} */ + +/*===========================================================================*/ +/* Driver macros. */ +/*===========================================================================*/ + +/** + * @brief Return the number of axes of the BaseAccelerometer. + * + * @param[in] devp pointer to @p ADXL355Driver. + * + * @return the number of axes. + * + * @api + */ +#define adxl355AccelerometerGetAxesNumber(devp) \ + accelerometerGetAxesNumber(&((devp)->acc_if)) + +/** + * @brief Retrieves raw data from the BaseAccelerometer. + * @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 + * BaseAccelerometer axes number. + * + * @param[in] devp pointer to @p ADXL355Driver. + * @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 adxl355AccelerometerReadRaw(devp, axes) \ + accelerometerReadRaw(&((devp)->acc_if), axes) + +/** + * @brief Retrieves cooked data from the BaseAccelerometer. + * @note This data is manipulated according to the formula + * cooked = (raw * sensitivity) - bias. + * @note Final data is expressed as milli-G. + * @note The axes array must be at least the same size of the + * BaseAccelerometer axes number. + * + * @param[in] devp pointer to @p ADXL355Driver. + * @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 adxl355AccelerometerReadCooked(devp, axes) \ + accelerometerReadCooked(&((devp)->acc_if), axes) + +/** + * @brief Set bias values for the BaseAccelerometer. + * @note Bias must be expressed as milli-G. + * @note The bias buffer must be at least the same size of the + * BaseAccelerometer axes number. + * + * @param[in] devp pointer to @p ADXL355Driver. + * @param[in] bp a buffer which contains biases. + * + * @return The operation status. + * @retval MSG_OK if the function succeeded. + * + * @api + */ +#define adxl355AccelerometerSetBias(devp, bp) \ + accelerometerSetBias(&((devp)->acc_if), bp) + +/** + * @brief Reset bias values for the BaseAccelerometer. + * @note Default biases value are obtained from device datasheet when + * available otherwise they are considered zero. + * + * @param[in] devp pointer to @p ADXL355Driver. + * + * @return The operation status. + * @retval MSG_OK if the function succeeded. + * + * @api + */ +#define adxl355AccelerometerResetBias(devp) \ + accelerometerResetBias(&((devp)->acc_if)) + +/** + * @brief Set sensitivity values for the BaseAccelerometer. + * @note Sensitivity must be expressed as milli-G/LSB. + * @note The sensitivity buffer must be at least the same size of the + * BaseAccelerometer axes number. + * + * @param[in] devp pointer to @p ADXL355Driver. + * @param[in] sp a buffer which contains sensitivities. + * + * @return The operation status. + * @retval MSG_OK if the function succeeded. + * + * @api + */ +#define adxl355AccelerometerSetSensitivity(devp, sp) \ + accelerometerSetSensitivity(&((devp)->acc_if), sp) + +/** + * @brief Reset sensitivity values for the BaseAccelerometer. + * @note Default sensitivities value are obtained from device datasheet. + * + * @param[in] devp pointer to @p ADXL355Driver. + * + * @return The operation status. + * @retval MSG_OK if the function succeeded. + * @retval MSG_RESET otherwise. + * + * @api + */ +#define adxl355AccelerometerResetSensitivity(devp) \ + accelerometerResetSensitivity(&((devp)->acc_if)) + +/** + * @brief Changes the ADXL355Driver accelerometer fullscale value. + * @note This function also rescale sensitivities and biases based on + * previous and next fullscale value. + * @note A recalibration is highly suggested after calling this function. + * + * @param[in] devp pointer to @p ADXL355Driver. + * @param[in] fs new fullscale value. + * + * @return The operation status. + * @retval MSG_OK if the function succeeded. + * @retval MSG_RESET otherwise. + * + * @api + */ +#define adxl355AccelerometerSetFullScale(devp, fs) \ + (devp)->vmt->acc_set_full_scale(devp, fs) + +/*===========================================================================*/ +/* External declarations. */ +/*===========================================================================*/ + +#ifdef __cplusplus +extern "C" { +#endif + void adxl355ObjectInit(ADXL355Driver *devp); + void adxl355Start(ADXL355Driver *devp, const ADXL355Config *config); + void adxl355Stop(ADXL355Driver *devp); +#ifdef __cplusplus +} +#endif + +#endif /* _ADXL355_H_ */ + +/** @} */ + diff --git a/os/ex/devices/ADI/adxl355.mk b/os/ex/devices/ADI/adxl355.mk new file mode 100644 index 000000000..6dbefd593 --- /dev/null +++ b/os/ex/devices/ADI/adxl355.mk @@ -0,0 +1,10 @@ +# List of all the ADX355 device files. +ADXL355SRC := $(CHIBIOS)/os/ex/devices/ADI/adxl355.c + +# Required include directories +ADXL355INC := $(CHIBIOS)/os/ex/include \ + $(CHIBIOS)/os/ex/devices/ADI + +# Shared variables +ALLCSRC += $(ADXL355SRC) +ALLINC += $(ADXL355INC) \ No newline at end of file diff --git a/os/ex/dox/main.dox b/os/ex/dox/main.dox index a02193db0..e63220267 100644 --- a/os/ex/dox/main.dox +++ b/os/ex/dox/main.dox @@ -31,6 +31,11 @@ * - STMicroelectronics Devices * . * + * @section adi_devices Analog Devices Devices + * This section contains all the drivers of devices produced by ADI. + * Devices currently supported are MEMS and are: + * - @b ADXL355: Low Noise, Low Drift, Low Power, 3-Axis Accelerometers; + * * @section bosch_devices Bosch Devices * This section contains all the drivers of devices produced by Bosch. * Devices currently supported are MEMS and are: @@ -76,6 +81,12 @@ * * @ingroup EX */ +/** + * @defgroup EX_ADI Analog Devices Devices + * @brief Analog Devices Devices. + * + * @ingroup EX_DEVICES + */ /** * @defgroup EX_BOSCH Bosch Devices diff --git a/readme.txt b/readme.txt index b55e5f664..073044be1 100644 --- a/readme.txt +++ b/readme.txt @@ -74,7 +74,9 @@ ***************************************************************************** *** Next *** -- NEW: Added latency measurement test application: +- EX: Added support for ADXL355 Low Noise, Low Drift, Low Power, 3-Axis + MEMS Accelerometers. +- NEW: Added latency measurement test application. - FIX: Fixed error in EXTIv1 ISRs (bug #1077) (backported to 20.3.1) - FIX: Fixed problem in chMtxUnlockAllS() (bug #1076)