ulp_riscv_i2c.c 24 KB

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