ulp_riscv_i2c.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. /* Nominal I2C bus timing parameters for I2C fast mode. Max SCL freq of 400 KHz. */
  48. #define ULP_RISCV_I2C_FAST_MODE_CONFIG() \
  49. .i2c_timing_cfg.scl_low_period = 1.4, \
  50. .i2c_timing_cfg.scl_high_period = 0.3, \
  51. .i2c_timing_cfg.sda_duty_period = 1, \
  52. .i2c_timing_cfg.scl_start_period = 2, \
  53. .i2c_timing_cfg.scl_stop_period = 1.3, \
  54. .i2c_timing_cfg.i2c_trans_timeout = 20, \
  55. /* Nominal I2C bus timing parameters for I2C standard mode. Max SCL freq of 100 KHz. */
  56. #define ULP_RISCV_I2C_STANDARD_MODE_CONFIG() \
  57. .i2c_timing_cfg.scl_low_period = 5, \
  58. .i2c_timing_cfg.scl_high_period = 5, \
  59. .i2c_timing_cfg.sda_duty_period = 2, \
  60. .i2c_timing_cfg.scl_start_period = 3, \
  61. .i2c_timing_cfg.scl_stop_period = 6, \
  62. .i2c_timing_cfg.i2c_trans_timeout = 20, \
  63. /* Default RTC I2C configuration settings. Uses I2C fast mode. */
  64. //TODO: Move to smaller units of time in the future like nano seconds to avoid floating point operations.
  65. #define ULP_RISCV_I2C_DEFAULT_CONFIG() \
  66. { \
  67. ULP_RISCV_I2C_DEFAULT_GPIO_CONFIG() \
  68. ULP_RISCV_I2C_FAST_MODE_CONFIG() \
  69. }
  70. /**
  71. * @brief Set the I2C slave device address
  72. *
  73. * @param slave_addr I2C slave address (7 bit)
  74. */
  75. void ulp_riscv_i2c_master_set_slave_addr(uint8_t slave_addr);
  76. /**
  77. * @brief Set the I2C slave device sub register address
  78. *
  79. * @note The RTC I2C peripheral always expects a slave sub register address to be programmed. If it is not, the I2C
  80. * peripheral uses the SENS_SAR_I2C_CTRL_REG[18:11] as the sub register address for the subsequent read or write
  81. * operation.
  82. *
  83. * @param slave_reg_addr I2C slave sub register address
  84. */
  85. void ulp_riscv_i2c_master_set_slave_reg_addr(uint8_t slave_reg_addr);
  86. /**
  87. * @brief Read from I2C slave device
  88. *
  89. * @note The I2C slave device address must be configured at least once before invoking this API.
  90. *
  91. * @param data_rd Buffer to hold data to be read
  92. * @param size Size of data to be read in bytes
  93. */
  94. void ulp_riscv_i2c_master_read_from_device(uint8_t *data_rd, size_t size);
  95. /**
  96. * @brief Write to I2C slave device
  97. *
  98. * @note The I2C slave device address must be configured at least once before invoking this API.
  99. *
  100. * @param data_wr Buffer which holds the data to be written
  101. * @param size Size of data to be written in bytes
  102. */
  103. void ulp_riscv_i2c_master_write_to_device(uint8_t *data_wr, size_t size);
  104. /**
  105. * @brief Initialize and configure the RTC I2C for use by ULP RISC-V
  106. * Currently RTC I2C can only be used in master mode
  107. *
  108. * @param cfg Configuration parameters
  109. * @return esp_err_t ESP_OK when successful
  110. */
  111. esp_err_t ulp_riscv_i2c_master_init(const ulp_riscv_i2c_cfg_t *cfg);
  112. #ifdef __cplusplus
  113. }
  114. #endif