8000 support restart condition except ESP32 · strv/arduino-esp32@5d3b504 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5d3b504

Browse files
committed
support restart condition except ESP32
1 parent 44da992 commit 5d3b504

File tree

1 file changed

+37
-13
lines changed

1 file changed

+37
-13
lines changed

cores/esp32/esp32-hal-i2c-slave.c

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,13 @@ esp_err_t i2cSlaveInit(uint8_t num, int sda, int scl, uint16_t slaveID, uint32_t
338338
goto fail;
339339
}
340340
}
341-
341+
342342
i2c_ll_txfifo_rst(i2c->dev);
343343
i2c_ll_rxfifo_rst(i2c->dev);
344344
i2c_ll_slave_enable_rx_it(i2c->dev);
345+
#ifndef CONFIG_IDF_TARGET_ESP32
346+
i2c_ll_enable_intr_mask(i2c->dev, I2C_DET_START_INT_ENA);
347+
#endif
345348
i2c_ll_set_stretch(i2c->dev, 0x3FF);
346349
i2c_ll_update(i2c->dev);
347350
I2C_SLAVE_MUTEX_UNLOCK();
@@ -683,24 +686,45 @@ static void i2c_slave_isr_handler(void* arg)
683686
uint32_t activeInt = i2c_ll_get_intsts_mask(i2c->dev);
684687
i2c_ll_clr_intsts_mask(i2c->dev, activeInt);
685688
uint8_t rx_fifo_len = i2c_ll_get_rxfifo_cnt(i2c->dev);
686-
bool slave_rw = i2c_ll_slave_rw(i2c->dev);
689+
bool slave_rw = i2c_ll_slave_rw(i2c->dev); // ture : master read from slave
687690

688691
if(activeInt & I2C_RXFIFO_WM_INT_ENA){ // RX FiFo Full
689692
pxHigherPriorityTaskWoken |= i2c_slave_handle_rx_fifo_full(i2c, rx_fifo_len);
690693
i2c_ll_slave_enable_rx_it(i2c->dev);//is this necessary?
691694
}
692695

693-
if(activeInt & I2C_TRANS_COMPLETE_INT_ENA){ // STOP
694-
if(rx_fifo_len){ //READ RX FIFO
696+
#ifndef CONFIG_IDF_TARGET_ESP32
697+
if(activeInt & I2C_DET_START_INT_ENA){ // detect START condition
698+
if(rx_fifo_len){ // Move data from hardware fifo to ring beffer
695699
pxHigherPriorityTaskWoken |= i2c_slave_handle_rx_fifo_full(i2c, rx_fifo_len);
696700
}
697-
if(i2c->rx_data_count){ //WRITE or RepeatedStart
698-
//SEND RX Event
701+
if(i2c->rx_data_count){
702+
// receieved some with START = restarted
703+
// Process received data
704+
// log_i("RESTART %d %08X %d", slave_rw, activeInt, rx_fifo_len);
699705
i2c_slave_queue_event_t event;
700706
event.event = I2C_SLAVE_EVT_RX;
701-
event.stop = !slave_rw;
707+
event.stop = false;
702708
event.param = i2c->rx_data_count;
703-
pxHigherPriorityTaskWoken |= i2c_slave_send_event(i2c, &event);
709+
pxHigherPriorityTaskWoken |= i2c_slave_send_event(i2c, &event); // create RX event
710+
//Zero RX count
711+
i2c->rx_data_count = 0;
712+
}
713+
}
714+
#endif
715+
716+
if(activeInt & I2C_TRANS_COMPLETE_INT_ENA){ // detect STOP condition
717+
//log_i("STOP %d %02X %d", slave_rw, activeInt, rx_fifo_len);
718+
if(rx_fifo_len){ // Move data from hardware fifo to ring beffer
719+
pxHigherPriorityTaskWoken |= i2c_slave_handle_rx_fifo_full(i2c, rx_fifo_len);
720+
}
721+
if(i2c->rx_data_count){ // If the hardware fifo is not empty with STOP sequence
722+
//log_i("STOP %d %08X %d", slave_rw, activeInt, rx_fifo_len);
723+
i2c_slave_queue_event_t event;
724+
event.event = I2C_SLAVE_EVT_RX;
725+
event.stop = true;
726+
event.param = i2c->rx_data_count;
727+
pxHigherPriorityTaskWoken |= i2c_slave_send_event(i2c, &event); // create RX event
704728
//Zero RX count
705729
i2c->rx_data_count = 0;
706730
}
@@ -774,9 +798,9 @@ static size_t i2c_slave_read_rx(i2c_slave_struct_t * i2c, uint8_t * data, size_t
774798
}
775799
return (data)?len:0;
776800
#else
777-
size_t dlen = 0,
778-
to_read = len,
779-
so_far = 0,
801+
size_t dlen = 0,
802+
to_read = len,
803+
so_far = 0,
780804
available = 0;
781805
uint8_t * rx_data = NULL;
782806

@@ -813,7 +837,7 @@ static void i2c_slave_task(void *pv_args)
813837
uint8_t * data = NULL;
814838
for(;;){
815839
if(xQueueReceive(i2c->event_queue, &event, portMAX_DELAY) == pdTRUE){
816-
// Write
840+
// Master Write to slave
817841
if(event.event == I2C_SLAVE_EVT_RX){
818842
len = event.param;
819843
stop = event.stop;
@@ -828,7 +852,7 @@ static void i2c_slave_task(void *pv_args)
828852
}
829853
free(data);
830854

831-
// Read
855+
// Master Read from slave
832856
} else if(event.event == I2C_SLAVE_EVT_TX){
833857
if(i2c->request_callback){
834858
i2c->request_callback(i2c->num, i2c->arg);

0 commit comments

Comments
 (0)
0