ulp_riscv_i2c.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "ulp_riscv_i2c.h"
  7. #include "esp_check.h"
  8. #include "soc/rtc_i2c_reg.h"
  9. #include "soc/rtc_i2c_struct.h"
  10. #include "soc/rtc_io_struct.h"
  11. #include "soc/sens_reg.h"
  12. #include "soc/clk_tree_defs.h"
  13. #include "hal/i2c_ll.h"
  14. #include "driver/rtc_io.h"
  15. #include "freertos/FreeRTOS.h"
  16. #include "freertos/task.h"
  17. #include "sdkconfig.h"
  18. static const char *RTCI2C_TAG = "ulp_riscv_i2c";
  19. #define I2C_CTRL_SLAVE_ADDR_MASK (0xFF << 0)
  20. #define I2C_CTRL_SLAVE_REG_ADDR_MASK (0xFF << 11)
  21. #define I2C_CTRL_MASTER_TX_DATA_MASK (0xFF << 19)
  22. #if CONFIG_IDF_TARGET_ESP32S3
  23. #define ULP_I2C_CMD_RESTART 0 /*!<I2C restart command */
  24. #define ULP_I2C_CMD_WRITE 1 /*!<I2C write command */
  25. #define ULP_I2C_CMD_READ 2 /*!<I2C read command */
  26. #define ULP_I2C_CMD_STOP 3 /*!<I2C stop command */
  27. #define ULP_I2C_CMD_END 4 /*!<I2C end command */
  28. #else
  29. #define ULP_I2C_CMD_RESTART I2C_LL_CMD_RESTART /*!<I2C restart command */
  30. #define ULP_I2C_CMD_WRITE I2C_LL_CMD_WRITE /*!<I2C write command */
  31. #define ULP_I2C_CMD_READ I2C_LL_CMD_READ /*!<I2C read command */
  32. #define ULP_I2C_CMD_STOP I2C_LL_CMD_STOP /*!<I2C stop command */
  33. #define ULP_I2C_CMD_END I2C_LL_CMD_END /*!<I2C end command */
  34. #endif // CONFIG_IDF_TARGET_ESP32S3
  35. /* Use the register structure to access RTC_I2C and RTCIO module registers */
  36. rtc_i2c_dev_t *i2c_dev = &RTC_I2C;
  37. rtc_io_dev_t *rtc_io_dev = &RTCIO;
  38. #define MICROSEC_TO_RTC_FAST_CLK(period) (period) * ((float)(SOC_CLK_RC_FAST_FREQ_APPROX) / (1000000.0))
  39. /* Read/Write timeout (number of iterations)*/
  40. #define ULP_RISCV_I2C_RW_TIMEOUT CONFIG_ULP_RISCV_I2C_RW_TIMEOUT
  41. static esp_err_t i2c_gpio_is_cfg_valid(gpio_num_t sda_io_num, gpio_num_t scl_io_num)
  42. {
  43. /* Verify that the SDA and SCL GPIOs are valid RTC I2C io pins */
  44. ESP_RETURN_ON_ERROR(!rtc_gpio_is_valid_gpio(sda_io_num), RTCI2C_TAG, "RTC I2C SDA GPIO invalid");
  45. ESP_RETURN_ON_ERROR(!rtc_gpio_is_valid_gpio(scl_io_num), RTCI2C_TAG, "RTC I2C SCL GPIO invalid");
  46. /* Verify that the SDA and SCL line belong to the RTC IO I2C function group */
  47. if ((sda_io_num != GPIO_NUM_1) && (sda_io_num != GPIO_NUM_3)) {
  48. ESP_LOGE(RTCI2C_TAG, "SDA pin can only be configured as GPIO#1 or GPIO#3");
  49. return ESP_ERR_INVALID_ARG;
  50. }
  51. if ((scl_io_num != GPIO_NUM_0) && (scl_io_num != GPIO_NUM_2)) {
  52. ESP_LOGE(RTCI2C_TAG, "SCL pin can only be configured as GPIO#0 or GPIO#2");
  53. return ESP_ERR_INVALID_ARG;
  54. }
  55. return ESP_OK;
  56. }
  57. static esp_err_t i2c_configure_io(gpio_num_t io_num, bool pullup_en)
  58. {
  59. /* Initialize IO Pin */
  60. ESP_RETURN_ON_ERROR(rtc_gpio_init(io_num), RTCI2C_TAG, "RTC GPIO Init failed for GPIO %d", io_num);
  61. /* Set direction to input+output */
  62. ESP_RETURN_ON_ERROR(rtc_gpio_set_direction(io_num, RTC_GPIO_MODE_INPUT_OUTPUT), RTCI2C_TAG, "RTC GPIO Set direction failed for %d", io_num);
  63. /* Disable pulldown on the io pin */
  64. ESP_RETURN_ON_ERROR(rtc_gpio_pulldown_dis(io_num), RTCI2C_TAG, "RTC GPIO pulldown disable failed for %d", io_num);
  65. /* Enable pullup based on pullup_en flag */
  66. if (pullup_en) {
  67. ESP_RETURN_ON_ERROR(rtc_gpio_pullup_en(io_num), RTCI2C_TAG, "RTC GPIO pullup enable failed for %d", io_num);
  68. } else {
  69. ESP_RETURN_ON_ERROR(rtc_gpio_pullup_dis(io_num), RTCI2C_TAG, "RTC GPIO pullup disable failed for %d", io_num);
  70. }
  71. return ESP_OK;
  72. }
  73. static esp_err_t i2c_set_pin(const ulp_riscv_i2c_cfg_t *cfg)
  74. {
  75. gpio_num_t sda_io_num = cfg->i2c_pin_cfg.sda_io_num;
  76. gpio_num_t scl_io_num = cfg->i2c_pin_cfg.scl_io_num;
  77. bool sda_pullup_en = cfg->i2c_pin_cfg.sda_pullup_en;
  78. bool scl_pullup_en = cfg->i2c_pin_cfg.scl_pullup_en;
  79. /* Verify that the I2C GPIOs are valid */
  80. ESP_RETURN_ON_ERROR(i2c_gpio_is_cfg_valid(sda_io_num, scl_io_num), RTCI2C_TAG, "RTC I2C GPIO config invalid");
  81. /* Initialize SDA Pin */
  82. ESP_RETURN_ON_ERROR(i2c_configure_io(sda_io_num, sda_pullup_en), RTCI2C_TAG, "RTC I2C SDA pin config failed");
  83. /* Initialize SCL Pin */
  84. ESP_RETURN_ON_ERROR(i2c_configure_io(scl_io_num, scl_pullup_en), RTCI2C_TAG, "RTC I2C SCL pin config failed");
  85. /* Route SDA IO signal to the RTC subsystem */
  86. rtc_io_dev->touch_pad[sda_io_num].mux_sel = 1;
  87. /* Route SCL IO signal to the RTC subsystem */
  88. rtc_io_dev->touch_pad[scl_io_num].mux_sel = 1;
  89. /* Select RTC I2C function for SDA pin */
  90. rtc_io_dev->touch_pad[sda_io_num].fun_sel = 3;
  91. /* Select RTC I2C function for SCL pin */
  92. rtc_io_dev->touch_pad[scl_io_num].fun_sel = 3;
  93. /* Map the SDA and SCL signals to the RTC I2C controller */
  94. if (sda_io_num == GPIO_NUM_1) {
  95. rtc_io_dev->sar_i2c_io.sda_sel = 0;
  96. } else {
  97. rtc_io_dev->sar_i2c_io.sda_sel = 1;
  98. }
  99. if (scl_io_num == GPIO_NUM_0) {
  100. rtc_io_dev->sar_i2c_io.scl_sel = 0;
  101. } else {
  102. rtc_io_dev->sar_i2c_io.scl_sel = 1;
  103. }
  104. return ESP_OK;
  105. }
  106. static esp_err_t i2c_set_timing(const ulp_riscv_i2c_cfg_t *cfg)
  107. {
  108. /* Convert all timing parameters from micro-seconds to period in RTC_FAST_CLK cycles.
  109. * RTC_FAST_CLK = 8.5 MHz for esp32s2 and 17.5 MHz for esp32s3.
  110. * The following calculations approximate the period for each parameter.
  111. */
  112. float scl_low_period = MICROSEC_TO_RTC_FAST_CLK(cfg->i2c_timing_cfg.scl_low_period);
  113. float scl_high_period = MICROSEC_TO_RTC_FAST_CLK(cfg->i2c_timing_cfg.scl_high_period);
  114. float sda_duty_period = MICROSEC_TO_RTC_FAST_CLK(cfg->i2c_timing_cfg.sda_duty_period);
  115. float scl_start_period = MICROSEC_TO_RTC_FAST_CLK(cfg->i2c_timing_cfg.scl_start_period);
  116. float scl_stop_period = MICROSEC_TO_RTC_FAST_CLK(cfg->i2c_timing_cfg.scl_stop_period);
  117. float i2c_trans_timeout = MICROSEC_TO_RTC_FAST_CLK(cfg->i2c_timing_cfg.i2c_trans_timeout);
  118. float setup_time_start = (cfg->i2c_timing_cfg.scl_high_period + cfg->i2c_timing_cfg.sda_duty_period);
  119. float hold_time_start = (cfg->i2c_timing_cfg.scl_start_period - cfg->i2c_timing_cfg.sda_duty_period);
  120. float setup_time_data = (cfg->i2c_timing_cfg.scl_low_period - cfg->i2c_timing_cfg.sda_duty_period);
  121. /* Verify timing constraints */
  122. ESP_RETURN_ON_FALSE(cfg->i2c_timing_cfg.scl_low_period >= 1.3f, ESP_ERR_INVALID_ARG, RTCI2C_TAG, "SCL low period cannot be less than 1.3 micro seconds");
  123. // TODO: As per specs, SCL high period must be greater than 0.6 micro seconds but after tests it is found that we can have a the period as 0.3 micro seconds to
  124. // achieve performance close to I2C fast mode. Therefore, this criteria is relaxed.
  125. ESP_RETURN_ON_FALSE(cfg->i2c_timing_cfg.scl_high_period >= 0.3f, ESP_ERR_INVALID_ARG, RTCI2C_TAG, "SCL high period cannot be less than 0.3 micro seconds");
  126. ESP_RETURN_ON_FALSE(setup_time_start >= 0.6f, ESP_ERR_INVALID_ARG, RTCI2C_TAG, "Setup time cannot be less than 0.6 micro seconds");
  127. ESP_RETURN_ON_FALSE(hold_time_start >= 0.6f, ESP_ERR_INVALID_ARG, RTCI2C_TAG, "Data hold time cannot be less than 0.6 micro seconds");
  128. ESP_RETURN_ON_FALSE(cfg->i2c_timing_cfg.scl_stop_period >= 0.6f, ESP_ERR_INVALID_ARG, RTCI2C_TAG, "Setup time cannot be less than 0.6 micro seconds");
  129. ESP_RETURN_ON_FALSE(cfg->i2c_timing_cfg.sda_duty_period <= 3.45f, ESP_ERR_INVALID_ARG, RTCI2C_TAG, "Data hold time cannot be greater than 3.45 micro seconds");
  130. ESP_RETURN_ON_FALSE((setup_time_data * 1000) >= 250, ESP_ERR_INVALID_ARG, RTCI2C_TAG, "Data setup time cannot be less than 250 nano seconds");
  131. /* Verify filtering constrains
  132. *
  133. * I2C may have glitches on the transition edge, so the edge will be filtered in the design,
  134. * which will also affect the value of the timing parameter register.
  135. * Therefore, the following filtering constraints must be followed:
  136. */
  137. ESP_RETURN_ON_FALSE(scl_stop_period > scl_high_period, ESP_ERR_INVALID_ARG, RTCI2C_TAG, "SCL Stop period cannot be greater than SCL high period");
  138. ESP_RETURN_ON_FALSE(sda_duty_period < scl_low_period, ESP_ERR_INVALID_ARG, RTCI2C_TAG, "SDA duty period cannot be less than the SCL low period");
  139. ESP_RETURN_ON_FALSE(scl_start_period > 8, ESP_ERR_INVALID_ARG, RTCI2C_TAG, "SCL start period must be greater than 8 RTC_FAST_CLK cycles");
  140. ESP_RETURN_ON_FALSE((scl_low_period + scl_high_period - sda_duty_period) > 8, ESP_ERR_INVALID_ARG, RTCI2C_TAG, "SCL low + SCL high - SDA duty must be greater than 8 RTC_FAST_CLK cycles");
  141. /* Verify SDA duty num constraints */
  142. ESP_RETURN_ON_FALSE(sda_duty_period > 14, ESP_ERR_INVALID_ARG, RTCI2C_TAG, "SDA duty period must be greater than 14 RTC_FAST_CLK cycles");
  143. /* Set the RTC I2C timing parameters */
  144. #if CONFIG_IDF_TARGET_ESP32S2
  145. i2c_dev->scl_low.val = scl_low_period; // SCL low period
  146. i2c_dev->scl_high.val = scl_high_period; // SCL high period
  147. i2c_dev->sda_duty.val = sda_duty_period; // SDA duty cycle
  148. i2c_dev->scl_start_period.val = scl_start_period; // Wait time after START condition
  149. i2c_dev->scl_stop_period.val = scl_stop_period; // Wait time before END condition
  150. i2c_dev->timeout.val = i2c_trans_timeout; // I2C transaction timeout
  151. #elif CONFIG_IDF_TARGET_ESP32S3
  152. i2c_dev->i2c_scl_low.val = scl_low_period; // SCL low period
  153. i2c_dev->i2c_scl_high.val = scl_high_period; // SCL high period
  154. i2c_dev->i2c_sda_duty.val = sda_duty_period; // SDA duty cycle
  155. i2c_dev->i2c_scl_start_period.val = scl_start_period; // Wait time after START condition
  156. i2c_dev->i2c_scl_stop_period.val = scl_stop_period; // Wait time before END condition
  157. i2c_dev->i2c_to.val = i2c_trans_timeout; // I2C transaction timeout
  158. #endif // CONFIG_IDF_TARGET_ESP32S2
  159. return ESP_OK;
  160. }
  161. /*
  162. * The RTC I2C controller follows the I2C command registers to perform read/write operations.
  163. * The cmd registers have the following format:
  164. *
  165. * 31 30:14 13:11 10 9 8 7:0
  166. * |----------|----------|---------|---------|----------|------------|---------|
  167. * | CMD_DONE | Reserved | OPCODE |ACK Value|ACK Expect|ACK Check En|Byte Num |
  168. * |----------|----------|---------|---------|----------|------------|---------|
  169. */
  170. static void ulp_riscv_i2c_format_cmd(uint32_t cmd_idx, uint8_t op_code, uint8_t ack_val,
  171. uint8_t ack_expected, uint8_t ack_check_en, uint8_t byte_num)
  172. {
  173. #if CONFIG_IDF_TARGET_ESP32S2
  174. /* Reset cmd register */
  175. i2c_dev->command[cmd_idx].val = 0;
  176. /* Write new command to cmd register */
  177. i2c_dev->command[cmd_idx].done = 0; // CMD Done
  178. i2c_dev->command[cmd_idx].op_code = op_code; // Opcode
  179. i2c_dev->command[cmd_idx].ack_val = ack_val; // ACK bit sent by I2C controller during READ.
  180. // Ignored during RSTART, STOP, END and WRITE cmds.
  181. i2c_dev->command[cmd_idx].ack_exp = ack_expected; // ACK bit expected by I2C controller during WRITE.
  182. // Ignored during RSTART, STOP, END and READ cmds.
  183. i2c_dev->command[cmd_idx].ack_en = ack_check_en; // I2C controller verifies that the ACK bit sent by the
  184. // slave device matches the ACK expected bit during WRITE.
  185. // Ignored during RSTART, STOP, END and READ cmds.
  186. i2c_dev->command[cmd_idx].byte_num = byte_num; // Byte Num
  187. #elif CONFIG_IDF_TARGET_ESP32S3
  188. /* Reset cmd register */
  189. i2c_dev->i2c_cmd[cmd_idx].val = 0;
  190. /* Write new command to cmd register */
  191. i2c_dev->i2c_cmd[cmd_idx].i2c_command_done = 0; // CMD Done
  192. i2c_dev->i2c_cmd[cmd_idx].i2c_op_code = op_code; // Opcode
  193. i2c_dev->i2c_cmd[cmd_idx].i2c_ack_val = ack_val; // ACK bit sent by I2C controller during READ.
  194. // Ignored during RSTART, STOP, END and WRITE cmds.
  195. i2c_dev->i2c_cmd[cmd_idx].i2c_ack_exp = ack_expected; // ACK bit expected by I2C controller during WRITE.
  196. // Ignored during RSTART, STOP, END and READ cmds.
  197. i2c_dev->i2c_cmd[cmd_idx].i2c_ack_en = ack_check_en; // I2C controller verifies that the ACK bit sent by the
  198. // slave device matches the ACK expected bit during WRITE.
  199. // Ignored during RSTART, STOP, END and READ cmds.
  200. i2c_dev->i2c_cmd[cmd_idx].i2c_byte_num = byte_num; // Byte Num
  201. #endif // CONFIG_IDF_TARGET_ESP32S2
  202. }
  203. static inline esp_err_t ulp_riscv_i2c_wait_for_interrupt(int32_t ticks_to_wait)
  204. {
  205. uint32_t status = 0;
  206. uint32_t to = 0;
  207. esp_err_t ret = ESP_OK;
  208. while (1) {
  209. status = READ_PERI_REG(RTC_I2C_INT_ST_REG);
  210. /* Return ESP_OK if Tx or Rx data interrupt bits are set. */
  211. if ((status & RTC_I2C_TX_DATA_INT_ST) ||
  212. (status & RTC_I2C_RX_DATA_INT_ST)) {
  213. ret = ESP_OK;
  214. break;
  215. /* In case of error status, break and return ESP_FAIL */
  216. #if CONFIG_IDF_TARGET_ESP32S2
  217. } else if ((status & RTC_I2C_TIMEOUT_INT_ST) ||
  218. #elif CONFIG_IDF_TARGET_ESP32S3
  219. } else if ((status & RTC_I2C_TIME_OUT_INT_ST) ||
  220. #endif // CONFIG_IDF_TARGET_ESP32S2
  221. (status & RTC_I2C_ACK_ERR_INT_ST) ||
  222. (status & RTC_I2C_ARBITRATION_LOST_INT_ST)) {
  223. ret = ESP_FAIL;
  224. break;
  225. }
  226. if (ticks_to_wait > -1) {
  227. /* If the ticks_to_wait value is not -1, keep track of ticks and
  228. * break from the loop once the timeout is reached.
  229. */
  230. vTaskDelay(1);
  231. to++;
  232. if (to >= ticks_to_wait) {
  233. ret = ESP_ERR_TIMEOUT;
  234. break;
  235. }
  236. }
  237. }
  238. return ret;
  239. }
  240. void ulp_riscv_i2c_master_set_slave_addr(uint8_t slave_addr)
  241. {
  242. CLEAR_PERI_REG_MASK(SENS_SAR_I2C_CTRL_REG, I2C_CTRL_SLAVE_ADDR_MASK);
  243. SET_PERI_REG_BITS(SENS_SAR_I2C_CTRL_REG, 0xFF, slave_addr, 0);
  244. }
  245. void ulp_riscv_i2c_master_set_slave_reg_addr(uint8_t slave_reg_addr)
  246. {
  247. CLEAR_PERI_REG_MASK(SENS_SAR_I2C_CTRL_REG, I2C_CTRL_SLAVE_REG_ADDR_MASK);
  248. SET_PERI_REG_BITS(SENS_SAR_I2C_CTRL_REG, 0xFF, slave_reg_addr, 11);
  249. }
  250. /*
  251. * I2C transactions when master reads one byte of data from the slave device:
  252. *
  253. * |--------|--------|---------|--------|--------|--------|--------|---------|--------|--------|--------|--------|
  254. * | Master | START | SAD + W | | SUB | | SR | SAD + R | | | NACK | STOP |
  255. * |--------|--------|---------|--------|--------|--------|--------|---------|--------|--------|--------|--------|
  256. * | Slave | | | ACK | | ACK | | | ACK | DATA | | |
  257. * |--------|--------|---------|--------|--------|--------|--------|---------|--------|--------|--------|--------|
  258. *
  259. * I2C transactions when master reads multiple bytes of data from the slave device:
  260. *
  261. * |--------|--------|---------|--------|--------|--------|--------|---------|--------|--------|--------|--------|--------|--------|
  262. * | Master | START | SAD + W | | SUB | | SR | SAD + R | | | ACK | | NACK | STOP |
  263. * |--------|--------|---------|--------|--------|--------|--------|---------|--------|--------|--------|--------|--------|--------|
  264. * | Slave | | | ACK | | ACK | | | ACK | DATA | | DATA | | |
  265. * |--------|--------|---------|--------|--------|--------|--------|---------|--------|--------|--------|--------|--------|--------|
  266. */
  267. void ulp_riscv_i2c_master_read_from_device(uint8_t *data_rd, size_t size)
  268. {
  269. uint32_t i = 0;
  270. uint32_t cmd_idx = 0;
  271. esp_err_t ret = ESP_OK;
  272. if (size == 0) {
  273. // Quietly return
  274. return;
  275. }
  276. /* By default, RTC I2C controller is hard wired to use CMD2 register onwards for read operations */
  277. cmd_idx = 2;
  278. /* Write slave addr */
  279. ulp_riscv_i2c_format_cmd(cmd_idx++, ULP_I2C_CMD_WRITE, 0, 0, 1, 2);
  280. /* Repeated START */
  281. ulp_riscv_i2c_format_cmd(cmd_idx++, ULP_I2C_CMD_RESTART, 0, 0, 0, 0);
  282. /* Write slave register addr */
  283. ulp_riscv_i2c_format_cmd(cmd_idx++, ULP_I2C_CMD_WRITE, 0, 0, 1, 1);
  284. if (size > 1) {
  285. /* Read n - 1 bytes */
  286. ulp_riscv_i2c_format_cmd(cmd_idx++, ULP_I2C_CMD_READ, 0, 0, 1, size - 1);
  287. }
  288. /* Read last byte + NACK */
  289. ulp_riscv_i2c_format_cmd(cmd_idx++, ULP_I2C_CMD_READ, 1, 1, 1, 1);
  290. /* STOP */
  291. ulp_riscv_i2c_format_cmd(cmd_idx++, ULP_I2C_CMD_STOP, 0, 0, 0, 0);
  292. /* Configure the RTC I2C controller in read mode */
  293. SET_PERI_REG_BITS(SENS_SAR_I2C_CTRL_REG, 0x1, 0, 27);
  294. /* Start RTC I2C transmission */
  295. SET_PERI_REG_MASK(SENS_SAR_I2C_CTRL_REG, SENS_SAR_I2C_START_FORCE);
  296. SET_PERI_REG_MASK(SENS_SAR_I2C_CTRL_REG, SENS_SAR_I2C_START);
  297. for (i = 0; i < size; i++) {
  298. /* Poll for RTC I2C Rx Data interrupt bit to be set */
  299. ret = ulp_riscv_i2c_wait_for_interrupt(ULP_RISCV_I2C_RW_TIMEOUT);
  300. if (ret == ESP_OK) {
  301. /* Read the data
  302. *
  303. * Unfortunately, the RTC I2C has no fifo buffer to help us with reading and storing
  304. * multiple bytes of data. Therefore, we need to read one byte at a time and clear the
  305. * Rx interrupt to get ready for the next byte.
  306. */
  307. #if CONFIG_IDF_TARGET_ESP32S2
  308. data_rd[i] = REG_GET_FIELD(RTC_I2C_DATA_REG, RTC_I2C_RDATA);
  309. #elif CONFIG_IDF_TARGET_ESP32S3
  310. data_rd[i] = REG_GET_FIELD(RTC_I2C_DATA_REG, RTC_I2C_I2C_RDATA);
  311. #endif // CONFIG_IDF_TARGET_ESP32S2
  312. /* Clear the Rx data interrupt bit */
  313. SET_PERI_REG_MASK(RTC_I2C_INT_CLR_REG, RTC_I2C_RX_DATA_INT_CLR);
  314. } else {
  315. ESP_LOGE(RTCI2C_TAG, "Read Failed!");
  316. uint32_t status = READ_PERI_REG(RTC_I2C_INT_RAW_REG);
  317. ESP_LOGE(RTCI2C_TAG, "RTC I2C Interrupt Raw Reg 0x%"PRIx32"", status);
  318. ESP_LOGE(RTCI2C_TAG, "RTC I2C Status Reg 0x%"PRIx32"", READ_PERI_REG(RTC_I2C_STATUS_REG));
  319. SET_PERI_REG_MASK(RTC_I2C_INT_CLR_REG, status);
  320. break;
  321. }
  322. }
  323. /* Clear the RTC I2C transmission bits */
  324. CLEAR_PERI_REG_MASK(SENS_SAR_I2C_CTRL_REG, SENS_SAR_I2C_START_FORCE);
  325. CLEAR_PERI_REG_MASK(SENS_SAR_I2C_CTRL_REG, SENS_SAR_I2C_START);
  326. }
  327. /*
  328. * I2C transactions when master writes one byte of data to the slave device:
  329. *
  330. * |--------|--------|---------|--------|--------|--------|--------|--------|--------|
  331. * | Master | START | SAD + W | | SUB | | DATA | | STOP |
  332. * |--------|--------|---------|--------|--------|--------|--------|--------|--------|
  333. * | Slave | | | ACK | | ACK | | ACK | |
  334. * |--------|--------|---------|--------|--------|--------|--------|--------|--------|
  335. *
  336. * I2C transactions when master writes multiple bytes of data to the slave device:
  337. *
  338. * |--------|--------|---------|--------|--------|--------|--------|--------|--------|--------|--------|
  339. * | Master | START | SAD + W | | SUB | | DATA | | DATA | | STOP |
  340. * |--------|--------|---------|--------|--------|--------|--------|--------|--------|--------|--------|
  341. * | Slave | | | ACK | | ACK | | ACK | | ACK | |
  342. * |--------|--------|---------|--------|--------|--------|--------|--------|--------|--------|--------|
  343. */
  344. void ulp_riscv_i2c_master_write_to_device(uint8_t *data_wr, size_t size)
  345. {
  346. uint32_t i = 0;
  347. uint32_t cmd_idx = 0;
  348. esp_err_t ret = ESP_OK;
  349. if (size == 0) {
  350. // Quietly return
  351. return;
  352. }
  353. /* By default, RTC I2C controller is hard wired to use CMD0 and CMD1 registers for write operations */
  354. cmd_idx = 0;
  355. /* Write slave addr + reg addr + data */
  356. ulp_riscv_i2c_format_cmd(cmd_idx++, ULP_I2C_CMD_WRITE, 0, 0, 1, 2 + size);
  357. /* Stop */
  358. ulp_riscv_i2c_format_cmd(cmd_idx++, ULP_I2C_CMD_STOP, 0, 0, 0, 0);
  359. /* Configure the RTC I2C controller in write mode */
  360. SET_PERI_REG_BITS(SENS_SAR_I2C_CTRL_REG, 0x1, 1, 27);
  361. for (i = 0; i < size; i++) {
  362. /* Write the data to be transmitted */
  363. CLEAR_PERI_REG_MASK(SENS_SAR_I2C_CTRL_REG, I2C_CTRL_MASTER_TX_DATA_MASK);
  364. SET_PERI_REG_BITS(SENS_SAR_I2C_CTRL_REG, 0xFF, data_wr[i], 19);
  365. if (i == 0) {
  366. /* Start RTC I2C transmission. (Needn't do it for every byte) */
  367. SET_PERI_REG_MASK(SENS_SAR_I2C_CTRL_REG, SENS_SAR_I2C_START_FORCE);
  368. SET_PERI_REG_MASK(SENS_SAR_I2C_CTRL_REG, SENS_SAR_I2C_START);
  369. }
  370. /* Poll for RTC I2C Tx Data interrupt bit to be set */
  371. ret = ulp_riscv_i2c_wait_for_interrupt(ULP_RISCV_I2C_RW_TIMEOUT);
  372. if (ret == ESP_OK) {
  373. /* Clear the Tx data interrupt bit */
  374. SET_PERI_REG_MASK(RTC_I2C_INT_CLR_REG, RTC_I2C_TX_DATA_INT_CLR);
  375. } else {
  376. ESP_LOGE(RTCI2C_TAG, "Write Failed!");
  377. uint32_t status = READ_PERI_REG(RTC_I2C_INT_RAW_REG);
  378. ESP_LOGE(RTCI2C_TAG, "RTC I2C Interrupt Raw Reg 0x%"PRIx32"", status);
  379. ESP_LOGE(RTCI2C_TAG, "RTC I2C Status Reg 0x%"PRIx32"", READ_PERI_REG(RTC_I2C_STATUS_REG));
  380. SET_PERI_REG_MASK(RTC_I2C_INT_CLR_REG, status);
  381. break;
  382. }
  383. }
  384. /* Clear the RTC I2C transmission bits */
  385. CLEAR_PERI_REG_MASK(SENS_SAR_I2C_CTRL_REG, SENS_SAR_I2C_START_FORCE);
  386. CLEAR_PERI_REG_MASK(SENS_SAR_I2C_CTRL_REG, SENS_SAR_I2C_START);
  387. }
  388. esp_err_t ulp_riscv_i2c_master_init(const ulp_riscv_i2c_cfg_t *cfg)
  389. {
  390. /* Clear any stale config registers */
  391. WRITE_PERI_REG(RTC_I2C_CTRL_REG, 0);
  392. WRITE_PERI_REG(SENS_SAR_I2C_CTRL_REG, 0);
  393. /* Reset RTC I2C */
  394. #if CONFIG_IDF_TARGET_ESP32S2
  395. i2c_dev->ctrl.i2c_reset = 1;
  396. esp_rom_delay_us(20);
  397. i2c_dev->ctrl.i2c_reset = 0;
  398. #elif CONFIG_IDF_TARGET_ESP32S3
  399. SET_PERI_REG_MASK(SENS_SAR_PERI_RESET_CONF_REG, SENS_RTC_I2C_RESET);
  400. i2c_dev->i2c_ctrl.i2c_i2c_reset = 1;
  401. esp_rom_delay_us(20);
  402. i2c_dev->i2c_ctrl.i2c_i2c_reset = 0;
  403. CLEAR_PERI_REG_MASK(SENS_SAR_PERI_RESET_CONF_REG, SENS_RTC_I2C_RESET);
  404. #endif // CONFIG_IDF_TARGET_ESP32S2
  405. /* Verify that the input cfg param is valid */
  406. ESP_RETURN_ON_FALSE(cfg, ESP_ERR_INVALID_ARG, RTCI2C_TAG, "RTC I2C configuration is NULL");
  407. /* Configure RTC I2C GPIOs */
  408. ESP_RETURN_ON_ERROR(i2c_set_pin(cfg), RTCI2C_TAG, "Failed to configure RTC I2C GPIOs");
  409. #if CONFIG_IDF_TARGET_ESP32S2
  410. /* Configure the RTC I2C controller in master mode */
  411. i2c_dev->ctrl.ms_mode = 1;
  412. /* Enable RTC I2C Clock gate */
  413. i2c_dev->ctrl.i2c_ctrl_clk_gate_en = 1;
  414. #elif CONFIG_IDF_TARGET_ESP32S3
  415. /* For esp32s3, we need to enable the rtc_i2c clock gate before accessing rtc i2c registers */
  416. SET_PERI_REG_MASK(SENS_SAR_PERI_CLK_GATE_CONF_REG, SENS_RTC_I2C_CLK_EN);
  417. /* Configure the RTC I2C controller in master mode */
  418. i2c_dev->i2c_ctrl.i2c_ms_mode = 1;
  419. /* Enable RTC I2C Clock gate */
  420. i2c_dev->i2c_ctrl.i2c_i2c_ctrl_clk_gate_en = 1;
  421. #endif // CONFIG_IDF_TARGET_ESP32S2
  422. /* Configure RTC I2C timing paramters */
  423. ESP_RETURN_ON_ERROR(i2c_set_timing(cfg), RTCI2C_TAG, "Failed to configure RTC I2C timing");
  424. /* Enable RTC I2C interrupts */
  425. SET_PERI_REG_MASK(RTC_I2C_INT_ENA_REG, RTC_I2C_RX_DATA_INT_ENA |
  426. RTC_I2C_TX_DATA_INT_ENA |
  427. RTC_I2C_ARBITRATION_LOST_INT_ENA |
  428. RTC_I2C_ACK_ERR_INT_ENA |
  429. #if CONFIG_IDF_TARGET_ESP32S2
  430. RTC_I2C_TIMEOUT_INT_ENA);
  431. #elif CONFIG_IDF_TARGET_ESP32S3
  432. RTC_I2C_TIME_OUT_INT_ENA);
  433. #endif // CONFIG_IDF_TARGET_ESP32S2
  434. return ESP_OK;
  435. }