|
@@ -1,76 +1,95 @@
|
|
|
/*
|
|
|
- * Copyright (c) 2006-2022, RT-Thread Development Team
|
|
|
+ * Copyright (c) 2006-2024, RT-Thread Development Team
|
|
|
*
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
*
|
|
|
* Change Logs:
|
|
|
* Date Author Notes
|
|
|
* 2021-09-13 AisinoChip first implementation.
|
|
|
+ * 2024-04-23 LZero Modify the I2C framework.
|
|
|
*/
|
|
|
|
|
|
-#include <board.h>
|
|
|
+#ifdef BSP_USING_SOFT_I2C
|
|
|
|
|
|
-#ifdef RT_USING_I2C_BITOPS
|
|
|
+#include "drv_soft_i2c.h"
|
|
|
|
|
|
-#include <rtdevice.h>
|
|
|
-#include <drivers/pin.h>
|
|
|
+#define DBG_TAG "drv.i2c"
|
|
|
+#ifdef DRV_DEBUG
|
|
|
+ #define DBG_LVL DBG_LOG
|
|
|
+#else
|
|
|
+ #define DBG_LVL DBG_INFO
|
|
|
+#endif /* DRV_DEBUG */
|
|
|
|
|
|
-#define I2C_BUS_NAME "i2cs"
|
|
|
+static struct acm32_soft_i2c_config soft_i2c_config[] =
|
|
|
+{
|
|
|
+#ifdef BSP_USING_I2C0
|
|
|
+ I2C0_BUS_CONFIG,
|
|
|
+#endif
|
|
|
+};
|
|
|
|
|
|
-/* user should change this to adapt specific board */
|
|
|
-#define I2C_SCL_PIN GPIO_PIN_6
|
|
|
-#define I2C_SCL_PORT GPIOD
|
|
|
-#define I2C_SDA_PIN GPIO_PIN_7
|
|
|
-#define I2C_SDA_PORT GPIOD
|
|
|
+static struct acm32_i2c i2c_obj[sizeof(soft_i2c_config) / sizeof(soft_i2c_config[0])];
|
|
|
|
|
|
-struct acm32_i2c_bit_data
|
|
|
+static void drv_i2c_gpio_init(struct acm32_i2c* i2c)
|
|
|
{
|
|
|
- struct
|
|
|
+ struct acm32_soft_i2c_config* cfg = (struct acm32_soft_i2c_config*)i2c->ops.data;
|
|
|
+
|
|
|
+ rt_pin_mode(cfg->scl, PIN_MODE_OUTPUT_OD);
|
|
|
+ rt_pin_mode(cfg->sda, PIN_MODE_OUTPUT_OD);
|
|
|
+
|
|
|
+ rt_pin_write(cfg->scl, PIN_HIGH);
|
|
|
+ rt_pin_write(cfg->sda, PIN_HIGH);
|
|
|
+}
|
|
|
+
|
|
|
+static void acm32_i2c_pin_init(void)
|
|
|
+{
|
|
|
+ rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct acm32_i2c);
|
|
|
+
|
|
|
+ for(rt_size_t i = 0; i < obj_num; i++)
|
|
|
{
|
|
|
- enum_GPIOx_t port;
|
|
|
- rt_uint32_t pin;
|
|
|
- } scl, sda;
|
|
|
-};
|
|
|
+ drv_i2c_gpio_init(&i2c_obj[i]);
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
static void _set_sda(void *data, rt_int32_t state)
|
|
|
{
|
|
|
- struct acm32_i2c_bit_data* bd = data;
|
|
|
+ struct acm32_soft_i2c_config* cfg = (struct acm32_soft_i2c_config*)data;
|
|
|
|
|
|
if (state)
|
|
|
{
|
|
|
- HAL_GPIO_WritePin(bd->sda.port, bd->sda.pin, GPIO_PIN_SET);
|
|
|
+ rt_pin_write(cfg->sda, PIN_HIGH);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- HAL_GPIO_WritePin(bd->sda.port, bd->sda.pin, GPIO_PIN_CLEAR);
|
|
|
+ rt_pin_write(cfg->sda, PIN_LOW);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
static void _set_scl(void *data, rt_int32_t state)
|
|
|
{
|
|
|
- struct acm32_i2c_bit_data* bd = data;
|
|
|
+ struct acm32_soft_i2c_config* cfg = (struct acm32_soft_i2c_config*)data;
|
|
|
+
|
|
|
if (state)
|
|
|
{
|
|
|
- HAL_GPIO_WritePin(bd->scl.port, bd->scl.pin, GPIO_PIN_SET);
|
|
|
+ rt_pin_write(cfg->scl, PIN_HIGH);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- HAL_GPIO_WritePin(bd->scl.port, bd->scl.pin, GPIO_PIN_CLEAR);
|
|
|
+ rt_pin_write(cfg->scl, PIN_LOW);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
static rt_int32_t _get_sda(void *data)
|
|
|
{
|
|
|
- struct acm32_i2c_bit_data* bd = data;
|
|
|
+ struct acm32_soft_i2c_config* cfg = (struct acm32_soft_i2c_config*)data;
|
|
|
|
|
|
- return HAL_GPIO_ReadPin(bd->sda.port, bd->sda.pin);
|
|
|
+ return rt_pin_read(cfg->sda);
|
|
|
}
|
|
|
|
|
|
static rt_int32_t _get_scl(void *data)
|
|
|
{
|
|
|
- struct acm32_i2c_bit_data* bd = data;
|
|
|
+ struct acm32_soft_i2c_config* cfg = (struct acm32_soft_i2c_config*)data;
|
|
|
|
|
|
- return HAL_GPIO_ReadPin(bd->scl.port, bd->scl.pin);
|
|
|
+ return rt_pin_read(cfg->scl);
|
|
|
}
|
|
|
|
|
|
static void acm32_udelay(rt_uint32_t us)
|
|
@@ -103,55 +122,40 @@ static void acm32_udelay(rt_uint32_t us)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-static void drv_i2c_gpio_init(const struct acm32_i2c_bit_data* bd)
|
|
|
+static const struct rt_i2c_bit_ops acm32_bit_ops_default =
|
|
|
{
|
|
|
- GPIO_InitTypeDef GPIO_Handle;
|
|
|
-
|
|
|
- GPIO_Handle.Pin = bd->sda.pin;
|
|
|
- GPIO_Handle.Mode = GPIO_MODE_OUTPUT_OD;
|
|
|
- GPIO_Handle.Pull = GPIO_PULLUP;
|
|
|
- GPIO_Handle.Alternate = GPIO_FUNCTION_0;
|
|
|
- HAL_GPIO_Init(bd->sda.port, &GPIO_Handle);
|
|
|
-
|
|
|
- GPIO_Handle.Pin = bd->scl.pin;
|
|
|
- GPIO_Handle.Mode = GPIO_MODE_OUTPUT_OD;
|
|
|
- GPIO_Handle.Pull = GPIO_PULLUP;
|
|
|
- GPIO_Handle.Alternate = GPIO_FUNCTION_0;
|
|
|
- HAL_GPIO_Init(bd->scl.port, &GPIO_Handle);
|
|
|
-
|
|
|
- _set_sda((void*)bd, 1);
|
|
|
- _set_scl((void*)bd, 1);
|
|
|
-}
|
|
|
+ .data = RT_NULL,
|
|
|
+ .pin_init = acm32_i2c_pin_init,
|
|
|
+ .set_sda = _set_sda,
|
|
|
+ .set_scl = _set_scl,
|
|
|
+ .get_sda = _get_sda,
|
|
|
+ .get_scl = _get_scl,
|
|
|
+ .udelay = acm32_udelay,
|
|
|
+ .delay_us = 1,
|
|
|
+ .timeout = 100,
|
|
|
+ .i2c_pin_init_flag = RT_FALSE
|
|
|
+};
|
|
|
|
|
|
int rt_soft_i2c_init(void)
|
|
|
{
|
|
|
- static struct rt_i2c_bus_device i2c_device;
|
|
|
- static const struct acm32_i2c_bit_data _i2c_bdata =
|
|
|
- {
|
|
|
- /* SCL */
|
|
|
- { I2C_SCL_PORT, I2C_SCL_PIN},
|
|
|
- /* SDA */
|
|
|
- { I2C_SDA_PORT, I2C_SDA_PIN},
|
|
|
- };
|
|
|
+ rt_err_t result;
|
|
|
|
|
|
- static const struct rt_i2c_bit_ops _i2c_bit_ops =
|
|
|
+ for (rt_size_t i = 0; i < sizeof(i2c_obj) / sizeof(struct acm32_i2c); i++)
|
|
|
{
|
|
|
- (void*)&_i2c_bdata,
|
|
|
- _set_sda,
|
|
|
- _set_scl,
|
|
|
- _get_sda,
|
|
|
- _get_scl,
|
|
|
- acm32_udelay,
|
|
|
- 1,
|
|
|
- 100
|
|
|
- };
|
|
|
-
|
|
|
- drv_i2c_gpio_init(&_i2c_bdata);
|
|
|
-
|
|
|
- i2c_device.priv = (void *)&_i2c_bit_ops;
|
|
|
- rt_i2c_bit_add_bus(&i2c_device, I2C_BUS_NAME);
|
|
|
-
|
|
|
- return 0;
|
|
|
+ i2c_obj[i].ops = acm32_bit_ops_default;
|
|
|
+ i2c_obj[i].ops.data = (void*)&soft_i2c_config[i];
|
|
|
+ i2c_obj[i].i2c_bus.priv = &i2c_obj[i].ops;
|
|
|
+
|
|
|
+ result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c_bus, soft_i2c_config[i].bus_name);
|
|
|
+ RT_ASSERT(result == RT_EOK);
|
|
|
+
|
|
|
+ LOG_D("software simulation %s init done, pin scl: %d, pin sda: %d",
|
|
|
+ soft_i2c_config[i].bus_name,
|
|
|
+ soft_i2c_config[i].scl,
|
|
|
+ soft_i2c_config[i].sda);
|
|
|
+ }
|
|
|
+
|
|
|
+ return RT_EOK;
|
|
|
}
|
|
|
INIT_DEVICE_EXPORT(rt_soft_i2c_init);
|
|
|
|