/** @file mems.c * @brief MB851 MEMS drivers * * * */ #include PLATFORM_HEADER #include "hal/hal.h" #include "hal/error.h" #include "hal/micro/mems.h" #define TIMEOUT 20000 #define SUCCESS 1 #define SEND_BYTE(data) do{ SC2_DATA=(data); SC2_TWICTRL1 |= SC_TWISEND; }while(0) #define WAIT_CMD_FIN() do{}while((SC2_TWISTAT&SC_TWICMDFIN)!=SC_TWICMDFIN) #define WAIT_TX_FIN() do{}while((SC2_TWISTAT&SC_TWITXFIN)!=SC_TWITXFIN) #define WAIT_RX_FIN() do{}while((SC2_TWISTAT&SC_TWIRXFIN)!=SC_TWIRXFIN) static uint8_t i2c_MEMS_Init (void); static uint8_t i2c_MEMS_Read (t_mems_data *mems_data); //extern void halInternalResetWatchDog(void); static uint8_t i2c_Send_Frame (uint8_t DeviceAddress, uint8_t *pBuffer, uint8_t NoOfBytes); static uint8_t i2c_Send_Frame (uint8_t DeviceAddress, uint8_t *pBuffer, uint8_t NoOfBytes); uint8_t i2c_write_reg (uint8_t slave_addr, uint8_t reg_addr, uint8_t reg_value); static uint8_t i2c_MEMS_Init (void); static uint8_t i2c_MEMS_Read (t_mems_data *mems_data); /* Functions -----------------------------------------------------------------*/ uint8_t mems_Init(void) { uint8_t ret = 0; // GPIO assignments // PA1: SC2SDA (Serial Data) // PA2: SC2SCL (Serial Clock) //-----SC2 I2C Master GPIO configuration TIM2_CCER &= 0xFFFFEEEE; SC2_MODE = SC2_MODE_I2C; GPIO_PACFGL &= 0xFFFFF00F; GPIO_PACFGL |= 0x00000DD0; SC2_RATELIN = 14; // generates standard 100kbps or 400kbps SC2_RATEEXP = 1; // 3 yields 100kbps; 1 yields 400kbps SC2_TWICTRL1 = 0; // start from a clean state SC2_TWICTRL2 = 0; // start from a clean state ret = i2c_MEMS_Init(); //Add later if really needed #ifdef ST_DBG if (!ret) i2c_DeInit(MEMS_I2C); #endif return ret; }/* end mems_Init */ uint8_t mems_GetValue(t_mems_data *mems_data) { uint8_t i; i = i2c_MEMS_Read(mems_data); return i; }/* end mems_GetValue() */ /* Private Functions ---------------------------------------------------------*/ /******************************************************************************* * Function Name : i2c_Send_Frame * Description : It sends I2C frame * Input : DeviceAddress is the destination device address * pBUffer is the buffer data * NoOfBytes is the number of bytes * Output : None * Return : status *******************************************************************************/ static uint8_t i2c_Send_Frame (uint8_t DeviceAddress, uint8_t *pBuffer, uint8_t NoOfBytes) { uint8_t i, data; SC2_TWICTRL1 |= SC_TWISTART; // send start WAIT_CMD_FIN(); SEND_BYTE(DeviceAddress); // send the address low byte WAIT_TX_FIN(); // loop sending the data for (i=0; i 1) addr += REPETIR; SC2_TWICTRL1 |= SC_TWISTART; // send start WAIT_CMD_FIN(); SEND_BYTE(slave_addr | 0x00); // send the address low byte WAIT_TX_FIN(); SEND_BYTE(addr); WAIT_TX_FIN(); SC2_TWICTRL1 |= SC_TWISTART; // send start WAIT_CMD_FIN(); SEND_BYTE(slave_addr | 0x01); // send the address low byte WAIT_TX_FIN(); // loop receiving the data for (i=0;ioutx_h = i2c_buffer[0]; mems_data->outx_l = i2c_buffer[1]; mems_data->outy_h = i2c_buffer[2]; mems_data->outy_l = i2c_buffer[3]; mems_data->outz_h = i2c_buffer[4]; mems_data->outz_l = i2c_buffer[5]; return i; }/* end i2c_MEMS_Read() */