From b89c65bd8d6fa479b2e0a01e873fef2220ce563e Mon Sep 17 00:00:00 2001 From: gdisirio Date: Sun, 1 Jan 2012 13:16:19 +0000 Subject: Added MEMS support to the STM32F4-Discovery demo. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@3702 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- demos/ARMCM4-STM32F407-DISCOVERY/Makefile | 4 +-- demos/ARMCM4-STM32F407-DISCOVERY/main.c | 42 ++++++++++++++++++++++++++---- demos/ARMCM4-STM32F407-DISCOVERY/mcuconf.h | 2 +- 3 files changed, 40 insertions(+), 8 deletions(-) (limited to 'demos') diff --git a/demos/ARMCM4-STM32F407-DISCOVERY/Makefile b/demos/ARMCM4-STM32F407-DISCOVERY/Makefile index fbd0c2245..975e5f999 100644 --- a/demos/ARMCM4-STM32F407-DISCOVERY/Makefile +++ b/demos/ARMCM4-STM32F407-DISCOVERY/Makefile @@ -78,8 +78,8 @@ CSRC = $(PORTSRC) \ $(HALSRC) \ $(PLATFORMSRC) \ $(BOARDSRC) \ - $(CHIBIOS)/os/various/evtimer.c \ - $(CHIBIOS)/os/various/syscalls.c \ + $(CHIBIOS)/os/various/lis302dl.c \ + $(CHIBIOS)/os/various/chprintf.c \ main.c # C++ sources that can be compiled in ARM or THUMB mode depending on the global diff --git a/demos/ARMCM4-STM32F407-DISCOVERY/main.c b/demos/ARMCM4-STM32F407-DISCOVERY/main.c index 161bc63b5..d8b72e3f7 100644 --- a/demos/ARMCM4-STM32F407-DISCOVERY/main.c +++ b/demos/ARMCM4-STM32F407-DISCOVERY/main.c @@ -21,6 +21,8 @@ #include "ch.h" #include "hal.h" #include "test.h" +#include "lis302dl.h" +#include "chprintf.h" static void pwmpcb(PWMDriver *pwmp); static void adccb(ADCDriver *adcp, adcsample_t *buffer, size_t n); @@ -78,11 +80,24 @@ static PWMConfig pwmcfg = { }; /* - * SPI configuration structure. - * Maximum speed (12MHz), CPHA=0, CPOL=0, 16bits frames, MSb transmitted first. - * The slave select line is the pin GPIOA_SPI1NSS on the port GPIOA. + * SPI1 configuration structure. + * Speed 5.25MHz, CPHA=1, CPOL=1, 8bits frames, MSb transmitted first. + * The slave select line is the pin GPIOE_CS_SPI on the port GPIOE. */ -static const SPIConfig spicfg = { +static const SPIConfig spi1cfg = { + NULL, + /* HW dependent part.*/ + GPIOE, + GPIOE_CS_SPI, + SPI_CR1_BR_0 | SPI_CR1_BR_1 | SPI_CR1_CPOL | SPI_CR1_CPHA +}; + +/* + * SPI2 configuration structure. + * Speed 21MHz, CPHA=0, CPOL=0, 16bits frames, MSb transmitted first. + * The slave select line is the pin 12 on the port GPIOA. + */ +static const SPIConfig spi2cfg = { spicb, /* HW dependent part.*/ GPIOB, @@ -204,7 +219,7 @@ int main(void) { * PB14 - MISO. * PB15 - MOSI. */ - spiStart(&SPID2, &spicfg); + spiStart(&SPID2, &spi2cfg); palSetPad(GPIOB, 12); palSetPadMode(GPIOB, 12, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_HIGHEST); /* NSS. */ @@ -234,6 +249,16 @@ int main(void) { */ chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); + /* + * Initializes the SPI driver 1 in order to access the MEMS. The signals + * are initialized in the board file. + * Several LIS302DL registers are then initialized. + */ + spiStart(&SPID1, &spi1cfg); + lis302dlWriteRegister(&SPID1, LIS302DL_CTRL_REG1, 0x43); + lis302dlWriteRegister(&SPID1, LIS302DL_CTRL_REG2, 0x00); + lis302dlWriteRegister(&SPID1, LIS302DL_CTRL_REG3, 0x00); + /* * Normal main() thread activity, in this demo it does nothing except * sleeping in a loop and check the button state, when the button is @@ -241,8 +266,15 @@ int main(void) { * driver 2. */ while (TRUE) { + uint8_t x, y, z; + if (palReadPad(GPIOA, GPIOA_BUTTON)) TestThread(&SD2); + + x = lis302dlReadRegister(&SPID1, LIS302DL_OUTX); + y = lis302dlReadRegister(&SPID1, LIS302DL_OUTY); + z = lis302dlReadRegister(&SPID1, LIS302DL_OUTZ); + chprintf((BaseChannel *)&SD2, "%d, %d, %d\r\n", x, y, z); chThdSleepMilliseconds(500); } } diff --git a/demos/ARMCM4-STM32F407-DISCOVERY/mcuconf.h b/demos/ARMCM4-STM32F407-DISCOVERY/mcuconf.h index ba8c322fd..73999ec6b 100644 --- a/demos/ARMCM4-STM32F407-DISCOVERY/mcuconf.h +++ b/demos/ARMCM4-STM32F407-DISCOVERY/mcuconf.h @@ -173,7 +173,7 @@ /* * SPI driver system settings. */ -#define STM32_SPI_USE_SPI1 FALSE +#define STM32_SPI_USE_SPI1 TRUE #define STM32_SPI_USE_SPI2 TRUE #define STM32_SPI_USE_SPI3 FALSE #define STM32_SPI_SPI1_RX_DMA_STREAM STM32_DMA_STREAM_ID(2, 0) -- cgit v1.2.3