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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
|
/*
* Copyright (c) 2006-2024, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2024-02-14 ShichengChu first version
*/
#ifndef __DRV_HW_I2C_H__
#define __DRV_HW_I2C_H__
#include <rtthread.h>
#include "rtdevice.h"
#include <rthw.h>
#include "pinctrl.h"
#include "mmio.h"
#define I2C0 0x0
#define I2C1 0x1
#define I2C2 0x2
#define I2C3 0x3
#define I2C4 0x4
#define I2C0_BASE 0x4000000
#define I2C1_BASE 0x4010000
#define I2C2_BASE 0x4020000
#define I2C3_BASE 0x4030000
#define I2C4_BASE 0x4040000
#define I2C0_IRQ (BSP_I2C_IRQ_BASE + 0)
#define I2C1_IRQ (BSP_I2C_IRQ_BASE + 1)
#define I2C2_IRQ (BSP_I2C_IRQ_BASE + 2)
#define I2C3_IRQ (BSP_I2C_IRQ_BASE + 3)
#define I2C4_IRQ (BSP_I2C_IRQ_BASE + 4)
#if !defined(IC_CLK)
#define IC_CLK 100
#endif
/*
* I2C register bit definitions
*/
/* IC_CON, offset: 0x00 */
#define DW_IIC_CON_DEFAUL (0x23U)
#define DW_IIC_CON_MASTER_Pos (0U)
#define DW_IIC_CON_MASTER_Msk (0x1U << DW_IIC_CON_MASTER_Pos)
#define DW_IIC_CON_MASTER_EN DW_IIC_CON_MASTER_Msk
#define DW_IIC_CON_SPEEDL_Pos (1U)
#define DW_IIC_CON_SPEEDL_Msk (0x1U << DW_IIC_CON_SPEEDL_Pos)
#define DW_IIC_CON_SPEEDL_EN DW_IIC_CON_SPEEDL_Msk
#define DW_IIC_CON_SPEEDH_Pos (2U)
#define DW_IIC_CON_SPEEDH_Msk (0x1U << DW_IIC_CON_SPEEDH_Pos)
#define DW_IIC_CON_SPEEDH_EN DW_IIC_CON_SPEEDH_Msk
#define DW_IIC_CON_SLAVE_ADDR_MODE_Pos (3U)
#define DW_IIC_CON_SLAVE_ADDR_MODE_Msk (0x1U << DW_IIC_CON_SLAVE_ADDR_MODE_Pos)
#define DW_IIC_CON_SLAVE_ADDR_MODE DW_IIC_CON_SLAVE_ADDR_MODE_Msk
#define DW_IIC_CON_MASTER_ADDR_MODE_Pos (4U)
#define DW_IIC_CON_MASTER_ADDR_MODE_Msk (0x1U << DW_IIC_CON_MASTER_ADDR_MODE_Pos)
#define DW_IIC_CON_MASTER_ADDR_MODE DW_IIC_CON_MASTER_ADDR_MODE_Msk
#define DW_IIC_CON_RESTART_Pos (5U)
#define DW_IIC_CON_RESTART_Msk (0x1U << DW_IIC_CON_RESTART_Pos)
#define DW_IIC_CON_RESTART_EN DW_IIC_CON_RESTART_Msk
#define DW_IIC_CON_SLAVE_Pos (6U)
#define DW_IIC_CON_SLAVE_Msk (0x1U << DW_IIC_CON_SLAVE_Pos)
#define DW_IIC_CON_SLAVE_EN DW_IIC_CON_SLAVE_Msk
/* IC_TAR, offset: 0x04 */
#define DW_IIC_TAR_GC_OR_START_Pos (10U)
#define DW_IIC_TAR_GC_OR_START_Msk (0x1U << DW_IIC_TAR_GC_OR_START_Pos)
#define DW_IIC_TAR_GC_OR_START DW_IIC_TAR_GC_OR_START_Msk
#define DW_IIC_TAR_SPECIAL_Pos (11U)
#define DW_IIC_TAR_SPECIAL_Msk (0x1U << DW_IIC_TAR_SPECIAL_Pos)
#define DW_IIC_TAR_SPECIAL DW_IIC_TAR_SPECIAL_Msk
#define DW_IIC_TAR_MASTER_ADDR_MODE_Pos (12U)
#define DW_IIC_TAR_MASTER_ADDR_MODE_Msk (0x1U << DW_IIC_TAR_MASTER_ADDR_MODE_Pos)
#define DW_IIC_TAR_MASTER_ADDR_MODE DW_IIC_TAR_MASTER_ADDR_MODE_Msk
/* IC_DATA_CMD, offset: 0x10 */
#define DW_IIC_DATA_CMD_Pos (8U)
#define DW_IIC_DATA_CMD_Msk (0x1U << DW_IIC_DATA_CMD_Pos)
#define DW_IIC_DATA_CMD DW_IIC_DATA_CMD_Msk
#define DW_IIC_DATA_STOP_Pos (9U)
#define DW_IIC_DATA_STOP_Msk (0x1U << DW_IIC_DATA_STOP_Pos)
#define DW_IIC_DATA_STOP DW_IIC_DATA_STOP_Msk
/* IC_INTR_STAT, offset: 0x2C */
#define DW_IIC_INTR_RX_UNDER_Pos (0U)
#define DW_IIC_INTR_RX_UNDER_Msk (0x1U << DW_IIC_INTR_RX_UNDER_Pos)
#define DW_IIC_INTR_RX_UNDER DW_IIC_INTR_RX_UNDER_Msk
#define DW_IIC_INTR_RX_OVER_Pos (1U)
#define DW_IIC_INTR_RX_OVER_Msk (0x1U << DW_IIC_INTR_RX_OVER_Pos)
#define DW_IIC_INTR_RX_OVER DW_IIC_INTR_RX_OVER_Msk
#define DW_IIC_INTR_RX_FULL_Pos (2U)
#define DW_IIC_INTR_RX_FULL_Msk (0x1U << DW_IIC_INTR_RX_FULL_Pos)
#define DW_IIC_INTR_RX_FULL DW_IIC_INTR_RX_FULL_Msk
#define DW_IIC_INTR_TX_OVER_Pos (3U)
#define DW_IIC_INTR_TX_OVER_Msk (0x1U << DW_IIC_INTR_TX_OVER_Pos)
#define DW_IIC_INTR_TX_OVER DW_IIC_INTR_TX_OVER_Msk
#define DW_IIC_INTR_TX_EMPTY_Pos (4U)
#define DW_IIC_INTR_TX_EMPTY_Msk (0x1U << DW_IIC_INTR_TX_EMPTY_Pos)
#define DW_IIC_INTR_TX_EMPTY DW_IIC_INTR_TX_EMPTY_Msk
#define DW_IIC_INTR_RD_REQ_Pos (5U)
#define DW_IIC_INTR_RD_REQ_Msk (0x1U << DW_IIC_INTR_RD_REQ_Pos)
#define DW_IIC_INTR_RD_REQ DW_IIC_INTR_RD_REQ_Msk
#define DW_IIC_INTR_TX_ABRT_Pos (6U)
#define DW_IIC_INTR_TX_ABRT_Msk (0x1U << DW_IIC_INTR_TX_ABRT_Pos)
#define DW_IIC_INTR_TX_ABRT DW_IIC_INTR_TX_ABRT_Msk
#define DW_IIC_INTR_RX_DONE_Pos (7U)
#define DW_IIC_INTR_RX_DONE_Msk (0x1U << DW_IIC_INTR_RX_DONE_Pos)
#define DW_IIC_INTR_RX_DONE DW_IIC_INTR_RX_DONE_Msk
#define DW_IIC_INTR_ACTIVITY_Pos (8U)
#define DW_IIC_INTR_ACTIVITY_Msk (0x1U << DW_IIC_INTR_ACTIVITY_Pos)
#define DW_IIC_INTR_ACTIVITY DW_IIC_INTR_ACTIVITY_Msk
#define DW_IIC_INTR_STOP_DET_Pos (9U)
#define DW_IIC_INTR_STOP_DET_Msk (0x1U << DW_IIC_INTR_STOP_DET_Pos)
#define DW_IIC_INTR_STOP_DET DW_IIC_INTR_STOP_DET_Msk
#define DW_IIC_INTR_START_DET_Pos (10U)
#define DW_IIC_INTR_START_DET_Msk (0x1U << DW_IIC_INTR_START_DET_Pos)
#define DW_IIC_INTR_START_DET DW_IIC_INTR_START_DET_Msk
#define DW_IIC_INTR_GEN_CALL_Pos (11U)
#define DW_IIC_INTR_GEN_CALL_Msk (0x1U << DW_IIC_INTR_GEN_CALL_Pos)
#define DW_IIC_INTR_GEN_CALL DW_IIC_INTR_GEN_CALL_Msk
/* IC_INTR_MASK, offset: 0x30 */
#define DW_IIC_M_RX_UNDER_Pos (0U)
#define DW_IIC_M_RX_UNDER_Msk (0x1U << DW_IIC_INTR_RX_UNDER_Pos)
#define DW_IIC_M_RX_UNDER DW_IIC_INTR_RX_UNDER_Msk
#define DW_IIC_M_RX_OVER_Pos (1U)
#define DW_IIC_M_RX_OVER_Msk (0x1U << DW_IIC_INTR_RX_OVER_Pos)
#define DW_IIC_M_RX_OVER DW_IIC_INTR_RX_OVER_Msk
#define DW_IIC_M_RX_FULL_Pos (2U)
#define DW_IIC_M_RX_FULL_Msk (0x1U << DW_IIC_INTR_RX_FULL_Pos)
#define DW_IIC_M_RX_FULL DW_IIC_INTR_RX_FULL_Msk
#define DW_IIC_M_TX_OVER_Pos (3U)
#define DW_IIC_M_TX_OVER_Msk (0x1U << DW_IIC_INTR_TX_OVER_Pos)
#define DW_IIC_M_TX_OVER DW_IIC_INTR_TX_OVER_Msk
#define DW_IIC_M_TX_EMPTY_Pos (4U)
#define DW_IIC_M_TX_EMPTY_Msk (0x1U << DW_IIC_INTR_TX_EMPTY_Pos)
#define DW_IIC_M_TX_EMPTY DW_IIC_INTR_TX_EMPTY_Msk
#define DW_IIC_M_RD_REQ_Pos (5U)
#define DW_IIC_M_RD_REQ_Msk (0x1U << DW_IIC_INTR_RD_REQ_Pos)
#define DW_IIC_M_RD_REQ DW_IIC_INTR_RD_REQ_Msk
#define DW_IIC_M_TX_ABRT_Pos (6U)
#define DW_IIC_M_TX_ABRT_Msk (0x1U << DW_IIC_INTR_TX_ABRT_Pos)
#define DW_IIC_M_TX_ABRT DW_IIC_INTR_TX_ABRT_Msk
#define DW_IIC_M_RX_DONE_Pos (7U)
#define DW_IIC_M_RX_DONE_Msk (0x1U << DW_IIC_INTR_RX_DONE_Pos)
#define DW_IIC_M_RX_DONE DW_IIC_INTR_RX_DONE_Msk
#define DW_IIC_M_ACTIVITY_Pos (8U)
#define DW_IIC_M_ACTIVITY_Msk (0x1U << DW_IIC_INTR_ACTIVITY_Pos)
#define DW_IIC_M_ACTIVITY DW_IIC_INTR_ACTIVITY_Msk
#define DW_IIC_M_STOP_DET_Pos (9U)
#define DW_IIC_M_STOP_DET_Msk (0x1U << DW_IIC_INTR_STOP_DET_Pos)
#define DW_IIC_M_STOP_DET DW_IIC_INTR_STOP_DET_Msk
#define DW_IIC_M_START_DET_Pos (10U)
#define DW_IIC_M_START_DET_Msk (0x1U << DW_IIC_INTR_START_DET_Pos)
#define DW_IIC_M_START_DET DW_IIC_INTR_START_DET_Msk
#define DW_IIC_M_GEN_CALL_Pos (11U)
#define DW_IIC_M_GEN_CALL_Msk (0x1U << DW_IIC_INTR_GEN_CALL_Pos)
#define DW_IIC_M_GEN_CALL DW_IIC_INTR_GEN_CALL_Msk
#define DW_IIC_INTR_DEFAULT_MASK ( DW_IIC_M_RX_FULL | DW_IIC_M_TX_EMPTY | DW_IIC_M_TX_ABRT | DW_IIC_M_STOP_DET)
/* IC_RAW_INTR_STAT, offset: 0x34 */
#define DW_IIC_RAW_RX_UNDER_Pos (0U)
#define DW_IIC_RAW_RX_UNDER_Msk (0x1U << DW_IIC_INTR_RX_UNDER_Pos)
#define DW_IIC_RAW_RX_UNDER DW_IIC_INTR_RX_UNDER_Msk
#define DW_IIC_RAW_RX_OVER_Pos (1U)
#define DW_IIC_RAW_RX_OVER_Msk (0x1U << DW_IIC_INTR_RX_OVER_Pos)
#define DW_IIC_RAW_RX_OVER DW_IIC_INTR_RX_OVER_Msk
#define DW_IIC_RAW_RX_FULL_Pos (2U)
#define DW_IIC_RAW_RX_FULL_Msk (0x1U << DW_IIC_INTR_RX_FULL_Pos)
#define DW_IIC_RAW_RX_FULL DW_IIC_INTR_RX_FULL_Msk
#define DW_IIC_RAW_TX_OVER_Pos (3U)
#define DW_IIC_RAW_TX_OVER_Msk (0x1U << DW_IIC_INTR_TX_OVER_Pos)
#define DW_IIC_RAW_TX_OVER DW_IIC_INTR_TX_OVER_Msk
#define DW_IIC_RAW_TX_EMPTY_Pos (4U)
#define DW_IIC_RAW_TX_EMPTY_Msk (0x1U << DW_IIC_INTR_TX_EMPTY_Pos)
#define DW_IIC_RAW_TX_EMPTY DW_IIC_INTR_TX_EMPTY_Msk
#define DW_IIC_RAW_RD_REQ_Pos (5U)
#define DW_IIC_RAW_RD_REQ_Msk (0x1U << DW_IIC_INTR_RD_REQ_Pos)
#define DW_IIC_RAW_RD_REQ DW_IIC_INTR_RD_REQ_Msk
#define DW_IIC_RAW_TX_ABRT_Pos (6U)
#define DW_IIC_RAW_TX_ABRT_Msk (0x1U << DW_IIC_INTR_TX_ABRT_Pos)
#define DW_IIC_RAW_TX_ABRT DW_IIC_INTR_TX_ABRT_Msk
#define DW_IIC_RAW_RX_DONE_Pos (7U)
#define DW_IIC_RAW_RX_DONE_Msk (0x1U << DW_IIC_INTR_RX_DONE_Pos)
#define DW_IIC_RAW_RX_DONE DW_IIC_INTR_RX_DONE_Msk
#define DW_IIC_RAW_ACTIVITY_Pos (8U)
#define DW_IIC_RAW_ACTIVITY_Msk (0x1U << DW_IIC_INTR_ACTIVITY_Pos)
#define DW_IIC_RAW_ACTIVITY DW_IIC_INTR_ACTIVITY_Msk
#define DW_IIC_RAW_STOP_DET_Pos (9U)
#define DW_IIC_RAW_STOP_DET_Msk (0x1U << DW_IIC_INTR_STOP_DET_Pos)
#define DW_IIC_RAW_STOP_DET DW_IIC_INTR_STOP_DET_Msk
#define DW_IIC_RAW_START_DET_Pos (10U)
#define DW_IIC_RAW_START_DET_Msk (0x1U << DW_IIC_INTR_START_DET_Pos)
#define DW_IIC_RAW_START_DET DW_IIC_INTR_START_DET_Msk
#define DW_IIC_RAW_GEN_CALL_Pos (11U)
#define DW_IIC_RAW_GEN_CALL_Msk (0x1U << DW_IIC_INTR_GEN_CALL_Pos)
#define DW_IIC_RAW_GEN_CALL DW_IIC_INTR_GEN_CALL_Msk
/* IC_ENABLE, offset: 0x6C */
#define DW_IIC_ENABLE_Pos (0U)
#define DW_IIC_ENABLE_Msk (0x1U << DW_IIC_ENABLE_Pos)
#define DW_IIC_EN DW_IIC_ENABLE_Msk
/* IC_STATUS, offset: 0x70 */
#define DW_IIC_STATUS_ACTIVITY_Pos (0U)
#define DW_IIC_STATUS_ACTIVITY_Msk (0x1U << DW_IIC_STATUS_ACTIVITY_Pos)
#define DW_IIC_STATUS_ACTIVITY_STATE DW_IIC_STATUS_ACTIVITY_Msk
#define DW_IIC_STATUS_TFNE_Pos (1U)
#define DW_IIC_STATUS_TFNE_Msk (0x1U << DW_IIC_STATUS_TFNE_Pos)
#define DW_IIC_TXFIFO_NOT_FULL_STATE DW_IIC_STATUS_TFNE_Msk
#define DW_IIC_STATUS_TFE_Pos (2U)
#define DW_IIC_STATUS_TFE_Msk (0x1U << DW_IIC_STATUS_TFE_Pos)
#define DW_IIC_TXFIFO_EMPTY_STATE DW_IIC_STATUS_TFE_Msk
#define DW_IIC_STATUS_RFNE_Pos (3U)
#define DW_IIC_STATUS_RFNE_Msk (0x1U << DW_IIC_STATUS_RFNE_Pos)
#define DW_IIC_RXFIFO_NOT_EMPTY_STATE DW_IIC_STATUS_RFNE_Msk
#define DW_IIC_STATUS_REF_Pos (4U)
#define DW_IIC_STATUS_REF_Msk (0x1U << DW_IIC_STATUS_REF_Pos)
#define DW_IIC_RXFIFO_FULL_STATE DW_IIC_STATUS_REF_Msk
#define DW_IIC_STATUS_MST_ACTIVITY_Pos (5U)
#define DW_IIC_STATUS_MST_ACTIVITY_Msk (0x1U << DW_IIC_STATUS_MST_ACTIVITY_Pos)
#define DW_IIC_MST_ACTIVITY_STATE DW_IIC_STATUS_MST_ACTIVITY_Msk
#define DW_IIC_STATUS_SLV_ACTIVITY_Pos (6U)
#define DW_IIC_STATUS_SLV_ACTIVITY_Msk (0x1U << DW_IIC_STATUS_SLV_ACTIVITY_Pos)
#define DW_IIC_SLV_ACTIVITY_STATE DW_IIC_STATUS_SLV_ACTIVITY_Msk
/* IC_TX_ABRT_SOURCE, offset: 0x80 */
#define DW_IIC_TX_ABRT_7B_ADDR_NOACK_Pos (0U)
#define DW_IIC_TX_ABRT_7B_ADDR_NOACK_Msk (0x1U << DW_IIC_TX_ABRT_7B_ADDR_NOACK_Pos)
#define DW_IIC_TX_ABRT_7B_ADDR_NOACK DW_IIC_TX_ABRT_7B_ADDR_NOACK_Msk
#define DW_IIC_TX_ABRT_10ADDR1_NOACK_Pos (1U)
#define DW_IIC_TX_ABRT_10ADDR1_NOACK_Msk (0x1U << DW_IIC_TX_ABRT_10ADDR1_NOACK_Pos)
#define DW_IIC_TX_ABRT_10ADDR1_NOACK DW_IIC_TX_ABRT_10ADDR1_NOACK_Msk
#define DW_IIC_TX_ABRT_10ADDR2_NOACK_Pos (2U)
#define DW_IIC_TX_ABRT_10ADDR2_NOACK_Msk (0x1U << DW_IIC_TX_ABRT_10ADDR2_NOACK_Pos)
#define DW_IIC_TX_ABRT_10ADDR2_NOACK DW_IIC_TX_ABRT_10ADDR2_NOACK_Msk
#define DW_IIC_TX_ABRT_TXDATA_NOACK_Pos (3U)
#define DW_IIC_TX_ABRT_TXDATA_NOACK_Msk (0x1U << DW_IIC_TX_ABRT_TXDATA_NOACK_Pos)
#define DW_IIC_TX_ABRT_TXDATA_NOACK DW_IIC_TX_ABRT_TXDATA_NOACK_Msk
#define DW_IIC_TX_ABRT_GCALL_NOACK_Pos (4U)
#define DW_IIC_TX_ABRT_GCALL_NOACK_Msk (0x1U << DW_IIC_TX_ABRT_GCALL_NOACK_Pos)
#define DW_IIC_TX_ABRT_GCALL_NOACK DW_IIC_TX_ABRT_GCALL_NOACK_Msk
#define DW_IIC_TX_ABRT_GCALL_READ_Pos (5U)
#define DW_IIC_TX_ABRT_GCALL_READ_Msk (0x1U << DW_IIC_TX_ABRT_GCALL_READ_Pos)
#define DW_IIC_TX_ABRT_GCALL_READ DW_IIC_TX_ABRT_GCALL_READ_Msk
#define DW_IIC_TX_ABRT_HS_ACKDET_Pos (6U)
#define DW_IIC_TX_ABRT_HS_ACKDET_Msk (0x1U << DW_IIC_TX_ABRT_HS_ACKDET_Pos)
#define DW_IIC_TX_ABRT_HS_ACKDET DW_IIC_TX_ABRT_HS_ACKDET_Msk
#define DW_IIC_TX_ABRT_SBYTE_ACKDET_Pos (7U)
#define DW_IIC_TX_ABRT_SBYTE_ACKDET_Msk (0x1U << DW_IIC_TX_ABRT_SBYTE_ACKDET_Pos)
#define DW_IIC_TX_ABRT_SBYTE_ACKDET DW_IIC_TX_ABRT_SBYTE_ACKDET_Msk
#define DW_IIC_TX_ABRT_HS_NORSTRT_Pos (8U)
#define DW_IIC_TX_ABRT_HS_NORSTRT_Msk (0x1U << DW_IIC_TX_ABRT_HS_NORSTRT_Pos)
#define DW_IIC_TX_ABRT_HS_NORSTRT DW_IIC_TX_ABRT_HS_NORSTRT_Msk
#define DW_IIC_TX_ABRT_SBYTE_NORSTRT_Pos (9U)
#define DW_IIC_TX_ABRT_SBYTE_NORSTRT_Msk (0x1U << DW_IIC_TX_ABRT_SBYTE_NORSTRT_Pos)
#define DW_IIC_TX_ABRT_SBYTE_NORSTRT DW_IIC_TX_ABRT_SBYTE_NORSTRT_Msk
#define DW_IIC_TX_ABRT_10B_RD_NORSTRT_Pos (10U)
#define DW_IIC_TX_ABRT_10B_RD_NORSTRT_Msk (0x1U << DW_IIC_TX_ABRT_10B_RD_NORSTRT_Pos)
#define DW_IIC_TX_ABRT_10B_RD_NORSTRT DW_IIC_TX_ABRT_10B_RD_NORSTRT_Msk
#define DW_IIC_TX_ABRT_ARB_MASTER_DIS_Pos (11U)
#define DW_IIC_TX_ABRT_ARB_MASTER_DIS_Msk (0x1U << DW_IIC_TX_ABRT_ARB_MASTER_DIS_Pos)
#define DW_IIC_TX_ABRT_ARB_MASTER_DIS DW_IIC_TX_ABRT_ARB_MASTER_DIS_Msk
#define DW_IIC_TX_ABRT_ARB_LOST_Pos (12U)
#define DW_IIC_TX_ABRT_ARB_LOST_Msk (0x1U << DW_IIC_TX_ABRT_ARB_LOST_Pos)
#define DW_IIC_TX_ABRT_ARB_LOST DW_IIC_TX_ABRT_ARB_LOST_Msk
#define DW_IIC_TX_ABRT_SLVFLUSH_TXFIFO_Pos (13U)
#define DW_IIC_TX_ABRT_SLVFLUSH_TXFIFO_Msk (0x1U << DW_IIC_TX_ABRT_SLVFLUSH_TXFIFO_Pos)
#define DW_IIC_TX_ABRT_SLVFLUSH_TXFIFO DW_IIC_TX_ABRT_SLVFLUSH_TXFIFO_Msk
#define DW_IIC_TX_ABRT_SLV_ARBLOST_Pos (14U)
#define DW_IIC_TX_ABRT_SLV_ARBLOST_Msk (0x1U << DW_IIC_TX_ABRT_SLV_ARBLOST_Pos)
#define DW_IIC_TX_ABRT_SLV_ARBLOST DW_IIC_TX_ABRT_SLV_ARBLOST_Msk
#define DW_IIC_TX_ABRT_SLVRD_INTX_Pos (15U)
#define DW_IIC_TX_ABRT_SLVRD_INTX_Msk (0x1U << DW_IIC_TX_ABRT_SLVRD_INTX_Pos)
#define DW_IIC_TX_ABRT_SLVRD_INTX DW_IIC_TX_ABRT_SLVRD_INTX_Msk
/* IC_DMA_CR, offset: 0x88 */
#define DW_IIC_DMA_CR_RDMAE_Pos (0U)
#define DW_IIC_DMA_CR_RDMAE_Msk (0x1U << DW_IIC_DMA_CR_RDMAE_Pos)
#define DW_IIC_DMA_CR_RDMAE DW_IIC_DMA_CR_RDMAE_Msk
#define DW_IIC_DMA_CR_TDMAE_Pos (1U)
#define DW_IIC_DMA_CR_TDMAE_Msk (0x1U << DW_IIC_DMA_CR_TDMAE_Pos)
#define DW_IIC_DMA_CR_TDMAE DW_IIC_DMA_CR_TDMAE_Msk
/* IC_DMA_TDLR, offset: 0x8C */
#define DW_IIC_DMA_TDLR_Msk (0x7U)
/* IC_DMA_RDLR, offset: 0x90 */
#define DW_IIC_DMA_RDLR_Msk (0x7U)
/* IC_GEN_CALL_EN, offset: 0xA0 */
//no this register
#define DW_IIC_GEN_CALL_EN_Pos (0U)
#define DW_IIC_GEN_CALL_EN_Msk (0x1U << DW_IIC_GEN_CALL_EN_Pos)
#define DW_IIC_GEN_CALL_EN DW_IIC_GEN_CALL_EN_Msk
/* IC_FIFO_RST_EN, offset: 0xA4 */
//no this register
#define DW_IIC_FIFO_RST_EN_Pos (0U)
#define DW_IIC_FIFO_RST_EN_Msk (0x1U << DW_IIC_FIFO_RST_EN_Pos)
#define DW_IIC_FIFO_RST_EN DW_IIC_FIFO_RST_EN_Msk
#define TXFIFO_IRQ_TH (0x4U)
#define RXFIFO_IRQ_TH (0x2U)
#define IIC_MAX_FIFO (0x8U)
/* IIC default value definitions */
#define DW_IIC_TIMEOUT_DEF_VAL 0x1000U
#define DW_IIC_EEPROM_MAX_WRITE_LEN 0X1U
typedef struct {
volatile uint32_t IC_CON; /* Offset: 0x000 (R/W) I2C Control */
volatile uint32_t IC_TAR; /* Offset: 0x004 (R/W) I2C target address */
volatile uint32_t IC_SAR; /* Offset: 0x008 (R/W) I2C slave address */
volatile uint32_t IC_HS_MADDR; /* Offset: 0x00C (R/W) I2C HS Master Mode Code Address */
volatile uint32_t IC_DATA_CMD; /* Offset: 0x010 (R/W) I2C RX/TX Data Buffer and Command */
volatile uint32_t IC_SS_SCL_HCNT; /* Offset: 0x014 (R/W) Standard speed I2C Clock SCL High Count */
volatile uint32_t IC_SS_SCL_LCNT; /* Offset: 0x018 (R/W) Standard speed I2C Clock SCL Low Count */
volatile uint32_t IC_FS_SCL_HCNT; /* Offset: 0x01C (R/W) Fast speed I2C Clock SCL High Count */
volatile uint32_t IC_FS_SCL_LCNT; /* Offset: 0x020 (R/W) Fast speed I2C Clock SCL Low Count */
volatile uint32_t IC_HS_SCL_HCNT; /* Offset: 0x024 (R/W) High speed I2C Clock SCL High Count*/
volatile uint32_t IC_HS_SCL_LCNT; /* Offset: 0x028 (R/W) High speed I2C Clock SCL Low Count */
volatile const uint32_t IC_INTR_STAT; /* Offset: 0x02C (R) I2C Interrupt Status */
volatile uint32_t IC_INTR_MASK; /* Offset: 0x030 (R/W) I2C Interrupt Mask */
volatile const uint32_t IC_RAW_INTR_STAT; /* Offset: 0x034 (R) I2C Raw Interrupt Status */
volatile uint32_t IC_RX_TL; /* Offset: 0x038 (R/W) I2C Receive FIFO Threshold */
volatile uint32_t IC_TX_TL; /* Offset: 0x03C (R/W) I2C Transmit FIFO Threshold */
volatile const uint32_t IC_CLR_INTR; /* Offset: 0x040 (R) Clear combined and individual interrupts*/
volatile const uint32_t IC_CLR_RX_UNDER; /* Offset: 0x044 (R) I2C Clear RX_UNDER interrupt */
volatile const uint32_t IC_CLR_RX_OVER; /* Offset: 0x048 (R) I2C Clear RX_OVER interrupt */
volatile const uint32_t IC_CLR_TX_OVER; /* Offset: 0x04C (R) I2C Clear TX_OVER interrupt */
volatile const uint32_t IC_CLR_RD_REQ; /* Offset: 0x050 (R) I2C Clear RD_REQ interrupt */
volatile const uint32_t IC_CLR_TX_ABRT; /* Offset: 0x054 (R) I2C Clear TX_ABRT interrupt */
volatile const uint32_t IC_CLR_RX_DONE; /* Offset: 0x058 (R) I2C Clear RX_DONE interrupt */
volatile const uint32_t IC_CLR_ACTIVITY; /* Offset: 0x05C (R) I2C Clear ACTIVITY interrupt */
volatile const uint32_t IC_CLR_STOP_DET; /* Offset: 0x060 (R) I2C Clear STOP_DET interrupt */
volatile const uint32_t IC_CLR_START_DET; /* Offset: 0x064 (R) I2C Clear START_DET interrupt */
volatile const uint32_t IC_CLR_GEN_CALL; /* Offset: 0x068 (R) I2C Clear GEN_CAL interrupt */
volatile uint32_t IC_ENABLE; /* Offset: 0x06C (R/W) I2C enable */
volatile const uint32_t IC_STATUS; /* Offset: 0x070 (R) I2C status register */
volatile const uint32_t IC_TXFLR; /* Offset: 0x074 (R) Transmit FIFO Level register */
volatile const uint32_t IC_RXFLR; /* Offset: 0x078 (R) Receive FIFO Level Register */
volatile uint32_t IC_SDA_HOLD; /* Offset: 0x07C (R/W) SDA hold time register */
volatile uint32_t IC_TX_ABRT_SOURCE; /* Offset: 0x080 (R/W) I2C Transmit Abort Status Register */
volatile uint32_t IC_SLV_DAT_NACK_ONLY; /* Offset: 0x084 (R/W) I2C Slave Address1 */
volatile uint32_t IC_DMA_CR; /* Offset: 0x088 (R/W) DMA Control Register for transmit and receive handshaking interface */
volatile uint32_t IC_DMA_TDLR; /* Offset: 0x08C (R/W) DMA Transmit Data Level */
volatile uint32_t IC_DMA_RDLR; /* Offset: 0x090 (R/W) DMA Receive Data Level */
volatile uint32_t IC_SDA_SETUP; /* Offset: 0x094 (R/W) I2C Slave Address2 */
volatile uint32_t IC_ACK_GENERAL_CALL; /* Offset: 0x098 (R/W) I2C Slave Address3 */
volatile uint32_t IC_ENABLE_STATUS; /* Offset: 0x09C (R/W) I2C address number in slave mode */
volatile uint32_t IC_FS_SPKLEN; /* Offset: 0x0A0 (R/W) I2C general call mask register when I2C is in the slave mode */
volatile uint32_t IC_HS_SPKLEN; /* Offset: 0x0A4 (R/W) I2C FIFO flush register when I2C is in the slave transfer mode*/
} dw_iic_regs_t;
static inline void dw_iic_enable(dw_iic_regs_t *iic_base)
{
iic_base->IC_ENABLE = DW_IIC_EN;
}
static inline void dw_iic_disable(dw_iic_regs_t *iic_base)
{
/* First clear ACTIVITY, then Disable IIC */
iic_base->IC_CLR_ACTIVITY;
iic_base->IC_ENABLE = ~DW_IIC_EN;
}
static inline uint32_t dw_iic_get_iic_status(dw_iic_regs_t *iic_base)
{
return iic_base->IC_ENABLE;
}
static inline void dw_iic_enable_restart(dw_iic_regs_t *iic_base)
{
iic_base->IC_CON |= DW_IIC_CON_RESTART_EN;
}
static inline void dw_iic_master_enable_transmit_irq(dw_iic_regs_t *iic_base)
{
iic_base->IC_INTR_MASK = DW_IIC_INTR_TX_EMPTY | DW_IIC_INTR_TX_OVER | DW_IIC_INTR_STOP_DET;
iic_base->IC_CLR_INTR;
}
static inline void dw_iic_slave_enable_transmit_irq(dw_iic_regs_t *iic_base)
{
iic_base->IC_INTR_MASK = DW_IIC_INTR_RD_REQ | DW_IIC_INTR_STOP_DET;
iic_base->IC_CLR_INTR;
}
static inline void dw_iic_master_enable_receive_irq(dw_iic_regs_t *iic_base)
{
iic_base->IC_INTR_MASK = DW_IIC_INTR_STOP_DET | DW_IIC_INTR_RX_FULL | DW_IIC_INTR_RX_OVER;
iic_base->IC_CLR_INTR;
}
static inline void dw_iic_slave_enable_receive_irq(dw_iic_regs_t *iic_base)
{
iic_base->IC_INTR_MASK = DW_IIC_INTR_STOP_DET | DW_IIC_INTR_RX_FULL;
iic_base->IC_CLR_INTR;
}
static inline void dw_iic_clear_all_irq(dw_iic_regs_t *iic_base)
{
iic_base->IC_CLR_INTR;
iic_base->IC_CLR_RX_UNDER;
iic_base->IC_CLR_RX_OVER;
iic_base->IC_CLR_TX_OVER;
iic_base->IC_CLR_RD_REQ;
iic_base->IC_CLR_TX_ABRT;
iic_base->IC_CLR_RX_DONE;
iic_base->IC_CLR_ACTIVITY;
iic_base->IC_CLR_STOP_DET;
iic_base->IC_CLR_START_DET;
iic_base->IC_CLR_GEN_CALL;
}
static inline void dw_iic_set_sda_hold_time(dw_iic_regs_t *iic_base, uint32_t val)
{
iic_base->IC_SDA_HOLD = val;
}
static inline void dw_iic_flush_rxfifo(dw_iic_regs_t *iic_base)
{
while (iic_base->IC_STATUS & DW_IIC_RXFIFO_NOT_EMPTY_STATE)
iic_base->IC_DATA_CMD;
}
static inline void dw_iic_set_slave_10bit_addr_mode(dw_iic_regs_t *iic_base)
{
iic_base->IC_CON |= DW_IIC_CON_SLAVE_ADDR_MODE;
}
static inline void dw_iic_set_slave_7bit_addr_mode(dw_iic_regs_t *iic_base)
{
iic_base->IC_CON &= ~DW_IIC_CON_SLAVE_ADDR_MODE;
}
static inline void dw_iic_set_master_10bit_addr_mode(dw_iic_regs_t *iic_base)
{
iic_base->IC_TAR |= DW_IIC_TAR_MASTER_ADDR_MODE;
}
static inline void dw_iic_set_master_7bit_addr_mode(dw_iic_regs_t *iic_base)
{
iic_base->IC_TAR &= ~DW_IIC_TAR_MASTER_ADDR_MODE;
iic_base->IC_CON &= ~DW_IIC_CON_MASTER_ADDR_MODE;
}
static inline void dw_iic_set_standard_scl_hcnt(dw_iic_regs_t *iic_base, uint32_t cnt)
{
iic_base->IC_SS_SCL_HCNT = cnt;
}
static inline void dw_iic_set_standard_scl_lcnt(dw_iic_regs_t *iic_base, uint32_t cnt)
{
iic_base->IC_SS_SCL_LCNT = cnt;
}
static inline void dw_iic_set_fast_scl_hcnt(dw_iic_regs_t *iic_base, uint32_t cnt)
{
iic_base->IC_FS_SCL_HCNT = cnt;
}
static inline void dw_iic_set_fast_scl_lcnt(dw_iic_regs_t *iic_base, uint32_t cnt)
{
iic_base->IC_FS_SCL_LCNT = cnt;
}
static inline void dw_iic_set_high_scl_hcnt(dw_iic_regs_t *iic_base, uint32_t cnt)
{
iic_base->IC_HS_SCL_HCNT = cnt;
}
static inline void dw_iic_set_high_scl_lcnt(dw_iic_regs_t *iic_base, uint32_t cnt)
{
iic_base->IC_HS_SCL_LCNT = cnt;
}
static inline void dw_iic_set_own_address(dw_iic_regs_t *iic_base, uint32_t address)
{
iic_base->IC_SAR = address;
}
static inline void dw_iic_set_transmit_fifo_threshold(dw_iic_regs_t *iic_base, uint32_t level)
{
iic_base->IC_TX_TL = level;
}
static inline void dw_iic_set_receive_fifo_threshold(dw_iic_regs_t *iic_base, uint32_t level)
{
iic_base->IC_RX_TL = level - 1U;
}
static inline uint32_t dw_iic_get_transmit_fifo_num(dw_iic_regs_t *iic_base)
{
return iic_base->IC_TXFLR;
}
static inline uint32_t dw_iic_get_receive_fifo_num(dw_iic_regs_t *iic_base)
{
return iic_base->IC_RXFLR;
}
static inline void dw_iic_transmit_data(dw_iic_regs_t *iic_base, uint16_t data)
{
iic_base->IC_DATA_CMD = data;
}
static inline uint8_t dw_iic_receive_data(dw_iic_regs_t *iic_base)
{
return (uint8_t)iic_base->IC_DATA_CMD;
}
static inline void dw_iic_data_cmd(dw_iic_regs_t *iic_base)
{
iic_base->IC_DATA_CMD = DW_IIC_DATA_CMD;
}
static inline void dw_iic_data_cmd_stop(dw_iic_regs_t *iic_base)
{
iic_base->IC_DATA_CMD = DW_IIC_DATA_CMD | (1<<9);
}
static inline void dw_iic_fifo_rst(dw_iic_regs_t *iic_base, uint32_t en)
{
//no this register
//iic_base->IC_FIFO_RST_EN = (en & DW_IIC_FIFO_RST_EN_Msk);
}
static inline void dw_iic_dma_transmit_enable(dw_iic_regs_t *iic_base)
{
iic_base->IC_DMA_CR |= DW_IIC_DMA_CR_TDMAE;
}
static inline void dw_iic_dma_transmit_disable(dw_iic_regs_t *iic_base)
{
iic_base->IC_DMA_CR &= ~DW_IIC_DMA_CR_TDMAE;
}
static inline void dw_iic_dma_receive_enable(dw_iic_regs_t *iic_base)
{
iic_base->IC_DMA_CR |= DW_IIC_DMA_CR_RDMAE;
}
static inline void dw_iic_dma_receive_disable(dw_iic_regs_t *iic_base)
{
iic_base->IC_DMA_CR &= ~DW_IIC_DMA_CR_RDMAE;
}
static inline void dw_iic_dma_transmit_level(dw_iic_regs_t *iic_base, uint8_t level)
{
iic_base->IC_DMA_TDLR = ((uint32_t)level & DW_IIC_DMA_TDLR_Msk);
}
static inline void dw_iic_dma_receive_level(dw_iic_regs_t *iic_base, uint8_t level)
{
iic_base->IC_DMA_RDLR = ((uint32_t)level & DW_IIC_DMA_RDLR_Msk);
}
static inline uint32_t dw_iic_get_raw_interrupt_state(dw_iic_regs_t *iic_base)
{
return iic_base->IC_RAW_INTR_STAT;
}
static inline void dw_iic_disable_all_irq(dw_iic_regs_t *iic_base)
{
iic_base->IC_INTR_MASK = 0U;
}
static inline uint32_t dw_iic_read_clear_intrbits(dw_iic_regs_t *iic_base)
{
uint32_t stat = 0U;
stat = iic_base->IC_INTR_STAT;
if (stat & DW_IIC_INTR_RX_UNDER)
{
iic_base->IC_CLR_RX_UNDER;
}
if (stat & DW_IIC_INTR_RX_OVER)
{
iic_base->IC_CLR_RX_OVER;
}
if (stat & DW_IIC_INTR_TX_OVER)
{
iic_base->IC_CLR_TX_OVER;
}
if (stat & DW_IIC_INTR_RD_REQ)
{
iic_base->IC_CLR_RD_REQ;
}
if (stat & DW_IIC_INTR_TX_ABRT)
{
iic_base->IC_TX_ABRT_SOURCE;
}
if (stat & DW_IIC_INTR_RX_DONE)
{
iic_base->IC_CLR_RX_DONE;
}
if (stat & DW_IIC_INTR_ACTIVITY)
{
iic_base->IC_CLR_ACTIVITY;
}
if (stat & DW_IIC_INTR_STOP_DET)
{
iic_base->IC_CLR_STOP_DET;
}
if (stat & DW_IIC_INTR_START_DET)
{
iic_base->IC_CLR_START_DET;
}
if (stat & DW_IIC_INTR_GEN_CALL)
{
iic_base->IC_CLR_GEN_CALL;
}
return stat;
}
static inline uint32_t dw_iic_get_intrrupt_state(dw_iic_regs_t *iic_base)
{
return iic_base->IC_INTR_STAT;
}
int rt_hw_i2c_init(void);
#endif /* __DRV_HW_I2C_H__ */
|