summaryrefslogtreecommitdiff
path: root/components/drivers
diff options
context:
space:
mode:
authorwdfk-prog <[email protected]>2026-03-09 08:48:55 +0800
committerRbb666 <[email protected]>2026-03-12 15:15:02 +0800
commitb0af22a96ff58458aac3169c9b0f6834db547d94 (patch)
tree981ededf553d48f4ed32e38271b987480dadca8d /components/drivers
parent4617729c4c4d8ca231ba1ca9805bf3856a164950 (diff)
feat[i2c]: add bus configuration and max-hz control
Diffstat (limited to 'components/drivers')
-rw-r--r--components/drivers/i2c/dev_i2c_core.c45
-rw-r--r--components/drivers/include/drivers/dev_i2c.h19
2 files changed, 55 insertions, 9 deletions
diff --git a/components/drivers/i2c/dev_i2c_core.c b/components/drivers/i2c/dev_i2c_core.c
index 97c860f829..fd7774e034 100644
--- a/components/drivers/i2c/dev_i2c_core.c
+++ b/components/drivers/i2c/dev_i2c_core.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2006-2023, RT-Thread Development Team
+ * Copyright (c) 2006-2024, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
@@ -7,6 +7,7 @@
* Date Author Notes
* 2012-04-25 weety first version
* 2021-04-20 RiceChen added support for bus control api
+ * 2024-06-23 wdfk-prog Add max_hz setting
*/
#include <rtdevice.h>
@@ -101,16 +102,42 @@ rt_err_t rt_i2c_control(struct rt_i2c_bus_device *bus,
{
rt_err_t ret;
- if(bus->ops->i2c_bus_control)
+ switch (cmd)
{
- ret = bus->ops->i2c_bus_control(bus, cmd, args);
- return ret;
- }
- else
- {
- LOG_E("I2C bus operation not supported");
- return -RT_EINVAL;
+ case RT_I2C_CTRL_SET_MAX_HZ:
+ {
+ if (args == RT_NULL)
+ {
+ return -RT_ERROR;
+ }
+
+ rt_uint32_t max_hz = *(rt_uint32_t *)args;
+ if(max_hz > 0)
+ {
+ bus->config.max_hz = max_hz;
+ }
+ else
+ {
+ return -RT_ERROR;
+ }
+ break;
+ }
+ default:
+ {
+ if(bus->ops->i2c_bus_control)
+ {
+ ret = bus->ops->i2c_bus_control(bus, cmd, args);
+ return ret;
+ }
+ else
+ {
+ LOG_E("I2C bus operation not supported");
+ return -RT_EINVAL;
+ }
+ break;
+ }
}
+ return RT_EOK;
}
rt_ssize_t rt_i2c_master_send(struct rt_i2c_bus_device *bus,
diff --git a/components/drivers/include/drivers/dev_i2c.h b/components/drivers/include/drivers/dev_i2c.h
index e704d512e0..869917855e 100644
--- a/components/drivers/include/drivers/dev_i2c.h
+++ b/components/drivers/include/drivers/dev_i2c.h
@@ -7,6 +7,7 @@
* Date Author Notes
* 2012-04-25 weety first version
* 2021-04-20 RiceChen added support for bus control api
+ * 2024-06-23 wdfk-prog Add the config struct
*/
#ifndef __DEV_I2C_H__
@@ -185,6 +186,8 @@ extern "C" {
#define RT_I2C_NO_READ_ACK (1u << 6) /* when I2C reading, we do not ACK */
#define RT_I2C_NO_STOP (1u << 7) /*!< do not generate STOP condition */
+#define RT_I2C_CTRL_SET_MAX_HZ 0x20
+
#define RT_I2C_DEV_CTRL_10BIT (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x01)
#define RT_I2C_DEV_CTRL_ADDR (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x02)
#define RT_I2C_DEV_CTRL_TIMEOUT (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x03)
@@ -234,6 +237,21 @@ struct rt_i2c_bus_device_ops
};
/**
+ * I2C configuration structure.
+ * mode : master: 0x00; slave: 0x01;
+ * max_hz: Maximum limit baud rate.
+ * usage_freq: Actual usage baud rate.
+ */
+struct rt_i2c_configuration
+{
+ rt_uint8_t mode;
+ rt_uint8_t reserved[3];
+
+ rt_uint32_t max_hz;
+ rt_uint32_t usage_freq;
+};
+
+/**
* @brief I2C Bus Device
*/
struct rt_i2c_bus_device
@@ -244,6 +262,7 @@ struct rt_i2c_bus_device
struct rt_mutex lock;
rt_uint32_t timeout;
rt_uint32_t retries;
+ struct rt_i2c_configuration config;
void *priv;
};