@@ -338,10 +338,13 @@ esp_err_t i2cSlaveInit(uint8_t num, int sda, int scl, uint16_t slaveID, uint32_t
338
338
goto fail ;
339
339
}
340
340
}
341
-
341
+
342
342
i2c_ll_txfifo_rst (i2c -> dev );
343
343
i2c_ll_rxfifo_rst (i2c -> dev );
344
344
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
345
348
i2c_ll_set_stretch (i2c -> dev , 0x3FF );
346
349
i2c_ll_update (i2c -> dev );
347
350
I2C_SLAVE_MUTEX_UNLOCK ();
@@ -683,24 +686,45 @@ static void i2c_slave_isr_handler(void* arg)
683
686
uint32_t activeInt = i2c_ll_get_intsts_mask (i2c -> dev );
684
687
i2c_ll_clr_intsts_mask (i2c -> dev , activeInt );
685
688
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
687
690
688
691
if (activeInt & I2C_RXFIFO_WM_INT_ENA ){ // RX FiFo Full
689
692
pxHigherPriorityTaskWoken |= i2c_slave_handle_rx_fifo_full (i2c , rx_fifo_len );
690
693
i2c_ll_slave_enable_rx_it (i2c -> dev );//is this necessary?
691
694
}
692
695
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
695
699
pxHigherPriorityTaskWoken |= i2c_slave_handle_rx_fifo_full (i2c , rx_fifo_len );
696
700
}
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);
699
705
i2c_slave_queue_event_t event ;
700
706
event .event = I2C_SLAVE_EVT_RX ;
701
- event .stop = ! slave_rw ;
707
+ event .stop = false ;
702
708
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
704
728
//Zero RX count
705
729
i2c -> rx_data_count = 0 ;
706
730
}
@@ -774,9 +798,9 @@ static size_t i2c_slave_read_rx(i2c_slave_struct_t * i2c, uint8_t * data, size_t
774
798
}
775
799
return (data )?len :0 ;
776
800
#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 ,
780
804
available = 0 ;
781
805
uint8_t * rx_data = NULL ;
782
806
@@ -813,7 +837,7 @@ static void i2c_slave_task(void *pv_args)
813
837
uint8_t * data = NULL ;
814
838
for (;;){
815
839
if (xQueueReceive (i2c -> event_queue , & event , portMAX_DELAY ) == pdTRUE ){
816
- // Write
840
+ // Master Write to slave
817
841
if (event .event == I2C_SLAVE_EVT_RX ){
818
842
len = event .param ;
819
843
stop = event .stop ;
@@ -828,7 +852,7 @@ static void i2c_slave_task(void *pv_args)
828
852
}
829
853
free (data );
830
854
831
- // Read
855
+ // Master Read from slave
832
856
} else if (event .event == I2C_SLAVE_EVT_TX ){
833
857
if (i2c -> request_callback ){
834
858
i2c -> request_callback (i2c -> num , i2c -> arg );
0 commit comments