From 962a3cb36e19f9d45974269dd686ec7dc54583ae Mon Sep 17 00:00:00 2001 From: Rocco Marco Guglielmi Date: Thu, 8 Mar 2018 10:45:49 +0000 Subject: Updated STM32F4xx/I2C-LSM303DLHC git-svn-id: https://svn.code.sf.net/p/chibios/svn2/trunk@11630 110e8d01-0319-4d1e-a829-52ad28d1bb01 --- testex/STM32/STM32F4xx/I2C-LSM303DLHC/Makefile | 27 +-- testex/STM32/STM32F4xx/I2C-LSM303DLHC/main.c | 279 ++++-------------------- testex/STM32/STM32F4xx/I2C-LSM303DLHC/mcuconf.h | 38 ++-- 3 files changed, 74 insertions(+), 270 deletions(-) (limited to 'testex') diff --git a/testex/STM32/STM32F4xx/I2C-LSM303DLHC/Makefile b/testex/STM32/STM32F4xx/I2C-LSM303DLHC/Makefile index 37305ed89..363d8023c 100644 --- a/testex/STM32/STM32F4xx/I2C-LSM303DLHC/Makefile +++ b/testex/STM32/STM32F4xx/I2C-LSM303DLHC/Makefile @@ -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,31 +100,24 @@ 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/lsm303dlhc.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)/STM32F401xC.ld # C sources that can be compiled in ARM or THUMB mode depending on the global # setting. -CSRC = $(STARTUPSRC) \ - $(KERNSRC) \ - $(PORTSRC) \ - $(OSALSRC) \ - $(HALSRC) \ - $(PLATFORMSRC) \ - $(BOARDSRC) \ - $(LSM303DLHCSRC) \ - $(STREAMSSRC) \ - $(SHELLSRC) \ +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 +143,7 @@ TCPPSRC = ASMSRC = ASMXSRC = $(STARTUPASM) $(PORTASM) $(OSALASM) -INCDIR = $(CHIBIOS)/os/license \ - $(STARTUPINC) $(KERNINC) $(PORTINC) $(OSALINC) \ - $(HALINC) $(PLATFORMINC) $(BOARDINC) $(LSM303DLHCINC) \ - $(STREAMSINC) $(SHELLINC) +INCDIR = $(ALLINC) $(TESTINC) # # Project, sources and paths diff --git a/testex/STM32/STM32F4xx/I2C-LSM303DLHC/main.c b/testex/STM32/STM32F4xx/I2C-LSM303DLHC/main.c index d61ba9449..baaef81ad 100644 --- a/testex/STM32/STM32F4xx/I2C-LSM303DLHC/main.c +++ b/testex/STM32/STM32F4xx/I2C-LSM303DLHC/main.c @@ -18,23 +18,23 @@ #include "hal.h" #include "usbcfg.h" -#include "string.h" -#include "shell.h" #include "chprintf.h" - #include "lsm303dlhc.h" +#define cls(chp) chprintf(chp, "\033[2J\033[1;1H") + /*===========================================================================*/ -/* LSM303DLHC related. */ +/* LSM303DLHC related. */ /*===========================================================================*/ /* LSM303DLHC Driver: This object represent an LSM303DLHC instance */ static LSM303DLHCDriver LSM303DLHCD1; -static int32_t rawdata[LSM303DLHC_ACC_NUMBER_OF_AXES + - LSM303DLHC_COMP_NUMBER_OF_AXES]; -static float cookeddata[LSM303DLHC_ACC_NUMBER_OF_AXES + - LSM303DLHC_COMP_NUMBER_OF_AXES]; +static int32_t accraw[LSM303DLHC_ACC_NUMBER_OF_AXES]; +static int32_t compraw[LSM303DLHC_COMP_NUMBER_OF_AXES]; + +static float acccooked[LSM303DLHC_ACC_NUMBER_OF_AXES]; +static float compcooked[LSM303DLHC_COMP_NUMBER_OF_AXES]; static char axisID[LSM303DLHC_ACC_NUMBER_OF_AXES] = {'X', 'Y', 'Z'}; static uint32_t i; @@ -45,7 +45,9 @@ static const I2CConfig i2ccfg = { FAST_DUTY_CYCLE_2, }; -static const LSM303DLHCAccConfig lsm303dlhcacccfg = { +static const LSM303DLHCConfig lsm303dlhccfg = { + &I2CD1, + &i2ccfg, NULL, NULL, LSM303DLHC_ACC_FS_4G, @@ -56,226 +58,20 @@ static const LSM303DLHCAccConfig lsm303dlhcacccfg = { LSM303DLHC_ACC_BDU_BLOCK, LSM303DLHC_ACC_END_LITTLE, #endif -}; - -static const LSM303DLHCCompConfig lsm303dlhccompcfg = { - NULL, /* Use default sensitivity.*/ - NULL, /* Use default bias.*/ - LSM303DLHC_COMP_FS_1P3GA, /* Full scale value 1.3 Gauss.*/ - LSM303DLHC_COMP_ODR_30HZ, /* Output data rate 30 Hz.*/ -#if LSM303DLHC_COMP_USE_ADVANCED || defined(__DOXYGEN__) + NULL, + NULL, + LSM303DLHC_COMP_FS_1P3GA, + LSM303DLHC_COMP_ODR_30HZ, +#if LSM303DLHC_COMP_USE_ADVANCED LSM303DLHC_COMP_MD_BLOCK #endif }; -static const LSM303DLHCConfig lsm303dlhccfg = { - &I2CD1, - &i2ccfg, - &lsm303dlhcacccfg, - &lsm303dlhccompcfg -}; - -/*===========================================================================*/ -/* 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 != 2) { - chprintf(chp, "Usage: read [acc|comp|both] [raw|cooked]\r\n"); - return; - } - - while (chnGetTimeout((BaseChannel *)chp, 150) == Q_TIMEOUT) { - if (!strcmp (argv[0], "acc")) { - if (!strcmp (argv[1], "raw")) { -#if CHPRINTF_USE_ANSI_CODE - chprintf(chp, "\033[2J\033[1;1H"); -#endif - accelerometerReadRaw(&LSM303DLHCD1, rawdata); - chprintf(chp, "LSM303DLHC Accelerometer raw data...\r\n"); - for(i = 0; i < LSM303DLHC_ACC_NUMBER_OF_AXES; i++) { - chprintf(chp, "%c-axis: %d\r\n", axisID[i], rawdata[i]); - } - } - else if (!strcmp (argv[1], "cooked")) { -#if CHPRINTF_USE_ANSI_CODE - chprintf(chp, "\033[2J\033[1;1H"); -#endif - accelerometerReadCooked(&LSM303DLHCD1, cookeddata); - chprintf(chp, "LSM303DLHC Accelerometer cooked data...\r\n"); - for(i = 0; i < LSM303DLHC_ACC_NUMBER_OF_AXES; i++) { - chprintf(chp, "%c-axis: %.4f mG\r\n", axisID[i], cookeddata[i]); - } - } - else { - chprintf(chp, "Usage: read [acc|comp|both] [raw|cooked]\r\n"); - return; - } - } - else if (!strcmp (argv[0], "comp")) { - if (!strcmp (argv[1], "raw")) { -#if CHPRINTF_USE_ANSI_CODE - chprintf(chp, "\033[2J\033[1;1H"); -#endif - compassReadRaw(&LSM303DLHCD1, rawdata); - chprintf(chp, "LSM303DLHC Compass raw data...\r\n"); - for(i = 0; i < LSM303DLHC_COMP_NUMBER_OF_AXES; i++) { - chprintf(chp, "%c-axis: %d\r\n", axisID[i], rawdata[i]); - } - } - else if (!strcmp (argv[1], "cooked")) { -#if CHPRINTF_USE_ANSI_CODE - chprintf(chp, "\033[2J\033[1;1H"); -#endif - compassReadCooked(&LSM303DLHCD1, cookeddata); - chprintf(chp, "LSM303DLHC Compass cooked data...\r\n"); - for(i = 0; i < LSM303DLHC_COMP_NUMBER_OF_AXES; i++) { - chprintf(chp, "%c-axis: %.4f Gauss\r\n", axisID[i], cookeddata[i]); - } - } - else { - chprintf(chp, "Usage: read [acc|comp|both] [raw|cooked]\r\n"); - return; - } - } - else if (!strcmp (argv[0], "both")) { - if (!strcmp (argv[1], "raw")) { -#if CHPRINTF_USE_ANSI_CODE - chprintf(chp, "\033[2J\033[1;1H"); -#endif - sensorReadRaw(&LSM303DLHCD1, rawdata); - chprintf(chp, "LSM303DLHC Accelerometer raw data...\r\n"); - for(i = 0; i < LSM303DLHC_ACC_NUMBER_OF_AXES; i++) { - chprintf(chp, "%c-axis: %d\r\n", axisID[i], rawdata[i]); - } - chprintf(chp, "LSM303DLHC Compass raw data...\r\n"); - for(i = 0; i < LSM303DLHC_COMP_NUMBER_OF_AXES; i++) { - chprintf(chp, "%c-axis: %d\r\n", axisID[i], - rawdata[i + LSM303DLHC_ACC_NUMBER_OF_AXES]); - } - } - else if (!strcmp (argv[1], "cooked")) { -#if CHPRINTF_USE_ANSI_CODE - chprintf(chp, "\033[2J\033[1;1H"); -#endif - sensorReadCooked(&LSM303DLHCD1, cookeddata); - chprintf(chp, "LSM303DLHC Accelerometer cooked data...\r\n"); - for(i = 0; i < LSM303DLHC_ACC_NUMBER_OF_AXES; i++) { - chprintf(chp, "%c-axis: %.4f mG\r\n", axisID[i], cookeddata[i]); - } - chprintf(chp, "LSM303DLHC Compass cooked data...\r\n"); - for(i = 0; i < LSM303DLHC_COMP_NUMBER_OF_AXES; i++) { - chprintf(chp, "%c-axis: %.4f Gauss\r\n", axisID[i], - cookeddata[i + LSM303DLHC_ACC_NUMBER_OF_AXES]); - } - } - else { - chprintf(chp, "Usage: read [acc|comp|both] [raw|cooked]\r\n"); - return; - } - } - else { - chprintf(chp, "Usage: read [acc|comp|both] [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 [acc|comp] [value]\r\n"); - return; - } - if (!strcmp (argv[0], "acc")) { -#if CHPRINTF_USE_ANSI_CODE - chprintf(chp, "\033[2J\033[1;1H"); -#endif - if(!strcmp (argv[1], "2G")) { - accelerometerSetFullScale(&LSM303DLHCD1, LSM303DLHC_ACC_FS_2G); - chprintf(chp, "LSM303DLHC Accelerometer full scale set to 2G...\r\n"); - } - else if(!strcmp (argv[1], "4G")) { - accelerometerSetFullScale(&LSM303DLHCD1, LSM303DLHC_ACC_FS_4G); - chprintf(chp, "LSM303DLHC Accelerometer full scale set to 4G...\r\n"); - } - else if(!strcmp (argv[1], "8G")) { - accelerometerSetFullScale(&LSM303DLHCD1, LSM303DLHC_ACC_FS_8G); - chprintf(chp, "LSM303DLHC Accelerometer full scale set to 8G...\r\n"); - } - else if(!strcmp (argv[1], "16G")) { - accelerometerSetFullScale(&LSM303DLHCD1, LSM303DLHC_ACC_FS_16G); - chprintf(chp, "LSM303DLHC Accelerometer full scale set to 16G...\r\n"); - } - else { - chprintf(chp, "Usage: fullscale acc [2G|4G|8G|16G]\r\n"); - return; - } - } - else if (!strcmp (argv[0], "comp")) { -#if CHPRINTF_USE_ANSI_CODE - chprintf(chp, "\033[2J\033[1;1H"); -#endif - if(!strcmp (argv[1], "1.3")) { - compassSetFullScale(&LSM303DLHCD1, LSM303DLHC_COMP_FS_1P3GA); - chprintf(chp, "LSM303DLHC Compass full scale set to 1.3 Gauss...\r\n"); - } - else if(!strcmp (argv[1], "1.9")) { - compassSetFullScale(&LSM303DLHCD1, LSM303DLHC_COMP_FS_1P9GA); - chprintf(chp, "LSM303DLHC Compass full scale set to 1.9 Gauss...\r\n"); - } - else if(!strcmp (argv[1], "2.5")) { - compassSetFullScale(&LSM303DLHCD1, LSM303DLHC_COMP_FS_2P5GA); - chprintf(chp, "LSM303DLHC Compass full scale set to 2.5 Gauss...\r\n"); - } - else if(!strcmp (argv[1], "4.0")) { - compassSetFullScale(&LSM303DLHCD1, LSM303DLHC_COMP_FS_4P0GA); - chprintf(chp, "LSM303DLHC Compass full scale set to 4.0 Gauss...\r\n"); - } - else if(!strcmp (argv[1], "4.7")) { - compassSetFullScale(&LSM303DLHCD1, LSM303DLHC_COMP_FS_4P7GA); - chprintf(chp, "LSM303DLHC Compass full scale set to 4.7 Gauss...\r\n"); - } - else if(!strcmp (argv[1], "5.6")) { - compassSetFullScale(&LSM303DLHCD1, LSM303DLHC_COMP_FS_5P6GA); - chprintf(chp, "LSM303DLHC Compass full scale set to 5.6 Gauss...\r\n"); - } - else if(!strcmp (argv[1], "8.1")) { - compassSetFullScale(&LSM303DLHCD1, LSM303DLHC_COMP_FS_8P1GA); - chprintf(chp, "LSM303DLHC Compass full scale set to 8.1 Gauss...\r\n"); - } - else { - chprintf(chp, "Usage: fullscale comp [1.3|1.9|2.5|4.0|4.7|5.6|8.1]\r\n"); - return; - } - } - else { - chprintf(chp, "Usage: fullscale [acc|comp] [value]\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. */ @@ -288,9 +84,7 @@ static THD_FUNCTION(Thread1, arg) { systime_t time; time = serusbcfg.usbp->state == USB_ACTIVE ? 250 : 500; - palClearLine(LINE_LED5); - chThdSleepMilliseconds(time); - palSetLine(LINE_LED5); + palToggleLine(LINE_LED5); chThdSleepMilliseconds(time); } } @@ -342,19 +136,34 @@ int main(void) { lsm303dlhcStart(&LSM303DLHCD1, &lsm303dlhccfg); /* - * Shell manager initialization. + * Normal main() thread activity, printing MEMS data on the serial driver 1. */ - shellInit(); + while (true) { + lsm303dlhcAccelerometerReadRaw(&LSM303DLHCD1, accraw); + chprintf(chp, "LSM303DLHC Accelerometer raw data...\r\n"); + for(i = 0; i < LSM303DLHC_ACC_NUMBER_OF_AXES; i++) { + chprintf(chp, "%c-axis: %d\r\n", axisID[i], accraw[i]); + } + + lsm303dlhcCompassReadRaw(&LSM303DLHCD1, compraw); + chprintf(chp, "LSM303DLHC Compass raw data...\r\n"); + for(i = 0; i < LSM303DLHC_COMP_NUMBER_OF_AXES; i++) { + chprintf(chp, "%c-axis: %d\r\n", axisID[i], compraw[i]); + } + + lsm303dlhcAccelerometerReadCooked(&LSM303DLHCD1, acccooked); + chprintf(chp, "LSM303DLHC Accelerometer cooked data...\r\n"); + for(i = 0; i < LSM303DLHC_ACC_NUMBER_OF_AXES; i++) { + chprintf(chp, "%c-axis: %.3f\r\n", axisID[i], acccooked[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. */ + lsm303dlhcCompassReadCooked(&LSM303DLHCD1, compcooked); + chprintf(chp, "LSM303DLHC Compass cooked data...\r\n"); + for(i = 0; i < LSM303DLHC_COMP_NUMBER_OF_AXES; i++) { + chprintf(chp, "%c-axis: %.3f\r\n", axisID[i], compcooked[i]); } - chThdSleepMilliseconds(1000); + chThdSleepMilliseconds(100); + cls(chp); } lsm303dlhcStop(&LSM303DLHCD1); - return 0; } diff --git a/testex/STM32/STM32F4xx/I2C-LSM303DLHC/mcuconf.h b/testex/STM32/STM32F4xx/I2C-LSM303DLHC/mcuconf.h index 5665f3a99..02f1bca9d 100644 --- a/testex/STM32/STM32F4xx/I2C-LSM303DLHC/mcuconf.h +++ b/testex/STM32/STM32F4xx/I2C-LSM303DLHC/mcuconf.h @@ -64,32 +64,34 @@ #define STM32_PLS STM32_PLS_LEV0 #define STM32_BKPRAM_ENABLE FALSE +/* + * IRQ system settings. + */ +#define STM32_IRQ_EXTI0_PRIORITY 6 +#define STM32_IRQ_EXTI1_PRIORITY 6 +#define STM32_IRQ_EXTI2_PRIORITY 6 +#define STM32_IRQ_EXTI3_PRIORITY 6 +#define STM32_IRQ_EXTI4_PRIORITY 6 +#define STM32_IRQ_EXTI5_9_PRIORITY 6 +#define STM32_IRQ_EXTI10_15_PRIORITY 6 +#define STM32_IRQ_EXTI16_PRIORITY 6 +#define STM32_IRQ_EXTI17_PRIORITY 15 +#define STM32_IRQ_EXTI18_PRIORITY 6 +#define STM32_IRQ_EXTI19_PRIORITY 6 +#define STM32_IRQ_EXTI20_PRIORITY 6 +#define STM32_IRQ_EXTI21_PRIORITY 15 +#define STM32_IRQ_EXTI22_PRIORITY 15 + /* * ADC driver system settings. */ -#define STM32_ADC_ADCPRE ADC_CCR_ADCPRE_DIV4 +#define STM32_ADC_ADCPRE ADC_CCR_ADCPRE_DIV4 #define STM32_ADC_USE_ADC1 FALSE #define STM32_ADC_ADC1_DMA_STREAM STM32_DMA_STREAM_ID(2, 4) #define STM32_ADC_ADC1_DMA_PRIORITY 2 #define STM32_ADC_IRQ_PRIORITY 6 #define STM32_ADC_ADC1_DMA_IRQ_PRIORITY 6 -/* - * EXT driver system settings. - */ -#define STM32_EXT_EXTI0_IRQ_PRIORITY 6 -#define STM32_EXT_EXTI1_IRQ_PRIORITY 6 -#define STM32_EXT_EXTI2_IRQ_PRIORITY 6 -#define STM32_EXT_EXTI3_IRQ_PRIORITY 6 -#define STM32_EXT_EXTI4_IRQ_PRIORITY 6 -#define STM32_EXT_EXTI5_9_IRQ_PRIORITY 6 -#define STM32_EXT_EXTI10_15_IRQ_PRIORITY 6 -#define STM32_EXT_EXTI16_IRQ_PRIORITY 6 -#define STM32_EXT_EXTI17_IRQ_PRIORITY 6 -#define STM32_EXT_EXTI18_IRQ_PRIORITY 6 -#define STM32_EXT_EXTI21_IRQ_PRIORITY 6 -#define STM32_EXT_EXTI22_IRQ_PRIORITY 6 - /* * GPT driver system settings. */ @@ -190,7 +192,7 @@ /* * SPI driver system settings. */ -#define STM32_SPI_USE_SPI1 TRUE +#define STM32_SPI_USE_SPI1 FALSE #define STM32_SPI_USE_SPI2 FALSE #define STM32_SPI_USE_SPI3 FALSE #define STM32_SPI_SPI1_RX_DMA_STREAM STM32_DMA_STREAM_ID(2, 0) -- cgit v1.2.3