ulp_riscv_i2c.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. #include <stdint.h>
  11. #include <stdbool.h>
  12. #include "hal/gpio_types.h"
  13. #include "esp_err.h"
  14. /**
  15. * @brief ULP RISC-V RTC I2C pin config
  16. */
  17. typedef struct {
  18. uint32_t sda_io_num; /*!< GPIO pin for SDA signal. Only GPIO#1 or GPIO#3 can be used as the SDA pin. */
  19. uint32_t scl_io_num; /*!< GPIO pin for SCL signal. Only GPIO#0 or GPIO#2 can be used as the SCL pin. */
  20. bool sda_pullup_en; /*!< SDA line enable internal pullup. Can be configured if external pullup is not used. */
  21. bool scl_pullup_en; /*!< SCL line enable internal pullup. Can be configured if external pullup is not used. */
  22. } ulp_riscv_i2c_pin_cfg_t;
  23. /**
  24. * @brief ULP RISC-V RTC I2C timing config
  25. */
  26. typedef struct {
  27. float scl_low_period; /*!< SCL low period in micro seconds */
  28. float scl_high_period; /*!< SCL high period in micro seconds */
  29. float sda_duty_period; /*!< Period between the SDA switch and the falling edge of SCL in micro seconds */
  30. float scl_start_period; /*!< Waiting time after the START condition in micro seconds */
  31. float scl_stop_period; /*!< Waiting time before the END condition in micro seconds */
  32. float i2c_trans_timeout; /*!< I2C transaction timeout in micro seconds */
  33. } ulp_riscv_i2c_timing_cfg_t;
  34. /**
  35. * @brief ULP RISC-V RTC I2C init parameters
  36. */
  37. typedef struct {
  38. ulp_riscv_i2c_pin_cfg_t i2c_pin_cfg; /*!< RTC I2C pin configuration */
  39. ulp_riscv_i2c_timing_cfg_t i2c_timing_cfg; /*!< RTC I2C timing configuration */
  40. } ulp_riscv_i2c_cfg_t;
  41. /* Default RTC I2C GPIO settings */
  42. #define ULP_RISCV_I2C_DEFAULT_GPIO_CONFIG() \
  43. .i2c_pin_cfg.sda_io_num = GPIO_NUM_3, \
  44. .i2c_pin_cfg.scl_io_num = GPIO_NUM_2, \
  45. .i2c_pin_cfg.sda_pullup_en = true, \
  46. .i2c_pin_cfg.scl_pullup_en = true, \
  47. #if CONFIG_IDF_TARGET_ESP32S3
  48. /* Nominal I2C bus timing parameters for I2C fast mode. Max SCL freq of 400 KHz. */
  49. #define ULP_RISCV_I2C_FAST_MODE_CONFIG() \
  50. .i2c_timing_cfg.scl_low_period = 1.4, \
  51. .i2c_timing_cfg.scl_high_period = 0.3, \
  52. .i2c_timing_cfg.sda_duty_period = 1, \
  53. .i2c_timing_cfg.scl_start_period = 2, \
  54. .i2c_timing_cfg.scl_stop_period = 1.3, \
  55. .i2c_timing_cfg.i2c_trans_timeout = 20,
  56. #elif CONFIG_IDF_TARGET_ESP32S2
  57. /* Nominal I2C bus timing parameters for I2C fast mode. Max SCL freq on S2 is about 233 KHz due to timing constraints. */
  58. #define ULP_RISCV_I2C_FAST_MODE_CONFIG() \
  59. .i2c_timing_cfg.scl_low_period = 2, \
  60. .i2c_timing_cfg.scl_high_period = 0.7, \
  61. .i2c_timing_cfg.sda_duty_period = 1.7, \
  62. .i2c_timing_cfg.scl_start_period = 2.4, \
  63. .i2c_timing_cfg.scl_stop_period = 1.3, \
  64. .i2c_timing_cfg.i2c_trans_timeout = 20,
  65. #endif
  66. /* Nominal I2C bus timing parameters for I2C standard mode. Max SCL freq of 100 KHz. */
  67. #define ULP_RISCV_I2C_STANDARD_MODE_CONFIG() \
  68. .i2c_timing_cfg.scl_low_period = 5, \
  69. .i2c_timing_cfg.scl_high_period = 5, \
  70. .i2c_timing_cfg.sda_duty_period = 2, \
  71. .i2c_timing_cfg.scl_start_period = 3, \
  72. .i2c_timing_cfg.scl_stop_period = 6, \
  73. .i2c_timing_cfg.i2c_trans_timeout = 20, \
  74. /* Default RTC I2C configuration settings. Uses I2C fast mode. */
  75. //TODO: Move to smaller units of time in the future like nano seconds to avoid floating point operations.
  76. #define ULP_RISCV_I2C_DEFAULT_CONFIG() \
  77. { \
  78. ULP_RISCV_I2C_DEFAULT_GPIO_CONFIG() \
  79. ULP_RISCV_I2C_FAST_MODE_CONFIG() \
  80. }
  81. /**
  82. * @brief Set the I2C slave device address
  83. *
  84. * @param slave_addr I2C slave address (7 bit)
  85. */
  86. void ulp_riscv_i2c_master_set_slave_addr(uint8_t slave_addr);
  87. /**
  88. * @brief Set the I2C slave device sub register address
  89. *
  90. * @note The RTC I2C peripheral always expects a slave sub register address to be programmed. If it is not, the I2C
  91. * peripheral uses the SENS_SAR_I2C_CTRL_REG[18:11] as the sub register address for the subsequent read or write
  92. * operation.
  93. *
  94. * @param slave_reg_addr I2C slave sub register address
  95. */
  96. void ulp_riscv_i2c_master_set_slave_reg_addr(uint8_t slave_reg_addr);
  97. /**
  98. * @brief Read from I2C slave device
  99. *
  100. * @note The I2C slave device address must be configured at least once before invoking this API.
  101. *
  102. * @param data_rd Buffer to hold data to be read
  103. * @param size Size of data to be read in bytes
  104. */
  105. void ulp_riscv_i2c_master_read_from_device(uint8_t *data_rd, size_t size);
  106. /**
  107. * @brief Write to I2C slave device
  108. *
  109. * @note The I2C slave device address must be configured at least once before invoking this API.
  110. *
  111. * @param data_wr Buffer which holds the data to be written
  112. * @param size Size of data to be written in bytes
  113. */
  114. void ulp_riscv_i2c_master_write_to_device(uint8_t *data_wr, size_t size);
  115. /**
  116. * @brief Initialize and configure the RTC I2C for use by ULP RISC-V
  117. * Currently RTC I2C can only be used in master mode
  118. *
  119. * @param cfg Configuration parameters
  120. * @return esp_err_t ESP_OK when successful
  121. */
  122. esp_err_t ulp_riscv_i2c_master_init(const ulp_riscv_i2c_cfg_t *cfg);
  123. #ifdef __cplusplus
  124. }
  125. #endif