i2c_common.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include "sdkconfig.h"
  9. #include "esp_types.h"
  10. #if CONFIG_I2C_ENABLE_DEBUG_LOG
  11. // The local log level must be defined before including esp_log.h
  12. // Set the maximum log level for this source file
  13. #define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
  14. #endif
  15. #include "esp_log.h"
  16. #include "esp_check.h"
  17. #include "esp_pm.h"
  18. #include "freertos/FreeRTOS.h"
  19. #include "hal/i2c_hal.h"
  20. #include "hal/gpio_hal.h"
  21. #include "esp_private/periph_ctrl.h"
  22. #include "esp_rom_gpio.h"
  23. #include "i2c_private.h"
  24. #include "driver/gpio.h"
  25. #include "soc/clk_tree_defs.h"
  26. #include "soc/i2c_periph.h"
  27. #include "esp_clk_tree.h"
  28. #include "clk_ctrl_os.h"
  29. static const char *TAG = "i2c.common";
  30. typedef struct i2c_platform_t {
  31. _lock_t mutex; // platform level mutex lock.
  32. i2c_bus_handle_t buses[SOC_I2C_NUM]; // array of I2C bus instances.
  33. uint32_t count[SOC_I2C_NUM]; // reference count used to protect group install/uninstall.
  34. } i2c_platform_t;
  35. static i2c_platform_t s_i2c_platform = {}; // singleton platform
  36. esp_err_t i2c_acquire_bus_handle(i2c_port_num_t port_num, i2c_bus_handle_t *i2c_new_bus, i2c_bus_mode_t mode)
  37. {
  38. #if CONFIG_I2C_ENABLE_DEBUG_LOG
  39. esp_log_level_set(TAG, ESP_LOG_DEBUG);
  40. #endif
  41. bool new_bus = false;
  42. i2c_bus_t *bus = NULL;
  43. esp_err_t ret = ESP_OK;
  44. if (!s_i2c_platform.buses[port_num]) {
  45. new_bus = true;
  46. bus = heap_caps_calloc(1, sizeof(i2c_bus_t), I2C_MEM_ALLOC_CAPS);
  47. if (bus) {
  48. s_i2c_platform.buses[port_num] = bus;
  49. bus->port_num = port_num;
  50. bus->spinlock = (portMUX_TYPE)portMUX_INITIALIZER_UNLOCKED;
  51. bus->bus_mode = mode;
  52. // Enable the I2C module
  53. periph_module_enable(i2c_periph_signal[port_num].module);
  54. periph_module_reset(i2c_periph_signal[port_num].module);
  55. i2c_hal_init(&bus->hal, port_num);
  56. }
  57. } else {
  58. ESP_LOGE(TAG, "I2C bus id(%d) has already been acquired", port_num);
  59. bus = s_i2c_platform.buses[port_num];
  60. ret = ESP_ERR_INVALID_STATE;
  61. }
  62. if (bus) {
  63. s_i2c_platform.count[port_num]++;
  64. }
  65. if (new_bus) {
  66. ESP_LOGD(TAG, "new bus(%d) at %p", port_num, bus);
  67. }
  68. *i2c_new_bus = bus;
  69. return ret;
  70. }
  71. bool i2c_bus_occupied(i2c_port_num_t port_num)
  72. {
  73. bool bus_occupied = false;
  74. _lock_acquire(&s_i2c_platform.mutex);
  75. if (s_i2c_platform.buses[port_num]) {
  76. bus_occupied = true;
  77. }
  78. _lock_release(&s_i2c_platform.mutex);
  79. return bus_occupied;
  80. }
  81. uint8_t i2c_release_bus_handle(i2c_bus_handle_t i2c_bus)
  82. {
  83. int port_num = i2c_bus->port_num;
  84. i2c_clock_source_t clk_src = i2c_bus->clk_src;
  85. bool do_deinitialize = false;
  86. _lock_acquire(&s_i2c_platform.mutex);
  87. if (s_i2c_platform.buses[port_num]) {
  88. s_i2c_platform.count[port_num]--;
  89. if (s_i2c_platform.count[port_num] == 0) {
  90. do_deinitialize = true;
  91. s_i2c_platform.buses[port_num] = NULL;
  92. // Disable I2C module
  93. periph_module_disable(i2c_periph_signal[port_num].module);
  94. free(i2c_bus);
  95. }
  96. }
  97. _lock_release(&s_i2c_platform.mutex);
  98. switch (clk_src) {
  99. #if SOC_I2C_SUPPORT_RTC
  100. case I2C_CLK_SRC_RC_FAST:
  101. periph_rtc_dig_clk8m_disable();
  102. break;
  103. #endif // SOC_I2C_SUPPORT_RTC
  104. default:
  105. break;
  106. }
  107. if (do_deinitialize) {
  108. ESP_LOGD(TAG, "delete bus %d", port_num);
  109. }
  110. return s_i2c_platform.count[port_num];
  111. }
  112. esp_err_t i2c_select_periph_clock(i2c_bus_handle_t handle, i2c_clock_source_t clk_src)
  113. {
  114. esp_err_t ret = ESP_OK;
  115. ESP_RETURN_ON_FALSE(handle, ESP_ERR_INVALID_ARG, TAG, "I2C empty controller handle");
  116. uint32_t periph_src_clk_hz = 0;
  117. bool clock_selection_conflict = 0;
  118. portENTER_CRITICAL(&handle->spinlock);
  119. if (handle->clk_src == 0) {
  120. handle->clk_src = clk_src;
  121. } else {
  122. clock_selection_conflict = (handle->clk_src != clk_src);
  123. }
  124. portEXIT_CRITICAL(&handle->spinlock);
  125. ESP_RETURN_ON_FALSE(!clock_selection_conflict, ESP_ERR_INVALID_STATE, TAG,
  126. "group clock conflict, already is %d but attempt to %d", handle->clk_src, clk_src);
  127. // TODO: [clk_tree] to use a generic clock enable/disable or acquire/release function for all clock source
  128. #if SOC_I2C_SUPPORT_RTC
  129. if (clk_src == I2C_CLK_SRC_RC_FAST) {
  130. // RC_FAST clock is not enabled automatically on start up, we enable it here manually.
  131. // Note there's a ref count in the enable/disable function, we must call them in pair in the driver.
  132. periph_rtc_dig_clk8m_enable();
  133. }
  134. #endif // SOC_I2C_SUPPORT_RTC
  135. ESP_RETURN_ON_ERROR(esp_clk_tree_src_get_freq_hz(clk_src, ESP_CLK_TREE_SRC_FREQ_PRECISION_APPROX, &periph_src_clk_hz), TAG, "i2c get clock frequency error");
  136. handle->clk_src_freq_hz = periph_src_clk_hz;
  137. #if CONFIG_PM_ENABLE
  138. bool need_pm_lock = true;
  139. // to make the I2C work reliable, the source clock must stay alive and unchanged
  140. // driver will create different pm lock for that purpose, according to different clock source
  141. esp_pm_lock_type_t pm_lock_type = ESP_PM_NO_LIGHT_SLEEP;
  142. #if SOC_I2C_SUPPORT_RTC
  143. if (clk_src == I2C_CLK_SRC_RC_FAST) {
  144. // I2C use fifo, which connected to APB, so we cannot use I2C either when in light sleep.
  145. need_pm_lock = ESP_PM_NO_LIGHT_SLEEP;
  146. }
  147. #endif // SOC_I2C_SUPPORT_RTC
  148. #if SOC_I2C_SUPPORT_APB
  149. if (clk_src == I2C_CLK_SRC_APB) {
  150. // APB clock frequency can be changed during DFS
  151. pm_lock_type = ESP_PM_APB_FREQ_MAX;
  152. }
  153. #endif // SOC_I2C_SUPPORT_APB
  154. if (need_pm_lock) {
  155. sprintf(handle->pm_lock_name, "I2C_%d", handle->port_num); // e.g. PORT_0
  156. ret = esp_pm_lock_create(pm_lock_type, 0, handle->pm_lock_name, &handle->pm_lock);
  157. ESP_RETURN_ON_ERROR(ret, TAG, "create pm lock failed");
  158. }
  159. #endif // CONFIG_PM_ENABLE
  160. ESP_LOGD(TAG, "bus clock source frequency: %"PRIu32"hz", periph_src_clk_hz);
  161. return ret;
  162. }
  163. esp_err_t i2c_common_set_pins(i2c_bus_handle_t handle)
  164. {
  165. esp_err_t ret = ESP_OK;
  166. ESP_RETURN_ON_FALSE(handle, ESP_ERR_INVALID_ARG, TAG, "I2C empty controller handle");
  167. int port_id = handle->port_num;
  168. // SDA pin configurations
  169. gpio_config_t sda_conf = {
  170. .intr_type = GPIO_INTR_DISABLE,
  171. .mode = GPIO_MODE_INPUT_OUTPUT_OD,
  172. .pull_down_en = false,
  173. .pull_up_en = handle->pull_up_enable ? GPIO_PULLUP_ENABLE : GPIO_PULLUP_DISABLE,
  174. .pin_bit_mask = 1ULL << handle->sda_num,
  175. };
  176. #if CONFIG_IDF_TARGET_ESP32
  177. // on esp32, must enable internal pull-up
  178. sda_conf.pull_up_en = GPIO_PULLUP_ENABLE;
  179. #endif
  180. ESP_RETURN_ON_ERROR(gpio_config(&sda_conf), TAG, "config GPIO failed");
  181. ESP_RETURN_ON_ERROR(gpio_set_level(handle->sda_num, 1), TAG, "i2c sda pin set level failed");
  182. gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[handle->sda_num], PIN_FUNC_GPIO);
  183. esp_rom_gpio_connect_out_signal(handle->sda_num, i2c_periph_signal[port_id].sda_out_sig, 0, 0);
  184. esp_rom_gpio_connect_in_signal(handle->sda_num, i2c_periph_signal[port_id].sda_in_sig, 0);
  185. // SCL pin configurations
  186. gpio_config_t scl_conf = {
  187. .intr_type = GPIO_INTR_DISABLE,
  188. .mode = GPIO_MODE_INPUT_OUTPUT_OD,
  189. .pull_down_en = false,
  190. .pull_up_en = handle->pull_up_enable ? GPIO_PULLUP_ENABLE : GPIO_PULLUP_DISABLE,
  191. .pin_bit_mask = 1ULL << handle->scl_num,
  192. };
  193. #if CONFIG_IDF_TARGET_ESP32
  194. // on esp32, must enable internal pull-up
  195. scl_conf.pull_up_en = GPIO_PULLUP_ENABLE;
  196. #endif
  197. ESP_RETURN_ON_ERROR(gpio_config(&scl_conf), TAG, "config GPIO failed");
  198. ESP_RETURN_ON_ERROR(gpio_set_level(handle->scl_num, 1), TAG, "i2c scl pin set level failed");
  199. gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[handle->scl_num], PIN_FUNC_GPIO);
  200. esp_rom_gpio_connect_out_signal(handle->scl_num, i2c_periph_signal[port_id].scl_out_sig, 0, 0);
  201. esp_rom_gpio_connect_in_signal(handle->scl_num, i2c_periph_signal[port_id].scl_in_sig, 0);
  202. return ret;
  203. }