SRD Standelone programmation box: i2c0.h File Reference

| SRD Standelone programmation box 2.0 (16/11/15 10:00:00) Documentation of the SRD standelone programmation box

- Main Page
- Data Structures
- Files

- File List
- Globals

All Data Structures Files Functions Variables Enumerations Enumerator Macros Pages
Macros | Functions
i2c0.h File Reference

Headerfile for I2C0 peripheral driver. More...

`#include "dsp.h"`
`#include "i2c_common.h"`
Include dependency graph for i2c0.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

## Macros

#define | I2C0_SEND_ACK
| Send a ack. More...
#define | I2C0_MASTER_MODE
| Define the I2C mode (Slave or master). More...
#define | I2C0_MULT (0x00)
| I2C constants used to define the baudrate. More...
#define | I2C0_MULT_RATIO (1)
| I2C constants used to define the baudrate. More...
#define | I2C0_ICR (0x1F)
| I2C constants used to define the baudrate. More...
#define | I2C0_ICR_RATIO (240)
| I2C constants used to define the baudrate. More...
#define | I2C0Enable() (I2C0->C1 |= I2C_C1_IICEN_MASK)
| Set/Clear/Read the enable status of the I2C0 module. More...
#define | I2C0Disable() (I2C0->C1 &= ~I2C_C1_IICEN_MASK)
| Set/Clear/Read the enable status of the I2C0 module. More...
#define | I2C0GetEnable() ((I2C0->C1 & I2C_C1_IICEN_MASK) >> I2C_C1_IICEN_SHIFT)
| Set/Clear/Read the enable status of the I2C0 module. More...

## Functions

void | I2C0Init (void)
| Initialized the I2C0 module with the defined parameters. More...
unsigned long | I2C0GetBaudrate (void)
| return the I2C0 baudrate. More...
signed char | I2C0Send (unsigned char address, unsigned char *data, unsigned char dataLength)
| Send a packet on the I2C interface. More...
signed char | I2C0Read (unsigned char address, unsigned char *preambule, unsigned char preambuleLength, unsigned char *data, unsigned char dataLength)
| Read a packet on the I2C interface. More...

## Detailed Description

Headerfile for I2C0 peripheral driver.

Author
  Thibaut LALUQUE 3D-Ouest

Date
  26/09/2014

Version
  1.0

supported device : MKL16Z256VLH4 All Right Reserved.

Definition in file i2c0.h.

## Macro Definition Documentation

#define I2C0_ICR (0x1F)

I2C constants used to define the baudrate.

See the device datasheet to learn how to define the baudrate.

Definition at line 69 of file i2c0.h.

Referenced by I2C0Init().

#define I2C0_ICR_RATIO (240)

I2C constants used to define the baudrate.

See the device datasheet to learn how to define the baudrate.

Definition at line 70 of file i2c0.h.

Referenced by I2C0Init().

#define I2C0_MASTER_MODE

Define the I2C mode (Slave or master).

Definition at line 37 of file i2c0.h.

#define I2C0_MULT (0x00)

I2C constants used to define the baudrate.

See the device datasheet to learn how to define the baudrate.

Definition at line 67 of file i2c0.h.

Referenced by I2C0Init().

#define I2C0_MULT_RATIO (1)

I2C constants used to define the baudrate.

See the device datasheet to learn how to define the baudrate.

Definition at line 68 of file i2c0.h.

Referenced by I2C0Init().

#define I2C0_SEND_ACK

Send a ack.

Uncomment to send ack on I2C bus / comment to never send ack on bus.

Definition at line 58 of file i2c0.h.

#define I2C0Disable | ( |  | ) | (I2C0->C1 &= ~I2C_C1_IICEN_MASK)

Set/Clear/Read the enable status of the I2C0 module.

Definition at line 125 of file i2c0.h.

#define I2C0Enable | ( |  | ) | (I2C0->C1 |= I2C_C1_IICEN_MASK)

Set/Clear/Read the enable status of the I2C0 module.

Definition at line 124 of file i2c0.h.

Referenced by BQ27510DRZRG2_Init().

#define I2C0GetEnable | ( |  | ) | ((I2C0->C1 & I2C_C1_IICEN_MASK) >> I2C_C1_IICEN_SHIFT)

Set/Clear/Read the enable status of the I2C0 module.

Definition at line 126 of file i2c0.h.

Referenced by I2C0Read(), and I2C0Send().

## Function Documentation

unsigned long I2C0GetBaudrate | ( | void |  | ) |

return the I2C0 baudrate.

Returns
  (unsigned long) : baudrate of the I2C0 module.

Definition at line 123 of file i2c0.c.

References I2C0Baudrate.

123 {
124 #ifdef I2C0_MASTER_MODE
125 return I2C0Baudrate;
126 #else
127 return 0;
128 #endif
129 }
I2C0Baudrate
unsigned long I2C0Baudrate
Definition: i2c0.c:69

void I2C0Init | ( | void |  | ) |

Initialized the I2C0 module with the defined parameters.

Definition at line 95 of file i2c0.c.

References I2C0_CLOCK_SOURCE, I2C0_ICR, I2C0_ICR_RATIO, I2C0_MULT, I2C0_MULT_RATIO, I2C0Baudrate, I2C0ClockGate(), and I2C0PinMappingPeripheral().

Referenced by BQ27510DRZRG2_Init().

95 {
96
97 /*=== Enable clock on the pripheral ===*/
98 I2C0ClockGate(1);
99
100 /*=== Make sure I2C module is disable ===*/
101 I2C0->C1 = 0;
102
103 /*=== Pin Mapping ===*/
104 I2C0PinMappingPeripheral();
105
106 I2C0->F = I2C0_MULT<<6 | I2C0_ICR;
107 I2C0Baudrate = I2C0_CLOCK_SOURCE / (I2C0_MULT_RATIO*I2C0_ICR_RATIO);
108
109 I2C0->C1 = 0; //No interrupt / No DMA / No wake up
110
111 #ifdef I2C0_SEND_ACK
112 I2C0->C1 |= I2C_C1_TXAK_MASK;
113 #endif
114
115 I2C0->C2 = 0; //No generalCallAddress / No Address extension / No highDrive
116 I2C0->FLT = 0; //No filter / No stop interrupt
117 I2C0->SMB = 0x00;
118
119 I2C0->SLTL = 0xFF;
120 I2C0->SLTH = 0xFF;
121 }
I2C0PinMappingPeripheral
void I2C0PinMappingPeripheral(void)
Functions to MAP the defined IO of the I2C module x in the rigth pin.
Definition: DSP_mkl16z256vlh4.c:435
I2C0_MULT_RATIO
#define I2C0_MULT_RATIO
I2C constants used to define the baudrate.
Definition: i2c0.h:68
I2C0_ICR_RATIO
#define I2C0_ICR_RATIO
I2C constants used to define the baudrate.
Definition: i2c0.h:70
I2C0Baudrate
unsigned long I2C0Baudrate
Definition: i2c0.c:69
I2C0_MULT
#define I2C0_MULT
I2C constants used to define the baudrate.
Definition: i2c0.h:67
I2C0_ICR
#define I2C0_ICR
I2C constants used to define the baudrate.
Definition: i2c0.h:69
I2C0_CLOCK_SOURCE
#define I2C0_CLOCK_SOURCE
Define clock source for peripehrals.
Definition: DSP_mkl16z256vlh4.h:328
I2C0ClockGate
void I2C0ClockGate(unsigned char bool)
Functions to enable/disable the main clock of the specified I2C.
Definition: DSP_mkl16z256vlh4.c:137

Here is the call graph for this function:

Here is the caller graph for this function:

signed char I2C0Read | ( | unsigned char | address,
|  | unsigned char * | preambule,
|  | unsigned char | preambuleLength,
|  | unsigned char * | data,
|  | unsigned char | dataLength
| ) |  |

Read a packet on the I2C interface.

Parameters

  [in] | address | (unsigned char) : address where the packet will be read.
  [in] | preambule | (unsigned char*) : pointer command send to the slave to request it a read.
  [in] | preambuleLength | (unsigned char) : Length of the preambule data sent to the slave.
  [in] | data | (unsigned char*) : pointer on the data receiver buffer.
  [in] | dataLength | (unsigned char) : length of the data to be read.

Returns
  (signed char) : Return -1 if an error occurs else return 0.

Definition at line 183 of file i2c0.c.

References I2C0BusIsBusy, I2C0ByteIsTransferred, I2C0ClearByteTransfertFlag, I2C0GetAckReceive, I2C0GetEnable, I2C0Send(), I2C0SetDirection, I2C0SetTransmitAck, I2C0Start, I2C0Stop, I2C_ACK, I2C_DATADIR_RECEIVE, I2C_DATADIR_TRANSMIT, and I2C_NACK.

Referenced by BQ_getAvailableEnergy(), BQ_getAverageCurrent(), BQ_getAveragePower(), BQ_getCellTemperature(), BQ_getCellVoltage(), BQ_getCycleCount(), BQ_getFullAvailableCapacity(), BQ_getFullChargeCapacity(), BQ_getMaxLoadCurrent(), BQ_getMaxLoadTimeToEmpty(), BQ_getNominalAvailableCapacity(), BQ_getRemainingCapacity(), BQ_getStandbyCurrent(), BQ_getStandbyTimeToEmpty(), BQ_getStateOfCharge(), BQ_getTimeToEmpty(), BQ_getTimeToFull(), and BQ_getTTEAtConstantPower().

183 {
184 signed char result = 0;
185
186 if(I2C0GetEnable()) {
187
188 /*=== If preambule exist send it ===*/
189 if(preambuleLength != 0) {
190
191 result = I2C0Send(address,preambule, preambuleLength);
192 }
193
194 /*=== Start read operation ===*/
195 if(result != -1) {
196
197 I2C0SetDirection(I2C_DATADIR_TRANSMIT); //Set transmit mode
198
199 while(I2C0BusIsBusy()); //Wait for bus idle
200
201 I2C0ClearByteTransfertFlag(); //Clear byte transferred
202
203 I2C0Start(); //Send start condition
204 I2C0->D = ((address<<1) | 1); //Send address
205
206 while(I2C0ByteIsTransferred() == 0); //Wait for address transmission
207
208 if(I2C0GetAckReceive() == I2C_ACK) { //Wait slave ack
209
210 I2C0SetDirection(I2C_DATADIR_RECEIVE); //Set receive mode
211
212 /*-- dummy read --*/
213 I2C0ClearByteTransfertFlag();
214 I2C0SetTransmitAck(I2C_ACK);
215 unsigned char dummy = I2C0->D;
216 while(I2C0ByteIsTransferred() == 0);
217
218 while(dataLength != 0) {
219
220 if(dataLength == 1) {
221 I2C0SetTransmitAck(I2C_NACK);
222 } else {
223 I2C0SetTransmitAck(I2C_ACK);
224 }
225
226 if(dataLength == 0) {
227 I2C0Stop();
228 }
229
230 I2C0ClearByteTransfertFlag();
231 *data = I2C0->D;
232
233 while(I2C0ByteIsTransferred() == 0 && (I2C0->C1 & I2C_C1_MST_MASK ) != 0 );
234
235 data++;
236 dataLength--;
237
238 }
239
240 } else { //Error return error
241 result = -1;
242 }
243 }
244
245 I2C0Stop();
246
247 } else {
248 result = -1; //I2C is not enable -> return error
249 }
250
251
252 return result;
253
254 }
I2C0SetDirection
#define I2C0SetDirection(I2CDataDir)
Definition: i2c0.c:36
I2C_DATADIR_TRANSMIT
Definition: i2c_common.h:58
I2C0ClearByteTransfertFlag
#define I2C0ClearByteTransfertFlag()
Definition: i2c0.c:51
I2C0GetAckReceive
#define I2C0GetAckReceive()
Definition: i2c0.c:46
I2C0Send
signed char I2C0Send(unsigned char address, unsigned char *data, unsigned char dataLength)
Send a packet on the I2C interface.
Definition: i2c0.c:131
I2C0GetEnable
#define I2C0GetEnable()
Set/Clear/Read the enable status of the I2C0 module.
Definition: i2c0.h:126
I2C0BusIsBusy
#define I2C0BusIsBusy()
Definition: i2c0.c:45
I2C0Start
#define I2C0Start()
Definition: i2c0.c:39
I2C0ByteIsTransferred
#define I2C0ByteIsTransferred()
Definition: i2c0.c:50
I2C0SetTransmitAck
#define I2C0SetTransmitAck(I2CAck)
Definition: i2c0.c:48
I2C_ACK
Definition: i2c_common.h:68
I2C_NACK
Definition: i2c_common.h:69
I2C0Stop
#define I2C0Stop()
Definition: i2c0.c:41
I2C_DATADIR_RECEIVE
Definition: i2c_common.h:57

Here is the call graph for this function:

Here is the caller graph for this function:

signed char I2C0Send | ( | unsigned char | address,
|  | unsigned char * | data,
|  | unsigned char | dataLength
| ) |  |

Send a packet on the I2C interface.

Parameters

  [in] | address | (unsigned char) : address where the packet will be sent.
  [in] | data | (unsigned char*) : pointer on the data to send.
  [in] | dataLength | (unsigned char) : length of the data pointed by data.

Returns
  (signed char) : return 0 if the message have been send. Return -1 if an error occurs.

Definition at line 131 of file i2c0.c.

References I2C0BusIsBusy, I2C0ByteIsTransferred, I2C0ClearByteTransfertFlag, I2C0GetAckReceive, I2C0GetEnable, I2C0SetDirection, I2C0Start, I2C0Stop, I2C_ACK, and I2C_DATADIR_TRANSMIT.

Referenced by BQ_batteryIsConnected(), and I2C0Read().

131 {
132 signed char result = 0;
133
134 if(I2C0GetEnable()) {
135
136 while(I2C0BusIsBusy()); //Wait for bus idle
137
138 I2C0SetDirection(I2C_DATADIR_TRANSMIT);
139
140 I2C0Start();
141
142 I2C0ClearByteTransfertFlag(); //Clear byte transferred
143
144 I2C0->D = ((address<<1) | 0); //Send address
145
146 while(I2C0ByteIsTransferred() == 0); //Wait for address transmission
147
148 while(dataLength) {
149
150 I2C0ClearByteTransfertFlag();
151
152 if(I2C0GetAckReceive() == I2C_ACK) {
153
154 I2C0->D = *data;
155
156 while(I2C0ByteIsTransferred() == 0); //Wait for transmission
157
158 dataLength--;
159 data++;
160
161 } else {
162 dataLength = 0;
163 result = -1;
164 }
165 }
166
167 } else {
168 result = -1;
169 }
170
171 if( result == 0) {
172 if(I2C0GetAckReceive() != I2C_ACK) {
173 result = -1;
174 }
175 }
176
177 I2C0Stop();
178
179 return result;
180 }
I2C0SetDirection
#define I2C0SetDirection(I2CDataDir)
Definition: i2c0.c:36
I2C_DATADIR_TRANSMIT
Definition: i2c_common.h:58
I2C0ClearByteTransfertFlag
#define I2C0ClearByteTransfertFlag()
Definition: i2c0.c:51
I2C0GetAckReceive
#define I2C0GetAckReceive()
Definition: i2c0.c:46
I2C0GetEnable
#define I2C0GetEnable()
Set/Clear/Read the enable status of the I2C0 module.
Definition: i2c0.h:126
I2C0BusIsBusy
#define I2C0BusIsBusy()
Definition: i2c0.c:45
I2C0Start
#define I2C0Start()
Definition: i2c0.c:39
I2C0ByteIsTransferred
#define I2C0ByteIsTransferred()
Definition: i2c0.c:50
I2C_ACK
Definition: i2c_common.h:68
I2C0Stop
#define I2C0Stop()
Definition: i2c0.c:41

Here is the caller graph for this function: