#include "main.h"

#define I2C_Speed              100000

static vu32 dr_temp ;

void GPIO_I2C_Configuration(void)
   {
   GPIO_InitTypeDef  GPIO_InitStructure;

   /* Configure I2C1 pins: SCL and SDA */
   GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6 | GPIO_Pin_7;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
   GPIO_Init(GPIOB, &GPIO_InitStructure);
   }

void I2C_Configuration(void)
   {
   I2C_InitTypeDef  I2C_InitStructure;

   /* I2C configuration */
   I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
   I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
   I2C_InitStructure.I2C_OwnAddress1 = 0xC0;
   I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
   I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
   I2C_InitStructure.I2C_ClockSpeed = I2C_Speed;

   /* I2C Peripheral Enable */
   I2C_Cmd(I2C1, ENABLE);
   /* Apply I2C configuration after enabling it */
   I2C_Init(I2C1, &I2C_InitStructure);

   // Enable MEMS
   MEMS_WriteOneByte ( MEMS_CTRL_REG1, 0xF7) ;

   }

void init_I2C()
   {
   /* GPIO configuration */
   GPIO_I2C_Configuration();

   /* I2C configuration */
   I2C_Configuration();
   }

void MEMS_WriteOneByte(u8 WriteAddr, u8 WriteValue)
   {
   /* Send STRAT condition */
   I2C_GenerateSTART(I2C1, ENABLE);

   /* Test on EV5 and clear it */

   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT))
      {
      if(I2C1->SR1 & I2C_FLAG_RXNE)
         dr_temp = I2C1->DR;
      }

   /* Send MEMS address for write */
   I2C_Send7bitAddress(I2C1, MEMS_ADDR, I2C_Direction_Transmitter);

   /* Test on EV6 and clear it */
   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

   /* Send the MEMS's internal address to write to */
   I2C_SendData(I2C1, WriteAddr);

   /* Test on EV8 and clear it */
   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

   /* Send the byte to be written */
   I2C_SendData(I2C1, WriteValue);

   /* Test on EV8 and clear it */
   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

   /* Send STOP condition */
   I2C_GenerateSTOP(I2C1, ENABLE);
   }

void MEMS_ReadMultByte(u8 ReadAddr, u8 NumByteToRead, u8* pBuffer)
   {
   // Set MSB to enable address auto-increment
   ReadAddr |= 0x80;

   /* Send START condition */
   I2C_GenerateSTART(I2C1, ENABLE);

   /* Test on EV5 and clear it */

   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT))
      {
      if(I2C1->SR1 & I2C_FLAG_RXNE)
         dr_temp = I2C1->DR;
      }

   /* Send MEMS address for write */
   I2C_Send7bitAddress(I2C1, MEMS_ADDR, I2C_Direction_Transmitter);

   /* Test on EV6 and clear it */
   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

   /* Clear EV6 by setting again the PE bit */
   I2C_Cmd(I2C1, ENABLE);

   /* Send the MEMS's internal address to write to */
   I2C_SendData(I2C1, ReadAddr);

   /* Test on EV8 and clear it */
   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

   /* Send STRAT condition a second time */
   I2C_GenerateSTART(I2C1, ENABLE);

   /* Test on EV5 and clear it */
   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));

   /* Send MEMS address for read */
   I2C_Send7bitAddress(I2C1, MEMS_ADDR, I2C_Direction_Receiver);

   /* Test on EV6 and clear it */
   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));

   /* In the case of a single data transfer disable ACK before reading the data */
   if(NumByteToRead==1)
      {
      I2C_AcknowledgeConfig(I2C1, DISABLE);
      }

   /* While there is data to be read */
   while(NumByteToRead)
      {
      /* Test on EV7 and clear it */

      if(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED))
         {

         /* Read a byte from the MEMS */
         *pBuffer = I2C_ReceiveData(I2C1);

         if(NumByteToRead == 2)
            {
            /* Disable Acknowledgement */
            I2C_AcknowledgeConfig(I2C1, DISABLE);
            }

         if(NumByteToRead == 1)
            {
            /* Send STOP Condition */
            I2C_GenerateSTOP(I2C1, ENABLE);
            }

         /* Point to the next location where the byte read will be saved */
         pBuffer++;

         /* Decrement the read bytes counter */
         NumByteToRead--;
         }
      }

   /* Enable Acknowledgement to be ready for another reception */
   I2C_AcknowledgeConfig(I2C1, ENABLE);
   }

u8 MEMS_ReadOneByte(u8 ReadAddr)
   {
   u8 val;
   MEMS_ReadMultByte(ReadAddr, 1, &val);
   return val;
   }
