Updated STM32L4xx/SPI-L3GD20 demo. Cosmetic changes to L3GD20 demos

git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@11655 110e8d01-0319-4d1e-a829-52ad28d1bb01
This commit is contained in:
Rocco Marco Guglielmi 2018-03-09 08:02:15 +00:00
parent afefa3bffd
commit c72c670130
5 changed files with 51 additions and 195 deletions

View File

@ -95,29 +95,19 @@ int main(void) {
halInit(); halInit();
chSysInit(); chSysInit();
/* /* Activates the serial driver 1 using the driver default configuration.*/
* Activates the serial driver 1 using the driver default configuration.
*/
sdStart(&SD1, NULL); sdStart(&SD1, NULL);
/* /* Creates the blinker thread.*/
* Creates the blinker thread.
*/
chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
/* /* L3GD20 Object Initialization.*/
* L3GD20 Object Initialization
*/
l3gd20ObjectInit(&L3GD20D1); l3gd20ObjectInit(&L3GD20D1);
/* /* Activates the L3GD20 driver.*/
* Activates the L3GD20 driver.
*/
l3gd20Start(&L3GD20D1, &l3gd20cfg); l3gd20Start(&L3GD20D1, &l3gd20cfg);
/* /* Normal main() thread activity, printing MEMS data on the SD1.*/
* Normal main() thread activity, printing MEMS data on the serial driver 1.
*/
while (true) { while (true) {
l3gd20GyroscopeReadRaw(&L3GD20D1, gyroraw); l3gd20GyroscopeReadRaw(&L3GD20D1, gyroraw);
chprintf(chp, "L3GD20 Gyroscope raw data...\r\n"); chprintf(chp, "L3GD20 Gyroscope raw data...\r\n");

View File

@ -63,7 +63,7 @@ static L3GD20Config l3gd20cfg = {
}; };
/*===========================================================================*/ /*===========================================================================*/
/* Main code. */ /* Generic code. */
/*===========================================================================*/ /*===========================================================================*/
static BaseSequentialStream* chp = (BaseSequentialStream*)&SDU1; static BaseSequentialStream* chp = (BaseSequentialStream*)&SDU1;
@ -116,22 +116,15 @@ int main(void) {
usbConnectBus(serusbcfg.usbp); usbConnectBus(serusbcfg.usbp);
/* Creates the blinker thread.*/ /* Creates the blinker thread.*/
chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO + 1, Thread1, NULL); chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
/* /* L3GD20 Object Initialization.*/
* L3GD20 Object Initialization
*/
l3gd20ObjectInit(&L3GD20D1); l3gd20ObjectInit(&L3GD20D1);
/* /* Activates the L3GD20 driver.*/
* Activates the L3GD20 driver.
*/
l3gd20Start(&L3GD20D1, &l3gd20cfg); l3gd20Start(&L3GD20D1, &l3gd20cfg);
/* /* Normal main() thread activity, printing MEMS data on the SDU1.*/
* Normal main() thread activity, printing MEMS data on the serial over usb
* driver 1.
*/
while (true) { while (true) {
l3gd20GyroscopeReadRaw(&L3GD20D1, gyroraw); l3gd20GyroscopeReadRaw(&L3GD20D1, gyroraw);
chprintf(chp, "L3GD20 Gyroscope raw data...\r\n"); chprintf(chp, "L3GD20 Gyroscope raw data...\r\n");

View File

@ -5,7 +5,7 @@
# Compiler options here. # Compiler options here.
ifeq ($(USE_OPT),) ifeq ($(USE_OPT),)
USE_OPT = -O0 -ggdb -fomit-frame-pointer -falign-functions=16 USE_OPT = -O2 -ggdb -fomit-frame-pointer -falign-functions=16
endif endif
# C specific options here (added to USE_OPT). # C specific options here (added to USE_OPT).
@ -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 = no USE_FPU = hard
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_stm32l4xx.mk include $(CHIBIOS)/os/common/startup/ARMCMx/compilers/GCC/mk/startup_stm32l4xx.mk
# HAL-OSAL files (optional). # HAL-OSAL files (optional).
@ -97,31 +100,23 @@ 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/l3gd20.mk include $(CHIBIOS)/os/ex/ST/l3gd20.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)/STM32L476xG.ld LDSCRIPT= $(STARTUPLD)/STM32L476xG.ld
# 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.
CSRC = $(STARTUPSRC) \ CSRC = $(ALLCSRC) \
$(KERNSRC) \ $(TESTSRC) \
$(PORTSRC) \
$(OSALSRC) \
$(HALSRC) \
$(PLATFORMSRC) \
$(BOARDSRC) \
$(L3GD20SRC) \
$(STREAMSSRC) \
$(SHELLSRC) \
main.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
@ -144,13 +139,10 @@ TCSRC =
TCPPSRC = TCPPSRC =
# List ASM source files here # List ASM source files here
ASMSRC = ASMSRC = $(ALLASMSRC)
ASMXSRC = $(STARTUPASM) $(PORTASM) $(OSALASM) ASMXSRC = $(ALLXASMSRC)
INCDIR = $(CHIBIOS)/os/license \ INCDIR = $(ALLINC) $(TESTINC)
$(STARTUPINC) $(KERNINC) $(PORTINC) $(OSALINC) \
$(HALINC) $(PLATFORMINC) $(BOARDINC) $(L3GD20INC) \
$(STREAMSINC) $(SHELLINC)
# #
# Project, sources and paths # Project, sources and paths
@ -200,7 +192,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 \
-DL3GD20_USE_ADVANCED=0 -DL3GD20_SHARED_SPI=0 -DL3GD20_USE_ADVANCED=0 -DL3GD20_SHARED_SPI=0
# Define ASM defines here # Define ASM defines here

View File

@ -17,12 +17,11 @@
#include "ch.h" #include "ch.h"
#include "hal.h" #include "hal.h"
#include "string.h"
#include "shell.h"
#include "chprintf.h" #include "chprintf.h"
#include "l3gd20.h" #include "l3gd20.h"
#define cls(chp) chprintf(chp, "\033[2J\033[1;1H")
/*===========================================================================*/ /*===========================================================================*/
/* L3GD20 related. */ /* L3GD20 related. */
/*===========================================================================*/ /*===========================================================================*/
@ -30,10 +29,10 @@
/* L3GD20 Driver: This object represent an L3GD20 instance.*/ /* L3GD20 Driver: This object represent an L3GD20 instance.*/
static L3GD20Driver L3GD20D1; static L3GD20Driver L3GD20D1;
static int32_t rawdata[L3GD20_NUMBER_OF_AXES]; static int32_t gyroraw[L3GD20_GYRO_NUMBER_OF_AXES];
static float cookeddata[L3GD20_NUMBER_OF_AXES]; static float gyrocooked[L3GD20_GYRO_NUMBER_OF_AXES];
static char axisID[L3GD20_NUMBER_OF_AXES] = {'X', 'Y', 'Z'}; static char axisID[L3GD20_GYRO_NUMBER_OF_AXES] = {'X', 'Y', 'Z'};
static uint32_t i; static uint32_t i;
static const SPIConfig spicfg = { static const SPIConfig spicfg = {
@ -63,134 +62,12 @@ static L3GD20Config l3gd20cfg = {
}; };
/*===========================================================================*/ /*===========================================================================*/
/* Command line related. */ /* Generic code. */
/*===========================================================================*/ /*===========================================================================*/
static BaseSequentialStream* chp = (BaseSequentialStream*)&SD2;
/* /*
* DP resistor control is not possible on the STM32F3-Discovery, using stubs * Red LED blinker thread, times are in milliseconds.
* for the connection macros.
*/
#define usb_lld_connect_bus(usbp)
#define usb_lld_disconnect_bus(usbp)
/* 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
gyroscopeReadRaw(&L3GD20D1, rawdata);
chprintf(chp, "L3GD20 Gyroscope raw data...\r\n");
for(i = 0; i < L3GD20_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
gyroscopeReadCooked(&L3GD20D1, cookeddata);
chprintf(chp, "L3GD20 Gyroscope cooked data...\r\n");
for(i = 0; i < L3GD20_NUMBER_OF_AXES; i++) {
chprintf(chp, "%c-axis: %.4f DPS\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 [250|500|2000]\r\n");
return;
}
#if CHPRINTF_USE_ANSI_CODE
chprintf(chp, "\033[2J\033[1;1H");
#endif
if(!strcmp (argv[0], "250")) {
gyroscopeSetFullScale(&L3GD20D1, L3GD20_FS_250DPS);
chprintf(chp, "L3GD20 Gyroscope full scale set to 250 dps...\r\n");
}
else if(!strcmp (argv[0], "500")) {
gyroscopeSetFullScale(&L3GD20D1, L3GD20_FS_500DPS);
chprintf(chp, "L3GD20 Gyroscope full scale set to 500 dps...\r\n");
}
else if(!strcmp (argv[0], "2000")) {
gyroscopeSetFullScale(&L3GD20D1, L3GD20_FS_2000DPS);
chprintf(chp, "L3GD20 Gyroscope full scale set to 2000 dps...\r\n");
}
else {
chprintf(chp, "Usage: fullscale [250|500|2000]\r\n");
return;
}
}
static void cmd_bias(BaseSequentialStream *chp, int argc, char *argv[]) {
(void)argv;
if (argc != 1) {
chprintf(chp, "Usage: bias [sample|reset]\r\n");
return;
}
if(!strcmp (argv[0], "sample")) {
#if CHPRINTF_USE_ANSI_CODE
chprintf(chp, "\033[2J\033[1;1H");
#endif
chprintf(chp, "Please don't move the device while Green LED is on!\r\n");
chprintf(chp, "Press a key to start...\r\n");
while (chnGetTimeout((BaseChannel *)chp, 500) == Q_TIMEOUT)
;
palSetLine(LINE_LED_GREEN);
chThdSleepMilliseconds(1000);
gyroscopeSampleBias(&L3GD20D1);
palClearLine(LINE_LED_GREEN);
chprintf(chp, "Procedure completed!\r\n");
}
else if(!strcmp (argv[0], "reset")) {
#if CHPRINTF_USE_ANSI_CODE
chprintf(chp, "\033[2J\033[1;1H");
#endif
gyroscopeResetBias(&L3GD20D1);
chprintf(chp, "Bias correction removed!\r\n");
}
else {
chprintf(chp, "Usage: bias [sample|reset]\r\n");
return;
}
}
static const ShellCommand commands[] = {
{"read", cmd_read},
{"fullscale", cmd_fullscale},
{"bias", cmd_bias},
{NULL, NULL}
};
static const ShellConfig shell_cfg1 = {
(BaseSequentialStream *)&SD2,
commands
};
/*===========================================================================*/
/* Main code. */
/*===========================================================================*/
/*
* LED blinker thread, times are in milliseconds.
*/ */
static THD_WORKING_AREA(waThread1, 128); static THD_WORKING_AREA(waThread1, 128);
static THD_FUNCTION(Thread1, arg) { static THD_FUNCTION(Thread1, arg) {
@ -198,10 +75,8 @@ static THD_FUNCTION(Thread1, arg) {
(void)arg; (void)arg;
chRegSetThreadName("blinker"); chRegSetThreadName("blinker");
while (true) { while (true) {
palClearLine(LINE_LED_RED); palToggleLine(LINE_LED_RED);
chThdSleepMilliseconds(500); chThdSleepMilliseconds(250);
palSetLine(LINE_LED_RED);
chThdSleepMilliseconds(500);
} }
} }
@ -232,15 +107,22 @@ int main(void) {
/* Activates the L3GD20 driver.*/ /* Activates the L3GD20 driver.*/
l3gd20Start(&L3GD20D1, &l3gd20cfg); l3gd20Start(&L3GD20D1, &l3gd20cfg);
/* Shell manager initialization.*/ /* Normal main() thread activity, printing MEMS data on the SD2.*/
shellInit(); while (true) {
l3gd20GyroscopeReadRaw(&L3GD20D1, gyroraw);
chprintf(chp, "L3GD20 Gyroscope raw data...\r\n");
for(i = 0; i < L3GD20_GYRO_NUMBER_OF_AXES; i++) {
chprintf(chp, "%c-axis: %d\r\n", axisID[i], gyroraw[i]);
}
while(TRUE) { l3gd20GyroscopeReadCooked(&L3GD20D1, gyrocooked);
thread_t *shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE, chprintf(chp, "L3GD20 Gyroscope cooked data...\r\n");
"shell", NORMALPRIO + 1, for(i = 0; i < L3GD20_GYRO_NUMBER_OF_AXES; i++) {
shellThread, (void *)&shell_cfg1); chprintf(chp, "%c-axis: %.3f\r\n", axisID[i], gyrocooked[i]);
chThdWait(shelltp); /* Waiting termination. */ }
chThdSleepMilliseconds(100);
cls(chp);
} }
l3gd20Stop(&L3GD20D1); l3gd20Stop(&L3GD20D1);
return 0;
} }

View File

@ -8,8 +8,7 @@ The demo runs on an STM32L4 Discovery board.
** The Demo ** ** The Demo **
The demo uses the ChibiOS Shell in order to test L3GD20 APIs. The demo prints L3GD20 data on the SD2 BaseSequentialStream.
Shell is used on the SDU1 BaseSequentialStream.
** Build Procedure ** ** Build Procedure **