i2c.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 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. #include "esp_attr.h"
  11. #include "esp_intr_alloc.h"
  12. #include "esp_log.h"
  13. #include "esp_check.h"
  14. #include "malloc.h"
  15. #include "freertos/FreeRTOS.h"
  16. #include "freertos/semphr.h"
  17. #include "freertos/task.h"
  18. #include "freertos/ringbuf.h"
  19. #include "esp_pm.h"
  20. #include "soc/soc_memory_layout.h"
  21. #include "hal/i2c_hal.h"
  22. #include "hal/gpio_hal.h"
  23. #include "soc/i2c_periph.h"
  24. #include "driver/i2c.h"
  25. #include "esp_private/periph_ctrl.h"
  26. #include "esp_rom_gpio.h"
  27. #include "esp_rom_sys.h"
  28. #include <sys/param.h>
  29. #include "soc/clk_tree_defs.h"
  30. #if SOC_I2C_SUPPORT_APB || SOC_I2C_SUPPORT_XTAL
  31. #include "esp_private/esp_clk.h"
  32. #endif
  33. #if SOC_I2C_SUPPORT_RTC
  34. #include "clk_ctrl_os.h"
  35. #endif
  36. static const char *I2C_TAG = "i2c";
  37. /* DRAM_ATTR is required to avoid I2C array placed in flash, due to accessed from ISR */
  38. #define I2C_ENTER_CRITICAL_ISR(mux) portENTER_CRITICAL_ISR(mux)
  39. #define I2C_EXIT_CRITICAL_ISR(mux) portEXIT_CRITICAL_ISR(mux)
  40. #define I2C_ENTER_CRITICAL(mux) portENTER_CRITICAL(mux)
  41. #define I2C_EXIT_CRITICAL(mux) portEXIT_CRITICAL(mux)
  42. #define I2C_DRIVER_ERR_STR "i2c driver install error"
  43. #define I2C_DRIVER_MALLOC_ERR_STR "i2c driver malloc error"
  44. #define I2C_INTR_ALLOC_ERR_STR "i2c interrupt allocation error"
  45. #define I2C_NUM_ERROR_STR "i2c number error"
  46. #define I2C_TIMING_VAL_ERR_STR "i2c timing value error"
  47. #define I2C_ADDR_ERROR_STR "i2c null address error"
  48. #define I2C_DRIVER_NOT_INSTALL_ERR_STR "i2c driver not installed"
  49. #if SOC_I2C_SUPPORT_SLAVE
  50. #define I2C_SLAVE_BUFFER_LEN_ERR_STR "i2c buffer size too small for slave mode"
  51. #define I2C_MODE_SLAVE_ERR_STR "Only allowed in slave mode"
  52. #endif
  53. #define I2C_EVT_QUEUE_ERR_STR "i2c evt queue error"
  54. #define I2C_SEM_ERR_STR "i2c semaphore error"
  55. #define I2C_BUF_ERR_STR "i2c ringbuffer error"
  56. #define I2C_MASTER_MODE_ERR_STR "Only allowed in master mode"
  57. #define I2C_CMD_MALLOC_ERR_STR "i2c command link malloc error"
  58. #define I2C_CMD_USER_ALLOC_ERR_STR "i2c command link allocation error: the buffer provided is too small."
  59. #define I2C_TRANS_MODE_ERR_STR "i2c trans mode error"
  60. #define I2C_MODE_ERR_STR "i2c mode error"
  61. #define I2C_SDA_IO_ERR_STR "sda gpio number error"
  62. #define I2C_SCL_IO_ERR_STR "scl gpio number error"
  63. #define I2C_SCL_SDA_EQUAL_ERR_STR "scl and sda gpio numbers are the same"
  64. #define I2C_CMD_LINK_INIT_ERR_STR "i2c command link error"
  65. #define I2C_GPIO_PULLUP_ERR_STR "this i2c pin does not support internal pull-up"
  66. #define I2C_ACK_TYPE_ERR_STR "i2c ack type error"
  67. #define I2C_DATA_LEN_ERR_STR "i2c data read length error"
  68. #define I2C_PSRAM_BUFFER_WARN_STR "Using buffer allocated from psram"
  69. #define I2C_LOCK_ERR_STR "Power lock creation error"
  70. #define I2C_CLK_FLAG_ERR_STR "i2c clock choice is invalid, please check flag and frequency"
  71. #define I2C_FIFO_FULL_THRESH_VAL (28)
  72. #define I2C_FIFO_EMPTY_THRESH_VAL (5)
  73. #define I2C_IO_INIT_LEVEL (1)
  74. #define I2C_CMD_ALIVE_INTERVAL_TICK (1000 / portTICK_PERIOD_MS)
  75. #define I2C_CMD_EVT_ALIVE (0)
  76. #define I2C_CMD_EVT_DONE (1)
  77. #define I2C_EVT_QUEUE_LEN (1)
  78. #if SOC_I2C_SUPPORT_SLAVE
  79. #define I2C_SLAVE_TIMEOUT_DEFAULT (32000) /* I2C slave timeout value, APB clock cycle number */
  80. #define I2C_SLAVE_SDA_SAMPLE_DEFAULT (10) /* I2C slave sample time after scl positive edge default value */
  81. #define I2C_SLAVE_SDA_HOLD_DEFAULT (10) /* I2C slave hold time after scl negative edge default value */
  82. #endif
  83. #define I2C_MASTER_TOUT_CNUM_DEFAULT (8) /* I2C master timeout cycle number of I2C clock, after which the timeout interrupt will be triggered */
  84. #define I2C_ACKERR_CNT_MAX (10)
  85. #define I2C_FILTER_CYC_NUM_DEF (7) /* The number of apb cycles filtered by default*/
  86. #define I2C_CLR_BUS_SCL_NUM (9)
  87. #define I2C_CLR_BUS_HALF_PERIOD_US (5)
  88. #define I2C_TRANS_BUF_MINIMUM_SIZE (sizeof(i2c_cmd_desc_t) + \
  89. sizeof(i2c_cmd_link_t) * 8) /* It is required to have allocate one i2c_cmd_desc_t per command:
  90. * start + write (device address) + write buffer +
  91. * start + write (device address) + read buffer + read buffer for NACK +
  92. * stop */
  93. #define I2C_CONTEX_INIT_DEF(uart_num) {\
  94. .hal.dev = I2C_LL_GET_HW(uart_num),\
  95. .spinlock = portMUX_INITIALIZER_UNLOCKED,\
  96. .hw_enabled = false,\
  97. }
  98. #define I2C_CLOCK_INVALID (-1)
  99. /**
  100. * I2C bus are defined in the header files, let's check that the values are correct
  101. */
  102. #if SOC_I2C_NUM >= 2
  103. _Static_assert(I2C_NUM_1 == 1, "I2C_NUM_1 must be equal to 1");
  104. #endif // SOC_I2C_NUM >= 2
  105. _Static_assert(I2C_NUM_MAX == SOC_I2C_NUM, "I2C_NUM_MAX must be equal to SOC_I2C_NUM");
  106. typedef struct {
  107. i2c_hw_cmd_t hw_cmd;
  108. union {
  109. uint8_t* data; // When total_bytes > 1
  110. uint8_t data_byte; //when total_byte == 1
  111. };
  112. size_t bytes_used;
  113. size_t total_bytes;
  114. } i2c_cmd_t;
  115. typedef struct i2c_cmd_link {
  116. i2c_cmd_t cmd; /*!< command in current cmd link */
  117. struct i2c_cmd_link *next; /*!< next cmd link */
  118. } i2c_cmd_link_t;
  119. typedef struct {
  120. i2c_cmd_link_t *head; /*!< head of the command link */
  121. i2c_cmd_link_t *cur; /*!< last node of the command link */
  122. i2c_cmd_link_t *free; /*!< the first node to free of the command link */
  123. void *free_buffer; /*!< pointer to the next free data in user's buffer */
  124. uint32_t free_size; /*!< remaining size of the user's buffer */
  125. } i2c_cmd_desc_t;
  126. /* INTERNAL_STRUCT_SIZE must be at least sizeof(i2c_cmd_link_t) */
  127. _Static_assert(I2C_INTERNAL_STRUCT_SIZE >= sizeof(i2c_cmd_link_t),
  128. "I2C_INTERNAL_STRUCT_SIZE must be at least sizeof(i2c_cmd_link_t), please adjust this value.");
  129. typedef enum {
  130. I2C_STATUS_READ, /*!< read status for current master command */
  131. I2C_STATUS_WRITE, /*!< write status for current master command */
  132. I2C_STATUS_IDLE, /*!< idle status for current master command */
  133. I2C_STATUS_ACK_ERROR, /*!< ack error status for current master command */
  134. I2C_STATUS_DONE, /*!< I2C command done */
  135. I2C_STATUS_TIMEOUT, /*!< I2C bus status error, and operation timeout */
  136. } i2c_status_t;
  137. typedef struct {
  138. int type;
  139. } i2c_cmd_evt_t;
  140. typedef struct {
  141. int i2c_num; /*!< I2C port number */
  142. int mode; /*!< I2C mode, master or slave */
  143. intr_handle_t intr_handle; /*!< I2C interrupt handle*/
  144. int cmd_idx; /*!< record current command index, for master mode */
  145. int status; /*!< record current command status, for master mode */
  146. int rx_cnt; /*!< record current read index, for master mode */
  147. uint8_t data_buf[SOC_I2C_FIFO_LEN ]; /*!< a buffer to store i2c fifo data */
  148. i2c_cmd_desc_t cmd_link; /*!< I2C command link */
  149. QueueHandle_t cmd_evt_queue; /*!< I2C command event queue */
  150. #if CONFIG_SPIRAM_USE_MALLOC
  151. uint8_t *evt_queue_storage; /*!< The buffer that will hold the items in the queue */
  152. int intr_alloc_flags; /*!< Used to allocate the interrupt */
  153. StaticQueue_t evt_queue_buffer; /*!< The buffer that will hold the queue structure*/
  154. #endif
  155. SemaphoreHandle_t cmd_mux; /*!< semaphore to lock command process */
  156. #ifdef CONFIG_PM_ENABLE
  157. esp_pm_lock_handle_t pm_lock;
  158. #endif
  159. #if SOC_I2C_SUPPORT_SLAVE
  160. SemaphoreHandle_t slv_rx_mux; /*!< slave rx buffer mux */
  161. SemaphoreHandle_t slv_tx_mux; /*!< slave tx buffer mux */
  162. #endif // SOC_I2C_SUPPORT_SLAVE
  163. size_t rx_buf_length; /*!< rx buffer length */
  164. RingbufHandle_t rx_ring_buf; /*!< rx ringbuffer handler of slave mode */
  165. size_t tx_buf_length; /*!< tx buffer length */
  166. RingbufHandle_t tx_ring_buf; /*!< tx ringbuffer handler of slave mode */
  167. } i2c_obj_t;
  168. typedef struct {
  169. i2c_hal_context_t hal; /*!< I2C hal context */
  170. portMUX_TYPE spinlock;
  171. bool hw_enabled;
  172. #if !SOC_I2C_SUPPORT_HW_CLR_BUS
  173. int scl_io_num;
  174. int sda_io_num;
  175. #endif
  176. } i2c_context_t;
  177. typedef struct
  178. {
  179. uint8_t character; /*!< I2C source clock characteristic */
  180. } i2c_clk_alloc_t;
  181. static i2c_context_t i2c_context[I2C_NUM_MAX] = {
  182. I2C_CONTEX_INIT_DEF(I2C_NUM_0),
  183. /* Now that I2C_NUM_MAX is part of an enum (i2c_port_t), we cannot use
  184. * it anomore in the preprocessor! */
  185. #if SOC_I2C_NUM > 1
  186. I2C_CONTEX_INIT_DEF(I2C_NUM_1),
  187. #endif
  188. };
  189. // i2c clock characteristic, the entry order and numbers MUST be the same as SOC_I2C_CLKS
  190. static i2c_clk_alloc_t i2c_clk_alloc[] = {
  191. #if SOC_I2C_SUPPORT_APB
  192. {0}, /*!< I2C APB clock characteristic*/
  193. #endif
  194. #if SOC_I2C_SUPPORT_XTAL
  195. {0}, /*!< I2C XTAL characteristic*/
  196. #endif
  197. #if SOC_I2C_SUPPORT_RTC
  198. {I2C_SCLK_SRC_FLAG_LIGHT_SLEEP | I2C_SCLK_SRC_FLAG_AWARE_DFS}, /*!< I2C 20M RTC characteristic*/
  199. #endif
  200. #if SOC_I2C_SUPPORT_REF_TICK
  201. {I2C_SCLK_SRC_FLAG_AWARE_DFS}, /*!< I2C REF_TICK characteristic*/
  202. #endif
  203. };
  204. static i2c_obj_t *p_i2c_obj[I2C_NUM_MAX] = {0};
  205. static void i2c_isr_handler_default(void *arg);
  206. static void i2c_master_cmd_begin_static(i2c_port_t i2c_num, portBASE_TYPE* HPTaskAwoken);
  207. static esp_err_t i2c_hw_fsm_reset(i2c_port_t i2c_num);
  208. static void i2c_hw_disable(i2c_port_t i2c_num)
  209. {
  210. I2C_ENTER_CRITICAL(&(i2c_context[i2c_num].spinlock));
  211. if (i2c_context[i2c_num].hw_enabled != false) {
  212. periph_module_disable(i2c_periph_signal[i2c_num].module);
  213. i2c_context[i2c_num].hw_enabled = false;
  214. }
  215. I2C_EXIT_CRITICAL(&(i2c_context[i2c_num].spinlock));
  216. }
  217. static void i2c_hw_enable(i2c_port_t i2c_num)
  218. {
  219. I2C_ENTER_CRITICAL(&(i2c_context[i2c_num].spinlock));
  220. if (i2c_context[i2c_num].hw_enabled != true) {
  221. periph_module_enable(i2c_periph_signal[i2c_num].module);
  222. i2c_context[i2c_num].hw_enabled = true;
  223. }
  224. I2C_EXIT_CRITICAL(&(i2c_context[i2c_num].spinlock));
  225. }
  226. /*
  227. For i2c master mode, we don't need to use a buffer for the data, the APIs will execute the master commands
  228. and return after all of the commands have been sent out or when error occurs. So when we send master commands,
  229. we should free or modify the source data only after the i2c_master_cmd_begin function returns.
  230. For i2c slave mode, we need a data buffer to stash the sending and receiving data, because the hardware fifo
  231. has only 32 bytes.
  232. */
  233. esp_err_t i2c_driver_install(i2c_port_t i2c_num, i2c_mode_t mode, size_t slv_rx_buf_len, size_t slv_tx_buf_len,
  234. int intr_alloc_flags)
  235. {
  236. ESP_RETURN_ON_FALSE(i2c_num < I2C_NUM_MAX, ESP_ERR_INVALID_ARG, I2C_TAG, I2C_NUM_ERROR_STR);
  237. #if SOC_I2C_SUPPORT_SLAVE
  238. ESP_RETURN_ON_FALSE(mode == I2C_MODE_MASTER || ( slv_rx_buf_len > 100 || slv_tx_buf_len > 100 ),
  239. ESP_ERR_INVALID_ARG, I2C_TAG, I2C_SLAVE_BUFFER_LEN_ERR_STR);
  240. #endif // SOC_I2C_SUPPORT_SLAVE
  241. esp_err_t ret = ESP_OK;
  242. if (p_i2c_obj[i2c_num] == NULL) {
  243. #if !CONFIG_SPIRAM_USE_MALLOC
  244. p_i2c_obj[i2c_num] = (i2c_obj_t *) calloc(1, sizeof(i2c_obj_t));
  245. #else
  246. if ( !(intr_alloc_flags & ESP_INTR_FLAG_IRAM) ) {
  247. p_i2c_obj[i2c_num] = (i2c_obj_t *) calloc(1, sizeof(i2c_obj_t));
  248. } else {
  249. p_i2c_obj[i2c_num] = (i2c_obj_t *) heap_caps_calloc(1, sizeof(i2c_obj_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
  250. }
  251. #endif
  252. if (p_i2c_obj[i2c_num] == NULL) {
  253. ESP_LOGE(I2C_TAG, I2C_DRIVER_MALLOC_ERR_STR);
  254. return ESP_FAIL;
  255. }
  256. i2c_obj_t *p_i2c = p_i2c_obj[i2c_num];
  257. p_i2c->i2c_num = i2c_num;
  258. p_i2c->mode = mode;
  259. p_i2c->cmd_idx = 0;
  260. p_i2c->rx_cnt = 0;
  261. p_i2c->status = I2C_STATUS_IDLE;
  262. #if CONFIG_SPIRAM_USE_MALLOC
  263. p_i2c->intr_alloc_flags = intr_alloc_flags;
  264. #endif
  265. #if SOC_I2C_SUPPORT_SLAVE
  266. if (mode == I2C_MODE_SLAVE) {
  267. #if CONFIG_RINGBUF_PLACE_ISR_FUNCTIONS_INTO_FLASH
  268. if (intr_alloc_flags & ESP_INTR_FLAG_IRAM ) {
  269. ESP_LOGE(I2C_TAG, "ringbuf ISR functions in flash, but used in IRAM interrupt");
  270. goto err;
  271. }
  272. #endif
  273. //we only use ringbuffer for slave mode.
  274. if (slv_rx_buf_len > 0) {
  275. p_i2c->rx_ring_buf = xRingbufferCreate(slv_rx_buf_len, RINGBUF_TYPE_BYTEBUF);
  276. if (p_i2c->rx_ring_buf == NULL) {
  277. ESP_LOGE(I2C_TAG, I2C_BUF_ERR_STR);
  278. goto err;
  279. }
  280. p_i2c->rx_buf_length = slv_rx_buf_len;
  281. } else {
  282. p_i2c->rx_ring_buf = NULL;
  283. p_i2c->rx_buf_length = 0;
  284. }
  285. if (slv_tx_buf_len > 0) {
  286. p_i2c->tx_ring_buf = xRingbufferCreate(slv_tx_buf_len, RINGBUF_TYPE_BYTEBUF);
  287. if (p_i2c->tx_ring_buf == NULL) {
  288. ESP_LOGE(I2C_TAG, I2C_BUF_ERR_STR);
  289. goto err;
  290. }
  291. p_i2c->tx_buf_length = slv_tx_buf_len;
  292. } else {
  293. p_i2c->tx_ring_buf = NULL;
  294. p_i2c->tx_buf_length = 0;
  295. }
  296. p_i2c->slv_rx_mux = xSemaphoreCreateMutex();
  297. p_i2c->slv_tx_mux = xSemaphoreCreateMutex();
  298. if (p_i2c->slv_rx_mux == NULL || p_i2c->slv_tx_mux == NULL) {
  299. ESP_LOGE(I2C_TAG, I2C_SEM_ERR_STR);
  300. goto err;
  301. }
  302. } else
  303. #endif // SOC_I2C_SUPPORT_SLAVE
  304. {
  305. //semaphore to sync sending process, because we only have 32 bytes for hardware fifo.
  306. p_i2c->cmd_mux = xSemaphoreCreateMutex();
  307. #ifdef CONFIG_PM_ENABLE
  308. if (esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "i2c_driver", &p_i2c->pm_lock) != ESP_OK) {
  309. ESP_LOGE(I2C_TAG, I2C_LOCK_ERR_STR);
  310. goto err;
  311. }
  312. #endif
  313. #if !CONFIG_SPIRAM_USE_MALLOC
  314. p_i2c->cmd_evt_queue = xQueueCreate(I2C_EVT_QUEUE_LEN, sizeof(i2c_cmd_evt_t));
  315. #else
  316. if ( !(intr_alloc_flags & ESP_INTR_FLAG_IRAM) ) {
  317. p_i2c->cmd_evt_queue = xQueueCreate(I2C_EVT_QUEUE_LEN, sizeof(i2c_cmd_evt_t));
  318. } else {
  319. p_i2c->evt_queue_storage = (uint8_t *)heap_caps_calloc(I2C_EVT_QUEUE_LEN, sizeof(i2c_cmd_evt_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
  320. if ( p_i2c->evt_queue_storage == NULL ) {
  321. ESP_LOGE(I2C_TAG, I2C_DRIVER_MALLOC_ERR_STR);
  322. goto err;
  323. }
  324. memset(&p_i2c->evt_queue_buffer, 0, sizeof(StaticQueue_t));
  325. p_i2c->cmd_evt_queue = xQueueCreateStatic(I2C_EVT_QUEUE_LEN, sizeof(i2c_cmd_evt_t), p_i2c->evt_queue_storage, &p_i2c->evt_queue_buffer);
  326. }
  327. #endif
  328. if (p_i2c->cmd_mux == NULL || p_i2c->cmd_evt_queue == NULL) {
  329. ESP_LOGE(I2C_TAG, I2C_SEM_ERR_STR);
  330. goto err;
  331. }
  332. //command link
  333. p_i2c->cmd_link.cur = NULL;
  334. p_i2c->cmd_link.head = NULL;
  335. p_i2c->cmd_link.free = NULL;
  336. p_i2c->tx_ring_buf = NULL;
  337. p_i2c->rx_buf_length = 0;
  338. p_i2c->tx_ring_buf = NULL;
  339. p_i2c->tx_buf_length = 0;
  340. }
  341. } else {
  342. ESP_LOGE(I2C_TAG, I2C_DRIVER_ERR_STR);
  343. return ESP_FAIL;
  344. }
  345. i2c_hw_enable(i2c_num);
  346. //Disable I2C interrupt.
  347. i2c_hal_disable_intr_mask(&(i2c_context[i2c_num].hal), I2C_LL_INTR_MASK);
  348. i2c_hal_clr_intsts_mask(&(i2c_context[i2c_num].hal), I2C_LL_INTR_MASK);
  349. //hook isr handler
  350. ret = esp_intr_alloc(i2c_periph_signal[i2c_num].irq, intr_alloc_flags,
  351. i2c_isr_handler_default, p_i2c_obj[i2c_num],
  352. &p_i2c_obj[i2c_num]->intr_handle);
  353. ESP_GOTO_ON_ERROR(ret, err, I2C_TAG, I2C_INTR_ALLOC_ERR_STR);
  354. #if SOC_I2C_SUPPORT_SLAVE
  355. //Enable I2C slave rx interrupt
  356. if (mode == I2C_MODE_SLAVE) {
  357. i2c_hal_enable_slave_rx_it(&(i2c_context[i2c_num].hal));
  358. }
  359. #endif // SOC_I2C_SUPPORT_SLAVE
  360. return ESP_OK;
  361. err:
  362. //Some error has happened. Free/destroy all allocated things and return ESP_FAIL.
  363. if (p_i2c_obj[i2c_num]) {
  364. if (p_i2c_obj[i2c_num]->rx_ring_buf) {
  365. vRingbufferDelete(p_i2c_obj[i2c_num]->rx_ring_buf);
  366. p_i2c_obj[i2c_num]->rx_ring_buf = NULL;
  367. p_i2c_obj[i2c_num]->rx_buf_length = 0;
  368. }
  369. if (p_i2c_obj[i2c_num]->tx_ring_buf) {
  370. vRingbufferDelete(p_i2c_obj[i2c_num]->tx_ring_buf);
  371. p_i2c_obj[i2c_num]->tx_ring_buf = NULL;
  372. p_i2c_obj[i2c_num]->tx_buf_length = 0;
  373. }
  374. if (p_i2c_obj[i2c_num]->cmd_evt_queue) {
  375. vQueueDelete(p_i2c_obj[i2c_num]->cmd_evt_queue);
  376. p_i2c_obj[i2c_num]->cmd_evt_queue = NULL;
  377. }
  378. if (p_i2c_obj[i2c_num]->cmd_mux) {
  379. vSemaphoreDelete(p_i2c_obj[i2c_num]->cmd_mux);
  380. }
  381. #if SOC_I2C_SUPPORT_SLAVE
  382. if (p_i2c_obj[i2c_num]->slv_rx_mux) {
  383. vSemaphoreDelete(p_i2c_obj[i2c_num]->slv_rx_mux);
  384. }
  385. if (p_i2c_obj[i2c_num]->slv_tx_mux) {
  386. vSemaphoreDelete(p_i2c_obj[i2c_num]->slv_tx_mux);
  387. }
  388. #endif
  389. #ifdef CONFIG_PM_ENABLE
  390. if (p_i2c_obj[i2c_num]->pm_lock) {
  391. esp_pm_lock_delete(p_i2c_obj[i2c_num]->pm_lock);
  392. p_i2c_obj[i2c_num]->pm_lock = NULL;
  393. }
  394. #endif
  395. #if CONFIG_SPIRAM_USE_MALLOC
  396. if (p_i2c_obj[i2c_num]->evt_queue_storage) {
  397. free(p_i2c_obj[i2c_num]->evt_queue_storage);
  398. p_i2c_obj[i2c_num]->evt_queue_storage = NULL;
  399. }
  400. #endif
  401. }
  402. free(p_i2c_obj[i2c_num]);
  403. p_i2c_obj[i2c_num] = NULL;
  404. return ESP_FAIL;
  405. }
  406. esp_err_t i2c_driver_delete(i2c_port_t i2c_num)
  407. {
  408. ESP_RETURN_ON_FALSE(i2c_num < I2C_NUM_MAX, ESP_ERR_INVALID_ARG, I2C_TAG, I2C_NUM_ERROR_STR);
  409. ESP_RETURN_ON_FALSE(p_i2c_obj[i2c_num] != NULL, ESP_FAIL, I2C_TAG, I2C_DRIVER_ERR_STR);
  410. i2c_obj_t *p_i2c = p_i2c_obj[i2c_num];
  411. i2c_hal_disable_intr_mask(&(i2c_context[i2c_num].hal), I2C_LL_INTR_MASK);
  412. esp_intr_free(p_i2c->intr_handle);
  413. p_i2c->intr_handle = NULL;
  414. if (p_i2c->cmd_mux) {
  415. // Let any command in progress finish.
  416. xSemaphoreTake(p_i2c->cmd_mux, portMAX_DELAY);
  417. xSemaphoreGive(p_i2c->cmd_mux);
  418. vSemaphoreDelete(p_i2c->cmd_mux);
  419. }
  420. if (p_i2c_obj[i2c_num]->cmd_evt_queue) {
  421. vQueueDelete(p_i2c_obj[i2c_num]->cmd_evt_queue);
  422. p_i2c_obj[i2c_num]->cmd_evt_queue = NULL;
  423. }
  424. #if SOC_I2C_SUPPORT_SLAVE
  425. if (p_i2c->slv_rx_mux) {
  426. vSemaphoreDelete(p_i2c->slv_rx_mux);
  427. }
  428. if (p_i2c->slv_tx_mux) {
  429. vSemaphoreDelete(p_i2c->slv_tx_mux);
  430. }
  431. #endif
  432. if (p_i2c->rx_ring_buf) {
  433. vRingbufferDelete(p_i2c->rx_ring_buf);
  434. p_i2c->rx_ring_buf = NULL;
  435. p_i2c->rx_buf_length = 0;
  436. }
  437. if (p_i2c->tx_ring_buf) {
  438. vRingbufferDelete(p_i2c->tx_ring_buf);
  439. p_i2c->tx_ring_buf = NULL;
  440. p_i2c->tx_buf_length = 0;
  441. }
  442. #ifdef CONFIG_PM_ENABLE
  443. if (p_i2c->pm_lock) {
  444. esp_pm_lock_delete(p_i2c->pm_lock);
  445. p_i2c->pm_lock = NULL;
  446. }
  447. #endif
  448. #if CONFIG_SPIRAM_USE_MALLOC
  449. if (p_i2c_obj[i2c_num]->evt_queue_storage) {
  450. free(p_i2c_obj[i2c_num]->evt_queue_storage);
  451. p_i2c_obj[i2c_num]->evt_queue_storage = NULL;
  452. }
  453. #endif
  454. free(p_i2c_obj[i2c_num]);
  455. p_i2c_obj[i2c_num] = NULL;
  456. i2c_hw_disable(i2c_num);
  457. return ESP_OK;
  458. }
  459. esp_err_t i2c_reset_tx_fifo(i2c_port_t i2c_num)
  460. {
  461. ESP_RETURN_ON_FALSE(i2c_num < I2C_NUM_MAX, ESP_ERR_INVALID_ARG, I2C_TAG, I2C_NUM_ERROR_STR);
  462. I2C_ENTER_CRITICAL(&(i2c_context[i2c_num].spinlock));
  463. i2c_hal_txfifo_rst(&(i2c_context[i2c_num].hal));
  464. I2C_EXIT_CRITICAL(&(i2c_context[i2c_num].spinlock));
  465. return ESP_OK;
  466. }
  467. esp_err_t i2c_reset_rx_fifo(i2c_port_t i2c_num)
  468. {
  469. ESP_RETURN_ON_FALSE(i2c_num < I2C_NUM_MAX, ESP_ERR_INVALID_ARG, I2C_TAG, I2C_NUM_ERROR_STR);
  470. I2C_ENTER_CRITICAL(&(i2c_context[i2c_num].spinlock));
  471. i2c_hal_rxfifo_rst(&(i2c_context[i2c_num].hal));
  472. I2C_EXIT_CRITICAL(&(i2c_context[i2c_num].spinlock));
  473. return ESP_OK;
  474. }
  475. static void IRAM_ATTR i2c_isr_handler_default(void *arg)
  476. {
  477. i2c_obj_t *p_i2c = (i2c_obj_t *) arg;
  478. int i2c_num = p_i2c->i2c_num;
  479. // Interrupt protection.
  480. // On C3 and S3 targets, the I2C may trigger a spurious interrupt,
  481. // in order to detect these false positive, check the I2C's hardware interrupt mask
  482. uint32_t int_mask;
  483. i2c_hal_get_intsts_mask(&(i2c_context[i2c_num].hal), &int_mask);
  484. if (int_mask == 0) {
  485. return;
  486. }
  487. i2c_intr_event_t evt_type = I2C_INTR_EVENT_ERR;
  488. portBASE_TYPE HPTaskAwoken = pdFALSE;
  489. portBASE_TYPE HPTaskAwokenCallee = pdFALSE;
  490. if (p_i2c->mode == I2C_MODE_MASTER) {
  491. if (p_i2c->status == I2C_STATUS_WRITE) {
  492. i2c_hal_master_handle_tx_event(&(i2c_context[i2c_num].hal), &evt_type);
  493. } else if (p_i2c->status == I2C_STATUS_READ) {
  494. i2c_hal_master_handle_rx_event(&(i2c_context[i2c_num].hal), &evt_type);
  495. }
  496. if (evt_type == I2C_INTR_EVENT_NACK) {
  497. p_i2c_obj[i2c_num]->status = I2C_STATUS_ACK_ERROR;
  498. i2c_master_cmd_begin_static(i2c_num, &HPTaskAwokenCallee);
  499. } else if (evt_type == I2C_INTR_EVENT_TOUT) {
  500. p_i2c_obj[i2c_num]->status = I2C_STATUS_TIMEOUT;
  501. i2c_master_cmd_begin_static(i2c_num, &HPTaskAwokenCallee);
  502. } else if (evt_type == I2C_INTR_EVENT_ARBIT_LOST) {
  503. p_i2c_obj[i2c_num]->status = I2C_STATUS_TIMEOUT;
  504. i2c_master_cmd_begin_static(i2c_num, &HPTaskAwokenCallee);
  505. } else if (evt_type == I2C_INTR_EVENT_END_DET) {
  506. i2c_master_cmd_begin_static(i2c_num, &HPTaskAwokenCallee);
  507. } else if (evt_type == I2C_INTR_EVENT_TRANS_DONE) {
  508. if (p_i2c->status != I2C_STATUS_ACK_ERROR && p_i2c->status != I2C_STATUS_IDLE) {
  509. i2c_master_cmd_begin_static(i2c_num, &HPTaskAwokenCallee);
  510. }
  511. } else {
  512. // Do nothing if there is no proper event.
  513. return;
  514. }
  515. i2c_cmd_evt_t evt = {
  516. .type = I2C_CMD_EVT_ALIVE
  517. };
  518. xQueueSendFromISR(p_i2c->cmd_evt_queue, &evt, &HPTaskAwoken);
  519. }
  520. #if SOC_I2C_SUPPORT_SLAVE
  521. else {
  522. i2c_hal_slave_handle_event(&(i2c_context[i2c_num].hal), &evt_type);
  523. if (evt_type == I2C_INTR_EVENT_TRANS_DONE || evt_type == I2C_INTR_EVENT_RXFIFO_FULL) {
  524. uint32_t rx_fifo_cnt;
  525. i2c_hal_get_rxfifo_cnt(&(i2c_context[i2c_num].hal), &rx_fifo_cnt);
  526. i2c_hal_read_rxfifo(&(i2c_context[i2c_num].hal), p_i2c->data_buf, rx_fifo_cnt);
  527. xRingbufferSendFromISR(p_i2c->rx_ring_buf, p_i2c->data_buf, rx_fifo_cnt, &HPTaskAwoken);
  528. i2c_hal_slave_clr_rx_it(&(i2c_context[i2c_num].hal));
  529. } else if (evt_type == I2C_INTR_EVENT_TXFIFO_EMPTY) {
  530. uint32_t tx_fifo_rem;
  531. i2c_hal_get_txfifo_cnt(&(i2c_context[i2c_num].hal), &tx_fifo_rem);
  532. size_t size = 0;
  533. uint8_t *data = (uint8_t *) xRingbufferReceiveUpToFromISR(p_i2c->tx_ring_buf, &size, tx_fifo_rem);
  534. if (data) {
  535. i2c_hal_write_txfifo(&(i2c_context[i2c_num].hal), data, size);
  536. vRingbufferReturnItemFromISR(p_i2c->tx_ring_buf, data, &HPTaskAwoken);
  537. } else {
  538. i2c_hal_disable_slave_tx_it(&(i2c_context[i2c_num].hal));
  539. }
  540. i2c_hal_slave_clr_tx_it(&(i2c_context[i2c_num].hal));
  541. }
  542. }
  543. #endif // SOC_I2C_SUPPORT_SLAVE
  544. //We only need to check here if there is a high-priority task needs to be switched.
  545. if (HPTaskAwoken == pdTRUE || HPTaskAwokenCallee == pdTRUE) {
  546. portYIELD_FROM_ISR();
  547. }
  548. }
  549. esp_err_t i2c_set_data_mode(i2c_port_t i2c_num, i2c_trans_mode_t tx_trans_mode, i2c_trans_mode_t rx_trans_mode)
  550. {
  551. ESP_RETURN_ON_FALSE(i2c_num < I2C_NUM_MAX, ESP_ERR_INVALID_ARG, I2C_TAG, I2C_NUM_ERROR_STR);
  552. ESP_RETURN_ON_FALSE(tx_trans_mode < I2C_DATA_MODE_MAX, ESP_ERR_INVALID_ARG, I2C_TAG, I2C_TRANS_MODE_ERR_STR);
  553. ESP_RETURN_ON_FALSE(rx_trans_mode < I2C_DATA_MODE_MAX, ESP_ERR_INVALID_ARG, I2C_TAG, I2C_TRANS_MODE_ERR_STR);
  554. I2C_ENTER_CRITICAL(&(i2c_context[i2c_num].spinlock));
  555. i2c_hal_set_data_mode(&(i2c_context[i2c_num].hal), tx_trans_mode, rx_trans_mode);
  556. i2c_hal_update_config(&(i2c_context[i2c_num].hal));
  557. I2C_EXIT_CRITICAL(&(i2c_context[i2c_num].spinlock));
  558. return ESP_OK;
  559. }
  560. esp_err_t i2c_get_data_mode(i2c_port_t i2c_num, i2c_trans_mode_t *tx_trans_mode, i2c_trans_mode_t *rx_trans_mode)
  561. {
  562. ESP_RETURN_ON_FALSE(i2c_num < I2C_NUM_MAX, ESP_ERR_INVALID_ARG, I2C_TAG, I2C_NUM_ERROR_STR);
  563. i2c_hal_get_data_mode(&(i2c_context[i2c_num].hal), tx_trans_mode, rx_trans_mode);
  564. return ESP_OK;
  565. }
  566. /* Some slave device will die by accident and keep the SDA in low level,
  567. * in this case, master should send several clock to make the slave release the bus.
  568. * Slave mode of ESP32 might also get in wrong state that held the SDA low,
  569. * in this case, master device could send a stop signal to make esp32 slave release the bus.
  570. **/
  571. static esp_err_t i2c_master_clear_bus(i2c_port_t i2c_num)
  572. {
  573. #if !SOC_I2C_SUPPORT_HW_CLR_BUS
  574. const int scl_half_period = I2C_CLR_BUS_HALF_PERIOD_US; // use standard 100kHz data rate
  575. int i = 0;
  576. int scl_io = i2c_context[i2c_num].scl_io_num;
  577. int sda_io = i2c_context[i2c_num].sda_io_num;
  578. gpio_set_direction(scl_io, GPIO_MODE_OUTPUT_OD);
  579. gpio_set_direction(sda_io, GPIO_MODE_INPUT_OUTPUT_OD);
  580. // If a SLAVE device was in a read operation when the bus was interrupted, the SLAVE device is controlling SDA.
  581. // The only bit during the 9 clock cycles of a READ byte the MASTER(ESP32) is guaranteed control over is during the ACK bit
  582. // period. If the slave is sending a stream of ZERO bytes, it will only release SDA during the ACK bit period.
  583. // So, this reset code needs to synchronize the bit stream with, Either, the ACK bit, Or a 1 bit to correctly generate
  584. // a STOP condition.
  585. gpio_set_level(scl_io, 0);
  586. gpio_set_level(sda_io, 1);
  587. esp_rom_delay_us(scl_half_period);
  588. while (!gpio_get_level(sda_io) && (i++ < I2C_CLR_BUS_SCL_NUM)) {
  589. gpio_set_level(scl_io, 1);
  590. esp_rom_delay_us(scl_half_period);
  591. gpio_set_level(scl_io, 0);
  592. esp_rom_delay_us(scl_half_period);
  593. }
  594. gpio_set_level(sda_io, 0); // setup for STOP
  595. gpio_set_level(scl_io, 1);
  596. esp_rom_delay_us(scl_half_period);
  597. gpio_set_level(sda_io, 1); // STOP, SDA low -> high while SCL is HIGH
  598. i2c_set_pin(i2c_num, sda_io, scl_io, 1, 1, I2C_MODE_MASTER);
  599. #else
  600. i2c_hal_master_clr_bus(&(i2c_context[i2c_num].hal));
  601. #endif
  602. return ESP_OK;
  603. }
  604. /**if the power and SDA/SCL wires are in proper condition, everything works find with reading the slave.
  605. * If we remove the power supply for the slave during I2C is reading, or directly connect SDA or SCL to ground,
  606. * this would cause the I2C FSM get stuck in wrong state, all we can do is to reset the I2C hardware in this case.
  607. **/
  608. static esp_err_t i2c_hw_fsm_reset(i2c_port_t i2c_num)
  609. {
  610. // A workaround for avoiding cause timeout issue when using
  611. // hardware reset.
  612. #if !SOC_I2C_SUPPORT_HW_FSM_RST
  613. int scl_low_period, scl_high_period, scl_wait_high_period;
  614. int scl_start_hold, scl_rstart_setup;
  615. int scl_stop_hold, scl_stop_setup;
  616. int sda_hold, sda_sample;
  617. int timeout;
  618. uint8_t filter_cfg;
  619. i2c_hal_get_scl_clk_timing(&(i2c_context[i2c_num].hal), &scl_high_period, &scl_low_period, &scl_wait_high_period);
  620. i2c_hal_get_start_timing(&(i2c_context[i2c_num].hal), &scl_rstart_setup, &scl_start_hold);
  621. i2c_hal_get_stop_timing(&(i2c_context[i2c_num].hal), &scl_stop_setup, &scl_stop_hold);
  622. i2c_hal_get_sda_timing(&(i2c_context[i2c_num].hal), &sda_sample, &sda_hold);
  623. i2c_hal_get_tout(&(i2c_context[i2c_num].hal), &timeout);
  624. i2c_hal_get_filter(&(i2c_context[i2c_num].hal), &filter_cfg);
  625. //to reset the I2C hw module, we need re-enable the hw
  626. i2c_hw_disable(i2c_num);
  627. i2c_master_clear_bus(i2c_num);
  628. i2c_hw_enable(i2c_num);
  629. i2c_hal_master_init(&(i2c_context[i2c_num].hal), i2c_num);
  630. i2c_hal_disable_intr_mask(&(i2c_context[i2c_num].hal), I2C_LL_INTR_MASK);
  631. i2c_hal_clr_intsts_mask(&(i2c_context[i2c_num].hal), I2C_LL_INTR_MASK);
  632. i2c_hal_set_scl_clk_timing(&(i2c_context[i2c_num].hal), scl_high_period, scl_low_period, scl_wait_high_period);
  633. i2c_hal_set_start_timing(&(i2c_context[i2c_num].hal), scl_rstart_setup, scl_start_hold);
  634. i2c_hal_set_stop_timing(&(i2c_context[i2c_num].hal), scl_stop_setup, scl_stop_hold);
  635. i2c_hal_set_sda_timing(&(i2c_context[i2c_num].hal), sda_sample, sda_hold);
  636. i2c_hal_set_tout(&(i2c_context[i2c_num].hal), timeout);
  637. i2c_hal_set_filter(&(i2c_context[i2c_num].hal), filter_cfg);
  638. #else
  639. i2c_hal_master_fsm_rst(&(i2c_context[i2c_num].hal));
  640. i2c_master_clear_bus(i2c_num);
  641. #endif
  642. return ESP_OK;
  643. }
  644. static uint32_t s_get_src_clk_freq(i2c_clock_source_t clk_src)
  645. {
  646. // TODO: replace the following switch table by clk_tree API
  647. uint32_t periph_src_clk_hz = 0;
  648. switch (clk_src) {
  649. #if SOC_I2C_SUPPORT_APB
  650. case I2C_CLK_SRC_APB:
  651. periph_src_clk_hz = esp_clk_apb_freq();
  652. break;
  653. #endif
  654. #if SOC_I2C_SUPPORT_XTAL
  655. case I2C_CLK_SRC_XTAL:
  656. periph_src_clk_hz = esp_clk_xtal_freq();
  657. break;
  658. #endif
  659. #if SOC_I2C_SUPPORT_RTC
  660. case I2C_CLK_SRC_RC_FAST:
  661. periph_rtc_dig_clk8m_enable();
  662. periph_src_clk_hz = periph_rtc_dig_clk8m_get_freq();
  663. break;
  664. #endif
  665. #if SOC_I2C_SUPPORT_REF_TICK
  666. case RMT_CLK_SRC_REF_TICK:
  667. periph_src_clk_hz = REF_CLK_FREQ;
  668. break;
  669. #endif
  670. default:
  671. ESP_RETURN_ON_FALSE(false, ESP_ERR_NOT_SUPPORTED, I2C_TAG, "clock source %d is not supported", clk_src);
  672. break;
  673. }
  674. return periph_src_clk_hz;
  675. }
  676. static i2c_clock_source_t s_get_clk_src(const uint32_t clk_flags, const uint32_t clk_speed)
  677. {
  678. i2c_clock_source_t clk_srcs[] = SOC_I2C_CLKS;
  679. for (size_t i = 0; i < sizeof(clk_srcs)/ sizeof(clk_srcs[0]); i++) {
  680. if ( ((clk_flags & i2c_clk_alloc[i].character) == clk_flags) &&
  681. (clk_speed <= (s_get_src_clk_freq(clk_srcs[i]) / 20))) { // I2C SCL clock frequency should not larger than clock source frequency/20
  682. return clk_srcs[i];
  683. }
  684. }
  685. return I2C_CLOCK_INVALID; // flag invalid;
  686. }
  687. esp_err_t i2c_param_config(i2c_port_t i2c_num, const i2c_config_t *i2c_conf)
  688. {
  689. i2c_clock_source_t src_clk = I2C_CLK_SRC_DEFAULT;
  690. esp_err_t ret = ESP_OK;
  691. ESP_RETURN_ON_FALSE(i2c_conf != NULL, ESP_ERR_INVALID_ARG, I2C_TAG, I2C_ADDR_ERROR_STR);
  692. ESP_RETURN_ON_FALSE(i2c_conf->mode < I2C_MODE_MAX, ESP_ERR_INVALID_ARG, I2C_TAG, I2C_MODE_ERR_STR);
  693. if (i2c_conf->mode == I2C_MODE_MASTER) {
  694. src_clk = s_get_clk_src(i2c_conf->clk_flags, i2c_conf->master.clk_speed);
  695. }
  696. #if SOC_I2C_SUPPORT_SLAVE
  697. else {
  698. #if SOC_I2C_SUPPORT_REF_TICK
  699. /* On ESP32-S2, APB clock shall always be used in slave mode as the
  700. * other one, I2C_CLK_SRC_REF_TICK, is too slow, even for sampling a
  701. * 100KHz SCL. */
  702. src_clk = I2C_CLK_SRC_APB;
  703. #else
  704. src_clk = s_get_clk_src(i2c_conf->clk_flags, i2c_conf->slave.maximum_speed);
  705. #endif // CONFIG_IDF_TARGET_ESP32S2
  706. }
  707. #endif // SOC_I2C_SUPPORT_SLAVE
  708. ESP_RETURN_ON_FALSE(src_clk != I2C_CLOCK_INVALID, ESP_ERR_INVALID_ARG, I2C_TAG, I2C_CLK_FLAG_ERR_STR);
  709. ret = i2c_set_pin(i2c_num, i2c_conf->sda_io_num, i2c_conf->scl_io_num,
  710. i2c_conf->sda_pullup_en, i2c_conf->scl_pullup_en, i2c_conf->mode);
  711. if (ret != ESP_OK) {
  712. return ret;
  713. }
  714. i2c_hw_enable(i2c_num);
  715. I2C_ENTER_CRITICAL(&(i2c_context[i2c_num].spinlock));
  716. i2c_hal_disable_intr_mask(&(i2c_context[i2c_num].hal), I2C_LL_INTR_MASK);
  717. i2c_hal_clr_intsts_mask(&(i2c_context[i2c_num].hal), I2C_LL_INTR_MASK);
  718. #if SOC_I2C_SUPPORT_SLAVE
  719. if (i2c_conf->mode == I2C_MODE_SLAVE) { //slave mode
  720. i2c_hal_slave_init(&(i2c_context[i2c_num].hal), i2c_num);
  721. i2c_hal_set_source_clk(&(i2c_context[i2c_num].hal), src_clk);
  722. i2c_hal_set_slave_addr(&(i2c_context[i2c_num].hal), i2c_conf->slave.slave_addr, i2c_conf->slave.addr_10bit_en);
  723. i2c_hal_set_rxfifo_full_thr(&(i2c_context[i2c_num].hal), I2C_FIFO_FULL_THRESH_VAL);
  724. i2c_hal_set_txfifo_empty_thr(&(i2c_context[i2c_num].hal), I2C_FIFO_EMPTY_THRESH_VAL);
  725. //set timing for data
  726. i2c_hal_set_sda_timing(&(i2c_context[i2c_num].hal), I2C_SLAVE_SDA_SAMPLE_DEFAULT, I2C_SLAVE_SDA_HOLD_DEFAULT);
  727. i2c_hal_set_tout(&(i2c_context[i2c_num].hal), I2C_SLAVE_TIMEOUT_DEFAULT);
  728. i2c_hal_enable_slave_rx_it(&(i2c_context[i2c_num].hal));
  729. } else
  730. #endif // SOC_I2C_SUPPORT_SLAVE
  731. {
  732. i2c_hal_master_init(&(i2c_context[i2c_num].hal), i2c_num);
  733. //Default, we enable hardware filter
  734. i2c_hal_set_filter(&(i2c_context[i2c_num].hal), I2C_FILTER_CYC_NUM_DEF);
  735. i2c_hal_set_bus_timing(&(i2c_context[i2c_num].hal), i2c_conf->master.clk_speed, src_clk, s_get_src_clk_freq(src_clk));
  736. }
  737. i2c_hal_update_config(&(i2c_context[i2c_num].hal));
  738. I2C_EXIT_CRITICAL(&(i2c_context[i2c_num].spinlock));
  739. return ESP_OK;
  740. }
  741. esp_err_t i2c_set_period(i2c_port_t i2c_num, int high_period, int low_period)
  742. {
  743. ESP_RETURN_ON_FALSE(i2c_num < I2C_NUM_MAX, ESP_ERR_INVALID_ARG, I2C_TAG, I2C_NUM_ERROR_STR);
  744. ESP_RETURN_ON_FALSE((high_period <= I2C_SCL_HIGH_PERIOD_V) && (high_period > 0), ESP_ERR_INVALID_ARG, I2C_TAG, I2C_TIMING_VAL_ERR_STR);
  745. ESP_RETURN_ON_FALSE((low_period <= I2C_SCL_LOW_PERIOD_V) && (low_period > 0), ESP_ERR_INVALID_ARG, I2C_TAG, I2C_TIMING_VAL_ERR_STR);
  746. I2C_ENTER_CRITICAL(&(i2c_context[i2c_num].spinlock));
  747. i2c_hal_set_scl_timing(&(i2c_context[i2c_num].hal), high_period, low_period);
  748. i2c_hal_update_config(&(i2c_context[i2c_num].hal));
  749. I2C_EXIT_CRITICAL(&(i2c_context[i2c_num].spinlock));
  750. return ESP_OK;
  751. }
  752. esp_err_t i2c_get_period(i2c_port_t i2c_num, int *high_period, int *low_period)
  753. {
  754. ESP_RETURN_ON_FALSE(i2c_num < I2C_NUM_MAX && high_period != NULL && low_period != NULL, ESP_ERR_INVALID_ARG, I2C_TAG, I2C_NUM_ERROR_STR);
  755. I2C_ENTER_CRITICAL(&(i2c_context[i2c_num].spinlock));
  756. i2c_hal_get_scl_timing(&(i2c_context[i2c_num].hal), high_period, low_period);
  757. I2C_EXIT_CRITICAL(&(i2c_context[i2c_num].spinlock));
  758. return ESP_OK;
  759. }
  760. esp_err_t i2c_filter_enable(i2c_port_t i2c_num, uint8_t cyc_num)
  761. {
  762. ESP_RETURN_ON_FALSE(i2c_num < I2C_NUM_MAX, ESP_ERR_INVALID_ARG, I2C_TAG, I2C_NUM_ERROR_STR);
  763. ESP_RETURN_ON_FALSE(p_i2c_obj[i2c_num] != NULL, ESP_FAIL, I2C_TAG, I2C_DRIVER_ERR_STR);
  764. I2C_ENTER_CRITICAL(&(i2c_context[i2c_num].spinlock));
  765. i2c_hal_set_filter(&(i2c_context[i2c_num].hal), cyc_num);
  766. i2c_hal_update_config(&(i2c_context[i2c_num].hal));
  767. I2C_EXIT_CRITICAL(&(i2c_context[i2c_num].spinlock));
  768. return ESP_OK;
  769. }
  770. esp_err_t i2c_filter_disable(i2c_port_t i2c_num)
  771. {
  772. ESP_RETURN_ON_FALSE(i2c_num < I2C_NUM_MAX, ESP_ERR_INVALID_ARG, I2C_TAG, I2C_NUM_ERROR_STR);
  773. I2C_ENTER_CRITICAL(&(i2c_context[i2c_num].spinlock));
  774. i2c_hal_set_filter(&(i2c_context[i2c_num].hal), 0);
  775. i2c_hal_update_config(&(i2c_context[i2c_num].hal));
  776. I2C_EXIT_CRITICAL(&(i2c_context[i2c_num].spinlock));
  777. return ESP_OK;
  778. }
  779. esp_err_t i2c_set_start_timing(i2c_port_t i2c_num, int setup_time, int hold_time)
  780. {
  781. ESP_RETURN_ON_FALSE(i2c_num < I2C_NUM_MAX, ESP_ERR_INVALID_ARG, I2C_TAG, I2C_NUM_ERROR_STR);
  782. ESP_RETURN_ON_FALSE((hold_time <= I2C_SCL_START_HOLD_TIME_V) && (hold_time > 0), ESP_ERR_INVALID_ARG, I2C_TAG, I2C_TIMING_VAL_ERR_STR);
  783. ESP_RETURN_ON_FALSE((setup_time <= I2C_SCL_RSTART_SETUP_TIME_V) && (setup_time > 0), ESP_ERR_INVALID_ARG, I2C_TAG, I2C_TIMING_VAL_ERR_STR);
  784. I2C_ENTER_CRITICAL(&(i2c_context[i2c_num].spinlock));
  785. i2c_hal_set_start_timing(&(i2c_context[i2c_num].hal), setup_time, hold_time);
  786. i2c_hal_update_config(&(i2c_context[i2c_num].hal));
  787. I2C_EXIT_CRITICAL(&(i2c_context[i2c_num].spinlock));
  788. return ESP_OK;
  789. }
  790. esp_err_t i2c_get_start_timing(i2c_port_t i2c_num, int *setup_time, int *hold_time)
  791. {
  792. ESP_RETURN_ON_FALSE(i2c_num < I2C_NUM_MAX && setup_time != NULL && hold_time != NULL, ESP_ERR_INVALID_ARG, I2C_TAG, I2C_NUM_ERROR_STR);
  793. I2C_ENTER_CRITICAL(&(i2c_context[i2c_num].spinlock));
  794. i2c_hal_get_start_timing(&(i2c_context[i2c_num].hal), setup_time, hold_time);
  795. I2C_EXIT_CRITICAL(&(i2c_context[i2c_num].spinlock));
  796. return ESP_OK;
  797. }
  798. esp_err_t i2c_set_stop_timing(i2c_port_t i2c_num, int setup_time, int hold_time)
  799. {
  800. ESP_RETURN_ON_FALSE(i2c_num < I2C_NUM_MAX, ESP_ERR_INVALID_ARG, I2C_TAG, I2C_NUM_ERROR_STR);
  801. ESP_RETURN_ON_FALSE((setup_time <= I2C_SCL_STOP_SETUP_TIME_V) && (setup_time > 0), ESP_ERR_INVALID_ARG, I2C_TAG, I2C_TIMING_VAL_ERR_STR);
  802. ESP_RETURN_ON_FALSE((hold_time <= I2C_SCL_STOP_HOLD_TIME_V) && (hold_time > 0), ESP_ERR_INVALID_ARG, I2C_TAG, I2C_TIMING_VAL_ERR_STR);
  803. I2C_ENTER_CRITICAL(&(i2c_context[i2c_num].spinlock));
  804. i2c_hal_set_stop_timing(&(i2c_context[i2c_num].hal), setup_time, hold_time);
  805. i2c_hal_update_config(&(i2c_context[i2c_num].hal));
  806. I2C_EXIT_CRITICAL(&(i2c_context[i2c_num].spinlock));
  807. return ESP_OK;
  808. }
  809. esp_err_t i2c_get_stop_timing(i2c_port_t i2c_num, int *setup_time, int *hold_time)
  810. {
  811. ESP_RETURN_ON_FALSE(i2c_num < I2C_NUM_MAX && setup_time != NULL && hold_time != NULL, ESP_ERR_INVALID_ARG, I2C_TAG, I2C_NUM_ERROR_STR);
  812. I2C_ENTER_CRITICAL(&(i2c_context[i2c_num].spinlock));
  813. i2c_hal_get_stop_timing(&(i2c_context[i2c_num].hal), setup_time, hold_time);
  814. I2C_EXIT_CRITICAL(&(i2c_context[i2c_num].spinlock));
  815. return ESP_OK;
  816. }
  817. esp_err_t i2c_set_data_timing(i2c_port_t i2c_num, int sample_time, int hold_time)
  818. {
  819. ESP_RETURN_ON_FALSE(i2c_num < I2C_NUM_MAX, ESP_ERR_INVALID_ARG, I2C_TAG, I2C_NUM_ERROR_STR);
  820. ESP_RETURN_ON_FALSE((sample_time <= I2C_SDA_SAMPLE_TIME_V) && (sample_time > 0), ESP_ERR_INVALID_ARG, I2C_TAG, I2C_TIMING_VAL_ERR_STR);
  821. ESP_RETURN_ON_FALSE((hold_time <= I2C_SDA_HOLD_TIME_V) && (hold_time > 0), ESP_ERR_INVALID_ARG, I2C_TAG, I2C_TIMING_VAL_ERR_STR);
  822. I2C_ENTER_CRITICAL(&(i2c_context[i2c_num].spinlock));
  823. i2c_hal_set_sda_timing(&(i2c_context[i2c_num].hal), sample_time, hold_time);
  824. i2c_hal_update_config(&(i2c_context[i2c_num].hal));
  825. I2C_EXIT_CRITICAL(&(i2c_context[i2c_num].spinlock));
  826. return ESP_OK;
  827. }
  828. esp_err_t i2c_get_data_timing(i2c_port_t i2c_num, int *sample_time, int *hold_time)
  829. {
  830. ESP_RETURN_ON_FALSE(i2c_num < I2C_NUM_MAX && sample_time != NULL && hold_time != NULL, ESP_ERR_INVALID_ARG, I2C_TAG, I2C_NUM_ERROR_STR);
  831. I2C_ENTER_CRITICAL(&(i2c_context[i2c_num].spinlock));
  832. i2c_hal_get_sda_timing(&(i2c_context[i2c_num].hal), sample_time, hold_time);
  833. I2C_EXIT_CRITICAL(&(i2c_context[i2c_num].spinlock));
  834. return ESP_OK;
  835. }
  836. esp_err_t i2c_set_timeout(i2c_port_t i2c_num, int timeout)
  837. {
  838. ESP_RETURN_ON_FALSE(i2c_num < I2C_NUM_MAX, ESP_ERR_INVALID_ARG, I2C_TAG, I2C_NUM_ERROR_STR);
  839. ESP_RETURN_ON_FALSE((timeout <= I2C_LL_MAX_TIMEOUT) && (timeout > 0), ESP_ERR_INVALID_ARG, I2C_TAG, I2C_TIMING_VAL_ERR_STR);
  840. I2C_ENTER_CRITICAL(&(i2c_context[i2c_num].spinlock));
  841. i2c_hal_set_tout(&(i2c_context[i2c_num].hal), timeout);
  842. I2C_EXIT_CRITICAL(&(i2c_context[i2c_num].spinlock));
  843. return ESP_OK;
  844. }
  845. esp_err_t i2c_get_timeout(i2c_port_t i2c_num, int *timeout)
  846. {
  847. ESP_RETURN_ON_FALSE(i2c_num < I2C_NUM_MAX && timeout != NULL, ESP_ERR_INVALID_ARG, I2C_TAG, I2C_NUM_ERROR_STR);
  848. i2c_hal_get_tout(&(i2c_context[i2c_num].hal), timeout);
  849. return ESP_OK;
  850. }
  851. esp_err_t i2c_set_pin(i2c_port_t i2c_num, int sda_io_num, int scl_io_num, bool sda_pullup_en, bool scl_pullup_en, i2c_mode_t mode)
  852. {
  853. ESP_RETURN_ON_FALSE(( i2c_num < I2C_NUM_MAX ), ESP_ERR_INVALID_ARG, I2C_TAG, I2C_NUM_ERROR_STR);
  854. ESP_RETURN_ON_FALSE(((sda_io_num < 0) || ((GPIO_IS_VALID_OUTPUT_GPIO(sda_io_num)))), ESP_ERR_INVALID_ARG, I2C_TAG, I2C_SDA_IO_ERR_STR);
  855. ESP_RETURN_ON_FALSE(scl_io_num < 0 ||
  856. #if SOC_I2C_SUPPORT_SLAVE
  857. (GPIO_IS_VALID_GPIO(scl_io_num) && mode == I2C_MODE_SLAVE) ||
  858. #endif // SOC_I2C_SUPPORT_SLAVE
  859. (GPIO_IS_VALID_OUTPUT_GPIO(scl_io_num)),
  860. ESP_ERR_INVALID_ARG, I2C_TAG,
  861. I2C_SCL_IO_ERR_STR);
  862. ESP_RETURN_ON_FALSE(sda_io_num < 0 ||
  863. (sda_pullup_en == GPIO_PULLUP_ENABLE && GPIO_IS_VALID_OUTPUT_GPIO(sda_io_num)) ||
  864. sda_pullup_en == GPIO_PULLUP_DISABLE, ESP_ERR_INVALID_ARG, I2C_TAG, I2C_GPIO_PULLUP_ERR_STR);
  865. ESP_RETURN_ON_FALSE(scl_io_num < 0 ||
  866. (scl_pullup_en == GPIO_PULLUP_ENABLE && GPIO_IS_VALID_OUTPUT_GPIO(scl_io_num)) ||
  867. scl_pullup_en == GPIO_PULLUP_DISABLE, ESP_ERR_INVALID_ARG, I2C_TAG, I2C_GPIO_PULLUP_ERR_STR);
  868. ESP_RETURN_ON_FALSE((sda_io_num != scl_io_num), ESP_ERR_INVALID_ARG, I2C_TAG, I2C_SCL_SDA_EQUAL_ERR_STR);
  869. int sda_in_sig, sda_out_sig, scl_in_sig, scl_out_sig;
  870. sda_out_sig = i2c_periph_signal[i2c_num].sda_out_sig;
  871. sda_in_sig = i2c_periph_signal[i2c_num].sda_in_sig;
  872. scl_out_sig = i2c_periph_signal[i2c_num].scl_out_sig;
  873. scl_in_sig = i2c_periph_signal[i2c_num].scl_in_sig;
  874. if (sda_io_num >= 0) {
  875. gpio_set_level(sda_io_num, I2C_IO_INIT_LEVEL);
  876. gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[sda_io_num], PIN_FUNC_GPIO);
  877. gpio_set_direction(sda_io_num, GPIO_MODE_INPUT_OUTPUT_OD);
  878. if (sda_pullup_en == GPIO_PULLUP_ENABLE) {
  879. gpio_set_pull_mode(sda_io_num, GPIO_PULLUP_ONLY);
  880. } else {
  881. gpio_set_pull_mode(sda_io_num, GPIO_FLOATING);
  882. }
  883. esp_rom_gpio_connect_out_signal(sda_io_num, sda_out_sig, 0, 0);
  884. esp_rom_gpio_connect_in_signal(sda_io_num, sda_in_sig, 0);
  885. }
  886. if (scl_io_num >= 0) {
  887. if (mode == I2C_MODE_MASTER) {
  888. gpio_set_level(scl_io_num, I2C_IO_INIT_LEVEL);
  889. gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[scl_io_num], PIN_FUNC_GPIO);
  890. gpio_set_direction(scl_io_num, GPIO_MODE_INPUT_OUTPUT_OD);
  891. esp_rom_gpio_connect_out_signal(scl_io_num, scl_out_sig, 0, 0);
  892. } else {
  893. gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[scl_io_num], PIN_FUNC_GPIO);
  894. gpio_set_direction(scl_io_num, GPIO_MODE_INPUT);
  895. }
  896. esp_rom_gpio_connect_in_signal(scl_io_num, scl_in_sig, 0);
  897. if (scl_pullup_en == GPIO_PULLUP_ENABLE) {
  898. gpio_set_pull_mode(scl_io_num, GPIO_PULLUP_ONLY);
  899. } else {
  900. gpio_set_pull_mode(scl_io_num, GPIO_FLOATING);
  901. }
  902. }
  903. #if !SOC_I2C_SUPPORT_HW_CLR_BUS
  904. i2c_context[i2c_num].scl_io_num = scl_io_num;
  905. i2c_context[i2c_num].sda_io_num = sda_io_num;
  906. #endif
  907. return ESP_OK;
  908. }
  909. esp_err_t i2c_master_write_to_device(i2c_port_t i2c_num, uint8_t device_address,
  910. const uint8_t* write_buffer, size_t write_size,
  911. TickType_t ticks_to_wait)
  912. {
  913. esp_err_t err = ESP_OK;
  914. uint8_t buffer[I2C_TRANS_BUF_MINIMUM_SIZE] = { 0 };
  915. i2c_cmd_handle_t handle = i2c_cmd_link_create_static(buffer, sizeof(buffer));
  916. assert (handle != NULL);
  917. err = i2c_master_start(handle);
  918. if (err != ESP_OK) {
  919. goto end;
  920. }
  921. err = i2c_master_write_byte(handle, device_address << 1 | I2C_MASTER_WRITE, true);
  922. if (err != ESP_OK) {
  923. goto end;
  924. }
  925. err = i2c_master_write(handle, write_buffer, write_size, true);
  926. if (err != ESP_OK) {
  927. goto end;
  928. }
  929. i2c_master_stop(handle);
  930. err = i2c_master_cmd_begin(i2c_num, handle, ticks_to_wait);
  931. end:
  932. i2c_cmd_link_delete_static(handle);
  933. return err;
  934. }
  935. esp_err_t i2c_master_read_from_device(i2c_port_t i2c_num, uint8_t device_address,
  936. uint8_t* read_buffer, size_t read_size,
  937. TickType_t ticks_to_wait)
  938. {
  939. esp_err_t err = ESP_OK;
  940. uint8_t buffer[I2C_TRANS_BUF_MINIMUM_SIZE] = { 0 };
  941. i2c_cmd_handle_t handle = i2c_cmd_link_create_static(buffer, sizeof(buffer));
  942. assert (handle != NULL);
  943. err = i2c_master_start(handle);
  944. if (err != ESP_OK) {
  945. goto end;
  946. }
  947. err = i2c_master_write_byte(handle, device_address << 1 | I2C_MASTER_READ, true);
  948. if (err != ESP_OK) {
  949. goto end;
  950. }
  951. err = i2c_master_read(handle, read_buffer, read_size, I2C_MASTER_LAST_NACK);
  952. if (err != ESP_OK) {
  953. goto end;
  954. }
  955. i2c_master_stop(handle);
  956. err = i2c_master_cmd_begin(i2c_num, handle, ticks_to_wait);
  957. end:
  958. i2c_cmd_link_delete_static(handle);
  959. return err;
  960. }
  961. esp_err_t i2c_master_write_read_device(i2c_port_t i2c_num, uint8_t device_address,
  962. const uint8_t* write_buffer, size_t write_size,
  963. uint8_t* read_buffer, size_t read_size,
  964. TickType_t ticks_to_wait)
  965. {
  966. esp_err_t err = ESP_OK;
  967. uint8_t buffer[I2C_TRANS_BUF_MINIMUM_SIZE] = { 0 };
  968. i2c_cmd_handle_t handle = i2c_cmd_link_create_static(buffer, sizeof(buffer));
  969. assert (handle != NULL);
  970. err = i2c_master_start(handle);
  971. if (err != ESP_OK) {
  972. goto end;
  973. }
  974. err = i2c_master_write_byte(handle, device_address << 1 | I2C_MASTER_WRITE, true);
  975. if (err != ESP_OK) {
  976. goto end;
  977. }
  978. err = i2c_master_write(handle, write_buffer, write_size, true);
  979. if (err != ESP_OK) {
  980. goto end;
  981. }
  982. err = i2c_master_start(handle);
  983. if (err != ESP_OK) {
  984. goto end;
  985. }
  986. err = i2c_master_write_byte(handle, device_address << 1 | I2C_MASTER_READ, true);
  987. if (err != ESP_OK) {
  988. goto end;
  989. }
  990. err = i2c_master_read(handle, read_buffer, read_size, I2C_MASTER_LAST_NACK);
  991. if (err != ESP_OK) {
  992. goto end;
  993. }
  994. i2c_master_stop(handle);
  995. err = i2c_master_cmd_begin(i2c_num, handle, ticks_to_wait);
  996. end:
  997. i2c_cmd_link_delete_static(handle);
  998. return err;
  999. }
  1000. static inline bool i2c_cmd_link_is_static(i2c_cmd_desc_t *cmd_desc)
  1001. {
  1002. return (cmd_desc->free_buffer != NULL);
  1003. }
  1004. i2c_cmd_handle_t i2c_cmd_link_create_static(uint8_t* buffer, uint32_t size)
  1005. {
  1006. if (buffer == NULL || size <= sizeof(i2c_cmd_desc_t)) {
  1007. return NULL;
  1008. }
  1009. i2c_cmd_desc_t *cmd_desc = (i2c_cmd_desc_t *) buffer;
  1010. cmd_desc->head = NULL;
  1011. cmd_desc->cur = NULL;
  1012. cmd_desc->free = NULL;
  1013. cmd_desc->free_buffer = cmd_desc + 1;
  1014. cmd_desc->free_size = size - sizeof(i2c_cmd_desc_t);
  1015. return (i2c_cmd_handle_t) cmd_desc;
  1016. }
  1017. i2c_cmd_handle_t i2c_cmd_link_create(void)
  1018. {
  1019. #if !CONFIG_SPIRAM_USE_MALLOC
  1020. i2c_cmd_desc_t *cmd_desc = (i2c_cmd_desc_t *) calloc(1, sizeof(i2c_cmd_desc_t));
  1021. #else
  1022. i2c_cmd_desc_t *cmd_desc = (i2c_cmd_desc_t *) heap_caps_calloc(1, sizeof(i2c_cmd_desc_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
  1023. #endif
  1024. return (i2c_cmd_handle_t) cmd_desc;
  1025. }
  1026. void i2c_cmd_link_delete_static(i2c_cmd_handle_t cmd_handle)
  1027. {
  1028. i2c_cmd_desc_t *cmd = (i2c_cmd_desc_t *) cmd_handle;
  1029. if (cmd == NULL || !i2c_cmd_link_is_static(cmd)) {
  1030. return;
  1031. }
  1032. /* Currently, this function does nothing, but it is not impossible
  1033. * that it will change in a near future. */
  1034. }
  1035. void i2c_cmd_link_delete(i2c_cmd_handle_t cmd_handle)
  1036. {
  1037. i2c_cmd_desc_t *cmd = (i2c_cmd_desc_t *) cmd_handle;
  1038. /* Memory should be freed only if allocated dynamically.
  1039. * If the user gave the buffer for a static allocation, do
  1040. * nothing. */
  1041. if (cmd == NULL || i2c_cmd_link_is_static(cmd)) {
  1042. return;
  1043. }
  1044. while (cmd->free) {
  1045. i2c_cmd_link_t *ptmp = cmd->free;
  1046. cmd->free = cmd->free->next;
  1047. free(ptmp);
  1048. }
  1049. cmd->cur = NULL;
  1050. cmd->free = NULL;
  1051. cmd->head = NULL;
  1052. free(cmd_handle);
  1053. return;
  1054. }
  1055. static esp_err_t i2c_cmd_allocate(i2c_cmd_desc_t *cmd_desc, size_t n, size_t size, void** outptr)
  1056. {
  1057. esp_err_t err = ESP_OK;
  1058. if (i2c_cmd_link_is_static(cmd_desc)) {
  1059. const size_t required = n * size;
  1060. /* User defined buffer.
  1061. * Check whether there is enough space in the buffer. */
  1062. if (cmd_desc->free_size < required) {
  1063. err = ESP_ERR_NO_MEM;
  1064. } else {
  1065. /* Allocate the pointer. */
  1066. *outptr = cmd_desc->free_buffer;
  1067. /* Decrement the free size from the user's bufffer. */
  1068. cmd_desc->free_buffer += required;
  1069. cmd_desc->free_size -= required;
  1070. }
  1071. } else {
  1072. #if !CONFIG_SPIRAM_USE_MALLOC
  1073. *outptr = calloc(n, size);
  1074. #else
  1075. *outptr = heap_caps_calloc(n, size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
  1076. #endif
  1077. if (*outptr == NULL) {
  1078. err = ESP_FAIL;
  1079. }
  1080. }
  1081. return err;
  1082. }
  1083. static inline void i2c_cmd_log_alloc_error(i2c_cmd_desc_t *cmd_desc)
  1084. {
  1085. if (i2c_cmd_link_is_static(cmd_desc)) {
  1086. ESP_LOGE(I2C_TAG, I2C_CMD_USER_ALLOC_ERR_STR);
  1087. } else {
  1088. ESP_LOGE(I2C_TAG, I2C_CMD_MALLOC_ERR_STR);
  1089. }
  1090. }
  1091. static esp_err_t i2c_cmd_link_append(i2c_cmd_handle_t cmd_handle, i2c_cmd_t *cmd)
  1092. {
  1093. esp_err_t err = ESP_OK;
  1094. i2c_cmd_desc_t *cmd_desc = (i2c_cmd_desc_t *) cmd_handle;
  1095. assert(cmd_desc != NULL);
  1096. if (cmd_desc->head == NULL) {
  1097. err = i2c_cmd_allocate(cmd_desc, 1, sizeof(i2c_cmd_link_t), (void**) &cmd_desc->head);
  1098. if (err != ESP_OK) {
  1099. i2c_cmd_log_alloc_error(cmd_desc);
  1100. return err;
  1101. }
  1102. cmd_desc->cur = cmd_desc->head;
  1103. cmd_desc->free = cmd_desc->head;
  1104. } else {
  1105. assert(cmd_desc->cur != NULL);
  1106. err = i2c_cmd_allocate(cmd_desc, 1, sizeof(i2c_cmd_link_t), (void**) &cmd_desc->cur->next);
  1107. if (err != ESP_OK) {
  1108. i2c_cmd_log_alloc_error(cmd_desc);
  1109. return err;
  1110. }
  1111. cmd_desc->cur = cmd_desc->cur->next;
  1112. }
  1113. memcpy((uint8_t *) &cmd_desc->cur->cmd, (uint8_t *) cmd, sizeof(i2c_cmd_t));
  1114. cmd_desc->cur->next = NULL;
  1115. return err;
  1116. }
  1117. esp_err_t i2c_master_start(i2c_cmd_handle_t cmd_handle)
  1118. {
  1119. ESP_RETURN_ON_FALSE(cmd_handle != NULL, ESP_ERR_INVALID_ARG, I2C_TAG, I2C_CMD_LINK_INIT_ERR_STR);
  1120. i2c_cmd_t cmd = { 0 };
  1121. cmd.hw_cmd.op_code = I2C_LL_CMD_RESTART;
  1122. return i2c_cmd_link_append(cmd_handle, &cmd);
  1123. }
  1124. esp_err_t i2c_master_stop(i2c_cmd_handle_t cmd_handle)
  1125. {
  1126. ESP_RETURN_ON_FALSE(cmd_handle != NULL, ESP_ERR_INVALID_ARG, I2C_TAG, I2C_CMD_LINK_INIT_ERR_STR);
  1127. i2c_cmd_t cmd = { 0 };
  1128. cmd.hw_cmd.op_code = I2C_LL_CMD_STOP;
  1129. return i2c_cmd_link_append(cmd_handle, &cmd);
  1130. }
  1131. esp_err_t i2c_master_write(i2c_cmd_handle_t cmd_handle, const uint8_t *data, size_t data_len, bool ack_en)
  1132. {
  1133. ESP_RETURN_ON_FALSE((data != NULL), ESP_ERR_INVALID_ARG, I2C_TAG, I2C_ADDR_ERROR_STR);
  1134. ESP_RETURN_ON_FALSE(cmd_handle != NULL, ESP_ERR_INVALID_ARG, I2C_TAG, I2C_CMD_LINK_INIT_ERR_STR);
  1135. if (data_len == 1) {
  1136. /* If data_len if 1, i2c_master_write_byte should have been called,
  1137. * correct this here. */
  1138. return i2c_master_write_byte(cmd_handle, *data, ack_en);
  1139. }
  1140. i2c_cmd_t cmd = {
  1141. .hw_cmd = {
  1142. .ack_en = ack_en,
  1143. .op_code = I2C_LL_CMD_WRITE,
  1144. },
  1145. .data = (uint8_t*) data,
  1146. .total_bytes = data_len,
  1147. };
  1148. return i2c_cmd_link_append(cmd_handle, &cmd);
  1149. }
  1150. esp_err_t i2c_master_write_byte(i2c_cmd_handle_t cmd_handle, uint8_t data, bool ack_en)
  1151. {
  1152. ESP_RETURN_ON_FALSE(cmd_handle != NULL, ESP_ERR_INVALID_ARG, I2C_TAG, I2C_CMD_LINK_INIT_ERR_STR);
  1153. i2c_cmd_t cmd = {
  1154. .hw_cmd = {
  1155. .ack_en = ack_en,
  1156. .op_code = I2C_LL_CMD_WRITE,
  1157. },
  1158. .data_byte = data,
  1159. .total_bytes = 1,
  1160. };
  1161. return i2c_cmd_link_append(cmd_handle, &cmd);
  1162. }
  1163. static esp_err_t i2c_master_read_static(i2c_cmd_handle_t cmd_handle, uint8_t *data, size_t data_len, i2c_ack_type_t ack)
  1164. {
  1165. i2c_cmd_t cmd = {
  1166. .hw_cmd = {
  1167. .ack_val = ack & 0x1,
  1168. .op_code = I2C_LL_CMD_READ,
  1169. },
  1170. .data = data,
  1171. .total_bytes = data_len,
  1172. };
  1173. return i2c_cmd_link_append(cmd_handle, &cmd);
  1174. }
  1175. esp_err_t i2c_master_read_byte(i2c_cmd_handle_t cmd_handle, uint8_t *data, i2c_ack_type_t ack)
  1176. {
  1177. ESP_RETURN_ON_FALSE((data != NULL), ESP_ERR_INVALID_ARG, I2C_TAG, I2C_ADDR_ERROR_STR);
  1178. ESP_RETURN_ON_FALSE(cmd_handle != NULL, ESP_ERR_INVALID_ARG, I2C_TAG, I2C_CMD_LINK_INIT_ERR_STR);
  1179. ESP_RETURN_ON_FALSE(ack < I2C_MASTER_ACK_MAX, ESP_ERR_INVALID_ARG, I2C_TAG, I2C_ACK_TYPE_ERR_STR);
  1180. i2c_cmd_t cmd = {
  1181. .hw_cmd = {
  1182. .ack_val = ((ack == I2C_MASTER_LAST_NACK) ? I2C_MASTER_NACK : (ack & 0x1)),
  1183. .op_code = I2C_LL_CMD_READ,
  1184. },
  1185. .data = data,
  1186. .total_bytes = 1,
  1187. };
  1188. return i2c_cmd_link_append(cmd_handle, &cmd);
  1189. }
  1190. esp_err_t i2c_master_read(i2c_cmd_handle_t cmd_handle, uint8_t *data, size_t data_len, i2c_ack_type_t ack)
  1191. {
  1192. ESP_RETURN_ON_FALSE((data != NULL), ESP_ERR_INVALID_ARG, I2C_TAG, I2C_ADDR_ERROR_STR);
  1193. ESP_RETURN_ON_FALSE(cmd_handle != NULL, ESP_ERR_INVALID_ARG, I2C_TAG, I2C_CMD_LINK_INIT_ERR_STR);
  1194. ESP_RETURN_ON_FALSE(ack < I2C_MASTER_ACK_MAX, ESP_ERR_INVALID_ARG, I2C_TAG, I2C_ACK_TYPE_ERR_STR);
  1195. ESP_RETURN_ON_FALSE(data_len > 0, ESP_ERR_INVALID_ARG, I2C_TAG, I2C_DATA_LEN_ERR_STR);
  1196. esp_err_t ret = ESP_OK;
  1197. /* Check if we can perform a single transfer.
  1198. * This is the case if a NACK is NOT required at the end of the last transferred byte
  1199. * (i.e. ACK is required at the end), or if a single byte has to be read.
  1200. */
  1201. if (ack != I2C_MASTER_LAST_NACK) {
  1202. ret = i2c_master_read_static(cmd_handle, data, data_len, ack);
  1203. } else if (data_len == 1) {
  1204. ret = i2c_master_read_byte(cmd_handle, data, I2C_MASTER_NACK);
  1205. } else {
  1206. /* In this case, we have to read data_len-1 bytes sending an ACK at the end
  1207. * of each one.
  1208. */
  1209. ret = i2c_master_read_static(cmd_handle, data, data_len - 1, I2C_MASTER_ACK);
  1210. /* Last byte has to be NACKed. */
  1211. if (ret == ESP_OK) {
  1212. ret = i2c_master_read_byte(cmd_handle, data + data_len - 1, I2C_MASTER_NACK);
  1213. }
  1214. }
  1215. return ret;
  1216. }
  1217. static inline bool i2c_cmd_is_single_byte(const i2c_cmd_t *cmd) {
  1218. return cmd->total_bytes == 1;
  1219. }
  1220. static void IRAM_ATTR i2c_master_cmd_begin_static(i2c_port_t i2c_num, portBASE_TYPE* HPTaskAwoken)
  1221. {
  1222. i2c_obj_t *p_i2c = p_i2c_obj[i2c_num];
  1223. i2c_cmd_evt_t evt = { 0 };
  1224. if (p_i2c->cmd_link.head != NULL && p_i2c->status == I2C_STATUS_READ) {
  1225. i2c_cmd_t *cmd = &p_i2c->cmd_link.head->cmd;
  1226. i2c_hal_read_rxfifo(&(i2c_context[i2c_num].hal), cmd->data + cmd->bytes_used, p_i2c->rx_cnt);
  1227. /* rx_cnt bytes have just been read, increment the number of bytes used from the buffer */
  1228. cmd->bytes_used += p_i2c->rx_cnt;
  1229. /* Test if there are still some remaining bytes to send. */
  1230. if (cmd->bytes_used != cmd->total_bytes) {
  1231. p_i2c->cmd_idx = 0;
  1232. } else {
  1233. p_i2c->cmd_link.head = p_i2c->cmd_link.head->next;
  1234. if (p_i2c->cmd_link.head != NULL) {
  1235. p_i2c->cmd_link.head->cmd.bytes_used = 0;
  1236. }
  1237. }
  1238. } else if ((p_i2c->status == I2C_STATUS_ACK_ERROR)
  1239. || (p_i2c->status == I2C_STATUS_TIMEOUT)) {
  1240. assert(HPTaskAwoken != NULL);
  1241. evt.type = I2C_CMD_EVT_DONE;
  1242. xQueueOverwriteFromISR(p_i2c->cmd_evt_queue, &evt, HPTaskAwoken);
  1243. return;
  1244. } else if (p_i2c->status == I2C_STATUS_DONE) {
  1245. return;
  1246. }
  1247. if (p_i2c->cmd_link.head == NULL) {
  1248. assert(HPTaskAwoken != NULL);
  1249. p_i2c->cmd_link.cur = NULL;
  1250. evt.type = I2C_CMD_EVT_DONE;
  1251. xQueueOverwriteFromISR(p_i2c->cmd_evt_queue, &evt, HPTaskAwoken);
  1252. // Return to the IDLE status after cmd_eve_done signal were send out.
  1253. p_i2c->status = I2C_STATUS_IDLE;
  1254. return;
  1255. }
  1256. const i2c_hw_cmd_t hw_end_cmd = {
  1257. .op_code = I2C_LL_CMD_END
  1258. };
  1259. while (p_i2c->cmd_link.head) {
  1260. i2c_cmd_t *cmd = &p_i2c->cmd_link.head->cmd;
  1261. const size_t remaining_bytes = cmd->total_bytes - cmd->bytes_used;
  1262. i2c_hw_cmd_t hw_cmd = cmd->hw_cmd;
  1263. uint8_t fifo_fill = 0;
  1264. if (cmd->hw_cmd.op_code == I2C_LL_CMD_WRITE) {
  1265. uint8_t *write_pr = NULL;
  1266. //TODO: to reduce interrupt number
  1267. if (!i2c_cmd_is_single_byte(cmd)) {
  1268. fifo_fill = MIN(remaining_bytes, SOC_I2C_FIFO_LEN);
  1269. /* cmd->data shall not be altered!
  1270. * Else it would not be possible to reuse the commands list. */
  1271. write_pr = cmd->data + cmd->bytes_used;
  1272. cmd->bytes_used += fifo_fill;
  1273. } else {
  1274. fifo_fill = 1;
  1275. /* `data_byte` field contains the data itself.
  1276. * NOTE: It is possible to get the correct data (and not 0s)
  1277. * because both Xtensa and RISC-V architectures used on ESP
  1278. * boards are little-endian.
  1279. */
  1280. write_pr = (uint8_t*) &cmd->data_byte;
  1281. }
  1282. hw_cmd.byte_num = fifo_fill;
  1283. i2c_hal_write_txfifo(&(i2c_context[i2c_num].hal), write_pr, fifo_fill);
  1284. i2c_hal_write_cmd_reg(&(i2c_context[i2c_num].hal), hw_cmd, p_i2c->cmd_idx);
  1285. i2c_hal_write_cmd_reg(&(i2c_context[i2c_num].hal), hw_end_cmd, p_i2c->cmd_idx + 1);
  1286. i2c_hal_enable_master_tx_it(&(i2c_context[i2c_num].hal));
  1287. p_i2c->cmd_idx = 0;
  1288. if (i2c_cmd_is_single_byte(cmd) || cmd->total_bytes == cmd->bytes_used) {
  1289. p_i2c->cmd_link.head = p_i2c->cmd_link.head->next;
  1290. if(p_i2c->cmd_link.head) {
  1291. p_i2c->cmd_link.head->cmd.bytes_used = 0;
  1292. }
  1293. }
  1294. p_i2c->status = I2C_STATUS_WRITE;
  1295. break;
  1296. } else if (cmd->hw_cmd.op_code == I2C_LL_CMD_READ) {
  1297. //TODO: to reduce interrupt number
  1298. fifo_fill = MIN(remaining_bytes, SOC_I2C_FIFO_LEN);
  1299. p_i2c->rx_cnt = fifo_fill;
  1300. hw_cmd.byte_num = fifo_fill;
  1301. i2c_hal_write_cmd_reg(&(i2c_context[i2c_num].hal), hw_cmd, p_i2c->cmd_idx);
  1302. i2c_hal_write_cmd_reg(&(i2c_context[i2c_num].hal), hw_end_cmd, p_i2c->cmd_idx + 1);
  1303. i2c_hal_enable_master_rx_it(&(i2c_context[i2c_num].hal));
  1304. p_i2c->status = I2C_STATUS_READ;
  1305. break;
  1306. } else {
  1307. i2c_hal_write_cmd_reg(&(i2c_context[i2c_num].hal), hw_cmd, p_i2c->cmd_idx);
  1308. }
  1309. p_i2c->cmd_idx++;
  1310. p_i2c->cmd_link.head = p_i2c->cmd_link.head->next;
  1311. if (p_i2c->cmd_link.head == NULL || p_i2c->cmd_idx >= 15) {
  1312. p_i2c->cmd_idx = 0;
  1313. break;
  1314. }
  1315. }
  1316. i2c_hal_update_config(&(i2c_context[i2c_num].hal));
  1317. i2c_hal_trans_start(&(i2c_context[i2c_num].hal));
  1318. return;
  1319. }
  1320. #if CONFIG_SPIRAM_USE_MALLOC
  1321. //Check whether read or write buffer in cmd_link is internal.
  1322. static bool is_cmd_link_buffer_internal(const i2c_cmd_link_t *link)
  1323. {
  1324. bool is_internal = true;
  1325. for (const i2c_cmd_link_t *cmd_link = link;
  1326. cmd_link != NULL && is_internal;
  1327. cmd_link = cmd_link->next)
  1328. {
  1329. /* A command node has a valid pointer if it is a read command or a write command with more than one byte. */
  1330. const bool data_pointer = (cmd_link->cmd.hw_cmd.op_code == I2C_LL_CMD_WRITE && !i2c_cmd_is_single_byte(&cmd_link->cmd))
  1331. || cmd_link->cmd.hw_cmd.op_code == I2C_LL_CMD_READ;
  1332. /* Check if the (non-NULL) pointer points to internal memory. */
  1333. is_internal &= !data_pointer || cmd_link->cmd.data == NULL || esp_ptr_internal(cmd_link->cmd.data);
  1334. }
  1335. return is_internal;
  1336. }
  1337. #endif
  1338. static uint8_t clear_bus_cnt[I2C_NUM_MAX] = { 0 };
  1339. esp_err_t i2c_master_cmd_begin(i2c_port_t i2c_num, i2c_cmd_handle_t cmd_handle, TickType_t ticks_to_wait)
  1340. {
  1341. ESP_RETURN_ON_FALSE(( i2c_num < I2C_NUM_MAX ), ESP_ERR_INVALID_ARG, I2C_TAG, I2C_NUM_ERROR_STR);
  1342. ESP_RETURN_ON_FALSE(p_i2c_obj[i2c_num] != NULL, ESP_ERR_INVALID_STATE, I2C_TAG, I2C_DRIVER_NOT_INSTALL_ERR_STR);
  1343. ESP_RETURN_ON_FALSE(p_i2c_obj[i2c_num]->mode == I2C_MODE_MASTER, ESP_ERR_INVALID_STATE, I2C_TAG, I2C_MASTER_MODE_ERR_STR);
  1344. ESP_RETURN_ON_FALSE(cmd_handle != NULL, ESP_ERR_INVALID_ARG, I2C_TAG, I2C_CMD_LINK_INIT_ERR_STR);
  1345. #if CONFIG_SPIRAM_USE_MALLOC
  1346. //If the i2c read or write buffer is not in internal RAM, we will return ESP_FAIL
  1347. //to avoid the ISR handler function crashing when the cache is disabled.
  1348. if ( (p_i2c_obj[i2c_num]->intr_alloc_flags & ESP_INTR_FLAG_IRAM) &&
  1349. !is_cmd_link_buffer_internal(((const i2c_cmd_desc_t *)cmd_handle)->head) ) {
  1350. ESP_LOGE(I2C_TAG, I2C_PSRAM_BUFFER_WARN_STR);
  1351. return ESP_ERR_INVALID_ARG;
  1352. }
  1353. #endif
  1354. // Sometimes when the FSM get stuck, the ACK_ERR interrupt will occur endlessly until we reset the FSM and clear bus.
  1355. esp_err_t ret = ESP_FAIL;
  1356. i2c_obj_t *p_i2c = p_i2c_obj[i2c_num];
  1357. TickType_t ticks_start = xTaskGetTickCount();
  1358. portBASE_TYPE res = xSemaphoreTake(p_i2c->cmd_mux, ticks_to_wait);
  1359. if (res == pdFALSE) {
  1360. return ESP_ERR_TIMEOUT;
  1361. }
  1362. #ifdef CONFIG_PM_ENABLE
  1363. esp_pm_lock_acquire(p_i2c->pm_lock);
  1364. #endif
  1365. xQueueReset(p_i2c->cmd_evt_queue);
  1366. if (p_i2c->status == I2C_STATUS_TIMEOUT
  1367. || i2c_hal_is_bus_busy(&(i2c_context[i2c_num].hal))) {
  1368. i2c_hw_fsm_reset(i2c_num);
  1369. clear_bus_cnt[i2c_num] = 0;
  1370. }
  1371. i2c_reset_tx_fifo(i2c_num);
  1372. i2c_reset_rx_fifo(i2c_num);
  1373. const i2c_cmd_desc_t *cmd = (const i2c_cmd_desc_t *) cmd_handle;
  1374. /* Before starting the transfer, resetset the number of bytes sent to 0.
  1375. * `i2c_master_cmd_begin_static` will also reset this field for each node
  1376. * while browsing the command list. */
  1377. cmd->head->cmd.bytes_used = 0;
  1378. p_i2c->cmd_link.free = cmd->free;
  1379. p_i2c->cmd_link.cur = cmd->cur;
  1380. p_i2c->cmd_link.head = cmd->head;
  1381. p_i2c->status = I2C_STATUS_IDLE;
  1382. p_i2c->cmd_idx = 0;
  1383. p_i2c->rx_cnt = 0;
  1384. i2c_reset_tx_fifo(i2c_num);
  1385. i2c_reset_rx_fifo(i2c_num);
  1386. // These two interrupts some times can not be cleared when the FSM gets stuck.
  1387. // so we disable them when these two interrupt occurs and re-enable them here.
  1388. i2c_hal_disable_intr_mask(&(i2c_context[i2c_num].hal), I2C_LL_INTR_MASK);
  1389. i2c_hal_clr_intsts_mask(&(i2c_context[i2c_num].hal), I2C_LL_INTR_MASK);
  1390. //start send commands, at most 32 bytes one time, isr handler will process the remaining commands.
  1391. i2c_master_cmd_begin_static(i2c_num, NULL);
  1392. // Wait event bits
  1393. i2c_cmd_evt_t evt;
  1394. while (1) {
  1395. TickType_t wait_time = xTaskGetTickCount();
  1396. if (wait_time - ticks_start > ticks_to_wait) { // out of time
  1397. wait_time = I2C_CMD_ALIVE_INTERVAL_TICK;
  1398. } else {
  1399. wait_time = ticks_to_wait - (wait_time - ticks_start);
  1400. if (wait_time < I2C_CMD_ALIVE_INTERVAL_TICK) {
  1401. wait_time = I2C_CMD_ALIVE_INTERVAL_TICK;
  1402. }
  1403. }
  1404. // In master mode, since we don't have an interrupt to detective bus error or FSM state, what we do here is to make
  1405. // sure the interrupt mechanism for master mode is still working.
  1406. // If the command sending is not finished and there is no interrupt any more, the bus is probably dead caused by external noise.
  1407. portBASE_TYPE evt_res = xQueueReceive(p_i2c->cmd_evt_queue, &evt, wait_time);
  1408. if (evt_res == pdTRUE) {
  1409. if (evt.type == I2C_CMD_EVT_DONE) {
  1410. if (p_i2c->status == I2C_STATUS_TIMEOUT) {
  1411. // If the I2C slave are powered off or the SDA/SCL are connected to ground, for example,
  1412. // I2C hw FSM would get stuck in wrong state, we have to reset the I2C module in this case.
  1413. i2c_hw_fsm_reset(i2c_num);
  1414. clear_bus_cnt[i2c_num] = 0;
  1415. ret = ESP_ERR_TIMEOUT;
  1416. } else if (p_i2c->status == I2C_STATUS_ACK_ERROR) {
  1417. clear_bus_cnt[i2c_num]++;
  1418. if (clear_bus_cnt[i2c_num] >= I2C_ACKERR_CNT_MAX) {
  1419. clear_bus_cnt[i2c_num] = 0;
  1420. i2c_hw_fsm_reset(i2c_num);
  1421. }
  1422. ret = ESP_FAIL;
  1423. } else {
  1424. ret = ESP_OK;
  1425. }
  1426. break;
  1427. }
  1428. if (evt.type == I2C_CMD_EVT_ALIVE) {
  1429. }
  1430. } else {
  1431. ret = ESP_ERR_TIMEOUT;
  1432. // If the I2C slave are powered off or the SDA/SCL are connected to ground, for example,
  1433. // I2C hw FSM would get stuck in wrong state, we have to reset the I2C module in this case.
  1434. i2c_hw_fsm_reset(i2c_num);
  1435. clear_bus_cnt[i2c_num] = 0;
  1436. break;
  1437. }
  1438. }
  1439. p_i2c->status = I2C_STATUS_DONE;
  1440. #ifdef CONFIG_PM_ENABLE
  1441. esp_pm_lock_release(p_i2c->pm_lock);
  1442. #endif
  1443. xSemaphoreGive(p_i2c->cmd_mux);
  1444. return ret;
  1445. }
  1446. #if SOC_I2C_SUPPORT_SLAVE
  1447. int i2c_slave_write_buffer(i2c_port_t i2c_num, const uint8_t *data, int size, TickType_t ticks_to_wait)
  1448. {
  1449. ESP_RETURN_ON_FALSE(( i2c_num < I2C_NUM_MAX ), ESP_FAIL, I2C_TAG, I2C_NUM_ERROR_STR);
  1450. ESP_RETURN_ON_FALSE(p_i2c_obj[i2c_num] != NULL, ESP_FAIL, I2C_TAG, I2C_DRIVER_ERR_STR);
  1451. ESP_RETURN_ON_FALSE((data != NULL), ESP_FAIL, I2C_TAG, I2C_ADDR_ERROR_STR);
  1452. ESP_RETURN_ON_FALSE(p_i2c_obj[i2c_num]->mode == I2C_MODE_SLAVE, ESP_FAIL, I2C_TAG, I2C_MODE_SLAVE_ERR_STR);
  1453. i2c_obj_t *p_i2c = p_i2c_obj[i2c_num];
  1454. portBASE_TYPE res;
  1455. int cnt = 0;
  1456. TickType_t ticks_end = xTaskGetTickCount() + ticks_to_wait;
  1457. res = xSemaphoreTake(p_i2c->slv_tx_mux, ticks_to_wait);
  1458. if (res == pdFALSE) {
  1459. return 0;
  1460. }
  1461. ticks_to_wait = ticks_end - xTaskGetTickCount();
  1462. res = xRingbufferSend(p_i2c->tx_ring_buf, data, size, ticks_to_wait);
  1463. if (res == pdFALSE) {
  1464. cnt = 0;
  1465. } else {
  1466. I2C_ENTER_CRITICAL(&(i2c_context[i2c_num].spinlock));
  1467. i2c_hal_enable_slave_tx_it(&(i2c_context[i2c_num].hal));
  1468. I2C_EXIT_CRITICAL(&(i2c_context[i2c_num].spinlock));
  1469. cnt = size;
  1470. }
  1471. xSemaphoreGive(p_i2c->slv_tx_mux);
  1472. return cnt;
  1473. }
  1474. int i2c_slave_read_buffer(i2c_port_t i2c_num, uint8_t *data, size_t max_size, TickType_t ticks_to_wait)
  1475. {
  1476. ESP_RETURN_ON_FALSE(( i2c_num < I2C_NUM_MAX ), ESP_FAIL, I2C_TAG, I2C_NUM_ERROR_STR);
  1477. ESP_RETURN_ON_FALSE(p_i2c_obj[i2c_num] != NULL, ESP_FAIL, I2C_TAG, I2C_DRIVER_ERR_STR);
  1478. ESP_RETURN_ON_FALSE((data != NULL), ESP_FAIL, I2C_TAG, I2C_ADDR_ERROR_STR);
  1479. ESP_RETURN_ON_FALSE(p_i2c_obj[i2c_num]->mode == I2C_MODE_SLAVE, ESP_FAIL, I2C_TAG, I2C_MODE_SLAVE_ERR_STR);
  1480. size_t size = 0;
  1481. size_t size_rem = max_size;
  1482. i2c_obj_t *p_i2c = p_i2c_obj[i2c_num];
  1483. if (xSemaphoreTake(p_i2c->slv_rx_mux, ticks_to_wait) == pdFALSE) {
  1484. return 0;
  1485. }
  1486. TickType_t ticks_rem = ticks_to_wait;
  1487. TickType_t ticks_end = xTaskGetTickCount() + ticks_to_wait;
  1488. I2C_ENTER_CRITICAL(&(i2c_context[i2c_num].spinlock));
  1489. i2c_hal_enable_slave_rx_it(&(i2c_context[i2c_num].hal));
  1490. I2C_EXIT_CRITICAL(&(i2c_context[i2c_num].spinlock));
  1491. while (size_rem && ticks_rem <= ticks_to_wait) {
  1492. uint8_t *pdata = (uint8_t *) xRingbufferReceiveUpTo(p_i2c->rx_ring_buf, &size, ticks_to_wait, size_rem);
  1493. if (pdata && size > 0) {
  1494. memcpy(data, pdata, size);
  1495. vRingbufferReturnItem(p_i2c->rx_ring_buf, pdata);
  1496. data += size;
  1497. size_rem -= size;
  1498. }
  1499. if (ticks_to_wait != portMAX_DELAY) {
  1500. ticks_rem = ticks_end - xTaskGetTickCount();
  1501. }
  1502. }
  1503. xSemaphoreGive(p_i2c->slv_rx_mux);
  1504. return max_size - size_rem;
  1505. }
  1506. #endif