Skip to content

Commit c077d75

Browse files
committed
zero struct with memset
1 parent c26b634 commit c077d75

File tree

1 file changed

+50
-49
lines changed

1 file changed

+50
-49
lines changed

cores/esp32/esp32-hal-i2c.c

+50-49
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#include "esp32/rom/ets_sys.h"
3232
#elif CONFIG_IDF_TARGET_ESP32S2
3333
#include "esp32s2/rom/ets_sys.h"
34-
#else
34+
#else
3535
#error Target CONFIG_IDF_TARGET is not supported
3636
#endif
3737
#else // ESP32 Before IDF 4.0
@@ -110,7 +110,7 @@ typedef union {
110110
};
111111
uint32_t val;
112112
} I2C_FIFO_ST_t;
113-
113+
114114
// end from tools/sdk/include/soc/soc/i2c_struct.h
115115

116116
// sync between dispatch(i2cProcQueue) and worker(i2c_isr_handler_default)
@@ -353,7 +353,7 @@ static void i2cDumpI2c(i2c_t * i2c)
353353
}
354354
#endif
355355

356-
#if (ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO)
356+
#if (ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO)
357357
static void i2cDumpInts(uint8_t num)
358358
{
359359
#if defined (ENABLE_I2C_DEBUG_BUFFER)
@@ -393,10 +393,10 @@ static void ARDUINO_ISR_ATTR i2cDumpStatus(i2c_t * i2c){
393393
};
394394
uint32_t val;
395395
} status_reg;
396-
396+
397397
status_reg sr;
398398
sr.val= i2c->dev->status_reg.val;
399-
399+
400400
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);
401401
}
402402
#endif
@@ -449,17 +449,17 @@ if(i != fifoPos){// actual data
449449
static void ARDUINO_ISR_ATTR i2cTriggerDumps(i2c_t * i2c, uint8_t trigger, const char locus[]){
450450
#if (ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO)&&(defined ENABLE_I2C_DEBUG_BUFFER)
451451
if( trigger ){
452-
log_i("%s",locus);
452+
log_i("%s",locus);
453453
if(trigger & 1) i2cDumpI2c(i2c);
454454
if(trigger & 2) i2cDumpInts(i2c->num);
455455
if(trigger & 4) i2cDumpCmdQueue(i2c);
456456
if(trigger & 8) i2cDumpStatus(i2c);
457457
if(trigger & 16) i2cDumpFifo(i2c);
458458
}
459-
#endif
459+
#endif
460460
}
461461
// end of debug support routines
462-
462+
463463
/* Start of CPU Clock change Support
464464
*/
465465

@@ -470,29 +470,29 @@ static void i2cApbChangeCallback(void * arg, apb_change_ev_t ev_type, uint32_t o
470470
}
471471
uint32_t oldFreq=0;
472472
switch(ev_type){
473-
case APB_BEFORE_CHANGE :
473+
case APB_BEFORE_CHANGE :
474474
if(new_apb < 3000000) {// too slow
475475
log_e("apb speed %d too slow",new_apb);
476476
break;
477477
}
478478
I2C_MUTEX_LOCK(); // lock will spin until current transaction is completed
479479
break;
480480
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
482482
if(oldFreq>0) { // was configured with value
483483
oldFreq = old_apb / oldFreq;
484484
i2cSetFrequency(i2c,oldFreq);
485485
}
486486
I2C_MUTEX_UNLOCK();
487487
break;
488-
default :
488+
default :
489489
log_e("unk ev %u",ev_type);
490490
I2C_MUTEX_UNLOCK();
491491
}
492492
return;
493493
}
494494
/* End of CPU Clock change Support
495-
*/
495+
*/
496496
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)
497497
{
498498
I2C_COMMAND_t cmd;
@@ -504,7 +504,7 @@ static void ARDUINO_ISR_ATTR i2cSetCmd(i2c_t * i2c, uint8_t index, uint8_t op_co
504504
cmd.op_code = op_code;
505505
i2c->dev->command[index].val = cmd.val;
506506
}
507-
507+
508508

509509
static void ARDUINO_ISR_ATTR fillCmdQueue(i2c_t * i2c, bool INTS)
510510
{
@@ -521,7 +521,7 @@ static void ARDUINO_ISR_ATTR fillCmdQueue(i2c_t * i2c, bool INTS)
521521
bool ena_tx=false; // if we add a Write op, better enable TX_Fifo IRQ
522522

523523
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
525525
qp++;
526526
} else {
527527
needMoreCmds=true;
@@ -572,7 +572,7 @@ static void ARDUINO_ISR_ATTR fillCmdQueue(i2c_t * i2c, bool INTS)
572572
uint8_t blkSize=0; // max is 255
573573
while(( tdq->cmdBytesNeeded > tdq->ctrl.mode )&&(!done )) { // more bytes needed and room in cmd queue, leave room for END
574574
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;
576576
if(tdq->ctrl.mode==1) { //read mode
577577
i2cSetCmd(i2c, (cmdIdx)++, I2C_CMD_READ, blkSize,false,false,false); // read cmd, this can't be the last read.
578578
ena_rx=true; // need to enable rxFifo IRQ
@@ -637,8 +637,8 @@ static void ARDUINO_ISR_ATTR fillCmdQueue(i2c_t * i2c, bool INTS)
637637
if(cmdIdx==15) { //need continuation, even if STOP is in 14, it will not matter
638638
// cmd buffer is almost full, Add END as a continuation feature
639639
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;
642642
done = true;
643643
}
644644

@@ -731,9 +731,9 @@ static void ARDUINO_ISR_ATTR fillTxFifo(i2c_t * i2c)
731731
if( ! rxQueueEncountered ) {
732732
rxQueueEncountered = true;
733733
if(a > i2c->queuePos){
734-
i2c->queuePos = a;
734+
i2c->queuePos = a;
735735
}
736-
}
736+
}
737737
}
738738
#if (ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO) && (defined ENABLE_I2C_DEBUG_BUFFER)
739739

@@ -759,18 +759,18 @@ static void ARDUINO_ISR_ATTR fillTxFifo(i2c_t * i2c)
759759
static void ARDUINO_ISR_ATTR emptyRxFifo(i2c_t * i2c)
760760
{
761761
uint32_t d, cnt=0, moveCnt;
762-
762+
763763
moveCnt = i2c->dev->status_reg.rx_fifo_cnt;//no need to check the reg until this many are read
764764

765-
while((moveCnt > 0)&&(i2c->queuePos < i2c->queueCount)){ // data to move
765+
while((moveCnt > 0)&&(i2c->queuePos < i2c->queueCount)){ // data to move
766766

767767
I2C_DATA_QUEUE_t *tdq =&i2c->dq[i2c->queuePos]; //short cut
768768

769769
while((tdq->position >= tdq->length)&&( i2c->queuePos < i2c->queueCount)){ // find were to store
770770
i2c->queuePos++;
771771
tdq = &i2c->dq[i2c->queuePos];
772772
}
773-
773+
774774
if(i2c->queuePos >= i2c->queueCount){ // bad stuff, rx data but no place to put it!
775775
log_e("no Storage location for %d",moveCnt);
776776
// discard
@@ -781,7 +781,7 @@ static void ARDUINO_ISR_ATTR emptyRxFifo(i2c_t * i2c)
781781
}
782782
break;
783783
}
784-
784+
785785
if(tdq->ctrl.mode == 1){ // read command
786786
if(moveCnt > (tdq->length - tdq->position)) { //make sure they go in this dq
787787
// part of these reads go into the next dq
@@ -807,7 +807,7 @@ static void ARDUINO_ISR_ATTR emptyRxFifo(i2c_t * i2c)
807807

808808
moveCnt = i2c->dev->status_reg.rx_fifo_cnt; //any more out there?
809809
}
810-
810+
811811
#if (ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO)&& (defined ENABLE_I2C_DEBUG_BUFFER)
812812
// update Debug rxCount
813813
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)
866866
Only called after an error has occurred, so, most of the time this function is never used.
867867
This function obliterates the need to interrupt monitor each byte transferred, at high bitrates
868868
the byte interrupts were overwhelming the OS. Spurious Interrupts were being generated.
869-
it updates errorByteCnt, errorQueue.
869+
it updates errorByteCnt, errorQueue.
870870
*/
871871
uint16_t a=0; // start at top of DQ, count how many bytes added to tx fifo, and received from rx_fifo.
872872
int16_t bc = 0;
873873
I2C_DATA_QUEUE_t *tdq;
874-
i2c->errorByteCnt = 0;
874+
i2c->errorByteCnt = 0;
875875
while( a < i2c->queueCount){ // add up all bytes loaded into fifo's
876876
tdq = &i2c->dq[a];
877877
i2c->errorByteCnt += tdq->ctrl.addrSent;
878-
i2c->errorByteCnt += tdq->position;
878+
i2c->errorByteCnt += tdq->position;
879879
a++;
880880
}
881881
// 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)
900900
}
901901
}
902902
} else break;
903-
903+
904904
i2c->errorQueue++;
905-
}
906-
905+
}
906+
907907
i2c->errorByteCnt = bc;
908908
}
909909

@@ -936,7 +936,7 @@ static void ARDUINO_ISR_ATTR i2c_isr_handler_default(void* arg)
936936
intBuff[intPos[p_i2c->num]][2][p_i2c->num] = xTaskGetTickCountFromISR(); // when IRQ fired
937937

938938
#endif
939-
939+
940940
if (activeInt & I2C_TRANS_START_INT_ST_M) {
941941
if(p_i2c->stage==I2C_STARTUP) {
942942
p_i2c->stage=I2C_RUNNING;
@@ -985,7 +985,7 @@ static void ARDUINO_ISR_ATTR i2c_isr_handler_default(void* arg)
985985
// the Statemachine only has a 13.1ms max timout, some Devices >500ms
986986
p_i2c->dev->int_clr.time_out =1;
987987
activeInt &=~I2C_TIME_OUT_INT_ST;
988-
// since a timeout occurred, capture the rxFifo data
988+
// since a timeout occurred, capture the rxFifo data
989989
emptyRxFifo(p_i2c);
990990
p_i2c->dev->int_clr.rx_fifo_full=1;
991991
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)
11911191
i2c->dev->ctr.trans_start=0; // Pause Machine
11921192
i2c->dev->timeout.tout = 0xFFFFF; // max 13ms
11931193
i2c->dev->int_clr.val = 0x1FFF; // kill them All!
1194-
1194+
11951195
i2c->dev->ctr.ms_mode = 1; // master!
11961196
i2c->queuePos=0;
11971197
i2c->errorByteCnt=0;
@@ -1229,7 +1229,7 @@ i2c_err_t i2cProcQueue(i2c_t * i2c, uint32_t *readCount, uint16_t timeOutMillis)
12291229
// sometimes the emptyRx() actually moves 31 bytes
12301230
// it hasn't overflowed yet, I cannot tell if the new byte is added while
12311231
// emptyRX() is executing or before?
1232-
// let i2cSetFrequency() set thrhds
1232+
// let i2cSetFrequency() set thrhds
12331233
// f.rx_fifo_full_thrhd = 30; // 30 bytes before INT is issued
12341234
// f.tx_fifo_empty_thrhd = 0;
12351235
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)
12441244
// it should receive a TXFIFO_EMPTY immediately, even before it
12451245
// receives the TRANS_START
12461246

1247-
1247+
12481248
uint32_t interruptsEnabled =
12491249
I2C_ACK_ERR_INT_ENA | // (BIT(10)) Causes Fatal Error Exit
12501250
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)
12561256
I2C_RXFIFO_OVF_INT_ENA | //(BIT(2)) unhandled
12571257
I2C_TXFIFO_EMPTY_INT_ENA | // (BIT(1)) triggers fillTxFifo()
12581258
I2C_RXFIFO_FULL_INT_ENA; // (BIT(0)) trigger emptyRxFifo()
1259-
1259+
12601260
i2c->dev->int_ena.val = interruptsEnabled;
1261-
1261+
12621262
if(!i2c->intr_handle) { // create ISR for either peripheral
12631263
// log_i("create ISR %d",i2c->num);
12641264
uint32_t ret = 0;
12651265
uint32_t flags = ARDUINO_ISR_FLAG | //< ISR can be called if cache is disabled
12661266
ESP_INTR_FLAG_LOWMED | //< Low and medium prio interrupts. These can be handled in C.
12671267
ESP_INTR_FLAG_SHARED; //< Reduce resource requirements, Share interrupts
1268-
1268+
12691269
if(i2c->num) {
12701270
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);
12711271
} else {
@@ -1296,9 +1296,9 @@ i2c_err_t i2cProcQueue(i2c_t * i2c, uint32_t *readCount, uint16_t timeOutMillis)
12961296
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_DEBUG
12971297
portTickType tBefore=xTaskGetTickCount();
12981298
#endif
1299-
1299+
13001300
// wait for ISR to complete the transfer, or until timeOut in case of bus fault, hardware problem
1301-
1301+
13021302
uint32_t eBits = xEventGroupWaitBits(i2c->i2c_event,EVENT_DONE,pdFALSE,pdTRUE,ticksTimeOut);
13031303

13041304
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_DEBUG
@@ -1684,7 +1684,7 @@ i2c_err_t i2cSetFrequency(i2c_t * i2c, uint32_t clk_speed)
16841684
if(i2c == NULL) {
16851685
return I2C_ERROR_DEV;
16861686
}
1687-
uint32_t apb = getApbFrequency();
1687+
uint32_t apb = getApbFrequency();
16881688
uint32_t period = (apb/clk_speed) / 2;
16891689

16901690
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)
17011701
}
17021702
#ifdef ENABLE_I2C_DEBUG_BUFFER
17031703
log_v("freq=%dHz",clk_speed);
1704-
#endif
1704+
#endif
17051705
uint32_t halfPeriod = period/2;
17061706
uint32_t quarterPeriod = period/4;
17071707

@@ -1718,8 +1718,8 @@ i2c_err_t i2cSetFrequency(i2c_t * i2c, uint32_t clk_speed)
17181718
#ifdef ENABLE_I2C_DEBUG_BUFFER
17191719
log_v("cpu Freq=%dMhz, i2c Freq=%dHz",getCpuFrequencyMhz(),clk_speed);
17201720
#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;
17231723
f.rx_fifo_full_thrhd = 32 - fifo_delta;
17241724
f.tx_fifo_empty_thrhd = fifo_delta;
17251725
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){
17711771
return i2c->debugFlags;
17721772
}
17731773
return 0;
1774-
1774+
17751775
}
1776-
1776+
17771777
uint32_t i2cGetStatus(i2c_t * i2c){
17781778
if(i2c != NULL){
17791779
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){
18161816
i2c_driver_delete((i2c_port_t)i2c_num);
18171817
}
18181818

1819-
i2c_config_t conf = {0};
1819+
i2c_config_t conf;
1820+
memset(&conf, 0, sizeof(i2c_config_t));
18201821
conf.mode = I2C_MODE_MASTER;
18211822
conf.scl_io_num = (gpio_num_t)scl;
18221823
conf.sda_io_num = (gpio_num_t)sda;
@@ -1917,7 +1918,7 @@ i2c_err_t i2cDetachSDA(i2c_t * i2c, int8_t sda){
19171918
need to add multi-thread capability, use dq.queueEvent as the group marker. When multiple threads
19181919
transactions are present in the same queue, and an error occurs, abort all succeeding unserviced transactions
19191920
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,
19221923
*/
19231924

0 commit comments

Comments
 (0)