ISSDK  1.8
IoT Sensing Software Development Kit
register_io_i2c.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 /**
10  * @file register_io_i2c.c
11  * @brief The register_io_i2c.c file contains definitions for low-level interface functions
12  * for reading and writing sensor registers.
13  */
14 
15 /* Standard C Includes */
16 #include <string.h>
17 
18 /* ISSDK Includes */
19 #include "issdk_hal.h"
20 #include "register_io_i2c.h"
21 
22 /*******************************************************************************
23  * Types
24  ******************************************************************************/
25 #define I2C_COUNT (sizeof(i2cBases) / sizeof(void *))
26 
27 /*******************************************************************************
28  * Variables
29  ******************************************************************************/
31 volatile bool b_I2C_CompletionFlag[I2C_COUNT] = {false};
32 volatile uint32_t g_I2C_ErrorEvent[I2C_COUNT] = {ARM_I2C_EVENT_TRANSFER_DONE};
33 
34 /*******************************************************************************
35  * Code
36  ******************************************************************************/
37 
38 #if defined(I2C0)
39 /* The I2C0 Signal Event Handler function. */
40 void I2C0_SignalEvent_t(uint32_t event)
41 {
42  if (event != ARM_I2C_EVENT_TRANSFER_DONE)
43  {
44  g_I2C_ErrorEvent[0] = event;
45  }
46  b_I2C_CompletionFlag[0] = true;
47 }
48 #endif
49 
50 #if defined(I2C1)
51 /* The I2C1 Signal Event Handler function. */
52 void I2C1_SignalEvent_t(uint32_t event)
53 {
54  if (event != ARM_I2C_EVENT_TRANSFER_DONE)
55  {
56  g_I2C_ErrorEvent[1] = event;
57  }
58  b_I2C_CompletionFlag[1] = true;
59 }
60 #endif
61 
62 #if defined(I2C2)
63 /* The I2C2 Signal Event Handler function. */
64 void I2C2_SignalEvent_t(uint32_t event)
65 {
66  if (event != ARM_I2C_EVENT_TRANSFER_DONE)
67  {
68  g_I2C_ErrorEvent[2] = event;
69  }
70  b_I2C_CompletionFlag[2] = true;
71 }
72 #endif
73 
74 #if defined(I2C3)
75 /* The I2C3 Signal Event Handler function. */
76 void I2C3_SignalEvent_t(uint32_t event)
77 {
78  if (event != ARM_I2C_EVENT_TRANSFER_DONE)
79  {
80  g_I2C_ErrorEvent[3] = event;
81  }
82  b_I2C_CompletionFlag[3] = true;
83 }
84 #endif
85 
86 #if defined(I2C4)
87 /* The I2C4 Signal Event Handler function. */
88 void I2C4_SignalEvent_t(uint32_t event)
89 {
90  if (event != ARM_I2C_EVENT_TRANSFER_DONE)
91  {
92  g_I2C_ErrorEvent[4] = event;
93  }
94  b_I2C_CompletionFlag[4] = true;
95 }
96 #endif
97 
98 #if defined(I2C5)
99 /* The I2C5 Signal Event Handler function. */
100 void I2C5_SignalEvent_t(uint32_t event)
101 {
102  if (event != ARM_I2C_EVENT_TRANSFER_DONE)
103  {
104  g_I2C_ErrorEvent[5] = event;
105  }
106  b_I2C_CompletionFlag[5] = true;
107 }
108 #endif
109 
110 #if defined(I2C6)
111 /* The I2C6 Signal Event Handler function. */
112 void I2C6_SignalEvent_t(uint32_t event)
113 {
114  if (event != ARM_I2C_EVENT_TRANSFER_DONE)
115  {
116  g_I2C_ErrorEvent[6] = event;
117  }
118  b_I2C_CompletionFlag[6] = true;
119 }
120 #endif
121 
122 #if defined(I2C7)
123 /* The I2C7 Signal Event Handler function. */
124 void I2C7_SignalEvent_t(uint32_t event)
125 {
126  if (event != ARM_I2C_EVENT_TRANSFER_DONE)
127  {
128  g_I2C_ErrorEvent[7] = event;
129  }
130  b_I2C_CompletionFlag[7] = true;
131 }
132 #endif
133 
134 #ifdef MIMXRT500_AGM01
135 #if defined(I2C11)
136 /* The I2C11 Signal Event Handler function. */
137 void I2C11_SignalEvent_t(uint32_t event)
138 {
139  if (event != ARM_I2C_EVENT_TRANSFER_DONE)
140  {
141  g_I2C_ErrorEvent[11] = event;
142  }
143  b_I2C_CompletionFlag[11] = true;
144 }
145 #endif
146 #endif
147 
148 /*! The interface function to block write sensor registers. */
149 int32_t Register_I2C_BlockWrite(ARM_DRIVER_I2C *pCommDrv,
150  registerDeviceInfo_t *devInfo,
151  uint16_t slaveAddress,
152  uint8_t offset,
153  const uint8_t *pBuffer,
154  uint8_t bytesToWrite)
155 {
156  int32_t status;
157  uint8_t buffer[SENSOR_MAX_REGISTER_COUNT];
158 
159  buffer[0] = offset;
160  memcpy(buffer + 1, pBuffer, bytesToWrite);
161 
162  b_I2C_CompletionFlag[devInfo->deviceInstance] = false;
163  g_I2C_ErrorEvent[devInfo->deviceInstance] = ARM_I2C_EVENT_TRANSFER_DONE;
164  status = pCommDrv->MasterTransmit(slaveAddress, buffer, bytesToWrite + 1, false);
165  if (ARM_DRIVER_OK == status)
166  {
167  /* Wait for completion */
168  while (!b_I2C_CompletionFlag[devInfo->deviceInstance])
169  {
170  if (devInfo->idleFunction)
171  {
172  devInfo->idleFunction(devInfo->functionParam);
173  }
174  else
175  {
176  __NOP();
177  }
178  }
179  if (g_I2C_ErrorEvent[devInfo->deviceInstance] == ARM_I2C_EVENT_TRANSFER_INCOMPLETE)
180  {
181  pCommDrv->Control(ARM_I2C_ABORT_TRANSFER, 0);
182  }
183  if (g_I2C_ErrorEvent[devInfo->deviceInstance] != ARM_I2C_EVENT_TRANSFER_DONE)
184  {
185  status = ARM_DRIVER_ERROR;
186  }
187  }
188 
189  return status;
190 }
191 
192 /*! The interface function to write a sensor register. */
193 int32_t Register_I2C_Write(ARM_DRIVER_I2C *pCommDrv,
194  registerDeviceInfo_t *devInfo,
195  uint16_t slaveAddress,
196  uint8_t offset,
197  uint8_t value,
198  uint8_t mask,
199  bool repeatedStart)
200 {
201  int32_t status;
202  uint8_t config[] = {offset, 0x00};
203 
204  /*! Set the register based on the values in the register value pair configuration.*/
205  if (mask)
206  {
207  b_I2C_CompletionFlag[devInfo->deviceInstance] = false;
208  g_I2C_ErrorEvent[devInfo->deviceInstance] = ARM_I2C_EVENT_TRANSFER_DONE;
209  /*! Send the register address to read from.*/
210  status = pCommDrv->MasterTransmit(slaveAddress, &config[0], 1, true);
211  if (ARM_DRIVER_OK == status)
212  {
213  /* Wait for completion without calling idle function */
214  while (!b_I2C_CompletionFlag[devInfo->deviceInstance])
215  {
216  if (devInfo->idleFunction)
217  {
218  devInfo->idleFunction(devInfo->functionParam);
219  }
220  else
221  {
222  __NOP();
223  }
224  };
225  if (g_I2C_ErrorEvent[devInfo->deviceInstance] == ARM_I2C_EVENT_TRANSFER_INCOMPLETE)
226  {
227  pCommDrv->Control(ARM_I2C_ABORT_TRANSFER, 0);
228  }
229  if (g_I2C_ErrorEvent[devInfo->deviceInstance] != ARM_I2C_EVENT_TRANSFER_DONE)
230  {
231  return ARM_DRIVER_ERROR;
232  }
233  }
234  else
235  {
236  return status;
237  }
238  b_I2C_CompletionFlag[devInfo->deviceInstance] = false;
239  g_I2C_ErrorEvent[devInfo->deviceInstance] = ARM_I2C_EVENT_TRANSFER_DONE;
240  /*! Read the value.*/
241  status = pCommDrv->MasterReceive(slaveAddress, &config[1], 1, false);
242  if (ARM_DRIVER_OK == status)
243  {
244  /* Wait for completion */
245  while (!b_I2C_CompletionFlag[devInfo->deviceInstance])
246  {
247  if (devInfo->idleFunction)
248  {
249  devInfo->idleFunction(devInfo->functionParam);
250  }
251  else
252  {
253  __NOP();
254  }
255  }
256  if (g_I2C_ErrorEvent[devInfo->deviceInstance] == ARM_I2C_EVENT_TRANSFER_INCOMPLETE)
257  {
258  pCommDrv->Control(ARM_I2C_ABORT_TRANSFER, 0);
259  }
260  if (g_I2C_ErrorEvent[devInfo->deviceInstance] != ARM_I2C_EVENT_TRANSFER_DONE)
261  {
262  return ARM_DRIVER_ERROR;
263  }
264  }
265  else
266  {
267  return status;
268  }
269  /*! 'OR' in the requested values to the current contents of the register */
270  config[1] = (config[1] & ~mask) | value;
271  }
272  else
273  {
274  /*! Overwrite the register with specified value.*/
275  config[1] = value;
276  }
277 
278  b_I2C_CompletionFlag[devInfo->deviceInstance] = false;
279  g_I2C_ErrorEvent[devInfo->deviceInstance] = ARM_I2C_EVENT_TRANSFER_DONE;
280  /*! Write the updated value. */
281  status = pCommDrv->MasterTransmit(slaveAddress, config, sizeof(config), repeatedStart);
282  if (ARM_DRIVER_OK == status)
283  {
284  /* Wait for completion */
285  while (!b_I2C_CompletionFlag[devInfo->deviceInstance])
286  {
287  if (devInfo->idleFunction)
288  {
289  devInfo->idleFunction(devInfo->functionParam);
290  }
291  else
292  {
293  __NOP();
294  }
295  }
296  if (g_I2C_ErrorEvent[devInfo->deviceInstance] == ARM_I2C_EVENT_TRANSFER_INCOMPLETE)
297  {
298  pCommDrv->Control(ARM_I2C_ABORT_TRANSFER, 0);
299  }
300  if (g_I2C_ErrorEvent[devInfo->deviceInstance] != ARM_I2C_EVENT_TRANSFER_DONE)
301  {
302  status = ARM_DRIVER_ERROR;
303  }
304  }
305 
306  return status;
307 }
308 
309 /*! The interface function to read a sensor register. */
310 int32_t Register_I2C_Read(ARM_DRIVER_I2C *pCommDrv,
311  registerDeviceInfo_t *devInfo,
312  uint16_t slaveAddress,
313  uint8_t offset,
314  uint8_t length,
315  uint8_t *pOutBuffer)
316 {
317  int32_t status;
318 
319  b_I2C_CompletionFlag[devInfo->deviceInstance] = false;
320  g_I2C_ErrorEvent[devInfo->deviceInstance] = ARM_I2C_EVENT_TRANSFER_DONE;
321  status = pCommDrv->MasterTransmit(slaveAddress, &offset, 1, true);
322  if (ARM_DRIVER_OK == status)
323  {
324  /* Wait for completion without calling idle function. */
325  while (!b_I2C_CompletionFlag[devInfo->deviceInstance])
326  {
327  if (devInfo->idleFunction)
328  {
329  devInfo->idleFunction(devInfo->functionParam);
330  }
331  else
332  {
333  __NOP();
334  }
335  };
336  if (g_I2C_ErrorEvent[devInfo->deviceInstance] == ARM_I2C_EVENT_TRANSFER_INCOMPLETE)
337  {
338  pCommDrv->Control(ARM_I2C_ABORT_TRANSFER, 0);
339  }
340  if (g_I2C_ErrorEvent[devInfo->deviceInstance] != ARM_I2C_EVENT_TRANSFER_DONE)
341  {
342  return ARM_DRIVER_ERROR;
343  }
344  }
345  else
346  {
347  return status;
348  }
349 
350  b_I2C_CompletionFlag[devInfo->deviceInstance] = false;
351  g_I2C_ErrorEvent[devInfo->deviceInstance] = ARM_I2C_EVENT_TRANSFER_DONE;
352  /*! Read and update the value.*/
353  status = pCommDrv->MasterReceive(slaveAddress, pOutBuffer, length, false);
354  if (ARM_DRIVER_OK == status)
355  {
356  /* Wait for completion */
357  while (!b_I2C_CompletionFlag[devInfo->deviceInstance])
358  {
359  if (devInfo->idleFunction)
360  {
361  devInfo->idleFunction(devInfo->functionParam);
362  }
363  else
364  {
365  __NOP();
366  }
367  }
368  if (g_I2C_ErrorEvent[devInfo->deviceInstance] == ARM_I2C_EVENT_TRANSFER_INCOMPLETE)
369  {
370  pCommDrv->Control(ARM_I2C_ABORT_TRANSFER, 0);
371  }
372  if (g_I2C_ErrorEvent[devInfo->deviceInstance] != ARM_I2C_EVENT_TRANSFER_DONE)
373  {
374  status = ARM_DRIVER_ERROR;
375  }
376  }
377 
378  return status;
379 }
This structure defines the device specific info required by register I/O.
Definition: sensor_drv.h:102
int32_t status
int32_t Register_I2C_Read(ARM_DRIVER_I2C *pCommDrv, registerDeviceInfo_t *devInfo, uint16_t slaveAddress, uint8_t offset, uint8_t length, uint8_t *pOutBuffer)
The interface function to read a sensor register.
int32_t Register_I2C_Write(ARM_DRIVER_I2C *pCommDrv, registerDeviceInfo_t *devInfo, uint16_t slaveAddress, uint8_t offset, uint8_t value, uint8_t mask, bool repeatedStart)
The interface function to write a sensor register.
#define I2C_BASE_PTRS
Definition: frdm_ke15z.h:132
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
The register_io_i2c.h file declares low-level interface functions for reading and writing sensor regi...
volatile bool b_I2C_CompletionFlag[I2C_COUNT]
#define SENSOR_MAX_REGISTER_COUNT
Definition: sensor_drv.h:42
#define I2C_Type
Definition: frdm_ke15z.h:130
#define I2C_COUNT
int32_t Register_I2C_BlockWrite(ARM_DRIVER_I2C *pCommDrv, registerDeviceInfo_t *devInfo, uint16_t slaveAddress, uint8_t offset, const uint8_t *pBuffer, uint8_t bytesToWrite)
The interface function to write a sensor register.
volatile uint32_t g_I2C_ErrorEvent[I2C_COUNT]
registeridlefunction_t idleFunction
Definition: sensor_drv.h:104
I2C_Type *const i2cBases[]