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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
/*
* I2C.c
*
* Created on: Jun 6, 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 "I2C.h"
#include "inc/hw_i2c.h"
//#include "i2c_fifo.h"
#define I2C_DELAY 3000
uint8_t Main_Mux_Flag = UNKNOWN;
//uint32_t I2C_BUSY_DELAY = 100;
uint32_t I2C_2_3_BUSY_DELAY = 3000; //I2C2_BASE, I2C3_BASE (3000 only for first communication changed to 100 in main)
uint32_t I2C_4_BUSY_DELAY = 3000; //I2C4_BASE (RFID + Dispensers) (RFUD working only with 3000)
void InitI2C2()
{
//
//Enable GPIO for Configuring the I2C Interface Pins
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG);
//
// Wait for the Peripheral to be ready for programming
//
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOG));
//
// Configure Pins for I2C2 Master Interface
//
GPIOPinConfigure(GPIO_PG2_I2C2SCL);
GPIOPinConfigure(GPIO_PG3_I2C2SDA);
GPIOPinTypeI2C(GPIO_PORTG_BASE, GPIO_PIN_3);
GPIOPinTypeI2CSCL(GPIO_PORTG_BASE, GPIO_PIN_2);
// GPIOPadConfigSet(GPIO_PORTG_BASE,GPIO_PIN_2,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU); // needed if using the EVB
// GPIOPadConfigSet(GPIO_PORTG_BASE,GPIO_PIN_3,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU);
}
void Configuring_Master_I2C2(/*uint32_t ui32SysClock*/)
{
uint32_t ui32SysClock;
ui32SysClock = SysCtlClockFreqSet((SYSCTL_OSC_MAIN | SYSCTL_USE_PLL | SYSCTL_XTAL_25MHZ |
SYSCTL_CFG_VCO_480), 120000000);
//
// Stop the Clock, Reset and Enable I2C Module
// in Master Function
//
SysCtlPeripheralDisable(SYSCTL_PERIPH_I2C2);
SysCtlPeripheralReset(SYSCTL_PERIPH_I2C2);
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C2);
//
// Wait for the Peripheral to be ready for programming
//
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_I2C2));
//
// Initialize and Configure the Master Module
//
#ifdef I2C_2_Data_Transfer_Rate_400kbps
I2CMasterInitExpClk(I2C2_BASE, ui32SysClock, true);//The last parameter sets the I2C data transfer rate: false for 100kbps and true for 400kbps
#else
I2CMasterInitExpClk(I2C2_BASE, ui32SysClock, false);//The last parameter sets the I2C data transfer rate: false for 100kbps and true for 400kbps
#endif
//i2c_fifo_setup(I2C2_BASE);
}
//--------------------------------------------------------------
void InitI2C3()
{
//
//Enable GPIO for Configuring the I2C Interface Pins
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOR);
//
// Wait for the Peripheral to be ready for programming
//
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOR));
//
// Configure Pins for I2C2 Master Interface
//
GPIOPinConfigure(GPIO_PR4_I2C3SCL);
GPIOPinConfigure(GPIO_PR5_I2C3SDA);
GPIOPinTypeI2C(GPIO_PORTR_BASE, GPIO_PIN_5);
GPIOPinTypeI2CSCL(GPIO_PORTR_BASE, GPIO_PIN_4);
// GPIOPadConfigSet(GPIO_PORTR_BASE,GPIO_PIN_4,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU); // needed if using the EVB
// GPIOPadConfigSet(GPIO_PORTR_BASE,GPIO_PIN_5,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU);
}
void Configuring_Master_I2C3(/*uint32_t ui32SysClock*/)
{
uint32_t ui32SysClock;
ui32SysClock = SysCtlClockFreqSet((SYSCTL_OSC_MAIN | SYSCTL_USE_PLL | SYSCTL_XTAL_25MHZ |
SYSCTL_CFG_VCO_480), 120000000);
//
// Stop the Clock, Reset and Enable I2C Module
// in Master Function
//
SysCtlPeripheralDisable(SYSCTL_PERIPH_I2C3);
SysCtlPeripheralReset(SYSCTL_PERIPH_I2C3);
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C3);
//
// Wait for the Peripheral to be ready for programming
//
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_I2C3));
//
// Initialize and Configure the Master Module
//
#ifdef I2C_3_Data_Transfer_Rate_400kbps
I2CMasterInitExpClk(I2C3_BASE, ui32SysClock, true);//The last parameter sets the I2C data transfer rate: false for 100kbps and true for 400kbps
#else
I2CMasterInitExpClk(I2C3_BASE, ui32SysClock, false);//The last parameter sets the I2C data transfer rate: false for 100kbps and true for 400kbps
#endif
//i2c_fifo_setup(I2C3_BASE);
}
void InitI2C4()
{
//
//Enable GPIO for Configuring the I2C Interface Pins
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOR);
//
// Wait for the Peripheral to be ready for programming
//
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOR));
//
// Configure Pins for I2C2 Master Interface
//
GPIOPinConfigure(GPIO_PR6_I2C4SCL);
GPIOPinConfigure(GPIO_PR7_I2C4SDA);
GPIOPinTypeI2C(GPIO_PORTR_BASE, GPIO_PIN_7);
GPIOPinTypeI2CSCL(GPIO_PORTR_BASE, GPIO_PIN_6);
// GPIOPadConfigSet(GPIO_PORTR_BASE,GPIO_PIN_6,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU); // needed if using the EVB
// GPIOPadConfigSet(GPIO_PORTR_BASE,GPIO_PIN_6,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU);
}
void Configuring_Master_I2C4(/*uint32_t ui32SysClock*/)
{
uint32_t ui32SysClock;
ui32SysClock = SysCtlClockFreqSet((SYSCTL_OSC_MAIN | SYSCTL_USE_PLL | SYSCTL_XTAL_25MHZ |
SYSCTL_CFG_VCO_480), 120000000);
//
// Stop the Clock, Reset and Enable I2C Module
// in Master Function
//
SysCtlPeripheralDisable(SYSCTL_PERIPH_I2C4);
SysCtlPeripheralReset(SYSCTL_PERIPH_I2C4);
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C4);
//
// Wait for the Peripheral to be ready for programming
//
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_I2C4));
//
// Initialize and Configure the Master Module
//
#ifdef I2C_4_Data_Transfer_Rate_400kbps
I2CMasterInitExpClk(I2C4_BASE, ui32SysClock, true);//The last parameter sets the I2C data transfer rate: false for 100kbps and true for 400kbps
#else
I2CMasterInitExpClk(I2C4_BASE, ui32SysClock, false);//The last parameter sets the I2C data transfer rate: false for 100kbps and true for 400kbps
#endif
//i2c_fifo_setup(I2C4_BASE);
}
void Init_All_I2C()
{
//I2C switches address: 0xE0, 0xE4, 0xE3
InitI2C4();
Configuring_Master_I2C4();
//I2C switches address: 0xE0, 0xE4, 0xE3 + I2C-ADC address: 0x40, 0x44, 0x46
InitI2C3();
Configuring_Master_I2C3();
//I2C-DAC address: 0x98 + I2C EEPROM address: 0xA0
InitI2C2();
Configuring_Master_I2C2();
}
void Write_I2C(uint32_t I2C_BASE,uint32_t Slave_Add, uint8_t Byte2Write)
{
//#define SLAVE_ADDRESS_EXT (0xE0>>1)
//Slave_Add = Slave_Add
#define NUM_OF_I2CBYTES 1
uint8_t g_ui8MasterBytes = 1 ;
uint8_t g_ui8MasterTxData = Byte2Write; //TODO
uint8_t g_ui8MasterBytesLength = 1;
// *****************************************************************************
//
// Define for I2C Module
//
// *****************************************************************************
// Send the Slave Address with RnW as Transmit
// and First Data Byte. Based on Number of bytes the
// command would be either START or FINISH
//
I2CMasterSlaveAddrSet(I2C_BASE, Slave_Add, false);
//TODO Add here 'for' to send more than 1 byte
I2CMasterDataPut(I2C_BASE, g_ui8MasterTxData);
if(g_ui8MasterBytes == g_ui8MasterBytesLength)
{
I2CMasterControl(I2C_BASE, I2C_MASTER_CMD_SINGLE_SEND);
}
else
{
I2CMasterControl(I2C_BASE, I2C_MASTER_CMD_BURST_SEND_START);
}
SysCtlDelay(I2C_DELAY);//15000
}
uint8_t Read_I2C(uint32_t I2C_BASE,uint32_t Slave_Add)
{
// Start Read from configured slave
I2CMasterSlaveAddrSet(I2C_BASE, Slave_Add, true);
I2CMasterControl(I2C_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START);
SysCtlDelay(I2C_DELAY);//15000
uint8_t get = I2CMasterDataGet(I2C_BASE);
I2CMasterControl(I2C_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
SysCtlDelay(I2C_DELAY);//15000
//uint8_t get1 = I2CMasterDataGet(I2C4_BASE);
return get;
}
uint8_t Write_Single_I2C(uint32_t I2C_BASE,uint32_t Slave_Add, uint8_t Byte2Write)
{
#define NUM_OF_I2CBYTES 1
//uint8_t g_ui8MasterBytes = 1 ;
uint8_t g_ui8MasterTxData = Byte2Write; //TODO
//uint8_t g_ui8MasterBytesLength = 1;
// *****************************************************************************
//
// Define for I2C Module
//
// *****************************************************************************
// Send the Slave Address with RnW as Transmit
// and First Data Byte. Based on Number of bytes the
// command would be either START or FINISH
//
I2CMasterSlaveAddrSet(I2C_BASE, Slave_Add, false);
I2CMasterDataPut(I2C_BASE, g_ui8MasterTxData);
I2CMasterControl(I2C_BASE, I2C_MASTER_CMD_SINGLE_SEND);
SysCtlDelay(I2C_DELAY);//15000
return OK;
}
uint8_t Read_Single_I2C(uint32_t I2C_BASE,uint32_t Slave_Add)
{
// Start Read from configured slave
I2CMasterSlaveAddrSet(I2C_BASE, Slave_Add, true);
I2CMasterControl(I2C_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
SysCtlDelay(I2C_DELAY);//15000
uint8_t get = I2CMasterDataGet(I2C_BASE);
return get;
}
uint32_t I2C_control(uint32_t I2C_ID, uint32_t Slave_Add, bool RW,uint32_t Byte2Write )
{
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;*/
Slave_Add = Slave_Add>>1;
Byte2Write = Byte2Write & 0xFF;
if(RW == 0 )
{
Write_I2C(I2C_BASE, Slave_Add,Byte2Write);
return 0;
}
else
{
//uint8_t value = Read_I2C(I2C_BASE, Slave_Add);
uint8_t value = Read_Single_I2C(I2C_BASE, Slave_Add);
return value;
}
}
//--------------------------------------------------------------------------------------
#define I2C_ID_MUX_TCA9548A_0xE4 0x03
#define I2C3_MAIN_MUX_TCA9548A_0xE4 0xE4//Slave_Add
uint32_t Block_Main_MUX_TCA9548A_0xE4() ////unused I2C MUX
{
uint32_t status= OK;
uint8_t Write_Buf[1];
Write_Buf[0] = 0x00;//Block all
status = I2C_Write(I2C_ID_MUX_TCA9548A_0xE4, I2C3_MAIN_MUX_TCA9548A_0xE4, Write_Buf, 1);
return status;
}
|