-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapb-i2c
557 lines (398 loc) · 11.6 KB
/
apb-i2c
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/******************************************************************************
*
* Copyright (C) 2009 - 2014 Xilinx, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* Use of the Software is limited solely to applications:
* (a) running on a Xilinx device, or
* (b) that uint8_teract with a Xilinx device through a bus or uint8_terconnect.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* XILINX BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Except as contained in this notice, the name of the Xilinx shall not be used
* in advertising or otherwise to promote the sale, use or other dealings in
* this Software without prior written authorization from Xilinx.
*
******************************************************************************/
/*
* helloworld.c: simple test application
*
* This application configures UART 16550 to baud rate 9600.
* PS7 UART (Zynq) is not initialized by this application, since
* bootrom/bsp configures it to baud rate 115200
*
* ------------------------------------------------
* | UART TYPE BAUD RATE |
* ------------------------------------------------
* uartns550 9600
* uartlite Configurable only in HW design
* ps7_uart 115200 (configured by bootrom/bsp)
*/
#include <stdio.h>
#include "platform.h"
#include "xil_printf.h"
#include "xparameters.h"
#include "xil_io.h"
#define ENABLE_DEBUG_LOG // disable DEBUG_LOG
#ifdef ENABLE_DEBUG_LOG
#define LOG_D(x) { printf("DEBUG :"); printf(x);}
#else
#define LOG_D(x) // nothing
#endif
#define BASEADDR XPAR_ABP_WRAPPER_0_BASEADDR
#define REG_CR 0
#define REG_SR0 1
#define REG_SR1 2
#define REG_IER 3
#define REG_TxFF 4
#define REG_RxFF 5
#define REG_CHL 6
#define REG_CHH 7
#define REG_CHHL 8
#define REG_CHHH 9
#define REG_TxCLR 10
#define START 0x01
#define STOP 0x02
#define FULL 16
//#define ENABLE_CLOCK_INFO
#define SYSTEM_FREQ 25000000
#define SCL_FREQ 100000
// I2C Standard----
#define WRITE 0
#define READ 1
uint8_t init_value;
uint8_t global_i;
uint8_t wr_arr[16];
uint8_t rd_arr[16];
//------------Checking a bit is Set (1) or not -------------------
uint8_t isKBitSet(uint8_t n, uint8_t k)
{
if (n & (1 << (k - 1)))
return 1;
else
return 0;
}
//----------------Setting frequency---------------------
void set_clock(long SYS_freq, long SCL_freq)
{
long cal_ch;
long cal_chh;
uint8_t chl,chh,chhl,chhh;
uint8_t value=0;
cal_ch = SYS_freq/(2*SCL_freq);
cal_chh = SYS_freq/(4*SCL_freq);
chl = cal_ch;
chh = (0xFF00 & cal_ch)>>8;
chhl = cal_chh;
chhh = (0xFF00 & cal_chh)>>8;
Xil_Out8(BASEADDR+REG_CHL, chl);
Xil_Out8(BASEADDR+REG_CHH, chh);
Xil_Out8(BASEADDR+REG_CHHL, chhl);
Xil_Out8(BASEADDR+REG_CHHH, chhh);
#ifdef ENABLE_CLOCK_INFO
value = Xil_In8(BASEADDR+REG_CHL);
printf(" CHL data : 0x%x \n", value);
value = Xil_In8(BASEADDR+REG_CHH);
printf(" CHH data : 0x%x \n", value);
value = Xil_In8(BASEADDR+REG_CHHL);
printf(" CHHL data : 0x%x \n", value);
value = Xil_In8(BASEADDR+REG_CHHH);
printf(" CHHH data : 0x%x \n", value);
#endif
}
//---------------Checking TxFIFO is empty or not
uint8_t chk_clr_TxFIFo()
{
uint8_t value=0;
value = Xil_In8(BASEADDR+REG_SR0);
// printf("Read Value : 0x%x \n", value);
if(isKBitSet(value,4))
{
LOG_D(" TxFF is Empty \n");
return 1;
}
else
{
LOG_D(" TxFF is Not Empty \n");
LOG_D(" Clearing the FIFO. \n");
Xil_Out8(BASEADDR+REG_TxCLR, 0xFF);
chk_clr_TxFIFo();
}
return 0;
}
//-------------------Check FIFO Full-----------------
uint8_t Is_FIFO_Full()
{
uint8_t value=0;
value = Xil_In8(BASEADDR+REG_SR0);
if(isKBitSet(value,3))
{
LOG_D(" TxFF is Full \n");
return 1;
}
else
// { printf(" TxFF is Not Full... Continue \n"); }
return 0;
}
//----------------checking the STOP FLAG--------------------
uint8_t stop_flag()
{
uint8_t value=0;
value = Xil_In8(BASEADDR+REG_SR0);
if(isKBitSet(value,2))
{
LOG_D(" Stop Flag Sent \n");
return 1;
}
else
// printf(" TxFF is Not Full... Continue \n");
return 0;
}
uint8_t Is_TxCompl()
{
uint8_t value=0;
value = Xil_In8(BASEADDR+REG_SR0);
if(isKBitSet(value,5))
{
LOG_D(" Tx is Completed \n");
return 1;
}
else
// { printf(" TxFF is Not Full... Continue \n"); }
return 0;
}
//------ START or STOP operation------------------
void start_stop_write(uint8_t strt_stp)
{
Xil_Out8(BASEADDR, strt_stp);
}
//-----------------------------------------------------------------
void slv_addr (uint8_t addr, uint8_t rw)
{
// rw = 0 : Write
// = 1 : Read
uint8_t addr_tr = (addr & 0xFE);
uint8_t addr_db = addr_tr+rw;
Xil_Out8(BASEADDR+REG_TxFF, addr_db );
}
//----check NACK---------------------
uint8_t Is_NACK()
{
uint8_t value=0;
value = Xil_In8(BASEADDR+REG_SR1);
// printf("Read Value : 0x%x \n", value);
if(isKBitSet(value,1))
{
LOG_D("Received NACK \n");
return 1;
}
else
{
//printf(" No NACK ... \n ");
return 0;
}
}
//-----------------Writing continous data uint8_to TxFF while it is full
void write_data_fifo (uint8_t value, uint8_t No_txn)
{
uint8_t i=0;
while(!Is_FIFO_Full()) {
Xil_Out8(BASEADDR+REG_TxFF, value+i);
// printf(" Write data : %d \n", value+i);
++i;
if(i==No_txn)
break;
}
// printf(" No of operation performed is : %d \n", i);
}
void write_random_data (uint8_t start_add, uint8_t No_txn)
{
uint8_t val=0;
while(No_txn) {
val = rand() % 255;
wr_arr[start_add++] = val ;
Xil_Out8(BASEADDR+REG_TxFF, val);
--No_txn;
//printf(" Write data : %d \n", val);
}
}
//-------------------WRITE_OPERTION----------------------
void write_opertion (uint8_t start_add, uint8_t start_val, uint8_t no_txn)
{
LOG_D(" \n *** Start Write Operation... *** \n ");
init_value = start_val; // It will be used for comparison in read operation
chk_clr_TxFIFo();
// set_clock(SYSTEM_FREQ, SCL_FREQ); //setting the value of CHL, CHH, CHHL, CHHH
RESTART_ :
start_stop_write(START);
slv_addr(start_add, WRITE);
// write_data_fifo (start_val, no_txn);
write_random_data(start_add, FULL);
while(!Is_TxCompl()) // Waiting for Tx Complete
;
LOG_D(" Tx is completed... Let's Check NACK...\n ");
if(Is_NACK()){
LOG_D(" NACK is asserted... Please, Restart Process...\n ");
goto RESTART_ ;
}
else {
LOG_D(" NACK is not asserted ... Let's STOP Operation...\n ");
start_stop_write(STOP);
}
while(!stop_flag()) // waiting for STOP FLAG
;
LOG_D(" \n *** Write Operation Completed ... *** \n ");
}
//----------------------------READING Function----------------------------------------
uint8_t Is_RxFIFO_Empty()
{
uint8_t value=0;
value = Xil_In8(BASEADDR+REG_SR0);
if(isKBitSet(value,7))
{
LOG_D(" RxFIFO is Empty \n");
return 1;
}
else
// printf(" TxFF is Not Full... Continue \n");
return 0;
}
//------ START or STOP Read operation------------------
void start_stop_read(uint8_t strt_stp)
{
Xil_Out8(BASEADDR, strt_stp);
}
//---------------------------------------------------------
uint8_t Is_RxCompl()
{
uint8_t value=0;
value = Xil_In8(BASEADDR+REG_SR0);
//printf("SRO_VALUE, %d", value );
//printf("Waiting for Rx completion \n");
if(isKBitSet(value,8))
{
LOG_D(" Rx Completed \n");
return 1;
}
else
// printf(" TxFF is Not Full... Continue \n");
return 0;
}
uint8_t compare_value(uint8_t value)
{
if(init_value + global_i == value)
return 1;
else
{ printf("Read Value Mismatch with Write: 0x%x, 0x%x \n", value, init_value + global_i);
return 0;
}
}
//-------------------READ_OPERTION----------------------
void read_opertion (uint8_t Start_Addr, uint8_t read_bytes)
{
uint8_t start_seq;
uint8_t value = 0;
uint8_t cmpr = 0;
uint8_t i = Start_Addr;
LOG_D(" \n*** Start Read Operation... *** \n ");
RESTART_ :
chk_clr_TxFIFo();
// set_clock(SYSTEM_FREQ, SCL_FREQ); //setting the value of CHL, CHH, CHHL, CHHH
start_seq = read_bytes <<2;
start_seq++;
// printf(" Read start command: 0x%x",start_seq );
start_stop_read(start_seq); // Setting the start with number of read length
slv_addr(Start_Addr, READ); // Setting slave address
while(!Is_TxCompl()) // Waiting for Tx Complete
;
if(Is_NACK()){ // Checking the NACK
LOG_D(" NACK is asserted... Please, Wait and Restart Process...\n ");
while(!stop_flag()) // waiting for STOP FLAG
;
goto RESTART_ ;
}
while(!Is_RxCompl()) // wait for RX completion
;
LOG_D("Waiting for RX completion \n ");
// Is_RxFIFO_Empty();
//uint8_t value=0;
value = Xil_In8(BASEADDR+REG_SR0);
printf(" SRO_value: 0x%x \n",value );
while(!Is_RxFIFO_Empty())
{
value = Xil_In8(BASEADDR+REG_RxFF); // Reading of RX FIFO till it empty
printf(" value: 0x%x \n",value );
rd_arr[i++] = value;
// cmpr = compare_value(value);
// if(!cmpr)
// break;
// ++global_i ;
}
/* if(global_i==read_bytes)
LOG_D(" Read Value is MATCHED with Write \n");
global_i=0;
*/
while(!stop_flag()) // waiting for STOP FLAG
;
start_stop_write(STOP);
LOG_D(" \n *** Read Operation Completed ... *** \n ");
}
int main()
{
init_platform();
printf("\n Hello World.... Testing the APB-I2C...\n");
///////////////////////////////////////////////
// read_opertion(START_ADD, No_of_BYTES); //
//////////////////////////////////////////////
// read_opertion(0x00, FULL);
////////////////////////////////////////////////////////////
// write_opertion(START_ADD, START_VALUE, No_of_TXN); //
///////////////////////////////////////////////////////////
write_opertion(0x00, 0x00, FULL);
// write_opertion(0x00, 1, FULL);
read_opertion(0x00, 31);
for(int i = 0; i < 16; ++i){
printf(" ARR_VAL %d ---> %d \n", wr_arr[i], rd_arr[i]);
}
/*
uint8_t rnd_data;
uint8_t rnd_addr;
uint8_t rnd_byte;
uint8_t START_ADD ;
uint8_t START_DATA ;
uint8_t NO_BYTE;
for(uint8_t i=0; i<250; ++i)
{
rnd_data = rand() % 255;
rnd_addr = rand() % 16;
rnd_byte = rand() % 16;
START_ADD = rnd_addr;
START_DATA = rnd_data;
NO_BYTE = rnd_byte;
if(NO_BYTE>0)
{
write_opertion(START_ADD, START_DATA, NO_BYTE);
read_opertion(START_ADD, NO_BYTE);
printf("Write and Read Operation %d is completed successfully \n ", i+1);
}
}
*/
printf("\n Tested the APB-I2C...\n");
cleanup_platform();
return 0;
}