18 #include PLATFORM_HEADER
29 #define SEND_BYTE(data) do{ SC2_DATA=(data); SC2_TWICTRL1 |= SC_TWISEND; }while(0)
31 #define WAIT_CMD_FIN() { \
33 timer_set(&t, CLOCK_SECOND/100); \
34 while((SC2_TWISTAT&SC_TWICMDFIN)!=SC_TWICMDFIN){ \
35 if(timer_expired(&t)){ \
41 #define WAIT_TX_FIN() { \
43 timer_set(&t, CLOCK_SECOND/100); \
44 while((SC2_TWISTAT&SC_TWITXFIN)!=SC_TWITXFIN){ \
45 if(timer_expired(&t)){ \
50 #define WAIT_RX_FIN() { \
52 timer_set(&t, CLOCK_SECOND/100); \
53 while((SC2_TWISTAT&SC_TWIRXFIN)!=SC_TWIRXFIN){ \
54 if(timer_expired(&t)){ \
61 static boolean fullscale_state;
64 static int8u I2C_MEMS_Init (
void);
66 static int8u I2C_Send_Frame (int8u DeviceAddress, int8u *pBuffer, int8u NoOfBytes);
67 int8u i2c_write_reg (int8u slave_addr, int8u reg_addr, int8u reg_value);
89 TIM2_CCER &= 0xFFFFEEEE;
90 SC2_MODE = SC2_MODE_I2C;
91 GPIO_PACFGL &= 0xFFFFF00F;
92 GPIO_PACFGL |= 0x00000DD0;
99 ret = I2C_MEMS_Init();
101 fullscale_state = MEMS_LOW_RANGE;
106 I2C_DeInit(MEMS_I2C);
139 static int8u I2C_Send_Frame (int8u DeviceAddress, int8u *pBuffer, int8u NoOfBytes)
143 SC2_TWICTRL1 |= SC_TWISTART;
146 SEND_BYTE(DeviceAddress);
150 for (i=0; i<NoOfBytes; i++) {
160 SC2_TWICTRL1 |= SC_TWISTOP;
175 static int8u I2C_Receive_Frame (int8u slave_addr, int8u reg_addr, int8u *pBuffer, int8u NoOfBytes)
177 int8u i, addr = reg_addr;
182 SC2_TWICTRL1 |= SC_TWISTART;
185 SEND_BYTE(slave_addr | 0x00);
191 SC2_TWICTRL1 |= SC_TWISTART;
194 SEND_BYTE(slave_addr | 0x01);
198 for (i=0;i<NoOfBytes;i++){
201 if (i < (NoOfBytes - 1))
202 SC2_TWICTRL2 |= SC_TWIACK;
204 SC2_TWICTRL2 &= ~SC_TWIACK;
206 SC2_TWICTRL1 |= SC_TWIRECV;
208 *(pBuffer+i) = SC2_DATA;
211 SC2_TWICTRL1 |= SC_TWISTOP;
227 int8u i2c_write_reg (int8u slave_addr, int8u reg_addr, int8u reg_value)
231 i2c_buffer[0] = reg_addr;
232 i2c_buffer[1] = reg_value;
234 return I2C_Send_Frame (slave_addr, i2c_buffer, 2);
247 int8u i2c_read_reg (int8u slave_addr, int8u reg_addr, int8u *pBuffer, int8u NoOfBytes)
249 return I2C_Receive_Frame (slave_addr, reg_addr, pBuffer, NoOfBytes);
260 static int8u I2C_MEMS_Init (
void)
264 i += i2c_write_reg (kLIS3L02DQ_SLAVE_ADDR, STATUS_REG, 0x00);
265 i += i2c_write_reg (kLIS3L02DQ_SLAVE_ADDR, FF_WU_CFG, 0x00);
266 i += i2c_write_reg (kLIS3L02DQ_SLAVE_ADDR, DD_CFG, 0x00);
269 i += i2c_write_reg (kLIS3L02DQ_SLAVE_ADDR, CTRL_REG2, 0x00);
271 i += i2c_write_reg (kLIS3L02DQ_SLAVE_ADDR, CTRL_REG1, 0x87);
288 return i2c_write_reg (kLIS3L02DQ_SLAVE_ADDR, CTRL_REG1, 0xC7);
298 int8u MEMS_Off (
void)
300 return i2c_write_reg (kLIS3L02DQ_SLAVE_ADDR, CTRL_REG1, 0x87);
310 int8u MEMS_SetFullScale (
boolean range)
314 if(!i2c_read_reg(kLIS3L02DQ_SLAVE_ADDR, CTRL_REG1, &i2c_buffer, 1))
317 if(range==MEMS_HIGH_RANGE){
324 if(!i2c_write_reg(kLIS3L02DQ_SLAVE_ADDR, CTRL_REG1, i2c_buffer))
327 fullscale_state = range;
340 boolean MEMS_GetFullScale (
void)
342 return fullscale_state;