From 6fe823352bef37a3dedd6b080de2cbab13d4d33a Mon Sep 17 00:00:00 2001 From: Rocco Marco Guglielmi Date: Sun, 11 Mar 2018 14:47:29 +0000 Subject: Updated LIS302DL driver git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@11702 110e8d01-0319-4d1e-a829-52ad28d1bb01 --- os/ex/ST/lis302dl.c | 400 +++++++++++++++++-------- os/ex/ST/lis302dl.h | 354 +++++++++++++++------- testex/STM32/STM32F4xx/SPI-LIS302DL/Makefile | 26 +- testex/STM32/STM32F4xx/SPI-LIS302DL/main.c | 135 ++------- testex/STM32/STM32F4xx/SPI-LIS302DL/readme.txt | 10 +- 5 files changed, 573 insertions(+), 352 deletions(-) diff --git a/os/ex/ST/lis302dl.c b/os/ex/ST/lis302dl.c index 58eaf2d57..1bcb8851d 100644 --- a/os/ex/ST/lis302dl.c +++ b/os/ex/ST/lis302dl.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. @@ -86,157 +86,291 @@ static void lis302dlSPIWriteRegister(SPIDriver *spip, uint8_t reg, size_t n, } #endif /* LIS302DL_USE_SPI */ -/* - * Interface implementation. +/** + * @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 get_axes_number(void *ip) { - - osalDbgCheck(ip != NULL); - return LIS302DL_NUMBER_OF_AXES; +static size_t acc_get_axes_number(void *ip) { + (void)ip; + + return LIS302DL_ACC_NUMBER_OF_AXES; } -static msg_t read_raw(void *ip, int32_t axes[LIS302DL_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[]) { + LIS302DLDriver* devp; uint8_t i, tmp; + msg_t msg = MSG_OK; osalDbgCheck((ip != NULL) && (axes != NULL)); + + /* Getting parent instance pointer.*/ + devp = objGetInstance(LIS302DLDriver*, (BaseAccelerometer*)ip); - osalDbgAssert((((LIS302DLDriver *)ip)->state == LIS302DL_READY), - "read_raw(), invalid state"); + osalDbgAssert((devp->state == LIS302DL_READY), + "acc_read_raw(), invalid state"); #if LIS302DL_USE_SPI - osalDbgAssert((((LIS302DLDriver *)ip)->config->spip->state == SPI_READY), - "read_raw(), channel not ready"); - #if LIS302DL_SHARED_SPI - spiAcquireBus(((LIS302DLDriver *)ip)->config->spip); - spiStart(((LIS302DLDriver *)ip)->config->spip, - ((LIS302DLDriver *)ip)->config->spicfg); + 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 /* LIS302DL_SHARED_SPI */ - for(i = 0; i < LIS302DL_NUMBER_OF_AXES; i++) { - lis302dlSPIReadRegister(((LIS302DLDriver *)ip)->config->spip, + for(i = 0; i < LIS302DL_ACC_NUMBER_OF_AXES; i++) { + lis302dlSPIReadRegister(devp->config->spip, LIS302DL_AD_OUT_X + (i * 2), 1, &tmp); axes[i] = (int32_t)((int8_t)tmp); } #if LIS302DL_SHARED_SPI - spiReleaseBus(((LIS302DLDriver *)ip)->config->spip); -#endif /* LIS302DL_SHARED_SPI */ - -#endif /* LIS302DL_USE_SPI */ - return MSG_OK; + spiReleaseBus(devp->config->spip); +#endif /* LIS302DL_SHARED_SPI */ +#endif /* LIS302DL_USE_SPI */ + return msg; } -static msg_t read_cooked(void *ip, float 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] 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[]) { + LIS302DLDriver* devp; uint32_t i; - int32_t raw[LIS302DL_NUMBER_OF_AXES]; + int32_t raw[LIS302DL_ACC_NUMBER_OF_AXES]; msg_t msg; osalDbgCheck((ip != NULL) && (axes != NULL)); - osalDbgAssert((((LIS302DLDriver *)ip)->state == LIS302DL_READY), - "read_cooked(), invalid state"); + /* Getting parent instance pointer.*/ + devp = objGetInstance(LIS302DLDriver*, (BaseAccelerometer*)ip); + + osalDbgAssert((devp->state == LIS302DL_READY), + "acc_read_cooked(), invalid state"); - msg = read_raw(ip, raw); - for(i = 0; i < LIS302DL_NUMBER_OF_AXES ; i++){ - axes[i] = (raw[i] * ((LIS302DLDriver *)ip)->sensitivity[i]); - axes[i] -= ((LIS302DLDriver *)ip)->bias[i]; + msg = acc_read_raw(ip, raw); + for(i = 0; i < LIS302DL_ACC_NUMBER_OF_AXES; i++) { + axes[i] = (raw[i] * devp->accsensitivity[i]) - devp->accbias[i]; } return msg; } -static msg_t set_bias(void *ip, float *bp) { +/** + * @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) { + LIS302DLDriver* devp; uint32_t i; - - osalDbgCheck((ip != NULL) && (bp !=NULL)); + msg_t msg = MSG_OK; - osalDbgAssert((((LIS302DLDriver *)ip)->state == LIS302DL_READY) || - (((LIS302DLDriver *)ip)->state == LIS302DL_STOP), - "set_bias(), invalid state"); - - for(i = 0; i < LIS302DL_NUMBER_OF_AXES; i++) { - ((LIS302DLDriver *)ip)->bias[i] = bp[i]; + osalDbgCheck((ip != NULL) && (bp != NULL)); + + /* Getting parent instance pointer.*/ + devp = objGetInstance(LIS302DLDriver*, (BaseAccelerometer*)ip); + + osalDbgAssert((devp->state == LIS302DL_READY), + "acc_set_bias(), invalid state"); + + for(i = 0; i < LIS302DL_ACC_NUMBER_OF_AXES; i++) { + devp->accbias[i] = bp[i]; } - return MSG_OK; + return msg; } -static msg_t reset_bias(void *ip) { +/** + * @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) { + LIS302DLDriver* devp; uint32_t i; + msg_t msg = MSG_OK; osalDbgCheck(ip != NULL); + + /* Getting parent instance pointer.*/ + devp = objGetInstance(LIS302DLDriver*, (BaseAccelerometer*)ip); - osalDbgAssert((((LIS302DLDriver *)ip)->state == LIS302DL_READY) || - (((LIS302DLDriver *)ip)->state == LIS302DL_STOP), - "reset_bias(), invalid state"); + osalDbgAssert((devp->state == LIS302DL_READY), + "acc_reset_bias(), invalid state"); - for(i = 0; i < LIS302DL_NUMBER_OF_AXES; i++) - ((LIS302DLDriver *)ip)->bias[i] = 0; - return MSG_OK; + for(i = 0; i < LIS302DL_ACC_NUMBER_OF_AXES; i++) + devp->accbias[i] = LIS302DL_ACC_BIAS; + return msg; } -static msg_t set_sensivity(void *ip, float *sp) { +/** + * @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) { + LIS302DLDriver* devp; uint32_t i; - - osalDbgCheck((ip != NULL) && (sp !=NULL)); + msg_t msg = MSG_OK; - osalDbgAssert((((LIS302DLDriver *)ip)->state == LIS302DL_READY), - "set_sensivity(), invalid state"); - - for(i = 0; i < LIS302DL_NUMBER_OF_AXES; i++) { - ((LIS302DLDriver *)ip)->sensitivity[i] = sp[i]; + /* Getting parent instance pointer.*/ + devp = objGetInstance(LIS302DLDriver*, (BaseAccelerometer*)ip); + + osalDbgCheck((ip != NULL) && (sp != NULL)); + + osalDbgAssert((devp->state == LIS302DL_READY), + "acc_set_sensivity(), invalid state"); + + for(i = 0; i < LIS302DL_ACC_NUMBER_OF_AXES; i++) { + devp->accsensitivity[i] = sp[i]; } - return MSG_OK; + return msg; } -static msg_t reset_sensivity(void *ip) { +/** + * @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) { + LIS302DLDriver* devp; uint32_t i; + msg_t msg = MSG_OK; osalDbgCheck(ip != NULL); - - osalDbgAssert((((LIS302DLDriver *)ip)->state == LIS302DL_READY), - "reset_sensivity(), invalid state"); - - if(((LIS302DLDriver *)ip)->config->fullscale == LIS302DL_FS_2G) - for(i = 0; i < LIS302DL_NUMBER_OF_AXES; i++) - ((LIS302DLDriver *)ip)->sensitivity[i] = LIS302DL_SENS_2G; - else if(((LIS302DLDriver *)ip)->config->fullscale == LIS302DL_FS_8G) - for(i = 0; i < LIS302DL_NUMBER_OF_AXES; i++) - ((LIS302DLDriver *)ip)->sensitivity[i] = LIS302DL_SENS_8G; + + /* Getting parent instance pointer.*/ + devp = objGetInstance(LIS302DLDriver*, (BaseAccelerometer*)ip); + + osalDbgAssert((devp->state == LIS302DL_READY), + "acc_reset_sensivity(), invalid state"); + + if(devp->config->accfullscale == LIS302DL_ACC_FS_2G) + for(i = 0; i < LIS302DL_ACC_NUMBER_OF_AXES; i++) + devp->accsensitivity[i] = LIS302DL_ACC_SENS_2G; + else if(devp->config->accfullscale == LIS302DL_ACC_FS_8G) + for(i = 0; i < LIS302DL_ACC_NUMBER_OF_AXES; i++) + devp->accsensitivity[i] = LIS302DL_ACC_SENS_8G; else { - osalDbgAssert(FALSE, "reset_sensivity(), full scale issue"); + osalDbgAssert(FALSE, + "acc_reset_sensivity(), accelerometer full scale issue"); return MSG_RESET; } - return MSG_OK; + return msg; } -static msg_t set_full_scale(void *ip, lis302dl_fs_t fs) { +/** + * @brief Changes the LIS302DLDriver 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] ip pointer to @p LIS302DLDriver 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(LIS302DLDriver *devp, + lis302dl_acc_fs_t fs) { float newfs, scale; uint8_t i, cr; + msg_t msg; - if(fs == LIS302DL_FS_2G) { - newfs = LIS302DL_2G; + osalDbgCheck(devp != NULL); + + osalDbgAssert((devp->state == LIS302DL_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 == LIS302DL_ACC_FS_2G) { + newfs = LIS302DL_ACC_2G; } - else if(fs == LIS302DL_FS_8G) { - newfs = LIS302DL_8G; + else if(fs == LIS302DL_ACC_FS_8G) { + newfs = LIS302DL_ACC_8G; } else { - return MSG_RESET; + msg = MSG_RESET; + return msg; } - if(newfs != ((LIS302DLDriver *)ip)->fullscale) { - scale = newfs / ((LIS302DLDriver *)ip)->fullscale; - ((LIS302DLDriver *)ip)->fullscale = newfs; + if(newfs != devp->accfullscale) { + /* Computing scale value.*/ + scale = newfs / devp->accfullscale; + devp->accfullscale = newfs; #if LIS302DL_USE_SPI #if LIS302DL_SHARED_SPI - spiAcquireBus(((LIS302DLDriver *)ip)->config->spip); - spiStart(((LIS302DLDriver *)ip)->config->spip, - ((LIS302DLDriver *)ip)->config->spicfg); + spiAcquireBus(devp->config->spip); + spiStart(devp->config->spip, + devp->config->spicfg); #endif /* LIS302DL_SHARED_SPI */ - lis302dlSPIReadRegister(((LIS302DLDriver *)ip)->config->spip, - LIS302DL_AD_CTRL_REG1, 1, &cr); + + /* Getting data from register.*/ + lis302dlSPIReadRegister(devp->config->spip, LIS302DL_AD_CTRL_REG1, 1, &cr); + #if LIS302DL_SHARED_SPI - spiReleaseBus(((LIS302DLDriver *)ip)->config->spip); + spiReleaseBus(devp->config->spip); #endif /* LIS302DL_SHARED_SPI */ #endif /* LIS302DL_USE_SPI */ @@ -245,34 +379,37 @@ static msg_t set_full_scale(void *ip, lis302dl_fs_t fs) { #if LIS302DL_USE_SPI #if LIS302DL_SHARED_SPI - spiAcquireBus(((LIS302DLDriver *)ip)->config->spip); - spiStart(((LIS302DLDriver *)ip)->config->spip, - ((LIS302DLDriver *)ip)->config->spicfg); + spiAcquireBus(devp->config->spip); + spiStart(devp->config->spip, + devp->config->spicfg); #endif /* LIS302DL_SHARED_SPI */ - lis302dlSPIWriteRegister(((LIS302DLDriver *)ip)->config->spip, - LIS302DL_AD_CTRL_REG1, 1, &cr); + + /* Getting data from register.*/ + lis302dlSPIWriteRegister(devp->config->spip, LIS302DL_AD_CTRL_REG1, 1, &cr); + #if LIS302DL_SHARED_SPI - spiReleaseBus(((LIS302DLDriver *)ip)->config->spip); + spiReleaseBus(devp->config->spip); #endif /* LIS302DL_SHARED_SPI */ #endif /* LIS302DL_USE_SPI */ /* Scaling sensitivity and bias. Re-calibration is suggested anyway. */ - for(i = 0; i < LIS302DL_NUMBER_OF_AXES; i++) { - ((LIS302DLDriver *)ip)->sensitivity[i] *= scale; - ((LIS302DLDriver *)ip)->bias[i] *= scale; + for(i = 0; i < LIS302DL_ACC_NUMBER_OF_AXES; i++) { + devp->accsensitivity[i] *= scale; + devp->accbias[i] *= scale; } } - return MSG_OK; + return msg; } -static const struct BaseSensorVMT vmt_sensor = { - get_axes_number, read_raw, read_cooked +static const struct LIS302DLVMT vmt_device = { + (size_t)0, + acc_set_full_scale }; -static const struct LIS302DLAccelerometerVMT vmt_accelerometer = { - get_axes_number, read_raw, read_cooked, - set_bias, reset_bias, set_sensivity, reset_sensivity, - set_full_scale +static const struct BaseAccelerometerVMT vmt_accelerometer = { + sizeof(struct LIS302DLVMT*), + acc_get_axes_number, acc_read_raw, acc_read_cooked, + acc_set_bias, acc_reset_bias, acc_set_sensivity, acc_reset_sensivity }; /*===========================================================================*/ @@ -287,13 +424,14 @@ static const struct LIS302DLAccelerometerVMT vmt_accelerometer = { * @init */ void lis302dlObjectInit(LIS302DLDriver *devp) { - uint32_t i; - devp->vmt_sensor = &vmt_sensor; - devp->vmt_accelerometer = &vmt_accelerometer; + devp->vmt = &vmt_device; + devp->acc_if.vmt = &vmt_accelerometer; + devp->config = NULL; - for(i = 0; i < LIS302DL_NUMBER_OF_AXES; i++) - devp->bias[i] = 0.0f; - devp->state = LIS302DL_STOP; + + devp->accaxes = LIS302DL_ACC_NUMBER_OF_AXES; + + devp->state = LIS302DL_STOP; } /** @@ -318,15 +456,15 @@ void lis302dlStart(LIS302DLDriver *devp, const LIS302DLConfig *config) { { cr[0] = LIS302DL_CTRL_REG1_XEN | LIS302DL_CTRL_REG1_YEN | LIS302DL_CTRL_REG1_ZEN | LIS302DL_CTRL_REG1_PD | - devp->config->outputdatarate | - devp->config->fullscale; + devp->config->accoutputdatarate | + devp->config->accfullscale; } /* Control register 2 configuration block.*/ { #if LIS302DL_USE_ADVANCED || defined(__DOXYGEN__) if(devp->config->hpmode != LIS302DL_HPM_BYPASSED) - cr[1] = devp->config->highpass; + cr[1] = devp->config->acchighpass; #endif } @@ -345,31 +483,35 @@ void lis302dlStart(LIS302DLDriver *devp, const LIS302DLConfig *config) { #endif /* LIS302DL_USE_SPI */ /* Storing sensitivity information according to full scale value */ - if(devp->config->fullscale == LIS302DL_FS_2G) { - devp->fullscale = LIS302DL_2G; - if(devp->config->sensitivity == NULL) - for(i = 0; i < LIS302DL_NUMBER_OF_AXES; i++) - devp->sensitivity[i] = LIS302DL_SENS_2G; + if(devp->config->accfullscale == LIS302DL_ACC_FS_2G) { + devp->accfullscale = LIS302DL_ACC_2G; + if(devp->config->accsensitivity == NULL) + for(i = 0; i < LIS302DL_ACC_NUMBER_OF_AXES; i++) + devp->accsensitivity[i] = LIS302DL_ACC_SENS_2G; else - for(i = 0; i < LIS302DL_NUMBER_OF_AXES; i++) - devp->sensitivity[i] = devp->config->sensitivity[i]; + for(i = 0; i < LIS302DL_ACC_NUMBER_OF_AXES; i++) + devp->accsensitivity[i] = devp->config->accsensitivity[i]; } - else if(devp->config->fullscale == LIS302DL_FS_8G) { - devp->fullscale = LIS302DL_8G; - if(devp->config->sensitivity == NULL) - for(i = 0; i < LIS302DL_NUMBER_OF_AXES; i++) - devp->sensitivity[i] = LIS302DL_SENS_8G; + else if(devp->config->accfullscale == LIS302DL_ACC_FS_8G) { + devp->accfullscale = LIS302DL_ACC_8G; + if(devp->config->accsensitivity == NULL) + for(i = 0; i < LIS302DL_ACC_NUMBER_OF_AXES; i++) + devp->accsensitivity[i] = LIS302DL_ACC_SENS_8G; else - for(i = 0; i < LIS302DL_NUMBER_OF_AXES; i++) - devp->sensitivity[i] = devp->config->sensitivity[i]; + for(i = 0; i < LIS302DL_ACC_NUMBER_OF_AXES; i++) + devp->accsensitivity[i] = devp->config->accsensitivity[i]; } else { osalDbgAssert(FALSE, "lis302dlStart(), accelerometer full scale issue"); } - if(devp->config->bias != NULL) - for(i = 0; i < LIS302DL_NUMBER_OF_AXES; i++) - devp->bias[i] = devp->config->bias[i]; + /* Storing bias information according to user setting */ + if(devp->config->accbias != NULL) + for(i = 0; i < LIS302DL_ACC_NUMBER_OF_AXES; i++) + devp->accbias[i] = devp->config->accbias[i]; + else + for(i = 0; i < LIS302DL_ACC_NUMBER_OF_AXES; i++) + devp->accbias[i] = LIS302DL_ACC_BIAS; /* This is the Accelerometer transient recovery time */ osalThreadSleepMilliseconds(10); @@ -388,7 +530,8 @@ void lis302dlStop(LIS302DLDriver *devp) { uint8_t cr1; osalDbgCheck(devp != NULL); - osalDbgAssert((devp->state == LIS302DL_STOP) || (devp->state == LIS302DL_READY), + osalDbgAssert((devp->state == LIS302DL_STOP) || + (devp->state == LIS302DL_READY), "lis302dlStop(), invalid state"); if (devp->state == LIS302DL_READY) { @@ -398,6 +541,7 @@ void lis302dlStop(LIS302DLDriver *devp) { spiStart((devp)->config->spip, (devp)->config->spicfg); #endif /* LIS302DL_SHARED_SPI */ + /* Disabling all axes and enabling power down mode.*/ cr1 = 0; lis302dlSPIWriteRegister(devp->config->spip, LIS302DL_AD_CTRL_REG1, 1, &cr1); spiStop((devp)->config->spip); diff --git a/os/ex/ST/lis302dl.h b/os/ex/ST/lis302dl.h index a6e710fc1..be1737d64 100644 --- a/os/ex/ST/lis302dl.h +++ b/os/ex/ST/lis302dl.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. @@ -43,7 +43,7 @@ /** * @brief LIS302DL driver version string. */ -#define EX_LIS302DL_VERSION "1.0.4" +#define EX_LIS302DL_VERSION "1.0.5" /** * @brief LIS302DL driver version major number. @@ -58,21 +58,26 @@ /** * @brief LIS302DL driver version patch number. */ -#define EX_LIS302DL_PATCH 4 +#define EX_LIS302DL_PATCH 5 /** @} */ /** - * @brief LIS302DL characteristics. + * @brief LIS302DL 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 LIS302DL_NUMBER_OF_AXES 3U +#define LIS302DL_ACC_NUMBER_OF_AXES 3U -#define LIS302DL_2G 2.0f -#define LIS302DL_8G 8.0f +#define LIS302DL_ACC_2G 2.0f +#define LIS302DL_ACC_8G 8.0f -#define LIS302DL_SENS_2G 18.0f -#define LIS302DL_SENS_8G 72.0f +#define LIS302DL_ACC_SENS_2G 18.0f +#define LIS302DL_ACC_SENS_8G 72.0f + +#define LIS302DL_ACC_BIAS 0.0f /** @} */ /** @@ -121,45 +126,45 @@ * @name LIS302DL_CTRL_REG1 register bits definitions * @{ */ -#define LIS302DL_CTRL_REG1_MASK 0xFF /**< LIS302DL_CTRL_REG1 mask */ -#define LIS302DL_CTRL_REG1_XEN (1 << 0) /**< X axis enable */ -#define LIS302DL_CTRL_REG1_YEN (1 << 1) /**< Y axis enable */ -#define LIS302DL_CTRL_REG1_ZEN (1 << 2) /**< Z axis enable */ -#define LIS302DL_CTRL_REG1_STM (1 << 3) /**< Self test P-M */ -#define LIS302DL_CTRL_REG1_STP (1 << 4) /**< Self test P-M */ -#define LIS302DL_CTRL_REG1_FS_MASK 0x20 /**< Full scale field mask */ -#define LIS302DL_CTRL_REG1_FS (1 << 5) /**< Full scale */ -#define LIS302DL_CTRL_REG1_PD (1 << 6) /**< Power-down mode enable */ -#define LIS302DL_CTRL_REG1_DR (1 << 7) /**< Output data rate */ +#define LIS302DL_CTRL_REG1_MASK 0xFF +#define LIS302DL_CTRL_REG1_XEN (1 << 0) +#define LIS302DL_CTRL_REG1_YEN (1 << 1) +#define LIS302DL_CTRL_REG1_ZEN (1 << 2) +#define LIS302DL_CTRL_REG1_STM (1 << 3) +#define LIS302DL_CTRL_REG1_STP (1 << 4) +#define LIS302DL_CTRL_REG1_FS_MASK 0x20 +#define LIS302DL_CTRL_REG1_FS (1 << 5) +#define LIS302DL_CTRL_REG1_PD (1 << 6) +#define LIS302DL_CTRL_REG1_DR (1 << 7) /** @} */ /** * @name LIS302DL_CTRL_REG2 register bits definitions * @{ */ -#define LIS302DL_CTRL_REG2_MASK 0xDF /**< LIS302DL_CTRL_REG2 mask */ -#define LIS302DL_CTRL_REG2_HPCF1 (1 << 0) /**< HP filter cutoff bit 0 */ -#define LIS302DL_CTRL_REG2_HPCF2 (1 << 1) /**< HP filter cutoff bit 1 */ -#define LIS302DL_CTRL_REG2_HPFFWU1 (1 << 2) /**< HP filter cutoff bit 2 */ -#define LIS302DL_CTRL_REG2_HPFFWU2 (1 << 3) /**< HP filter cutoff bit 3 */ -#define LIS302DL_CTRL_REG2_FDS (1 << 4) /**< HP filter mode bit 0 */ -#define LIS302DL_CTRL_REG2_BOOT (1 << 6) /**< HP filter mode bit 1 */ -#define LIS302DL_CTRL_REG2_SIM (1 << 7) /**< HP filter mode bit 1 */ +#define LIS302DL_CTRL_REG2_MASK 0xDF +#define LIS302DL_CTRL_REG2_HPCF1 (1 << 0) +#define LIS302DL_CTRL_REG2_HPCF2 (1 << 1) +#define LIS302DL_CTRL_REG2_HPFFWU1 (1 << 2) +#define LIS302DL_CTRL_REG2_HPFFWU2 (1 << 3) +#define LIS302DL_CTRL_REG2_FDS (1 << 4) +#define LIS302DL_CTRL_REG2_BOOT (1 << 6) +#define LIS302DL_CTRL_REG2_SIM (1 << 7) /** @} */ /** * @name LIS302DL_CTRL_REG3 register bits definitions * @{ */ -#define LIS302DL_CTRL_REG3_MASK 0xFF /**< LIS302DL_CTRL_REG3 mask */ -#define LIS302DL_CTRL_REG3_I1CFG0 (1 << 0) /**< Interrupt 1 config bit 0 */ -#define LIS302DL_CTRL_REG3_I1CFG1 (1 << 1) /**< Interrupt 1 config bit 1 */ -#define LIS302DL_CTRL_REG3_I1CFG2 (1 << 2) /**< Interrupt 1 config bit 2 */ -#define LIS302DL_CTRL_REG3_I2CFG0 (1 << 3) /**< Interrupt 2 config bit 0 */ -#define LIS302DL_CTRL_REG3_I2CFG1 (1 << 4) /**< Interrupt 2 config bit 1 */ -#define LIS302DL_CTRL_REG3_I2CFG2 (1 << 5) /**< Interrupt 2 config bit 2 */ -#define LIS302DL_CTRL_REG3_PP_OD (1 << 6) /**< Push-pull open-drain */ -#define LIS302DL_CTRL_REG3_IHL (1 << 7) /**< Int active high low */ +#define LIS302DL_CTRL_REG3_MASK 0xFF +#define LIS302DL_CTRL_REG3_I1CFG0 (1 << 0) +#define LIS302DL_CTRL_REG3_I1CFG1 (1 << 1) +#define LIS302DL_CTRL_REG3_I1CFG2 (1 << 2) +#define LIS302DL_CTRL_REG3_I2CFG0 (1 << 3) +#define LIS302DL_CTRL_REG3_I2CFG1 (1 << 4) +#define LIS302DL_CTRL_REG3_I2CFG2 (1 << 5) +#define LIS302DL_CTRL_REG3_PP_OD (1 << 6) +#define LIS302DL_CTRL_REG3_IHL (1 << 7) /** @} */ /*===========================================================================*/ @@ -179,6 +184,16 @@ #define LIS302DL_USE_SPI TRUE #endif +/** + * @brief LIS302DL 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(LIS302DL_SHARED_SPI) || defined(__DOXYGEN__) +#define LIS302DL_SHARED_SPI FALSE +#endif + /** * @brief LIS302DL I2C interface switch. * @details If set to @p TRUE the support for I2C is included. @@ -189,22 +204,23 @@ #endif /** - * @brief LIS302DL advanced configurations switch. - * @details If set to @p TRUE more configurations are available. - * @note The default is @p FALSE. + * @brief LIS302DL 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(LIS302DL_USE_ADVANCED) || defined(__DOXYGEN__) -#define LIS302DL_USE_ADVANCED FALSE +#if !defined(LIS302DL_SHARED_I2C) || defined(__DOXYGEN__) +#define LIS302DL_SHARED_I2C FALSE #endif /** - * @brief LIS302DL 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. + * @brief LIS302DL accelerometer subsystem advanced configurations + * switch. + * @details If set to @p TRUE more configurations are available. + * @note The default is @p FALSE. */ -#if !defined(LIS302DL_SHARED_SPI) || defined(__DOXYGEN__) -#define LIS302DL_SHARED_SPI FALSE +#if !defined(LIS302DL_ACC_USE_ADVANCED) || defined(__DOXYGEN__) +#define LIS302DL_ACC_USE_ADVANCED FALSE #endif /** @} */ @@ -220,12 +236,23 @@ #error "LIS302DL_USE_SPI requires HAL_USE_SPI" #endif +#if LIS302DL_SHARED_SPI && !SPI_USE_MUTUAL_EXCLUSION +#error "LIS302DL_SHARED_SPI requires SPI_USE_MUTUAL_EXCLUSION" +#endif + #if LIS302DL_USE_I2C && !HAL_USE_I2C #error "LIS302DL_USE_I2C requires HAL_USE_I2C" #endif -#if LIS302DL_SHARED_SPI && !SPI_USE_MUTUAL_EXCLUSION -#error "LIS302DL_SHARED_SPI requires SPI_USE_MUTUAL_EXCLUSION" +#if LIS302DL_SHARED_I2C && !I2C_USE_MUTUAL_EXCLUSION +#error "LIS302DL_SHARED_I2C requires I2C_USE_MUTUAL_EXCLUSION" +#endif + +/** + * @todo Add support for LIS302DL over I2C. + */ +#if LIS302DL_USE_I2C +#error "LIS302DL over I2C still not supported" #endif /*===========================================================================*/ @@ -236,32 +263,37 @@ * @name LIS302DL data structures and types * @{ */ +/** + * @brief Structure representing a LIS302DL driver. + */ +typedef struct LIS302DLDriver LIS302DLDriver; + /** * @brief LIS302DL full scale. */ typedef enum { - LIS302DL_FS_2G = 0x00, /**< Full scale ±2g. */ - LIS302DL_FS_8G = 0x20 /**< Full scale ±8g. */ -}lis302dl_fs_t; + LIS302DL_ACC_FS_2G = 0x00, /**< Full scale ±2g. */ + LIS302DL_ACC_FS_8G = 0x20 /**< Full scale ±8g. */ +}lis302dl_acc_fs_t; /** * @brief LIS302DL output data rate and bandwidth. */ typedef enum { - LIS302DL_ODR_100HZ = 0x00, /**< ODR 100 Hz. */ - LIS302DL_ODR_400HZ = 0x80 /**< ODR 400 Hz. */ -}lis302dl_odr_t; + LIS302DL_ACC_ODR_100HZ = 0x00, /**< ODR 100 Hz. */ + LIS302DL_ACC_ODR_400HZ = 0x80 /**< ODR 400 Hz. */ +}lis302dl_acc_odr_t; /** * @brief LIS302DL high pass filtering. */ typedef enum { - LIS302DL_HP_DISABLED = 0x00, /**< HP bypassed. */ - LIS302DL_HP_0 = 0x10, /**< HP cutoff 2Hz (ODR 100Hz) or 8Hz */ - LIS302DL_HP_1 = 0x11, /**< HP cutoff 1Hz or 4Hz */ - LIS302DL_HP_2 = 0x12, /**< HP cutoff 0.5Hz or 2Hz */ - LIS302DL_HP_3 = 0x13 /**< HP cutoff 0.25Hz or 1Hz */ -}lis302dl_hp_t; + LIS302DL_ACC_HP_DISABLED = 0x00, /**< HP bypassed. */ + LIS302DL_ACC_HP_0 = 0x10, /**< HP cutoff 2Hz (ODR 100Hz) or 8Hz */ + LIS302DL_ACC_HP_1 = 0x11, /**< HP cutoff 1Hz or 4Hz */ + LIS302DL_ACC_HP_2 = 0x12, /**< HP cutoff 0.5Hz or 2Hz */ + LIS302DL_ACC_HP_3 = 0x13 /**< HP cutoff 0.25Hz or 1Hz */ +}lis302dl_acc_hp_t; /** * @brief Driver state machine possible states. @@ -298,52 +330,51 @@ typedef struct { const I2CConfig *i2ccfg; #endif /* LIS302DL_USE_I2C */ /** - * @brief LIS302DL initial sensitivity. + * @brief LIS302DL accelerometer subsystem initial sensitivity. */ - float *sensitivity; + float *accsensitivity; /** - * @brief LIS302DL initial bias. + * @brief LIS302DL accelerometer subsystem initial bias. */ - float *bias; + float *accbias; /** - * @brief LIS302DL full scale value. + * @brief LIS302DL accelerometer subsystem initial full scale. */ - lis302dl_fs_t fullscale; + lis302dl_acc_fs_t accfullscale; /** * @brief LIS302DL output data rate selection. */ - lis302dl_odr_t outputdatarate; + lis302dl_acc_odr_t accoutputdatarate; #if LIS302DL_USE_ADVANCED || defined(__DOXYGEN__) /** * @brief LIS302DL high pass filtering. */ - lis302dl_hp_t highpass; + lis302dl_acc_hp_t acchighpass; #endif } LIS302DLConfig; /** - * @brief @p LIS302DL accelerometer subsystem specific methods. + * @brief @p LIS302DL specific methods. */ -#define _lis302dl_accelerometer_methods_alone \ +#define _lis302dl_methods_alone \ /* Change full scale value of LIS302DL .*/ \ - msg_t (*set_full_scale)(void *instance, lis302dl_fs_t fs); + msg_t (*set_full_scale)(LIS302DLDriver *devp, lis302dl_acc_fs_t fs); /** - * @brief @p LIS302DL accelerometer subsystem specific methods with inherited - * ones. + * @brief @p LIS302DL specific methods with inherited ones. */ -#define _lis302dl_accelerometer_methods \ - _base_accelerometer_methods \ - _lis302dl_accelerometer_methods_alone +#define _lis302dl_methods \ + _base_object_methods \ + _lis302dl_methods_alone /** - * @extends BaseAccelerometerVMT + * @extends BaseObjectVMT * * @brief @p LIS302DL accelerometer virtual methods table. */ -struct LIS302DLAccelerometerVMT { - _lis302dl_accelerometer_methods +struct LIS302DLVMT { + _lis302dl_methods }; /** @@ -354,34 +385,25 @@ struct LIS302DLAccelerometerVMT { lis302dl_state_t state; \ /* Current configuration data.*/ \ const LIS302DLConfig *config; \ + /* Accelerometer subsystem axes number.*/ \ + size_t accaxes; \ /* Current sensitivity.*/ \ - float sensitivity[LIS302DL_NUMBER_OF_AXES]; \ + float accsensitivity[LIS302DL_ACC_NUMBER_OF_AXES]; \ /* Bias data.*/ \ - int32_t bias[LIS302DL_NUMBER_OF_AXES]; \ + int32_t accbias[LIS302DL_ACC_NUMBER_OF_AXES]; \ /* Current full scale value.*/ \ - float fullscale; + float accfullscale; /** - * @extends BaseAccelerometer - * * @brief LIS302DL 3-axis accelerometer class. - * @details This class extends @p BaseAccelerometer by adding physical - * driver implementation. */ struct LIS302DLDriver { - /** @brief BaseSensor Virtual Methods Table. */ - const struct BaseSensorVMT *vmt_sensor; - _base_sensor_data - /** @brief BaseAccelerometer Virtual Methods Table. */ - const struct LIS302DLAccelerometerVMT *vmt_accelerometer; - _base_accelerometer_data + /** @brief Virtual Methods Table.*/ + const struct LIS302DLVMT *vmt; + /** @brief Base accelerometer interface.*/ + BaseAccelerometer acc_if; _lis302dl_data }; - -/** - * @brief Structure representing a LIS302DL driver. - */ -typedef struct LIS302DLDriver LIS302DLDriver; /** @} */ /*===========================================================================*/ @@ -389,19 +411,141 @@ typedef struct LIS302DLDriver LIS302DLDriver; /*===========================================================================*/ /** - * @brief Change accelerometer fullscale value. + * @brief Return the number of axes of the BaseAccelerometer. + * + * @param[in] devp pointer to @p LIS302DLDriver. * - * @param[in] ip pointer to a @p LIS302DLDriver class. - * @param[in] fs the new full scale value. + * @return the number of axes. + * + * @api + */ +#define lis302dlAccelerometerGetAxesNumber(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 LIS302DLDriver. + * @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 errors occurred. + * @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 lis302dlAccelerometerReadRaw(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 LIS302DLDriver. + * @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 lis302dlAccelerometerReadCooked(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 LIS302DLDriver. + * @param[in] bp a buffer which contains biases. + * + * @return The operation status. + * @retval MSG_OK if the function succeeded. + * + * @api + */ +#define lis302dlAccelerometerSetBias(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 LIS302DLDriver. + * + * @return The operation status. + * @retval MSG_OK if the function succeeded. + * + * @api + */ +#define lis302dlAccelerometerResetBias(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 LIS302DLDriver. + * @param[in] sp a buffer which contains sensitivities. + * + * @return The operation status. + * @retval MSG_OK if the function succeeded. + * + * @api + */ +#define lis302dlAccelerometerSetSensitivity(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 LIS302DLDriver. + * + * @return The operation status. + * @retval MSG_OK if the function succeeded. + * @retval MSG_RESET otherwise. + * + * @api + */ +#define lis302dlAccelerometerResetSensitivity(devp) \ + accelerometerResetSensitivity(&((devp)->acc_if)) + +/** + * @brief Changes the LIS302DLDriver 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 LIS302DLDriver. + * @param[in] fs new fullscale value. + * + * @return The operation status. + * @retval MSG_OK if the function succeeded. + * @retval MSG_RESET otherwise. + * * @api */ -#define accelerometerSetFullScale(ip, fs) \ - (ip)->vmt_accelerometer->set_full_scale(ip, fs) - +#define lis302dlAccelerometerSetFullScale(devp, fs) \ + (devp)->vmt->acc_set_full_scale(devp, fs) /*===========================================================================*/ /* External declarations. */ diff --git a/testex/STM32/STM32F4xx/SPI-LIS302DL/Makefile b/testex/STM32/STM32F4xx/SPI-LIS302DL/Makefile index 6a70d7b0e..b7f62bcc9 100644 --- a/testex/STM32/STM32F4xx/SPI-LIS302DL/Makefile +++ b/testex/STM32/STM32F4xx/SPI-LIS302DL/Makefile @@ -71,7 +71,7 @@ endif # Enables the use of FPU (no, softfp, hard). ifeq ($(USE_FPU),) - USE_FPU = hard + USE_FPU = no endif # @@ -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,10 +100,10 @@ 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/lis302dl.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)/STM32F407xG.ld @@ -114,14 +117,20 @@ CSRC = $(STARTUPSRC) \ $(HALSRC) \ $(PLATFORMSRC) \ $(BOARDSRC) \ - $(LIS302DLSRC) \ + $(LIS3DSHSRC) \ $(STREAMSSRC) \ $(SHELLSRC) \ usbcfg.c main.c +# C sources that can be compiled in ARM or THUMB mode depending on the global +# setting. +CSRC = $(ALLCSRC) \ + $(TESTSRC) \ + usbcfg.c 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 +156,7 @@ TCPPSRC = ASMSRC = ASMXSRC = $(STARTUPASM) $(PORTASM) $(OSALASM) -INCDIR = $(CHIBIOS)/os/license \ - $(STARTUPINC) $(KERNINC) $(PORTINC) $(OSALINC) \ - $(HALINC) $(PLATFORMINC) $(BOARDINC) $(LIS302DLINC) \ - $(STREAMSINC) $(SHELLINC) +INCDIR = $(ALLINC) $(TESTINC) # # Project, sources and paths @@ -200,7 +206,7 @@ CPPWARN = -Wall -Wextra -Wundef # # List all user C define here, like -D_DEBUG=1 -UDEFS = -DCHPRINTF_USE_FLOAT=1 -DSHELL_CMD_TEST_ENABLED=0 \ +UDEFS = -DCHPRINTF_USE_FLOAT=1 \ -DLIS302DL_USE_ADVANCED=0 -DLIS302DL_SHARED_SPI=0 # Define ASM defines here diff --git a/testex/STM32/STM32F4xx/SPI-LIS302DL/main.c b/testex/STM32/STM32F4xx/SPI-LIS302DL/main.c index aea1105f7..8b8cb1636 100644 --- a/testex/STM32/STM32F4xx/SPI-LIS302DL/main.c +++ b/testex/STM32/STM32F4xx/SPI-LIS302DL/main.c @@ -18,23 +18,23 @@ #include "hal.h" #include "usbcfg.h" -#include "string.h" -#include "shell.h" #include "chprintf.h" - #include "lis302dl.h" +#define cls(chp) chprintf(chp, "\033[2J\033[1;1H") + /*===========================================================================*/ -/* LIS302DL related. */ +/* LIS302DL related. */ /*===========================================================================*/ /* LIS302DL Driver: This object represent an LIS302DL instance */ static LIS302DLDriver LIS302DLD1; -static int32_t rawdata[LIS302DL_NUMBER_OF_AXES]; -static float cookeddata[LIS302DL_NUMBER_OF_AXES]; +static int32_t accraw[LIS302DL_ACC_NUMBER_OF_AXES]; -static char axisID[LIS302DL_NUMBER_OF_AXES] = {'X', 'Y', 'Z'}; +static float acccooked[LIS302DL_ACC_NUMBER_OF_AXES]; + +static char axisID[LIS302DL_ACC_NUMBER_OF_AXES] = {'X', 'Y', 'Z'}; static uint32_t i; static const SPIConfig spicfg = { @@ -46,100 +46,23 @@ static const SPIConfig spicfg = { 0 }; -static LIS302DLConfig l3gd20cfg = { +static LIS302DLConfig lis302dlcfg = { &SPID1, &spicfg, NULL, NULL, - LIS302DL_FS_2G, - LIS302DL_ODR_100HZ, -#if LIS302DL_USE_ADVANCED || defined(__DOXYGEN__) - LIS302DL_HP_DISABLED, -#endif -}; - -/*===========================================================================*/ -/* 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 != 1) { - chprintf(chp, "Usage: read [raw|cooked]\r\n"); - return; - } - - while (chnGetTimeout((BaseChannel *)chp, 150) == Q_TIMEOUT) { - if (!strcmp (argv[0], "raw")) { -#if CHPRINTF_USE_ANSI_CODE - chprintf(chp, "\033[2J\033[1;1H"); -#endif - accelerometerReadRaw(&LIS302DLD1, rawdata); - chprintf(chp, "LIS302DL Accelerometer raw data...\r\n"); - for(i = 0; i < LIS302DL_NUMBER_OF_AXES; i++) { - chprintf(chp, "%c-axis: %d\r\n", axisID[i], rawdata[i]); - } - } - else if (!strcmp (argv[0], "cooked")) { -#if CHPRINTF_USE_ANSI_CODE - chprintf(chp, "\033[2J\033[1;1H"); -#endif - accelerometerReadCooked(&LIS302DLD1, cookeddata); - chprintf(chp, "LIS302DL Accelerometer cooked data...\r\n"); - for(i = 0; i < LIS302DL_NUMBER_OF_AXES; i++) { - chprintf(chp, "%c-axis: %.4f mg\r\n", axisID[i], cookeddata[i]); - } - } - else { - chprintf(chp, "Usage: read [raw|cooked]\r\n"); - return; - } - } - chprintf(chp, "Stopped\r\n"); -} - -static void cmd_fullscale(BaseSequentialStream *chp, int argc, char *argv[]) { - (void)argv; - if (argc != 1) { - chprintf(chp, "Usage: fullscale [2G|8G]\r\n"); - return; - } -#if CHPRINTF_USE_ANSI_CODE - chprintf(chp, "\033[2J\033[1;1H"); + LIS302DL_ACC_FS_2G, + LIS302DL_ACC_ODR_100HZ, +#if LIS302DL_USE_ADVANCED + LIS302DL_ACC_HP_1, #endif - if(!strcmp (argv[0], "2G")) { - accelerometerSetFullScale(&LIS302DLD1, LIS302DL_FS_2G); - chprintf(chp, "LIS302DL Accelerometer full scale set to 2G...\r\n"); - } - else if(!strcmp (argv[0], "8G")) { - accelerometerSetFullScale(&LIS302DLD1, LIS302DL_FS_8G); - chprintf(chp, "LIS302DL Accelerometer full scale set to 8G...\r\n"); - } - else { - chprintf(chp, "Usage: fullscale [2G|8G]\r\n"); - return; - } -} - -static const ShellCommand commands[] = { - {"read", cmd_read}, - {"fullscale", cmd_fullscale}, - {NULL, NULL} -}; - -static const ShellConfig shell_cfg1 = { - (BaseSequentialStream *)&SDU1, - commands }; /*===========================================================================*/ -/* Main code. */ +/* Generic code. */ /*===========================================================================*/ +static BaseSequentialStream* chp = (BaseSequentialStream*)&SDU1; /* * LED blinker thread, times are in milliseconds. */ @@ -152,9 +75,7 @@ static THD_FUNCTION(Thread1, arg) { systime_t time; time = serusbcfg.usbp->state == USB_ACTIVE ? 250 : 500; - palClearLine(LINE_LED6); - chThdSleepMilliseconds(time); - palSetLine(LINE_LED6); + palToggleLine(LINE_LED6); chThdSleepMilliseconds(time); } } @@ -195,21 +116,23 @@ int main(void) { lis302dlObjectInit(&LIS302DLD1); /* Activates the LIS302DL driver.*/ - lis302dlStart(&LIS302DLD1, &l3gd20cfg); + lis302dlStart(&LIS302DLD1, &lis302dlcfg); - /* Shell manager initialization.*/ - shellInit(); + /* Normal main() thread activity, printing MEMS data on the SDU1.*/ + while (true) { + lis302dlAccelerometerReadRaw(&LIS302DLD1, accraw); + chprintf(chp, "LIS302DL Accelerometer raw data...\r\n"); + for(i = 0; i < LIS302DL_ACC_NUMBER_OF_AXES; i++) { + chprintf(chp, "%c-axis: %d\r\n", axisID[i], accraw[i]); + } - while(TRUE) { - if (SDU1.config->usbp->state == USB_ACTIVE) { - thread_t *shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE, - "shell", NORMALPRIO + 1, - shellThread, (void *)&shell_cfg1); - chThdWait(shelltp); /* Waiting termination. */ + lis302dlAccelerometerReadCooked(&LIS302DLD1, acccooked); + chprintf(chp, "LIS302DL Accelerometer cooked data...\r\n"); + for(i = 0; i < LIS302DL_ACC_NUMBER_OF_AXES; i++) { + chprintf(chp, "%c-axis: %.3f\r\n", axisID[i], acccooked[i]); } - chThdSleepMilliseconds(1000); + chThdSleepMilliseconds(100); + cls(chp); } lis302dlStop(&LIS302DLD1); - return 0; } - diff --git a/testex/STM32/STM32F4xx/SPI-LIS302DL/readme.txt b/testex/STM32/STM32F4xx/SPI-LIS302DL/readme.txt index 11e515e07..3a02b8d69 100644 --- a/testex/STM32/STM32F4xx/SPI-LIS302DL/readme.txt +++ b/testex/STM32/STM32F4xx/SPI-LIS302DL/readme.txt @@ -4,12 +4,16 @@ ** TARGET ** -The demo runs on an STM32F407 Discovery board rev MB997B. +The demo runs on an STM32F407 Discovery board REV B or previous. ** The Demo ** -The demo flashes the board LED using a thread, read data from LIS302DL printing -it on a BaseSequentialStream (SDU1, mapped on USB virtual COM port). +The application demonstrates the use of the STM32F4xx SPI driver in order +to acquire data from LIS302L using ChibiOS/EX. + +** Board Setup ** + +None required. ** Build Procedure ** -- cgit v1.2.3