Master Thesis  V1.0
Research and Design of Sensor Node for NMSD Treatment
I2C.c
Go to the documentation of this file.
1 /*
2  * I2C.c
3  *
4  * Created on: Nov 18, 2019
5  * Author: jonac
6  */
7 /***************************************************************************/
15 /* ____ ____ _ __ __ ____ ___
16  * | _ \| _ \ / \ | \/ |/ ___/ _ \
17  * | | | | |_) | / _ \ | |\/| | | | | | |
18  * | |_| | _ < / ___ \| | | | |__| |_| |
19  * |____/|_| \_\/_/ \_\_| |_|\____\___/
20  * research group
21  * dramco.be/
22  *
23  * KU Leuven - Technology Campus Gent,
24  * Gebroeders De Smetstraat 1,
25  * B-9000 Gent, Belgium
26  *
27  * File: iic.c
28  * Created: 2018-03-22
29  * Author: Geoffrey Ottoy
30  *
31  * Description: I2C functionality (wrapper for EFM 32).
32  * Used for interfacing with sensors.
33  */
34 
35 #include <i2cspm.h>
36 #include <em_i2c.h>
37 
38 #include "I2C.h"
39 
40 #include "pinout.h"
41 
42 #define I2C_PORT_LOCATION 1
43 
44 I2CSPM_Init_TypeDef i2cInit = I2CSPM_INIT_DEFAULT;
45 
46 
47 /**************************************************************************/
52 void IIC_Init(void){
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 }
70 
71 void IIC_Enable( bool enable )
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 }
84 
85 /**************************************************************************/
94 void IIC_Reset(void){
95  I2C_Reset(i2cInit.port);
96 }
97 
98 
99 /**************************************************************************/
112 bool IIC_WriteBuffer(uint8_t iicAddress, uint8_t * wBuffer, uint8_t wLength){
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 }
134 
135 /**************************************************************************/
152 bool IIC_ReadBuffer(uint8_t iicAddress, uint8_t * rBuffer, uint8_t rLength){
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 }
171 
172 
173 /**************************************************************************/
190 bool IIC_WriteReadBuffer(uint8_t iicAddress, uint8_t * wBuffer, uint8_t wLength, uint8_t *rBuffer, uint8_t rLength){
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 }
ICM_20948_SCL_PIN
#define ICM_20948_SCL_PIN
Definition: pinout.h:62
i2cInit
I2CSPM_Init_TypeDef i2cInit
Definition: I2C.c:44
IIC_Reset
void IIC_Reset(void)
Resets the I2C interface.
Definition: I2C.c:94
IIC_Enable
void IIC_Enable(bool enable)
Definition: I2C.c:71
IIC_Init
void IIC_Init(void)
Setup I2C functionality.
Definition: I2C.c:52
IIC_WriteBuffer
bool IIC_WriteBuffer(uint8_t iicAddress, uint8_t *wBuffer, uint8_t wLength)
I2C write functionality.
Definition: I2C.c:112
pinout.h
File to keep track of most the pins used, pins for BLE are in own file BLE.h.
ICM_20948_SDA_PORT
#define ICM_20948_SDA_PORT
Definition: pinout.h:59
IIC_ReadBuffer
bool IIC_ReadBuffer(uint8_t iicAddress, uint8_t *rBuffer, uint8_t rLength)
I2C read functionality.
Definition: I2C.c:152
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.
Definition: I2C.c:190
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
I2C.h
I2C function to read-write the I2C bus, based on DRAMCO code.