ulp_riscv_i2c.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * SPDX-FileCopyrightText: 2022 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. typedef struct {
  15. uint32_t sda_io_num; // GPIO pin for SDA signal. Only GPIO#1 or GPIO#3 can be used as the SDA pin.
  16. uint32_t scl_io_num; // GPIO pin for SCL signal. Only GPIO#0 or GPIO#2 can be used as the SCL pin.
  17. bool sda_pullup_en; // SDA line enable internal pullup. Can be configured if external pullup is not used.
  18. bool scl_pullup_en; // SCL line enable internal pullup. Can be configured if external pullup is not used.
  19. } ulp_riscv_i2c_pin_cfg_t;
  20. typedef struct {
  21. uint32_t scl_low_period; // SCL low period
  22. uint32_t scl_high_period; // SCL high period
  23. uint32_t sda_duty_period; // Period between the SDA switch and the falling edge of SCL
  24. uint32_t scl_start_period; // Waiting time after the START condition
  25. uint32_t scl_stop_period; // Waiting time before the END condition
  26. uint32_t i2c_trans_timeout; // I2C transaction timeout
  27. } ulp_riscv_i2c_timing_cfg_t;
  28. typedef struct {
  29. ulp_riscv_i2c_pin_cfg_t i2c_pin_cfg; // RTC I2C pin configuration
  30. ulp_riscv_i2c_timing_cfg_t i2c_timing_cfg; // RTC I2C timing configuration
  31. } ulp_riscv_i2c_cfg_t;
  32. /* Nominal default GPIO settings and timing parametes */
  33. #define ULP_RISCV_I2C_DEFAULT_CONFIG() \
  34. { \
  35. .i2c_pin_cfg.sda_io_num = GPIO_NUM_3, \
  36. .i2c_pin_cfg.scl_io_num = GPIO_NUM_2, \
  37. .i2c_pin_cfg.sda_pullup_en = true, \
  38. .i2c_pin_cfg.scl_pullup_en = true, \
  39. .i2c_timing_cfg.scl_low_period = 5, \
  40. .i2c_timing_cfg.scl_high_period = 5, \
  41. .i2c_timing_cfg.sda_duty_period = 2, \
  42. .i2c_timing_cfg.scl_start_period = 3, \
  43. .i2c_timing_cfg.scl_stop_period = 6, \
  44. .i2c_timing_cfg.i2c_trans_timeout = 20, \
  45. }
  46. /**
  47. * @brief Set the I2C slave device address
  48. *
  49. * @param slave_addr I2C slave address (7 bit)
  50. */
  51. void ulp_riscv_i2c_master_set_slave_addr(uint8_t slave_addr);
  52. /**
  53. * @brief Set the I2C slave device sub register address
  54. *
  55. * @note The RTC I2C peripheral always expects a slave sub register address to be programmed. If it is not, the I2C
  56. * peripheral uses the SENS_SAR_I2C_CTRL_REG[18:11] as the sub register address for the subsequent read or write
  57. * operation.
  58. *
  59. * @param slave_reg_addr I2C slave sub register address
  60. */
  61. void ulp_riscv_i2c_master_set_slave_reg_addr(uint8_t slave_reg_addr);
  62. /**
  63. * @brief Read from I2C slave device
  64. *
  65. * @note The I2C slave device address must be configured at least once before invoking this API.
  66. *
  67. * @param data_rd Buffer to hold data to be read
  68. * @param size Size of data to be read in bytes
  69. */
  70. void ulp_riscv_i2c_master_read_from_device(uint8_t *data_rd, size_t size);
  71. /**
  72. * @brief Write to I2C slave device
  73. *
  74. * @note The I2C slave device address must be configured at least once before invoking this API.
  75. *
  76. * @param data_wr Buffer which holds the data to be written
  77. * @param size Size of data to be written in bytes
  78. */
  79. void ulp_riscv_i2c_master_write_to_device(uint8_t *data_wr, size_t size);
  80. /**
  81. * @brief Initialize and configure the RTC I2C for use by ULP RISC-V
  82. * Currently RTC I2C can only be used in master mode
  83. *
  84. * @param cfg Configuration parameters
  85. * @return esp_err_t ESP_OK when successful
  86. */
  87. esp_err_t ulp_riscv_i2c_master_init(const ulp_riscv_i2c_cfg_t *cfg);
  88. #ifdef __cplusplus
  89. }
  90. #endif