i2c.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <string.h>
  14. #include <stdio.h>
  15. #include "esp_types.h"
  16. #include "esp_attr.h"
  17. #include "esp_intr_alloc.h"
  18. #include "esp_log.h"
  19. #include "malloc.h"
  20. #include "freertos/FreeRTOS.h"
  21. #include "freertos/semphr.h"
  22. #include "freertos/xtensa_api.h"
  23. #include "freertos/task.h"
  24. #include "freertos/ringbuf.h"
  25. #include "soc/i2c_periph.h"
  26. #include "soc/soc_memory_layout.h"
  27. #include "driver/i2c.h"
  28. #include "driver/gpio.h"
  29. #include "driver/periph_ctrl.h"
  30. #include "esp_pm.h"
  31. #include "sdkconfig.h"
  32. static const char* I2C_TAG = "i2c";
  33. #define I2C_CHECK(a, str, ret) if(!(a)) { \
  34. ESP_LOGE(I2C_TAG,"%s:%d (%s):%s", __FILE__, __LINE__, __FUNCTION__, str); \
  35. return (ret); \
  36. }
  37. static portMUX_TYPE i2c_spinlock[I2C_NUM_MAX] = {portMUX_INITIALIZER_UNLOCKED, portMUX_INITIALIZER_UNLOCKED};
  38. /* DRAM_ATTR is required to avoid I2C array placed in flash, due to accessed from ISR */
  39. static DRAM_ATTR i2c_dev_t* const I2C[I2C_NUM_MAX] = { &I2C0, &I2C1 };
  40. #define I2C_ENTER_CRITICAL_ISR(mux) portENTER_CRITICAL_ISR(mux)
  41. #define I2C_EXIT_CRITICAL_ISR(mux) portEXIT_CRITICAL_ISR(mux)
  42. #define I2C_ENTER_CRITICAL(mux) portENTER_CRITICAL(mux)
  43. #define I2C_EXIT_CRITICAL(mux) portEXIT_CRITICAL(mux)
  44. #define I2C_DRIVER_ERR_STR "i2c driver install error"
  45. #define I2C_DRIVER_MALLOC_ERR_STR "i2c driver malloc error"
  46. #define I2C_NUM_ERROR_STR "i2c number error"
  47. #define I2C_TIMEING_VAL_ERR_STR "i2c timing value error"
  48. #define I2C_ADDR_ERROR_STR "i2c null address error"
  49. #define I2C_DRIVER_NOT_INSTALL_ERR_STR "i2c driver not installed"
  50. #define I2C_SLAVE_BUFFER_LEN_ERR_STR "i2c buffer size too small for slave mode"
  51. #define I2C_EVT_QUEUE_ERR_STR "i2c evt queue error"
  52. #define I2C_SEM_ERR_STR "i2c semaphore error"
  53. #define I2C_BUF_ERR_STR "i2c ringbuffer error"
  54. #define I2C_MASTER_MODE_ERR_STR "Only allowed in master mode"
  55. #define I2C_MODE_SLAVE_ERR_STR "Only allowed in slave mode"
  56. #define I2C_CMD_MALLOC_ERR_STR "i2c command link malloc error"
  57. #define I2C_TRANS_MODE_ERR_STR "i2c trans mode error"
  58. #define I2C_MODE_ERR_STR "i2c mode error"
  59. #define I2C_SDA_IO_ERR_STR "sda gpio number error"
  60. #define I2C_SCL_IO_ERR_STR "scl gpio number error"
  61. #define I2C_CMD_LINK_INIT_ERR_STR "i2c command link error"
  62. #define I2C_GPIO_PULLUP_ERR_STR "this i2c pin does not support internal pull-up"
  63. #define I2C_ACK_TYPE_ERR_STR "i2c ack type error"
  64. #define I2C_DATA_LEN_ERR_STR "i2c data read length error"
  65. #define I2C_PSRAM_BUFFER_WARN_STR "Using buffer allocated from psram"
  66. #define I2C_LOCK_ERR_STR "Power lock creation error"
  67. #define I2C_FIFO_FULL_THRESH_VAL (28)
  68. #define I2C_FIFO_EMPTY_THRESH_VAL (5)
  69. #define I2C_IO_INIT_LEVEL (1)
  70. #define I2C_CMD_ALIVE_INTERVAL_TICK (1000 / portTICK_PERIOD_MS)
  71. #define I2C_CMD_EVT_ALIVE (0)
  72. #define I2C_CMD_EVT_DONE (1)
  73. #define I2C_EVT_QUEUE_LEN (1)
  74. #define I2C_SLAVE_TIMEOUT_DEFAULT (32000) /* I2C slave timeout value, APB clock cycle number */
  75. #define I2C_SLAVE_SDA_SAMPLE_DEFAULT (10) /* I2C slave sample time after scl positive edge default value */
  76. #define I2C_SLAVE_SDA_HOLD_DEFAULT (10) /* I2C slave hold time after scl negative edge default value */
  77. #define I2C_MASTER_TOUT_CNUM_DEFAULT (8) /* I2C master timeout cycle number of I2C clock, after which the timeout interrupt will be triggered */
  78. #define I2C_ACKERR_CNT_MAX (10)
  79. #define I2C_FILTER_CYC_NUM_DEF (7) /* The number of apb cycles filtered by default*/
  80. #define I2C_CLR_BUS_SCL_NUM (9)
  81. #define I2C_CLR_BUS_HALF_PERIOD_US (5)
  82. typedef struct {
  83. uint8_t byte_num; /*!< cmd byte number */
  84. uint8_t ack_en; /*!< ack check enable */
  85. uint8_t ack_exp; /*!< expected ack level to get */
  86. uint8_t ack_val; /*!< ack value to send */
  87. uint8_t* data; /*!< data address */
  88. uint8_t byte_cmd; /*!< to save cmd for one byte command mode */
  89. i2c_opmode_t op_code; /*!< hardware cmd type */
  90. } i2c_cmd_t;
  91. typedef typeof(I2C[0]->command[0]) i2c_hw_cmd_t;
  92. typedef struct i2c_cmd_link{
  93. i2c_cmd_t cmd; /*!< command in current cmd link */
  94. struct i2c_cmd_link *next; /*!< next cmd link */
  95. } i2c_cmd_link_t;
  96. typedef struct {
  97. i2c_cmd_link_t* head; /*!< head of the command link */
  98. i2c_cmd_link_t* cur; /*!< last node of the command link */
  99. i2c_cmd_link_t* free; /*!< the first node to free of the command link */
  100. } i2c_cmd_desc_t;
  101. typedef enum {
  102. I2C_STATUS_READ, /*!< read status for current master command */
  103. I2C_STATUS_WRITE, /*!< write status for current master command */
  104. I2C_STATUS_IDLE, /*!< idle status for current master command */
  105. I2C_STATUS_ACK_ERROR, /*!< ack error status for current master command */
  106. I2C_STATUS_DONE, /*!< I2C command done */
  107. I2C_STATUS_TIMEOUT, /*!< I2C bus status error, and operation timeout */
  108. } i2c_status_t;
  109. typedef struct {
  110. int type;
  111. } i2c_cmd_evt_t;
  112. typedef struct {
  113. int i2c_num; /*!< I2C port number */
  114. int mode; /*!< I2C mode, master or slave */
  115. intr_handle_t intr_handle; /*!< I2C interrupt handle*/
  116. int cmd_idx; /*!< record current command index, for master mode */
  117. int status; /*!< record current command status, for master mode */
  118. int rx_cnt; /*!< record current read index, for master mode */
  119. uint8_t data_buf[I2C_FIFO_LEN]; /*!< a buffer to store i2c fifo data */
  120. i2c_cmd_desc_t cmd_link; /*!< I2C command link */
  121. QueueHandle_t cmd_evt_queue; /*!< I2C command event queue */
  122. #if CONFIG_SPIRAM_USE_MALLOC
  123. uint8_t* evt_queue_storage; /*!< The buffer that will hold the items in the queue */
  124. int intr_alloc_flags; /*!< Used to allocate the interrupt */
  125. StaticQueue_t evt_queue_buffer; /*!< The buffer that will hold the queue structure*/
  126. #endif
  127. xSemaphoreHandle cmd_mux; /*!< semaphore to lock command process */
  128. #ifdef CONFIG_PM_ENABLE
  129. esp_pm_lock_handle_t pm_lock;
  130. #endif
  131. size_t tx_fifo_remain; /*!< tx fifo remain length, for master mode */
  132. size_t rx_fifo_remain; /*!< rx fifo remain length, for master mode */
  133. xSemaphoreHandle slv_rx_mux; /*!< slave rx buffer mux */
  134. xSemaphoreHandle slv_tx_mux; /*!< slave tx buffer mux */
  135. size_t rx_buf_length; /*!< rx buffer length */
  136. RingbufHandle_t rx_ring_buf; /*!< rx ringbuffer handler of slave mode */
  137. size_t tx_buf_length; /*!< tx buffer length */
  138. RingbufHandle_t tx_ring_buf; /*!< tx ringbuffer handler of slave mode */
  139. } i2c_obj_t;
  140. static i2c_obj_t *p_i2c_obj[I2C_NUM_MAX] = {0};
  141. static void i2c_isr_handler_default(void* arg);
  142. static void IRAM_ATTR i2c_master_cmd_begin_static(i2c_port_t i2c_num);
  143. static esp_err_t IRAM_ATTR i2c_hw_fsm_reset(i2c_port_t i2c_num);
  144. /*
  145. For i2c master mode, we don't need to use a buffer for the data, the APIs will execute the master commands
  146. and return after all of the commands have been sent out or when error occurs. So when we send master commands,
  147. we should free or modify the source data only after the i2c_master_cmd_begin function returns.
  148. For i2c slave mode, we need a data buffer to stash the sending and receiving data, because the hardware fifo
  149. has only 32 bytes.
  150. */
  151. 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,
  152. int intr_alloc_flags)
  153. {
  154. I2C_CHECK(i2c_num < I2C_NUM_MAX, I2C_NUM_ERROR_STR, ESP_ERR_INVALID_ARG);
  155. I2C_CHECK(mode == I2C_MODE_MASTER || ( slv_rx_buf_len > 100 || slv_tx_buf_len > 100 ), I2C_SLAVE_BUFFER_LEN_ERR_STR,
  156. ESP_ERR_INVALID_ARG);
  157. uint32_t intr_mask = 0;
  158. if (p_i2c_obj[i2c_num] == NULL) {
  159. #if !CONFIG_SPIRAM_USE_MALLOC
  160. p_i2c_obj[i2c_num] = (i2c_obj_t*) calloc(1, sizeof(i2c_obj_t));
  161. #else
  162. if( !(intr_alloc_flags & ESP_INTR_FLAG_IRAM) ) {
  163. p_i2c_obj[i2c_num] = (i2c_obj_t*) calloc(1, sizeof(i2c_obj_t));
  164. } else {
  165. p_i2c_obj[i2c_num] = (i2c_obj_t*) heap_caps_calloc(1, sizeof(i2c_obj_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
  166. }
  167. #endif
  168. if (p_i2c_obj[i2c_num] == NULL) {
  169. ESP_LOGE(I2C_TAG, I2C_DRIVER_MALLOC_ERR_STR);
  170. return ESP_FAIL;
  171. }
  172. i2c_obj_t* p_i2c = p_i2c_obj[i2c_num];
  173. p_i2c->i2c_num = i2c_num;
  174. p_i2c->mode = mode;
  175. p_i2c->cmd_idx = 0;
  176. p_i2c->rx_cnt = 0;
  177. p_i2c->status = I2C_STATUS_IDLE;
  178. #if CONFIG_SPIRAM_USE_MALLOC
  179. p_i2c->intr_alloc_flags = intr_alloc_flags;
  180. #endif
  181. p_i2c->rx_fifo_remain = I2C_FIFO_LEN;
  182. p_i2c->tx_fifo_remain = I2C_FIFO_LEN;
  183. if (mode == I2C_MODE_SLAVE) {
  184. //we only use ringbuffer for slave mode.
  185. if (slv_rx_buf_len > 0) {
  186. p_i2c->rx_ring_buf = xRingbufferCreate(slv_rx_buf_len, RINGBUF_TYPE_BYTEBUF);
  187. if (p_i2c->rx_ring_buf == NULL) {
  188. ESP_LOGE(I2C_TAG, I2C_BUF_ERR_STR);
  189. goto err;
  190. }
  191. p_i2c->rx_buf_length = slv_rx_buf_len;
  192. } else {
  193. p_i2c->rx_ring_buf = NULL;
  194. p_i2c->rx_buf_length = 0;
  195. }
  196. if (slv_tx_buf_len > 0) {
  197. p_i2c->tx_ring_buf = xRingbufferCreate(slv_tx_buf_len, RINGBUF_TYPE_BYTEBUF);
  198. if (p_i2c->tx_ring_buf == NULL) {
  199. ESP_LOGE(I2C_TAG, I2C_BUF_ERR_STR);
  200. goto err;
  201. }
  202. p_i2c->tx_buf_length = slv_tx_buf_len;
  203. } else {
  204. p_i2c->tx_ring_buf = NULL;
  205. p_i2c->tx_buf_length = 0;
  206. }
  207. p_i2c->slv_rx_mux = xSemaphoreCreateMutex();
  208. p_i2c->slv_tx_mux = xSemaphoreCreateMutex();
  209. if (p_i2c->slv_rx_mux == NULL || p_i2c->slv_tx_mux == NULL) {
  210. ESP_LOGE(I2C_TAG, I2C_SEM_ERR_STR);
  211. goto err;
  212. }
  213. intr_mask |= ( I2C_RXFIFO_FULL_INT_ENA_M | I2C_TRANS_COMPLETE_INT_ENA_M);
  214. } else {
  215. //semaphore to sync sending process, because we only have 32 bytes for hardware fifo.
  216. p_i2c->cmd_mux = xSemaphoreCreateMutex();
  217. #ifdef CONFIG_PM_ENABLE
  218. if (esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "i2c_driver", &p_i2c->pm_lock) != ESP_OK) {
  219. ESP_LOGE(I2C_TAG, I2C_LOCK_ERR_STR);
  220. goto err;
  221. }
  222. #endif
  223. #if !CONFIG_SPIRAM_USE_MALLOC
  224. p_i2c->cmd_evt_queue = xQueueCreate(I2C_EVT_QUEUE_LEN, sizeof(i2c_cmd_evt_t));
  225. #else
  226. if( !(intr_alloc_flags & ESP_INTR_FLAG_IRAM) ) {
  227. p_i2c->cmd_evt_queue = xQueueCreate(I2C_EVT_QUEUE_LEN, sizeof(i2c_cmd_evt_t));
  228. } else {
  229. 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);
  230. if( p_i2c->evt_queue_storage == NULL ) {
  231. ESP_LOGE(I2C_TAG, I2C_DRIVER_MALLOC_ERR_STR);
  232. goto err;
  233. }
  234. memset(&p_i2c->evt_queue_buffer, 0, sizeof(StaticQueue_t));
  235. 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);
  236. }
  237. #endif
  238. if (p_i2c->cmd_mux == NULL || p_i2c->cmd_evt_queue == NULL) {
  239. ESP_LOGE(I2C_TAG, I2C_SEM_ERR_STR);
  240. goto err;
  241. }
  242. //command link
  243. p_i2c->cmd_link.cur = NULL;
  244. p_i2c->cmd_link.head = NULL;
  245. p_i2c->cmd_link.free = NULL;
  246. p_i2c->tx_ring_buf = NULL;
  247. p_i2c->rx_buf_length = 0;
  248. p_i2c->tx_ring_buf = NULL;
  249. p_i2c->tx_buf_length = 0;
  250. intr_mask |= I2C_ARBITRATION_LOST_INT_ENA_M | I2C_TIME_OUT_INT_ST_M;
  251. }
  252. } else {
  253. ESP_LOGE(I2C_TAG, I2C_DRIVER_ERR_STR);
  254. return ESP_FAIL;
  255. }
  256. //hook isr handler
  257. i2c_isr_register(i2c_num, i2c_isr_handler_default, p_i2c_obj[i2c_num], intr_alloc_flags, &p_i2c_obj[i2c_num]->intr_handle);
  258. intr_mask |= ( I2C_TRANS_COMPLETE_INT_ENA_M |
  259. I2C_TRANS_START_INT_ENA_M |
  260. I2C_ACK_ERR_INT_ENA_M |
  261. I2C_RXFIFO_OVF_INT_ENA_M |
  262. I2C_SLAVE_TRAN_COMP_INT_ENA_M);
  263. I2C[i2c_num]->int_clr.val = intr_mask;
  264. I2C[i2c_num]->int_ena.val = intr_mask;
  265. return ESP_OK;
  266. err:
  267. //Some error has happened. Free/destroy all allocated things and return ESP_FAIL.
  268. if (p_i2c_obj[i2c_num]) {
  269. if (p_i2c_obj[i2c_num]->rx_ring_buf) {
  270. vRingbufferDelete(p_i2c_obj[i2c_num]->rx_ring_buf);
  271. p_i2c_obj[i2c_num]->rx_ring_buf = NULL;
  272. p_i2c_obj[i2c_num]->rx_buf_length = 0;
  273. }
  274. if (p_i2c_obj[i2c_num]->tx_ring_buf) {
  275. vRingbufferDelete(p_i2c_obj[i2c_num]->tx_ring_buf);
  276. p_i2c_obj[i2c_num]->tx_ring_buf = NULL;
  277. p_i2c_obj[i2c_num]->tx_buf_length = 0;
  278. }
  279. if (p_i2c_obj[i2c_num]->cmd_evt_queue) {
  280. vQueueDelete(p_i2c_obj[i2c_num]->cmd_evt_queue);
  281. p_i2c_obj[i2c_num]->cmd_evt_queue = NULL;
  282. }
  283. if (p_i2c_obj[i2c_num]->cmd_mux) {
  284. vSemaphoreDelete(p_i2c_obj[i2c_num]->cmd_mux);
  285. }
  286. if (p_i2c_obj[i2c_num]->slv_rx_mux) {
  287. vSemaphoreDelete(p_i2c_obj[i2c_num]->slv_rx_mux);
  288. }
  289. if (p_i2c_obj[i2c_num]->slv_tx_mux) {
  290. vSemaphoreDelete(p_i2c_obj[i2c_num]->slv_tx_mux);
  291. }
  292. #ifdef CONFIG_PM_ENABLE
  293. if (p_i2c_obj[i2c_num]->pm_lock) {
  294. esp_pm_lock_delete(p_i2c_obj[i2c_num]->pm_lock);
  295. p_i2c_obj[i2c_num]->pm_lock = NULL;
  296. }
  297. #endif
  298. #if CONFIG_SPIRAM_USE_MALLOC
  299. if (p_i2c_obj[i2c_num]->evt_queue_storage) {
  300. free(p_i2c_obj[i2c_num]->evt_queue_storage);
  301. p_i2c_obj[i2c_num]->evt_queue_storage = NULL;
  302. }
  303. #endif
  304. }
  305. free(p_i2c_obj[i2c_num]);
  306. p_i2c_obj[i2c_num] = NULL;
  307. return ESP_FAIL;
  308. }
  309. static esp_err_t i2c_hw_enable(i2c_port_t i2c_num)
  310. {
  311. if (i2c_num == I2C_NUM_0) {
  312. periph_module_enable(PERIPH_I2C0_MODULE);
  313. } else if (i2c_num == I2C_NUM_1) {
  314. periph_module_enable(PERIPH_I2C1_MODULE);
  315. }
  316. return ESP_OK;
  317. }
  318. static esp_err_t i2c_hw_disable(i2c_port_t i2c_num)
  319. {
  320. if (i2c_num == I2C_NUM_0) {
  321. periph_module_disable(PERIPH_I2C0_MODULE);
  322. } else if (i2c_num == I2C_NUM_1) {
  323. periph_module_disable(PERIPH_I2C1_MODULE);
  324. }
  325. return ESP_OK;
  326. }
  327. esp_err_t i2c_driver_delete(i2c_port_t i2c_num)
  328. {
  329. I2C_CHECK(i2c_num < I2C_NUM_MAX, I2C_NUM_ERROR_STR, ESP_ERR_INVALID_ARG);
  330. I2C_CHECK(p_i2c_obj[i2c_num] != NULL, I2C_DRIVER_ERR_STR, ESP_FAIL);
  331. i2c_obj_t* p_i2c = p_i2c_obj[i2c_num];
  332. I2C[i2c_num]->int_ena.val = 0;
  333. esp_intr_free(p_i2c->intr_handle);
  334. p_i2c->intr_handle = NULL;
  335. if (p_i2c->cmd_mux) {
  336. xSemaphoreTake(p_i2c->cmd_mux, portMAX_DELAY);
  337. vSemaphoreDelete(p_i2c->cmd_mux);
  338. }
  339. if (p_i2c_obj[i2c_num]->cmd_evt_queue) {
  340. vQueueDelete(p_i2c_obj[i2c_num]->cmd_evt_queue);
  341. p_i2c_obj[i2c_num]->cmd_evt_queue = NULL;
  342. }
  343. if (p_i2c->slv_rx_mux) {
  344. vSemaphoreDelete(p_i2c->slv_rx_mux);
  345. }
  346. if (p_i2c->slv_tx_mux) {
  347. vSemaphoreDelete(p_i2c->slv_tx_mux);
  348. }
  349. if (p_i2c->rx_ring_buf) {
  350. vRingbufferDelete(p_i2c->rx_ring_buf);
  351. p_i2c->rx_ring_buf = NULL;
  352. p_i2c->rx_buf_length = 0;
  353. }
  354. if (p_i2c->tx_ring_buf) {
  355. vRingbufferDelete(p_i2c->tx_ring_buf);
  356. p_i2c->tx_ring_buf = NULL;
  357. p_i2c->tx_buf_length = 0;
  358. }
  359. #ifdef CONFIG_PM_ENABLE
  360. if (p_i2c->pm_lock) {
  361. esp_pm_lock_delete(p_i2c->pm_lock);
  362. p_i2c->pm_lock = NULL;
  363. }
  364. #endif
  365. #if CONFIG_SPIRAM_USE_MALLOC
  366. if (p_i2c_obj[i2c_num]->evt_queue_storage) {
  367. free(p_i2c_obj[i2c_num]->evt_queue_storage);
  368. p_i2c_obj[i2c_num]->evt_queue_storage = NULL;
  369. }
  370. #endif
  371. free(p_i2c_obj[i2c_num]);
  372. p_i2c_obj[i2c_num] = NULL;
  373. i2c_hw_disable(i2c_num);
  374. return ESP_OK;
  375. }
  376. esp_err_t i2c_reset_tx_fifo(i2c_port_t i2c_num)
  377. {
  378. I2C_CHECK(i2c_num < I2C_NUM_MAX, I2C_NUM_ERROR_STR, ESP_ERR_INVALID_ARG);
  379. I2C_ENTER_CRITICAL(&i2c_spinlock[i2c_num]);
  380. I2C[i2c_num]->fifo_conf.tx_fifo_rst = 1;
  381. I2C[i2c_num]->fifo_conf.tx_fifo_rst = 0;
  382. I2C_EXIT_CRITICAL(&i2c_spinlock[i2c_num]);
  383. return ESP_OK;
  384. }
  385. esp_err_t i2c_reset_rx_fifo(i2c_port_t i2c_num)
  386. {
  387. I2C_CHECK(i2c_num < I2C_NUM_MAX, I2C_NUM_ERROR_STR, ESP_ERR_INVALID_ARG);
  388. I2C_ENTER_CRITICAL(&i2c_spinlock[i2c_num]);
  389. I2C[i2c_num]->fifo_conf.rx_fifo_rst = 1;
  390. I2C[i2c_num]->fifo_conf.rx_fifo_rst = 0;
  391. I2C_EXIT_CRITICAL(&i2c_spinlock[i2c_num]);
  392. return ESP_OK;
  393. }
  394. static void IRAM_ATTR i2c_isr_handler_default(void* arg)
  395. {
  396. i2c_obj_t* p_i2c = (i2c_obj_t*) arg;
  397. int i2c_num = p_i2c->i2c_num;
  398. uint32_t status = I2C[i2c_num]->int_status.val;
  399. int idx = 0;
  400. portBASE_TYPE HPTaskAwoken = pdFALSE;
  401. while (status != 0) {
  402. status = I2C[i2c_num]->int_status.val;
  403. if (status & I2C_TX_SEND_EMPTY_INT_ST_M) {
  404. I2C[i2c_num]->int_clr.tx_send_empty = 1;
  405. } else if (status & I2C_RX_REC_FULL_INT_ST_M) {
  406. I2C[i2c_num]->int_clr.rx_rec_full = 1;
  407. } else if (status & I2C_ACK_ERR_INT_ST_M) {
  408. I2C[i2c_num]->int_ena.ack_err = 0;
  409. I2C[i2c_num]->int_clr.ack_err = 1;
  410. if (p_i2c->mode == I2C_MODE_MASTER) {
  411. p_i2c_obj[i2c_num]->status = I2C_STATUS_ACK_ERROR;
  412. I2C[i2c_num]->int_clr.ack_err = 1;
  413. //get error ack value from slave device, stop the commands
  414. i2c_master_cmd_begin_static(i2c_num);
  415. }
  416. } else if (status & I2C_TRANS_START_INT_ST_M) {
  417. I2C[i2c_num]->int_clr.trans_start = 1;
  418. } else if (status & I2C_TIME_OUT_INT_ST_M) {
  419. I2C[i2c_num]->int_ena.time_out = 0;
  420. I2C[i2c_num]->int_clr.time_out = 1;
  421. p_i2c_obj[i2c_num]->status = I2C_STATUS_TIMEOUT;
  422. i2c_master_cmd_begin_static(i2c_num);
  423. } else if (status & I2C_TRANS_COMPLETE_INT_ST_M) {
  424. I2C[i2c_num]->int_clr.trans_complete = 1;
  425. if (p_i2c->mode == I2C_MODE_SLAVE) {
  426. int rx_fifo_cnt = I2C[i2c_num]->status_reg.rx_fifo_cnt;
  427. for (idx = 0; idx < rx_fifo_cnt; idx++) {
  428. #if CONFIG_IDF_TARGET_ESP32
  429. p_i2c->data_buf[idx] = I2C[i2c_num]->fifo_data.data;
  430. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  431. p_i2c->data_buf[idx] = READ_PERI_REG(I2C_DATA_APB_REG(i2c_num));
  432. #endif
  433. }
  434. xRingbufferSendFromISR(p_i2c->rx_ring_buf, p_i2c->data_buf, rx_fifo_cnt, &HPTaskAwoken);
  435. I2C[i2c_num]->int_clr.rx_fifo_full = 1;
  436. } else {
  437. // add check for unexcepted situations caused by noise.
  438. if (p_i2c->status != I2C_STATUS_ACK_ERROR && p_i2c->status != I2C_STATUS_IDLE) {
  439. i2c_master_cmd_begin_static(i2c_num);
  440. }
  441. }
  442. } else if (status & I2C_MASTER_TRAN_COMP_INT_ST_M) {
  443. I2C[i2c_num]->int_clr.master_tran_comp = 1;
  444. } else if (status & I2C_ARBITRATION_LOST_INT_ST_M) {
  445. I2C[i2c_num]->int_clr.arbitration_lost = 1;
  446. p_i2c_obj[i2c_num]->status = I2C_STATUS_TIMEOUT;
  447. i2c_master_cmd_begin_static(i2c_num);
  448. } else if (status & I2C_SLAVE_TRAN_COMP_INT_ST_M) {
  449. I2C[i2c_num]->int_clr.slave_tran_comp = 1;
  450. } else if (status & I2C_END_DETECT_INT_ST_M) {
  451. I2C[i2c_num]->int_ena.end_detect = 0;
  452. I2C[i2c_num]->int_clr.end_detect = 1;
  453. i2c_master_cmd_begin_static(i2c_num);
  454. } else if (status & I2C_RXFIFO_OVF_INT_ST_M) {
  455. I2C[i2c_num]->int_clr.rx_fifo_ovf = 1;
  456. } else if (status & I2C_TXFIFO_EMPTY_INT_ST_M) {
  457. int tx_fifo_rem = I2C_FIFO_LEN - I2C[i2c_num]->status_reg.tx_fifo_cnt;
  458. size_t size = 0;
  459. uint8_t *data = (uint8_t*) xRingbufferReceiveUpToFromISR(p_i2c->tx_ring_buf, &size, tx_fifo_rem);
  460. if (data) {
  461. for (idx = 0; idx < size; idx++) {
  462. WRITE_PERI_REG(I2C_DATA_APB_REG(i2c_num), data[idx]);
  463. }
  464. vRingbufferReturnItemFromISR(p_i2c->tx_ring_buf, data, &HPTaskAwoken);
  465. I2C[i2c_num]->int_ena.tx_fifo_empty = 1;
  466. I2C[i2c_num]->int_clr.tx_fifo_empty = 1;
  467. } else {
  468. I2C[i2c_num]->int_ena.tx_fifo_empty = 0;
  469. I2C[i2c_num]->int_clr.tx_fifo_empty = 1;
  470. }
  471. } else if (status & I2C_RXFIFO_FULL_INT_ST_M) {
  472. int rx_fifo_cnt = I2C[i2c_num]->status_reg.rx_fifo_cnt;
  473. for (idx = 0; idx < rx_fifo_cnt; idx++) {
  474. #if CONFIG_IDF_TARGET_ESP32
  475. p_i2c->data_buf[idx] = I2C[i2c_num]->fifo_data.data;
  476. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  477. p_i2c->data_buf[idx] = READ_PERI_REG(I2C_DATA_APB_REG(i2c_num));
  478. #endif
  479. }
  480. xRingbufferSendFromISR(p_i2c->rx_ring_buf, p_i2c->data_buf, rx_fifo_cnt, &HPTaskAwoken);
  481. I2C[i2c_num]->int_clr.rx_fifo_full = 1;
  482. } else {
  483. I2C[i2c_num]->int_clr.val = status;
  484. }
  485. }
  486. if (p_i2c->mode == I2C_MODE_MASTER) {
  487. i2c_cmd_evt_t evt;
  488. evt.type = I2C_CMD_EVT_ALIVE;
  489. xQueueSendFromISR(p_i2c->cmd_evt_queue, &evt, &HPTaskAwoken);
  490. }
  491. //We only need to check here if there is a high-priority task needs to be switched.
  492. if(HPTaskAwoken == pdTRUE) {
  493. portYIELD_FROM_ISR();
  494. }
  495. }
  496. 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)
  497. {
  498. I2C_CHECK(i2c_num < I2C_NUM_MAX, I2C_NUM_ERROR_STR, ESP_ERR_INVALID_ARG);
  499. I2C_CHECK(tx_trans_mode < I2C_DATA_MODE_MAX, I2C_TRANS_MODE_ERR_STR, ESP_ERR_INVALID_ARG);
  500. I2C_CHECK(rx_trans_mode < I2C_DATA_MODE_MAX, I2C_TRANS_MODE_ERR_STR, ESP_ERR_INVALID_ARG);
  501. I2C_ENTER_CRITICAL(&i2c_spinlock[i2c_num]);
  502. I2C[i2c_num]->ctr.rx_lsb_first = rx_trans_mode; //set rx data msb first
  503. I2C[i2c_num]->ctr.tx_lsb_first = tx_trans_mode; //set tx data msb first
  504. I2C_EXIT_CRITICAL(&i2c_spinlock[i2c_num]);
  505. return ESP_OK;
  506. }
  507. 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)
  508. {
  509. I2C_CHECK(i2c_num < I2C_NUM_MAX, I2C_NUM_ERROR_STR, ESP_ERR_INVALID_ARG);
  510. if (tx_trans_mode) {
  511. *tx_trans_mode = I2C[i2c_num]->ctr.tx_lsb_first;
  512. }
  513. if (rx_trans_mode) {
  514. *rx_trans_mode = I2C[i2c_num]->ctr.rx_lsb_first;
  515. }
  516. return ESP_OK;
  517. }
  518. /* Some slave device will die by accident and keep the SDA in low level,
  519. * in this case, master should send several clock to make the slave release the bus.
  520. * Slave mode of ESP32 might also get in wrong state that held the SDA low,
  521. * in this case, master device could send a stop signal to make esp32 slave release the bus.
  522. **/
  523. static esp_err_t i2c_master_clear_bus(i2c_port_t i2c_num)
  524. {
  525. I2C_CHECK(i2c_num < I2C_NUM_MAX, I2C_NUM_ERROR_STR, ESP_ERR_INVALID_ARG);
  526. #if CONFIG_IDF_TARGET_ESP32
  527. const int scl_half_period = I2C_CLR_BUS_HALF_PERIOD_US; // use standard 100kHz data rate
  528. int sda_in_sig = 0, scl_in_sig = 0;
  529. int i = 0;
  530. if (i2c_num == I2C_NUM_0) {
  531. sda_in_sig = I2CEXT0_SDA_IN_IDX;
  532. scl_in_sig = I2CEXT0_SCL_IN_IDX;
  533. } else if (i2c_num == I2C_NUM_1) {
  534. sda_in_sig = I2CEXT1_SDA_IN_IDX;
  535. scl_in_sig = I2CEXT1_SCL_IN_IDX;
  536. }
  537. int scl_io = GPIO.func_in_sel_cfg[scl_in_sig].func_sel;
  538. int sda_io = GPIO.func_in_sel_cfg[sda_in_sig].func_sel;
  539. I2C_CHECK((GPIO_IS_VALID_OUTPUT_GPIO(scl_io)), I2C_SCL_IO_ERR_STR, ESP_ERR_INVALID_ARG);
  540. I2C_CHECK((GPIO_IS_VALID_OUTPUT_GPIO(sda_io)), I2C_SDA_IO_ERR_STR, ESP_ERR_INVALID_ARG);
  541. gpio_set_direction(scl_io, GPIO_MODE_OUTPUT_OD);
  542. gpio_set_direction(sda_io, GPIO_MODE_INPUT_OUTPUT_OD);
  543. // If a SLAVE device was in a read operation when the bus was interrupted, the SLAVE device is controlling SDA.
  544. // The only bit during the 9 clock cycles of a READ byte the MASTER(ESP32) is guaranteed control over is during the ACK bit
  545. // period. If the slave is sending a stream of ZERO bytes, it will only release SDA during the ACK bit period.
  546. // So, this reset code needs to synchronize the bit stream with, Either, the ACK bit, Or a 1 bit to correctly generate
  547. // a STOP condition.
  548. gpio_set_level(scl_io, 0);
  549. gpio_set_level(sda_io, 1);
  550. ets_delay_us(scl_half_period);
  551. while(!gpio_get_level(sda_io) && (i++ < I2C_CLR_BUS_SCL_NUM)) {
  552. gpio_set_level(scl_io, 1);
  553. ets_delay_us(scl_half_period);
  554. gpio_set_level(scl_io, 0);
  555. ets_delay_us(scl_half_period);
  556. }
  557. gpio_set_level(sda_io,0); // setup for STOP
  558. gpio_set_level(scl_io,1);
  559. ets_delay_us(scl_half_period);
  560. gpio_set_level(sda_io, 1); // STOP, SDA low -> high while SCL is HIGH
  561. i2c_set_pin(i2c_num, sda_io, scl_io, 1, 1, I2C_MODE_MASTER);
  562. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  563. I2C[i2c_num]->scl_sp_conf.scl_rst_slv_num = 9;
  564. I2C[i2c_num]->scl_sp_conf.scl_rst_slv_en = 1;
  565. #endif
  566. return ESP_OK;
  567. }
  568. /**if the power and SDA/SCL wires are in proper condition, everything works find with reading the slave.
  569. * If we remove the power supply for the slave during I2C is reading, or directly connect SDA or SCL to ground,
  570. * this would cause the I2C FSM get stuck in wrong state, all we can do is to reset the I2C hardware in this case.
  571. **/
  572. static esp_err_t i2c_hw_fsm_reset(i2c_port_t i2c_num)
  573. {
  574. I2C_CHECK(i2c_num < I2C_NUM_MAX, I2C_NUM_ERROR_STR, ESP_ERR_INVALID_ARG);
  575. #if CONFIG_IDF_TARGET_ESP32
  576. uint32_t ctr = I2C[i2c_num]->ctr.val;
  577. uint32_t fifo_conf = I2C[i2c_num]->fifo_conf.val;
  578. uint32_t scl_low_period = I2C[i2c_num]->scl_low_period.val;
  579. uint32_t scl_high_period = I2C[i2c_num]->scl_high_period.val;
  580. uint32_t scl_start_hold = I2C[i2c_num]->scl_start_hold.val;
  581. uint32_t scl_rstart_setup = I2C[i2c_num]->scl_rstart_setup.val;
  582. uint32_t scl_stop_hold = I2C[i2c_num]->scl_stop_hold.val;
  583. uint32_t scl_stop_setup = I2C[i2c_num]->scl_stop_setup.val;
  584. uint32_t sda_hold = I2C[i2c_num]->sda_hold.val;
  585. uint32_t sda_sample = I2C[i2c_num]->sda_sample.val;
  586. uint32_t timeout = I2C[i2c_num]->timeout.val;
  587. uint32_t scl_filter_cfg = I2C[i2c_num]->scl_filter_cfg.val;
  588. uint32_t sda_filter_cfg = I2C[i2c_num]->sda_filter_cfg.val;
  589. uint32_t slave_addr = I2C[i2c_num]->slave_addr.val;
  590. //to reset the I2C hw module, we need re-enable the hw
  591. i2c_hw_disable(i2c_num);
  592. i2c_master_clear_bus(i2c_num);
  593. i2c_hw_enable(i2c_num);
  594. I2C[i2c_num]->ctr.val = ctr & (~I2C_TRANS_START_M);
  595. I2C[i2c_num]->fifo_conf.val = fifo_conf;
  596. I2C[i2c_num]->scl_low_period.val = scl_low_period;
  597. I2C[i2c_num]->scl_high_period.val = scl_high_period;
  598. I2C[i2c_num]->scl_start_hold.val = scl_start_hold;
  599. I2C[i2c_num]->scl_rstart_setup.val = scl_rstart_setup;
  600. I2C[i2c_num]->scl_stop_hold.val = scl_stop_hold;
  601. I2C[i2c_num]->scl_stop_setup.val = scl_stop_setup;
  602. I2C[i2c_num]->sda_hold.val = sda_hold;
  603. I2C[i2c_num]->sda_sample.val = sda_sample;
  604. I2C[i2c_num]->timeout.val = timeout;
  605. I2C[i2c_num]->scl_filter_cfg.val = scl_filter_cfg;
  606. I2C[i2c_num]->sda_filter_cfg.val = sda_filter_cfg;
  607. I2C[i2c_num]->slave_addr.val = slave_addr;
  608. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  609. i2c_master_clear_bus(i2c_num);
  610. I2C[i2c_num]->ctr.fsm_rst = 1;
  611. I2C[i2c_num]->ctr.fsm_rst = 0;
  612. I2C[i2c_num]->fifo_conf.rx_fifo_rst = 1;
  613. I2C[i2c_num]->fifo_conf.rx_fifo_rst = 0;
  614. I2C[i2c_num]->fifo_conf.tx_fifo_rst = 1;
  615. I2C[i2c_num]->fifo_conf.tx_fifo_rst = 0;
  616. #endif
  617. I2C[i2c_num]->int_ena.val = 0;
  618. uint32_t intr_mask = I2C_TRANS_COMPLETE_INT_ENA_M
  619. | I2C_TRANS_START_INT_ENA_M
  620. | I2C_ACK_ERR_INT_ENA_M
  621. | I2C_RXFIFO_OVF_INT_ENA_M
  622. | I2C_SLAVE_TRAN_COMP_INT_ENA_M
  623. | I2C_TIME_OUT_INT_ENA_M
  624. | I2C_RXFIFO_FULL_INT_ENA_M
  625. | I2C_ARBITRATION_LOST_INT_ENA_M;
  626. I2C[i2c_num]->int_clr.val = intr_mask;
  627. I2C[i2c_num]->int_ena.val = intr_mask;
  628. return ESP_OK;
  629. }
  630. esp_err_t i2c_param_config(i2c_port_t i2c_num, const i2c_config_t* i2c_conf)
  631. {
  632. I2C_CHECK(i2c_num < I2C_NUM_MAX, I2C_NUM_ERROR_STR, ESP_ERR_INVALID_ARG);
  633. I2C_CHECK(i2c_conf != NULL, I2C_ADDR_ERROR_STR, ESP_ERR_INVALID_ARG);
  634. I2C_CHECK(i2c_conf->mode < I2C_MODE_MAX, I2C_MODE_ERR_STR, ESP_ERR_INVALID_ARG);
  635. esp_err_t ret = i2c_set_pin(i2c_num, i2c_conf->sda_io_num, i2c_conf->scl_io_num,
  636. i2c_conf->sda_pullup_en, i2c_conf->scl_pullup_en, i2c_conf->mode);
  637. if (ret != ESP_OK) {
  638. return ret;
  639. }
  640. // Reset the I2C hardware in case there is a soft reboot.
  641. i2c_hw_disable(i2c_num);
  642. i2c_hw_enable(i2c_num);
  643. I2C_ENTER_CRITICAL(&i2c_spinlock[i2c_num]);
  644. I2C[i2c_num]->ctr.rx_lsb_first = I2C_DATA_MODE_MSB_FIRST; //set rx data msb first
  645. I2C[i2c_num]->ctr.tx_lsb_first = I2C_DATA_MODE_MSB_FIRST; //set tx data msb first
  646. I2C[i2c_num]->ctr.ms_mode = i2c_conf->mode; //mode for master or slave
  647. I2C[i2c_num]->ctr.sda_force_out = 1; // set open-drain output mode
  648. I2C[i2c_num]->ctr.scl_force_out = 1; // set open-drain output mode
  649. I2C[i2c_num]->ctr.sample_scl_level = 0; //sample at high level of clock
  650. #if CONFIG_IDF_TARGET_ESP32S2BETA
  651. I2C[i2c_num]->ctr.ref_always_on = 1;
  652. I2C[i2c_num]->sda_filter_cfg.val = 0;
  653. I2C[i2c_num]->scl_filter_cfg.val = 0;
  654. #endif
  655. if (i2c_conf->mode == I2C_MODE_SLAVE) { //slave mode
  656. I2C[i2c_num]->slave_addr.addr = i2c_conf->slave.slave_addr;
  657. I2C[i2c_num]->slave_addr.en_10bit = i2c_conf->slave.addr_10bit_en;
  658. I2C[i2c_num]->fifo_conf.nonfifo_en = 0;
  659. I2C[i2c_num]->fifo_conf.fifo_addr_cfg_en = 0;
  660. I2C[i2c_num]->fifo_conf.rx_fifo_full_thrhd = I2C_FIFO_FULL_THRESH_VAL;
  661. I2C[i2c_num]->fifo_conf.tx_fifo_empty_thrhd = I2C_FIFO_EMPTY_THRESH_VAL;
  662. I2C[i2c_num]->ctr.trans_start = 0;
  663. I2C[i2c_num]->timeout.tout = I2C_SLAVE_TIMEOUT_DEFAULT;
  664. //set timing for data
  665. I2C[i2c_num]->sda_hold.time = I2C_SLAVE_SDA_HOLD_DEFAULT;
  666. I2C[i2c_num]->sda_sample.time = I2C_SLAVE_SDA_SAMPLE_DEFAULT;
  667. } else {
  668. I2C[i2c_num]->fifo_conf.nonfifo_en = 0;
  669. int cycle = (I2C_APB_CLK_FREQ / i2c_conf->master.clk_speed);
  670. int half_cycle = cycle / 2;
  671. I2C[i2c_num]->timeout.tout = cycle * I2C_MASTER_TOUT_CNUM_DEFAULT;
  672. //set timing for data
  673. I2C[i2c_num]->sda_hold.time = half_cycle / 2;
  674. #if CONFIG_IDF_TARGET_ESP32
  675. I2C[i2c_num]->sda_sample.time = half_cycle / 2;
  676. I2C[i2c_num]->scl_low_period.period = half_cycle;
  677. I2C[i2c_num]->scl_high_period.period = half_cycle;
  678. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  679. int low_period = half_cycle;
  680. int wait_high = 0;
  681. int high_period = cycle - low_period - wait_high;
  682. I2C[i2c_num]->sda_sample.time = high_period / 2;
  683. I2C[i2c_num]->scl_low_period.period = low_period;
  684. I2C[i2c_num]->scl_high_period.period = high_period;
  685. I2C[i2c_num]->scl_high_period.scl_wait_high_period = wait_high;
  686. #endif
  687. //set timing for start signal
  688. I2C[i2c_num]->scl_start_hold.time = half_cycle;
  689. I2C[i2c_num]->scl_rstart_setup.time = half_cycle;
  690. //set timing for stop signal
  691. I2C[i2c_num]->scl_stop_hold.time = half_cycle;
  692. I2C[i2c_num]->scl_stop_setup.time = half_cycle;
  693. //Default, we enable hardware filter
  694. i2c_filter_enable(i2c_num, I2C_FILTER_CYC_NUM_DEF);
  695. }
  696. I2C_EXIT_CRITICAL(&i2c_spinlock[i2c_num]);
  697. return ESP_OK;
  698. }
  699. esp_err_t i2c_set_period(i2c_port_t i2c_num, int high_period, int low_period)
  700. {
  701. I2C_CHECK(i2c_num < I2C_NUM_MAX, I2C_NUM_ERROR_STR, ESP_ERR_INVALID_ARG);
  702. I2C_CHECK((high_period <= I2C_SCL_HIGH_PERIOD_V) && (high_period > 0), I2C_TIMEING_VAL_ERR_STR, ESP_ERR_INVALID_ARG);
  703. I2C_CHECK((low_period <= I2C_SCL_LOW_PERIOD_V) && (low_period > 0), I2C_TIMEING_VAL_ERR_STR, ESP_ERR_INVALID_ARG);
  704. I2C_ENTER_CRITICAL(&i2c_spinlock[i2c_num]);
  705. I2C[i2c_num]->scl_high_period.period = high_period;
  706. I2C[i2c_num]->scl_low_period.period = low_period;
  707. I2C_EXIT_CRITICAL(&i2c_spinlock[i2c_num]);
  708. return ESP_OK;
  709. }
  710. esp_err_t i2c_get_period(i2c_port_t i2c_num, int* high_period, int* low_period)
  711. {
  712. I2C_CHECK(i2c_num < I2C_NUM_MAX, I2C_NUM_ERROR_STR, ESP_ERR_INVALID_ARG);
  713. I2C_ENTER_CRITICAL(&i2c_spinlock[i2c_num]);
  714. if (high_period) {
  715. *high_period = I2C[i2c_num]->scl_high_period.period;
  716. }
  717. if (low_period) {
  718. *low_period = I2C[i2c_num]->scl_low_period.period;
  719. }
  720. I2C_EXIT_CRITICAL(&i2c_spinlock[i2c_num]);
  721. return ESP_OK;
  722. }
  723. esp_err_t i2c_filter_enable(i2c_port_t i2c_num, uint8_t cyc_num)
  724. {
  725. I2C_CHECK(i2c_num < I2C_NUM_MAX, I2C_NUM_ERROR_STR, ESP_ERR_INVALID_ARG);
  726. I2C_ENTER_CRITICAL(&i2c_spinlock[i2c_num]);
  727. I2C[i2c_num]->scl_filter_cfg.thres = cyc_num;
  728. I2C[i2c_num]->sda_filter_cfg.thres = cyc_num;
  729. I2C[i2c_num]->scl_filter_cfg.en = 1;
  730. I2C[i2c_num]->sda_filter_cfg.en = 1;
  731. I2C_EXIT_CRITICAL(&i2c_spinlock[i2c_num]);
  732. return ESP_OK;
  733. }
  734. esp_err_t i2c_filter_disable(i2c_port_t i2c_num)
  735. {
  736. I2C_CHECK(i2c_num < I2C_NUM_MAX, I2C_NUM_ERROR_STR, ESP_ERR_INVALID_ARG);
  737. I2C_ENTER_CRITICAL(&i2c_spinlock[i2c_num]);
  738. I2C[i2c_num]->scl_filter_cfg.en = 0;
  739. I2C[i2c_num]->sda_filter_cfg.en = 0;
  740. I2C_EXIT_CRITICAL(&i2c_spinlock[i2c_num]);
  741. return ESP_OK;
  742. }
  743. esp_err_t i2c_set_start_timing(i2c_port_t i2c_num, int setup_time, int hold_time)
  744. {
  745. I2C_CHECK(i2c_num < I2C_NUM_MAX, I2C_NUM_ERROR_STR, ESP_ERR_INVALID_ARG);
  746. I2C_CHECK((hold_time <= I2C_SCL_START_HOLD_TIME_V) && (hold_time > 0), I2C_TIMEING_VAL_ERR_STR, ESP_ERR_INVALID_ARG);
  747. I2C_CHECK((setup_time <= I2C_SCL_RSTART_SETUP_TIME_V) && (setup_time > 0), I2C_TIMEING_VAL_ERR_STR, ESP_ERR_INVALID_ARG);
  748. I2C_ENTER_CRITICAL(&i2c_spinlock[i2c_num]);
  749. I2C[i2c_num]->scl_start_hold.time = hold_time;
  750. I2C[i2c_num]->scl_rstart_setup.time = setup_time;
  751. I2C_EXIT_CRITICAL(&i2c_spinlock[i2c_num]);
  752. return ESP_OK;
  753. }
  754. esp_err_t i2c_get_start_timing(i2c_port_t i2c_num, int* setup_time, int* hold_time)
  755. {
  756. I2C_CHECK(i2c_num < I2C_NUM_MAX, I2C_NUM_ERROR_STR, ESP_ERR_INVALID_ARG);
  757. I2C_ENTER_CRITICAL(&i2c_spinlock[i2c_num]);
  758. if (hold_time) {
  759. *hold_time = I2C[i2c_num]->scl_start_hold.time;
  760. }
  761. if (setup_time) {
  762. *setup_time = I2C[i2c_num]->scl_rstart_setup.time;
  763. }
  764. I2C_EXIT_CRITICAL(&i2c_spinlock[i2c_num]);
  765. return ESP_OK;
  766. }
  767. esp_err_t i2c_set_stop_timing(i2c_port_t i2c_num, int setup_time, int hold_time)
  768. {
  769. I2C_CHECK(i2c_num < I2C_NUM_MAX, I2C_NUM_ERROR_STR, ESP_ERR_INVALID_ARG);
  770. I2C_CHECK((setup_time <= I2C_SCL_STOP_SETUP_TIME_V) && (setup_time > 0), I2C_TIMEING_VAL_ERR_STR, ESP_ERR_INVALID_ARG);
  771. I2C_CHECK((hold_time <= I2C_SCL_STOP_HOLD_TIME_V) && (hold_time > 0), I2C_TIMEING_VAL_ERR_STR, ESP_ERR_INVALID_ARG);
  772. I2C_ENTER_CRITICAL(&i2c_spinlock[i2c_num]);
  773. I2C[i2c_num]->scl_stop_hold.time = hold_time;
  774. I2C[i2c_num]->scl_stop_setup.time = setup_time;
  775. I2C_EXIT_CRITICAL(&i2c_spinlock[i2c_num]);
  776. return ESP_OK;
  777. }
  778. esp_err_t i2c_get_stop_timing(i2c_port_t i2c_num, int* setup_time, int* hold_time)
  779. {
  780. I2C_CHECK(i2c_num < I2C_NUM_MAX, I2C_NUM_ERROR_STR, ESP_ERR_INVALID_ARG);
  781. I2C_ENTER_CRITICAL(&i2c_spinlock[i2c_num]);
  782. if (setup_time) {
  783. *setup_time = I2C[i2c_num]->scl_stop_setup.time;
  784. }
  785. if (hold_time) {
  786. *hold_time = I2C[i2c_num]->scl_stop_hold.time;
  787. }
  788. I2C_EXIT_CRITICAL(&i2c_spinlock[i2c_num]);
  789. return ESP_OK;
  790. }
  791. esp_err_t i2c_set_data_timing(i2c_port_t i2c_num, int sample_time, int hold_time)
  792. {
  793. I2C_CHECK(i2c_num < I2C_NUM_MAX, I2C_NUM_ERROR_STR, ESP_ERR_INVALID_ARG);
  794. I2C_CHECK((sample_time <= I2C_SDA_SAMPLE_TIME_V) && (sample_time > 0), I2C_TIMEING_VAL_ERR_STR, ESP_ERR_INVALID_ARG);
  795. I2C_CHECK((hold_time <= I2C_SDA_HOLD_TIME_V) && (hold_time > 0), I2C_TIMEING_VAL_ERR_STR, ESP_ERR_INVALID_ARG);
  796. I2C_ENTER_CRITICAL(&i2c_spinlock[i2c_num]);
  797. I2C[i2c_num]->sda_hold.time = hold_time;
  798. I2C[i2c_num]->sda_sample.time = sample_time;
  799. I2C_EXIT_CRITICAL(&i2c_spinlock[i2c_num]);
  800. return ESP_OK;
  801. }
  802. esp_err_t i2c_get_data_timing(i2c_port_t i2c_num, int* sample_time, int* hold_time)
  803. {
  804. I2C_CHECK(i2c_num < I2C_NUM_MAX, I2C_NUM_ERROR_STR, ESP_ERR_INVALID_ARG);
  805. I2C_ENTER_CRITICAL(&i2c_spinlock[i2c_num]);
  806. if (sample_time) {
  807. *sample_time = I2C[i2c_num]->sda_sample.time;
  808. }
  809. if (hold_time) {
  810. *hold_time = I2C[i2c_num]->sda_hold.time;
  811. }
  812. I2C_EXIT_CRITICAL(&i2c_spinlock[i2c_num]);
  813. return ESP_OK;
  814. }
  815. esp_err_t i2c_set_timeout(i2c_port_t i2c_num, int timeout)
  816. {
  817. I2C_CHECK(i2c_num < I2C_NUM_MAX, I2C_NUM_ERROR_STR, ESP_ERR_INVALID_ARG);
  818. I2C_CHECK((timeout <= I2C_TIME_OUT_REG_V) && (timeout > 0), I2C_TIMEING_VAL_ERR_STR, ESP_ERR_INVALID_ARG);
  819. I2C_ENTER_CRITICAL(&i2c_spinlock[i2c_num]);
  820. I2C[i2c_num]->timeout.tout = timeout;
  821. I2C_EXIT_CRITICAL(&i2c_spinlock[i2c_num]);
  822. return ESP_OK;
  823. }
  824. esp_err_t i2c_get_timeout(i2c_port_t i2c_num, int* timeout)
  825. {
  826. I2C_CHECK(i2c_num < I2C_NUM_MAX, I2C_NUM_ERROR_STR, ESP_ERR_INVALID_ARG);
  827. if (timeout) {
  828. *timeout = I2C[i2c_num]->timeout.tout;
  829. }
  830. return ESP_OK;
  831. }
  832. esp_err_t i2c_isr_register(i2c_port_t i2c_num, void (*fn)(void*), void * arg, int intr_alloc_flags, intr_handle_t *handle)
  833. {
  834. I2C_CHECK(i2c_num < I2C_NUM_MAX, I2C_NUM_ERROR_STR, ESP_ERR_INVALID_ARG);
  835. I2C_CHECK(fn != NULL, I2C_ADDR_ERROR_STR, ESP_ERR_INVALID_ARG);
  836. esp_err_t ret;
  837. switch (i2c_num) {
  838. case I2C_NUM_1:
  839. ret = esp_intr_alloc(ETS_I2C_EXT1_INTR_SOURCE, intr_alloc_flags, fn, arg, handle);
  840. break;
  841. case I2C_NUM_0:
  842. default:
  843. ret = esp_intr_alloc(ETS_I2C_EXT0_INTR_SOURCE, intr_alloc_flags, fn, arg, handle);
  844. break;
  845. }
  846. return ret;
  847. }
  848. esp_err_t i2c_isr_free(intr_handle_t handle)
  849. {
  850. return esp_intr_free(handle);
  851. }
  852. esp_err_t i2c_set_pin(i2c_port_t i2c_num, int sda_io_num, int scl_io_num, gpio_pullup_t sda_pullup_en, gpio_pullup_t scl_pullup_en, i2c_mode_t mode)
  853. {
  854. I2C_CHECK(( i2c_num < I2C_NUM_MAX ), I2C_NUM_ERROR_STR, ESP_ERR_INVALID_ARG);
  855. I2C_CHECK(((sda_io_num < 0) || ((GPIO_IS_VALID_OUTPUT_GPIO(sda_io_num)))), I2C_SDA_IO_ERR_STR, ESP_ERR_INVALID_ARG);
  856. I2C_CHECK(scl_io_num < 0 ||
  857. (GPIO_IS_VALID_OUTPUT_GPIO(scl_io_num)) ||
  858. (GPIO_IS_VALID_GPIO(scl_io_num) && mode == I2C_MODE_SLAVE),
  859. I2C_SCL_IO_ERR_STR,
  860. ESP_ERR_INVALID_ARG);
  861. I2C_CHECK(sda_io_num < 0 ||
  862. (sda_pullup_en == GPIO_PULLUP_ENABLE && GPIO_IS_VALID_OUTPUT_GPIO(sda_io_num)) ||
  863. sda_pullup_en == GPIO_PULLUP_DISABLE, I2C_GPIO_PULLUP_ERR_STR, ESP_ERR_INVALID_ARG);
  864. I2C_CHECK(scl_io_num < 0 ||
  865. (scl_pullup_en == GPIO_PULLUP_ENABLE && GPIO_IS_VALID_OUTPUT_GPIO(scl_io_num)) ||
  866. scl_pullup_en == GPIO_PULLUP_DISABLE, I2C_GPIO_PULLUP_ERR_STR, ESP_ERR_INVALID_ARG);
  867. int sda_in_sig, sda_out_sig, scl_in_sig, scl_out_sig;
  868. switch (i2c_num) {
  869. case I2C_NUM_1:
  870. sda_out_sig = I2CEXT1_SDA_OUT_IDX;
  871. sda_in_sig = I2CEXT1_SDA_IN_IDX;
  872. scl_out_sig = I2CEXT1_SCL_OUT_IDX;
  873. scl_in_sig = I2CEXT1_SCL_IN_IDX;
  874. break;
  875. case I2C_NUM_0:
  876. default:
  877. sda_out_sig = I2CEXT0_SDA_OUT_IDX;
  878. sda_in_sig = I2CEXT0_SDA_IN_IDX;
  879. scl_out_sig = I2CEXT0_SCL_OUT_IDX;
  880. scl_in_sig = I2CEXT0_SCL_IN_IDX;
  881. break;
  882. }
  883. if (sda_io_num >= 0) {
  884. gpio_set_level(sda_io_num, I2C_IO_INIT_LEVEL);
  885. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[sda_io_num], PIN_FUNC_GPIO);
  886. gpio_set_direction(sda_io_num, GPIO_MODE_INPUT_OUTPUT_OD);
  887. if (sda_pullup_en == GPIO_PULLUP_ENABLE) {
  888. gpio_set_pull_mode(sda_io_num, GPIO_PULLUP_ONLY);
  889. } else {
  890. gpio_set_pull_mode(sda_io_num, GPIO_FLOATING);
  891. }
  892. gpio_matrix_out(sda_io_num, sda_out_sig, 0, 0);
  893. gpio_matrix_in(sda_io_num, sda_in_sig, 0);
  894. }
  895. if (scl_io_num >= 0) {
  896. gpio_set_level(scl_io_num, I2C_IO_INIT_LEVEL);
  897. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[scl_io_num], PIN_FUNC_GPIO);
  898. if (mode == I2C_MODE_MASTER) {
  899. gpio_set_direction(scl_io_num, GPIO_MODE_INPUT_OUTPUT_OD);
  900. gpio_matrix_out(scl_io_num, scl_out_sig, 0, 0);
  901. } else {
  902. gpio_set_direction(scl_io_num, GPIO_MODE_INPUT);
  903. }
  904. if (scl_pullup_en == GPIO_PULLUP_ENABLE) {
  905. gpio_set_pull_mode(scl_io_num, GPIO_PULLUP_ONLY);
  906. } else {
  907. gpio_set_pull_mode(scl_io_num, GPIO_FLOATING);
  908. }
  909. gpio_matrix_in(scl_io_num, scl_in_sig, 0);
  910. }
  911. return ESP_OK;
  912. }
  913. i2c_cmd_handle_t i2c_cmd_link_create(void)
  914. {
  915. #if !CONFIG_SPIRAM_USE_MALLOC
  916. i2c_cmd_desc_t* cmd_desc = (i2c_cmd_desc_t*) calloc(1, sizeof(i2c_cmd_desc_t));
  917. #else
  918. 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);
  919. #endif
  920. return (i2c_cmd_handle_t) cmd_desc;
  921. }
  922. void i2c_cmd_link_delete(i2c_cmd_handle_t cmd_handle)
  923. {
  924. if (cmd_handle == NULL) {
  925. return;
  926. }
  927. i2c_cmd_desc_t* cmd = (i2c_cmd_desc_t*) cmd_handle;
  928. while (cmd->free) {
  929. i2c_cmd_link_t* ptmp = cmd->free;
  930. cmd->free = cmd->free->next;
  931. free(ptmp);
  932. }
  933. cmd->cur = NULL;
  934. cmd->free = NULL;
  935. cmd->head = NULL;
  936. free(cmd_handle);
  937. return;
  938. }
  939. static esp_err_t i2c_cmd_link_append(i2c_cmd_handle_t cmd_handle, i2c_cmd_t* cmd)
  940. {
  941. i2c_cmd_desc_t* cmd_desc = (i2c_cmd_desc_t*) cmd_handle;
  942. if (cmd_desc->head == NULL) {
  943. #if !CONFIG_SPIRAM_USE_MALLOC
  944. cmd_desc->head = (i2c_cmd_link_t*) calloc(1, sizeof(i2c_cmd_link_t));
  945. #else
  946. cmd_desc->head = (i2c_cmd_link_t*) heap_caps_calloc(1, sizeof(i2c_cmd_link_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
  947. #endif
  948. if (cmd_desc->head == NULL) {
  949. ESP_LOGE(I2C_TAG, I2C_CMD_MALLOC_ERR_STR);
  950. goto err;
  951. }
  952. cmd_desc->cur = cmd_desc->head;
  953. cmd_desc->free = cmd_desc->head;
  954. } else {
  955. #if !CONFIG_SPIRAM_USE_MALLOC
  956. cmd_desc->cur->next = (i2c_cmd_link_t*) calloc(1, sizeof(i2c_cmd_link_t));
  957. #else
  958. cmd_desc->cur->next = (i2c_cmd_link_t*) heap_caps_calloc(1, sizeof(i2c_cmd_link_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
  959. #endif
  960. if (cmd_desc->cur->next == NULL) {
  961. ESP_LOGE(I2C_TAG, I2C_CMD_MALLOC_ERR_STR);
  962. goto err;
  963. }
  964. cmd_desc->cur = cmd_desc->cur->next;
  965. }
  966. memcpy((uint8_t*) &cmd_desc->cur->cmd, (uint8_t*) cmd, sizeof(i2c_cmd_t));
  967. cmd_desc->cur->next = NULL;
  968. return ESP_OK;
  969. err:
  970. return ESP_FAIL;
  971. }
  972. esp_err_t i2c_master_start(i2c_cmd_handle_t cmd_handle)
  973. {
  974. I2C_CHECK(cmd_handle != NULL, I2C_CMD_LINK_INIT_ERR_STR, ESP_ERR_INVALID_ARG);
  975. i2c_cmd_t cmd;
  976. cmd.ack_en = 0;
  977. cmd.ack_exp = 0;
  978. cmd.ack_val = 0;
  979. cmd.byte_num = 0;
  980. cmd.data = NULL;
  981. cmd.op_code = I2C_CMD_RESTART;
  982. return i2c_cmd_link_append(cmd_handle, &cmd);
  983. }
  984. esp_err_t i2c_master_stop(i2c_cmd_handle_t cmd_handle)
  985. {
  986. I2C_CHECK(cmd_handle != NULL, I2C_CMD_LINK_INIT_ERR_STR, ESP_ERR_INVALID_ARG);
  987. i2c_cmd_t cmd;
  988. cmd.ack_en = 0;
  989. cmd.ack_exp = 0;
  990. cmd.ack_val = 0;
  991. cmd.byte_num = 0;
  992. cmd.data = NULL;
  993. cmd.op_code = I2C_CMD_STOP;
  994. return i2c_cmd_link_append(cmd_handle, &cmd);
  995. }
  996. esp_err_t i2c_master_write(i2c_cmd_handle_t cmd_handle, uint8_t* data, size_t data_len, bool ack_en)
  997. {
  998. I2C_CHECK((data != NULL), I2C_ADDR_ERROR_STR, ESP_ERR_INVALID_ARG);
  999. I2C_CHECK(cmd_handle != NULL, I2C_CMD_LINK_INIT_ERR_STR, ESP_ERR_INVALID_ARG);
  1000. uint8_t len_tmp;
  1001. int data_offset = 0;
  1002. esp_err_t ret;
  1003. while (data_len > 0) {
  1004. len_tmp = data_len > 0xff ? 0xff : data_len;
  1005. data_len -= len_tmp;
  1006. i2c_cmd_t cmd;
  1007. cmd.ack_en = ack_en;
  1008. cmd.ack_exp = 0;
  1009. cmd.ack_val = 0;
  1010. cmd.byte_num = len_tmp;
  1011. cmd.op_code = I2C_CMD_WRITE;
  1012. cmd.data = data + data_offset;
  1013. ret = i2c_cmd_link_append(cmd_handle, &cmd);
  1014. data_offset += len_tmp;
  1015. if (ret != ESP_OK) {
  1016. return ret;
  1017. }
  1018. }
  1019. return ESP_OK;
  1020. }
  1021. esp_err_t i2c_master_write_byte(i2c_cmd_handle_t cmd_handle, uint8_t data, bool ack_en)
  1022. {
  1023. I2C_CHECK(cmd_handle != NULL, I2C_CMD_LINK_INIT_ERR_STR, ESP_ERR_INVALID_ARG);
  1024. i2c_cmd_t cmd;
  1025. cmd.ack_en = ack_en;
  1026. cmd.ack_exp = 0;
  1027. cmd.ack_val = 0;
  1028. cmd.byte_num = 1;
  1029. cmd.op_code = I2C_CMD_WRITE;
  1030. cmd.data = NULL;
  1031. cmd.byte_cmd = data;
  1032. return i2c_cmd_link_append(cmd_handle, &cmd);
  1033. }
  1034. 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)
  1035. {
  1036. int len_tmp;
  1037. int data_offset = 0;
  1038. esp_err_t ret;
  1039. while (data_len > 0) {
  1040. len_tmp = data_len > 0xff ? 0xff : data_len;
  1041. data_len -= len_tmp;
  1042. i2c_cmd_t cmd;
  1043. cmd.ack_en = 0;
  1044. cmd.ack_exp = 0;
  1045. cmd.ack_val = ack & 0x1;
  1046. cmd.byte_num = len_tmp;
  1047. cmd.op_code = I2C_CMD_READ;
  1048. cmd.data = data + data_offset;
  1049. ret = i2c_cmd_link_append(cmd_handle, &cmd);
  1050. data_offset += len_tmp;
  1051. if (ret != ESP_OK) {
  1052. return ret;
  1053. }
  1054. }
  1055. return ESP_OK;
  1056. }
  1057. esp_err_t i2c_master_read_byte(i2c_cmd_handle_t cmd_handle, uint8_t* data, i2c_ack_type_t ack)
  1058. {
  1059. I2C_CHECK((data != NULL), I2C_ADDR_ERROR_STR, ESP_ERR_INVALID_ARG);
  1060. I2C_CHECK(cmd_handle != NULL, I2C_CMD_LINK_INIT_ERR_STR, ESP_ERR_INVALID_ARG);
  1061. I2C_CHECK(ack < I2C_MASTER_ACK_MAX, I2C_ACK_TYPE_ERR_STR, ESP_ERR_INVALID_ARG);
  1062. i2c_cmd_t cmd;
  1063. cmd.ack_en = 0;
  1064. cmd.ack_exp = 0;
  1065. cmd.ack_val = ((ack == I2C_MASTER_LAST_NACK) ? I2C_MASTER_NACK : (ack & 0x1));
  1066. cmd.byte_num = 1;
  1067. cmd.op_code = I2C_CMD_READ;
  1068. cmd.data = data;
  1069. return i2c_cmd_link_append(cmd_handle, &cmd);
  1070. }
  1071. esp_err_t i2c_master_read(i2c_cmd_handle_t cmd_handle, uint8_t* data, size_t data_len, i2c_ack_type_t ack)
  1072. {
  1073. I2C_CHECK((data != NULL), I2C_ADDR_ERROR_STR, ESP_ERR_INVALID_ARG);
  1074. I2C_CHECK(cmd_handle != NULL, I2C_CMD_LINK_INIT_ERR_STR, ESP_ERR_INVALID_ARG);
  1075. I2C_CHECK(ack < I2C_MASTER_ACK_MAX, I2C_ACK_TYPE_ERR_STR, ESP_ERR_INVALID_ARG);
  1076. I2C_CHECK(data_len > 0, I2C_DATA_LEN_ERR_STR, ESP_ERR_INVALID_ARG);
  1077. if(ack != I2C_MASTER_LAST_NACK) {
  1078. return i2c_master_read_static(cmd_handle, data, data_len, ack);
  1079. } else {
  1080. if(data_len == 1) {
  1081. return i2c_master_read_byte(cmd_handle, data, I2C_MASTER_NACK);
  1082. } else {
  1083. esp_err_t ret;
  1084. if((ret = i2c_master_read_static(cmd_handle, data, data_len - 1, I2C_MASTER_ACK)) != ESP_OK) {
  1085. return ret;
  1086. }
  1087. return i2c_master_read_byte(cmd_handle, data + data_len - 1, I2C_MASTER_NACK);
  1088. }
  1089. }
  1090. }
  1091. static void IRAM_ATTR i2c_master_cmd_begin_static(i2c_port_t i2c_num)
  1092. {
  1093. i2c_obj_t* p_i2c = p_i2c_obj[i2c_num];
  1094. portBASE_TYPE HPTaskAwoken = pdFALSE;
  1095. i2c_cmd_evt_t evt;
  1096. //This should never happen
  1097. if (p_i2c->mode == I2C_MODE_SLAVE) {
  1098. return;
  1099. }
  1100. if (p_i2c->status == I2C_STATUS_DONE) {
  1101. return;
  1102. } else if ((p_i2c->status == I2C_STATUS_ACK_ERROR)
  1103. || (p_i2c->status == I2C_STATUS_TIMEOUT)) {
  1104. I2C[i2c_num]->int_ena.end_detect = 0;
  1105. I2C[i2c_num]->int_clr.end_detect = 1;
  1106. if(p_i2c->status == I2C_STATUS_TIMEOUT) {
  1107. I2C[i2c_num]->int_clr.time_out = 1;
  1108. I2C[i2c_num]->int_ena.val = 0;
  1109. }
  1110. evt.type = I2C_CMD_EVT_DONE;
  1111. xQueueOverwriteFromISR(p_i2c->cmd_evt_queue, &evt, &HPTaskAwoken);
  1112. if (HPTaskAwoken == pdTRUE) {
  1113. portYIELD_FROM_ISR();
  1114. }
  1115. return;
  1116. } else if (p_i2c->cmd_link.head != NULL && p_i2c->status == I2C_STATUS_READ) {
  1117. i2c_cmd_t *cmd = &p_i2c->cmd_link.head->cmd;
  1118. while (p_i2c->rx_cnt-- > 0) {
  1119. *cmd->data++ = READ_PERI_REG(I2C_DATA_APB_REG(i2c_num));
  1120. }
  1121. if (cmd->byte_num > 0) {
  1122. p_i2c->rx_fifo_remain = I2C_FIFO_LEN;
  1123. p_i2c->cmd_idx = 0;
  1124. } else {
  1125. p_i2c->cmd_link.head = p_i2c->cmd_link.head->next;
  1126. }
  1127. }
  1128. if (p_i2c->cmd_link.head == NULL) {
  1129. p_i2c->cmd_link.cur = NULL;
  1130. evt.type = I2C_CMD_EVT_DONE;
  1131. xQueueOverwriteFromISR(p_i2c->cmd_evt_queue, &evt, &HPTaskAwoken);
  1132. if (HPTaskAwoken == pdTRUE) {
  1133. portYIELD_FROM_ISR();
  1134. }
  1135. // Return to the IDLE status after cmd_eve_done signal were send out.
  1136. p_i2c->status = I2C_STATUS_IDLE;
  1137. return;
  1138. }
  1139. while (p_i2c->cmd_link.head) {
  1140. i2c_cmd_t *cmd = &p_i2c->cmd_link.head->cmd;
  1141. i2c_hw_cmd_t * const p_cur_hw_cmd = &I2C[i2c_num]->command[p_i2c->cmd_idx];
  1142. i2c_hw_cmd_t hw_cmd = {
  1143. .ack_en = cmd->ack_en,
  1144. .ack_exp = cmd->ack_exp,
  1145. .ack_val = cmd->ack_val,
  1146. .byte_num = cmd->byte_num,
  1147. .op_code = cmd->op_code
  1148. };
  1149. const i2c_hw_cmd_t hw_end_cmd = {
  1150. .op_code = I2C_CMD_END
  1151. };
  1152. if (cmd->op_code == I2C_CMD_WRITE) {
  1153. uint32_t wr_filled = 0;
  1154. //TODO: to reduce interrupt number
  1155. if (cmd->data) {
  1156. while (p_i2c->tx_fifo_remain > 0 && cmd->byte_num > 0) {
  1157. WRITE_PERI_REG(I2C_DATA_APB_REG(i2c_num), *cmd->data++);
  1158. p_i2c->tx_fifo_remain--;
  1159. cmd->byte_num--;
  1160. wr_filled++;
  1161. }
  1162. } else {
  1163. WRITE_PERI_REG(I2C_DATA_APB_REG(i2c_num), cmd->byte_cmd);
  1164. p_i2c->tx_fifo_remain--;
  1165. cmd->byte_num--;
  1166. wr_filled ++;
  1167. }
  1168. hw_cmd.byte_num = wr_filled;
  1169. *p_cur_hw_cmd = hw_cmd;
  1170. *(p_cur_hw_cmd + 1) = hw_end_cmd;
  1171. p_i2c->tx_fifo_remain = I2C_FIFO_LEN;
  1172. p_i2c->cmd_idx = 0;
  1173. if (cmd->byte_num > 0) {
  1174. } else {
  1175. p_i2c->cmd_link.head = p_i2c->cmd_link.head->next;
  1176. }
  1177. p_i2c->status = I2C_STATUS_WRITE;
  1178. break;
  1179. } else if(cmd->op_code == I2C_CMD_READ) {
  1180. //TODO: to reduce interrupt number
  1181. p_i2c->rx_cnt = cmd->byte_num > p_i2c->rx_fifo_remain ? p_i2c->rx_fifo_remain : cmd->byte_num;
  1182. cmd->byte_num -= p_i2c->rx_cnt;
  1183. hw_cmd.byte_num = p_i2c->rx_cnt;
  1184. hw_cmd.ack_val = cmd->ack_val;
  1185. *p_cur_hw_cmd = hw_cmd;
  1186. *(p_cur_hw_cmd + 1) = hw_end_cmd;
  1187. p_i2c->status = I2C_STATUS_READ;
  1188. break;
  1189. } else {
  1190. *p_cur_hw_cmd = hw_cmd;
  1191. }
  1192. p_i2c->cmd_idx++;
  1193. p_i2c->cmd_link.head = p_i2c->cmd_link.head->next;
  1194. if (p_i2c->cmd_link.head == NULL || p_i2c->cmd_idx >= 15) {
  1195. p_i2c->tx_fifo_remain = I2C_FIFO_LEN;
  1196. p_i2c->cmd_idx = 0;
  1197. break;
  1198. }
  1199. }
  1200. I2C[i2c_num]->int_clr.end_detect = 1;
  1201. I2C[i2c_num]->int_ena.end_detect = 1;
  1202. I2C[i2c_num]->ctr.trans_start = 0;
  1203. I2C[i2c_num]->ctr.trans_start = 1;
  1204. return;
  1205. }
  1206. #if CONFIG_SPIRAM_USE_MALLOC
  1207. //Check whether read or write buffer in cmd_link is internal.
  1208. static bool is_cmd_link_buffer_internal(i2c_cmd_link_t *link)
  1209. {
  1210. i2c_cmd_link_t* cmd_link = link;
  1211. while(cmd_link != NULL) {
  1212. if (cmd_link->cmd.op_code == I2C_CMD_WRITE || cmd_link->cmd.op_code == I2C_CMD_READ) {
  1213. if( cmd_link->cmd.data != NULL && !esp_ptr_internal(cmd_link->cmd.data)) {
  1214. return false;
  1215. }
  1216. }
  1217. cmd_link = cmd_link->next;
  1218. }
  1219. return true;
  1220. }
  1221. #endif
  1222. esp_err_t i2c_master_cmd_begin(i2c_port_t i2c_num, i2c_cmd_handle_t cmd_handle, TickType_t ticks_to_wait)
  1223. {
  1224. I2C_CHECK(( i2c_num < I2C_NUM_MAX ), I2C_NUM_ERROR_STR, ESP_ERR_INVALID_ARG);
  1225. I2C_CHECK(p_i2c_obj[i2c_num] != NULL, I2C_DRIVER_NOT_INSTALL_ERR_STR, ESP_ERR_INVALID_STATE);
  1226. I2C_CHECK(p_i2c_obj[i2c_num]->mode == I2C_MODE_MASTER, I2C_MASTER_MODE_ERR_STR, ESP_ERR_INVALID_STATE);
  1227. I2C_CHECK(cmd_handle != NULL, I2C_CMD_LINK_INIT_ERR_STR, ESP_ERR_INVALID_ARG);
  1228. #if CONFIG_SPIRAM_USE_MALLOC
  1229. //If the i2c read or write buffer is not in internal RAM, we will return ESP_FAIL
  1230. //to avoid the ISR handler function crashing when the cache is disabled.
  1231. if( (p_i2c_obj[i2c_num]->intr_alloc_flags & ESP_INTR_FLAG_IRAM) ) {
  1232. if( !is_cmd_link_buffer_internal(((i2c_cmd_desc_t*)cmd_handle)->head) ) {
  1233. ESP_LOGE(I2C_TAG, I2C_PSRAM_BUFFER_WARN_STR);
  1234. return ESP_ERR_INVALID_ARG;
  1235. }
  1236. }
  1237. #endif
  1238. // Sometimes when the FSM get stuck, the ACK_ERR interrupt will occur endlessly until we reset the FSM and clear bus.
  1239. static uint8_t clear_bus_cnt = 0;
  1240. esp_err_t ret = ESP_FAIL;
  1241. i2c_obj_t* p_i2c = p_i2c_obj[i2c_num];
  1242. portTickType ticks_start = xTaskGetTickCount();
  1243. portBASE_TYPE res = xSemaphoreTake(p_i2c->cmd_mux, ticks_to_wait);
  1244. #ifdef CONFIG_PM_ENABLE
  1245. esp_pm_lock_acquire(p_i2c->pm_lock);
  1246. #endif
  1247. if (res == pdFALSE) {
  1248. return ESP_ERR_TIMEOUT;
  1249. }
  1250. xQueueReset(p_i2c->cmd_evt_queue);
  1251. if (p_i2c->status == I2C_STATUS_TIMEOUT
  1252. || I2C[i2c_num]->status_reg.bus_busy == 1) {
  1253. i2c_hw_fsm_reset(i2c_num);
  1254. clear_bus_cnt = 0;
  1255. }
  1256. i2c_reset_tx_fifo(i2c_num);
  1257. i2c_reset_rx_fifo(i2c_num);
  1258. i2c_cmd_desc_t* cmd = (i2c_cmd_desc_t*) cmd_handle;
  1259. p_i2c->cmd_link.free = cmd->free;
  1260. p_i2c->cmd_link.cur = cmd->cur;
  1261. p_i2c->cmd_link.head = cmd->head;
  1262. p_i2c->status = I2C_STATUS_IDLE;
  1263. p_i2c->cmd_idx = 0;
  1264. p_i2c->rx_cnt = 0;
  1265. p_i2c->tx_fifo_remain = I2C_FIFO_LEN;
  1266. p_i2c->rx_fifo_remain = I2C_FIFO_LEN;
  1267. i2c_reset_tx_fifo(i2c_num);
  1268. i2c_reset_rx_fifo(i2c_num);
  1269. // These two interrupts some times can not be cleared when the FSM gets stuck.
  1270. // so we disable them when these two interrupt occurs and re-enable them here.
  1271. I2C[i2c_num]->int_ena.ack_err = 1;
  1272. I2C[i2c_num]->int_ena.time_out = 1;
  1273. //start send commands, at most 32 bytes one time, isr handler will process the remaining commands.
  1274. i2c_master_cmd_begin_static(i2c_num);
  1275. // Wait event bits
  1276. i2c_cmd_evt_t evt;
  1277. while (1) {
  1278. TickType_t wait_time = xTaskGetTickCount();
  1279. if (wait_time - ticks_start > ticks_to_wait) { // out of time
  1280. wait_time = I2C_CMD_ALIVE_INTERVAL_TICK;
  1281. } else {
  1282. wait_time = ticks_to_wait - (wait_time - ticks_start);
  1283. if (wait_time < I2C_CMD_ALIVE_INTERVAL_TICK) {
  1284. wait_time = I2C_CMD_ALIVE_INTERVAL_TICK;
  1285. }
  1286. }
  1287. // In master mode, since we don't have an interrupt to detective bus error or FSM state, what we do here is to make
  1288. // sure the interrupt mechanism for master mode is still working.
  1289. // If the command sending is not finished and there is no interrupt any more, the bus is probably dead caused by external noise.
  1290. portBASE_TYPE evt_res = xQueueReceive(p_i2c->cmd_evt_queue, &evt, wait_time);
  1291. if (evt_res == pdTRUE) {
  1292. if (evt.type == I2C_CMD_EVT_DONE) {
  1293. if (p_i2c->status == I2C_STATUS_TIMEOUT) {
  1294. // If the I2C slave are powered off or the SDA/SCL are connected to ground, for example,
  1295. // I2C hw FSM would get stuck in wrong state, we have to reset the I2C module in this case.
  1296. i2c_hw_fsm_reset(i2c_num);
  1297. clear_bus_cnt = 0;
  1298. ret = ESP_ERR_TIMEOUT;
  1299. } else if (p_i2c->status == I2C_STATUS_ACK_ERROR) {
  1300. clear_bus_cnt++;
  1301. if(clear_bus_cnt >= I2C_ACKERR_CNT_MAX) {
  1302. i2c_master_clear_bus(i2c_num);
  1303. clear_bus_cnt = 0;
  1304. }
  1305. ret = ESP_FAIL;
  1306. } else {
  1307. ret = ESP_OK;
  1308. }
  1309. break;
  1310. }
  1311. if (evt.type == I2C_CMD_EVT_ALIVE) {
  1312. }
  1313. } else {
  1314. ret = ESP_ERR_TIMEOUT;
  1315. // If the I2C slave are powered off or the SDA/SCL are connected to ground, for example,
  1316. // I2C hw FSM would get stuck in wrong state, we have to reset the I2C module in this case.
  1317. i2c_hw_fsm_reset(i2c_num);
  1318. clear_bus_cnt = 0;
  1319. break;
  1320. }
  1321. }
  1322. p_i2c->status = I2C_STATUS_DONE;
  1323. #ifdef CONFIG_PM_ENABLE
  1324. esp_pm_lock_release(p_i2c->pm_lock);
  1325. #endif
  1326. xSemaphoreGive(p_i2c->cmd_mux);
  1327. return ret;
  1328. }
  1329. int i2c_slave_write_buffer(i2c_port_t i2c_num, uint8_t* data, int size, TickType_t ticks_to_wait)
  1330. {
  1331. I2C_CHECK(( i2c_num < I2C_NUM_MAX ), I2C_NUM_ERROR_STR, ESP_FAIL);
  1332. I2C_CHECK((data != NULL), I2C_ADDR_ERROR_STR, ESP_FAIL);
  1333. I2C_CHECK(p_i2c_obj[i2c_num]->mode == I2C_MODE_SLAVE, I2C_MODE_SLAVE_ERR_STR, ESP_FAIL);
  1334. i2c_obj_t* p_i2c = p_i2c_obj[i2c_num];
  1335. portBASE_TYPE res;
  1336. int cnt = 0;
  1337. portTickType ticks_start = xTaskGetTickCount();
  1338. res = xSemaphoreTake(p_i2c->slv_tx_mux, ticks_to_wait);
  1339. if (res == pdFALSE) {
  1340. return 0;
  1341. }
  1342. TickType_t ticks_end = xTaskGetTickCount();
  1343. if (ticks_end - ticks_start > ticks_to_wait) {
  1344. ticks_to_wait = 0;
  1345. } else {
  1346. ticks_to_wait = ticks_to_wait - (ticks_end - ticks_start);
  1347. }
  1348. res = xRingbufferSend(p_i2c->tx_ring_buf, data, size, ticks_to_wait);
  1349. if (res == pdFALSE) {
  1350. cnt = 0;
  1351. } else {
  1352. I2C_ENTER_CRITICAL(&i2c_spinlock[i2c_num]);
  1353. I2C[i2c_num]->int_clr.tx_fifo_empty = 1;
  1354. I2C[i2c_num]->int_ena.tx_fifo_empty = 1;
  1355. I2C_EXIT_CRITICAL(&i2c_spinlock[i2c_num]);
  1356. cnt = size;
  1357. }
  1358. xSemaphoreGive(p_i2c->slv_tx_mux);
  1359. return cnt;
  1360. }
  1361. static int i2c_slave_read(i2c_port_t i2c_num, uint8_t* data, size_t max_size, TickType_t ticks_to_wait)
  1362. {
  1363. i2c_obj_t* p_i2c = p_i2c_obj[i2c_num];
  1364. size_t size = 0;
  1365. uint8_t* pdata = (uint8_t*) xRingbufferReceiveUpTo(p_i2c->rx_ring_buf, &size, ticks_to_wait, max_size);
  1366. if (pdata && size > 0) {
  1367. memcpy(data, pdata, size);
  1368. vRingbufferReturnItem(p_i2c->rx_ring_buf, pdata);
  1369. }
  1370. return size;
  1371. }
  1372. int i2c_slave_read_buffer(i2c_port_t i2c_num, uint8_t* data, size_t max_size, TickType_t ticks_to_wait)
  1373. {
  1374. I2C_CHECK(( i2c_num < I2C_NUM_MAX ), I2C_NUM_ERROR_STR, ESP_FAIL);
  1375. I2C_CHECK((data != NULL), I2C_ADDR_ERROR_STR, ESP_FAIL);
  1376. I2C_CHECK(p_i2c_obj[i2c_num]->mode == I2C_MODE_SLAVE, I2C_MODE_SLAVE_ERR_STR, ESP_FAIL);
  1377. i2c_obj_t* p_i2c = p_i2c_obj[i2c_num];
  1378. portBASE_TYPE res;
  1379. portTickType ticks_start = xTaskGetTickCount();
  1380. res = xSemaphoreTake(p_i2c->slv_rx_mux, ticks_to_wait);
  1381. if (res == pdFALSE) {
  1382. return 0;
  1383. }
  1384. TickType_t ticks_end = xTaskGetTickCount();
  1385. if (ticks_end - ticks_start > ticks_to_wait) {
  1386. ticks_to_wait = 0;
  1387. } else {
  1388. ticks_to_wait = ticks_to_wait - (ticks_end - ticks_start);
  1389. }
  1390. int cnt = i2c_slave_read(i2c_num, data, max_size, ticks_to_wait);
  1391. if (cnt > 0) {
  1392. I2C_ENTER_CRITICAL(&i2c_spinlock[i2c_num]);
  1393. I2C[i2c_num]->int_ena.rx_fifo_full = 1;
  1394. I2C_EXIT_CRITICAL(&i2c_spinlock[i2c_num]);
  1395. ticks_end = xTaskGetTickCount();
  1396. if (ticks_end - ticks_start > ticks_to_wait) {
  1397. ticks_to_wait = 0;
  1398. } else {
  1399. ticks_to_wait = ticks_to_wait - (ticks_end - ticks_start);
  1400. }
  1401. if (cnt < max_size && ticks_to_wait > 0) {
  1402. cnt += i2c_slave_read(i2c_num, data + cnt, max_size - cnt, ticks_to_wait);
  1403. }
  1404. } else {
  1405. cnt = 0;
  1406. }
  1407. xSemaphoreGive(p_i2c->slv_rx_mux);
  1408. return cnt;
  1409. }