1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
/*
* I2C_Comm.c
*
* Created on: Jun 12, 2018
* Author: avi
*/
#include <stdint.h>
#include <stdbool.h>
#include "include.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/tm4c1294ncpdt.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/i2c.h"
#include "inc/hw_i2c.h"
#include "i2c_fifo.h"
#include "i2c.h"
uint32_t LastReadI2C_BASE; unsigned char LastReadaddr; unsigned char* LastReaddata; unsigned int LastReadlen;
uint32_t LastWriteI2C_BASE; unsigned char LastWriteaddr; unsigned char *LastWritedata; unsigned int LastWritelen;
Task_Handle TaskId;
uint32_t Arb_Loss_Count = 0;
//*****************************************************************************
//! Indicates whether or not the I2C bus has timed out.
//!
//! \param ui32Base is the base address of the I2C module.
//!
//! This function returns an indication of whether or not the I2C bus has time
//! out. The I2C Master Timeout Value must be set.
//!
//! \return Returns \b true if the I2C bus has timed out; otherwise, returns
//! \b false.
//*****************************************************************************
char I2CErrMsg[181];
uint32_t I2C_WriteBuff(uint32_t I2C_BASE, unsigned char addr, unsigned char* data, unsigned int len)
{
uint32_t status = OK;
uint32_t I2C_BUSY_DELAY;
if(I2C_BASE == I2C4_BASE)
{
I2C_BUSY_DELAY = I2C_4_BUSY_DELAY;
}
else
{
I2C_BUSY_DELAY = I2C_2_3_BUSY_DELAY;
}
if (I2CMasterBusy(I2C_BASE))
{
ReportWithPackageFilter(CommFilter,"I2C_WriteBuff busy", __FILE__,__LINE__,status, RpMessage, Task_self(), 0);
return 0xFF;
}
I2CMasterSlaveAddrSet(I2C_BASE, addr, false);
I2CMasterDataPut(I2C_BASE, *data);
if (len == 1)
{
I2CMasterControl(I2C_BASE, I2C_MASTER_CMD_SINGLE_SEND);
SysCtlDelay(I2C_BUSY_DELAY);//The CPU waits for the Command processing to begin before it goes to check if the command is processed
status |= I2CMasterErr(I2C_BASE);
while(I2CMasterBusy(I2C_BASE));
}
else
{
// Start sending consecutive data
I2CMasterControl(I2C_BASE, I2C_MASTER_CMD_BURST_SEND_START);
SysCtlDelay(I2C_BUSY_DELAY);//The CPU waits for the Command processing to begin before it goes to check if the command is processed
status |= I2CMasterErr(I2C_BASE);
while(I2CMasterBusy(I2C_BASE));
len--;
data++;
// Continue sending consecutive data
while(len > 1){
I2CMasterDataPut(I2C_BASE, *data);
I2CMasterControl(I2C_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);
SysCtlDelay(I2C_BUSY_DELAY);//The CPU waits for the Command processing to begin before it goes to check if the command is processed
status |= I2CMasterErr(I2C_BASE);
while(I2CMasterBusy(I2C_BASE));
len--;
data++;
}
// Send last piece of data
I2CMasterDataPut(I2C_BASE, *data);
I2CMasterControl(I2C_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);
SysCtlDelay(I2C_BUSY_DELAY);//The CPU waits for the Command processing to begin before it goes to check if the command is processed
status |= I2CMasterErr(I2C_BASE);
while(I2CMasterBusy(I2C_BASE));
}
return status;
}
uint32_t I2C_ReadBuff(uint32_t I2C_BASE, unsigned char addr, unsigned char* data, unsigned int len)
{
uint32_t status = OK;
uint32_t I2C_BUSY_DELAY;
if(I2C_BASE == I2C4_BASE)
{
I2C_BUSY_DELAY = I2C_4_BUSY_DELAY;
}
else
{
I2C_BUSY_DELAY = I2C_2_3_BUSY_DELAY;
}
if (I2CMasterBusy(I2C_BASE))
{
ReportWithPackageFilter(CommFilter,"I2C_ReadBuff busy", __FILE__,__LINE__, status, RpMessage, Task_self(), 0);
return 0xFF;
}
/*if (Task_self()!= I2C_Task_Handle)
{
status = ERROR;
}
status = OK;*/
I2CMasterSlaveAddrSet(I2C_BASE, addr, true);
if (len == 1)
{
I2CMasterControl(I2C_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
//On the TM4C129 we discovered a design behavior that due to higher system clock frequency and lower I2C baud rate,
//the CPU when it starts a I2C Transaction or writes a new I2C Command, for the I2C to respond to it takes time.
//So in effect the I2CMasterBusy statement gets skipped which causes The I2C controller to lose data.
//see: https://e2e.ti.com/support/microcontrollers/other/f/908/t/368493?Why-is-a-delay-needed-before-writing-the-first-byte-from-I2C-Master-
//therefore we need delay before waiting until bus is not busy - it doesn't work as expected without it or with while(!(I2CMasterBusy(I2C0_BASE)
SysCtlDelay(I2C_BUSY_DELAY);//The CPU waits for the Command processing to begin before it goes to check if the command is processed
status |= I2CMasterErr(I2C_BASE);
while(I2CMasterBusy(I2C_BASE));
*data = I2CMasterDataGet(I2C_BASE);
}
else
{
// Start receiving consecutive data
I2CMasterControl(I2C_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START);
SysCtlDelay(I2C_BUSY_DELAY);//The CPU waits for the Command processing to begin before it goes to check if the command is processed
status |= I2CMasterErr(I2C_BASE);
while(I2CMasterBusy(I2C_BASE));
*data = I2CMasterDataGet(I2C_BASE);
len--;
data++;
// Continue receiving consecutive data
while(len > 1){
I2CMasterControl(I2C_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT);
SysCtlDelay(I2C_BUSY_DELAY);//The CPU waits for the Command processing to begin before it goes to check if the command is processed
status |= I2CMasterErr(I2C_BASE);
while(I2CMasterBusy(I2C_BASE));
*data = I2CMasterDataGet(I2C_BASE);
len--;
data++;
}
// Receive last piece of data
I2CMasterControl(I2C_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
SysCtlDelay(I2C_BUSY_DELAY);//The CPU waits for the Command processing to begin before it goes to check if the command is processed
status |= I2CMasterErr(I2C_BASE);
while(I2CMasterBusy(I2C_BASE));
*data = I2CMasterDataGet(I2C_BASE);
}
return status;
}
uint32_t I2C_Write(uint32_t I2C_ID, uint32_t Slave_Add, uint8_t *I2C_Write_buf, uint32_t No_BytesToWrite )
{
uint32_t status = OK;
uint32_t I2C_BASE;
if (I2C_ID == 4)
I2C_BASE = I2C4_BASE;
else
if (I2C_ID == 3)
I2C_BASE = I2C3_BASE;
else
if (I2C_ID == 2)
I2C_BASE = I2C2_BASE;
else
return ERROR;
/* if (Task_self()!=I2C_Task_Handle)
{
ReportWithPackageFilter(CommFilter,"I2C_Write called from different task", __FILE__,__LINE__, I2C_Task_Handle, RpMessage, Task_self(), 0);
}*/
/* if ( (Slave_Add != 0xE0) && (Slave_Add != 0xE4) && (Slave_Add != 0xE3) &
(Slave_Add != 0x40) && (Slave_Add != 0x44) && (Slave_Add != 0x46) &
(Slave_Add != 0x98) && (Slave_Add != 0xA0) )
return ERROR;*/
Slave_Add = Slave_Add>>1;
//Byte2Write = Byte2Write & 0xFF;
status = I2C_WriteBuff(I2C_BASE, Slave_Add, I2C_Write_buf, No_BytesToWrite);
//status = I2C_WriteFifo(I2C_BASE, Slave_Add, buff, No_BytesToWrite);
/*if(No_BytesToWrite <= 2)
{
status = I2C_WriteBuff(I2C_BASE, Slave_Add, I2C_Write_buf, No_BytesToWrite);
}
else
{
status = I2C_WriteFifo(I2C_BASE, Slave_Add, buff, No_BytesToWrite);
}*/
if (status == OK)
{
//LastWriteI2C_BASE = I2C_BASE;
LastWriteaddr = Slave_Add;
LastWritedata = I2C_Write_buf;
LastWritelen = No_BytesToWrite;
TaskId = Task_self();
}
else
{
usnprintf(I2CErrMsg, 180, "I2C Err write 0x%x 0x%x, last good read 0x%x, %d, write 0x%x, %d, T 0x%x 0x%x",status,Slave_Add,
LastReadaddr, LastReadlen,
LastWriteaddr, LastWritelen, Task_self(),TaskId);
Report(I2CErrMsg, __FILE__, __LINE__, status, RpWarning, Slave_Add, 0);
}
//uint32_t I2C_W_buf[256] = {0},i;
// for(i = 0; i < request->n_bytestwrite;i++)
// {
// I2C_W_buf[i] = request->bytestwrite[i];
// }
if (status)
{
if (Arb_Loss_Count++ >= 5)
{
Arb_Loss_Count= 0;
Init_All_I2C();
ReportWithPackageFilter(CommFilter,"Reinitializing I2C master", __FILE__,__LINE__, status, RpMessage, Task_self(), 0);
}
}
/*else
{
if (Arb_Loss_Count)
ReportWithPackageFilter(CommFilter,"Resetting count", __FILE__, Arb_Loss_Count, status, RpMessage, Task_self(), 0);
//Arb_Loss_Count = 0;
}*/
return status;
}
uint32_t I2C_Read(uint32_t I2C_ID, uint32_t Slave_Add, uint8_t *I2C_Read_buf, uint32_t No_BytesToRead )
{
uint32_t status = OK;
uint32_t I2C_BASE;
if (I2C_ID == 4)
I2C_BASE = I2C4_BASE;
else
if (I2C_ID == 3)
I2C_BASE = I2C3_BASE;
else
if (I2C_ID == 2)
I2C_BASE = I2C2_BASE;
else
return ERROR;
/* if ( (Slave_Add != 0xE0) && (Slave_Add != 0xE4) && (Slave_Add != 0xE3) &
(Slave_Add != 0x40) && (Slave_Add != 0x44) && (Slave_Add != 0x46) &
(Slave_Add != 0x98) && (Slave_Add != 0xA0) )
return ERROR;*/
/*if (Task_self()!=I2C_Task_Handle)
{
ReportWithPackageFilter(CommFilter,"I2C_Write called from different task", __FILE__,__LINE__, I2C_Task_Handle, RpMessage, Task_self(), 0);
}*/
Slave_Add = Slave_Add>>1;
//Byte2Write = Byte2Write & 0xFF;
status = I2C_ReadBuff(I2C_BASE, Slave_Add, I2C_Read_buf, No_BytesToRead);
if (status)
{
if (Arb_Loss_Count++ >= 5)
{
Arb_Loss_Count= 0;
Init_All_I2C();
ReportWithPackageFilter(CommFilter,"Reinitializing I2C master", __FILE__,__LINE__, status, RpMessage, Task_self(), 0);
}
}
if (status == OK)
{
//LastReadI2C_BASE = I2C_BASE;
LastReadaddr = Slave_Add;
LastReaddata = I2C_Read_buf;
LastReadlen = No_BytesToRead;
TaskId = Task_self();
}
else
{
usnprintf(I2CErrMsg, 180, "I2C Err read 0x%x 0x%x, last good read 0x%x, %d, write 0x%x, %d, T 0x%x 0x%x",status,Slave_Add,
LastReadaddr, LastReadlen,
LastWriteaddr, LastWritelen, Task_self(),TaskId);
Report(I2CErrMsg, __FILE__, __LINE__, status, RpWarning, Slave_Add, 0);
}
/*else
{
if (Arb_Loss_Count)
ReportWithPackageFilter(CommFilter,"Resetting count", __FILE__, Arb_Loss_Count, status, RpMessage, Task_self(), 0);
//Arb_Loss_Count = 0;
}*/
return status;
}
|