31
31
32
32
#define DR_REG_I2C_EXT_BASE_FIXED 0x60013000
33
33
#define DR_REG_I2C1_EXT_BASE_FIXED 0x60027000
34
-
34
+
35
35
struct i2c_struct_t {
36
36
i2c_dev_t * dev ;
37
37
#if !CONFIG_DISABLE_HAL_LOCKS
38
38
xSemaphoreHandle lock ;
39
39
#endif
40
40
uint8_t num ;
41
- I2C_MODE_t mode ;
41
+ I2C_MODE_t mode ;
42
42
I2C_STAGE_t stage ;
43
- I2C_ERROR_t error ;
44
- EventGroupHandle_t i2c_event ; // a way to monitor ISR process
45
- // maybe use it to trigger callback for OnRequest()
46
- intr_handle_t intr_handle ; /*!< I2C interrupt handle*/
43
+ I2C_ERROR_t error ;
44
+ EventGroupHandle_t i2c_event ; // a way to monitor ISR process
45
+ // maybe use it to trigger callback for OnRequest()
46
+ intr_handle_t intr_handle ; /*!< I2C interrupt handle*/
47
47
I2C_DATA_QUEUE_t * dq ;
48
48
uint16_t queueCount ;
49
49
uint16_t queuePos ;
@@ -78,6 +78,7 @@ static i2c_t _i2c_bus_array[2] = {
78
78
#endif
79
79
80
80
/* Stickbreaker added for ISR 11/2017
81
+ functional with Silicon date=0x16042000
81
82
*/
82
83
static i2c_err_t i2cAddQueue (i2c_t * i2c ,uint8_t mode , uint16_t i2cDeviceAddr , uint8_t * dataPtr , uint16_t dataLen ,bool sendStop , EventGroupHandle_t event ){
83
84
@@ -153,6 +154,11 @@ i2c_err_t i2cAddQueueRead(i2c_t * i2c, uint16_t i2cDeviceAddr, uint8_t *dataPtr,
153
154
//10bit read is kind of weird, first you do a 0byte Write with 10bit
154
155
// address, then a ReSTART then a 7bit Read using the the upper 7bit +
155
156
// readBit.
157
+
158
+ // this might cause an internal register pointer problem with 10bit
159
+ // devices, But, Don't have any to test agains.
160
+ // this is the Industry Standard specification.
161
+
156
162
if ((i2cDeviceAddr & 0xFC00 )== 0x7800 ){ // ten bit read
157
163
i2c_err_t err = i2cAddQueue (i2c ,0 ,i2cDeviceAddr ,NULL ,0 ,false,event );
158
164
if (err == I2C_ERROR_OK ){
@@ -218,26 +224,26 @@ i2c_err_t i2cDetachSDA(i2c_t * i2c, int8_t sda)
218
224
* */
219
225
void i2cSetCmd (i2c_t * i2c , uint8_t index , uint8_t op_code , uint8_t byte_num , bool ack_val , bool ack_exp , bool ack_check )
220
226
{
221
- I2C_COMMAND_t cmd ;
222
- cmd .val = 0 ;
223
- cmd .ack_en = ack_check ;
224
- cmd .ack_exp = ack_exp ;
225
- cmd .ack_val = ack_val ;
226
- cmd .byte_num = byte_num ;
227
- cmd .op_code = op_code ;
227
+ I2C_COMMAND_t cmd ;
228
+ cmd .val = 0 ;
229
+ cmd .ack_en = ack_check ;
230
+ cmd .ack_exp = ack_exp ;
231
+ cmd .ack_val = ack_val ;
232
+ cmd .byte_num = byte_num ;
233
+ cmd .op_code = op_code ;
228
234
i2c -> dev -> command [index ].val = cmd .val ;
229
235
}
230
236
231
237
void i2cResetFiFo (i2c_t * i2c )
232
238
{
233
- I2C_FIFO_CONF_t f ;
234
- f .val = i2c -> dev -> fifo_conf .val ;
235
- f .tx_fifo_rst = 1 ;
236
- f .rx_fifo_rst = 1 ;
237
- i2c -> dev -> fifo_conf .val = f .val ;
238
- f .tx_fifo_rst = 0 ;
239
- f .rx_fifo_rst = 0 ;
240
- i2c -> dev -> fifo_conf .val = f .val ;
239
+ I2C_FIFO_CONF_t f ;
240
+ f .val = i2c -> dev -> fifo_conf .val ;
241
+ f .tx_fifo_rst = 1 ;
242
+ f .rx_fifo_rst = 1 ;
243
+ i2c -> dev -> fifo_conf .val = f .val ;
244
+ f .tx_fifo_rst = 0 ;
245
+ f .rx_fifo_rst = 0 ;
246
+ i2c -> dev -> fifo_conf .val = f .val ;
241
247
}
242
248
243
249
i2c_err_t i2cSetFrequency (i2c_t * i2c , uint32_t clk_speed )
@@ -373,6 +379,24 @@ void i2cReset(i2c_t* i2c){
373
379
static uint32_t intBuff [INTBUFFMAX ][3 ];
374
380
static uint32_t intPos = 0 ;
375
381
#endif
382
+
383
+ /* Stickbreaker ISR mode debug support
384
+ */
385
+ void IRAM_ATTR dumpCmdQueue (i2c_t * i2c ){
386
+ uint8_t i = 0 ;
387
+ while (i < 16 ){
388
+ I2C_COMMAND_t c ;
389
+ c .val = i2c -> dev -> command [i ].val ;
390
+ log_e ("[%2d] %c op[%d] val[%d] exp[%d] en[%d] bytes[%d]" ,i ,(c .done ?'Y' :'N' ),
391
+ c .op_code ,
392
+ c .ack_val ,
393
+ c .ack_exp ,
394
+ c .ack_en ,
395
+ c .byte_num );
396
+ i ++ ;
397
+ }
398
+ }
399
+
376
400
/* Stickbreaker ISR mode support
377
401
*/
378
402
static void IRAM_ATTR fillCmdQueue (i2c_t * i2c , bool INTS ){
@@ -438,7 +462,7 @@ while(!done){ // fill the command[] until either 0..14 filled or out of cmds and
438
462
if ((!done )&& (tdq -> ctrl .addrCmdSent )){ //room in command[] for at least One data (read/Write) cmd
439
463
uint8_t blkSize = 0 ; // max is 255? does numBytes =0 actually mean 256? haven't tried it.
440
464
//log_e("needed=%2d index=%d",*neededRead,cmdIdx);
441
- while (( tdq -> cmdBytesNeeded > tdq -> ctrl .mode )&& (!done )) { // more bytes needed and room in cmd queue, leave room for END
465
+ while (( tdq -> cmdBytesNeeded > tdq -> ctrl .mode )&& (!done )) { // more bytes needed and room in cmd queue, leave room for END
442
466
blkSize = (tdq -> cmdBytesNeeded > 255 )?255 :(tdq -> cmdBytesNeeded - tdq -> ctrl .mode ); // Last read cmd needs different ACK setting, so leave 1 byte remainer on reads
443
467
tdq -> cmdBytesNeeded -= blkSize ; //
444
468
if (tdq -> ctrl .mode == 1 ){ //read mode
@@ -521,7 +545,7 @@ while(!done){ // fill the command[] until either 0..14 filled or out of cmds and
521
545
522
546
if (cmdIdx == 15 ){ //need continuation, even if STOP is in 14, it will not matter
523
547
// cmd buffer is almost full, Add END as a continuation feature
524
- // log_e("END at %d, left=%d",cmdIdx,neededRead);
548
+ // log_e("END at %d, left=%d",cmdIdx,neededRead);
525
549
i2cSetCmd (i2c , (cmdIdx )++ ,I2C_CMD_END , 0 ,false,false,false);
526
550
i2c -> dev -> int_ena .end_detect = 1 ; //maybe?
527
551
i2c -> dev -> int_clr .end_detect = 1 ; //maybe?
@@ -583,7 +607,7 @@ log_n("Enable Core Debug Level \"Error\"");
583
607
584
608
void i2cDumpI2c (i2c_t * i2c ){
585
609
log_e ("i2c=%p" ,i2c );
586
- log_e ("dev=%p" ,i2c -> dev );
610
+ log_e ("dev=%p date=%p " ,i2c -> dev , i2c -> dev -> date );
587
611
log_e ("lock=%p" ,i2c -> lock );
588
612
log_e ("num=%d" ,i2c -> num );
589
613
log_e ("mode=%d" ,i2c -> mode );
@@ -598,23 +622,6 @@ log_e("byteCnt=%d",i2c->byteCnt);
598
622
if (i2c -> dq ) i2cDumpDqData (i2c );
599
623
}
600
624
601
- /* Stickbreaker ISR mode debug support
602
- */
603
- void IRAM_ATTR dumpCmdQueue (i2c_t * i2c ){
604
- uint8_t i = 0 ;
605
- while (i < 16 ){
606
- I2C_COMMAND_t c ;
607
- c .val = i2c -> dev -> command [i ].val ;
608
- log_e ("[%2d] %c op[%d] val[%d] exp[%d] en[%d] bytes[%d]" ,i ,(c .done ?'Y' :'N' ),
609
- c .op_code ,
610
- c .ack_val ,
611
- c .ack_exp ,
612
- c .ack_en ,
613
- c .byte_num );
614
- i ++ ;
615
- }
616
- }
617
-
618
625
/* Stickbreaker ISR mode support
619
626
*/
620
627
static void IRAM_ATTR fillTxFifo (i2c_t * i2c ){
@@ -872,34 +879,37 @@ while (activeInt != 0) { // Ordering of 'if(activeInt)' statements is important,
872
879
if (p_i2c -> mode == I2C_MASTER ) {
873
880
// log_e("AcK Err byteCnt=%d, queuepos=%d",p_i2c->byteCnt,p_i2c->queuePos);
874
881
if (p_i2c -> byteCnt == 1 ) i2cIsrExit (p_i2c ,EVENT_ERROR_NAK ,true);
875
- else if ((p_i2c -> byteCnt == 2 ) && (p_i2c -> dq [p_i2c -> queuePos ].ctrl .addrReq == 2 ))
882
+ else if ((p_i2c -> byteCnt == 2 ) && (p_i2c -> dq [p_i2c -> queuePos ].ctrl .addrReq == 2 ))
876
883
i2cIsrExit (p_i2c ,EVENT_ERROR_NAK ,true);
877
884
else i2cIsrExit (p_i2c ,EVENT_ERROR_DATA_NAK ,true);
878
885
}
879
886
return ;
880
887
}
881
888
882
889
if (activeInt & I2C_TIME_OUT_INT_ST_M ) {//fatal death Happens Here
883
- i2cIsrExit (p_i2c ,EVENT_ERROR_TIMEOUT ,true);
884
- return ;
890
+ // let Gross timeout occure
891
+ p_i2c -> dev -> int_clr .time_out = 1 ;
892
+ activeInt &=~I2C_TIME_OUT_INT_ST ;
893
+ // i2cIsrExit(p_i2c,EVENT_ERROR_TIMEOUT,true);
894
+ // return;
885
895
}
886
-
896
+
887
897
if (activeInt & I2C_TRANS_COMPLETE_INT_ST_M ) {
888
898
i2cIsrExit (p_i2c ,EVENT_DONE ,false);
889
899
return ; // no more work to do
890
900
/*
891
901
// how does slave mode act?
892
902
if (p_i2c->mode == I2C_SLAVE) { // STOP detected
893
- // empty fifo
894
- // dispatch callback
903
+ // empty fifo
904
+ // dispatch callback
895
905
*/
896
906
}
897
907
898
908
if (activeInt & I2C_ARBITRATION_LOST_INT_ST_M ) { //fatal
899
909
i2cIsrExit (p_i2c ,EVENT_ERROR_ARBITRATION ,true);
900
910
return ; // no more work to do
901
911
}
902
-
912
+
903
913
if (activeInt & I2C_SLAVE_TRAN_COMP_INT_ST_M ) {
904
914
p_i2c -> dev -> int_clr .slave_tran_comp = 1 ;
905
915
// need to complete this !
@@ -908,7 +918,7 @@ while (activeInt != 0) { // Ordering of 'if(activeInt)' statements is important,
908
918
if (activeInt & I2C_END_DETECT_INT_ST_M ) {
909
919
p_i2c -> dev -> int_ena .end_detect = 0 ;
910
920
p_i2c -> dev -> int_clr .end_detect = 1 ;
911
- p_i2c -> dev -> ctr .trans_start = 0 ;
921
+ p_i2c -> dev -> ctr .trans_start = 0 ;
912
922
fillCmdQueue (p_i2c ,true); // enable interrupts
913
923
p_i2c -> dev -> ctr .trans_start = 1 ; // go for it
914
924
activeInt &=~I2C_END_DETECT_INT_ST_M ;
@@ -948,7 +958,7 @@ i2c_err_t i2cProcQueue(i2c_t * i2c, uint32_t *readCount, uint16_t timeOutMillis)
948
958
//log_e("procQueue i2c=%p",&i2c);
949
959
* readCount = 0 ; //total reads accomplished in all queue elements
950
960
if (i2c == NULL ){
951
- return I2C_ERROR_DEV ;
961
+ return I2C_ERROR_DEV ;
952
962
}
953
963
954
964
I2C_MUTEX_LOCK ();
@@ -1172,7 +1182,6 @@ return i2cFreeQueue(i2c);
1172
1182
24Nov17
1173
1183
Need to think about not usings I2C_MASTER_TRAN_COMP_INT_ST to adjust queuePos. This
1174
1184
INT triggers every byte. The only reason to know which byte is being transfered is
1175
- to decide where to store a READ or if an error occured. It may be possible to use
1176
1185
the status_reg.tx_fifo_cnt and a .txQueued to do this in the fillRxFifo(). The
1177
1186
same mechanism could work if an error occured in i2cErrorExit().
1178
1187
*/
0 commit comments