64
64
#if defined(CONFIG_ESPRESSIF_ESP32H2 ) || defined(CONFIG_ESPRESSIF_ESP32C6 )
65
65
# include "soc/pcr_reg.h"
66
66
#endif
67
+ #if defined(CONFIG_ESPRESSIF_LP_I2C0 )
68
+ # include "driver/rtc_io.h"
69
+ # include "hal/rtc_io_hal.h"
70
+ # include "io_mux.h"
71
+ # include "lp_core_i2c.h"
72
+ # include "soc/i2c_struct.h"
73
+ #endif
67
74
68
75
/****************************************************************************
69
76
* Pre-processor Definitions
70
77
****************************************************************************/
71
78
79
+ #if defined(CONFIG_ESPRESSIF_LP_I2C0 )
80
+ # define ESP_LP_I2C0_ID LP_I2C_NUM_0
81
+ #else
82
+ # define ESP_LP_I2C0_ID I2C_NUM_MAX
83
+ #endif
84
+
72
85
#ifdef CONFIG_ESPRESSIF_ESP32H2
73
86
# define SYSTEM_I2C_EXT0_CLK_EN PCR_I2C0_CLK_EN
74
87
# define SYSTEM_I2C_EXT0_RST PCR_I2C0_RST_EN
@@ -251,6 +264,8 @@ struct esp_i2c_priv_s
251
264
252
265
bool ready_read ; /* If I2C is ready for receiving data */
253
266
267
+ soc_module_clk_t clk_src ; /* Clock source */
268
+
254
269
uint32_t clk_freq ; /* Current I2C Clock frequency */
255
270
256
271
/* I2C trace support */
@@ -373,6 +388,7 @@ static struct esp_i2c_priv_s esp_i2c0_priv =
373
388
#endif
374
389
.error = 0 ,
375
390
.ready_read = false,
391
+ .clk_src = I2C_CLK_SRC_DEFAULT ,
376
392
.clk_freq = 0 ,
377
393
.ctx = & i2c0_ctx
378
394
};
@@ -424,11 +440,63 @@ static struct esp_i2c_priv_s esp_i2c1_priv =
424
440
#endif
425
441
.error = 0 ,
426
442
.ready_read = false,
443
+ .clk_src = I2C_CLK_SRC_DEFAULT ,
427
444
.clk_freq = 0 ,
428
445
.ctx = & i2c1_ctx
429
446
};
430
447
#endif /* CONFIG_ESPRESSIF_I2C1 */
431
448
449
+ #ifdef CONFIG_ESPRESSIF_LP_I2C0
450
+ i2c_hal_context_t lp_i2c0_ctx =
451
+ {
452
+ 0
453
+ };
454
+
455
+ /* I2C device structure */
456
+
457
+ static const struct esp_i2c_config_s esp_lp_i2c0_config =
458
+ {
459
+ .clk_freq = I2C_CLK_FREQ_DEF ,
460
+ .scl_pin = 7 ,
461
+ .sda_pin = 6 ,
462
+ #ifndef CONFIG_I2C_POLLED
463
+ .periph = ETS_LP_I2C_INTR_SOURCE ,
464
+ .irq = ESP_IRQ_LP_I2C ,
465
+ #endif
466
+ .clk_bit = SYSTEM_I2C_EXT0_CLK_EN ,
467
+ .rst_bit = SYSTEM_I2C_EXT0_RST ,
468
+ .scl_insig = 0 ,
469
+ .scl_outsig = 0 ,
470
+ .sda_insig = 0 ,
471
+ .sda_outsig = 0
472
+ };
473
+
474
+ static struct esp_i2c_priv_s esp_lp_i2c0_priv =
475
+ {
476
+ .ops = & esp_i2c_ops ,
477
+ .id = LP_I2C_NUM_0 ,
478
+ .module = PERIPH_LP_I2C0_MODULE ,
479
+ .config = & esp_lp_i2c0_config ,
480
+ .refs = 0 ,
481
+ .lock = NXMUTEX_INITIALIZER ,
482
+ #ifndef CONFIG_I2C_POLLED
483
+ .sem_isr = SEM_INITIALIZER (0 ),
484
+ #endif
485
+ .i2cstate = I2CSTATE_IDLE ,
486
+ .msgv = NULL ,
487
+ .msgid = 0 ,
488
+ .bytes = 0 ,
489
+ #ifndef CONFIG_I2C_POLLED
490
+ .cpuint = - ENOMEM ,
491
+ #endif
492
+ .error = 0 ,
493
+ .ready_read = false,
494
+ .clk_src = LP_I2C_SCLK_DEFAULT ,
495
+ .clk_freq = 0 ,
496
+ .ctx = & lp_i2c0_ctx
497
+ };
498
+ #endif /* CONFIG_ESPRESSIF_LP_I2C0 */
499
+
432
500
/* Trace events strings */
433
501
434
502
#ifdef CONFIG_I2C_TRACE
@@ -718,25 +786,64 @@ static void esp_i2c_sendstop(struct esp_i2c_priv_s *priv)
718
786
static void esp_i2c_init_clock (struct esp_i2c_priv_s * priv ,
719
787
uint32_t bus_freq )
720
788
{
721
- uint32_t src_clk_frequency ;
789
+ uint32_t src_clk_frequency = 0 ;
722
790
723
791
if (bus_freq == priv -> clk_freq )
724
792
{
725
793
return ;
726
794
}
727
795
728
- i2c_clock_source_t src_clk = I2C_CLK_SRC_DEFAULT ;
729
- esp_clk_tree_src_get_freq_hz ((soc_module_clk_t )src_clk ,
730
- ESP_CLK_TREE_SRC_FREQ_PRECISION_CACHED ,
796
+ esp_clk_tree_src_get_freq_hz (priv -> clk_src ,
797
+ ESP_CLK_TREE_SRC_FREQ_PRECISION_APPROX ,
731
798
& src_clk_frequency );
732
799
i2c_hal_set_bus_timing (priv -> ctx , priv -> config -> clk_freq ,
733
- src_clk , src_clk_frequency );
800
+ priv -> clk_src , src_clk_frequency );
734
801
i2c_hal_master_set_scl_timeout_val (priv -> ctx , SCL_TIMEOUT_VAL_US ,
735
802
src_clk_frequency );
803
+
804
+ #ifdef CONFIG_ESPRESSIF_LPI2C
805
+ PERIPH_RCC_ATOMIC ()
806
+ {
807
+ lp_i2c_ll_set_source_clk (priv -> ctx -> dev , priv -> clk_src );
808
+ }
809
+ #endif
810
+
736
811
i2c_ll_update (priv -> ctx -> dev );
737
812
priv -> clk_freq = bus_freq ;
738
813
}
739
814
815
+ #ifdef CONFIG_ESPRESSIF_LPI2C
816
+ /****************************************************************************
817
+ * Name: esp_lp_i2c_config_io
818
+ *
819
+ * Description:
820
+ * Configures LP I2C pin.
821
+ *
822
+ * Parameters:
823
+ * priv - Pointer to the private driver struct.
824
+ * pin - Pin number to configure.
825
+ *
826
+ * Return Value:
827
+ * None.
828
+ *
829
+ ****************************************************************************/
830
+
831
+ static void esp_lp_i2c_config_io (const struct esp_i2c_priv_s * priv ,
832
+ int8_t pin )
833
+ {
834
+ int lp_pin = rtc_io_num_map [pin ];
835
+
836
+ DEBUGASSERT (lp_pin != -1 );
837
+
838
+ rtc_gpio_set_level (pin , 1 );
839
+ rtc_gpio_init (pin );
840
+
841
+ rtc_gpio_set_direction (pin , RTC_GPIO_MODE_INPUT_OUTPUT_OD );
842
+ rtc_gpio_pulldown_dis (pin );
843
+ rtc_gpio_pullup_en (pin );
844
+ }
845
+ #endif /* CONFIG_ESPRESSIF_LPI2C */
846
+
740
847
/****************************************************************************
741
848
* Name: esp_i2c_init
742
849
*
@@ -752,22 +859,53 @@ static void esp_i2c_init(struct esp_i2c_priv_s *priv)
752
859
{
753
860
const struct esp_i2c_config_s * config = priv -> config ;
754
861
755
- /* Configure GPIO signals for I2C SCL and SDA pins */
862
+ if (priv -> id < ESP_LP_I2C0_ID )
863
+ {
864
+ /* Configure GPIO signals for I2C SCL and SDA pins */
865
+
866
+ esp_gpiowrite (config -> scl_pin , 1 );
867
+ esp_gpiowrite (config -> sda_pin , 1 );
756
868
757
- esp_gpiowrite (config -> scl_pin , 1 );
758
- esp_gpiowrite (config -> sda_pin , 1 );
869
+ esp_configgpio (config -> scl_pin , INPUT_PULLUP | OUTPUT_OPEN_DRAIN );
870
+ esp_gpio_matrix_out (config -> scl_pin , config -> scl_outsig , 0 , 0 );
871
+ esp_gpio_matrix_in (config -> scl_pin , config -> scl_insig , 0 );
759
872
760
- esp_configgpio (config -> scl_pin , INPUT_PULLUP | OUTPUT_OPEN_DRAIN );
761
- esp_gpio_matrix_out (config -> scl_pin , config -> scl_outsig , 0 , 0 );
762
- esp_gpio_matrix_in (config -> scl_pin , config -> scl_insig , 0 );
873
+ esp_configgpio (config -> sda_pin , INPUT_PULLUP | OUTPUT_OPEN_DRAIN );
874
+ esp_gpio_matrix_out (config -> sda_pin , config -> sda_outsig , 0 , 0 );
875
+ esp_gpio_matrix_in (config -> sda_pin , config -> sda_insig , 0 );
763
876
764
- esp_configgpio (config -> sda_pin , INPUT_PULLUP | OUTPUT_OPEN_DRAIN );
765
- esp_gpio_matrix_out (config -> sda_pin , config -> sda_outsig , 0 , 0 );
766
- esp_gpio_matrix_in (config -> sda_pin , config -> sda_insig , 0 );
877
+ /* Enable I2C hardware */
767
878
768
- /* Enable I2C hardware */
879
+ periph_module_enable (priv -> module );
880
+ }
881
+ #ifdef CONFIG_ESPRESSIF_LPI2C
882
+ else
883
+ {
884
+ PERIPH_RCC_ATOMIC ()
885
+ {
886
+ /* Enable LP I2C bus clock */
887
+
888
+ lp_i2c_ll_enable_bus_clock (priv -> id - LP_I2C_NUM_0 , true);
889
+
890
+ /* Reset LP I2C register */
891
+
892
+ lp_i2c_ll_reset_register (priv -> id - LP_I2C_NUM_0 );
893
+ }
894
+
895
+ esp_lp_i2c_config_io (priv , config -> scl_pin );
896
+ esp_lp_i2c_config_io (priv , config -> sda_pin );
897
+ const i2c_signal_conn_t * p_i2c_pin = & i2c_periph_signal [priv -> id ];
898
+ #if !SOC_LP_GPIO_MATRIX_SUPPORTED
899
+ rtc_gpio_iomux_func_sel (config -> sda_pin , p_i2c_pin -> iomux_func );
900
+ rtc_gpio_iomux_func_sel (config -> scl_pin , p_i2c_pin -> iomux_func );
901
+ #else
902
+ /* ToDo: Add LP I2C for LP GPIO Matrix
903
+ * supported devices (e.g ESP32-P4)
904
+ */
769
905
770
- periph_module_enable (priv -> module );
906
+ #endif /* SOC_LP_GPIO_MATRIX_SUPPORTED */
907
+ }
908
+ #endif
771
909
772
910
i2c_hal_init (priv -> ctx , priv -> id );
773
911
@@ -783,6 +921,17 @@ static void esp_i2c_init(struct esp_i2c_priv_s *priv)
783
921
784
922
i2c_ll_master_set_filter (priv -> ctx -> dev , I2C_FILTER_CYC_NUM_DEF );
785
923
924
+ #ifdef CONFIG_ESPRESSIF_LPI2C
925
+ /* Enable internal open-drain mode for I2C IO lines */
926
+
927
+ priv -> ctx -> dev -> ctr .sda_force_out = 0 ;
928
+ priv -> ctx -> dev -> ctr .scl_force_out = 0 ;
929
+
930
+ /* Configure the I2C master to send a NACK when the Rx FIFO count is full */
931
+
932
+ i2c_ll_master_rx_full_ack_level (priv -> ctx -> dev , 1 );
933
+ #endif
934
+
786
935
/* Initialize I2C bus clock */
787
936
788
937
esp_i2c_init_clock (priv , config -> clk_freq );
@@ -1531,6 +1680,11 @@ struct i2c_master_s *esp_i2cbus_initialize(int port)
1531
1680
case ESPRESSIF_I2C1 :
1532
1681
priv = & esp_i2c1_priv ;
1533
1682
break ;
1683
+ #endif
1684
+ #ifdef CONFIG_ESPRESSIF_LP_I2C0
1685
+ case ESPRESSIF_LP_I2C0 :
1686
+ priv = & esp_lp_i2c0_priv ;
1687
+ break ;
1534
1688
#endif
1535
1689
default :
1536
1690
return NULL ;
0 commit comments