Master Thesis  V1.0
Research and Design of Sensor Node for NMSD Treatment
I2C.c File Reference

I2C function to read-write the I2C bus, based on DRAMCO code. More...

#include <i2cspm.h>
#include <em_i2c.h>
#include "I2C.h"
#include "pinout.h"
Include dependency graph for I2C.c:

Go to the source code of this file.

Macros

#define I2C_PORT_LOCATION   1
 

Functions

void IIC_Init (void)
 Setup I2C functionality. More...
 
void IIC_Enable (bool enable)
 
void IIC_Reset (void)
 Resets the I2C interface. More...
 
bool IIC_WriteBuffer (uint8_t iicAddress, uint8_t *wBuffer, uint8_t wLength)
 I2C write functionality. More...
 
bool IIC_ReadBuffer (uint8_t iicAddress, uint8_t *rBuffer, uint8_t rLength)
 I2C read functionality. More...
 
bool IIC_WriteReadBuffer (uint8_t iicAddress, uint8_t *wBuffer, uint8_t wLength, uint8_t *rBuffer, uint8_t rLength)
 I2C read/write functionality, to read and write at the same time. More...
 

Variables

I2CSPM_Init_TypeDef i2cInit = I2CSPM_INIT_DEFAULT
 

Detailed Description

I2C function to read-write the I2C bus, based on DRAMCO code.

Version
1.0
Author
Jona Cappelle

Definition in file I2C.c.

Macro Definition Documentation

◆ I2C_PORT_LOCATION

#define I2C_PORT_LOCATION   1

Definition at line 42 of file I2C.c.

Function Documentation

◆ IIC_Enable()

void IIC_Enable ( bool  enable)

Definition at line 71 of file I2C.c.

72 {
73  if (enable)
74  {
75  GPIO_PinModeSet(ICM_20948_SDA_PORT, ICM_20948_SDA_PIN, gpioModeWiredAndPullUp, 1);
76  GPIO_PinModeSet(ICM_20948_SCL_PORT, ICM_20948_SCL_PIN, gpioModeWiredAndPullUp, 1);
77  }
78  else
79  {
80  GPIO_PinModeSet(ICM_20948_SDA_PORT, ICM_20948_SDA_PIN, gpioModeDisabled, 1);
81  GPIO_PinModeSet(ICM_20948_SCL_PORT, ICM_20948_SCL_PIN, gpioModeDisabled, 1);
82  }
83 }

References ICM_20948_SCL_PIN, ICM_20948_SCL_PORT, ICM_20948_SDA_PIN, and ICM_20948_SDA_PORT.

◆ IIC_Init()

void IIC_Init ( void  )

Setup I2C functionality.

Definition at line 52 of file I2C.c.

52  {
53 
54  /* We use the standard I2C pins and ports, so no need to adjust */
55  /*
56  i2cInit.port = I2C0;
57  i2cInit.sclPort = gpioPortD;
58  i2cInit.sclPin = 7;
59  i2cInit.sdaPort = gpioPortD;
60  i2cInit.sdaPin = 6;
61  i2cInit.i2cClhr = i2cClockHLRStandard;
62  i2cInit.i2cRefFreq = 0 ;
63  i2cInit.i2cMaxFreq = I2C_FREQ_STANDARD_MAX;
64  i2cInit.portLocation = I2C_PORT_LOCATION; //zie datasheet �controller (hashtag getal in pinout)
65 */
66 //i2cInit.i2cMaxFreq = I2C_FREQ_FAST_MAX;
67 
68  I2CSPM_Init(&i2cInit);
69 }

References i2cInit.

Referenced by ICM_20948_Init().

◆ IIC_ReadBuffer()

bool IIC_ReadBuffer ( uint8_t  iicAddress,
uint8_t *  rBuffer,
uint8_t  rLength 
)

I2C read functionality.

Parameters
[in]iicAddress10bit I2C address
[in]regCommandAddress that needs to be read
[in]rBufferAddress where the read data needs to be stored
[in]rLengthLength of the data that has to be read

Definition at line 152 of file I2C.c.

152  {
153  I2C_TransferSeq_TypeDef seq;
154  I2C_TransferReturn_TypeDef ret;
155 
156  seq.addr = iicAddress;
157  seq.flags = I2C_FLAG_READ;
158 
159  /* Select location/length of data to be read */
160  seq.buf[0].data = rBuffer;
161  seq.buf[0].len = rLength;
162 
163  ret = I2CSPM_Transfer(i2cInit.port, &seq);
164 
165  if (ret != i2cTransferDone) {
166  *rBuffer = 0;
167  return false;
168  }
169  return true;
170 }

References i2cInit.

◆ IIC_Reset()

void IIC_Reset ( void  )

Resets the I2C interface.

Note
Normally not needed, just in case
Parameters
[in]void

Definition at line 94 of file I2C.c.

94  {
95  I2C_Reset(i2cInit.port);
96 }

References i2cInit.

◆ IIC_WriteBuffer()

bool IIC_WriteBuffer ( uint8_t  iicAddress,
uint8_t *  wBuffer,
uint8_t  wLength 
)

I2C write functionality.

Parameters
[in]iicAddress10bit I2C address
[in]wBufferAddress where the data is that needs to be written
[in]wLengthLength of the data that has to be written

Definition at line 112 of file I2C.c.

112  {
113  I2C_TransferSeq_TypeDef seq;
114  I2C_TransferReturn_TypeDef ret;
115  uint8_t i2c_read_data[0];
116 
117  seq.addr = iicAddress;
118  seq.flags = I2C_FLAG_WRITE;
119  /* Point to write buffer (contains command & data) */
120  seq.buf[0].data = wBuffer;
121  seq.buf[0].len = wLength;
122  /* Select location/length of data to be read */
123  seq.buf[1].data = i2c_read_data;
124  seq.buf[1].len = 0;
125 
126  ret = I2CSPM_Transfer(i2cInit.port, &seq);
127 
128  if (ret != i2cTransferDone) {
129  return false;
130  }
131 
132  return true;
133 }

References i2cInit.

Referenced by ICM_20948_bankSelect(), ICM_20948_registerWrite(), and ICM_20948_write_mag_register().

◆ IIC_WriteReadBuffer()

bool IIC_WriteReadBuffer ( uint8_t  iicAddress,
uint8_t *  wBuffer,
uint8_t  wLength,
uint8_t *  rBuffer,
uint8_t  rLength 
)

I2C read/write functionality, to read and write at the same time.

Parameters
[in]iicAddress10bit I2C address
[in]regCommandAddress that needs to be read
[in]rBufferAddress where the read data needs to be stored
[in]rLengthLength of the data that has to be read

Definition at line 190 of file I2C.c.

190  {
191  I2C_TransferSeq_TypeDef seq;
192  I2C_TransferReturn_TypeDef ret;
193 
194  seq.addr = iicAddress;
195  seq.flags = I2C_FLAG_WRITE_READ;
196  /* Point to write buffer (contains command & data) */
197  seq.buf[0].data = wBuffer;
198  seq.buf[0].len = wLength;
199 
200  /* Select location/length of data to be read */
201  seq.buf[1].data = rBuffer;
202  seq.buf[1].len = rLength;
203 
204  ret = I2CSPM_Transfer(i2cInit.port, &seq);
205 
206  if (ret != i2cTransferDone) {
207  *rBuffer = 0;
208  return false;
209  }
210 
211  return true;
212 }

References i2cInit.

Referenced by ICM_20948_read_mag_register(), and ICM_20948_registerRead().

Variable Documentation

◆ i2cInit

I2CSPM_Init_TypeDef i2cInit = I2CSPM_INIT_DEFAULT

Definition at line 44 of file I2C.c.

Referenced by IIC_Init(), IIC_ReadBuffer(), IIC_Reset(), IIC_WriteBuffer(), and IIC_WriteReadBuffer().

ICM_20948_SCL_PIN
#define ICM_20948_SCL_PIN
Definition: pinout.h:62
i2cInit
I2CSPM_Init_TypeDef i2cInit
Definition: I2C.c:44
ICM_20948_SDA_PORT
#define ICM_20948_SDA_PORT
Definition: pinout.h:59
ICM_20948_SDA_PIN
#define ICM_20948_SDA_PIN
Definition: pinout.h:60
ICM_20948_SCL_PORT
#define ICM_20948_SCL_PORT
Definition: pinout.h:61