1 /* 2 * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 #ifndef _DRIVER_I2C_H_ 8 #define _DRIVER_I2C_H_ 9 10 #ifdef __cplusplus 11 extern "C" { 12 #endif 13 14 #include <esp_types.h> 15 #include "esp_err.h" 16 #include "esp_intr_alloc.h" 17 #include "freertos/FreeRTOS.h" 18 #include "freertos/semphr.h" 19 #include "freertos/task.h" 20 #include "freertos/queue.h" 21 #include "freertos/ringbuf.h" 22 #include "driver/gpio.h" 23 #include "soc/soc_caps.h" 24 #include "hal/i2c_types.h" 25 26 #define I2C_APB_CLK_FREQ APB_CLK_FREQ /*!< I2C source clock is APB clock, 80MHz */ 27 28 #define I2C_NUM_MAX (SOC_I2C_NUM) /*!< I2C port max */ 29 #define I2C_NUM_0 (0) /*!< I2C port 0 */ 30 #if SOC_I2C_NUM >= 2 31 #define I2C_NUM_1 (1) /*!< I2C port 1 */ 32 #endif 33 34 // I2C clk flags for users to use, can be expanded in the future. 35 #define I2C_SCLK_SRC_FLAG_FOR_NOMAL (0) /*!< Any one clock source that is available for the specified frequency may be choosen*/ 36 #define I2C_SCLK_SRC_FLAG_AWARE_DFS (1 << 0) /*!< For REF tick clock, it won't change with APB.*/ 37 #define I2C_SCLK_SRC_FLAG_LIGHT_SLEEP (1 << 1) /*!< For light sleep mode.*/ 38 39 /** 40 * @brief Minimum size, in bytes, of the internal private structure used to describe 41 * I2C commands link. 42 */ 43 #define I2C_INTERNAL_STRUCT_SIZE (24) 44 45 /** 46 * @brief The following macro is used to determine the recommended size of the 47 * buffer to pass to `i2c_cmd_link_create_static()` function. 48 * It requires one parameter, `TRANSACTIONS`, describing the number of transactions 49 * intended to be performed on the I2C port. 50 * For example, if one wants to perform a read on an I2C device register, `TRANSACTIONS` 51 * must be at least 2, because the commands required are the following: 52 * - write device register 53 * - read register content 54 * 55 * Signals such as "(repeated) start", "stop", "nack", "ack" shall not be counted. 56 */ 57 #define I2C_LINK_RECOMMENDED_SIZE(TRANSACTIONS) (2 * I2C_INTERNAL_STRUCT_SIZE + I2C_INTERNAL_STRUCT_SIZE * \ 58 (5 * TRANSACTIONS)) /* Make the assumption that each transaction 59 * of the user is surrounded by a "start", device address 60 * and a "nack/ack" signal. Allocate one more room for 61 * "stop" signal at the end. 62 * Allocate 2 more internal struct size for headers. 63 */ 64 65 /** 66 * @brief I2C initialization parameters 67 */ 68 typedef struct{ 69 i2c_mode_t mode; /*!< I2C mode */ 70 int sda_io_num; /*!< GPIO number for I2C sda signal */ 71 int scl_io_num; /*!< GPIO number for I2C scl signal */ 72 bool sda_pullup_en; /*!< Internal GPIO pull mode for I2C sda signal*/ 73 bool scl_pullup_en; /*!< Internal GPIO pull mode for I2C scl signal*/ 74 75 union { 76 struct { 77 uint32_t clk_speed; /*!< I2C clock frequency for master mode, (no higher than 1MHz for now) */ 78 } master; /*!< I2C master config */ 79 struct { 80 uint8_t addr_10bit_en; /*!< I2C 10bit address mode enable for slave mode */ 81 uint16_t slave_addr; /*!< I2C address for slave mode */ 82 uint32_t maximum_speed; /*!< I2C expected clock speed from SCL. */ 83 } slave; /*!< I2C slave config */ 84 }; 85 uint32_t clk_flags; /*!< Bitwise of ``I2C_SCLK_SRC_FLAG_**FOR_DFS**`` for clk source choice*/ 86 } i2c_config_t; 87 88 89 typedef void *i2c_cmd_handle_t; /*!< I2C command handle */ 90 91 /** 92 * @brief Install an I2C driver 93 * 94 * @param i2c_num I2C port number 95 * @param mode I2C mode (either master or slave) 96 * @param slv_rx_buf_len Receiving buffer size. Only slave mode will use this value, it is ignored in master mode. 97 * @param slv_tx_buf_len Sending buffer size. Only slave mode will use this value, it is ignored in master mode. 98 * @param intr_alloc_flags Flags used to allocate the interrupt. One or multiple (ORred) ESP_INTR_FLAG_* values. 99 * See esp_intr_alloc.h for more info. 100 * @note 101 * In master mode, if the cache is likely to be disabled(such as write flash) and the slave is time-sensitive, 102 * `ESP_INTR_FLAG_IRAM` is suggested to be used. In this case, please use the memory allocated from internal RAM in i2c read and write function, 103 * because we can not access the psram(if psram is enabled) in interrupt handle function when cache is disabled. 104 * 105 * @return 106 * - ESP_OK Success 107 * - ESP_ERR_INVALID_ARG Parameter error 108 * - ESP_FAIL Driver installation error 109 */ 110 esp_err_t i2c_driver_install(i2c_port_t i2c_num, i2c_mode_t mode, size_t slv_rx_buf_len, size_t slv_tx_buf_len, int intr_alloc_flags); 111 112 /** 113 * @brief Delete I2C driver 114 * 115 * @note This function does not guarantee thread safety. 116 * Please make sure that no thread will continuously hold semaphores before calling the delete function. 117 * 118 * @param i2c_num I2C port to delete 119 * 120 * @return 121 * - ESP_OK Success 122 * - ESP_ERR_INVALID_ARG Parameter error 123 */ 124 esp_err_t i2c_driver_delete(i2c_port_t i2c_num); 125 126 /** 127 * @brief Configure an I2C bus with the given configuration. 128 * 129 * @param i2c_num I2C port to configure 130 * @param i2c_conf Pointer to the I2C configuration 131 * 132 * @return 133 * - ESP_OK Success 134 * - ESP_ERR_INVALID_ARG Parameter error 135 */ 136 esp_err_t i2c_param_config(i2c_port_t i2c_num, const i2c_config_t *i2c_conf); 137 138 /** 139 * @brief reset I2C tx hardware fifo 140 * 141 * @param i2c_num I2C port number 142 * 143 * @return 144 * - ESP_OK Success 145 * - ESP_ERR_INVALID_ARG Parameter error 146 */ 147 esp_err_t i2c_reset_tx_fifo(i2c_port_t i2c_num); 148 149 /** 150 * @brief reset I2C rx fifo 151 * 152 * @param i2c_num I2C port number 153 * 154 * @return 155 * - ESP_OK Success 156 * - ESP_ERR_INVALID_ARG Parameter error 157 */ 158 esp_err_t i2c_reset_rx_fifo(i2c_port_t i2c_num); 159 160 /** 161 * @brief Register an I2C ISR handler. 162 * 163 * @param i2c_num I2C port number to attach handler to 164 * @param fn ISR handler function 165 * @param arg Parameter for the ISR handler 166 * @param intr_alloc_flags Flags used to allocate the interrupt. One or multiple (ORred) 167 * ESP_INTR_FLAG_* values. See esp_intr_alloc.h for more info. 168 * @param handle Handle return from esp_intr_alloc. 169 * 170 * @return 171 * - ESP_OK Success 172 * - ESP_ERR_INVALID_ARG Parameter error 173 */ 174 esp_err_t i2c_isr_register(i2c_port_t i2c_num, void (*fn)(void *), void *arg, int intr_alloc_flags, intr_handle_t *handle); 175 176 /** 177 * @brief Delete and free I2C ISR handle. 178 * 179 * @param handle Handle of isr to delete. 180 * 181 * @return 182 * - ESP_OK Success 183 * - ESP_ERR_INVALID_ARG Parameter error 184 */ 185 esp_err_t i2c_isr_free(intr_handle_t handle); 186 187 /** 188 * @brief Configure GPIO pins for I2C SCK and SDA signals. 189 * 190 * @param i2c_num I2C port number 191 * @param sda_io_num GPIO number for I2C SDA signal 192 * @param scl_io_num GPIO number for I2C SCL signal 193 * @param sda_pullup_en Enable the internal pullup for SDA pin 194 * @param scl_pullup_en Enable the internal pullup for SCL pin 195 * @param mode I2C mode 196 * 197 * @return 198 * - ESP_OK Success 199 * - ESP_ERR_INVALID_ARG Parameter error 200 */ 201 esp_err_t i2c_set_pin(i2c_port_t i2c_num, int sda_io_num, int scl_io_num, 202 bool sda_pullup_en, bool scl_pullup_en, i2c_mode_t mode); 203 204 /** 205 * @brief Perform a write to a device connected to a particular I2C port. 206 * This function is a wrapper to `i2c_master_start()`, `i2c_master_write()`, `i2c_master_read()`, etc... 207 * It shall only be called in I2C master mode. 208 * 209 * @param i2c_num I2C port number to perform the transfer on 210 * @param device_address I2C device's 7-bit address 211 * @param write_buffer Bytes to send on the bus 212 * @param write_size Size, in bytes, of the write buffer 213 * @param ticks_to_wait Maximum ticks to wait before issuing a timeout. 214 * 215 * @return 216 * - ESP_OK Success 217 * - ESP_ERR_INVALID_ARG Parameter error 218 * - ESP_FAIL Sending command error, slave hasn't ACK the transfer. 219 * - ESP_ERR_INVALID_STATE I2C driver not installed or not in master mode. 220 * - ESP_ERR_TIMEOUT Operation timeout because the bus is busy. 221 */ 222 esp_err_t i2c_master_write_to_device(i2c_port_t i2c_num, uint8_t device_address, 223 const uint8_t* write_buffer, size_t write_size, 224 TickType_t ticks_to_wait); 225 226 /** 227 * @brief Perform a read to a device connected to a particular I2C port. 228 * This function is a wrapper to `i2c_master_start()`, `i2c_master_write()`, `i2c_master_read()`, etc... 229 * It shall only be called in I2C master mode. 230 * 231 * @param i2c_num I2C port number to perform the transfer on 232 * @param device_address I2C device's 7-bit address 233 * @param read_buffer Buffer to store the bytes received on the bus 234 * @param read_size Size, in bytes, of the read buffer 235 * @param ticks_to_wait Maximum ticks to wait before issuing a timeout. 236 * 237 * @return 238 * - ESP_OK Success 239 * - ESP_ERR_INVALID_ARG Parameter error 240 * - ESP_FAIL Sending command error, slave hasn't ACK the transfer. 241 * - ESP_ERR_INVALID_STATE I2C driver not installed or not in master mode. 242 * - ESP_ERR_TIMEOUT Operation timeout because the bus is busy. 243 */ 244 esp_err_t i2c_master_read_from_device(i2c_port_t i2c_num, uint8_t device_address, 245 uint8_t* read_buffer, size_t read_size, 246 TickType_t ticks_to_wait); 247 248 /** 249 * @brief Perform a write followed by a read to a device on the I2C bus. 250 * A repeated start signal is used between the `write` and `read`, thus, the bus is 251 * not released until the two transactions are finished. 252 * This function is a wrapper to `i2c_master_start()`, `i2c_master_write()`, `i2c_master_read()`, etc... 253 * It shall only be called in I2C master mode. 254 * 255 * @param i2c_num I2C port number to perform the transfer on 256 * @param device_address I2C device's 7-bit address 257 * @param write_buffer Bytes to send on the bus 258 * @param write_size Size, in bytes, of the write buffer 259 * @param read_buffer Buffer to store the bytes received on the bus 260 * @param read_size Size, in bytes, of the read buffer 261 * @param ticks_to_wait Maximum ticks to wait before issuing a timeout. 262 * 263 * @return 264 * - ESP_OK Success 265 * - ESP_ERR_INVALID_ARG Parameter error 266 * - ESP_FAIL Sending command error, slave hasn't ACK the transfer. 267 * - ESP_ERR_INVALID_STATE I2C driver not installed or not in master mode. 268 * - ESP_ERR_TIMEOUT Operation timeout because the bus is busy. 269 */ 270 esp_err_t i2c_master_write_read_device(i2c_port_t i2c_num, uint8_t device_address, 271 const uint8_t* write_buffer, size_t write_size, 272 uint8_t* read_buffer, size_t read_size, 273 TickType_t ticks_to_wait); 274 275 276 /** 277 * @brief Create and initialize an I2C commands list with a given buffer. 278 * All the allocations for data or signals (START, STOP, ACK, ...) will be 279 * performed within this buffer. 280 * This buffer must be valid during the whole transaction. 281 * After finishing the I2C transactions, it is required to call `i2c_cmd_link_delete_static()`. 282 * 283 * @note It is **highly** advised to not allocate this buffer on the stack. The size of the data 284 * used underneath may increase in the future, resulting in a possible stack overflow as the macro 285 * `I2C_LINK_RECOMMENDED_SIZE` would also return a bigger value. 286 * A better option is to use a buffer allocated statically or dynamically (with `malloc`). 287 * 288 * @param buffer Buffer to use for commands allocations 289 * @param size Size in bytes of the buffer 290 * 291 * @return Handle to the I2C command link or NULL if the buffer provided is too small, please 292 * use `I2C_LINK_RECOMMENDED_SIZE` macro to get the recommended size for the buffer. 293 */ 294 i2c_cmd_handle_t i2c_cmd_link_create_static(uint8_t* buffer, uint32_t size); 295 296 /** 297 * @brief Create and initialize an I2C commands list with a given buffer. 298 * After finishing the I2C transactions, it is required to call `i2c_cmd_link_delete()` 299 * to release and return the resources. 300 * The required bytes will be dynamically allocated. 301 * 302 * @return Handle to the I2C command link 303 */ 304 i2c_cmd_handle_t i2c_cmd_link_create(void); 305 306 /** 307 * @brief Free the I2C commands list allocated statically with `i2c_cmd_link_create_static`. 308 * 309 * @param cmd_handle I2C commands list allocated statically. This handle should be created thanks to 310 * `i2c_cmd_link_create_static()` function 311 */ 312 void i2c_cmd_link_delete_static(i2c_cmd_handle_t cmd_handle); 313 314 /** 315 * @brief Free the I2C commands list 316 * 317 * @param cmd_handle I2C commands list. This handle should be created thanks to 318 * `i2c_cmd_link_create()` function 319 */ 320 void i2c_cmd_link_delete(i2c_cmd_handle_t cmd_handle); 321 322 /** 323 * @brief Queue a "START signal" to the given commands list. 324 * This function shall only be called in I2C master mode. 325 * Call `i2c_master_cmd_begin()` to send all the queued commands. 326 * 327 * @param cmd_handle I2C commands list 328 * 329 * @return 330 * - ESP_OK Success 331 * - ESP_ERR_INVALID_ARG Parameter error 332 * - ESP_ERR_NO_MEM The static buffer used to create `cmd_handler` is too small 333 * - ESP_FAIL No more memory left on the heap 334 */ 335 esp_err_t i2c_master_start(i2c_cmd_handle_t cmd_handle); 336 337 /** 338 * @brief Queue a "write byte" command to the commands list. 339 * A single byte will be sent on the I2C port. This function shall only be 340 * called in I2C master mode. 341 * Call `i2c_master_cmd_begin()` to send all queued commands 342 * 343 * @param cmd_handle I2C commands list 344 * @param data Byte to send on the port 345 * @param ack_en Enable ACK signal 346 * 347 * @return 348 * - ESP_OK Success 349 * - ESP_ERR_INVALID_ARG Parameter error 350 * - ESP_ERR_NO_MEM The static buffer used to create `cmd_handler` is too small 351 * - ESP_FAIL No more memory left on the heap 352 */ 353 esp_err_t i2c_master_write_byte(i2c_cmd_handle_t cmd_handle, uint8_t data, bool ack_en); 354 355 /** 356 * @brief Queue a "write (multiple) bytes" command to the commands list. 357 * This function shall only be called in I2C master mode. 358 * Call `i2c_master_cmd_begin()` to send all queued commands 359 * 360 * @param cmd_handle I2C commands list 361 * @param data Bytes to send. This buffer shall remain **valid** until the transaction is finished. 362 * If the PSRAM is enabled and `intr_flag` is set to `ESP_INTR_FLAG_IRAM`, 363 * `data` should be allocated from internal RAM. 364 * @param data_len Length, in bytes, of the data buffer 365 * @param ack_en Enable ACK signal 366 * 367 * @return 368 * - ESP_OK Success 369 * - ESP_ERR_INVALID_ARG Parameter error 370 * - ESP_ERR_NO_MEM The static buffer used to create `cmd_handler` is too small 371 * - ESP_FAIL No more memory left on the heap 372 */ 373 esp_err_t i2c_master_write(i2c_cmd_handle_t cmd_handle, const uint8_t *data, size_t data_len, bool ack_en); 374 375 /** 376 * @brief Queue a "read byte" command to the commands list. 377 * A single byte will be read on the I2C bus. This function shall only be 378 * called in I2C master mode. 379 * Call `i2c_master_cmd_begin()` to send all queued commands 380 * 381 * @param cmd_handle I2C commands list 382 * @param data Pointer where the received byte will the stored. This buffer shall remain **valid** 383 * until the transaction is finished. 384 * @param ack ACK signal 385 * 386 * @return 387 * - ESP_OK Success 388 * - ESP_ERR_INVALID_ARG Parameter error 389 * - ESP_ERR_NO_MEM The static buffer used to create `cmd_handler` is too small 390 * - ESP_FAIL No more memory left on the heap 391 */ 392 esp_err_t i2c_master_read_byte(i2c_cmd_handle_t cmd_handle, uint8_t *data, i2c_ack_type_t ack); 393 394 /** 395 * @brief Queue a "read (multiple) bytes" command to the commands list. 396 * Multiple bytes will be read on the I2C bus. This function shall only be 397 * called in I2C master mode. 398 * Call `i2c_master_cmd_begin()` to send all queued commands 399 * 400 * @param cmd_handle I2C commands list 401 * @param data Pointer where the received bytes will the stored. This buffer shall remain **valid** 402 * until the transaction is finished. 403 * @param data_len Size, in bytes, of the `data` buffer 404 * @param ack ACK signal 405 * 406 * @return 407 * - ESP_OK Success 408 * - ESP_ERR_INVALID_ARG Parameter error 409 * - ESP_ERR_NO_MEM The static buffer used to create `cmd_handler` is too small 410 * - ESP_FAIL No more memory left on the heap 411 */ 412 esp_err_t i2c_master_read(i2c_cmd_handle_t cmd_handle, uint8_t *data, size_t data_len, i2c_ack_type_t ack); 413 414 /** 415 * @brief Queue a "STOP signal" to the given commands list. 416 * This function shall only be called in I2C master mode. 417 * Call `i2c_master_cmd_begin()` to send all the queued commands. 418 * 419 * @param cmd_handle I2C commands list 420 * 421 * @return 422 * - ESP_OK Success 423 * - ESP_ERR_INVALID_ARG Parameter error 424 * - ESP_ERR_NO_MEM The static buffer used to create `cmd_handler` is too small 425 * - ESP_FAIL No more memory left on the heap 426 */ 427 esp_err_t i2c_master_stop(i2c_cmd_handle_t cmd_handle); 428 429 /** 430 * @brief Send all the queued commands on the I2C bus, in master mode. 431 * The task will be blocked until all the commands have been sent out. 432 * The I2C APIs are not thread-safe, if you want to use one I2C port in different tasks, 433 * you need to take care of the multi-thread issue. 434 * This function shall only be called in I2C master mode. 435 * 436 * @param i2c_num I2C port number 437 * @param cmd_handle I2C commands list 438 * @param ticks_to_wait Maximum ticks to wait before issuing a timeout. 439 * 440 * @return 441 * - ESP_OK Success 442 * - ESP_ERR_INVALID_ARG Parameter error 443 * - ESP_FAIL Sending command error, slave hasn't ACK the transfer. 444 * - ESP_ERR_INVALID_STATE I2C driver not installed or not in master mode. 445 * - ESP_ERR_TIMEOUT Operation timeout because the bus is busy. 446 */ 447 esp_err_t i2c_master_cmd_begin(i2c_port_t i2c_num, i2c_cmd_handle_t cmd_handle, TickType_t ticks_to_wait); 448 449 /** 450 * @brief Write bytes to internal ringbuffer of the I2C slave data. When the TX fifo empty, the ISR will 451 * fill the hardware FIFO with the internal ringbuffer's data. 452 * @note This function shall only be called in I2C slave mode. 453 * 454 * @param i2c_num I2C port number 455 * @param data Bytes to write into internal buffer 456 * @param size Size, in bytes, of `data` buffer 457 * @param ticks_to_wait Maximum ticks to wait. 458 * 459 * @return 460 * - ESP_FAIL (-1) Parameter error 461 * - Other (>=0) The number of data bytes pushed to the I2C slave buffer. 462 */ 463 int i2c_slave_write_buffer(i2c_port_t i2c_num, const uint8_t *data, int size, TickType_t ticks_to_wait); 464 465 /** 466 * @brief Read bytes from I2C internal buffer. When the I2C bus receives data, the ISR will copy them 467 * from the hardware RX FIFO to the internal ringbuffer. 468 * Calling this function will then copy bytes from the internal ringbuffer to the `data` user buffer. 469 * @note This function shall only be called in I2C slave mode. 470 * 471 * @param i2c_num I2C port number 472 * @param data Buffer to fill with ringbuffer's bytes 473 * @param max_size Maximum bytes to read 474 * @param ticks_to_wait Maximum waiting ticks 475 * 476 * @return 477 * - ESP_FAIL(-1) Parameter error 478 * - Others(>=0) The number of data bytes read from I2C slave buffer. 479 */ 480 int i2c_slave_read_buffer(i2c_port_t i2c_num, uint8_t *data, size_t max_size, TickType_t ticks_to_wait); 481 482 /** 483 * @brief Set I2C master clock period 484 * 485 * @param i2c_num I2C port number 486 * @param high_period Clock cycle number during SCL is high level, high_period is a 14 bit value 487 * @param low_period Clock cycle number during SCL is low level, low_period is a 14 bit value 488 * 489 * @return 490 * - ESP_OK Success 491 * - ESP_ERR_INVALID_ARG Parameter error 492 */ 493 esp_err_t i2c_set_period(i2c_port_t i2c_num, int high_period, int low_period); 494 495 /** 496 * @brief Get I2C master clock period 497 * 498 * @param i2c_num I2C port number 499 * @param high_period pointer to get clock cycle number during SCL is high level, will get a 14 bit value 500 * @param low_period pointer to get clock cycle number during SCL is low level, will get a 14 bit value 501 * 502 * @return 503 * - ESP_OK Success 504 * - ESP_ERR_INVALID_ARG Parameter error 505 */ 506 esp_err_t i2c_get_period(i2c_port_t i2c_num, int *high_period, int *low_period); 507 508 /** 509 * @brief Enable hardware filter on I2C bus 510 * Sometimes the I2C bus is disturbed by high frequency noise(about 20ns), or the rising edge of 511 * the SCL clock is very slow, these may cause the master state machine to break. 512 * Enable hardware filter can filter out high frequency interference and make the master more stable. 513 * @note Enable filter will slow down the SCL clock. 514 * 515 * @param i2c_num I2C port number to filter 516 * @param cyc_num the APB cycles need to be filtered (0<= cyc_num <=7). 517 * When the period of a pulse is less than cyc_num * APB_cycle, the I2C controller will ignore this pulse. 518 * 519 * @return 520 * - ESP_OK Success 521 * - ESP_ERR_INVALID_ARG Parameter error 522 */ 523 esp_err_t i2c_filter_enable(i2c_port_t i2c_num, uint8_t cyc_num); 524 525 /** 526 * @brief Disable filter on I2C bus 527 * 528 * @param i2c_num I2C port number 529 * 530 * @return 531 * - ESP_OK Success 532 * - ESP_ERR_INVALID_ARG Parameter error 533 */ 534 esp_err_t i2c_filter_disable(i2c_port_t i2c_num); 535 536 /** 537 * @brief set I2C master start signal timing 538 * 539 * @param i2c_num I2C port number 540 * @param setup_time clock number between the falling-edge of SDA and rising-edge of SCL for start mark, it's a 10-bit value. 541 * @param hold_time clock num between the falling-edge of SDA and falling-edge of SCL for start mark, it's a 10-bit value. 542 * 543 * @return 544 * - ESP_OK Success 545 * - ESP_ERR_INVALID_ARG Parameter error 546 */ 547 esp_err_t i2c_set_start_timing(i2c_port_t i2c_num, int setup_time, int hold_time); 548 549 /** 550 * @brief get I2C master start signal timing 551 * 552 * @param i2c_num I2C port number 553 * @param setup_time pointer to get setup time 554 * @param hold_time pointer to get hold time 555 * 556 * @return 557 * - ESP_OK Success 558 * - ESP_ERR_INVALID_ARG Parameter error 559 */ 560 esp_err_t i2c_get_start_timing(i2c_port_t i2c_num, int *setup_time, int *hold_time); 561 562 /** 563 * @brief set I2C master stop signal timing 564 * 565 * @param i2c_num I2C port number 566 * @param setup_time clock num between the rising-edge of SCL and the rising-edge of SDA, it's a 10-bit value. 567 * @param hold_time clock number after the STOP bit's rising-edge, it's a 14-bit value. 568 * 569 * @return 570 * - ESP_OK Success 571 * - ESP_ERR_INVALID_ARG Parameter error 572 */ 573 esp_err_t i2c_set_stop_timing(i2c_port_t i2c_num, int setup_time, int hold_time); 574 575 /** 576 * @brief get I2C master stop signal timing 577 * 578 * @param i2c_num I2C port number 579 * @param setup_time pointer to get setup time. 580 * @param hold_time pointer to get hold time. 581 * 582 * @return 583 * - ESP_OK Success 584 * - ESP_ERR_INVALID_ARG Parameter error 585 */ 586 esp_err_t i2c_get_stop_timing(i2c_port_t i2c_num, int *setup_time, int *hold_time); 587 588 /** 589 * @brief set I2C data signal timing 590 * 591 * @param i2c_num I2C port number 592 * @param sample_time clock number I2C used to sample data on SDA after the rising-edge of SCL, it's a 10-bit value 593 * @param hold_time clock number I2C used to hold the data after the falling-edge of SCL, it's a 10-bit value 594 * 595 * @return 596 * - ESP_OK Success 597 * - ESP_ERR_INVALID_ARG Parameter error 598 */ 599 esp_err_t i2c_set_data_timing(i2c_port_t i2c_num, int sample_time, int hold_time); 600 601 /** 602 * @brief get I2C data signal timing 603 * 604 * @param i2c_num I2C port number 605 * @param sample_time pointer to get sample time 606 * @param hold_time pointer to get hold time 607 * 608 * @return 609 * - ESP_OK Success 610 * - ESP_ERR_INVALID_ARG Parameter error 611 */ 612 esp_err_t i2c_get_data_timing(i2c_port_t i2c_num, int *sample_time, int *hold_time); 613 614 /** 615 * @brief set I2C timeout value 616 * @param i2c_num I2C port number 617 * @param timeout timeout value for I2C bus (unit: APB 80Mhz clock cycle) 618 * @return 619 * - ESP_OK Success 620 * - ESP_ERR_INVALID_ARG Parameter error 621 */ 622 esp_err_t i2c_set_timeout(i2c_port_t i2c_num, int timeout); 623 624 /** 625 * @brief get I2C timeout value 626 * @param i2c_num I2C port number 627 * @param timeout pointer to get timeout value 628 * @return 629 * - ESP_OK Success 630 * - ESP_ERR_INVALID_ARG Parameter error 631 */ 632 esp_err_t i2c_get_timeout(i2c_port_t i2c_num, int *timeout); 633 634 /** 635 * @brief set I2C data transfer mode 636 * 637 * @param i2c_num I2C port number 638 * @param tx_trans_mode I2C sending data mode 639 * @param rx_trans_mode I2C receving data mode 640 * 641 * @return 642 * - ESP_OK Success 643 * - ESP_ERR_INVALID_ARG Parameter error 644 */ 645 esp_err_t i2c_set_data_mode(i2c_port_t i2c_num, i2c_trans_mode_t tx_trans_mode, i2c_trans_mode_t rx_trans_mode); 646 647 /** 648 * @brief get I2C data transfer mode 649 * 650 * @param i2c_num I2C port number 651 * @param tx_trans_mode pointer to get I2C sending data mode 652 * @param rx_trans_mode pointer to get I2C receiving data mode 653 * 654 * @return 655 * - ESP_OK Success 656 * - ESP_ERR_INVALID_ARG Parameter error 657 */ 658 esp_err_t i2c_get_data_mode(i2c_port_t i2c_num, i2c_trans_mode_t *tx_trans_mode, i2c_trans_mode_t *rx_trans_mode); 659 660 #ifdef __cplusplus 661 } 662 #endif 663 664 #endif /*_DRIVER_I2C_H_*/ 665