EFM32 Gecko Software Documentation  efm32g-doc-5.1.2
si1147_i2c.c
Go to the documentation of this file.
1 /***************************************************************************/
16 #include "i2cspm.h"
17 #include "si114x_functions.h"
18 #include "rtcdriver.h"
19 #include "si1147_i2c.h"
20 
21 /*******************************************************************************
22  ************************** GLOBAL FUNCTIONS *******************************
23  ******************************************************************************/
24 
25 
26 
27 /**************************************************************************/
42 uint32_t Si1147_Read_Register(I2C_TypeDef *i2c,uint8_t addr, uint8_t reg, uint8_t *data)
43 {
46  uint8_t i2c_write_data[1];
47 
48  seq.addr = addr;
50  /* Select register to start reading from */
51  i2c_write_data[0] = reg;
52  seq.buf[0].data = i2c_write_data;
53  seq.buf[0].len = 1;
54  /* Select length of data to be read */
55  seq.buf[1].data = data;
56  seq.buf[1].len = 1;
57 
58  ret = I2CSPM_Transfer(i2c, &seq);
59  if (ret != i2cTransferDone)
60  {
61  *data = 0xff;
62  return (uint32_t)ret;
63  }
64  return (uint32_t)0;
65 }
66 
67 /**************************************************************************/
82 uint32_t Si1147_Write_Register(I2C_TypeDef *i2c,uint8_t addr, uint8_t reg, uint8_t data)
83 {
86  uint8_t i2c_write_data[2];
87  uint8_t i2c_read_data[1];
88 
89  seq.addr = addr;
90  seq.flags = I2C_FLAG_WRITE;
91  /* Select register and data to write */
92  i2c_write_data[0] = reg;
93  i2c_write_data[1] = data;
94  seq.buf[0].data = i2c_write_data;
95  seq.buf[0].len = 2;
96  seq.buf[1].data = i2c_read_data;
97  seq.buf[1].len = 0;
98 
99  ret = I2CSPM_Transfer(i2c, &seq);
100  if (ret != i2cTransferDone)
101  {
102  return (uint32_t)ret;
103  }
104  return (uint32_t)0;
105 }
106 
107 /**************************************************************************/
124 uint32_t Si1147_Write_Block_Register(I2C_TypeDef *i2c,uint8_t addr, uint8_t reg, uint8_t length, uint8_t const *data)
125 {
128  uint8_t i2c_write_data[10];
129  uint8_t i2c_read_data[1];
130  int i;
131 
132  seq.addr = addr;
133  seq.flags = I2C_FLAG_WRITE;
134  /* Select register to start writing to*/
135  i2c_write_data[0] = reg;
136  for (i=0; i<length;i++)
137  {
138  i2c_write_data[i+1] = data[i];
139  }
140  seq.buf[0].data = i2c_write_data;
141  seq.buf[0].len = 1+length;
142  seq.buf[1].data = i2c_read_data;
143  seq.buf[1].len = 0;
144 
145  ret = I2CSPM_Transfer(i2c, &seq);
146  if (ret != i2cTransferDone)
147  {
148  return (uint32_t)ret;
149  }
150  return (uint32_t)0;
151 }
152 
153 /**************************************************************************/
170 uint32_t Si1147_Read_Block_Register(I2C_TypeDef *i2c,uint8_t addr, uint8_t reg, uint8_t length, uint8_t *data)
171 {
174  uint8_t i2c_write_data[1];
175 
176  seq.addr = addr;
178  /* Select register to start reading from */
179  i2c_write_data[0] = reg;
180  seq.buf[0].data = i2c_write_data;
181  seq.buf[0].len = 1;
182  /* Select length of data to be read */
183  seq.buf[1].data = data;
184  seq.buf[1].len = length;
185 
186  ret = I2CSPM_Transfer(i2c, &seq);
187  if (ret != i2cTransferDone)
188  {
189  return (uint32_t)ret;
190  }
191  return (uint32_t)0;
192 }
193 
194 /**************************************************************************/
205 int16_t Si114xWriteToRegister(HANDLE si114x_handle, uint8_t address, uint8_t data)
206 {
207  return Si1147_Write_Register(((si114x_i2c_t*)si114x_handle)->i2c,((si114x_i2c_t*)si114x_handle)->addr, address, data);
208 }
209 
210 /**************************************************************************/
219 int16_t Si114xReadFromRegister(HANDLE si114x_handle, uint8_t address)
220 {
221  uint8_t data;
222  Si1147_Read_Register(((si114x_i2c_t*)si114x_handle)->i2c,((si114x_i2c_t*)si114x_handle)->addr, address, &data);
223  return data;
224 }
225 
226 /**************************************************************************/
240  uint8_t address, uint8_t length, uint8_t *values)
241 {
242  return Si1147_Write_Block_Register(((si114x_i2c_t*)si114x_handle)->i2c,((si114x_i2c_t*)si114x_handle)->addr, address, length, values);
243 }
244 
245 /**************************************************************************/
259  uint8_t address, uint8_t length, uint8_t *values)
260 {
261  return Si1147_Read_Block_Register(((si114x_i2c_t*)si114x_handle)->i2c,((si114x_i2c_t*)si114x_handle)->addr, address, length, values);
262 }
263 
264 /**************************************************************************/
268 {
269  // This is needed immediately after a reset command to the Si114x
270  // In the PGM_Toolkit, there is sufficient latency, so none is added
271  // here. This is a reminder that when porting code, that this must
272  // be implemented.
273  RTCDRV_Delay(10);
274 }
275 
276 /**************************************************************************/
279 void delay_1ms()
280 {
281  // This can be used to save power while polling Si114x status
282  // registers by using a sleep delay instead of busy wait.
283  RTCDRV_Delay(1);
284 }
void delay_10ms()
Implements 10ms delay.
Definition: si1147_i2c.c:267
I2C_TransferReturn_TypeDef I2CSPM_Transfer(I2C_TypeDef *i2c, I2C_TransferSeq_TypeDef *seq)
Perform I2C transfer.
Definition: i2cspm.c:124
Si114x function prototypes, structure and bit definitions.
uint32_t Si1147_Read_Block_Register(I2C_TypeDef *i2c, uint8_t addr, uint8_t reg, uint8_t length, uint8_t *data)
Reads a block of data from the Si1147 sensor.
Definition: si1147_i2c.c:170
uint32_t Si1147_Write_Register(I2C_TypeDef *i2c, uint8_t addr, uint8_t reg, uint8_t data)
Writes register in the Si1147 sensor.
Definition: si1147_i2c.c:82
Driver for the Si1147 Proximity sensor.
uint32_t Si1147_Read_Register(I2C_TypeDef *i2c, uint8_t addr, uint8_t reg, uint8_t *data)
Reads register from the Si1147 sensor.
Definition: si1147_i2c.c:42
int16_t Si114xBlockWrite(HANDLE si114x_handle, uint8_t address, uint8_t length, uint8_t *values)
Writes block of Si114x registers.
Definition: si1147_i2c.c:239
#define I2C_FLAG_WRITE
Indicate plain write sequence: S+ADDR(W)+DATA0+P.
Definition: em_i2c.h:124
RTCDRV timer API definition.
I2C_TransferReturn_TypeDef
Definition: em_i2c.h:179
int16_t Si114xWriteToRegister(HANDLE si114x_handle, uint8_t address, uint8_t data)
Writes to Si114x Register.
Definition: si1147_i2c.c:205
void delay_1ms()
Implements 1ms delay.
Definition: si1147_i2c.c:279
I2C simple poll-based master mode driver for the DK/STK.
Ecode_t RTCDRV_Delay(uint32_t ms)
Millisecond delay function.
Definition: rtcdriver.c:270
uint32_t Si1147_Write_Block_Register(I2C_TypeDef *i2c, uint8_t addr, uint8_t reg, uint8_t length, uint8_t const *data)
Writes a block of data to the Si1147 sensor.
Definition: si1147_i2c.c:124
struct I2C_TransferSeq_TypeDef::@0 buf[2]
int16_t Si114xReadFromRegister(HANDLE si114x_handle, uint8_t address)
Reads from Si114x register.
Definition: si1147_i2c.c:219
Master mode transfer message structure used to define a complete I2C transfer sequence (from start to...
Definition: em_i2c.h:252
int16_t Si114xBlockRead(HANDLE si114x_handle, uint8_t address, uint8_t length, uint8_t *values)
Reads block of Si114x registers.
Definition: si1147_i2c.c:258
static void * si114x_handle
Si114x PGM toolkit functions uses a void* to pass hardware parameters through to the lower level i2c ...
#define I2C_FLAG_WRITE_READ
Indicate combined write/read sequence: S+ADDR(W)+DATA0+Sr+ADDR(R)+DATA1+P.
Definition: em_i2c.h:148
uint16_t addr
Address to use after (repeated) start.
Definition: em_i2c.h:262