31
31
#include "esp32/rom/ets_sys.h"
32
32
#elif CONFIG_IDF_TARGET_ESP32S2
33
33
#include "esp32s2/rom/ets_sys.h"
34
- #else
34
+ #else
35
35
#error Target CONFIG_IDF_TARGET is not supported
36
36
#endif
37
37
#else // ESP32 Before IDF 4.0
@@ -110,7 +110,7 @@ typedef union {
110
110
};
111
111
uint32_t val ;
112
112
} I2C_FIFO_ST_t ;
113
-
113
+
114
114
// end from tools/sdk/include/soc/soc/i2c_struct.h
115
115
116
116
// sync between dispatch(i2cProcQueue) and worker(i2c_isr_handler_default)
@@ -353,7 +353,7 @@ static void i2cDumpI2c(i2c_t * i2c)
353
353
}
354
354
#endif
355
355
356
- #if (ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO )
356
+ #if (ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO )
357
357
static void i2cDumpInts (uint8_t num )
358
358
{
359
359
#if defined (ENABLE_I2C_DEBUG_BUFFER )
@@ -393,10 +393,10 @@ static void ARDUINO_ISR_ATTR i2cDumpStatus(i2c_t * i2c){
393
393
};
394
394
uint32_t val ;
395
395
} status_reg ;
396
-
396
+
397
397
status_reg sr ;
398
398
sr .val = i2c -> dev -> status_reg .val ;
399
-
399
+
400
400
log_i ("ack(%d) sl_rw(%d) to(%d) arb(%d) busy(%d) sl(%d) trans(%d) rx(%d) tx(%d) sclMain(%d) scl(%d)" ,sr .ack_rec ,sr .slave_rw ,sr .time_out ,sr .arb_lost ,sr .bus_busy ,sr .slave_addressed ,sr .byte_trans , sr .rx_fifo_cnt , sr .tx_fifo_cnt ,sr .scl_main_state_last , sr .scl_state_last );
401
401
}
402
402
#endif
@@ -449,17 +449,17 @@ if(i != fifoPos){// actual data
449
449
static void ARDUINO_ISR_ATTR i2cTriggerDumps (i2c_t * i2c , uint8_t trigger , const char locus []){
450
450
#if (ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO )&& (defined ENABLE_I2C_DEBUG_BUFFER )
451
451
if ( trigger ){
452
- log_i ("%s" ,locus );
452
+ log_i ("%s" ,locus );
453
453
if (trigger & 1 ) i2cDumpI2c (i2c );
454
454
if (trigger & 2 ) i2cDumpInts (i2c -> num );
455
455
if (trigger & 4 ) i2cDumpCmdQueue (i2c );
456
456
if (trigger & 8 ) i2cDumpStatus (i2c );
457
457
if (trigger & 16 ) i2cDumpFifo (i2c );
458
458
}
459
- #endif
459
+ #endif
460
460
}
461
461
// end of debug support routines
462
-
462
+
463
463
/* Start of CPU Clock change Support
464
464
*/
465
465
@@ -470,29 +470,29 @@ static void i2cApbChangeCallback(void * arg, apb_change_ev_t ev_type, uint32_t o
470
470
}
471
471
uint32_t oldFreq = 0 ;
472
472
switch (ev_type ){
473
- case APB_BEFORE_CHANGE :
473
+ case APB_BEFORE_CHANGE :
474
474
if (new_apb < 3000000 ) {// too slow
475
475
log_e ("apb speed %d too slow" ,new_apb );
476
476
break ;
477
477
}
478
478
I2C_MUTEX_LOCK (); // lock will spin until current transaction is completed
479
479
break ;
480
480
case APB_AFTER_CHANGE :
481
- oldFreq = (i2c -> dev -> scl_low_period .period + i2c -> dev -> scl_high_period .period ); //read old apbCycles
481
+ oldFreq = (i2c -> dev -> scl_low_period .period + i2c -> dev -> scl_high_period .period ); //read old apbCycles
482
482
if (oldFreq > 0 ) { // was configured with value
483
483
oldFreq = old_apb / oldFreq ;
484
484
i2cSetFrequency (i2c ,oldFreq );
485
485
}
486
486
I2C_MUTEX_UNLOCK ();
487
487
break ;
488
- default :
488
+ default :
489
489
log_e ("unk ev %u" ,ev_type );
490
490
I2C_MUTEX_UNLOCK ();
491
491
}
492
492
return ;
493
493
}
494
494
/* End of CPU Clock change Support
495
- */
495
+ */
496
496
static void ARDUINO_ISR_ATTR i2cSetCmd (i2c_t * i2c , uint8_t index , uint8_t op_code , uint8_t byte_num , bool ack_val , bool ack_exp , bool ack_check )
497
497
{
498
498
I2C_COMMAND_t cmd ;
@@ -504,7 +504,7 @@ static void ARDUINO_ISR_ATTR i2cSetCmd(i2c_t * i2c, uint8_t index, uint8_t op_co
504
504
cmd .op_code = op_code ;
505
505
i2c -> dev -> command [index ].val = cmd .val ;
506
506
}
507
-
507
+
508
508
509
509
static void ARDUINO_ISR_ATTR fillCmdQueue (i2c_t * i2c , bool INTS )
510
510
{
@@ -521,7 +521,7 @@ static void ARDUINO_ISR_ATTR fillCmdQueue(i2c_t * i2c, bool INTS)
521
521
bool ena_tx = false; // if we add a Write op, better enable TX_Fifo IRQ
522
522
523
523
while (!needMoreCmds && (qp < i2c -> queueCount )) { // check if more possible cmds
524
- if (i2c -> dq [qp ].ctrl .stopCmdSent ) {// marks that all required cmds[] have been added to peripheral
524
+ if (i2c -> dq [qp ].ctrl .stopCmdSent ) {// marks that all required cmds[] have been added to peripheral
525
525
qp ++ ;
526
526
} else {
527
527
needMoreCmds = true;
@@ -572,7 +572,7 @@ static void ARDUINO_ISR_ATTR fillCmdQueue(i2c_t * i2c, bool INTS)
572
572
uint8_t blkSize = 0 ; // max is 255
573
573
while (( tdq -> cmdBytesNeeded > tdq -> ctrl .mode )&& (!done )) { // more bytes needed and room in cmd queue, leave room for END
574
574
blkSize = (tdq -> cmdBytesNeeded > 255 )?255 :(tdq -> cmdBytesNeeded - tdq -> ctrl .mode ); // Last read cmd needs different ACK setting, so leave 1 byte remainder on reads
575
- tdq -> cmdBytesNeeded -= blkSize ;
575
+ tdq -> cmdBytesNeeded -= blkSize ;
576
576
if (tdq -> ctrl .mode == 1 ) { //read mode
577
577
i2cSetCmd (i2c , (cmdIdx )++ , I2C_CMD_READ , blkSize ,false,false,false); // read cmd, this can't be the last read.
578
578
ena_rx = true; // need to enable rxFifo IRQ
@@ -637,8 +637,8 @@ static void ARDUINO_ISR_ATTR fillCmdQueue(i2c_t * i2c, bool INTS)
637
637
if (cmdIdx == 15 ) { //need continuation, even if STOP is in 14, it will not matter
638
638
// cmd buffer is almost full, Add END as a continuation feature
639
639
i2cSetCmd (i2c , (cmdIdx )++ ,I2C_CMD_END , 0 ,false,false,false);
640
- i2c -> dev -> int_ena .end_detect = 1 ;
641
- i2c -> dev -> int_clr .end_detect = 1 ;
640
+ i2c -> dev -> int_ena .end_detect = 1 ;
641
+ i2c -> dev -> int_clr .end_detect = 1 ;
642
642
done = true;
643
643
}
644
644
@@ -731,9 +731,9 @@ static void ARDUINO_ISR_ATTR fillTxFifo(i2c_t * i2c)
731
731
if ( ! rxQueueEncountered ) {
732
732
rxQueueEncountered = true;
733
733
if (a > i2c -> queuePos ){
734
- i2c -> queuePos = a ;
734
+ i2c -> queuePos = a ;
735
735
}
736
- }
736
+ }
737
737
}
738
738
#if (ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO ) && (defined ENABLE_I2C_DEBUG_BUFFER )
739
739
@@ -759,18 +759,18 @@ static void ARDUINO_ISR_ATTR fillTxFifo(i2c_t * i2c)
759
759
static void ARDUINO_ISR_ATTR emptyRxFifo (i2c_t * i2c )
760
760
{
761
761
uint32_t d , cnt = 0 , moveCnt ;
762
-
762
+
763
763
moveCnt = i2c -> dev -> status_reg .rx_fifo_cnt ;//no need to check the reg until this many are read
764
764
765
- while ((moveCnt > 0 )&& (i2c -> queuePos < i2c -> queueCount )){ // data to move
765
+ while ((moveCnt > 0 )&& (i2c -> queuePos < i2c -> queueCount )){ // data to move
766
766
767
767
I2C_DATA_QUEUE_t * tdq = & i2c -> dq [i2c -> queuePos ]; //short cut
768
768
769
769
while ((tdq -> position >= tdq -> length )&& ( i2c -> queuePos < i2c -> queueCount )){ // find were to store
770
770
i2c -> queuePos ++ ;
771
771
tdq = & i2c -> dq [i2c -> queuePos ];
772
772
}
773
-
773
+
774
774
if (i2c -> queuePos >= i2c -> queueCount ){ // bad stuff, rx data but no place to put it!
775
775
log_e ("no Storage location for %d" ,moveCnt );
776
776
// discard
@@ -781,7 +781,7 @@ static void ARDUINO_ISR_ATTR emptyRxFifo(i2c_t * i2c)
781
781
}
782
782
break ;
783
783
}
784
-
784
+
785
785
if (tdq -> ctrl .mode == 1 ){ // read command
786
786
if (moveCnt > (tdq -> length - tdq -> position )) { //make sure they go in this dq
787
787
// part of these reads go into the next dq
@@ -807,7 +807,7 @@ static void ARDUINO_ISR_ATTR emptyRxFifo(i2c_t * i2c)
807
807
808
808
moveCnt = i2c -> dev -> status_reg .rx_fifo_cnt ; //any more out there?
809
809
}
810
-
810
+
811
811
#if (ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO )&& (defined ENABLE_I2C_DEBUG_BUFFER )
812
812
// update Debug rxCount
813
813
cnt += (intBuff [intPos [i2c -> num ]][1 ][i2c -> num ])& 0xffFF ;
@@ -866,16 +866,16 @@ static void ARDUINO_ISR_ATTR i2c_update_error_byte_cnt(i2c_t * i2c)
866
866
Only called after an error has occurred, so, most of the time this function is never used.
867
867
This function obliterates the need to interrupt monitor each byte transferred, at high bitrates
868
868
the byte interrupts were overwhelming the OS. Spurious Interrupts were being generated.
869
- it updates errorByteCnt, errorQueue.
869
+ it updates errorByteCnt, errorQueue.
870
870
*/
871
871
uint16_t a = 0 ; // start at top of DQ, count how many bytes added to tx fifo, and received from rx_fifo.
872
872
int16_t bc = 0 ;
873
873
I2C_DATA_QUEUE_t * tdq ;
874
- i2c -> errorByteCnt = 0 ;
874
+ i2c -> errorByteCnt = 0 ;
875
875
while ( a < i2c -> queueCount ){ // add up all bytes loaded into fifo's
876
876
tdq = & i2c -> dq [a ];
877
877
i2c -> errorByteCnt += tdq -> ctrl .addrSent ;
878
- i2c -> errorByteCnt += tdq -> position ;
878
+ i2c -> errorByteCnt += tdq -> position ;
879
879
a ++ ;
880
880
}
881
881
// now errorByteCnt contains total bytes moved into and out of FIFO's
@@ -900,10 +900,10 @@ static void ARDUINO_ISR_ATTR i2c_update_error_byte_cnt(i2c_t * i2c)
900
900
}
901
901
}
902
902
} else break ;
903
-
903
+
904
904
i2c -> errorQueue ++ ;
905
- }
906
-
905
+ }
906
+
907
907
i2c -> errorByteCnt = bc ;
908
908
}
909
909
@@ -936,7 +936,7 @@ static void ARDUINO_ISR_ATTR i2c_isr_handler_default(void* arg)
936
936
intBuff [intPos [p_i2c -> num ]][2 ][p_i2c -> num ] = xTaskGetTickCountFromISR (); // when IRQ fired
937
937
938
938
#endif
939
-
939
+
940
940
if (activeInt & I2C_TRANS_START_INT_ST_M ) {
941
941
if (p_i2c -> stage == I2C_STARTUP ) {
942
942
p_i2c -> stage = I2C_RUNNING ;
@@ -985,7 +985,7 @@ static void ARDUINO_ISR_ATTR i2c_isr_handler_default(void* arg)
985
985
// the Statemachine only has a 13.1ms max timout, some Devices >500ms
986
986
p_i2c -> dev -> int_clr .time_out = 1 ;
987
987
activeInt &=~I2C_TIME_OUT_INT_ST ;
988
- // since a timeout occurred, capture the rxFifo data
988
+ // since a timeout occurred, capture the rxFifo data
989
989
emptyRxFifo (p_i2c );
990
990
p_i2c -> dev -> int_clr .rx_fifo_full = 1 ;
991
991
p_i2c -> dev -> int_ena .rx_fifo_full = 1 ; //why?
@@ -1191,7 +1191,7 @@ i2c_err_t i2cProcQueue(i2c_t * i2c, uint32_t *readCount, uint16_t timeOutMillis)
1191
1191
i2c -> dev -> ctr .trans_start = 0 ; // Pause Machine
1192
1192
i2c -> dev -> timeout .tout = 0xFFFFF ; // max 13ms
1193
1193
i2c -> dev -> int_clr .val = 0x1FFF ; // kill them All!
1194
-
1194
+
1195
1195
i2c -> dev -> ctr .ms_mode = 1 ; // master!
1196
1196
i2c -> queuePos = 0 ;
1197
1197
i2c -> errorByteCnt = 0 ;
@@ -1229,7 +1229,7 @@ i2c_err_t i2cProcQueue(i2c_t * i2c, uint32_t *readCount, uint16_t timeOutMillis)
1229
1229
// sometimes the emptyRx() actually moves 31 bytes
1230
1230
// it hasn't overflowed yet, I cannot tell if the new byte is added while
1231
1231
// emptyRX() is executing or before?
1232
- // let i2cSetFrequency() set thrhds
1232
+ // let i2cSetFrequency() set thrhds
1233
1233
// f.rx_fifo_full_thrhd = 30; // 30 bytes before INT is issued
1234
1234
// f.tx_fifo_empty_thrhd = 0;
1235
1235
f .fifo_addr_cfg_en = 0 ; // no directed access
@@ -1244,7 +1244,7 @@ i2c_err_t i2cProcQueue(i2c_t * i2c, uint32_t *readCount, uint16_t timeOutMillis)
1244
1244
// it should receive a TXFIFO_EMPTY immediately, even before it
1245
1245
// receives the TRANS_START
1246
1246
1247
-
1247
+
1248
1248
uint32_t interruptsEnabled =
1249
1249
I2C_ACK_ERR_INT_ENA | // (BIT(10)) Causes Fatal Error Exit
1250
1250
I2C_TRANS_START_INT_ENA | // (BIT(9)) Triggered by trans_start=1, initial,END
@@ -1256,16 +1256,16 @@ i2c_err_t i2cProcQueue(i2c_t * i2c, uint32_t *readCount, uint16_t timeOutMillis)
1256
1256
I2C_RXFIFO_OVF_INT_ENA | //(BIT(2)) unhandled
1257
1257
I2C_TXFIFO_EMPTY_INT_ENA | // (BIT(1)) triggers fillTxFifo()
1258
1258
I2C_RXFIFO_FULL_INT_ENA ; // (BIT(0)) trigger emptyRxFifo()
1259
-
1259
+
1260
1260
i2c -> dev -> int_ena .val = interruptsEnabled ;
1261
-
1261
+
1262
1262
if (!i2c -> intr_handle ) { // create ISR for either peripheral
1263
1263
// log_i("create ISR %d",i2c->num);
1264
1264
uint32_t ret = 0 ;
1265
1265
uint32_t flags = ARDUINO_ISR_FLAG | //< ISR can be called if cache is disabled
1266
1266
ESP_INTR_FLAG_LOWMED | //< Low and medium prio interrupts. These can be handled in C.
1267
1267
ESP_INTR_FLAG_SHARED ; //< Reduce resource requirements, Share interrupts
1268
-
1268
+
1269
1269
if (i2c -> num ) {
1270
1270
ret = esp_intr_alloc_intrstatus (ETS_I2C_EXT1_INTR_SOURCE , flags , (uint32_t )& i2c -> dev -> int_status .val , interruptsEnabled , & i2c_isr_handler_default ,i2c , & i2c -> intr_handle );
1271
1271
} else {
@@ -1296,9 +1296,9 @@ i2c_err_t i2cProcQueue(i2c_t * i2c, uint32_t *readCount, uint16_t timeOutMillis)
1296
1296
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_DEBUG
1297
1297
portTickType tBefore = xTaskGetTickCount ();
1298
1298
#endif
1299
-
1299
+
1300
1300
// wait for ISR to complete the transfer, or until timeOut in case of bus fault, hardware problem
1301
-
1301
+
1302
1302
uint32_t eBits = xEventGroupWaitBits (i2c -> i2c_event ,EVENT_DONE ,pdFALSE ,pdTRUE ,ticksTimeOut );
1303
1303
1304
1304
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_DEBUG
@@ -1684,7 +1684,7 @@ i2c_err_t i2cSetFrequency(i2c_t * i2c, uint32_t clk_speed)
1684
1684
if (i2c == NULL ) {
1685
1685
return I2C_ERROR_DEV ;
1686
1686
}
1687
- uint32_t apb = getApbFrequency ();
1687
+ uint32_t apb = getApbFrequency ();
1688
1688
uint32_t period = (apb /clk_speed ) / 2 ;
1689
1689
1690
1690
if ((apb /8192 > clk_speed )|| (apb /MIN_I2C_CLKS < clk_speed )){ //out of bounds
@@ -1701,7 +1701,7 @@ i2c_err_t i2cSetFrequency(i2c_t * i2c, uint32_t clk_speed)
1701
1701
}
1702
1702
#ifdef ENABLE_I2C_DEBUG_BUFFER
1703
1703
log_v ("freq=%dHz" ,clk_speed );
1704
- #endif
1704
+ #endif
1705
1705
uint32_t halfPeriod = period /2 ;
1706
1706
uint32_t quarterPeriod = period /4 ;
1707
1707
@@ -1718,8 +1718,8 @@ i2c_err_t i2cSetFrequency(i2c_t * i2c, uint32_t clk_speed)
1718
1718
#ifdef ENABLE_I2C_DEBUG_BUFFER
1719
1719
log_v ("cpu Freq=%dMhz, i2c Freq=%dHz" ,getCpuFrequencyMhz (),clk_speed );
1720
1720
#endif
1721
- uint32_t fifo_delta = (INTERRUPT_CYCLE_OVERHEAD /((getCpuFrequencyMhz ()* 1000000 / clk_speed )* 10 ))+ 1 ;
1722
- if (fifo_delta > 24 ) fifo_delta = 24 ;
1721
+ uint32_t fifo_delta = (INTERRUPT_CYCLE_OVERHEAD /((getCpuFrequencyMhz ()* 1000000 / clk_speed )* 10 ))+ 1 ;
1722
+ if (fifo_delta > 24 ) fifo_delta = 24 ;
1723
1723
f .rx_fifo_full_thrhd = 32 - fifo_delta ;
1724
1724
f .tx_fifo_empty_thrhd = fifo_delta ;
1725
1725
i2c -> dev -> fifo_conf .val = f .val ; // set thresholds
@@ -1771,9 +1771,9 @@ uint32_t i2cDebug(i2c_t * i2c, uint32_t setBits, uint32_t resetBits){
1771
1771
return i2c -> debugFlags ;
1772
1772
}
1773
1773
return 0 ;
1774
-
1774
+
1775
1775
}
1776
-
1776
+
1777
1777
uint32_t i2cGetStatus (i2c_t * i2c ){
1778
1778
if (i2c != NULL ){
1779
1779
return i2c -> dev -> status_reg .val ;
@@ -1816,7 +1816,8 @@ i2c_t * i2cInit(uint8_t i2c_num, int8_t sda, int8_t scl, uint32_t clk_speed){
1816
1816
i2c_driver_delete ((i2c_port_t )i2c_num );
1817
1817
}
1818
1818
1819
- i2c_config_t conf = {0 };
1819
+ i2c_config_t conf ;
1820
+ memset (& conf , 0 , sizeof (i2c_config_t ));
1820
1821
conf .mode = I2C_MODE_MASTER ;
1821
1822
conf .scl_io_num = (gpio_num_t )scl ;
1822
1823
conf .sda_io_num = (gpio_num_t )sda ;
@@ -1917,7 +1918,7 @@ i2c_err_t i2cDetachSDA(i2c_t * i2c, int8_t sda){
1917
1918
need to add multi-thread capability, use dq.queueEvent as the group marker. When multiple threads
1918
1919
transactions are present in the same queue, and an error occurs, abort all succeeding unserviced transactions
1919
1920
with the same dq.queueEvent value. Succeeding unserviced transactions with different dq.queueEvent values
1920
- can be re-queued and processed independently.
1921
- 30JUL18 complete data only queue elements, this will allow transfers to use multiple data blocks,
1921
+ can be re-queued and processed independently.
1922
+ 30JUL18 complete data only queue elements, this will allow transfers to use multiple data blocks,
1922
1923
*/
1923
1924
0 commit comments