i2c_common.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. static esp_err_t s_i2c_bus_handle_aquire(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. I2C_RCC_ATOMIC() {
  54. i2c_ll_enable_bus_clock(bus->port_num, true);
  55. i2c_ll_reset_register(bus->port_num);
  56. }
  57. I2C_CLOCK_SRC_ATOMIC() {
  58. i2c_hal_init(&bus->hal, port_num);
  59. }
  60. }
  61. } else {
  62. ESP_LOGE(TAG, "I2C bus id(%d) has already been acquired", port_num);
  63. bus = s_i2c_platform.buses[port_num];
  64. ret = ESP_ERR_INVALID_STATE;
  65. }
  66. if (bus) {
  67. s_i2c_platform.count[port_num]++;
  68. }
  69. if (new_bus) {
  70. ESP_LOGD(TAG, "new bus(%d) at %p", port_num, bus);
  71. }
  72. *i2c_new_bus = bus;
  73. return ret;
  74. }
  75. static bool i2c_bus_occupied(i2c_port_num_t port_num)
  76. {
  77. return s_i2c_platform.buses[port_num] != NULL;
  78. }
  79. 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)
  80. {
  81. bool bus_occupied = false;
  82. bool bus_found = false;
  83. esp_err_t ret = ESP_OK;
  84. _lock_acquire(&s_i2c_platform.mutex);
  85. if (port_num == -1) {
  86. for (int i = 0; i < SOC_I2C_NUM; i++) {
  87. bus_occupied = i2c_bus_occupied(i);
  88. if (bus_occupied == false) {
  89. ret = s_i2c_bus_handle_aquire(i, i2c_new_bus, mode);
  90. if (ret != ESP_OK) {
  91. ESP_LOGE(TAG, "acquire bus failed");
  92. _lock_release(&s_i2c_platform.mutex);
  93. return ret;
  94. }
  95. bus_found = true;
  96. port_num = i;
  97. break;
  98. }
  99. }
  100. ESP_RETURN_ON_FALSE((bus_found == true), ESP_ERR_NOT_FOUND, TAG, "acquire bus failed, no free bus");
  101. } else {
  102. ret = s_i2c_bus_handle_aquire(port_num, i2c_new_bus, mode);
  103. if (ret != ESP_OK) {
  104. ESP_LOGE(TAG, "acquire bus failed");
  105. }
  106. }
  107. _lock_release(&s_i2c_platform.mutex);
  108. return ret;
  109. }
  110. esp_err_t i2c_release_bus_handle(i2c_bus_handle_t i2c_bus)
  111. {
  112. int port_num = i2c_bus->port_num;
  113. i2c_clock_source_t clk_src = i2c_bus->clk_src;
  114. bool do_deinitialize = false;
  115. _lock_acquire(&s_i2c_platform.mutex);
  116. if (s_i2c_platform.buses[port_num]) {
  117. s_i2c_platform.count[port_num]--;
  118. if (s_i2c_platform.count[port_num] == 0) {
  119. do_deinitialize = true;
  120. s_i2c_platform.buses[port_num] = NULL;
  121. if (i2c_bus->intr_handle) {
  122. ESP_RETURN_ON_ERROR(esp_intr_free(i2c_bus->intr_handle), TAG, "delete interrupt service failed");
  123. }
  124. if (i2c_bus->pm_lock) {
  125. ESP_RETURN_ON_ERROR(esp_pm_lock_delete(i2c_bus->pm_lock), TAG, "delete pm_lock failed");
  126. }
  127. // Disable I2C module
  128. I2C_RCC_ATOMIC() {
  129. i2c_ll_enable_bus_clock(port_num, false);
  130. }
  131. free(i2c_bus);
  132. }
  133. }
  134. _lock_release(&s_i2c_platform.mutex);
  135. switch (clk_src) {
  136. #if SOC_I2C_SUPPORT_RTC
  137. case I2C_CLK_SRC_RC_FAST:
  138. periph_rtc_dig_clk8m_disable();
  139. break;
  140. #endif // SOC_I2C_SUPPORT_RTC
  141. default:
  142. break;
  143. }
  144. if (do_deinitialize) {
  145. ESP_LOGD(TAG, "delete bus %d", port_num);
  146. }
  147. ESP_RETURN_ON_FALSE(s_i2c_platform.count[port_num] == 0, ESP_ERR_INVALID_STATE, TAG, "Bus not freed entirely");
  148. return ESP_OK;
  149. }
  150. esp_err_t i2c_select_periph_clock(i2c_bus_handle_t handle, i2c_clock_source_t clk_src)
  151. {
  152. esp_err_t ret = ESP_OK;
  153. ESP_RETURN_ON_FALSE(handle, ESP_ERR_INVALID_ARG, TAG, "I2C empty controller handle");
  154. uint32_t periph_src_clk_hz = 0;
  155. bool clock_selection_conflict = 0;
  156. portENTER_CRITICAL(&handle->spinlock);
  157. if (handle->clk_src == 0) {
  158. handle->clk_src = clk_src;
  159. } else {
  160. clock_selection_conflict = (handle->clk_src != clk_src);
  161. }
  162. portEXIT_CRITICAL(&handle->spinlock);
  163. ESP_RETURN_ON_FALSE(!clock_selection_conflict, ESP_ERR_INVALID_STATE, TAG,
  164. "group clock conflict, already is %d but attempt to %d", handle->clk_src, clk_src);
  165. // TODO: [clk_tree] to use a generic clock enable/disable or acquire/release function for all clock source
  166. #if SOC_I2C_SUPPORT_RTC
  167. if (clk_src == I2C_CLK_SRC_RC_FAST) {
  168. // RC_FAST clock is not enabled automatically on start up, we enable it here manually.
  169. // Note there's a ref count in the enable/disable function, we must call them in pair in the driver.
  170. periph_rtc_dig_clk8m_enable();
  171. }
  172. #endif // SOC_I2C_SUPPORT_RTC
  173. 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");
  174. handle->clk_src_freq_hz = periph_src_clk_hz;
  175. #if CONFIG_PM_ENABLE
  176. bool need_pm_lock = true;
  177. // to make the I2C work reliable, the source clock must stay alive and unchanged
  178. // driver will create different pm lock for that purpose, according to different clock source
  179. esp_pm_lock_type_t pm_lock_type = ESP_PM_NO_LIGHT_SLEEP;
  180. #if SOC_I2C_SUPPORT_RTC
  181. if (clk_src == I2C_CLK_SRC_RC_FAST) {
  182. // I2C use fifo, which connected to APB, so we cannot use I2C either when in light sleep.
  183. need_pm_lock = ESP_PM_NO_LIGHT_SLEEP;
  184. }
  185. #endif // SOC_I2C_SUPPORT_RTC
  186. #if SOC_I2C_SUPPORT_APB
  187. if (clk_src == I2C_CLK_SRC_APB) {
  188. // APB clock frequency can be changed during DFS
  189. pm_lock_type = ESP_PM_APB_FREQ_MAX;
  190. }
  191. #endif // SOC_I2C_SUPPORT_APB
  192. if (need_pm_lock) {
  193. sprintf(handle->pm_lock_name, "I2C_%d", handle->port_num); // e.g. PORT_0
  194. ret = esp_pm_lock_create(pm_lock_type, 0, handle->pm_lock_name, &handle->pm_lock);
  195. ESP_RETURN_ON_ERROR(ret, TAG, "create pm lock failed");
  196. }
  197. #endif // CONFIG_PM_ENABLE
  198. ESP_LOGD(TAG, "bus clock source frequency: %"PRIu32"hz", periph_src_clk_hz);
  199. return ret;
  200. }
  201. esp_err_t i2c_common_set_pins(i2c_bus_handle_t handle)
  202. {
  203. esp_err_t ret = ESP_OK;
  204. ESP_RETURN_ON_FALSE(handle, ESP_ERR_INVALID_ARG, TAG, "I2C empty controller handle");
  205. int port_id = handle->port_num;
  206. // SDA pin configurations
  207. gpio_config_t sda_conf = {
  208. .intr_type = GPIO_INTR_DISABLE,
  209. .mode = GPIO_MODE_INPUT_OUTPUT_OD,
  210. .pull_down_en = false,
  211. .pull_up_en = handle->pull_up_enable ? GPIO_PULLUP_ENABLE : GPIO_PULLUP_DISABLE,
  212. .pin_bit_mask = 1ULL << handle->sda_num,
  213. };
  214. ESP_RETURN_ON_ERROR(gpio_set_level(handle->sda_num, 1), TAG, "i2c sda pin set level failed");
  215. ESP_RETURN_ON_ERROR(gpio_config(&sda_conf), TAG, "config GPIO failed");
  216. gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[handle->sda_num], PIN_FUNC_GPIO);
  217. esp_rom_gpio_connect_out_signal(handle->sda_num, i2c_periph_signal[port_id].sda_out_sig, 0, 0);
  218. esp_rom_gpio_connect_in_signal(handle->sda_num, i2c_periph_signal[port_id].sda_in_sig, 0);
  219. // SCL pin configurations
  220. gpio_config_t scl_conf = {
  221. .intr_type = GPIO_INTR_DISABLE,
  222. .mode = GPIO_MODE_INPUT_OUTPUT_OD,
  223. .pull_down_en = false,
  224. .pull_up_en = handle->pull_up_enable ? GPIO_PULLUP_ENABLE : GPIO_PULLUP_DISABLE,
  225. .pin_bit_mask = 1ULL << handle->scl_num,
  226. };
  227. ESP_RETURN_ON_ERROR(gpio_set_level(handle->scl_num, 1), TAG, "i2c scl pin set level failed");
  228. ESP_RETURN_ON_ERROR(gpio_config(&scl_conf), TAG, "config GPIO failed");
  229. gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[handle->scl_num], PIN_FUNC_GPIO);
  230. esp_rom_gpio_connect_out_signal(handle->scl_num, i2c_periph_signal[port_id].scl_out_sig, 0, 0);
  231. esp_rom_gpio_connect_in_signal(handle->scl_num, i2c_periph_signal[port_id].scl_in_sig, 0);
  232. return ret;
  233. }