Files
2025-08-12 11:19:29 +08:00

97 lines
3.0 KiB
C

/*
SPDX-License-Identifier: MIT
MIT License
Copyright (c) 2021 Rop Gonggrijp.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef __I2CDEV_H__
#define __I2CDEV_H__
#include "driver/i2c_master.h"
#include "hal/i2c_types.h"
#include <esp_err.h>
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#ifdef __cplusplus
extern "C" {
#endif
// i2cdev-compatible I2C device descriptor for ESP-IDF v5.x
typedef struct
{
i2c_port_t port;
uint8_t addr;
i2c_master_dev_handle_t dev_handle; // 新的设备句柄
SemaphoreHandle_t mutex;
uint32_t timeout_ticks;
} i2c_dev_t;
// 函数声明
esp_err_t i2cdev_init();
esp_err_t i2cdev_done();
esp_err_t i2c_dev_create_mutex(i2c_dev_t *dev);
esp_err_t i2c_dev_delete_mutex(i2c_dev_t *dev);
esp_err_t i2c_dev_take_mutex(i2c_dev_t *dev);
esp_err_t i2c_dev_give_mutex(i2c_dev_t *dev);
esp_err_t i2c_dev_read(const i2c_dev_t *dev, const void *out_data,
size_t out_size, void *in_data, size_t in_size);
esp_err_t i2c_dev_write(const i2c_dev_t *dev, const void *out_reg,
size_t out_reg_size, const void *out_data, size_t out_size);
esp_err_t i2c_dev_read_reg(const i2c_dev_t *dev, uint8_t reg,
void *in_data, size_t in_size);
esp_err_t i2c_dev_write_reg(const i2c_dev_t *dev, uint8_t reg,
const void *out_data, size_t out_size);
#define I2C_DEV_TAKE_MUTEX(dev) do { \
esp_err_t __ = i2c_dev_take_mutex(dev); \
if (__ != ESP_OK) return __;\
} while (0)
#define I2C_DEV_GIVE_MUTEX(dev) do { \
esp_err_t __ = i2c_dev_give_mutex(dev); \
if (__ != ESP_OK) return __;\
} while (0)
#define I2C_DEV_CHECK(dev, X) do { \
esp_err_t ___ = X; \
if (___ != ESP_OK) { \
I2C_DEV_GIVE_MUTEX(dev); \
return ___; \
} \
} while (0)
#define I2C_DEV_CHECK_LOGE(dev, X, msg, ...) do { \
esp_err_t ___ = X; \
if (___ != ESP_OK) { \
I2C_DEV_GIVE_MUTEX(dev); \
ESP_LOGE(TAG, msg, ## __VA_ARGS__); \
return ___; \
} \
} while (0)
#ifdef __cplusplus
}
#endif
#endif /* __I2CDEV_H__ */