From bd504b071133f1536934d4c4ca1626f07937213d Mon Sep 17 00:00:00 2001 From: Thomas Pietrzak Date: Fri, 26 Dec 2014 12:53:27 +0100 Subject: [PATCH 1/1] i2cexample --- Makefile | 55 +++ i2cexample.c | 165 +++++++ lib/Makefile | 16 + lib/config.mk | 5 + lib/i2c.c | 160 +++++++ lib/i2c.h | 22 + lib/stm32f4_discovery.c | 253 ++++++++++ lib/stm32f4_discovery.h | 158 +++++++ lib/stm32f4_discovery_lis302dl.c | 502 ++++++++++++++++++++ lib/stm32f4_discovery_lis302dl.h | 772 +++++++++++++++++++++++++++++++ 10 files changed, 2108 insertions(+) create mode 100644 Makefile create mode 100644 i2cexample.c create mode 100644 lib/Makefile create mode 100644 lib/config.mk create mode 100644 lib/i2c.c create mode 100644 lib/i2c.h create mode 100755 lib/stm32f4_discovery.c create mode 100755 lib/stm32f4_discovery.h create mode 100755 lib/stm32f4_discovery_lis302dl.c create mode 100755 lib/stm32f4_discovery_lis302dl.h diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..6a6c87b --- /dev/null +++ b/Makefile @@ -0,0 +1,55 @@ +export NODEMO = true +ROOTDIR = $(CURDIR)/uC-sdk +export BOARD = stm32f4discovery + +TARGET = i2cexample.bin +TARGET_OBJS = lib/i2c.o lib/stm32f4_discovery.o lib/stm32f4_discovery_lis302dl.o + +LIBDEPS = uC-sdk/FreeRTOS/libFreeRTOS.a uC-sdk/arch/libarch.a uC-sdk/os/libos.a uC-sdk/libc/libc.a uC-sdk/libm/libm.a uC-sdk/acorn/libacorn.a +LIBS = -Wl,--start-group $(LIBDEPS) -Wl,--end-group +TARGET_INCLUDES = include + +export MAINDIR = $(CURDIR) +export ROOTDIR = $(MAINDIR)/uC-sdk/ + +include $(MAINDIR)/lib/config.mk +include $(ROOTDIR)/common.mk + +all: deps uC-sdk lib $(TARGET) + +clean: clean-generic + $(Q)rm -f $(TARGET) + $(Q)$(MAKE) $(MAKE_OPTS) -C uC-sdk clean + $(Q)$(MAKE) $(MAKE_OPTS) -C lib clean + +.PHONY: uC-sdk lib + +uC-sdk/FreeRTOS/libFreeRTOS.a: uC-sdk +uC-sdk/arch/libarch.a: uC-sdk +uC-sdk/os/libos.a: uC-sdk +uC-sdk/libc/libc.a: uC-sdk +uC-sdk/libm/libm.a: uC-sdk +uC-sdk/acorn/libacorn.a: uC-sdk + +lib: + $(E) "[MAKE] Entering lib" + $(Q)$(MAKE) $(MAKE_OPTS) -C lib + +uC-sdk: + $(E) "[MAKE] Entering uC-sdk" + $(Q)$(MAKE) $(MAKE_OPTS) -C uC-sdk + +deps: ldeps + $(E) "[DEPS] Creating dependency tree for uC-sdk" + $(Q)$(MAKE) $(MAKE_OPTS) -C uC-sdk ldeps + $(E) "[DEPS] Creating dependency tree for lib" + $(Q)$(MAKE) $(MAKE_OPTS) -C lib ldeps + +include $(ROOTDIR)/FreeRTOS/config.mk +include $(ROOTDIR)/arch/config.mk +include $(ROOTDIR)/os/config.mk +include $(ROOTDIR)/libc/config.mk +include $(ROOTDIR)/libm/config.mk +include $(ROOTDIR)/acorn/config.mk +include $(ROOTDIR)/target-rules.mk + diff --git a/i2cexample.c b/i2cexample.c new file mode 100644 index 0000000..e6111d0 --- /dev/null +++ b/i2cexample.c @@ -0,0 +1,165 @@ +/* +st-util: +openocd -f board/stm32f4discovery.cfg + +arm-none-eabi-gdbtui -ex "target extended-remote :3333" i2cexample.elf +monitor reset halt +load +continue +*/ + + + +#include +#include +#include +#include + + +#include +#include + +//Accelerometer +#include + +uint32_t pins[] = {GPIO_Pin_12, GPIO_Pin_13, GPIO_Pin_14, GPIO_Pin_15}; + +void initLed() +{ + RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); + //set led gpio to output mode + GPIO_InitTypeDef pindef; + pindef.GPIO_Mode = GPIO_Mode_OUT; + pindef.GPIO_Speed = GPIO_Speed_50MHz; + pindef.GPIO_OType = GPIO_OType_PP; + pindef.GPIO_PuPd = GPIO_PuPd_UP; + + int i; + for (i = 0 ; i < 4 ; i++) + { + pindef.GPIO_Pin = pins[i]; + GPIO_Init(GPIOD, &pindef); + } +} + + +void setLed(int n) +{ + GPIO_WriteBit(GPIOD, pins[n], Bit_SET); +} + +void resetLed(int n) +{ + GPIO_WriteBit(GPIOD, pins[n], Bit_RESET); +} + +#define THRESHOLD 200 + +void accelerometerTask() +{ + LIS302DL_InitTypeDef init; + init.Power_Mode = LIS302DL_LOWPOWERMODE_ACTIVE; + init.Output_DataRate = LIS302DL_DATARATE_100; + init.Axes_Enable = LIS302DL_XYZ_ENABLE; + init.Full_Scale = LIS302DL_FULLSCALE_2_3; + init.Self_Test = LIS302DL_SELFTEST_NORMAL; + LIS302DL_Init(&init); + + LIS302DL_InterruptConfigTypeDef initinterrupt; + initinterrupt.Latch_Request = LIS302DL_INTERRUPTREQUEST_LATCHED; + initinterrupt.SingleClick_Axes = LIS302DL_CLICKINTERRUPT_Z_ENABLE; + initinterrupt.DoubleClick_Axes = LIS302DL_DOUBLECLICKINTERRUPT_Z_ENABLE; + LIS302DL_InterruptConfig(&initinterrupt); + + vTaskDelay(30 / portTICK_RATE_MS); + + int32_t acceleration[3]; + + while (1) + { + LIS302DL_ReadACC(acceleration); + if (acceleration[0] > THRESHOLD) + { + setLed(1); + resetLed(3); + } + else if (acceleration[0] < -THRESHOLD) + { + resetLed(1); + setLed(3); + } + else + { + resetLed(1); + resetLed(3); + } + if (acceleration[1] > THRESHOLD) + { + setLed(0); + resetLed(2); + } + else if (acceleration[1] < -THRESHOLD) + { + resetLed(0); + setLed(2); + } + else + { + resetLed(0); + resetLed(2); + } + } +} + +void sleep(int duration) +{ + volatile unsigned int i; + for(i = 0 ; i < 1000 * duration ; i++); +} + +/* +static void blinkerTask(void *p) +{ + int i = 0; + printf("Start blinker task\n"); + while (1) + { + setLed(i); + vTaskDelay(500 / portTICK_RATE_MS); + resetLed(i); + vTaskDelay(500 / portTICK_RATE_MS); + i = (i + 1) % 4; + } +}*/ + +int main() +{ + init_malloc_wrapper(); + + printf("Init led\n"); + initLed(); + int cl = SystemCoreClock; + + //while (1) + int i; + for (i = 0 ; i < 4 ; i++) + { + setLed(i); + sleep(100); + } + for (i = 0 ; i < 4 ; i++) + { + resetLed(i); + sleep(100); + } + cl++; + + printf("Start blinkerTask\n"); + //xTaskCreate(blinkerTask, (const signed char *)NULL, configMINIMAL_STACK_SIZE, (void *)NULL, tskIDLE_PRIORITY, NULL); + xTaskCreate(accelerometerTask, (const signed char *)NULL, configMINIMAL_STACK_SIZE, (void *)NULL, tskIDLE_PRIORITY, NULL); + + //start task scheduler + vTaskStartScheduler(); + + return 0; +} diff --git a/lib/Makefile b/lib/Makefile new file mode 100644 index 0000000..fb62ec7 --- /dev/null +++ b/lib/Makefile @@ -0,0 +1,16 @@ +all: $(TARGET_OBJ) + +export BOARD = stm32f4discovery + +include $(ROOTDIR)/common.mk +include config.mk +include $(ROOTDIR)/arch/config.mk +include $(ROOTDIR)/FreeRTOS/config.mk + +TARGET_SRCS += i2c.c +TARGET_SRCS += stm32f4_discovery.c +TARGET_SRCS += stm32f4_discovery_lis302dl.c + +include $(ROOTDIR)/target-rules.mk + +clean: clean-generic diff --git a/lib/config.mk b/lib/config.mk new file mode 100644 index 0000000..10ef728 --- /dev/null +++ b/lib/config.mk @@ -0,0 +1,5 @@ +TARGET_INCLUDES += $(MAINDIR)/lib + +ifeq ($(USE_MPU),true) +TARGET_CPPFLAGS += -DportUSING_MPU_WRAPPERS=1 +endif diff --git a/lib/i2c.c b/lib/i2c.c new file mode 100644 index 0000000..0ae7fc5 --- /dev/null +++ b/lib/i2c.c @@ -0,0 +1,160 @@ +#include "i2c.h" +#include "task.h" +#include +#include +#include +#include + +#include + + +struct i2cInitDef_t { + // scl / sda + I2C_TypeDef * id; + uint8_t afid; + GPIO_TypeDef * tdef[2]; + GPIO_InitTypeDef gpiodef[2]; + uint16_t gpiopinsource[2]; + // i2c / gpio + volatile uint32_t * bridge[2]; + uint32_t peripheral[2]; +}; + +static struct i2cInitDef_t i2cInitDefs[2] = { + { I2C1, GPIO_AF_I2C1, { GPIOB, GPIOB }, { // I2C1 + { GPIO_Pin_6, GPIO_Mode_AF, GPIO_Speed_50MHz, GPIO_OType_OD, GPIO_PuPd_UP }, // SCL + { GPIO_Pin_7, GPIO_Mode_AF, GPIO_Speed_50MHz, GPIO_OType_OD, GPIO_PuPd_UP }, // SDA + }, {GPIO_PinSource6, GPIO_PinSource7}, {&RCC->APB1ENR, &RCC->AHB1ENR}, {RCC_APB1Periph_I2C1, RCC_AHB1Periph_GPIOB } }, + { I2C2, GPIO_AF_I2C2, { GPIOB, GPIOB }, { // I2C2 + { GPIO_Pin_10, GPIO_Mode_AF, GPIO_Speed_50MHz, GPIO_OType_OD, GPIO_PuPd_UP }, // SCL + { GPIO_Pin_11, GPIO_Mode_AF, GPIO_Speed_50MHz, GPIO_OType_OD, GPIO_PuPd_UP }, // SDA + }, {GPIO_PinSource10, GPIO_PinSource11}, {&RCC->APB1ENR, &RCC->AHB1ENR}, {RCC_APB1Periph_I2C2, RCC_AHB1Periph_GPIOB } }, +}; + + +void i2c_init(uint8_t id, uint32_t speed) +{ + if (!((id >= 1) && (id <= 3))) + return; + + struct i2cInitDef_t * i2cInitDef = i2cInitDefs + id - 1; + + int i; + for (i = 0; i < 2; i++) + *(i2cInitDef->bridge[i]) |= i2cInitDef->peripheral[i]; + + for (i = 0; i < 2; i++) + GPIO_Init(i2cInitDef->tdef[i], &i2cInitDef->gpiodef[i]); + + for (i = 0; i < 2; i++) + GPIO_PinAFConfig(i2cInitDef->tdef[i], i2cInitDef->gpiopinsource[i], i2cInitDef->afid); + + //Init I2C + I2C_InitTypeDef i2cdef; + i2cdef.I2C_Mode = I2C_Mode_I2C; + i2cdef.I2C_DutyCycle = I2C_DutyCycle_2; + i2cdef.I2C_OwnAddress1 = 0x00; + i2cdef.I2C_Ack = I2C_Ack_Enable; + i2cdef.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; + i2cdef.I2C_ClockSpeed = speed; + + I2C_Init(i2cInitDef->id, &i2cdef); + I2C_Cmd(i2cInitDef->id, ENABLE); + +#if (defined(I2C1_USE_DMA_TX) || defined(I2C1_USE_DMA_RX) || defined(I2C2_USE_DMA_TX) || defined(I2C2_USE_DMA_RX)) + i2c_dma_init(id); +#endif +} + +void i2c_dma_init(uint8_t id) +{ + +} + +void i2c_start_read(uint8_t id, uint8_t destination) +{ + if (!((id >= 1) && (id <= 3))) + return; + + struct i2cInitDef_t * i2cInitDef = i2cInitDefs + id - 1; + + I2C_GenerateSTART(i2cInitDef->id, ENABLE); + while (!I2C_CheckEvent(i2cInitDef->id, I2C_EVENT_MASTER_MODE_SELECT)); + I2C_Send7bitAddress(i2cInitDef->id, destination, I2C_Direction_Receiver); + while (!I2C_CheckEvent(i2cInitDef->id, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)); +} + +void i2c_start_write(uint8_t id, uint8_t destination) +{ + if (!((id >= 1) && (id <= 3))) + return; + + struct i2cInitDef_t * i2cInitDef = i2cInitDefs + id - 1; + + I2C_GenerateSTART(i2cInitDef->id, ENABLE); + while (!I2C_CheckEvent(i2cInitDef->id, I2C_EVENT_MASTER_MODE_SELECT)); + I2C_Send7bitAddress(i2cInitDef->id, destination, I2C_Direction_Transmitter); + while (!I2C_CheckEvent(i2cInitDef->id, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); +} + +void i2c_stop(uint8_t id) +{ + if (!((id >= 1) && (id <= 3))) + return; + + struct i2cInitDef_t * i2cInitDef = i2cInitDefs + id - 1; + + I2C_GenerateSTOP(i2cInitDef->id, ENABLE); +} + + +void i2c_read_polling(uint8_t id, uint8_t *buffer, uint8_t nb) +{ + if (!((id >= 1) && (id <= 3))) + return; + + struct i2cInitDef_t * i2cInitDef = i2cInitDefs + id - 1; + + taskENTER_CRITICAL(); + + while (nb--) + { + if (nb == 0) + I2C_AcknowledgeConfig(i2cInitDef->id, DISABLE); + + while (!I2C_CheckEvent(i2cInitDef->id, I2C_EVENT_MASTER_BYTE_RECEIVED)); + *buffer++ = I2C_ReceiveData(i2cInitDef->id); + } + + I2C_AcknowledgeConfig(i2cInitDef->id, ENABLE); + + taskEXIT_CRITICAL(); +} + +void i2c_write_polling(uint8_t id, uint8_t *buffer, uint8_t nb) +{ + if (!((id >= 1) && (id <= 3))) + return; + + struct i2cInitDef_t * i2cInitDef = i2cInitDefs + id - 1; + + taskENTER_CRITICAL(); + + while (nb--) + { + I2C_SendData(i2cInitDef->id, *buffer++); + while(!I2C_CheckEvent(i2cInitDef->id, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); + } + taskEXIT_CRITICAL(); +} + +void i2c_read_dma(uint8_t id, uint8_t *buffer, uint8_t nb) +{ + +} + +void i2c_write_dma(uint8_t id, uint8_t *buffer, uint8_t nb) +{ + +} + diff --git a/lib/i2c.h b/lib/i2c.h new file mode 100644 index 0000000..212e0c0 --- /dev/null +++ b/lib/i2c.h @@ -0,0 +1,22 @@ +#ifndef __I2C__ +#define __I2C__ + +#include "FreeRTOS.h" + +void i2c_init(uint8_t id, uint32_t speed); +void i2c_dma_init(uint8_t id); + +//call before initiating a read communication +void i2c_start_read(uint8_t id, uint8_t destination); +//call before initiating a write communication +void i2c_start_write(uint8_t id, uint8_t destination); +//call after communication ended +void i2c_stop(uint8_t id); + +void i2c_read_polling(uint8_t id, uint8_t *buffer, uint8_t nb); +void i2c_write_polling(uint8_t id, uint8_t *buffer, uint8_t nb); + +void i2c_read_dma(uint8_t id, uint8_t *buffer, uint8_t nb); +void i2c_write_dma(uint8_t id, uint8_t *buffer, uint8_t nb); + +#endif \ No newline at end of file diff --git a/lib/stm32f4_discovery.c b/lib/stm32f4_discovery.c new file mode 100755 index 0000000..fa76d3a --- /dev/null +++ b/lib/stm32f4_discovery.c @@ -0,0 +1,253 @@ +/** + ****************************************************************************** + * @file stm32f4_discovery.c + * @author MCD Application Team + * @version V1.1.0 + * @date 28-October-2011 + * @brief This file provides set of firmware functions to manage Leds and + * push-button available on STM32F4-Discovery Kit from STMicroelectronics. + ****************************************************************************** + * @attention + * + * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS + * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE + * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY + * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING + * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE + * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. + * + *

© COPYRIGHT 2011 STMicroelectronics

+ ****************************************************************************** + */ + +/* Includes ------------------------------------------------------------------*/ +#include "stm32f4_discovery.h" + +/** @addtogroup Utilities + * @{ + */ + +/** @addtogroup STM32F4_DISCOVERY + * @{ + */ + +/** @defgroup STM32F4_DISCOVERY_LOW_LEVEL + * @brief This file provides set of firmware functions to manage Leds and push-button + * available on STM32F4-Discovery Kit from STMicroelectronics. + * @{ + */ + +/** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_TypesDefinitions + * @{ + */ +/** + * @} + */ + + +/** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_Defines + * @{ + */ +/** + * @} + */ + + +/** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_Macros + * @{ + */ +/** + * @} + */ + + +/** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_Variables + * @{ + */ +GPIO_TypeDef* GPIO_PORT[LEDn] = {LED4_GPIO_PORT, LED3_GPIO_PORT, LED5_GPIO_PORT, + LED6_GPIO_PORT}; +const uint16_t GPIO_PIN[LEDn] = {LED4_PIN, LED3_PIN, LED5_PIN, + LED6_PIN}; +const uint32_t GPIO_CLK[LEDn] = {LED4_GPIO_CLK, LED3_GPIO_CLK, LED5_GPIO_CLK, + LED6_GPIO_CLK}; + +GPIO_TypeDef* BUTTON_PORT[BUTTONn] = {USER_BUTTON_GPIO_PORT }; + +const uint16_t BUTTON_PIN[BUTTONn] = {USER_BUTTON_PIN }; + +const uint32_t BUTTON_CLK[BUTTONn] = {USER_BUTTON_GPIO_CLK }; + +const uint16_t BUTTON_EXTI_LINE[BUTTONn] = {USER_BUTTON_EXTI_LINE }; + +const uint8_t BUTTON_PORT_SOURCE[BUTTONn] = {USER_BUTTON_EXTI_PORT_SOURCE}; + +const uint8_t BUTTON_PIN_SOURCE[BUTTONn] = {USER_BUTTON_EXTI_PIN_SOURCE }; +const uint8_t BUTTON_IRQn[BUTTONn] = {USER_BUTTON_EXTI_IRQn }; + +NVIC_InitTypeDef NVIC_InitStructure; + +/** + * @} + */ + + +/** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_FunctionPrototypes + * @{ + */ + +/** + * @} + */ + +/** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_Functions + * @{ + */ + +/** + * @brief Configures LED GPIO. + * @param Led: Specifies the Led to be configured. + * This parameter can be one of following parameters: + * @arg LED4 + * @arg LED3 + * @arg LED5 + * @arg LED6 + * @retval None + */ +void STM_EVAL_LEDInit(Led_TypeDef Led) +{ + GPIO_InitTypeDef GPIO_InitStructure; + + /* Enable the GPIO_LED Clock */ + RCC_AHB1PeriphClockCmd(GPIO_CLK[Led], ENABLE); + + /* Configure the GPIO_LED pin */ + GPIO_InitStructure.GPIO_Pin = GPIO_PIN[Led]; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; + GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_Init(GPIO_PORT[Led], &GPIO_InitStructure); +} + +/** + * @brief Turns selected LED On. + * @param Led: Specifies the Led to be set on. + * This parameter can be one of following parameters: + * @arg LED4 + * @arg LED3 + * @arg LED5 + * @arg LED6 + * @retval None + */ +void STM_EVAL_LEDOn(Led_TypeDef Led) +{ + GPIO_PORT[Led]->BSRRL = GPIO_PIN[Led]; +} + +/** + * @brief Turns selected LED Off. + * @param Led: Specifies the Led to be set off. + * This parameter can be one of following parameters: + * @arg LED4 + * @arg LED3 + * @arg LED5 + * @arg LED6 + * @retval None + */ +void STM_EVAL_LEDOff(Led_TypeDef Led) +{ + GPIO_PORT[Led]->BSRRH = GPIO_PIN[Led]; +} + +/** + * @brief Toggles the selected LED. + * @param Led: Specifies the Led to be toggled. + * This parameter can be one of following parameters: + * @arg LED4 + * @arg LED3 + * @arg LED5 + * @arg LED6 + * @retval None + */ +void STM_EVAL_LEDToggle(Led_TypeDef Led) +{ + GPIO_PORT[Led]->ODR ^= GPIO_PIN[Led]; +} + +/** + * @brief Configures Button GPIO and EXTI Line. + * @param Button: Specifies the Button to be configured. + * This parameter should be: BUTTON_USER + * @param Button_Mode: Specifies Button mode. + * This parameter can be one of following parameters: + * @arg BUTTON_MODE_GPIO: Button will be used as simple IO + * @arg BUTTON_MODE_EXTI: Button will be connected to EXTI line with interrupt + * generation capability + * @retval None + */ +void STM_EVAL_PBInit(Button_TypeDef Button, ButtonMode_TypeDef Button_Mode) +{ + GPIO_InitTypeDef GPIO_InitStructure; + EXTI_InitTypeDef EXTI_InitStructure; + NVIC_InitTypeDef myNVIC_InitStructure; + + /* Enable the BUTTON Clock */ + RCC_AHB1PeriphClockCmd(BUTTON_CLK[Button], ENABLE); + RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); + + /* Configure Button pin as input */ + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; + GPIO_InitStructure.GPIO_Pin = BUTTON_PIN[Button]; + GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStructure); + + if (Button_Mode == BUTTON_MODE_EXTI) + { + /* Connect Button EXTI Line to Button GPIO Pin */ + SYSCFG_EXTILineConfig(BUTTON_PORT_SOURCE[Button], BUTTON_PIN_SOURCE[Button]); + + /* Configure Button EXTI line */ + EXTI_InitStructure.EXTI_Line = BUTTON_EXTI_LINE[Button]; + EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; + EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; + EXTI_InitStructure.EXTI_LineCmd = ENABLE; + EXTI_Init(&EXTI_InitStructure); + + /* Enable and set Button EXTI Interrupt to the lowest priority */ + myNVIC_InitStructure.NVIC_IRQChannel = BUTTON_IRQn[Button]; + myNVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F; + myNVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F; + myNVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; + + NVIC_Init(&myNVIC_InitStructure); + } +} + +/** + * @brief Returns the selected Button state. + * @param Button: Specifies the Button to be checked. + * This parameter should be: BUTTON_USER + * @retval The Button GPIO pin value. + */ +uint32_t STM_EVAL_PBGetState(Button_TypeDef Button) +{ + return GPIO_ReadInputDataBit(BUTTON_PORT[Button], BUTTON_PIN[Button]); +} + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ diff --git a/lib/stm32f4_discovery.h b/lib/stm32f4_discovery.h new file mode 100755 index 0000000..b93b11a --- /dev/null +++ b/lib/stm32f4_discovery.h @@ -0,0 +1,158 @@ +/** + ****************************************************************************** + * @file stm32f4_discovery.h + * @author MCD Application Team + * @version V1.1.0 + * @date 28-October-2011 + * @brief This file contains definitions for STM32F4-Discovery Kit's Leds and + * push-button hardware resources. + ****************************************************************************** + * @attention + * + * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS + * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE + * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY + * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING + * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE + * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. + * + *

© COPYRIGHT 2011 STMicroelectronics

+ ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __STM32F4_DISCOVERY_H +#define __STM32F4_DISCOVERY_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ + #include "stm32f4xx.h" + +/** @addtogroup Utilities + * @{ + */ + +/** @addtogroup STM32F4_DISCOVERY + * @{ + */ + +/** @addtogroup STM32F4_DISCOVERY_LOW_LEVEL + * @{ + */ + +/** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Exported_Types + * @{ + */ +typedef enum +{ + LED4 = 0, + LED3 = 1, + LED5 = 2, + LED6 = 3 +} Led_TypeDef; + +typedef enum +{ + BUTTON_USER = 0, +} Button_TypeDef; + +typedef enum +{ + BUTTON_MODE_GPIO = 0, + BUTTON_MODE_EXTI = 1 +} ButtonMode_TypeDef; +/** + * @} + */ + +/** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Exported_Constants + * @{ + */ + +/** @addtogroup STM32F4_DISCOVERY_LOW_LEVEL_LED + * @{ + */ +#define LEDn 4 + +#define LED4_PIN GPIO_Pin_12 +#define LED4_GPIO_PORT GPIOD +#define LED4_GPIO_CLK RCC_AHB1Periph_GPIOD + +#define LED3_PIN GPIO_Pin_13 +#define LED3_GPIO_PORT GPIOD +#define LED3_GPIO_CLK RCC_AHB1Periph_GPIOD + +#define LED5_PIN GPIO_Pin_14 +#define LED5_GPIO_PORT GPIOD +#define LED5_GPIO_CLK RCC_AHB1Periph_GPIOD + +#define LED6_PIN GPIO_Pin_15 +#define LED6_GPIO_PORT GPIOD +#define LED6_GPIO_CLK RCC_AHB1Periph_GPIOD +/** + * @} + */ + +/** @addtogroup STM32F4_DISCOVERY_LOW_LEVEL_BUTTON + * @{ + */ +#define BUTTONn 1 + +/** + * @brief Wakeup push-button + */ +#define USER_BUTTON_PIN GPIO_Pin_0 +#define USER_BUTTON_GPIO_PORT GPIOA +#define USER_BUTTON_GPIO_CLK RCC_AHB1Periph_GPIOA +#define USER_BUTTON_EXTI_LINE EXTI_Line0 +#define USER_BUTTON_EXTI_PORT_SOURCE EXTI_PortSourceGPIOA +#define USER_BUTTON_EXTI_PIN_SOURCE EXTI_PinSource0 +#define USER_BUTTON_EXTI_IRQn EXTI0_IRQn +/** + * @} + */ + +/** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Exported_Macros + * @{ + */ +/** + * @} + */ + + +/** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Exported_Functions + * @{ + */ +void STM_EVAL_LEDInit(Led_TypeDef Led); +void STM_EVAL_LEDOn(Led_TypeDef Led); +void STM_EVAL_LEDOff(Led_TypeDef Led); +void STM_EVAL_LEDToggle(Led_TypeDef Led); +void STM_EVAL_PBInit(Button_TypeDef Button, ButtonMode_TypeDef Button_Mode); +uint32_t STM_EVAL_PBGetState(Button_TypeDef Button); +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __STM32F4_DISCOVERY_H */ +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + + + +/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ diff --git a/lib/stm32f4_discovery_lis302dl.c b/lib/stm32f4_discovery_lis302dl.c new file mode 100755 index 0000000..4051fdd --- /dev/null +++ b/lib/stm32f4_discovery_lis302dl.c @@ -0,0 +1,502 @@ +/** + ****************************************************************************** + * @file stm32f4_discovery_lis302dl.c + * @author MCD Application Team + * @version V1.1.0 + * @date 28-October-2011 + * @brief This file provides a set of functions needed to manage the LIS302DL + * MEMS accelerometer available on STM32F4-Discovery Kit. + ****************************************************************************** + * @attention + * + * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS + * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE + * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY + * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING + * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE + * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. + * + *

© COPYRIGHT 2011 STMicroelectronics

+ ****************************************************************************** + */ + +/* Includes ------------------------------------------------------------------*/ +#include "stm32f4_discovery_lis302dl.h" + +/** @addtogroup Utilities + * @{ + */ + +/** @addtogroup STM32F4_DISCOVERY + * @{ + */ + +/** @addtogroup STM32F4_DISCOVERY_LIS302DL + * @{ + */ + + +/** @defgroup STM32F4_DISCOVERY_LIS302DL_Private_TypesDefinitions + * @{ + */ + +/** + * @} + */ + +/** @defgroup STM32F4_DISCOVERY_LIS302DL_Private_Defines + * @{ + */ +__IO uint32_t LIS302DLTimeout = LIS302DL_FLAG_TIMEOUT; + +/* Read/Write command */ +#define READWRITE_CMD ((uint8_t)0x80) +/* Multiple byte read/write command */ +#define MULTIPLEBYTE_CMD ((uint8_t)0x40) +/* Dummy Byte Send by the SPI Master device in order to generate the Clock to the Slave device */ +#define DUMMY_BYTE ((uint8_t)0x00) + +/** + * @} + */ + +/** @defgroup STM32F4_DISCOVERY_LIS302DL_Private_Macros + * @{ + */ + +/** + * @} + */ + +/** @defgroup STM32F4_DISCOVERY_LIS302DL_Private_Variables + * @{ + */ + +/** + * @} + */ + +/** @defgroup STM32F4_DISCOVERY_LIS302DL_Private_FunctionPrototypes + * @{ + */ +static uint8_t LIS302DL_SendByte(uint8_t byte); +static void LIS302DL_LowLevel_Init(void); +/** + * @} + */ + +/** @defgroup STM32F4_DISCOVERY_LIS302DL_Private_Functions + * @{ + */ + + +/** + * @brief Set LIS302DL Initialization. + * @param LIS302DL_Config_Struct: pointer to a LIS302DL_Config_TypeDef structure + * that contains the configuration setting for the LIS302DL. + * @retval None + */ +void LIS302DL_Init(LIS302DL_InitTypeDef *LIS302DL_InitStruct) +{ + uint8_t ctrl = 0x00; + + /* Configure the low level interface ---------------------------------------*/ + LIS302DL_LowLevel_Init(); + + /* Configure MEMS: data rate, power mode, full scale, self test and axes */ + ctrl = (uint8_t) (LIS302DL_InitStruct->Output_DataRate | LIS302DL_InitStruct->Power_Mode | \ + LIS302DL_InitStruct->Full_Scale | LIS302DL_InitStruct->Self_Test | \ + LIS302DL_InitStruct->Axes_Enable); + + /* Write value to MEMS CTRL_REG1 regsister */ + LIS302DL_Write(&ctrl, LIS302DL_CTRL_REG1_ADDR, 1); +} + +/** + * @brief Set LIS302DL Internal High Pass Filter configuration. + * @param LIS302DL_Filter_ConfigTypeDef: pointer to a LIS302DL_FilterConfig_TypeDef + * structure that contains the configuration setting for the LIS302DL Filter. + * @retval None + */ +void LIS302DL_FilterConfig(LIS302DL_FilterConfigTypeDef *LIS302DL_FilterConfigStruct) +{ + uint8_t ctrl = 0x00; + + /* Read CTRL_REG2 register */ + LIS302DL_Read(&ctrl, LIS302DL_CTRL_REG2_ADDR, 1); + + /* Clear high pass filter cut-off level, interrupt and data selection bits*/ + ctrl &= (uint8_t)~(LIS302DL_FILTEREDDATASELECTION_OUTPUTREGISTER | \ + LIS302DL_HIGHPASSFILTER_LEVEL_3 | \ + LIS302DL_HIGHPASSFILTERINTERRUPT_1_2); + /* Configure MEMS high pass filter cut-off level, interrupt and data selection bits */ + ctrl |= (uint8_t)(LIS302DL_FilterConfigStruct->HighPassFilter_Data_Selection | \ + LIS302DL_FilterConfigStruct->HighPassFilter_CutOff_Frequency | \ + LIS302DL_FilterConfigStruct->HighPassFilter_Interrupt); + + /* Write value to MEMS CTRL_REG2 register */ + LIS302DL_Write(&ctrl, LIS302DL_CTRL_REG2_ADDR, 1); +} + +/** + * @brief Set LIS302DL Interrupt configuration + * @param LIS302DL_InterruptConfig_TypeDef: pointer to a LIS302DL_InterruptConfig_TypeDef + * structure that contains the configuration setting for the LIS302DL Interrupt. + * @retval None + */ +void LIS302DL_InterruptConfig(LIS302DL_InterruptConfigTypeDef *LIS302DL_IntConfigStruct) +{ + uint8_t ctrl = 0x00; + + /* Read CLICK_CFG register */ + LIS302DL_Read(&ctrl, LIS302DL_CLICK_CFG_REG_ADDR, 1); + + /* Configure latch Interrupt request, click interrupts and double click interrupts */ + ctrl = (uint8_t)(LIS302DL_IntConfigStruct->Latch_Request| \ + LIS302DL_IntConfigStruct->SingleClick_Axes | \ + LIS302DL_IntConfigStruct->DoubleClick_Axes); + + /* Write value to MEMS CLICK_CFG register */ + LIS302DL_Write(&ctrl, LIS302DL_CLICK_CFG_REG_ADDR, 1); +} + +/** + * @brief Change the lowpower mode for LIS302DL + * @param LowPowerMode: new state for the lowpower mode. + * This parameter can be one of the following values: + * @arg LIS302DL_LOWPOWERMODE_POWERDOWN: Power down mode + * @arg LIS302DL_LOWPOWERMODE_ACTIVE: Active mode + * @retval None + */ +void LIS302DL_LowpowerCmd(uint8_t LowPowerMode) +{ + uint8_t tmpreg; + + /* Read CTRL_REG1 register */ + LIS302DL_Read(&tmpreg, LIS302DL_CTRL_REG1_ADDR, 1); + + /* Set new low power mode configuration */ + tmpreg &= (uint8_t)~LIS302DL_LOWPOWERMODE_ACTIVE; + tmpreg |= LowPowerMode; + + /* Write value to MEMS CTRL_REG1 regsister */ + LIS302DL_Write(&tmpreg, LIS302DL_CTRL_REG1_ADDR, 1); +} + +/** + * @brief Data Rate command + * @param DataRateValue: Data rate value + * This parameter can be one of the following values: + * @arg LIS302DL_DATARATE_100: 100 Hz output data rate + * @arg LIS302DL_DATARATE_400: 400 Hz output data rate + * @retval None + */ +void LIS302DL_DataRateCmd(uint8_t DataRateValue) +{ + uint8_t tmpreg; + + /* Read CTRL_REG1 register */ + LIS302DL_Read(&tmpreg, LIS302DL_CTRL_REG1_ADDR, 1); + + /* Set new Data rate configuration */ + tmpreg &= (uint8_t)~LIS302DL_DATARATE_400; + tmpreg |= DataRateValue; + + /* Write value to MEMS CTRL_REG1 regsister */ + LIS302DL_Write(&tmpreg, LIS302DL_CTRL_REG1_ADDR, 1); +} + +/** + * @brief Change the Full Scale of LIS302DL + * @param FS_value: new full scale value. + * This parameter can be one of the following values: + * @arg LIS302DL_FULLSCALE_2_3: +-2.3g + * @arg LIS302DL_FULLSCALE_9_2: +-9.2g + * @retval None + */ +void LIS302DL_FullScaleCmd(uint8_t FS_value) +{ + uint8_t tmpreg; + + /* Read CTRL_REG1 register */ + LIS302DL_Read(&tmpreg, LIS302DL_CTRL_REG1_ADDR, 1); + + /* Set new full scale configuration */ + tmpreg &= (uint8_t)~LIS302DL_FULLSCALE_9_2; + tmpreg |= FS_value; + + /* Write value to MEMS CTRL_REG1 regsister */ + LIS302DL_Write(&tmpreg, LIS302DL_CTRL_REG1_ADDR, 1); +} + +/** + * @brief Reboot memory content of LIS302DL + * @param None + * @retval None + */ +void LIS302DL_RebootCmd(void) +{ + uint8_t tmpreg; + /* Read CTRL_REG2 register */ + LIS302DL_Read(&tmpreg, LIS302DL_CTRL_REG2_ADDR, 1); + + /* Enable or Disable the reboot memory */ + tmpreg |= LIS302DL_BOOT_REBOOTMEMORY; + + /* Write value to MEMS CTRL_REG2 regsister */ + LIS302DL_Write(&tmpreg, LIS302DL_CTRL_REG2_ADDR, 1); +} + +/** + * @brief Writes one byte to the LIS302DL. + * @param pBuffer : pointer to the buffer containing the data to be written to the LIS302DL. + * @param WriteAddr : LIS302DL's internal address to write to. + * @param NumByteToWrite: Number of bytes to write. + * @retval None + */ +void LIS302DL_Write(uint8_t* pBuffer, uint8_t WriteAddr, uint16_t NumByteToWrite) +{ + /* Configure the MS bit: + - When 0, the address will remain unchanged in multiple read/write commands. + - When 1, the address will be auto incremented in multiple read/write commands. + */ + if(NumByteToWrite > 0x01) + { + WriteAddr |= (uint8_t)MULTIPLEBYTE_CMD; + } + /* Set chip select Low at the start of the transmission */ + LIS302DL_CS_LOW(); + + /* Send the Address of the indexed register */ + LIS302DL_SendByte(WriteAddr); + /* Send the data that will be written into the device (MSB First) */ + while(NumByteToWrite >= 0x01) + { + LIS302DL_SendByte(*pBuffer); + NumByteToWrite--; + pBuffer++; + } + + /* Set chip select High at the end of the transmission */ + LIS302DL_CS_HIGH(); +} + +/** + * @brief Reads a block of data from the LIS302DL. + * @param pBuffer : pointer to the buffer that receives the data read from the LIS302DL. + * @param ReadAddr : LIS302DL's internal address to read from. + * @param NumByteToRead : number of bytes to read from the LIS302DL. + * @retval None + */ +void LIS302DL_Read(uint8_t* pBuffer, uint8_t ReadAddr, uint16_t NumByteToRead) +{ + if(NumByteToRead > 0x01) + { + ReadAddr |= (uint8_t)(READWRITE_CMD | MULTIPLEBYTE_CMD); + } + else + { + ReadAddr |= (uint8_t)READWRITE_CMD; + } + /* Set chip select Low at the start of the transmission */ + LIS302DL_CS_LOW(); + + /* Send the Address of the indexed register */ + LIS302DL_SendByte(ReadAddr); + + /* Receive the data that will be read from the device (MSB First) */ + while(NumByteToRead > 0x00) + { + /* Send dummy byte (0x00) to generate the SPI clock to LIS302DL (Slave device) */ + *pBuffer = LIS302DL_SendByte(DUMMY_BYTE); + NumByteToRead--; + pBuffer++; + } + + /* Set chip select High at the end of the transmission */ + LIS302DL_CS_HIGH(); +} + +/** + * @brief Read LIS302DL output register, and calculate the acceleration + * ACC[mg]=SENSITIVITY* (out_h*256+out_l)/16 (12 bit rappresentation) + * @param s16 buffer to store data + * @retval None + */ +void LIS302DL_ReadACC(int32_t* out) +{ + uint8_t buffer[6]; + uint8_t crtl, i = 0x00; + + LIS302DL_Read(&crtl, LIS302DL_CTRL_REG1_ADDR, 1); + LIS302DL_Read(buffer, LIS302DL_OUT_X_ADDR, 6); + + switch(crtl & 0x20) + { + /* FS bit = 0 ==> Sensitivity typical value = 18milligals/digit*/ + case 0x00: + for(i=0; i<0x03; i++) + { + *out =(int32_t)(LIS302DL_SENSITIVITY_2_3G * (int8_t)buffer[2*i]); + out++; + } + break; + /* FS bit = 1 ==> Sensitivity typical value = 72milligals/digit*/ + case 0x20: + for(i=0; i<0x03; i++) + { + *out =(int32_t)(LIS302DL_SENSITIVITY_9_2G * (int8_t)buffer[2*i]); + out++; + } + break; + default: + break; + } + } + +/** + * @brief Initializes the low level interface used to drive the LIS302DL + * @param None + * @retval None + */ +static void LIS302DL_LowLevel_Init(void) +{ + GPIO_InitTypeDef GPIO_InitStructure; + SPI_InitTypeDef SPI_InitStructure; + + /* Enable the SPI periph */ + RCC_APB2PeriphClockCmd(LIS302DL_SPI_CLK, ENABLE); + + /* Enable SCK, MOSI and MISO GPIO clocks */ + RCC_AHB1PeriphClockCmd(LIS302DL_SPI_SCK_GPIO_CLK | LIS302DL_SPI_MISO_GPIO_CLK | LIS302DL_SPI_MOSI_GPIO_CLK, ENABLE); + + /* Enable CS GPIO clock */ + RCC_AHB1PeriphClockCmd(LIS302DL_SPI_CS_GPIO_CLK, ENABLE); + + /* Enable INT1 GPIO clock */ + RCC_AHB1PeriphClockCmd(LIS302DL_SPI_INT1_GPIO_CLK, ENABLE); + + /* Enable INT2 GPIO clock */ + RCC_AHB1PeriphClockCmd(LIS302DL_SPI_INT2_GPIO_CLK, ENABLE); + + GPIO_PinAFConfig(LIS302DL_SPI_SCK_GPIO_PORT, LIS302DL_SPI_SCK_SOURCE, LIS302DL_SPI_SCK_AF); + GPIO_PinAFConfig(LIS302DL_SPI_MISO_GPIO_PORT, LIS302DL_SPI_MISO_SOURCE, LIS302DL_SPI_MISO_AF); + GPIO_PinAFConfig(LIS302DL_SPI_MOSI_GPIO_PORT, LIS302DL_SPI_MOSI_SOURCE, LIS302DL_SPI_MOSI_AF); + + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; + GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; + + /* SPI SCK pin configuration */ + GPIO_InitStructure.GPIO_Pin = LIS302DL_SPI_SCK_PIN; + GPIO_Init(LIS302DL_SPI_SCK_GPIO_PORT, &GPIO_InitStructure); + + /* SPI MOSI pin configuration */ + GPIO_InitStructure.GPIO_Pin = LIS302DL_SPI_MOSI_PIN; + GPIO_Init(LIS302DL_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure); + + /* SPI MISO pin configuration */ + GPIO_InitStructure.GPIO_Pin = LIS302DL_SPI_MISO_PIN; + GPIO_Init(LIS302DL_SPI_MISO_GPIO_PORT, &GPIO_InitStructure); + + /* SPI configuration -------------------------------------------------------*/ + SPI_I2S_DeInit(LIS302DL_SPI); + SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; + SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; + SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; + SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; + SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; + SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; + SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; + SPI_InitStructure.SPI_CRCPolynomial = 7; + SPI_InitStructure.SPI_Mode = SPI_Mode_Master; + SPI_Init(LIS302DL_SPI, &SPI_InitStructure); + + /* Enable SPI1 */ + SPI_Cmd(LIS302DL_SPI, ENABLE); + + /* Configure GPIO PIN for Lis Chip select */ + GPIO_InitStructure.GPIO_Pin = LIS302DL_SPI_CS_PIN; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; + GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_Init(LIS302DL_SPI_CS_GPIO_PORT, &GPIO_InitStructure); + + /* Deselect : Chip Select high */ + GPIO_SetBits(LIS302DL_SPI_CS_GPIO_PORT, LIS302DL_SPI_CS_PIN); + + /* Configure GPIO PINs to detect Interrupts */ + GPIO_InitStructure.GPIO_Pin = LIS302DL_SPI_INT1_PIN; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; + GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; + GPIO_Init(LIS302DL_SPI_INT1_GPIO_PORT, &GPIO_InitStructure); + + GPIO_InitStructure.GPIO_Pin = LIS302DL_SPI_INT2_PIN; + GPIO_Init(LIS302DL_SPI_INT2_GPIO_PORT, &GPIO_InitStructure); +} + +/** + * @brief Sends a Byte through the SPI interface and return the Byte received + * from the SPI bus. + * @param Byte : Byte send. + * @retval The received byte value + */ +static uint8_t LIS302DL_SendByte(uint8_t byte) +{ + /* Loop while DR register in not emplty */ + LIS302DLTimeout = LIS302DL_FLAG_TIMEOUT; + while (SPI_I2S_GetFlagStatus(LIS302DL_SPI, SPI_I2S_FLAG_TXE) == RESET) + { + if((LIS302DLTimeout--) == 0) return LIS302DL_TIMEOUT_UserCallback(); + } + + /* Send a Byte through the SPI peripheral */ + SPI_I2S_SendData(LIS302DL_SPI, byte); + + /* Wait to receive a Byte */ + LIS302DLTimeout = LIS302DL_FLAG_TIMEOUT; + while (SPI_I2S_GetFlagStatus(LIS302DL_SPI, SPI_I2S_FLAG_RXNE) == RESET) + { + if((LIS302DLTimeout--) == 0) return LIS302DL_TIMEOUT_UserCallback(); + } + + /* Return the Byte read from the SPI bus */ + return (uint8_t)SPI_I2S_ReceiveData(LIS302DL_SPI); +} + +#ifdef USE_DEFAULT_TIMEOUT_CALLBACK +/** + * @brief Basic management of the timeout situation. + * @param None. + * @retval None. + */ +uint32_t LIS302DL_TIMEOUT_UserCallback(void) +{ + /* Block communication and all processes */ + while (1) + { + } +} +#endif /* USE_DEFAULT_TIMEOUT_CALLBACK */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + + +/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ diff --git a/lib/stm32f4_discovery_lis302dl.h b/lib/stm32f4_discovery_lis302dl.h new file mode 100755 index 0000000..e73a241 --- /dev/null +++ b/lib/stm32f4_discovery_lis302dl.h @@ -0,0 +1,772 @@ +/** + ****************************************************************************** + * @file stm32f4_discovery_lis302dl.h + * @author MCD Application Team + * @version V1.1.0 + * @date 28-October-2011 + * @brief This file contains all the functions prototypes for the stm32f4_discovery_lis302dl.c + * firmware driver. + ****************************************************************************** + * @attention + * + * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS + * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE + * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY + * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING + * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE + * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. + * + *

© COPYRIGHT 2011 STMicroelectronics

+ ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __STM32F4_DISCOVERY_LIS302DL_H +#define __STM32F4_DISCOVERY_LIS302DL_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ + #include "stm32f4xx.h" + +/** @addtogroup Utilities + * @{ + */ + +/** @addtogroup STM32F4_DISCOVERY + * @{ + */ + +/** @addtogroup STM32F4_DISCOVERY_LIS302DL + * @{ + */ + + +/** @defgroup STM32F4_DISCOVERY_LIS302DL_Exported_Types + * @{ + */ + +/* LIS302DL struct */ +typedef struct +{ + uint8_t Power_Mode; /* Power-down/Active Mode */ + uint8_t Output_DataRate; /* OUT data rate 100 Hz / 400 Hz */ + uint8_t Axes_Enable; /* Axes enable */ + uint8_t Full_Scale; /* Full scale */ + uint8_t Self_Test; /* Self test */ +}LIS302DL_InitTypeDef; + +/* LIS302DL High Pass Filter struct */ +typedef struct +{ + uint8_t HighPassFilter_Data_Selection; /* Internal filter bypassed or data from internal filter send to output register*/ + uint8_t HighPassFilter_CutOff_Frequency; /* High pass filter cut-off frequency */ + uint8_t HighPassFilter_Interrupt; /* High pass filter enabled for Freefall/WakeUp #1 or #2 */ +}LIS302DL_FilterConfigTypeDef; + +/* LIS302DL Interrupt struct */ +typedef struct +{ + uint8_t Latch_Request; /* Latch interrupt request into CLICK_SRC register*/ + uint8_t SingleClick_Axes; /* Single Click Axes Interrupts */ + uint8_t DoubleClick_Axes; /* Double Click Axes Interrupts */ +}LIS302DL_InterruptConfigTypeDef; + +/** + * @} + */ + +/** @defgroup STM32F4_DISCOVERY_LIS302DL_Exported_Constants + * @{ + */ + +/* Uncomment the following line to use the default LIS302DL_TIMEOUT_UserCallback() + function implemented in stm32f4_discovery_lis302dl.c file. + LIS302DL_TIMEOUT_UserCallback() function is called whenever a timeout condition + occure during communication (waiting transmit data register empty flag(TXE) + or waiting receive data register is not empty flag (RXNE)). */ +#define USE_DEFAULT_TIMEOUT_CALLBACK + +/* Maximum Timeout values for flags waiting loops. These timeouts are not based + on accurate values, they just guarantee that the application will not remain + stuck if the SPI communication is corrupted. + You may modify these timeout values depending on CPU frequency and application + conditions (interrupts routines ...). */ +#define LIS302DL_FLAG_TIMEOUT ((uint32_t)0x1000) + +/** + * @brief LIS302DL SPI Interface pins + */ +#define LIS302DL_SPI SPI1 +#define LIS302DL_SPI_CLK RCC_APB2Periph_SPI1 + +#define LIS302DL_SPI_SCK_PIN GPIO_Pin_5 /* PA.05 */ +#define LIS302DL_SPI_SCK_GPIO_PORT GPIOA /* GPIOA */ +#define LIS302DL_SPI_SCK_GPIO_CLK RCC_AHB1Periph_GPIOA +#define LIS302DL_SPI_SCK_SOURCE GPIO_PinSource5 +#define LIS302DL_SPI_SCK_AF GPIO_AF_SPI1 + +#define LIS302DL_SPI_MISO_PIN GPIO_Pin_6 /* PA.6 */ +#define LIS302DL_SPI_MISO_GPIO_PORT GPIOA /* GPIOA */ +#define LIS302DL_SPI_MISO_GPIO_CLK RCC_AHB1Periph_GPIOA +#define LIS302DL_SPI_MISO_SOURCE GPIO_PinSource6 +#define LIS302DL_SPI_MISO_AF GPIO_AF_SPI1 + +#define LIS302DL_SPI_MOSI_PIN GPIO_Pin_7 /* PA.7 */ +#define LIS302DL_SPI_MOSI_GPIO_PORT GPIOA /* GPIOA */ +#define LIS302DL_SPI_MOSI_GPIO_CLK RCC_AHB1Periph_GPIOA +#define LIS302DL_SPI_MOSI_SOURCE GPIO_PinSource7 +#define LIS302DL_SPI_MOSI_AF GPIO_AF_SPI1 + +#define LIS302DL_SPI_CS_PIN GPIO_Pin_3 /* PE.03 */ +#define LIS302DL_SPI_CS_GPIO_PORT GPIOE /* GPIOE */ +#define LIS302DL_SPI_CS_GPIO_CLK RCC_AHB1Periph_GPIOE + +#define LIS302DL_SPI_INT1_PIN GPIO_Pin_0 /* PE.00 */ +#define LIS302DL_SPI_INT1_GPIO_PORT GPIOE /* GPIOE */ +#define LIS302DL_SPI_INT1_GPIO_CLK RCC_AHB1Periph_GPIOE +#define LIS302DL_SPI_INT1_EXTI_LINE EXTI_Line0 +#define LIS302DL_SPI_INT1_EXTI_PORT_SOURCE EXTI_PortSourceGPIOE +#define LIS302DL_SPI_INT1_EXTI_PIN_SOURCE EXTI_PinSource0 +#define LIS302DL_SPI_INT1_EXTI_IRQn EXTI0_IRQn + +#define LIS302DL_SPI_INT2_PIN GPIO_Pin_1 /* PE.01 */ +#define LIS302DL_SPI_INT2_GPIO_PORT GPIOE /* GPIOE */ +#define LIS302DL_SPI_INT2_GPIO_CLK RCC_AHB1Periph_GPIOE +#define LIS302DL_SPI_INT2_EXTI_LINE EXTI_Line1 +#define LIS302DL_SPI_INT2_EXTI_PORT_SOURCE EXTI_PortSourceGPIOE +#define LIS302DL_SPI_INT2_EXTI_PIN_SOURCE EXTI_PinSource1 +#define LIS302DL_SPI_INT2_EXTI_IRQn EXTI1_IRQn + + +/******************************************************************************/ +/*************************** START REGISTER MAPPING **************************/ +/******************************************************************************/ + +/******************************************************************************* +* WHO_AM_I Register: Device Identification Register +* Read only register +* Default value: 0x3B +*******************************************************************************/ +#define LIS302DL_WHO_AM_I_ADDR 0x0F + +/******************************************************************************* +* CTRL_REG1 Register: Control Register 1 +* Read Write register +* Default value: 0x07 +* 7 DR: Data Rate selection. +* 0 - 100 Hz output data rate +* 1 - 400 Hz output data rate +* 6 PD: Power Down control. +* 0 - power down mode +* 1 - active mode +* 5 FS: Full Scale selection. +* 0 - Typical measurement range 2.3 +* 1 - Typical measurement range 9.2 +* 4:3 STP-STM Self Test Enable: +* STP | STM | mode +* ---------------------------- +* 0 | 0 | Normal mode +* 0 | 1 | Self Test M +* 1 | 0 | Self Test P +* 2 Zen: Z axis enable. +* 0 - Z axis disabled +* 1- Z axis enabled +* 1 Yen: Y axis enable. +* 0 - Y axis disabled +* 1- Y axis enabled +* 0 Xen: X axis enable. +* 0 - X axis disabled +* 1- X axis enabled +********************************************************************************/ +#define LIS302DL_CTRL_REG1_ADDR 0x20 + +/******************************************************************************* +* CTRL_REG2 Regsiter: Control Register 2 +* Read Write register +* Default value: 0x00 +* 7 SIM: SPI Serial Interface Mode Selection. +* 0 - 4 wire interface +* 1 - 3 wire interface +* 6 BOOT: Reboot memory content +* 0 - normal mode +* 1 - reboot memory content +* 5 Reserved +* 4 FDS: Filtered data selection. +* 0 - internal filter bypassed +* 1 - data from internal filter sent to output register +* 3 HP FF_WU2: High pass filter enabled for FreeFall/WakeUp#2. +* 0 - filter bypassed +* 1 - filter enabled +* 2 HP FF_WU1: High pass filter enabled for FreeFall/WakeUp#1. +* 0 - filter bypassed +* 1 - filter enabled +* 1:0 HP coeff2-HP coeff1 High pass filter cut-off frequency (ft) configuration. +* ft= ODR[hz]/6*HP coeff +* HP coeff2 | HP coeff1 | HP coeff +* ------------------------------------------- +* 0 | 0 | 8 +* 0 | 1 | 16 +* 1 | 0 | 32 +* 1 | 1 | 64 +* HP coeff | ft[hz] | ft[hz] | +* |ODR 100Hz | ODR 400Hz | +* -------------------------------------------- +* 00 | 2 | 8 | +* 01 | 1 | 4 | +* 10 | 0.5 | 2 | +* 11 | 0.25 | 1 | +*******************************************************************************/ +#define LIS302DL_CTRL_REG2_ADDR 0x21 + +/******************************************************************************* +* CTRL_REG3 Register: Interrupt Control Register +* Read Write register +* Default value: 0x00 +* 7 IHL active: Interrupt active high/low. +* 0 - active high +* 1 - active low +* 6 PP_OD: push-pull/open-drain. +* 0 - push-pull +* 1 - open-drain +* 5:3 I2_CFG2 - I2_CFG0 Data signal on INT2 pad control bits +* 2:0 I1_CFG2 - I1_CFG0 Data signal on INT1 pad control bits +* I1(2)_CFG2 | I1(2)_CFG1 | I1(2)_CFG0 | INT1(2) Pad +* ---------------------------------------------------------- +* 0 | 0 | 0 | GND +* 0 | 0 | 1 | FreeFall/WakeUp#1 +* 0 | 1 | 0 | FreeFall/WakeUp#2 +* 0 | 1 | 1 | FreeFall/WakeUp#1 or FreeFall/WakeUp#2 +* 1 | 0 | 0 | Data ready +* 1 | 1 | 1 | Click interrupt +*******************************************************************************/ +#define LIS302DL_CTRL_REG3_ADDR 0x22 + +/******************************************************************************* +* HP_FILTER_RESET Register: Dummy register. Reading at this address zeroes +* instantaneously the content of the internal high pass filter. If the high pass +* filter is enabled all three axes are instantaneously set to 0g. +* This allows to overcome the settling time of the high pass filter. +* Read only register +* Default value: Dummy +*******************************************************************************/ +#define LIS302DL_HP_FILTER_RESET_REG_ADDR 0x23 + +/******************************************************************************* +* STATUS_REG Register: Status Register +* Default value: 0x00 +* 7 ZYXOR: X, Y and Z axis data overrun. +* 0: no overrun has occurred +* 1: new data has overwritten the previous one before it was read +* 6 ZOR: Z axis data overrun. +* 0: no overrun has occurred +* 1: new data for Z-axis has overwritten the previous one before it was read +* 5 yOR: y axis data overrun. +* 0: no overrun has occurred +* 1: new data for y-axis has overwritten the previous one before it was read +* 4 XOR: X axis data overrun. +* 0: no overrun has occurred +* 1: new data for X-axis has overwritten the previous one before it was read +* 3 ZYXDA: X, Y and Z axis new data available +* 0: a new set of data is not yet available +* 1: a new set of data is available +* 2 ZDA: Z axis new data available. +* 0: a new set of data is not yet available +* 1: a new data for Z axis is available +* 1 YDA: Y axis new data available +* 0: a new set of data is not yet available +* 1: a new data for Y axis is available +* 0 XDA: X axis new data available +* 0: a new set of data is not yet available +* 1: a new data for X axis is available +*******************************************************************************/ +#define LIS302DL_STATUS_REG_ADDR 0x27 + +/******************************************************************************* +* OUT_X Register: X-axis output Data +* Read only register +* Default value: output +* 7:0 XD7-XD0: X-axis output Data +*******************************************************************************/ +#define LIS302DL_OUT_X_ADDR 0x29 + +/******************************************************************************* +* OUT_Y Register: Y-axis output Data +* Read only register +* Default value: output +* 7:0 YD7-YD0: Y-axis output Data +*******************************************************************************/ +#define LIS302DL_OUT_Y_ADDR 0x2B + +/******************************************************************************* +* OUT_Z Register: Z-axis output Data +* Read only register +* Default value: output +* 7:0 ZD7-ZD0: Z-axis output Data +*******************************************************************************/ +#define LIS302DL_OUT_Z_ADDR 0x2D + +/******************************************************************************* +* FF_WW_CFG_1 Register: Configuration register for Interrupt 1 source. +* Read write register +* Default value: 0x00 +* 7 AOI: AND/OR combination of Interrupt events. +* 0: OR combination of interrupt events +* 1: AND combination of interrupt events +* 6 LIR: Latch/not latch interrupt request +* 0: interrupt request not latched +* 1: interrupt request latched +* 5 ZHIE: Enable interrupt generation on Z high event. +* 0: disable interrupt request +* 1: enable interrupt request on measured accel. value higher than preset threshold +* 4 ZLIE: Enable interrupt generation on Z low event. +* 0: disable interrupt request +* 1: enable interrupt request on measured accel. value lower than preset threshold +* 3 YHIE: Enable interrupt generation on Y high event. +* 0: disable interrupt request +* 1: enable interrupt request on measured accel. value higher than preset threshold +* 2 YLIE: Enable interrupt generation on Y low event. +* 0: disable interrupt request +* 1: enable interrupt request on measured accel. value lower than preset threshold +* 1 XHIE: Enable interrupt generation on X high event. +* 0: disable interrupt request +* 1: enable interrupt request on measured accel. value higher than preset threshold +* 0 XLIE: Enable interrupt generation on X low event. +* 0: disable interrupt request +* 1: enable interrupt request on measured accel. value lower than preset threshold +*******************************************************************************/ +#define LIS302DL_FF_WU_CFG1_REG_ADDR 0x30 + +/******************************************************************************* +* FF_WU_SRC_1 Register: Interrupt 1 source register. +* Reading at this address clears FF_WU_SRC_1 register and the FF, WU 1 interrupt +* and allow the refreshment of data in the FF_WU_SRC_1 register if the latched option +* was chosen. +* Read only register +* Default value: 0x00 +* 7 Reserved +* 6 IA: Interrupt active. +* 0: no interrupt has been generated +* 1: one or more interrupts have been generated +* 5 ZH: Z high. +* 0: no interrupt +* 1: ZH event has occurred +* 4 ZL: Z low. +* 0: no interrupt +* 1: ZL event has occurred +* 3 YH: Y high. +* 0: no interrupt +* 1: YH event has occurred +* 2 YL: Y low. +* 0: no interrupt +* 1: YL event has occurred +* 1 YH: X high. +* 0: no interrupt +* 1: XH event has occurred +* 0 YL: X low. +* 0: no interrupt +* 1: XL event has occurred +*******************************************************************************/ +#define LIS302DL_FF_WU_SRC1_REG_ADDR 0x31 + +/******************************************************************************* +* FF_WU_THS_1 Register: Threshold register +* Read Write register +* Default value: 0x00 +* 7 DCRM: Reset mode selection. +* 0 - counter resetted +* 1 - counter decremented +* 6 THS6-THS0: Free-fall/wake-up threshold value. +*******************************************************************************/ +#define LIS302DL_FF_WU_THS1_REG_ADDR 0x32 + +/******************************************************************************* +* FF_WU_DURATION_1 Register: duration Register +* Read Write register +* Default value: 0x00 +* 7:0 D7-D0 Duration value. (Duration steps and maximum values depend on the ODR chosen) + ******************************************************************************/ +#define LIS302DL_FF_WU_DURATION1_REG_ADDR 0x33 + +/******************************************************************************* +* FF_WW_CFG_2 Register: Configuration register for Interrupt 2 source. +* Read write register +* Default value: 0x00 +* 7 AOI: AND/OR combination of Interrupt events. +* 0: OR combination of interrupt events +* 1: AND combination of interrupt events +* 6 LIR: Latch/not latch interrupt request +* 0: interrupt request not latched +* 1: interrupt request latched +* 5 ZHIE: Enable interrupt generation on Z high event. +* 0: disable interrupt request +* 1: enable interrupt request on measured accel. value higher than preset threshold +* 4 ZLIE: Enable interrupt generation on Z low event. +* 0: disable interrupt request +* 1: enable interrupt request on measured accel. value lower than preset threshold +* 3 YHIE: Enable interrupt generation on Y high event. +* 0: disable interrupt request +* 1: enable interrupt request on measured accel. value higher than preset threshold +* 2 YLIE: Enable interrupt generation on Y low event. +* 0: disable interrupt request +* 1: enable interrupt request on measured accel. value lower than preset threshold +* 1 XHIE: Enable interrupt generation on X high event. +* 0: disable interrupt request +* 1: enable interrupt request on measured accel. value higher than preset threshold +* 0 XLIE: Enable interrupt generation on X low event. +* 0: disable interrupt request +* 1: enable interrupt request on measured accel. value lower than preset threshold +*******************************************************************************/ +#define LIS302DL_FF_WU_CFG2_REG_ADDR 0x34 + +/******************************************************************************* +* FF_WU_SRC_2 Register: Interrupt 2 source register. +* Reading at this address clears FF_WU_SRC_2 register and the FF, WU 2 interrupt +* and allow the refreshment of data in the FF_WU_SRC_2 register if the latched option +* was chosen. +* Read only register +* Default value: 0x00 +* 7 Reserved +* 6 IA: Interrupt active. +* 0: no interrupt has been generated +* 1: one or more interrupts have been generated +* 5 ZH: Z high. +* 0: no interrupt +* 1: ZH event has occurred +* 4 ZL: Z low. +* 0: no interrupt +* 1: ZL event has occurred +* 3 YH: Y high. +* 0: no interrupt +* 1: YH event has occurred +* 2 YL: Y low. +* 0: no interrupt +* 1: YL event has occurred +* 1 YH: X high. +* 0: no interrupt +* 1: XH event has occurred +* 0 YL: X low. +* 0: no interrupt +* 1: XL event has occurred +*******************************************************************************/ +#define LIS302DL_FF_WU_SRC2_REG_ADDR 0x35 + +/******************************************************************************* +* FF_WU_THS_2 Register: Threshold register +* Read Write register +* Default value: 0x00 +* 7 DCRM: Reset mode selection. +* 0 - counter resetted +* 1 - counter decremented +* 6 THS6-THS0: Free-fall/wake-up threshold value. +*******************************************************************************/ +#define LIS302DL_FF_WU_THS2_REG_ADDR 0x36 + +/******************************************************************************* +* FF_WU_DURATION_2 Register: duration Register +* Read Write register +* Default value: 0x00 +* 7:0 D7-D0 Duration value. (Duration steps and maximum values depend on the ODR chosen) + ******************************************************************************/ +#define LIS302DL_FF_WU_DURATION2_REG_ADDR 0x37 + +/****************************************************************************** +* CLICK_CFG Register: click Register +* Read Write register +* Default value: 0x00 +* 7 Reserved +* 6 LIR: Latch Interrupt request. +* 0: interrupt request not latched +* 1: interrupt request latched +* 5 Double_Z: Enable interrupt generation on double click event on Z axis. +* 0: disable interrupt request +* 1: enable interrupt request +* 4 Single_Z: Enable interrupt generation on single click event on Z axis. +* 0: disable interrupt request +* 1: enable interrupt request +* 3 Double_Y: Enable interrupt generation on double click event on Y axis. +* 0: disable interrupt request +* 1: enable interrupt request +* 2 Single_Y: Enable interrupt generation on single click event on Y axis. +* 0: disable interrupt request +* 1: enable interrupt request +* 1 Double_X: Enable interrupt generation on double click event on X axis. +* 0: disable interrupt request +* 1: enable interrupt request +* 0 Single_y: Enable interrupt generation on single click event on X axis. +* 0: disable interrupt request +* 1: enable interrupt request + ******************************************************************************/ +#define LIS302DL_CLICK_CFG_REG_ADDR 0x38 + +/****************************************************************************** +* CLICK_SRC Register: click status Register +* Read only register +* Default value: 0x00 +* 7 Reserved +* 6 IA: Interrupt active. +* 0: no interrupt has been generated +* 1: one or more interrupts have been generated +* 5 Double_Z: Double click on Z axis event. +* 0: no interrupt +* 1: Double Z event has occurred +* 4 Single_Z: Z low. +* 0: no interrupt +* 1: Single Z event has occurred +* 3 Double_Y: Y high. +* 0: no interrupt +* 1: Double Y event has occurred +* 2 Single_Y: Y low. +* 0: no interrupt +* 1: Single Y event has occurred +* 1 Double_X: X high. +* 0: no interrupt +* 1: Double X event has occurred +* 0 Single_X: X low. +* 0: no interrupt +* 1: Single X event has occurred +*******************************************************************************/ +#define LIS302DL_CLICK_SRC_REG_ADDR 0x39 + +/******************************************************************************* +* CLICK_THSY_X Register: Click threshold Y and X register +* Read Write register +* Default value: 0x00 +* 7:4 THSy3-THSy0: Click threshold on Y axis, step 0.5g +* 3:0 THSx3-THSx0: Click threshold on X axis, step 0.5g +*******************************************************************************/ +#define LIS302DL_CLICK_THSY_X_REG_ADDR 0x3B + +/******************************************************************************* +* CLICK_THSZ Register: Click threshold Z register +* Read Write register +* Default value: 0x00 +* 7:4 Reserved +* 3:0 THSz3-THSz0: Click threshold on Z axis, step 0.5g +*******************************************************************************/ +#define LIS302DL_CLICK_THSZ_REG_ADDR 0x3C + +/******************************************************************************* +* CLICK_TimeLimit Register: Time Limit register +* Read Write register +* Default value: 0x00 +* 7:0 Dur7-Dur0: Time Limit value, step 0.5g +*******************************************************************************/ +#define LIS302DL_CLICK_TIMELIMIT_REG_ADDR 0x3D + +/******************************************************************************* +* CLICK_Latency Register: Latency register +* Read Write register +* Default value: 0x00 +* 7:0 Lat7-Lat0: Latency value, step 1msec +*******************************************************************************/ +#define LIS302DL_CLICK_LATENCY_REG_ADDR 0x3E + +/******************************************************************************* +* CLICK_Window Register: Window register +* Read Write register +* Default value: 0x00 +* 7:0 Win7-Win0: Window value, step 1msec +*******************************************************************************/ +#define LIS302DL_CLICK_WINDOW_REG_ADDR 0x3F + +/******************************************************************************/ +/**************************** END REGISTER MAPPING ***************************/ +/******************************************************************************/ + +#define LIS302DL_SENSITIVITY_2_3G 18 /* 18 mg/digit*/ +#define LIS302DL_SENSITIVITY_9_2G 72 /* 72 mg/digit*/ + +/** @defgroup Data_Rate_selection + * @{ + */ +#define LIS302DL_DATARATE_100 ((uint8_t)0x00) +#define LIS302DL_DATARATE_400 ((uint8_t)0x80) +/** + * @} + */ + +/** @defgroup Power_Mode_selection + * @{ + */ +#define LIS302DL_LOWPOWERMODE_POWERDOWN ((uint8_t)0x00) +#define LIS302DL_LOWPOWERMODE_ACTIVE ((uint8_t)0x40) +/** + * @} + */ + +/** @defgroup Full_Scale_selection + * @{ + */ +#define LIS302DL_FULLSCALE_2_3 ((uint8_t)0x00) +#define LIS302DL_FULLSCALE_9_2 ((uint8_t)0x20) +/** + * @} + */ + +/** @defgroup Self_Test_selection + * @{ + */ +#define LIS302DL_SELFTEST_NORMAL ((uint8_t)0x00) +#define LIS302DL_SELFTEST_P ((uint8_t)0x10) +#define LIS302DL_SELFTEST_M ((uint8_t)0x08) +/** + * @} + */ + +/** @defgroup Direction_XYZ_selection + * @{ + */ +#define LIS302DL_X_ENABLE ((uint8_t)0x01) +#define LIS302DL_Y_ENABLE ((uint8_t)0x02) +#define LIS302DL_Z_ENABLE ((uint8_t)0x04) +#define LIS302DL_XYZ_ENABLE ((uint8_t)0x07) +/** + * @} + */ + + /** @defgroup SPI_Serial_Interface_Mode_selection + * @{ + */ +#define LIS302DL_SERIALINTERFACE_4WIRE ((uint8_t)0x00) +#define LIS302DL_SERIALINTERFACE_3WIRE ((uint8_t)0x80) +/** + * @} + */ + + /** @defgroup Boot_Mode_selection + * @{ + */ +#define LIS302DL_BOOT_NORMALMODE ((uint8_t)0x00) +#define LIS302DL_BOOT_REBOOTMEMORY ((uint8_t)0x40) +/** + * @} + */ + + /** @defgroup Filtered_Data_Selection_Mode_selection + * @{ + */ +#define LIS302DL_FILTEREDDATASELECTION_BYPASSED ((uint8_t)0x00) +#define LIS302DL_FILTEREDDATASELECTION_OUTPUTREGISTER ((uint8_t)0x20) +/** + * @} + */ + + /** @defgroup High_Pass_Filter_Interrupt_selection + * @{ + */ +#define LIS302DL_HIGHPASSFILTERINTERRUPT_OFF ((uint8_t)0x00) +#define LIS302DL_HIGHPASSFILTERINTERRUPT_1 ((uint8_t)0x04) +#define LIS302DL_HIGHPASSFILTERINTERRUPT_2 ((uint8_t)0x08) +#define LIS302DL_HIGHPASSFILTERINTERRUPT_1_2 ((uint8_t)0x0C) +/** + * @} + */ + + /** @defgroup High_Pass_Filter_selection + * @{ + */ +#define LIS302DL_HIGHPASSFILTER_LEVEL_0 ((uint8_t)0x00) +#define LIS302DL_HIGHPASSFILTER_LEVEL_1 ((uint8_t)0x01) +#define LIS302DL_HIGHPASSFILTER_LEVEL_2 ((uint8_t)0x02) +#define LIS302DL_HIGHPASSFILTER_LEVEL_3 ((uint8_t)0x03) +/** + * @} + */ + + +/** @defgroup latch_Interrupt_Request_selection + * @{ + */ +#define LIS302DL_INTERRUPTREQUEST_NOTLATCHED ((uint8_t)0x00) +#define LIS302DL_INTERRUPTREQUEST_LATCHED ((uint8_t)0x40) +/** + * @} + */ + +/** @defgroup Click_Interrupt_XYZ_selection + * @{ + */ +#define LIS302DL_CLICKINTERRUPT_XYZ_DISABLE ((uint8_t)0x00) +#define LIS302DL_CLICKINTERRUPT_X_ENABLE ((uint8_t)0x01) +#define LIS302DL_CLICKINTERRUPT_Y_ENABLE ((uint8_t)0x04) +#define LIS302DL_CLICKINTERRUPT_Z_ENABLE ((uint8_t)0x10) +#define LIS302DL_CLICKINTERRUPT_XYZ_ENABLE ((uint8_t)0x15) +/** + * @} + */ + +/** @defgroup Double_Click_Interrupt_XYZ_selection + * @{ + */ +#define LIS302DL_DOUBLECLICKINTERRUPT_XYZ_DISABLE ((uint8_t)0x00) +#define LIS302DL_DOUBLECLICKINTERRUPT_X_ENABLE ((uint8_t)0x02) +#define LIS302DL_DOUBLECLICKINTERRUPT_Y_ENABLE ((uint8_t)0x08) +#define LIS302DL_DOUBLECLICKINTERRUPT_Z_ENABLE ((uint8_t)0x20) +#define LIS302DL_DOUBLECLICKINTERRUPT_XYZ_ENABLE ((uint8_t)0x2A) +/** + * @} + */ +/** + * @} + */ + +/** @defgroup STM32F4_DISCOVERY_LIS302DL_Exported_Macros + * @{ + */ +#define LIS302DL_CS_LOW() GPIO_ResetBits(LIS302DL_SPI_CS_GPIO_PORT, LIS302DL_SPI_CS_PIN) +#define LIS302DL_CS_HIGH() GPIO_SetBits(LIS302DL_SPI_CS_GPIO_PORT, LIS302DL_SPI_CS_PIN) +/** + * @} + */ + +/** @defgroup STM32F4_DISCOVERY_LIS302DL_Exported_Functions + * @{ + */ +void LIS302DL_Init(LIS302DL_InitTypeDef *LIS302DL_InitStruct); +void LIS302DL_InterruptConfig(LIS302DL_InterruptConfigTypeDef *LIS302DL_InterruptConfigStruct); +void LIS302DL_FilterConfig(LIS302DL_FilterConfigTypeDef *LIS302DL_FilterConfigStruct); +void LIS302DL_LowpowerCmd(uint8_t LowPowerMode); +void LIS302DL_FullScaleCmd(uint8_t FS_value); +void LIS302DL_DataRateCmd(uint8_t DataRateValue); +void LIS302DL_RebootCmd(void); +void LIS302DL_ReadACC(int32_t* out); +void LIS302DL_Write(uint8_t* pBuffer, uint8_t WriteAddr, uint16_t NumByteToWrite); +void LIS302DL_Read(uint8_t* pBuffer, uint8_t ReadAddr, uint16_t NumByteToRead); + +/* USER Callbacks: This is function for which prototype only is declared in + MEMS accelerometre driver and that should be implemented into user applicaiton. */ +/* LIS302DL_TIMEOUT_UserCallback() function is called whenever a timeout condition + occure during communication (waiting transmit data register empty flag(TXE) + or waiting receive data register is not empty flag (RXNE)). + You can use the default timeout callback implementation by uncommenting the + define USE_DEFAULT_TIMEOUT_CALLBACK in stm32f4_discovery_lis302dl.h file. + Typically the user implementation of this callback should reset MEMS peripheral + and re-initialize communication or in worst case reset all the application. */ +uint32_t LIS302DL_TIMEOUT_UserCallback(void); + +#ifdef __cplusplus +} +#endif + +#endif /* __STM32F4_DISCOVERY_LIS302DL_H */ +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + + +/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ -- 2.30.2