Contiki 3.x
mems.c
1 /**
2  * \addtogroup mbxxx-platform
3  *
4  * @{
5  */
6 /******************** (C) COPYRIGHT 2009 STMicroelectronics ********************
7 * File Name : hal_led.c
8 * Author : MCD Application Team
9 * Version : V1.0
10 * Date : September 2009
11 * Description : Driver for leds management on STM32W108 MBXXX boards
12 ********************************************************************************
13 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
14 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
15 * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
16 * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
17 * CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
18 * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
19 *******************************************************************************/
20 
21 /* Includes ------------------------------------------------------------------*/
22 
23 #include PLATFORM_HEADER
24 #include "mems.h"
25 #include "timer.h"
26 #include "i2c.h"
27 
28 /* Private define -- ---------------------------------------------------------*/
29 #define SUCCESS 1
30 #define FAIL 0
31 
32 /* Private variables ---------------------------------------------------------*/
33 static boolean fullscale_state;
34 
35 /* Functions -----------------------------------------------------------------*/
36 
37 /*******************************************************************************
38 * Function Name : MEMS_Write_Reg
39 * Description : It writes a register on the I2C target
40 * Input : slave addr is the I2C target device
41 * reg_addr is the address of the register to be written
42 * reg_value is the value of the register to be written
43 * Output : None
44 * Return : 1 if the register has been successfully written, 0 otherwise.
45 *******************************************************************************/
46 uint8_t
47 MEMS_Write_Reg (uint8_t slave_addr, uint8_t reg_addr, uint8_t reg_value)
48 {
49  i2c_start();
50 
51  /* send the address low byte */
52  i2c_write(slave_addr);
53 
54  /* send register address */
55  i2c_write(reg_addr);
56 
57  /* send register value */
58  i2c_write(reg_value);
59 
60  i2c_stop();
61 
62  return SUCCESS;
63 }/* end MEMS_Write_Reg() */
64 
65 /*******************************************************************************
66 * Function Name : MEMS_Read_Reg
67 * Description : It reads a register on the I2C target
68 * Input : slave addr is the I2C target device
69 * reg_addr is the address of the register to be read
70 * pBuffer is the storage destination for the read data
71 * NoOfBytes is the amount of data to read
72 * Output : I2C frame
73 * Return : 1 if the register has been successfully read, 0 otherwise.
74 *******************************************************************************/
75 uint8_t
76 MEMS_Read_Reg (uint8_t slave_addr, uint8_t reg_addr, uint8_t *pBuffer,
77  uint8_t NoOfBytes)
78 {
79  uint8_t i, ack, addr = reg_addr;
80 
81  if (NoOfBytes > 1)
82  addr += REPETIR;
83 
84  i2c_start();
85 
86  /* send the address low byte */
87  i2c_write(slave_addr | 0x00);
88 
89  i2c_write(addr);
90 
91  i2c_start();
92 
93  /* send the address low byte */
94  i2c_write(slave_addr | 0x01);
95 
96  /* loop receiving the data */
97  for (i = 0; i < NoOfBytes; i++){
98 
99  if (i < (NoOfBytes - 1))
100  /* ack on receipt of data */
101  ack = 1;
102  else
103  /* don't ack if last one */
104  ack = 0;
105 
106  /* receive data */
107  *(pBuffer+i) = i2c_read(ack);
108  }
109 
110  i2c_stop();
111 
112  return SUCCESS;
113 }/* end MEMS_Read_Reg() */
114 
115 /*******************************************************************************
116 * Function Name : MEMS_Init
117 * Description : It inits mems
118 * Input : None
119 * Output : status
120 * Return : None
121 *******************************************************************************/
122 uint8_t
123 MEMS_Init(void)
124 {
125  TIM2_CCER &= 0xFFFFEEEE;
126  MEMS_Write_Reg (kLIS3L02DQ_SLAVE_ADDR, STATUS_REG, 0x00); //no flag
127  MEMS_Write_Reg (kLIS3L02DQ_SLAVE_ADDR, FF_WU_CFG, 0x00); // all off
128  MEMS_Write_Reg (kLIS3L02DQ_SLAVE_ADDR, DD_CFG, 0x00); // all off
129  MEMS_Write_Reg (kLIS3L02DQ_SLAVE_ADDR, CTRL_REG2, 0x00);
130  MEMS_Write_Reg (kLIS3L02DQ_SLAVE_ADDR, CTRL_REG1, 0x87);
131  fullscale_state = MEMS_LOW_RANGE;
132 
133  return 1;
134 }/* end MEMS_Init */
135 
136 /*******************************************************************************
137 * Function Name : I2C_MEMS_On
138 * Description : It turn on the device.
139 * Input : None
140 * Output : None
141 * Return : 1 if the device has been successfully set to normal mode, 0 otherwise.
142 *******************************************************************************/
143 uint8_t
144 MEMS_On (void)
145 {
146  return MEMS_Write_Reg (kLIS3L02DQ_SLAVE_ADDR, CTRL_REG1, 0xC7);
147 }
148 
149 /*******************************************************************************
150 * Function Name : MEMS_Off
151 * Description : It turn off the device.
152 * Input : None
153 * Output : None
154 * Return : 1 if the device has been successfully set to power-down mode, 0 otherwise.
155 *******************************************************************************/
156 uint8_t
157 MEMS_Off (void)
158 {
159  return MEMS_Write_Reg (kLIS3L02DQ_SLAVE_ADDR, CTRL_REG1, 0x87);
160 }
161 
162 /*******************************************************************************
163 * Function Name : I2C_MEMS_SetFullScale
164 * Description : It sets the full-scale range of the device.
165 * Input : range HIGH for high scale selection, LOW for low range.
166 * Output : None
167 * Return : 1 if the device has been successfully set to full scale mode, 0 otherwise.
168 *******************************************************************************/
169 uint8_t
170 MEMS_SetFullScale (boolean range)
171 {
172  uint8_t i2c_buffer;
173 
174  MEMS_Read_Reg(kLIS3L02DQ_SLAVE_ADDR, CTRL_REG1, &i2c_buffer, 1);
175 
176  if(range==MEMS_HIGH_RANGE){
177  i2c_buffer |= 0x20;
178  }
179  else {
180  i2c_buffer &= ~0x20;
181  }
182 
183  MEMS_Write_Reg(kLIS3L02DQ_SLAVE_ADDR, CTRL_REG1, i2c_buffer);
184 
185  fullscale_state = range;
186 
187  return 1;
188 }
189 
190 /*******************************************************************************
191 * Function Name : MEMS_GetFullScale
192 * Description : It get the full-scale range of the device.
193 * Input : None
194 * Output : None
195 * Return : range HIGH for high scale selection, LOW for low range.
196 *******************************************************************************/
197 boolean
198 MEMS_GetFullScale (void)
199 {
200  return fullscale_state;
201 }
202 
203 /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
204 /** @} */
205 
void i2c_write(uint8_t data)
Send a byte to I2C bus.
Definition: i2c.c:106
void i2c_stop(void)
Generate I2C STOP condition.
Definition: i2c.c:95
uint8_t i2c_read(int ack)
Read a byte from I2C bus.
Definition: i2c.c:121
void i2c_start(void)
Generate I2C START condition.
Definition: i2c.c:85