2013-07-11 17:50:15 +02:00
|
|
|
/**
|
|
|
|
* \addtogroup mbxxx-platform
|
|
|
|
*
|
|
|
|
* @{
|
|
|
|
*/
|
2011-04-01 10:22:29 +02:00
|
|
|
/******************** (C) COPYRIGHT 2009 STMicroelectronics ********************
|
|
|
|
* File Name : hal_led.c
|
|
|
|
* Author : MCD Application Team
|
|
|
|
* Version : V1.0
|
|
|
|
* Date : September 2009
|
2013-07-12 09:53:53 +02:00
|
|
|
* Description : Driver for leds management on STM32W108 MBXXX boards
|
2011-04-01 10:22:29 +02:00
|
|
|
********************************************************************************
|
|
|
|
* 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.
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
/* Includes ------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
#include PLATFORM_HEADER
|
|
|
|
#include "mems.h"
|
|
|
|
#include "timer.h"
|
2013-11-20 14:22:27 +01:00
|
|
|
#include "i2c.h"
|
2011-04-01 10:22:29 +02:00
|
|
|
|
|
|
|
/* Private define -- ---------------------------------------------------------*/
|
|
|
|
#define SUCCESS 1
|
2013-11-20 14:22:27 +01:00
|
|
|
#define FAIL 0
|
2011-04-01 10:22:29 +02:00
|
|
|
|
|
|
|
/* Private variables ---------------------------------------------------------*/
|
|
|
|
static boolean fullscale_state;
|
|
|
|
|
|
|
|
/* Functions -----------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/*******************************************************************************
|
2013-11-20 14:22:27 +01:00
|
|
|
* Function Name : MEMS_Write_Reg
|
|
|
|
* Description : It writes a register on the I2C target
|
|
|
|
* Input : slave addr is the I2C target device
|
|
|
|
* reg_addr is the address of the register to be written
|
|
|
|
* reg_value is the value of the register to be written
|
|
|
|
* Output : None
|
|
|
|
* Return : 1 if the register has been successfully written, 0 otherwise.
|
2011-04-01 10:22:29 +02:00
|
|
|
*******************************************************************************/
|
2013-11-20 14:22:27 +01:00
|
|
|
uint8_t
|
|
|
|
MEMS_Write_Reg (uint8_t slave_addr, uint8_t reg_addr, uint8_t reg_value)
|
|
|
|
{
|
|
|
|
i2c_start();
|
2011-04-01 10:22:29 +02:00
|
|
|
|
2013-11-20 14:22:27 +01:00
|
|
|
/* send the address low byte */
|
|
|
|
i2c_write(slave_addr);
|
2011-04-01 10:22:29 +02:00
|
|
|
|
2013-11-20 14:22:27 +01:00
|
|
|
/* send register address */
|
|
|
|
i2c_write(reg_addr);
|
2011-04-01 10:22:29 +02:00
|
|
|
|
2013-11-20 14:22:27 +01:00
|
|
|
/* send register value */
|
|
|
|
i2c_write(reg_value);
|
2011-04-01 10:22:29 +02:00
|
|
|
|
2013-11-20 14:22:27 +01:00
|
|
|
i2c_stop();
|
2011-04-01 10:22:29 +02:00
|
|
|
|
|
|
|
return SUCCESS;
|
2013-11-20 14:22:27 +01:00
|
|
|
}/* end MEMS_Write_Reg() */
|
2011-04-01 10:22:29 +02:00
|
|
|
|
|
|
|
/*******************************************************************************
|
2013-11-20 14:22:27 +01:00
|
|
|
* Function Name : MEMS_Read_Reg
|
|
|
|
* Description : It reads a register on the I2C target
|
|
|
|
* Input : slave addr is the I2C target device
|
|
|
|
* reg_addr is the address of the register to be read
|
|
|
|
* pBuffer is the storage destination for the read data
|
|
|
|
* NoOfBytes is the amount of data to read
|
|
|
|
* Output : I2C frame
|
|
|
|
* Return : 1 if the register has been successfully read, 0 otherwise.
|
2011-04-01 10:22:29 +02:00
|
|
|
*******************************************************************************/
|
2013-11-20 14:22:27 +01:00
|
|
|
uint8_t
|
|
|
|
MEMS_Read_Reg (uint8_t slave_addr, uint8_t reg_addr, uint8_t *pBuffer,
|
|
|
|
uint8_t NoOfBytes)
|
2011-04-01 10:22:29 +02:00
|
|
|
{
|
2013-11-20 14:22:27 +01:00
|
|
|
uint8_t i, ack, addr = reg_addr;
|
|
|
|
|
2011-04-01 10:22:29 +02:00
|
|
|
if (NoOfBytes > 1)
|
|
|
|
addr += REPETIR;
|
|
|
|
|
2013-11-20 14:22:27 +01:00
|
|
|
i2c_start();
|
2011-04-01 10:22:29 +02:00
|
|
|
|
2013-11-20 14:22:27 +01:00
|
|
|
/* send the address low byte */
|
|
|
|
i2c_write(slave_addr | 0x00);
|
2011-04-01 10:22:29 +02:00
|
|
|
|
2013-11-20 14:22:27 +01:00
|
|
|
i2c_write(addr);
|
2011-04-01 10:22:29 +02:00
|
|
|
|
2013-11-20 14:22:27 +01:00
|
|
|
i2c_start();
|
2011-04-01 10:22:29 +02:00
|
|
|
|
2013-11-20 14:22:27 +01:00
|
|
|
/* send the address low byte */
|
|
|
|
i2c_write(slave_addr | 0x01);
|
2011-04-01 10:22:29 +02:00
|
|
|
|
2013-11-20 14:22:27 +01:00
|
|
|
/* loop receiving the data */
|
|
|
|
for (i = 0; i < NoOfBytes; i++){
|
2011-04-01 10:22:29 +02:00
|
|
|
|
2013-11-20 14:22:27 +01:00
|
|
|
if (i < (NoOfBytes - 1))
|
|
|
|
/* ack on receipt of data */
|
|
|
|
ack = 1;
|
|
|
|
else
|
|
|
|
/* don't ack if last one */
|
|
|
|
ack = 0;
|
2011-04-01 10:22:29 +02:00
|
|
|
|
2013-11-20 14:22:27 +01:00
|
|
|
/* receive data */
|
|
|
|
*(pBuffer+i) = i2c_read(ack);
|
|
|
|
}
|
|
|
|
|
|
|
|
i2c_stop();
|
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}/* end MEMS_Read_Reg() */
|
2011-04-01 10:22:29 +02:00
|
|
|
|
|
|
|
/*******************************************************************************
|
2013-11-20 14:22:27 +01:00
|
|
|
* Function Name : MEMS_Init
|
|
|
|
* Description : It inits mems
|
2011-04-01 10:22:29 +02:00
|
|
|
* Input : None
|
2013-11-20 14:22:27 +01:00
|
|
|
* Output : status
|
|
|
|
* Return : None
|
2011-04-01 10:22:29 +02:00
|
|
|
*******************************************************************************/
|
2013-11-20 14:22:27 +01:00
|
|
|
uint8_t
|
|
|
|
MEMS_Init(void)
|
2011-04-01 10:22:29 +02:00
|
|
|
{
|
2013-11-20 14:22:27 +01:00
|
|
|
TIM2_CCER &= 0xFFFFEEEE;
|
|
|
|
MEMS_Write_Reg (kLIS3L02DQ_SLAVE_ADDR, STATUS_REG, 0x00); //no flag
|
|
|
|
MEMS_Write_Reg (kLIS3L02DQ_SLAVE_ADDR, FF_WU_CFG, 0x00); // all off
|
|
|
|
MEMS_Write_Reg (kLIS3L02DQ_SLAVE_ADDR, DD_CFG, 0x00); // all off
|
|
|
|
MEMS_Write_Reg (kLIS3L02DQ_SLAVE_ADDR, CTRL_REG2, 0x00);
|
|
|
|
MEMS_Write_Reg (kLIS3L02DQ_SLAVE_ADDR, CTRL_REG1, 0x87);
|
|
|
|
fullscale_state = MEMS_LOW_RANGE;
|
2011-04-01 10:22:29 +02:00
|
|
|
|
|
|
|
return 1;
|
2013-11-20 14:22:27 +01:00
|
|
|
}/* end MEMS_Init */
|
2011-04-01 10:22:29 +02:00
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* Function Name : I2C_MEMS_On
|
|
|
|
* Description : It turn on the device.
|
|
|
|
* Input : None
|
|
|
|
* Output : None
|
|
|
|
* Return : 1 if the device has been successfully set to normal mode, 0 otherwise.
|
|
|
|
*******************************************************************************/
|
2013-11-20 14:22:27 +01:00
|
|
|
uint8_t
|
|
|
|
MEMS_On (void)
|
2011-04-01 10:22:29 +02:00
|
|
|
{
|
2013-11-20 14:22:27 +01:00
|
|
|
return MEMS_Write_Reg (kLIS3L02DQ_SLAVE_ADDR, CTRL_REG1, 0xC7);
|
2011-04-01 10:22:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
2013-11-20 14:22:27 +01:00
|
|
|
* Function Name : MEMS_Off
|
2011-04-01 10:22:29 +02:00
|
|
|
* Description : It turn off the device.
|
|
|
|
* Input : None
|
|
|
|
* Output : None
|
|
|
|
* Return : 1 if the device has been successfully set to power-down mode, 0 otherwise.
|
|
|
|
*******************************************************************************/
|
2013-11-20 14:22:27 +01:00
|
|
|
uint8_t
|
|
|
|
MEMS_Off (void)
|
2011-04-01 10:22:29 +02:00
|
|
|
{
|
2013-11-20 14:22:27 +01:00
|
|
|
return MEMS_Write_Reg (kLIS3L02DQ_SLAVE_ADDR, CTRL_REG1, 0x87);
|
2011-04-01 10:22:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* Function Name : I2C_MEMS_SetFullScale
|
|
|
|
* Description : It sets the full-scale range of the device.
|
|
|
|
* Input : range HIGH for high scale selection, LOW for low range.
|
|
|
|
* Output : None
|
|
|
|
* Return : 1 if the device has been successfully set to full scale mode, 0 otherwise.
|
|
|
|
*******************************************************************************/
|
2013-11-20 14:22:27 +01:00
|
|
|
uint8_t
|
|
|
|
MEMS_SetFullScale (boolean range)
|
2011-04-01 10:22:29 +02:00
|
|
|
{
|
2013-07-11 17:50:15 +02:00
|
|
|
uint8_t i2c_buffer;
|
2013-11-20 14:22:27 +01:00
|
|
|
|
|
|
|
MEMS_Read_Reg(kLIS3L02DQ_SLAVE_ADDR, CTRL_REG1, &i2c_buffer, 1);
|
|
|
|
|
2011-04-01 10:22:29 +02:00
|
|
|
if(range==MEMS_HIGH_RANGE){
|
|
|
|
i2c_buffer |= 0x20;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
i2c_buffer &= ~0x20;
|
|
|
|
}
|
|
|
|
|
2013-11-20 14:22:27 +01:00
|
|
|
MEMS_Write_Reg(kLIS3L02DQ_SLAVE_ADDR, CTRL_REG1, i2c_buffer);
|
|
|
|
|
2011-04-01 10:22:29 +02:00
|
|
|
fullscale_state = range;
|
2013-11-20 14:22:27 +01:00
|
|
|
|
2011-04-01 10:22:29 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
2013-11-20 14:22:27 +01:00
|
|
|
* Function Name : MEMS_GetFullScale
|
2011-04-01 10:22:29 +02:00
|
|
|
* Description : It get the full-scale range of the device.
|
|
|
|
* Input : None
|
|
|
|
* Output : None
|
|
|
|
* Return : range HIGH for high scale selection, LOW for low range.
|
|
|
|
*******************************************************************************/
|
2013-11-20 14:22:27 +01:00
|
|
|
boolean
|
|
|
|
MEMS_GetFullScale (void)
|
|
|
|
{
|
2011-04-01 10:22:29 +02:00
|
|
|
return fullscale_state;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
|
2013-07-11 17:50:15 +02:00
|
|
|
/** @} */
|
2013-11-20 14:22:27 +01:00
|
|
|
|