si1147_i2c.c

Go to the documentation of this file.
00001 /***************************************************************************/
00016 #include "i2cspm.h"
00017 #include "si114x_functions.h"
00018 #include "rtcdriver.h"
00019 #include "si1147_i2c.h"
00020 
00021 /*******************************************************************************
00022  **************************   GLOBAL FUNCTIONS   *******************************
00023  ******************************************************************************/
00024 
00025 
00026 
00027 /**************************************************************************/
00042 uint32_t Si1147_Read_Register(I2C_TypeDef *i2c,uint8_t addr, uint8_t reg, uint8_t *data)
00043 {
00044   I2C_TransferSeq_TypeDef    seq;
00045   I2C_TransferReturn_TypeDef ret;
00046   uint8_t i2c_write_data[1];
00047 
00048   seq.addr  = addr;
00049   seq.flags = I2C_FLAG_WRITE_READ;
00050   /* Select register to start reading from */
00051   i2c_write_data[0] = reg;
00052   seq.buf[0].data = i2c_write_data;
00053   seq.buf[0].len  = 1;
00054   /* Select length of data to be read */
00055   seq.buf[1].data = data;
00056   seq.buf[1].len  = 1;
00057 
00058   ret = I2CSPM_Transfer(i2c, &seq);
00059   if (ret != i2cTransferDone)
00060   {
00061     *data = 0xff;
00062     return (uint32_t)ret;
00063   }
00064   return (uint32_t)0;
00065 }
00066 
00067 /**************************************************************************/
00082 uint32_t Si1147_Write_Register(I2C_TypeDef *i2c,uint8_t addr, uint8_t reg, uint8_t data)
00083 {
00084   I2C_TransferSeq_TypeDef    seq;
00085   I2C_TransferReturn_TypeDef ret;
00086   uint8_t i2c_write_data[2];
00087   uint8_t i2c_read_data[1];
00088 
00089   seq.addr  = addr;
00090   seq.flags = I2C_FLAG_WRITE;
00091   /* Select register and data to write */
00092   i2c_write_data[0] = reg;
00093   i2c_write_data[1] = data;
00094   seq.buf[0].data = i2c_write_data;
00095   seq.buf[0].len  = 2;
00096   seq.buf[1].data = i2c_read_data;
00097   seq.buf[1].len  = 0;
00098 
00099   ret = I2CSPM_Transfer(i2c, &seq);
00100   if (ret != i2cTransferDone)
00101   {
00102     return (uint32_t)ret;
00103   }
00104   return (uint32_t)0;
00105 }
00106 
00107 /**************************************************************************/
00124 uint32_t Si1147_Write_Block_Register(I2C_TypeDef *i2c,uint8_t addr, uint8_t reg, uint8_t length, uint8_t const *data)
00125 {
00126   I2C_TransferSeq_TypeDef    seq;
00127   I2C_TransferReturn_TypeDef ret;
00128   uint8_t i2c_write_data[10];
00129   uint8_t i2c_read_data[1];
00130   int i;
00131 
00132   seq.addr  = addr;
00133   seq.flags = I2C_FLAG_WRITE;
00134   /* Select register to start writing to*/
00135   i2c_write_data[0] = reg;
00136   for (i=0; i<length;i++)
00137   {
00138     i2c_write_data[i+1] = data[i];
00139   }
00140   seq.buf[0].data = i2c_write_data;
00141   seq.buf[0].len  = 1+length;
00142   seq.buf[1].data = i2c_read_data;
00143   seq.buf[1].len  = 0;
00144 
00145   ret = I2CSPM_Transfer(i2c, &seq);
00146   if (ret != i2cTransferDone)
00147   {
00148     return (uint32_t)ret;
00149   }
00150   return (uint32_t)0;
00151 }
00152 
00153 /**************************************************************************/
00170 uint32_t Si1147_Read_Block_Register(I2C_TypeDef *i2c,uint8_t addr, uint8_t reg, uint8_t length, uint8_t *data)
00171 {
00172   I2C_TransferSeq_TypeDef    seq;
00173   I2C_TransferReturn_TypeDef ret;
00174   uint8_t i2c_write_data[1];
00175 
00176   seq.addr  = addr;
00177   seq.flags = I2C_FLAG_WRITE_READ;
00178   /* Select register to start reading from */
00179   i2c_write_data[0] = reg;
00180   seq.buf[0].data = i2c_write_data;
00181   seq.buf[0].len  = 1;
00182   /* Select length of data to be read */
00183   seq.buf[1].data = data;
00184   seq.buf[1].len  = length;
00185 
00186   ret = I2CSPM_Transfer(i2c, &seq);
00187   if (ret != i2cTransferDone)
00188   {
00189     return (uint32_t)ret;
00190   }
00191   return (uint32_t)0;
00192 }
00193 
00194 /**************************************************************************/
00205 int16_t Si114xWriteToRegister(HANDLE si114x_handle, uint8_t address, uint8_t data)
00206 {
00207   return Si1147_Write_Register(((si114x_i2c_t*)si114x_handle)->i2c,((si114x_i2c_t*)si114x_handle)->addr, address, data);
00208 }
00209 
00210 /**************************************************************************/
00219 int16_t Si114xReadFromRegister(HANDLE si114x_handle, uint8_t address)
00220 {
00221   uint8_t data;
00222   Si1147_Read_Register(((si114x_i2c_t*)si114x_handle)->i2c,((si114x_i2c_t*)si114x_handle)->addr, address, &data);
00223   return data;
00224 }
00225 
00226 /**************************************************************************/
00239 int16_t Si114xBlockWrite(HANDLE si114x_handle,
00240                      uint8_t address, uint8_t length, uint8_t *values)
00241 {
00242   return Si1147_Write_Block_Register(((si114x_i2c_t*)si114x_handle)->i2c,((si114x_i2c_t*)si114x_handle)->addr, address, length, values);
00243 }
00244 
00245 /**************************************************************************/
00258 int16_t Si114xBlockRead(HANDLE si114x_handle,
00259                     uint8_t address, uint8_t length, uint8_t *values)
00260 {
00261   return Si1147_Read_Block_Register(((si114x_i2c_t*)si114x_handle)->i2c,((si114x_i2c_t*)si114x_handle)->addr, address, length, values);
00262 }
00263 
00264 /**************************************************************************/
00267 void delay_10ms()
00268 {
00269   // This is needed immediately after a reset command to the Si114x
00270   // In the PGM_Toolkit, there is sufficient latency, so none is added
00271   // here. This is a reminder that when porting code, that this must
00272   // be implemented.
00273   RTCDRV_Delay(10);
00274 }
00275 
00276 /**************************************************************************/
00279 void delay_1ms()
00280 {
00281   // This can be used to save power while polling Si114x status
00282   // registers by using a sleep delay instead of busy wait.
00283   RTCDRV_Delay(1);
00284 }