forked from apache/nuttx
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbl602_i2c.c
1051 lines (888 loc) · 27.6 KB
/
bl602_i2c.c
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
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
* arch/risc-v/src/bl602/bl602_i2c.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <assert.h>
#include <debug.h>
#include <errno.h>
#include <nuttx/arch.h>
#include <nuttx/i2c/i2c_master.h>
#include <nuttx/irq.h>
#include <arch/board/board.h>
#include "chip.h"
#include "riscv_internal.h"
#include "hardware/bl602_glb.h"
#include "hardware/bl602_hbn.h"
#include "hardware/bl602_i2c.h"
#include "bl602_i2c.h"
#include "bl602_gpio.h"
#include "bl602_glb.h"
/****************************************************************************
* Private Functions
****************************************************************************/
#define I2C0 0
#define PUT_UINT32_LE(n, b, i) \
{ \
(b)[(i)] = (uint8_t)((n)); \
(b)[(i) + 1] = (uint8_t)((n) >> 8); \
(b)[(i) + 2] = (uint8_t)((n) >> 16); \
(b)[(i) + 3] = (uint8_t)((n) >> 24); \
}
#define I2C_DEFAULT_FREQUENCY 100000
/* I2C state */
#define EV_I2C_END_INT 0
#define EV_I2C_TXF_INT 1
#define EV_I2C_RXF_INT 3
#define EV_I2C_FER_INT 4
#define EV_I2C_ARB_INT 5
#define EV_I2C_NAK_INT 6
#define EV_I2C_UNKNOW_INT 0xff
/* I2C Device hardware configuration */
struct bl602_i2c_config_s
{
uint32_t reg_base; /* I2C register base address */
uint8_t irq; /* Interrupt ID */
uint32_t clk_freq; /* i2c freq */
};
/* I2C Device Private Data */
struct bl602_i2c_priv_s
{
const struct i2c_ops_s *ops; /* Standard I2C operations */
/* Port configuration */
const struct bl602_i2c_config_s *config;
uint8_t subflag; /* Sub address flag */
uint32_t subaddr; /* Sub address */
uint8_t sublen; /* Sub address length */
sem_t sem_excl; /* Mutual exclusion semaphore */
sem_t sem_isr; /* Interrupt wait semaphore */
/* I2C work state */
uint8_t i2cstate;
struct i2c_msg_s *msgv; /* Message list */
uint8_t msgid; /* Current message ID */
ssize_t bytes; /* Processed data bytes */
int refs; /* Reference count */
};
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
static int bl602_i2c_transfer(struct i2c_master_s *dev,
struct i2c_msg_s * msgs,
int count);
#ifdef CONFIG_I2C_RESET
static int bl602_i2c_reset(struct i2c_master_s *dev);
#endif
/****************************************************************************
* Private Data
****************************************************************************/
/* I2C interface */
static const struct i2c_ops_s bl602_i2c_ops =
{
.transfer = bl602_i2c_transfer,
#ifdef CONFIG_I2C_RESET
.reset = bl602_i2c_reset
#endif
};
/* I2C device structures */
#ifdef CONFIG_BL602_I2C0
static const struct bl602_i2c_config_s bl602_i2c0_config =
{
.reg_base = BL602_I2C_BASE,
.irq = BL602_IRQ_I2C,
.clk_freq = I2C_DEFAULT_FREQUENCY,
};
static struct bl602_i2c_priv_s bl602_i2c0_priv =
{
.ops = &bl602_i2c_ops,
.config = &bl602_i2c0_config,
.subaddr = 0,
.sublen = 0,
.i2cstate = EV_I2C_END_INT,
.msgv = NULL,
.msgid = 0,
.bytes = 0,
};
#endif /* CONFIG_BL602_I2C0 */
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: bl602_i2c_send_data
*
* Description:
* Send I2C data
*
****************************************************************************/
static void bl602_i2c_send_data(struct bl602_i2c_priv_s *priv)
{
uint32_t temp = 0;
uint32_t val = 0;
int i;
int count;
struct i2c_msg_s *msg = &priv->msgv[priv->msgid];
count = msg->length - priv->bytes;
if (count >= 4)
{
count = 4;
}
for (i = 0; i < count; i++)
{
val = *(msg->buffer + priv->bytes + i);
temp += val << i * 8;
}
putreg32(temp, BL602_I2C_FIFO_WDATA);
priv->bytes += count;
i2cinfo("count=%d, temp=0x%lx\n", count, temp); ////
}
/****************************************************************************
* Name: bl602_i2c_recvdata
*
* Description:
* Receive I2C data
*
****************************************************************************/
static void bl602_i2c_recvdata(struct bl602_i2c_priv_s *priv)
{
uint32_t temp = 0;
int i = 0;
int count;
struct i2c_msg_s *msg = &priv->msgv[priv->msgid];
count = msg->length - priv->bytes;
temp = getreg32(BL602_I2C_FIFO_RDATA);
i2cinfo("count=%d, temp=0x%lx\n", count, temp); ////
if (count >= 4)
{
PUT_UINT32_LE(temp, msg->buffer, priv->bytes);
count = 4;
}
else if (count < 4)
{
for (i = 0; i < count; i++)
{
msg->buffer[priv->bytes + i] = (temp & 0xff);
temp = (temp >> 8);
}
}
priv->bytes += count;
}
/****************************************************************************
* Name: bl602_i2c_sem_init
*
* Description:
* Initialize semaphores
*
****************************************************************************/
static void bl602_i2c_sem_init(struct bl602_i2c_priv_s *priv)
{
nxsem_init(&priv->sem_excl, 0, 1);
/* This semaphore is used for signaling and, hence, should not have
* priority inheritance enabled.
*/
nxsem_init(&priv->sem_isr, 0, 1);
nxsem_set_protocol(&priv->sem_isr, SEM_PRIO_NONE);
}
/****************************************************************************
* Name: bl602_i2c_clear_status
*
* Description:
* clear i2c status
*
****************************************************************************/
static void bl602_i2c_clear_status(int i2cx)
{
if (i2cx == I2C0)
{
modifyreg32(BL602_I2C_INT_STS, 0, I2C_INT_STS_CR_END_CLR
| I2C_INT_STS_CR_NAK_CLR | I2C_INT_STS_CR_ARB_CLR);
}
else
{
i2cerr("port error\n");
}
}
/****************************************************************************
* Name: bl602_i2c_config_para
*
* Description:
* config i2c param
*
****************************************************************************/
static void bl602_i2c_config_para(struct bl602_i2c_priv_s *priv)
{
struct i2c_msg_s *msg = &priv->msgv[priv->msgid];
if (msg->flags & I2C_M_READ)
{
modifyreg32(BL602_I2C_CONFIG, 0, I2C_CONFIG_CR_I2C_PKT_DIR);
}
else
{
modifyreg32(BL602_I2C_CONFIG, I2C_CONFIG_CR_I2C_PKT_DIR, 0);
}
modifyreg32(BL602_I2C_CONFIG,
I2C_CONFIG_CR_I2C_SLV_ADDR_MASK,
msg->addr << I2C_CONFIG_CR_I2C_SLV_ADDR_SHIFT);
if (priv->subflag > 0)
{
modifyreg32(BL602_I2C_CONFIG, 0, I2C_CONFIG_CR_I2C_SUB_ADDR_EN);
modifyreg32(BL602_I2C_CONFIG,
I2C_CONFIG_CR_I2C_SUB_ADDR_BC_MASK,
(priv->sublen - 1) << I2C_CONFIG_CR_I2C_SUB_ADDR_BC_SHIFT);
}
else
{
modifyreg32(BL602_I2C_CONFIG, I2C_CONFIG_CR_I2C_SUB_ADDR_EN, 0);
}
modifyreg32(BL602_I2C_CONFIG,
I2C_CONFIG_CR_I2C_PKT_LEN_MASK,
(msg->length - 1) << I2C_CONFIG_CR_I2C_PKT_LEN_SHIFT);
if (priv->subflag > 0)
{
putreg32(priv->subaddr, BL602_I2C_SUB_ADDR);
}
}
/****************************************************************************
* Name: bl602_i2c_intmask
*
* Description:
* Mask/Unmask the I2C interrupt
*
****************************************************************************/
static void bl602_i2c_intmask(uint8_t int_type, uint8_t int_mask)
{
switch (int_type)
{
case I2C_TRANS_END_INT:
if (int_mask == 0)
{
/* UNMASK(Enable) this interrupt */
modifyreg32(BL602_I2C_INT_STS, 0, I2C_INT_STS_CR_END_EN);
modifyreg32(BL602_I2C_INT_STS, I2C_INT_STS_CR_END_MASK, 0);
}
else
{
/* MASK(Disable) this interrupt */
modifyreg32(BL602_I2C_INT_STS, I2C_INT_STS_CR_END_EN, 0);
modifyreg32(BL602_I2C_INT_STS, 0, I2C_INT_STS_CR_END_MASK);
}
break;
case I2C_TX_FIFO_READY_INT:
if (int_mask == 0)
{
/* UNMASK(Enable) this interrupt */
modifyreg32(BL602_I2C_INT_STS, 0, I2C_INT_STS_CR_TXF_EN);
modifyreg32(BL602_I2C_INT_STS, I2C_INT_STS_CR_TXF_MASK, 0);
}
else
{
/* MASK(Disable) this interrupt */
modifyreg32(BL602_I2C_INT_STS, I2C_INT_STS_CR_TXF_EN, 0);
modifyreg32(BL602_I2C_INT_STS, 0, I2C_INT_STS_CR_TXF_MASK);
}
break;
case I2C_RX_FIFO_READY_INT:
if (int_mask == 0)
{
/* UNMASK(Enable) this interrupt */
modifyreg32(BL602_I2C_INT_STS, 0, I2C_INT_STS_CR_RXF_EN);
modifyreg32(BL602_I2C_INT_STS, I2C_INT_STS_CR_RXF_MASK, 0);
}
else
{
/* MASK(Disable) this interrupt */
modifyreg32(BL602_I2C_INT_STS, I2C_INT_STS_CR_RXF_EN, 0);
modifyreg32(BL602_I2C_INT_STS, 0, I2C_INT_STS_CR_RXF_MASK);
}
break;
case I2C_NACK_RECV_INT:
if (int_mask == 0)
{
/* UNMASK(Enable) this interrupt */
modifyreg32(BL602_I2C_INT_STS, 0, I2C_INT_STS_CR_NAK_EN);
modifyreg32(BL602_I2C_INT_STS, I2C_INT_STS_CR_NAK_MASK, 0);
}
else
{
/* MASK(Disable) this interrupt */
modifyreg32(BL602_I2C_INT_STS, I2C_INT_STS_CR_NAK_EN, 0);
modifyreg32(BL602_I2C_INT_STS, 0, I2C_INT_STS_CR_NAK_MASK);
}
break;
case I2C_ARB_LOST_INT:
if (int_mask == 0)
{
/* UNMASK(Enable) this interrupt */
modifyreg32(BL602_I2C_INT_STS, 0, I2C_INT_STS_CR_ARB_EN);
modifyreg32(BL602_I2C_INT_STS, I2C_INT_STS_CR_ARB_MASK, 0);
}
else
{
/* MASK(Disable) this interrupt */
modifyreg32(BL602_I2C_INT_STS, I2C_INT_STS_CR_ARB_EN, 0);
modifyreg32(BL602_I2C_INT_STS, 0, I2C_INT_STS_CR_ARB_MASK);
}
break;
case I2C_FIFO_ERR_INT:
if (int_mask == 0)
{
/* UNMASK(Enable) this interrupt */
modifyreg32(BL602_I2C_INT_STS, 0, I2C_INT_STS_CR_FER_EN);
modifyreg32(BL602_I2C_INT_STS, I2C_INT_STS_CR_FER_MASK, 0);
}
else
{
/* MASK(Disable) this interrupt */
modifyreg32(BL602_I2C_INT_STS, I2C_INT_STS_CR_FER_EN, 0);
modifyreg32(BL602_I2C_INT_STS, 0, I2C_INT_STS_CR_FER_MASK);
}
break;
case I2C_INT_ALL:
if (int_mask == 0)
{
/* UNMASK(Enable) this interrupt */
modifyreg32(BL602_I2C_INT_STS, 0, I2C_INT_STS_CR_END_EN
| I2C_INT_STS_CR_TXF_EN | I2C_INT_STS_CR_RXF_EN
| I2C_INT_STS_CR_NAK_EN | I2C_INT_STS_CR_ARB_EN
| I2C_INT_STS_CR_FER_EN);
modifyreg32(BL602_I2C_INT_STS, I2C_INT_STS_CR_END_MASK
| I2C_INT_STS_CR_TXF_MASK | I2C_INT_STS_CR_RXF_MASK
| I2C_INT_STS_CR_NAK_MASK | I2C_INT_STS_CR_ARB_MASK
| I2C_INT_STS_CR_FER_MASK, 0);
}
else
{
/* MASK(Disable) this interrupt */
modifyreg32(BL602_I2C_INT_STS, I2C_INT_STS_CR_END_EN
| I2C_INT_STS_CR_TXF_EN | I2C_INT_STS_CR_RXF_EN
| I2C_INT_STS_CR_NAK_EN | I2C_INT_STS_CR_ARB_EN
| I2C_INT_STS_CR_FER_EN, 0);
modifyreg32(BL602_I2C_INT_STS, 0, I2C_INT_STS_CR_END_MASK
| I2C_INT_STS_CR_TXF_MASK | I2C_INT_STS_CR_RXF_MASK
| I2C_INT_STS_CR_NAK_MASK | I2C_INT_STS_CR_ARB_MASK
| I2C_INT_STS_CR_FER_MASK);
}
break;
default:
break;
}
}
/****************************************************************************
* Name: bl602_i2c_enable
*
* Description:
* i2c enable
*
****************************************************************************/
static void bl602_i2c_enable(void)
{
modifyreg32(BL602_I2C_FIFO_CONFIG_0, 0, I2C_FIFO_CONFIG_0_TX_FIFO_CLR);
modifyreg32(BL602_I2C_FIFO_CONFIG_0, 0, I2C_FIFO_CONFIG_0_RX_FIFO_CLR);
modifyreg32(BL602_I2C_CONFIG, 0, I2C_CONFIG_CR_I2C_M_EN);
}
/****************************************************************************
* Name: bl602_i2c_transfer_enable
*
* Description:
* i2c transfer enable
*
****************************************************************************/
static void bl602_i2c_transfer_enable(struct bl602_i2c_priv_s *priv)
{
struct i2c_msg_s *msg = &priv->msgv[priv->msgid];
if (msg->flags & I2C_M_READ)
{
bl602_i2c_intmask(I2C_RX_FIFO_READY_INT, 0);
}
else
{
bl602_i2c_intmask(I2C_TX_FIFO_READY_INT, 0);
}
bl602_i2c_intmask(I2C_TRANS_END_INT, 0);
bl602_i2c_intmask(I2C_FIFO_ERR_INT, 0);
bl602_i2c_intmask(I2C_ARB_LOST_INT, 0);
bl602_i2c_intmask(I2C_NACK_RECV_INT, 0);
bl602_i2c_enable();
}
/****************************************************************************
* Name: bl602_i2c_start_transfer
*
* Description:
* Send I2C start signal
*
****************************************************************************/
static void bl602_i2c_start_transfer(struct bl602_i2c_priv_s *priv)
{
bl602_i2c_clear_status(I2C0);
bl602_i2c_config_para(priv);
bl602_i2c_transfer_enable(priv);
}
/****************************************************************************
* Device Driver Operations
****************************************************************************/
/****************************************************************************
* Name: bl602_i2c_setsclsync
*
* Description:
* set i2c scl sync
*
****************************************************************************/
static void bl602_i2c_setsclsync(uint8_t enable)
{
if (enable)
{
modifyreg32(BL602_I2C_CONFIG, 0, I2C_CONFIG_CR_I2C_SCL_SYNC_EN);
}
else
{
modifyreg32(BL602_I2C_CONFIG, I2C_CONFIG_CR_I2C_SCL_SYNC_EN, 0);
}
}
/****************************************************************************
* Name: bl602_i2c_setprd
*
* Description:
* set i2c prd
*
****************************************************************************/
static void bl602_i2c_setprd(uint8_t phase)
{
modifyreg32(BL602_I2C_PRD_START, I2C_PRD_START_CR_PRD_S_PH_0_MASK, phase);
modifyreg32(BL602_I2C_PRD_START,
I2C_PRD_START_CR_PRD_S_PH_1_MASK,
phase << I2C_PRD_START_CR_PRD_S_PH_1_SHIFT);
modifyreg32(BL602_I2C_PRD_START,
I2C_PRD_START_CR_PRD_S_PH_2_MASK,
phase << I2C_PRD_START_CR_PRD_S_PH_2_SHIFT);
modifyreg32(BL602_I2C_PRD_START,
I2C_PRD_START_CR_PRD_S_PH_3_MASK,
phase << I2C_PRD_START_CR_PRD_S_PH_3_SHIFT);
modifyreg32(BL602_I2C_PRD_STOP, I2C_PRD_STOP_CR_PRD_P_PH_0_MASK, phase);
modifyreg32(BL602_I2C_PRD_STOP,
I2C_PRD_STOP_CR_PRD_P_PH_1_MASK,
phase << I2C_PRD_STOP_CR_PRD_P_PH_1_SHIFT);
modifyreg32(BL602_I2C_PRD_STOP,
I2C_PRD_STOP_CR_PRD_P_PH_2_MASK,
phase << I2C_PRD_STOP_CR_PRD_P_PH_2_SHIFT);
modifyreg32(BL602_I2C_PRD_STOP,
I2C_PRD_STOP_CR_PRD_P_PH_3_MASK,
phase << I2C_PRD_STOP_CR_PRD_P_PH_3_SHIFT);
modifyreg32(BL602_I2C_PRD_DATA, I2C_PRD_DATA_CR_PRD_D_PH_0_MASK, phase);
modifyreg32(BL602_I2C_PRD_DATA,
I2C_PRD_DATA_CR_PRD_D_PH_1_MASK,
phase << I2C_PRD_DATA_CR_PRD_D_PH_1_SHIFT);
modifyreg32(BL602_I2C_PRD_DATA,
I2C_PRD_DATA_CR_PRD_D_PH_2_MASK,
phase << I2C_PRD_DATA_CR_PRD_D_PH_2_SHIFT);
modifyreg32(BL602_I2C_PRD_DATA,
I2C_PRD_DATA_CR_PRD_D_PH_3_MASK,
phase << I2C_PRD_DATA_CR_PRD_D_PH_3_SHIFT);
}
/****************************************************************************
* Name: bl602_set_i2c_clk
*
* Description:
* set I2C clock.
*
****************************************************************************/
void bl602_set_i2c_clk(uint8_t enable, uint8_t div)
{
modifyreg32(BL602_CLK_CFG3,
CLK_CFG3_I2C_CLK_DIV_MASK,
div << CLK_CFG3_I2C_CLK_DIV_SHIFT);
if (enable)
{
modifyreg32(BL602_CLK_CFG3, 0, CLK_CFG3_I2C_CLK_EN);
}
else
{
modifyreg32(BL602_CLK_CFG3, CLK_CFG3_I2C_CLK_EN, 0);
}
}
/****************************************************************************
* Name: bl602_i2c_clockset
*
* Description:
* set i2c clock
*
****************************************************************************/
static void bl602_i2c_clockset(uint32_t clk)
{
uint8_t bclk_div = 0;
uint32_t sys_clock = 0;
bclk_div = bl602_glb_get_bclk_div();
sys_clock = getreg32(BL602_HBN_RSV2);
if (clk >= 100000)
{
bl602_set_i2c_clk(1, 0);
bl602_i2c_setprd((sys_clock / (bclk_div + 1)) / (clk * 4) -
1);
}
else if (clk >= 8000)
{
bl602_set_i2c_clk(1, 9);
bl602_i2c_setprd(
((sys_clock / (bclk_div + 1)) / 10) / (clk * 4) - 1);
}
else if (clk >= 800)
{
bl602_set_i2c_clk(1, 99);
bl602_i2c_setprd(
((sys_clock / (bclk_div + 1)) / 100) / (clk * 4) - 1);
}
else
{
bl602_set_i2c_clk(1, 255);
bl602_i2c_setprd(
((sys_clock / (bclk_div + 1)) / 256) / (clk * 4) - 1);
}
}
/****************************************************************************
* Name: bl602_i2c_set_freq
*
* Description:
* set i2c freq
*
****************************************************************************/
static void bl602_i2c_set_freq(int freq)
{
bl602_i2c_setsclsync(0);
bl602_i2c_clockset(freq);
}
/****************************************************************************
* Name: bl602_i2c_transfer
*
* Description:
* Generic I2C transfer function
*
****************************************************************************/
static int bl602_i2c_transfer(struct i2c_master_s *dev,
struct i2c_msg_s * msgs,
int count)
{
int i;
int j;
int ret = OK;
struct bl602_i2c_priv_s *priv = (struct bl602_i2c_priv_s *)dev;
if (count <= 0)
{
i2cerr("count is error\n");
return -1;
}
ret = nxsem_wait_uninterruptible(&priv->sem_excl);
if (ret < 0)
{
i2cerr("take sem_excl error\n");
return ret;
}
ret = nxsem_wait_uninterruptible(&priv->sem_isr);
if (ret < 0)
{
i2cerr("take sem_irq error\n");
return ret;
}
priv->msgv = msgs;
for (i = 0; i < count; i++)
{
priv->bytes = 0;
priv->i2cstate = EV_I2C_END_INT;
bl602_i2c_set_freq(msgs[i].frequency);
/* if msgs[i].flag I2C_M_NOSTOP,means start i2c with subddr */
if (msgs[i].flags & I2C_M_NOSTOP)
{
priv->subflag = 1;
priv->subaddr = 0;
for (j = 0; j < msgs[i].length; j++)
{
priv->subaddr += msgs[i].buffer[j] << (j * 8);
}
priv->sublen = msgs[i].length;
i++;
}
else
{
priv->subflag = 0;
priv->subaddr = 0;
priv->sublen = 0;
}
priv->msgid = i;
i2cinfo("subflag=%d, subaddr=0x%lx, sublen=%d\n", priv->subflag, priv->subaddr, priv->sublen); ////
bl602_i2c_start_transfer(priv);
/* wait for transter finished */
ret = nxsem_wait_uninterruptible(&priv->sem_isr);
if (ret < 0)
{
i2cerr("transter error\n");
return ret;
}
if (priv->i2cstate == EV_I2C_END_INT)
{
i2cinfo("i2c transfer success\n");
#ifdef CONFIG_INPUT_CST816S
/* Workaround for CST816S. See https://github.com/lupyuen/cst816s-nuttx#i2c-logging */
i2cwarn("i2c transfer success\n");
#endif /* CONFIG_INPUT_CST816S */
}
else
{
i2cerr("i2c transfer error, event = %d\n", priv->i2cstate);
}
nxsem_post(&priv->sem_isr);
}
nxsem_post(&priv->sem_excl);
return ret;
}
/****************************************************************************
* Name: bl602_i2c_reset
*
* Description:
* Perform an I2C bus reset in an attempt to break loose stuck I2C devices.
*
* Input Parameters:
* dev - Device-specific state data
*
* Returned Value:
* Zero (OK) on success; a negated errno value on failure.
*
****************************************************************************/
#ifdef CONFIG_I2C_RESET
static int bl602_i2c_reset(struct i2c_master_s *dev)
{
struct bl602_i2c_priv_s *priv = (struct bl602_i2c_priv_s *)dev;
bl602_swrst_ahb_slave1(AHB_SLAVE1_I2C);
bl602_i2c_set_freq(priv->config->clk_freq);
bl602_i2c_disable();
up_enable_irq(BL602_IRQ_I2C);
bl602_i2c_intmask(I2C_INT_ALL, 1);
priv->i2cstate = EV_I2C_END_INT;
priv->msgid = 0;
priv->bytes = 0;
return OK;
}
#endif
/****************************************************************************
* Name: bl602_i2c_transferbytes
*
* Description:
* i2c transfer bytes.
*
****************************************************************************/
static void bl602_i2c_transferbytes(struct bl602_i2c_priv_s *priv)
{
struct i2c_msg_s *msg = &priv->msgv[priv->msgid];
if (msg->flags & I2C_M_READ)
{
if (priv->i2cstate == EV_I2C_RXF_INT)
{
if (priv->bytes < msg->length)
{
bl602_i2c_recvdata(priv);
}
else if (priv->bytes == msg->length)
{
bl602_i2c_intmask(I2C_RX_FIFO_READY_INT, 1);
return;
}
else
{
}
}
}
else
{
if (priv->i2cstate == EV_I2C_TXF_INT)
{
if (msg->length > priv->bytes)
{
bl602_i2c_send_data(priv);
}
else if (priv->bytes == msg->length)
{
bl602_i2c_intmask(I2C_TX_FIFO_READY_INT, 1);
return;
}
else
{
}
}
}
}
/****************************************************************************
* Name: bl602_i2c_disable
*
* Description:
* disable i2c
*
****************************************************************************/
static void bl602_i2c_disable(void)
{
modifyreg32(BL602_I2C_CONFIG, I2C_CONFIG_CR_I2C_M_EN, 0);
/* Clear I2C fifo */
modifyreg32(BL602_I2C_FIFO_CONFIG_0, 0, I2C_FIFO_CONFIG_0_TX_FIFO_CLR);
modifyreg32(BL602_I2C_FIFO_CONFIG_0, 0, I2C_FIFO_CONFIG_0_RX_FIFO_CLR);
/* Clear I2C interrupt status */
modifyreg32(BL602_I2C_INT_STS, 0, I2C_INT_STS_CR_END_CLR);
modifyreg32(BL602_I2C_INT_STS, 0, I2C_INT_STS_CR_NAK_CLR);
modifyreg32(BL602_I2C_INT_STS, 0, I2C_INT_STS_CR_ARB_CLR);
}
/****************************************************************************
* Name: bl602_i2c_callback
*
* Description:
* callback function.
*
****************************************************************************/
static void bl602_i2c_callback(struct bl602_i2c_priv_s *priv)
{
bl602_i2c_disable();
bl602_i2c_intmask(I2C_INT_ALL, 1);
bl602_i2c_clear_status(I2C0);
nxsem_post(&priv->sem_isr);
}
/****************************************************************************
* Name: bl602_i2c_irq
*
* Description:
* This is the common I2C interrupt handler. It will be invoked
* when an interrupt received on the device.
*
****************************************************************************/
static int bl602_i2c_irq(int cpuint, void *context, void *arg)
{
uint32_t tmp_val;
struct bl602_i2c_priv_s *priv = (struct bl602_i2c_priv_s *)arg;
tmp_val = getreg32(BL602_I2C_INT_STS);
if (tmp_val & I2C_INT_STS_RXF_INT)
{
priv->i2cstate = EV_I2C_RXF_INT;
}
else if (tmp_val & I2C_INT_STS_END_INT)
{
priv->i2cstate = EV_I2C_END_INT;
bl602_i2c_callback(priv);
return 0;
}
else if (tmp_val & I2C_INT_STS_NAK_INT)
{
priv->i2cstate = EV_I2C_NAK_INT;
bl602_i2c_callback(priv);
return -1;
}
else if (tmp_val & I2C_INT_STS_TXF_INT)
{
priv->i2cstate = EV_I2C_TXF_INT;
}
else if (tmp_val & I2C_INT_STS_ARB_INT)
{
priv->i2cstate = EV_I2C_ARB_INT;
bl602_i2c_callback(priv);
return -1;
}
else if (tmp_val & I2C_INT_STS_FER_INT)
{
priv->i2cstate = EV_I2C_FER_INT;
bl602_i2c_callback(priv);
return -1;
}
else
{
i2cerr("other interrupt\n");
priv->i2cstate = EV_I2C_UNKNOW_INT;
bl602_i2c_callback(priv);
return -1;
}
bl602_i2c_transferbytes(priv);
return 0;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: bl602_i2cbus_initialize
*
* Description:
* Initialize one I2C bus
*
****************************************************************************/
struct i2c_master_s *bl602_i2cbus_initialize(int port)
{
irqstate_t flags;
struct bl602_i2c_priv_s * priv;
const struct bl602_i2c_config_s *config;
switch (port)
{
#ifdef CONFIG_BL602_I2C0
case 0:
priv = (struct bl602_i2c_priv_s *)&bl602_i2c0_priv;
break;
#endif
default:
return NULL;
}
config = priv->config;
flags = enter_critical_section();
priv->refs++;
if (priv->refs > 1)
{
leave_critical_section(flags);
return (struct i2c_master_s *)priv;
}
bl602_configgpio(BOARD_I2C_SCL);
bl602_configgpio(BOARD_I2C_SDA);