From dd84b8e66934e7480606e0700d96f5aa9aa3e1fb Mon Sep 17 00:00:00 2001 From: Rocco Marco Guglielmi Date: Mon, 31 Jul 2023 14:29:43 +0000 Subject: [PATCH] Updated ADXl355 Driver and related demos git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@16355 27425a3e-05d8-49a3-a47f-9c15f0e5edd8 --- os/ex/devices/ADI/adxl355.c | 108 +++++++++++--------- os/ex/devices/ADI/adxl355.h | 15 ++- testex/STM32/STM32F4xx/SPI-ADXL355/main.c | 25 +++-- testex/STM32/STM32F7xx/SPI-ADXL355/.project | 5 - testex/STM32/STM32F7xx/SPI-ADXL355/main.c | 45 ++++++-- 5 files changed, 119 insertions(+), 79 deletions(-) diff --git a/os/ex/devices/ADI/adxl355.c b/os/ex/devices/ADI/adxl355.c index 029f8c74d..e3b94b780 100644 --- a/os/ex/devices/ADI/adxl355.c +++ b/os/ex/devices/ADI/adxl355.c @@ -29,6 +29,7 @@ #include "hal.h" #include "adxl355.h" +#include "string.h" /*===========================================================================*/ /* Driver local definitions. */ @@ -46,54 +47,6 @@ /* 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] devp pointer to @p ADXL355Driver 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(ADXL355Driver *devp, uint8_t reg, size_t n, - uint8_t* b) { - unsigned i; - devp->commtxp[0] = (reg << 1) | ADXL355_RW; - cacheBufferFlush(&devp->commtxp[0], sizeof devp->commtxp); - spiSelect(devp->config->spip); - spiSend(devp->config->spip, 1, devp->commtxp); - spiReceive(devp->config->spip, n, devp->commrxp); - spiUnselect(devp->config->spip); - cacheBufferInvalidate(&devp->commrxp[0], sizeof devp->commrxp); - for(i = 0; i < n; i++, b++) { - *b = devp->commrxp[i]; - } -} - -/** - * @brief Writes a value into a generic register using SPI. - * @pre The SPI interface must be initialized and the driver started. - * - * @param[in] devp pointer to @p ADXL355Driver 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(ADXL355Driver *devp, uint8_t reg, size_t n, - uint8_t* b) { - unsigned i; - devp->commtxp[0] = (reg << 1); - for(i = 0; i < n; i++, b++) { - devp->commtxp[i + 1] = *b; - } - cacheBufferFlush(&devp->commtxp[0], sizeof devp->commtxp); - spiSelect(devp->config->spip); - spiSend(devp->config->spip, n + 1, devp->commtxp); - spiUnselect(devp->config->spip); -} -#endif /* ADXL355_USE_SPI */ - /** * @brief Return the number of axes of the BaseAccelerometer. * @@ -437,6 +390,65 @@ static const struct BaseAccelerometerVMT vmt_accelerometer = { /* Driver exported 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] devp pointer to @p ADXL355Driver interface. + * @param[in] reg starting register address + * @param[in] n number of consecutive registers to read + * @param[in] b pointer to an output buffer. + */ +void adxl355SPIReadRegister(ADXL355Driver *devp, uint8_t reg, size_t n, + uint8_t* b) { + + osalDbgCheck(n < ADXL355_COMM_BUFF_SIZE); + + /* Preparing a read. */ + devp->commtxp[0] = (reg << 1) | ADXL355_RW; + + /* Reading. */ + cacheBufferFlush(&devp->commtxp[0], sizeof(devp->commtxp)); + spiSelect(devp->config->spip); + spiSend(devp->config->spip, 1, devp->commtxp); + spiReceive(devp->config->spip, n, devp->commrxp); + spiUnselect(devp->config->spip); + cacheBufferInvalidate(&devp->commrxp[0], sizeof(devp->commrxp)); + + /* Copying the data in the final buffer. */ + memcpy(b, devp->commrxp, n); +} + +/** + * @brief Writes a value into a generic register using SPI. + * @pre The SPI interface must be initialized and the driver started. + * + * @param[in] devp pointer to @p ADXL355Driver 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. + */ +void adxl355SPIWriteRegister(ADXL355Driver *devp, uint8_t reg, size_t n, + uint8_t* b) { + unsigned i; + + osalDbgCheck(n < ADXL355_COMM_BUFF_SIZE); + + /* Preparing a write. */ + devp->commtxp[0] = (reg << 1); + for(i = 0; i < n; i++, b++) { + devp->commtxp[i + 1] = *b; + } + + /* Writing. */ + cacheBufferFlush(&devp->commtxp[0], sizeof(devp->commtxp)); + spiSelect(devp->config->spip); + spiSend(devp->config->spip, n + 1, devp->commtxp); + spiUnselect(devp->config->spip); +} +#endif /* ADXL355_USE_SPI */ + /** * @brief Initializes an instance. * @details The buffer should be at least large @p ADXL355_COMM_BUFF_SIZE. diff --git a/os/ex/devices/ADI/adxl355.h b/os/ex/devices/ADI/adxl355.h index 4507a019c..f754edd5c 100644 --- a/os/ex/devices/ADI/adxl355.h +++ b/os/ex/devices/ADI/adxl355.h @@ -43,7 +43,7 @@ /** * @brief ADXL355 driver version string. */ -#define EX_ADXL355_VERSION "1.0.2" +#define EX_ADXL355_VERSION "1.1.0" /** * @brief ADXL355 driver version major number. @@ -53,12 +53,12 @@ /** * @brief ADXL355 driver version minor number. */ -#define EX_ADXL355_MINOR 0 +#define EX_ADXL355_MINOR 1 /** * @brief ADXL355 driver version patch number. */ -#define EX_ADXL355_PATCH 2 +#define EX_ADXL355_PATCH 0 /** @} */ /** @@ -91,7 +91,6 @@ #define ADXL355_AD_MASK 0xFE #define ADXL355_RW (1 << 0) #define ADXL355_AD(n) (1 << (n + 1)) - /** @} */ /** @@ -287,7 +286,7 @@ * @brief ADXL355 internal communication buffer sizes. */ #if !defined(ADXL355_COMM_BUFF_SIZE) || defined(__DOXYGEN__) -#define ADXL355_COMM_BUFF_SIZE 9 +#define ADXL355_COMM_BUFF_SIZE 32 #endif /** @} */ @@ -638,6 +637,12 @@ struct ADXL355Driver { #ifdef __cplusplus extern "C" { #endif +#if (ADXL355_USE_SPI) || defined(__DOXYGEN__) + void adxl355SPIReadRegister(ADXL355Driver *devp, uint8_t reg, size_t n, + uint8_t* b); + void adxl355SPIWriteRegister(ADXL355Driver *devp, uint8_t reg, size_t n, + uint8_t* b); +#endif /* ADXL355_USE_SPI */ void adxl355ObjectInit(ADXL355Driver *devp, uint8_t* txbp, uint8_t* rxbp); void adxl355Start(ADXL355Driver *devp, const ADXL355Config *config); void adxl355Stop(ADXL355Driver *devp); diff --git a/testex/STM32/STM32F4xx/SPI-ADXL355/main.c b/testex/STM32/STM32F4xx/SPI-ADXL355/main.c index c3872b046..b53ed30c3 100644 --- a/testex/STM32/STM32F4xx/SPI-ADXL355/main.c +++ b/testex/STM32/STM32F4xx/SPI-ADXL355/main.c @@ -31,12 +31,15 @@ static uint8_t rxbuf[ADXL355_COMM_BUFF_SIZE]; /* ADXL355 Driver: This object represent an ADXL355 instance */ static ADXL355Driver ADXL355D1; +static BaseAccelerometer * accp = &(ADXL355D1.acc_if); +static uint32_t axes_count; static int32_t accraw[ADXL355_ACC_NUMBER_OF_AXES]; - static float acccooked[ADXL355_ACC_NUMBER_OF_AXES]; + static char axisID[ADXL355_ACC_NUMBER_OF_AXES] = {'X', 'Y', 'Z'}; +static const char device_name[] = "ADXL355"; static uint32_t i; static const SPIConfig spicfg = { @@ -66,7 +69,7 @@ static ADXL355Config adxl355cfg = { /*===========================================================================*/ static BaseSequentialStream* chp = (BaseSequentialStream*) &SD5; - + static THD_WORKING_AREA(waThreadBlinker, 128); static THD_FUNCTION(ThreadBlinker, arg) { @@ -114,20 +117,22 @@ int main(void) { /* Activates the ADXL355 driver.*/ adxl355Start(&ADXL355D1, &adxl355cfg); - /* Normal main() thread activity, printing MEMS data on the SDU1.*/ + /* Getting the count of axes. */ + axes_count = accelerometerGetAxesNumber(accp); + + /* Normal main() thread activity, printing MEMS data on the SD5.*/ while (true) { - adxl355AccelerometerReadRaw(&ADXL355D1, accraw); - chprintf(chp, "ADXL355 Accelerometer raw data...\r\n"); - for(i = 0; i < ADXL355_ACC_NUMBER_OF_AXES; i++) { + accelerometerReadRaw(accp, accraw); + chprintf(chp, "%s raw data...\r\n", device_name); + for(i = 0; i < axes_count; i++) { chprintf(chp, "%c-axis: %d\r\n", axisID[i], accraw[i]); } - adxl355AccelerometerReadCooked(&ADXL355D1, acccooked); - chprintf(chp, "ADXL355 Accelerometer cooked data...\r\n"); - for(i = 0; i < ADXL355_ACC_NUMBER_OF_AXES; i++) { + accelerometerReadCooked(accp, acccooked); + chprintf(chp, "%s cooked data...\r\n", device_name); + for(i = 0; i < axes_count; i++) { chprintf(chp, "%c-axis: %.3f\r\n", axisID[i], acccooked[i]); } chThdSleepMilliseconds(100); } - adxl355Stop(&ADXL355D1); } diff --git a/testex/STM32/STM32F7xx/SPI-ADXL355/.project b/testex/STM32/STM32F7xx/SPI-ADXL355/.project index cd31c0620..61c5a7a05 100644 --- a/testex/STM32/STM32F7xx/SPI-ADXL355/.project +++ b/testex/STM32/STM32F7xx/SPI-ADXL355/.project @@ -86,10 +86,5 @@ 2 CHIBIOS/os - - test - 2 - CHIBIOS/test - diff --git a/testex/STM32/STM32F7xx/SPI-ADXL355/main.c b/testex/STM32/STM32F7xx/SPI-ADXL355/main.c index e3345951c..674b998d5 100644 --- a/testex/STM32/STM32F7xx/SPI-ADXL355/main.c +++ b/testex/STM32/STM32F7xx/SPI-ADXL355/main.c @@ -27,17 +27,19 @@ /* * SPI TX and RX buffers. */ -CC_ALIGN_DATA(32) static uint8_t txbuf[32]; -CC_ALIGN_DATA(32) static uint8_t rxbuf[32]; +CC_ALIGN_DATA(32) static uint8_t txbuf[ADXL355_COMM_BUFF_SIZE]; +CC_ALIGN_DATA(32) static uint8_t rxbuf[ADXL355_COMM_BUFF_SIZE]; /* ADXL355 Driver: This object represent an ADXL355 instance */ static ADXL355Driver ADXL355D1; +static BaseAccelerometer * accp = &(ADXL355D1.acc_if); +static uint32_t axes_count; static int32_t accraw[ADXL355_ACC_NUMBER_OF_AXES]; - static float acccooked[ADXL355_ACC_NUMBER_OF_AXES]; static char axisID[ADXL355_ACC_NUMBER_OF_AXES] = {'X', 'Y', 'Z'}; +static const char device_name[] = "ADXL355"; static uint32_t i; static const SPIConfig spicfg = { @@ -68,6 +70,21 @@ static ADXL355Config adxl355cfg = { static BaseSequentialStream* chp = (BaseSequentialStream*)&SD1; +static THD_WORKING_AREA(waThreadBlinker, 128); +static THD_FUNCTION(ThreadBlinker, arg) { + + (void)arg; + + chRegSetThreadName("blinker"); + + while (true) { + palSetLine(LINE_ARD_D13); + chThdSleepMilliseconds(200); + palClearLine(LINE_ARD_D13); + chThdSleepMilliseconds(200); + } +} + /* * Application entry point. */ @@ -90,26 +107,32 @@ int main(void) { palSetLineMode(LINE_ARD_D12, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_HIGHEST); palSetLineMode(LINE_ARD_D13, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_HIGHEST); + /* Creating LED thread.*/ + chThdCreateStatic(waThreadBlinker, sizeof(waThreadBlinker), NORMALPRIO + 1, + ThreadBlinker, NULL); + /* ADXL355 Object Initialization.*/ adxl355ObjectInit(&ADXL355D1, txbuf, rxbuf); /* Activates the ADXL355 driver.*/ adxl355Start(&ADXL355D1, &adxl355cfg); - /* Normal main() thread activity, printing MEMS data on the SDU1.*/ + /* Getting the count of axes. */ + axes_count = accelerometerGetAxesNumber(accp); + + /* Normal main() thread activity, printing MEMS data on the SD1.*/ while (true) { - adxl355AccelerometerReadRaw(&ADXL355D1, accraw); - chprintf(chp, "ADXL355 Accelerometer raw data...\r\n"); - for(i = 0; i < ADXL355_ACC_NUMBER_OF_AXES; i++) { + accelerometerReadRaw(accp, accraw); + chprintf(chp, "%s raw data...\r\n", device_name); + for(i = 0; i < axes_count; i++) { chprintf(chp, "%c-axis: %d\r\n", axisID[i], accraw[i]); } - adxl355AccelerometerReadCooked(&ADXL355D1, acccooked); - chprintf(chp, "ADXL355 Accelerometer cooked data...\r\n"); - for(i = 0; i < ADXL355_ACC_NUMBER_OF_AXES; i++) { + accelerometerReadCooked(accp, acccooked); + chprintf(chp, "%s cooked data...\r\n", device_name); + for(i = 0; i < axes_count; i++) { chprintf(chp, "%c-axis: %.3f\r\n", axisID[i], acccooked[i]); } chThdSleepMilliseconds(100); } - adxl355Stop(&ADXL355D1); }