From be45af2a10460c65ef86ebec459a3d517a8af7d6 Mon Sep 17 00:00:00 2001 From: Rocco Marco Guglielmi Date: Thu, 28 Jul 2016 14:53:12 +0000 Subject: UpdatedEX: LIS3DSH driver updated to v1.0.0 git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@9728 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- testhal/STM32/STM32F4xx/SPI-LIS3DSH/Makefile | 9 +- testhal/STM32/STM32F4xx/SPI-LIS3DSH/main.c | 188 ++++++++++++++++++++------- 2 files changed, 145 insertions(+), 52 deletions(-) (limited to 'testhal') diff --git a/testhal/STM32/STM32F4xx/SPI-LIS3DSH/Makefile b/testhal/STM32/STM32F4xx/SPI-LIS3DSH/Makefile index e7b2b0d72..28b3b4b69 100644 --- a/testhal/STM32/STM32F4xx/SPI-LIS3DSH/Makefile +++ b/testhal/STM32/STM32F4xx/SPI-LIS3DSH/Makefile @@ -5,7 +5,7 @@ # Compiler options here. ifeq ($(USE_OPT),) - USE_OPT = -O0 -ggdb -fomit-frame-pointer -falign-functions=16 + USE_OPT = -O2 -ggdb -fomit-frame-pointer -falign-functions=16 endif # C specific options here (added to USE_OPT). @@ -100,6 +100,7 @@ include $(CHIBIOS)/os/common/ports/ARMCMx/compilers/GCC/mk/port_v7m.mk # Other files (optional). include $(CHIBIOS)/os/ex/ST/lis3dsh.mk include $(CHIBIOS)/os/hal/lib/streams/streams.mk +include $(CHIBIOS)/os/various/shell/shell.mk # Define linker script file here LDSCRIPT= $(STARTUPLD)/STM32F407xG.ld @@ -115,6 +116,7 @@ CSRC = $(STARTUPSRC) \ $(BOARDSRC) \ $(LIS3DSHSRC) \ $(STREAMSSRC) \ + $(SHELLSRC) \ usbcfg.c main.c # C++ sources that can be compiled in ARM or THUMB mode depending on the global @@ -148,7 +150,7 @@ ASMXSRC = $(STARTUPASM) $(PORTASM) $(OSALASM) INCDIR = $(CHIBIOS)/os/license \ $(STARTUPINC) $(KERNINC) $(PORTINC) $(OSALINC) \ $(HALINC) $(PLATFORMINC) $(BOARDINC) $(LIS3DSHINC) \ - $(STREAMSINC) $(CHIBIOS)/os/various + $(STREAMSINC) $(SHELLINC) # # Project, sources and paths @@ -198,7 +200,8 @@ CPPWARN = -Wall -Wextra -Wundef # # List all user C define here, like -D_DEBUG=1 -UDEFS = -DCHPRINTF_USE_FLOAT=1 +UDEFS = -DCHPRINTF_USE_FLOAT=1 -DSHELL_CMD_TEST_ENABLED=0 \ + -DLIS3DSH_USE_ADVANCED=0 # Define ASM defines here UADEFS = diff --git a/testhal/STM32/STM32F4xx/SPI-LIS3DSH/main.c b/testhal/STM32/STM32F4xx/SPI-LIS3DSH/main.c index e67eda9b8..018eb5d55 100644 --- a/testhal/STM32/STM32F4xx/SPI-LIS3DSH/main.c +++ b/testhal/STM32/STM32F4xx/SPI-LIS3DSH/main.c @@ -18,43 +18,140 @@ #include "hal.h" #include "usbcfg.h" - +#include "string.h" +#include "shell.h" #include "chprintf.h" -#include "lis3dsh.h" -/* Enable use of special ANSI escape sequences */ -#define CHPRINTF_USE_ANSI_CODE TRUE +#include "lis3dsh.h" -static BaseSequentialStream * chp = (BaseSequentialStream*) &SDU1; +/*===========================================================================*/ +/* LIS3DSH related. */ +/*===========================================================================*/ /* LIS3DSH Driver: This object represent an LIS3DSH instance */ static LIS3DSHDriver LIS3DSHD1; static int32_t rawdata[LIS3DSH_NUMBER_OF_AXES]; static float cookeddata[LIS3DSH_NUMBER_OF_AXES]; -static int8_t temperature; -static char axesID[LIS3DSH_NUMBER_OF_AXES] = {'X', 'Y', 'Z'}; +static char axisID[LIS3DSH_NUMBER_OF_AXES] = {'X', 'Y', 'Z'}; static uint32_t i; static const SPIConfig spicfg = { NULL, - GPIOE, /* port of LIS3DSH CS */ - GPIOE_CS_SPI, /* pin of LIS3DSH CS */ - SPI_CR1_BR_1 | SPI_CR1_CPOL | SPI_CR1_CPHA,/* CR1 register*/ /* CR2 register */ + GPIOE, /* port of LIS3DSH CS.*/ + GPIOE_CS_SPI, /* pin of LIS3DSH CS.*/ + SPI_CR1_BR_0 | SPI_CR1_CPOL | SPI_CR1_CPHA,/* CR1 register.*/ + 0 /* CR2 register.*/ }; static LIS3DSHConfig lis3dshcfg = { - &SPID1, /* Pointer to SPI Driver */ - &spicfg, /* Pointer to SPI Configuration */ - LIS3DSH_FS_2G, /* Full scale value */ - LIS3DSH_ODR_100HZ, /* Output data rate */ - LIS3DSH_BW_200HZ, /* AA filter BW 200 Hz */ - LIS3DSH_AE_XYZ, /* Enabled axes */ - LIS3DSH_BDU_BLOCKED, /* Block data update blocked */ - LIS3DSH_ACC_UNIT_G /* Measurement unit for cooked */ + &SPID1, /* Pointer to SPI Driver.*/ + &spicfg, /* Pointer to SPI Configuration.*/ + {0, 0, 0}, /* Use default sensitivity.*/ + {0, 0, 0}, /* Use default bias.*/ + LIS3DSH_FS_2G, /* Full scale value.*/ + LIS3DSH_ODR_100HZ, /* Output data rate.*/ +#if LIS3DSH_USE_ADVANCED || defined(__DOXYGEN__) + LIS3DSH_BW_400HZ, /* AA filter bandwidth.*/ + LIS3DSH_BDU_CONTINUOUS, /* Block data update continuous.*/ +#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(&LIS3DSHD1, rawdata); + chprintf(chp, "LIS3DSH Accelerometer raw data...\r\n"); + for(i = 0; i < LIS3DSH_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(&LIS3DSHD1, cookeddata); + chprintf(chp, "LIS3DSH Accelerometer cooked data...\r\n"); + for(i = 0; i < LIS3DSH_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|4G|6G|8G|16G]\r\n"); + return; + } +#if CHPRINTF_USE_ANSI_CODE + chprintf(chp, "\033[2J\033[1;1H"); +#endif + if(!strcmp (argv[0], "2G")) { + accelerometerSetFullScale(&LIS3DSHD1, LIS3DSH_FS_2G); + chprintf(chp, "LIS3DSH Accelerometer full scale set to 2G...\r\n"); + } + else if(!strcmp (argv[0], "4G")) { + accelerometerSetFullScale(&LIS3DSHD1, LIS3DSH_FS_4G); + chprintf(chp, "LIS3DSH Accelerometer full scale set to 4G...\r\n"); + } + else if(!strcmp (argv[0], "6G")) { + accelerometerSetFullScale(&LIS3DSHD1, LIS3DSH_FS_6G); + chprintf(chp, "LIS3DSH Accelerometer full scale set to 6G...\r\n"); + } + else if(!strcmp (argv[0], "8G")) { + accelerometerSetFullScale(&LIS3DSHD1, LIS3DSH_FS_8G); + chprintf(chp, "LIS3DSH Accelerometer full scale set to 8G...\r\n"); + } + else if(!strcmp (argv[0], "16G")) { + accelerometerSetFullScale(&LIS3DSHD1, LIS3DSH_FS_16G); + chprintf(chp, "LIS3DSH Accelerometer full scale set to 16G...\r\n"); + } + else { + chprintf(chp, "Usage: fullscale [2G|4G|6G|8G|16G]\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. */ +/*===========================================================================*/ + /* * LED blinker thread, times are in milliseconds. */ @@ -64,8 +161,13 @@ static THD_FUNCTION(Thread1, arg) { (void)arg; chRegSetThreadName("blinker"); while (true) { - palToggleLine(LINE_LED6); - chThdSleepMilliseconds(250); + systime_t time; + + time = serusbcfg.usbp->state == USB_ACTIVE ? 250 : 500; + palClearLine(LINE_LED6); + chThdSleepMilliseconds(time); + palSetLine(LINE_LED6); + chThdSleepMilliseconds(time); } } @@ -84,9 +186,7 @@ int main(void) { halInit(); chSysInit(); - /* - * Initializes a serial-over-USB CDC driver. - */ + /* Initializes a serial-over-USB CDC driver.*/ sduObjectInit(&SDU1); sduStart(&SDU1, &serusbcfg); @@ -99,38 +199,28 @@ int main(void) { chThdSleepMilliseconds(1500); usbStart(serusbcfg.usbp, &usbcfg); usbConnectBus(serusbcfg.usbp); - /* - * Creates the blinker thread. - */ + + /* Creates the blinker thread.*/ chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO + 1, Thread1, NULL); - /* - * LIS3DSH Object Initialization - */ + /* LIS3DSH Object Initialization.*/ lis3dshObjectInit(&LIS3DSHD1); - /* - * Activates the LIS3DSH driver. - */ + /* Activates the LIS3DSH driver.*/ lis3dshStart(&LIS3DSHD1, &lis3dshcfg); - while (TRUE) { - palToggleLine(LINE_LED3); - accelerometerReadRaw(&LIS3DSHD1, rawdata); - for(i = 0; i < LIS3DSH_NUMBER_OF_AXES; i++) - chprintf(chp, "RAW-%c:%d\r\n", axesID[i], rawdata[i]); - - accelerometerReadCooked(&LIS3DSHD1, cookeddata); - for(i = 0; i < LIS3DSH_NUMBER_OF_AXES; i++) - chprintf(chp, "COOKED-%c:%.3f g\r\n", axesID[i], cookeddata[i]); - - accelerometerGetTemp(&LIS3DSHD1, &temperature); - chprintf(chp, "TEMP:%d \r\n", temperature); - - chThdSleepMilliseconds(150); -#if CHPRINTF_USE_ANSI_CODE - chprintf(chp, "\033[2J\033[1;1H"); -#endif + /* Shell manager initialization.*/ + shellInit(); + + 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. */ + } + chThdSleepMilliseconds(1000); } lis3dshStop(&LIS3DSHD1); + return 0; } -- cgit v1.2.3