7 #include PLATFORM_HEADER
10 #include "hal/micro/mems.h"
16 #define SEND_BYTE(data) do{ SC2_DATA=(data); SC2_TWICTRL1 |= SC_TWISEND; }while(0)
18 #define WAIT_CMD_FIN() do{}while((SC2_TWISTAT&SC_TWICMDFIN)!=SC_TWICMDFIN)
19 #define WAIT_TX_FIN() do{}while((SC2_TWISTAT&SC_TWITXFIN)!=SC_TWITXFIN)
20 #define WAIT_RX_FIN() do{}while((SC2_TWISTAT&SC_TWIRXFIN)!=SC_TWIRXFIN)
22 static int8u i2c_MEMS_Init (
void);
23 static int8u i2c_MEMS_Read (
t_mems_data *mems_data);
25 static int8u i2c_Send_Frame (int8u DeviceAddress, int8u *pBuffer, int8u NoOfBytes);
26 static int8u i2c_Send_Frame (int8u DeviceAddress, int8u *pBuffer, int8u NoOfBytes);
27 int8u i2c_write_reg (int8u slave_addr, int8u reg_addr, int8u reg_value);
28 static int8u i2c_MEMS_Init (
void);
29 static int8u i2c_MEMS_Read (
t_mems_data *mems_data);
42 TIM2_CCER &= 0xFFFFEEEE;
43 SC2_MODE = SC2_MODE_I2C;
44 GPIO_PACFGL &= 0xFFFFF00F;
45 GPIO_PACFGL |= 0x00000DD0;
52 ret = i2c_MEMS_Init();
66 i = i2c_MEMS_Read(mems_data);
82 static int8u i2c_Send_Frame (int8u DeviceAddress, int8u *pBuffer, int8u NoOfBytes)
86 SC2_TWICTRL1 |= SC_TWISTART;
89 SEND_BYTE(DeviceAddress);
93 for (i=0; i<NoOfBytes; i++) {
103 SC2_TWICTRL1 |= SC_TWISTOP;
118 static int8u i2c_Receive_Frame (int8u slave_addr, int8u reg_addr, int8u *pBuffer, int8u NoOfBytes)
120 int8u i, addr = reg_addr;
125 SC2_TWICTRL1 |= SC_TWISTART;
128 SEND_BYTE(slave_addr | 0x00);
134 SC2_TWICTRL1 |= SC_TWISTART;
137 SEND_BYTE(slave_addr | 0x01);
141 for (i=0;i<NoOfBytes;i++){
144 if (i < (NoOfBytes - 1))
145 SC2_TWICTRL2 |= SC_TWIACK;
147 SC2_TWICTRL2 &= ~SC_TWIACK;
149 SC2_TWICTRL1 |= SC_TWIRECV;
151 *(pBuffer+i) = SC2_DATA;
154 SC2_TWICTRL1 |= SC_TWISTOP;
171 int8u i2c_write_reg (int8u slave_addr, int8u reg_addr, int8u reg_value)
175 i2c_buffer[0] = reg_addr;
176 i2c_buffer[1] = reg_value;
178 return i2c_Send_Frame (slave_addr, i2c_buffer, 2);
191 int8u i2c_read_reg (int8u slave_addr, int8u reg_addr, int8u *pBuffer, int8u NoOfBytes)
193 return i2c_Receive_Frame (slave_addr, reg_addr, pBuffer, NoOfBytes);
204 static int8u i2c_MEMS_Init (
void)
208 i += i2c_write_reg (kLIS3L02DQ_SLAVE_ADDR, STATUS_REG, 0x00);
209 i += i2c_write_reg (kLIS3L02DQ_SLAVE_ADDR, FF_WU_CFG, 0x00);
210 i += i2c_write_reg (kLIS3L02DQ_SLAVE_ADDR, DD_CFG, 0x00);
211 i += i2c_write_reg (kLIS3L02DQ_SLAVE_ADDR, CTRL_REG2, (0<<4) | (0<<1) | (0 << 0));
212 i += i2c_write_reg (kLIS3L02DQ_SLAVE_ADDR, CTRL_REG1, 0x47);
227 static int8u i2c_MEMS_Read (
t_mems_data *mems_data)
229 int8u i, i2c_buffer[8];
233 i = i2c_read_reg (kLIS3L02DQ_SLAVE_ADDR, STATUS_REG, i2c_buffer, 1);
234 if (i2c_buffer[0] & (1 << 3))
237 i = i2c_read_reg (kLIS3L02DQ_SLAVE_ADDR, OUTX_L, i2c_buffer, 8);
239 mems_data->outx_h = i2c_buffer[0];
240 mems_data->outx_l = i2c_buffer[1];
241 mems_data->outy_h = i2c_buffer[2];
242 mems_data->outy_l = i2c_buffer[3];
243 mems_data->outz_h = i2c_buffer[4];
244 mems_data->outz_l = i2c_buffer[5];