Updated LIS302DL driver

git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@11702 110e8d01-0319-4d1e-a829-52ad28d1bb01
This commit is contained in:
Rocco Marco Guglielmi 2018-03-11 14:47:29 +00:00
parent 77bc8a0909
commit 6fe823352b
5 changed files with 572 additions and 351 deletions

View File

@ -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. 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 */ #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) { static size_t acc_get_axes_number(void *ip) {
(void)ip;
osalDbgCheck(ip != NULL);
return LIS302DL_NUMBER_OF_AXES; 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; uint8_t i, tmp;
msg_t msg = MSG_OK;
osalDbgCheck((ip != NULL) && (axes != NULL)); osalDbgCheck((ip != NULL) && (axes != NULL));
/* Getting parent instance pointer.*/
devp = objGetInstance(LIS302DLDriver*, (BaseAccelerometer*)ip);
osalDbgAssert((((LIS302DLDriver *)ip)->state == LIS302DL_READY), osalDbgAssert((devp->state == LIS302DL_READY),
"read_raw(), invalid state"); "acc_read_raw(), invalid state");
#if LIS302DL_USE_SPI #if LIS302DL_USE_SPI
osalDbgAssert((((LIS302DLDriver *)ip)->config->spip->state == SPI_READY),
"read_raw(), channel not ready");
#if LIS302DL_SHARED_SPI #if LIS302DL_SHARED_SPI
spiAcquireBus(((LIS302DLDriver *)ip)->config->spip); osalDbgAssert((devp->config->spip->state == SPI_READY),
spiStart(((LIS302DLDriver *)ip)->config->spip, "acc_read_raw(), channel not ready");
((LIS302DLDriver *)ip)->config->spicfg);
spiAcquireBus(devp->config->spip);
spiStart(devp->config->spip,
devp->config->spicfg);
#endif /* LIS302DL_SHARED_SPI */ #endif /* LIS302DL_SHARED_SPI */
for(i = 0; i < LIS302DL_NUMBER_OF_AXES; i++) { for(i = 0; i < LIS302DL_ACC_NUMBER_OF_AXES; i++) {
lis302dlSPIReadRegister(((LIS302DLDriver *)ip)->config->spip, lis302dlSPIReadRegister(devp->config->spip,
LIS302DL_AD_OUT_X + (i * 2), 1, &tmp); LIS302DL_AD_OUT_X + (i * 2), 1, &tmp);
axes[i] = (int32_t)((int8_t)tmp); axes[i] = (int32_t)((int8_t)tmp);
} }
#if LIS302DL_SHARED_SPI #if LIS302DL_SHARED_SPI
spiReleaseBus(((LIS302DLDriver *)ip)->config->spip); spiReleaseBus(devp->config->spip);
#endif /* LIS302DL_SHARED_SPI */ #endif /* LIS302DL_SHARED_SPI */
#endif /* LIS302DL_USE_SPI */
#endif /* LIS302DL_USE_SPI */ return msg;
return MSG_OK;
} }
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; uint32_t i;
int32_t raw[LIS302DL_NUMBER_OF_AXES]; int32_t raw[LIS302DL_ACC_NUMBER_OF_AXES];
msg_t msg; msg_t msg;
osalDbgCheck((ip != NULL) && (axes != NULL)); osalDbgCheck((ip != NULL) && (axes != NULL));
osalDbgAssert((((LIS302DLDriver *)ip)->state == LIS302DL_READY), /* Getting parent instance pointer.*/
"read_cooked(), invalid state"); devp = objGetInstance(LIS302DLDriver*, (BaseAccelerometer*)ip);
msg = read_raw(ip, raw); osalDbgAssert((devp->state == LIS302DL_READY),
for(i = 0; i < LIS302DL_NUMBER_OF_AXES ; i++){ "acc_read_cooked(), invalid state");
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; 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; uint32_t i;
msg_t msg = MSG_OK;
osalDbgCheck((ip != NULL) && (bp !=NULL));
osalDbgAssert((((LIS302DLDriver *)ip)->state == LIS302DL_READY) || osalDbgCheck((ip != NULL) && (bp != NULL));
(((LIS302DLDriver *)ip)->state == LIS302DL_STOP),
"set_bias(), invalid state"); /* Getting parent instance pointer.*/
devp = objGetInstance(LIS302DLDriver*, (BaseAccelerometer*)ip);
for(i = 0; i < LIS302DL_NUMBER_OF_AXES; i++) {
((LIS302DLDriver *)ip)->bias[i] = bp[i]; 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; uint32_t i;
msg_t msg = MSG_OK;
osalDbgCheck(ip != NULL); osalDbgCheck(ip != NULL);
/* Getting parent instance pointer.*/
devp = objGetInstance(LIS302DLDriver*, (BaseAccelerometer*)ip);
osalDbgAssert((((LIS302DLDriver *)ip)->state == LIS302DL_READY) || osalDbgAssert((devp->state == LIS302DL_READY),
(((LIS302DLDriver *)ip)->state == LIS302DL_STOP), "acc_reset_bias(), invalid state");
"reset_bias(), invalid state");
for(i = 0; i < LIS302DL_NUMBER_OF_AXES; i++) for(i = 0; i < LIS302DL_ACC_NUMBER_OF_AXES; i++)
((LIS302DLDriver *)ip)->bias[i] = 0; devp->accbias[i] = LIS302DL_ACC_BIAS;
return MSG_OK; 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; uint32_t i;
msg_t msg = MSG_OK;
osalDbgCheck((ip != NULL) && (sp !=NULL));
osalDbgAssert((((LIS302DLDriver *)ip)->state == LIS302DL_READY), /* Getting parent instance pointer.*/
"set_sensivity(), invalid state"); devp = objGetInstance(LIS302DLDriver*, (BaseAccelerometer*)ip);
for(i = 0; i < LIS302DL_NUMBER_OF_AXES; i++) { osalDbgCheck((ip != NULL) && (sp != NULL));
((LIS302DLDriver *)ip)->sensitivity[i] = sp[i];
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; uint32_t i;
msg_t msg = MSG_OK;
osalDbgCheck(ip != NULL); osalDbgCheck(ip != NULL);
/* Getting parent instance pointer.*/
devp = objGetInstance(LIS302DLDriver*, (BaseAccelerometer*)ip);
osalDbgAssert((((LIS302DLDriver *)ip)->state == LIS302DL_READY), osalDbgAssert((devp->state == LIS302DL_READY),
"reset_sensivity(), invalid state"); "acc_reset_sensivity(), invalid state");
if(((LIS302DLDriver *)ip)->config->fullscale == LIS302DL_FS_2G) if(devp->config->accfullscale == LIS302DL_ACC_FS_2G)
for(i = 0; i < LIS302DL_NUMBER_OF_AXES; i++) for(i = 0; i < LIS302DL_ACC_NUMBER_OF_AXES; i++)
((LIS302DLDriver *)ip)->sensitivity[i] = LIS302DL_SENS_2G; devp->accsensitivity[i] = LIS302DL_ACC_SENS_2G;
else if(((LIS302DLDriver *)ip)->config->fullscale == LIS302DL_FS_8G) else if(devp->config->accfullscale == LIS302DL_ACC_FS_8G)
for(i = 0; i < LIS302DL_NUMBER_OF_AXES; i++) for(i = 0; i < LIS302DL_ACC_NUMBER_OF_AXES; i++)
((LIS302DLDriver *)ip)->sensitivity[i] = LIS302DL_SENS_8G; devp->accsensitivity[i] = LIS302DL_ACC_SENS_8G;
else { else {
osalDbgAssert(FALSE, "reset_sensivity(), full scale issue"); osalDbgAssert(FALSE,
"acc_reset_sensivity(), accelerometer full scale issue");
return MSG_RESET; 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; float newfs, scale;
uint8_t i, cr; uint8_t i, cr;
msg_t msg;
if(fs == LIS302DL_FS_2G) { osalDbgCheck(devp != NULL);
newfs = LIS302DL_2G;
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) { else if(fs == LIS302DL_ACC_FS_8G) {
newfs = LIS302DL_8G; newfs = LIS302DL_ACC_8G;
} }
else { else {
return MSG_RESET; msg = MSG_RESET;
return msg;
} }
if(newfs != ((LIS302DLDriver *)ip)->fullscale) { if(newfs != devp->accfullscale) {
scale = newfs / ((LIS302DLDriver *)ip)->fullscale; /* Computing scale value.*/
((LIS302DLDriver *)ip)->fullscale = newfs; scale = newfs / devp->accfullscale;
devp->accfullscale = newfs;
#if LIS302DL_USE_SPI #if LIS302DL_USE_SPI
#if LIS302DL_SHARED_SPI #if LIS302DL_SHARED_SPI
spiAcquireBus(((LIS302DLDriver *)ip)->config->spip); spiAcquireBus(devp->config->spip);
spiStart(((LIS302DLDriver *)ip)->config->spip, spiStart(devp->config->spip,
((LIS302DLDriver *)ip)->config->spicfg); devp->config->spicfg);
#endif /* LIS302DL_SHARED_SPI */ #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 #if LIS302DL_SHARED_SPI
spiReleaseBus(((LIS302DLDriver *)ip)->config->spip); spiReleaseBus(devp->config->spip);
#endif /* LIS302DL_SHARED_SPI */ #endif /* LIS302DL_SHARED_SPI */
#endif /* LIS302DL_USE_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_USE_SPI
#if LIS302DL_SHARED_SPI #if LIS302DL_SHARED_SPI
spiAcquireBus(((LIS302DLDriver *)ip)->config->spip); spiAcquireBus(devp->config->spip);
spiStart(((LIS302DLDriver *)ip)->config->spip, spiStart(devp->config->spip,
((LIS302DLDriver *)ip)->config->spicfg); devp->config->spicfg);
#endif /* LIS302DL_SHARED_SPI */ #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 #if LIS302DL_SHARED_SPI
spiReleaseBus(((LIS302DLDriver *)ip)->config->spip); spiReleaseBus(devp->config->spip);
#endif /* LIS302DL_SHARED_SPI */ #endif /* LIS302DL_SHARED_SPI */
#endif /* LIS302DL_USE_SPI */ #endif /* LIS302DL_USE_SPI */
/* Scaling sensitivity and bias. Re-calibration is suggested anyway. */ /* Scaling sensitivity and bias. Re-calibration is suggested anyway. */
for(i = 0; i < LIS302DL_NUMBER_OF_AXES; i++) { for(i = 0; i < LIS302DL_ACC_NUMBER_OF_AXES; i++) {
((LIS302DLDriver *)ip)->sensitivity[i] *= scale; devp->accsensitivity[i] *= scale;
((LIS302DLDriver *)ip)->bias[i] *= scale; devp->accbias[i] *= scale;
} }
} }
return MSG_OK; return msg;
} }
static const struct BaseSensorVMT vmt_sensor = { static const struct LIS302DLVMT vmt_device = {
get_axes_number, read_raw, read_cooked (size_t)0,
acc_set_full_scale
}; };
static const struct LIS302DLAccelerometerVMT vmt_accelerometer = { static const struct BaseAccelerometerVMT vmt_accelerometer = {
get_axes_number, read_raw, read_cooked, sizeof(struct LIS302DLVMT*),
set_bias, reset_bias, set_sensivity, reset_sensivity, acc_get_axes_number, acc_read_raw, acc_read_cooked,
set_full_scale acc_set_bias, acc_reset_bias, acc_set_sensivity, acc_reset_sensivity
}; };
/*===========================================================================*/ /*===========================================================================*/
@ -287,13 +424,14 @@ static const struct LIS302DLAccelerometerVMT vmt_accelerometer = {
* @init * @init
*/ */
void lis302dlObjectInit(LIS302DLDriver *devp) { void lis302dlObjectInit(LIS302DLDriver *devp) {
uint32_t i; devp->vmt = &vmt_device;
devp->vmt_sensor = &vmt_sensor; devp->acc_if.vmt = &vmt_accelerometer;
devp->vmt_accelerometer = &vmt_accelerometer;
devp->config = NULL; devp->config = NULL;
for(i = 0; i < LIS302DL_NUMBER_OF_AXES; i++)
devp->bias[i] = 0.0f; devp->accaxes = LIS302DL_ACC_NUMBER_OF_AXES;
devp->state = LIS302DL_STOP;
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 | cr[0] = LIS302DL_CTRL_REG1_XEN | LIS302DL_CTRL_REG1_YEN |
LIS302DL_CTRL_REG1_ZEN | LIS302DL_CTRL_REG1_PD | LIS302DL_CTRL_REG1_ZEN | LIS302DL_CTRL_REG1_PD |
devp->config->outputdatarate | devp->config->accoutputdatarate |
devp->config->fullscale; devp->config->accfullscale;
} }
/* Control register 2 configuration block.*/ /* Control register 2 configuration block.*/
{ {
#if LIS302DL_USE_ADVANCED || defined(__DOXYGEN__) #if LIS302DL_USE_ADVANCED || defined(__DOXYGEN__)
if(devp->config->hpmode != LIS302DL_HPM_BYPASSED) if(devp->config->hpmode != LIS302DL_HPM_BYPASSED)
cr[1] = devp->config->highpass; cr[1] = devp->config->acchighpass;
#endif #endif
} }
@ -345,31 +483,35 @@ void lis302dlStart(LIS302DLDriver *devp, const LIS302DLConfig *config) {
#endif /* LIS302DL_USE_SPI */ #endif /* LIS302DL_USE_SPI */
/* Storing sensitivity information according to full scale value */ /* Storing sensitivity information according to full scale value */
if(devp->config->fullscale == LIS302DL_FS_2G) { if(devp->config->accfullscale == LIS302DL_ACC_FS_2G) {
devp->fullscale = LIS302DL_2G; devp->accfullscale = LIS302DL_ACC_2G;
if(devp->config->sensitivity == NULL) if(devp->config->accsensitivity == NULL)
for(i = 0; i < LIS302DL_NUMBER_OF_AXES; i++) for(i = 0; i < LIS302DL_ACC_NUMBER_OF_AXES; i++)
devp->sensitivity[i] = LIS302DL_SENS_2G; devp->accsensitivity[i] = LIS302DL_ACC_SENS_2G;
else else
for(i = 0; i < LIS302DL_NUMBER_OF_AXES; i++) for(i = 0; i < LIS302DL_ACC_NUMBER_OF_AXES; i++)
devp->sensitivity[i] = devp->config->sensitivity[i]; devp->accsensitivity[i] = devp->config->accsensitivity[i];
} }
else if(devp->config->fullscale == LIS302DL_FS_8G) { else if(devp->config->accfullscale == LIS302DL_ACC_FS_8G) {
devp->fullscale = LIS302DL_8G; devp->accfullscale = LIS302DL_ACC_8G;
if(devp->config->sensitivity == NULL) if(devp->config->accsensitivity == NULL)
for(i = 0; i < LIS302DL_NUMBER_OF_AXES; i++) for(i = 0; i < LIS302DL_ACC_NUMBER_OF_AXES; i++)
devp->sensitivity[i] = LIS302DL_SENS_8G; devp->accsensitivity[i] = LIS302DL_ACC_SENS_8G;
else else
for(i = 0; i < LIS302DL_NUMBER_OF_AXES; i++) for(i = 0; i < LIS302DL_ACC_NUMBER_OF_AXES; i++)
devp->sensitivity[i] = devp->config->sensitivity[i]; devp->accsensitivity[i] = devp->config->accsensitivity[i];
} }
else { else {
osalDbgAssert(FALSE, "lis302dlStart(), accelerometer full scale issue"); osalDbgAssert(FALSE, "lis302dlStart(), accelerometer full scale issue");
} }
if(devp->config->bias != NULL) /* Storing bias information according to user setting */
for(i = 0; i < LIS302DL_NUMBER_OF_AXES; i++) if(devp->config->accbias != NULL)
devp->bias[i] = devp->config->bias[i]; 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 */ /* This is the Accelerometer transient recovery time */
osalThreadSleepMilliseconds(10); osalThreadSleepMilliseconds(10);
@ -388,7 +530,8 @@ void lis302dlStop(LIS302DLDriver *devp) {
uint8_t cr1; uint8_t cr1;
osalDbgCheck(devp != NULL); osalDbgCheck(devp != NULL);
osalDbgAssert((devp->state == LIS302DL_STOP) || (devp->state == LIS302DL_READY), osalDbgAssert((devp->state == LIS302DL_STOP) ||
(devp->state == LIS302DL_READY),
"lis302dlStop(), invalid state"); "lis302dlStop(), invalid state");
if (devp->state == LIS302DL_READY) { if (devp->state == LIS302DL_READY) {
@ -398,6 +541,7 @@ void lis302dlStop(LIS302DLDriver *devp) {
spiStart((devp)->config->spip, spiStart((devp)->config->spip,
(devp)->config->spicfg); (devp)->config->spicfg);
#endif /* LIS302DL_SHARED_SPI */ #endif /* LIS302DL_SHARED_SPI */
/* Disabling all axes and enabling power down mode.*/
cr1 = 0; cr1 = 0;
lis302dlSPIWriteRegister(devp->config->spip, LIS302DL_AD_CTRL_REG1, 1, &cr1); lis302dlSPIWriteRegister(devp->config->spip, LIS302DL_AD_CTRL_REG1, 1, &cr1);
spiStop((devp)->config->spip); spiStop((devp)->config->spip);

View File

@ -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. This file is part of ChibiOS.
@ -43,7 +43,7 @@
/** /**
* @brief LIS302DL driver version string. * @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. * @brief LIS302DL driver version major number.
@ -58,21 +58,26 @@
/** /**
* @brief LIS302DL driver version patch number. * @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_ACC_2G 2.0f
#define LIS302DL_8G 8.0f #define LIS302DL_ACC_8G 8.0f
#define LIS302DL_SENS_2G 18.0f #define LIS302DL_ACC_SENS_2G 18.0f
#define LIS302DL_SENS_8G 72.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 * @name LIS302DL_CTRL_REG1 register bits definitions
* @{ * @{
*/ */
#define LIS302DL_CTRL_REG1_MASK 0xFF /**< LIS302DL_CTRL_REG1 mask */ #define LIS302DL_CTRL_REG1_MASK 0xFF
#define LIS302DL_CTRL_REG1_XEN (1 << 0) /**< X axis enable */ #define LIS302DL_CTRL_REG1_XEN (1 << 0)
#define LIS302DL_CTRL_REG1_YEN (1 << 1) /**< Y axis enable */ #define LIS302DL_CTRL_REG1_YEN (1 << 1)
#define LIS302DL_CTRL_REG1_ZEN (1 << 2) /**< Z axis enable */ #define LIS302DL_CTRL_REG1_ZEN (1 << 2)
#define LIS302DL_CTRL_REG1_STM (1 << 3) /**< Self test P-M */ #define LIS302DL_CTRL_REG1_STM (1 << 3)
#define LIS302DL_CTRL_REG1_STP (1 << 4) /**< Self test P-M */ #define LIS302DL_CTRL_REG1_STP (1 << 4)
#define LIS302DL_CTRL_REG1_FS_MASK 0x20 /**< Full scale field mask */ #define LIS302DL_CTRL_REG1_FS_MASK 0x20
#define LIS302DL_CTRL_REG1_FS (1 << 5) /**< Full scale */ #define LIS302DL_CTRL_REG1_FS (1 << 5)
#define LIS302DL_CTRL_REG1_PD (1 << 6) /**< Power-down mode enable */ #define LIS302DL_CTRL_REG1_PD (1 << 6)
#define LIS302DL_CTRL_REG1_DR (1 << 7) /**< Output data rate */ #define LIS302DL_CTRL_REG1_DR (1 << 7)
/** @} */ /** @} */
/** /**
* @name LIS302DL_CTRL_REG2 register bits definitions * @name LIS302DL_CTRL_REG2 register bits definitions
* @{ * @{
*/ */
#define LIS302DL_CTRL_REG2_MASK 0xDF /**< LIS302DL_CTRL_REG2 mask */ #define LIS302DL_CTRL_REG2_MASK 0xDF
#define LIS302DL_CTRL_REG2_HPCF1 (1 << 0) /**< HP filter cutoff bit 0 */ #define LIS302DL_CTRL_REG2_HPCF1 (1 << 0)
#define LIS302DL_CTRL_REG2_HPCF2 (1 << 1) /**< HP filter cutoff bit 1 */ #define LIS302DL_CTRL_REG2_HPCF2 (1 << 1)
#define LIS302DL_CTRL_REG2_HPFFWU1 (1 << 2) /**< HP filter cutoff bit 2 */ #define LIS302DL_CTRL_REG2_HPFFWU1 (1 << 2)
#define LIS302DL_CTRL_REG2_HPFFWU2 (1 << 3) /**< HP filter cutoff bit 3 */ #define LIS302DL_CTRL_REG2_HPFFWU2 (1 << 3)
#define LIS302DL_CTRL_REG2_FDS (1 << 4) /**< HP filter mode bit 0 */ #define LIS302DL_CTRL_REG2_FDS (1 << 4)
#define LIS302DL_CTRL_REG2_BOOT (1 << 6) /**< HP filter mode bit 1 */ #define LIS302DL_CTRL_REG2_BOOT (1 << 6)
#define LIS302DL_CTRL_REG2_SIM (1 << 7) /**< HP filter mode bit 1 */ #define LIS302DL_CTRL_REG2_SIM (1 << 7)
/** @} */ /** @} */
/** /**
* @name LIS302DL_CTRL_REG3 register bits definitions * @name LIS302DL_CTRL_REG3 register bits definitions
* @{ * @{
*/ */
#define LIS302DL_CTRL_REG3_MASK 0xFF /**< LIS302DL_CTRL_REG3 mask */ #define LIS302DL_CTRL_REG3_MASK 0xFF
#define LIS302DL_CTRL_REG3_I1CFG0 (1 << 0) /**< Interrupt 1 config bit 0 */ #define LIS302DL_CTRL_REG3_I1CFG0 (1 << 0)
#define LIS302DL_CTRL_REG3_I1CFG1 (1 << 1) /**< Interrupt 1 config bit 1 */ #define LIS302DL_CTRL_REG3_I1CFG1 (1 << 1)
#define LIS302DL_CTRL_REG3_I1CFG2 (1 << 2) /**< Interrupt 1 config bit 2 */ #define LIS302DL_CTRL_REG3_I1CFG2 (1 << 2)
#define LIS302DL_CTRL_REG3_I2CFG0 (1 << 3) /**< Interrupt 2 config bit 0 */ #define LIS302DL_CTRL_REG3_I2CFG0 (1 << 3)
#define LIS302DL_CTRL_REG3_I2CFG1 (1 << 4) /**< Interrupt 2 config bit 1 */ #define LIS302DL_CTRL_REG3_I2CFG1 (1 << 4)
#define LIS302DL_CTRL_REG3_I2CFG2 (1 << 5) /**< Interrupt 2 config bit 2 */ #define LIS302DL_CTRL_REG3_I2CFG2 (1 << 5)
#define LIS302DL_CTRL_REG3_PP_OD (1 << 6) /**< Push-pull open-drain */ #define LIS302DL_CTRL_REG3_PP_OD (1 << 6)
#define LIS302DL_CTRL_REG3_IHL (1 << 7) /**< Int active high low */ #define LIS302DL_CTRL_REG3_IHL (1 << 7)
/** @} */ /** @} */
/*===========================================================================*/ /*===========================================================================*/
@ -179,6 +184,16 @@
#define LIS302DL_USE_SPI TRUE #define LIS302DL_USE_SPI TRUE
#endif #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. * @brief LIS302DL I2C interface switch.
* @details If set to @p TRUE the support for I2C is included. * @details If set to @p TRUE the support for I2C is included.
@ -189,22 +204,23 @@
#endif #endif
/** /**
* @brief LIS302DL advanced configurations switch. * @brief LIS302DL shared I2C switch.
* @details If set to @p TRUE more configurations are available. * @details If set to @p TRUE the device acquires I2C bus ownership
* @note The default is @p FALSE. * on each transaction.
* @note The default is @p FALSE. Requires I2C_USE_MUTUAL_EXCLUSION.
*/ */
#if !defined(LIS302DL_USE_ADVANCED) || defined(__DOXYGEN__) #if !defined(LIS302DL_SHARED_I2C) || defined(__DOXYGEN__)
#define LIS302DL_USE_ADVANCED FALSE #define LIS302DL_SHARED_I2C FALSE
#endif #endif
/** /**
* @brief LIS302DL shared SPI switch. * @brief LIS302DL accelerometer subsystem advanced configurations
* @details If set to @p TRUE the device acquires SPI bus ownership * switch.
* on each transaction. * @details If set to @p TRUE more configurations are available.
* @note The default is @p FALSE. Requires SPI_USE_MUTUAL_EXCLUSION. * @note The default is @p FALSE.
*/ */
#if !defined(LIS302DL_SHARED_SPI) || defined(__DOXYGEN__) #if !defined(LIS302DL_ACC_USE_ADVANCED) || defined(__DOXYGEN__)
#define LIS302DL_SHARED_SPI FALSE #define LIS302DL_ACC_USE_ADVANCED FALSE
#endif #endif
/** @} */ /** @} */
@ -220,12 +236,23 @@
#error "LIS302DL_USE_SPI requires HAL_USE_SPI" #error "LIS302DL_USE_SPI requires HAL_USE_SPI"
#endif #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 #if LIS302DL_USE_I2C && !HAL_USE_I2C
#error "LIS302DL_USE_I2C requires HAL_USE_I2C" #error "LIS302DL_USE_I2C requires HAL_USE_I2C"
#endif #endif
#if LIS302DL_SHARED_SPI && !SPI_USE_MUTUAL_EXCLUSION #if LIS302DL_SHARED_I2C && !I2C_USE_MUTUAL_EXCLUSION
#error "LIS302DL_SHARED_SPI requires SPI_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 #endif
/*===========================================================================*/ /*===========================================================================*/
@ -236,32 +263,37 @@
* @name LIS302DL data structures and types * @name LIS302DL data structures and types
* @{ * @{
*/ */
/**
* @brief Structure representing a LIS302DL driver.
*/
typedef struct LIS302DLDriver LIS302DLDriver;
/** /**
* @brief LIS302DL full scale. * @brief LIS302DL full scale.
*/ */
typedef enum { typedef enum {
LIS302DL_FS_2G = 0x00, /**< Full scale ±2g. */ LIS302DL_ACC_FS_2G = 0x00, /**< Full scale ±2g. */
LIS302DL_FS_8G = 0x20 /**< Full scale ±8g. */ LIS302DL_ACC_FS_8G = 0x20 /**< Full scale ±8g. */
}lis302dl_fs_t; }lis302dl_acc_fs_t;
/** /**
* @brief LIS302DL output data rate and bandwidth. * @brief LIS302DL output data rate and bandwidth.
*/ */
typedef enum { typedef enum {
LIS302DL_ODR_100HZ = 0x00, /**< ODR 100 Hz. */ LIS302DL_ACC_ODR_100HZ = 0x00, /**< ODR 100 Hz. */
LIS302DL_ODR_400HZ = 0x80 /**< ODR 400 Hz. */ LIS302DL_ACC_ODR_400HZ = 0x80 /**< ODR 400 Hz. */
}lis302dl_odr_t; }lis302dl_acc_odr_t;
/** /**
* @brief LIS302DL high pass filtering. * @brief LIS302DL high pass filtering.
*/ */
typedef enum { typedef enum {
LIS302DL_HP_DISABLED = 0x00, /**< HP bypassed. */ LIS302DL_ACC_HP_DISABLED = 0x00, /**< HP bypassed. */
LIS302DL_HP_0 = 0x10, /**< HP cutoff 2Hz (ODR 100Hz) or 8Hz */ LIS302DL_ACC_HP_0 = 0x10, /**< HP cutoff 2Hz (ODR 100Hz) or 8Hz */
LIS302DL_HP_1 = 0x11, /**< HP cutoff 1Hz or 4Hz */ LIS302DL_ACC_HP_1 = 0x11, /**< HP cutoff 1Hz or 4Hz */
LIS302DL_HP_2 = 0x12, /**< HP cutoff 0.5Hz or 2Hz */ LIS302DL_ACC_HP_2 = 0x12, /**< HP cutoff 0.5Hz or 2Hz */
LIS302DL_HP_3 = 0x13 /**< HP cutoff 0.25Hz or 1Hz */ LIS302DL_ACC_HP_3 = 0x13 /**< HP cutoff 0.25Hz or 1Hz */
}lis302dl_hp_t; }lis302dl_acc_hp_t;
/** /**
* @brief Driver state machine possible states. * @brief Driver state machine possible states.
@ -298,52 +330,51 @@ typedef struct {
const I2CConfig *i2ccfg; const I2CConfig *i2ccfg;
#endif /* LIS302DL_USE_I2C */ #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. * @brief LIS302DL output data rate selection.
*/ */
lis302dl_odr_t outputdatarate; lis302dl_acc_odr_t accoutputdatarate;
#if LIS302DL_USE_ADVANCED || defined(__DOXYGEN__) #if LIS302DL_USE_ADVANCED || defined(__DOXYGEN__)
/** /**
* @brief LIS302DL high pass filtering. * @brief LIS302DL high pass filtering.
*/ */
lis302dl_hp_t highpass; lis302dl_acc_hp_t acchighpass;
#endif #endif
} LIS302DLConfig; } 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 .*/ \ /* 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 * @brief @p LIS302DL specific methods with inherited ones.
* ones.
*/ */
#define _lis302dl_accelerometer_methods \ #define _lis302dl_methods \
_base_accelerometer_methods \ _base_object_methods \
_lis302dl_accelerometer_methods_alone _lis302dl_methods_alone
/** /**
* @extends BaseAccelerometerVMT * @extends BaseObjectVMT
* *
* @brief @p LIS302DL accelerometer virtual methods table. * @brief @p LIS302DL accelerometer virtual methods table.
*/ */
struct LIS302DLAccelerometerVMT { struct LIS302DLVMT {
_lis302dl_accelerometer_methods _lis302dl_methods
}; };
/** /**
@ -354,34 +385,25 @@ struct LIS302DLAccelerometerVMT {
lis302dl_state_t state; \ lis302dl_state_t state; \
/* Current configuration data.*/ \ /* Current configuration data.*/ \
const LIS302DLConfig *config; \ const LIS302DLConfig *config; \
/* Accelerometer subsystem axes number.*/ \
size_t accaxes; \
/* Current sensitivity.*/ \ /* Current sensitivity.*/ \
float sensitivity[LIS302DL_NUMBER_OF_AXES]; \ float accsensitivity[LIS302DL_ACC_NUMBER_OF_AXES]; \
/* Bias data.*/ \ /* Bias data.*/ \
int32_t bias[LIS302DL_NUMBER_OF_AXES]; \ int32_t accbias[LIS302DL_ACC_NUMBER_OF_AXES]; \
/* Current full scale value.*/ \ /* Current full scale value.*/ \
float fullscale; float accfullscale;
/** /**
* @extends BaseAccelerometer
*
* @brief LIS302DL 3-axis accelerometer class. * @brief LIS302DL 3-axis accelerometer class.
* @details This class extends @p BaseAccelerometer by adding physical
* driver implementation.
*/ */
struct LIS302DLDriver { struct LIS302DLDriver {
/** @brief BaseSensor Virtual Methods Table. */ /** @brief Virtual Methods Table.*/
const struct BaseSensorVMT *vmt_sensor; const struct LIS302DLVMT *vmt;
_base_sensor_data /** @brief Base accelerometer interface.*/
/** @brief BaseAccelerometer Virtual Methods Table. */ BaseAccelerometer acc_if;
const struct LIS302DLAccelerometerVMT *vmt_accelerometer;
_base_accelerometer_data
_lis302dl_data _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] ip pointer to a @p LIS302DLDriver class. * @param[in] devp pointer to @p LIS302DLDriver.
* @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. * @return The operation status.
* @retval MSG_OK if the function succeeded. * @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 * @api
*/ */
#define accelerometerSetFullScale(ip, fs) \ #define lis302dlAccelerometerReadRaw(devp, axes) \
(ip)->vmt_accelerometer->set_full_scale(ip, fs) 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 lis302dlAccelerometerSetFullScale(devp, fs) \
(devp)->vmt->acc_set_full_scale(devp, fs)
/*===========================================================================*/ /*===========================================================================*/
/* External declarations. */ /* External declarations. */

View File

@ -71,7 +71,7 @@ endif
# Enables the use of FPU (no, softfp, hard). # Enables the use of FPU (no, softfp, hard).
ifeq ($(USE_FPU),) ifeq ($(USE_FPU),)
USE_FPU = hard USE_FPU = no
endif endif
# #
@ -87,6 +87,9 @@ PROJECT = ch
# Imported source files and paths # Imported source files and paths
CHIBIOS = ../../../.. CHIBIOS = ../../../..
# Licensing files.
include $(CHIBIOS)/os/license/license.mk
# Startup files. # Startup files.
include $(CHIBIOS)/os/common/startup/ARMCMx/compilers/GCC/mk/startup_stm32f4xx.mk include $(CHIBIOS)/os/common/startup/ARMCMx/compilers/GCC/mk/startup_stm32f4xx.mk
# HAL-OSAL files (optional). # HAL-OSAL files (optional).
@ -97,10 +100,10 @@ include $(CHIBIOS)/os/hal/osal/rt/osal.mk
# RTOS files (optional). # RTOS files (optional).
include $(CHIBIOS)/os/rt/rt.mk include $(CHIBIOS)/os/rt/rt.mk
include $(CHIBIOS)/os/common/ports/ARMCMx/compilers/GCC/mk/port_v7m.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 include $(CHIBIOS)/os/ex/ST/lis302dl.mk
# Other files (optional).
include $(CHIBIOS)/os/hal/lib/streams/streams.mk include $(CHIBIOS)/os/hal/lib/streams/streams.mk
include $(CHIBIOS)/os/various/shell/shell.mk
# Define linker script file here # Define linker script file here
LDSCRIPT= $(STARTUPLD)/STM32F407xG.ld LDSCRIPT= $(STARTUPLD)/STM32F407xG.ld
@ -114,14 +117,20 @@ CSRC = $(STARTUPSRC) \
$(HALSRC) \ $(HALSRC) \
$(PLATFORMSRC) \ $(PLATFORMSRC) \
$(BOARDSRC) \ $(BOARDSRC) \
$(LIS302DLSRC) \ $(LIS3DSHSRC) \
$(STREAMSSRC) \ $(STREAMSSRC) \
$(SHELLSRC) \ $(SHELLSRC) \
usbcfg.c main.c 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 # C++ sources that can be compiled in ARM or THUMB mode depending on the global
# setting. # setting.
CPPSRC = CPPSRC = $(ALLCPPSRC)
# C sources to be compiled in ARM mode regardless of the global setting. # C sources to be compiled in ARM mode regardless of the global setting.
# NOTE: Mixing ARM and THUMB mode enables the -mthumb-interwork compiler # NOTE: Mixing ARM and THUMB mode enables the -mthumb-interwork compiler
@ -147,10 +156,7 @@ TCPPSRC =
ASMSRC = ASMSRC =
ASMXSRC = $(STARTUPASM) $(PORTASM) $(OSALASM) ASMXSRC = $(STARTUPASM) $(PORTASM) $(OSALASM)
INCDIR = $(CHIBIOS)/os/license \ INCDIR = $(ALLINC) $(TESTINC)
$(STARTUPINC) $(KERNINC) $(PORTINC) $(OSALINC) \
$(HALINC) $(PLATFORMINC) $(BOARDINC) $(LIS302DLINC) \
$(STREAMSINC) $(SHELLINC)
# #
# Project, sources and paths # Project, sources and paths
@ -200,7 +206,7 @@ CPPWARN = -Wall -Wextra -Wundef
# #
# List all user C define here, like -D_DEBUG=1 # 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 -DLIS302DL_USE_ADVANCED=0 -DLIS302DL_SHARED_SPI=0
# Define ASM defines here # Define ASM defines here

View File

@ -18,23 +18,23 @@
#include "hal.h" #include "hal.h"
#include "usbcfg.h" #include "usbcfg.h"
#include "string.h"
#include "shell.h"
#include "chprintf.h" #include "chprintf.h"
#include "lis302dl.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 */ /* LIS302DL Driver: This object represent an LIS302DL instance */
static LIS302DLDriver LIS302DLD1; static LIS302DLDriver LIS302DLD1;
static int32_t rawdata[LIS302DL_NUMBER_OF_AXES]; static int32_t accraw[LIS302DL_ACC_NUMBER_OF_AXES];
static float cookeddata[LIS302DL_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 uint32_t i;
static const SPIConfig spicfg = { static const SPIConfig spicfg = {
@ -46,100 +46,23 @@ static const SPIConfig spicfg = {
0 0
}; };
static LIS302DLConfig l3gd20cfg = { static LIS302DLConfig lis302dlcfg = {
&SPID1, &SPID1,
&spicfg, &spicfg,
NULL, NULL,
NULL, NULL,
LIS302DL_FS_2G, LIS302DL_ACC_FS_2G,
LIS302DL_ODR_100HZ, LIS302DL_ACC_ODR_100HZ,
#if LIS302DL_USE_ADVANCED || defined(__DOXYGEN__) #if LIS302DL_USE_ADVANCED
LIS302DL_HP_DISABLED, LIS302DL_ACC_HP_1,
#endif #endif
}; };
/*===========================================================================*/ /*===========================================================================*/
/* Command line related. */ /* Generic code. */
/*===========================================================================*/
/* 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");
#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. */
/*===========================================================================*/ /*===========================================================================*/
static BaseSequentialStream* chp = (BaseSequentialStream*)&SDU1;
/* /*
* LED blinker thread, times are in milliseconds. * LED blinker thread, times are in milliseconds.
*/ */
@ -152,9 +75,7 @@ static THD_FUNCTION(Thread1, arg) {
systime_t time; systime_t time;
time = serusbcfg.usbp->state == USB_ACTIVE ? 250 : 500; time = serusbcfg.usbp->state == USB_ACTIVE ? 250 : 500;
palClearLine(LINE_LED6); palToggleLine(LINE_LED6);
chThdSleepMilliseconds(time);
palSetLine(LINE_LED6);
chThdSleepMilliseconds(time); chThdSleepMilliseconds(time);
} }
} }
@ -195,21 +116,23 @@ int main(void) {
lis302dlObjectInit(&LIS302DLD1); lis302dlObjectInit(&LIS302DLD1);
/* Activates the LIS302DL driver.*/ /* Activates the LIS302DL driver.*/
lis302dlStart(&LIS302DLD1, &l3gd20cfg); lis302dlStart(&LIS302DLD1, &lis302dlcfg);
/* Shell manager initialization.*/ /* Normal main() thread activity, printing MEMS data on the SDU1.*/
shellInit(); while (true) {
lis302dlAccelerometerReadRaw(&LIS302DLD1, accraw);
while(TRUE) { chprintf(chp, "LIS302DL Accelerometer raw data...\r\n");
if (SDU1.config->usbp->state == USB_ACTIVE) { for(i = 0; i < LIS302DL_ACC_NUMBER_OF_AXES; i++) {
thread_t *shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE, chprintf(chp, "%c-axis: %d\r\n", axisID[i], accraw[i]);
"shell", NORMALPRIO + 1,
shellThread, (void *)&shell_cfg1);
chThdWait(shelltp); /* Waiting termination. */
} }
chThdSleepMilliseconds(1000);
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(100);
cls(chp);
} }
lis302dlStop(&LIS302DLD1); lis302dlStop(&LIS302DLD1);
return 0;
} }

View File

@ -4,12 +4,16 @@
** TARGET ** ** 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 **
The demo flashes the board LED using a thread, read data from LIS302DL printing The application demonstrates the use of the STM32F4xx SPI driver in order
it on a BaseSequentialStream (SDU1, mapped on USB virtual COM port). to acquire data from LIS302L using ChibiOS/EX.
** Board Setup **
None required.
** Build Procedure ** ** Build Procedure **