[i2c] adjust the timing for I2C initialization

This commit is contained in:
LZerro 2024-04-30 08:46:12 +08:00 committed by GitHub
parent 4c9d3be98a
commit f190cba0ab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
27 changed files with 436 additions and 139 deletions

View File

@ -47,6 +47,16 @@ static void ifx_i2c_gpio_init(struct ifx_i2c *i2c)
rt_pin_write(cfg->sda, PIN_HIGH);
}
static void ifx_i2c_pin_init(void)
{
rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct ifx_i2c);
for(rt_size_t i = 0; i < obj_num; i++)
{
ifx_i2c_gpio_init(&i2c_obj[i]);
}
}
/**
* This function sets the sda pin.
*
@ -112,13 +122,15 @@ static rt_int32_t ifx_get_scl(void *data)
static const struct rt_i2c_bit_ops ifx_bit_ops_default =
{
.data = RT_NULL,
.pin_init = ifx_i2c_pin_init,
.set_sda = ifx_set_sda,
.set_scl = ifx_set_scl,
.get_sda = ifx_get_sda,
.get_scl = ifx_get_scl,
.udelay = rt_hw_us_delay,
.delay_us = 1,
.timeout = 100
.timeout = 100,
.i2c_pin_init_flag = RT_FALSE
};
/**
@ -162,7 +174,7 @@ int rt_hw_i2c_init(void)
i2c_obj[i].ops = ifx_bit_ops_default;
i2c_obj[i].ops.data = (void *)&soft_i2c_config[i];
i2c_obj[i].i2c_bus.priv = &i2c_obj[i].ops;
ifx_i2c_gpio_init(&i2c_obj[i]);
result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c_bus, soft_i2c_config[i].bus_name);
RT_ASSERT(result == RT_EOK);
ifx_i2c_bus_unlock(&soft_i2c_config[i]);

View File

@ -106,6 +106,28 @@ menu "On-chip Peripheral Drivers"
select RT_USING_I2C
endmenu
menuconfig BSP_USING_SOFT_I2C
bool "Enable Soft I2C"
select RT_USING_I2C
select RT_USING_I2C_BITOPS
select RT_USING_PIN
default n
if BSP_USING_SOFT_I2C
menuconfig BSP_USING_I2C0
bool "Enable I2C1 BUS (software simulation)"
default n
if BSP_USING_I2C0
config BSP_I2C0_SCL_PIN
int "i2c0 scl pin number (PD, 6)"
range 0 63
default 54
config BSP_I2C0_SDA_PIN
int "I2C0 sda pin number (PD, 7)"
range 0 63
default 55
endif
endif
menu "Hardware CAN"
config BSP_USING_CAN1
bool "Enable CAN1"

View File

@ -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"
/* 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
struct acm32_i2c_bit_data
static struct acm32_soft_i2c_config soft_i2c_config[] =
{
struct
{
enum_GPIOx_t port;
rt_uint32_t pin;
} scl, sda;
#ifdef BSP_USING_I2C0
I2C0_BUS_CONFIG,
#endif
};
static struct acm32_i2c i2c_obj[sizeof(soft_i2c_config) / sizeof(soft_i2c_config[0])];
static void drv_i2c_gpio_init(struct acm32_i2c* i2c)
{
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++)
{
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 =
rt_err_t result;
for (rt_size_t i = 0; i < sizeof(i2c_obj) / sizeof(struct acm32_i2c); i++)
{
/* SCL */
{ I2C_SCL_PORT, I2C_SCL_PIN},
/* SDA */
{ I2C_SDA_PORT, I2C_SDA_PIN},
};
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);
static const struct rt_i2c_bit_ops _i2c_bit_ops =
{
(void*)&_i2c_bdata,
_set_sda,
_set_scl,
_get_sda,
_get_scl,
acm32_udelay,
1,
100
};
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);
}
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;
return RT_EOK;
}
INIT_DEVICE_EXPORT(rt_soft_i2c_init);

View File

@ -0,0 +1,44 @@
/*
* Copyright (c) 2006-2024, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2024-04-23 LZero Modify the I2C framework.
*/
#ifndef __DRV_I2C__
#define __DRV_I2C__
#include <rtthread.h>
#include <board.h>
#include <rtdbg.h>
#include <rtdevice.h>
#include <drivers/pin.h>
/* acm32 config class */
struct acm32_soft_i2c_config
{
rt_uint8_t scl;
rt_uint8_t sda;
const char *bus_name;
};
/* acm32 i2c dirver class */
struct acm32_i2c
{
struct rt_i2c_bit_ops ops;
struct rt_i2c_bus_device i2c_bus;
};
#ifdef BSP_USING_I2C0
#define I2C0_BUS_CONFIG \
{ \
.scl = BSP_I2C0_SCL_PIN, \
.sda = BSP_I2C0_SDA_PIN, \
.bus_name = "i2c0", \
}
#endif
#endif //__DRV_I2C__

View File

@ -56,6 +56,16 @@ static void air32_i2c_gpio_init(struct air32_i2c *i2c)
rt_pin_write(cfg->sda, PIN_HIGH);
}
static void air32_i2c_pin_init(void)
{
rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct air32_i2c);
for(rt_size_t i = 0; i < obj_num; i++)
{
air32_i2c_gpio_init(&i2c_obj[i]);
}
}
/**
* This function sets the sda pin.
*
@ -153,13 +163,15 @@ static void air32_udelay(rt_uint32_t us)
static const struct rt_i2c_bit_ops air32_bit_ops_default =
{
.data = RT_NULL,
.pin_init = air32_i2c_pin_init,
.set_sda = air32_set_sda,
.set_scl = air32_set_scl,
.get_sda = air32_get_sda,
.get_scl = air32_get_scl,
.udelay = air32_udelay,
.delay_us = 1,
.timeout = 100
.timeout = 100,
.i2c_pin_init_flag = RT_FALSE
};
/**
@ -201,7 +213,7 @@ int rt_sw_i2c_init(void)
i2c_obj[i].ops = air32_bit_ops_default;
i2c_obj[i].ops.data = (void*)&soft_i2c_config[i];
i2c_obj[i].i2c_bus.priv = &i2c_obj[i].ops;
air32_i2c_gpio_init(&i2c_obj[i]);
result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c_bus, soft_i2c_config[i].bus_name);
RT_ASSERT(result == RT_EOK);
air32_i2c_bus_unlock(&soft_i2c_config[i]);

View File

@ -86,6 +86,16 @@ static void apm32_soft_i2c_gpio_init(struct apm32_soft_i2c *i2c)
rt_pin_write(cfg->sda_pin, PIN_HIGH);
}
static void apm32_i2c_pin_init(void)
{
rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct apm32_soft_i2c);
for(rt_size_t i = 0; i < obj_num; i++)
{
apm32_soft_i2c_gpio_init(&i2c_obj[i]);
}
}
/**
* @brief This function sets the sda pin.
*
@ -199,13 +209,15 @@ static rt_err_t apm32_i2c_bus_unlock(const struct apm32_soft_i2c_config *cfg)
static const struct rt_i2c_bit_ops apm32_bit_ops_default =
{
.data = RT_NULL,
.pin_init = apm32_i2c_pin_init,
.set_sda = apm32_soft_i2c_set_sda,
.set_scl = apm32_soft_i2c_set_scl,
.get_sda = apm32_soft_i2c_get_sda,
.get_scl = apm32_soft_i2c_get_scl,
.udelay = apm32_soft_i2c_udelay,
.delay_us = 1,
.timeout = 100
.timeout = 100,
.i2c_pin_init_flag = RT_FALSE
};
/**
@ -218,12 +230,11 @@ int rt_hw_i2c_init(void)
rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct apm32_soft_i2c);
rt_err_t result;
for (int i = 0; i < obj_num; i++)
for (rt_size_t i = 0; i < obj_num; i++)
{
i2c_obj[i].ops = apm32_bit_ops_default;
i2c_obj[i].ops.data = (void *)&soft_i2c_config[i];
i2c_obj[i].i2c_bus.priv = &i2c_obj[i].ops;
apm32_soft_i2c_gpio_init(&i2c_obj[i]);
result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c_bus, soft_i2c_config[i].bus_name);

View File

@ -53,6 +53,16 @@ static void at32_i2c_gpio_init(struct at32_i2c *i2c)
rt_pin_write(cfg->sda, PIN_HIGH);
}
static void at32_i2c_pin_init(void)
{
rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct at32_i2c);
for(rt_size_t i = 0; i < obj_num; i++)
{
at32_i2c_gpio_init(&i2c_obj[i]);
}
}
/**
* this function sets the sda pin.
*
@ -151,13 +161,15 @@ static void at32_udelay(rt_uint32_t us)
static const struct rt_i2c_bit_ops at32_bit_ops_default =
{
.data = RT_NULL,
.pin_init = at32_i2c_pin_init,
.set_sda = at32_set_sda,
.set_scl = at32_set_scl,
.get_sda = at32_get_sda,
.get_scl = at32_get_scl,
.udelay = at32_udelay,
.delay_us = 1,
.timeout = 100
.timeout = 100,
.i2c_pin_init_flag = RT_FALSE
};
/**
@ -195,12 +207,12 @@ int rt_hw_i2c_init(void)
rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct at32_i2c);
rt_err_t result;
for (int i = 0; i < obj_num; i++)
for (rt_size_t i = 0; i < obj_num; i++)
{
i2c_obj[i].ops = at32_bit_ops_default;
i2c_obj[i].ops.data = (void*)&soft_i2c_config[i];
i2c_obj[i].i2c_bus.priv = &i2c_obj[i].ops;
at32_i2c_gpio_init(&i2c_obj[i]);
result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c_bus, soft_i2c_config[i].bus_name);
RT_ASSERT(result == RT_EOK);
at32_i2c_bus_unlock(&soft_i2c_config[i]);

View File

@ -13,7 +13,7 @@
#include <at91sam926x.h>
static void at91_i2c_gpio_init()
static void at91_i2c_gpio_init(void)
{
at91_sys_write(AT91_PMC_PCER, 1 << AT91SAM9260_ID_PIOA); //enable PIOA clock
at91_sys_write(AT91_PIOA + PIO_PUER, (1 << 23));
@ -83,11 +83,11 @@ static const struct rt_i2c_bit_ops bit_ops = {
at91_set_scl,
at91_get_sda,
at91_get_scl,
at91_udelay,
5,
100
100,
at91_i2c_gpio_init,
RT_FALSE
};
int at91_i2c_init(void)
@ -105,8 +105,6 @@ int at91_i2c_init(void)
bus->priv = (void *)&bit_ops;
at91_i2c_gpio_init();
rt_i2c_bit_add_bus(bus, "i2c0");
return 0;

View File

@ -13,7 +13,7 @@
#include <at91sam9g45.h>
static void at91_i2c_gpio_init()
static void at91_i2c_gpio_init(void)
{
AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOA; //enable PIOA clock
AT91C_BASE_PIOA->PIO_PUER = (1 << 23);
@ -83,11 +83,11 @@ static const struct rt_i2c_bit_ops bit_ops = {
at91_set_scl,
at91_get_sda,
at91_get_scl,
at91_udelay,
5,
100
100,
at91_i2c_gpio_init,
RT_FALSE
};
int at91_i2c_init(void)
@ -105,8 +105,6 @@ int at91_i2c_init(void)
bus->priv = (void *)&bit_ops;
at91_i2c_gpio_init();
rt_i2c_bit_add_bus(bus, "i2c0");
return 0;

View File

@ -39,6 +39,16 @@ static void avr32_i2c_gpio_init(struct avr32_i2c *i2c)
gpio_set_gpio_open_drain_pin(cfg->sda);
}
static void avr32_i2c_pin_init(void)
{
rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct avr32_i2c);
for(rt_size_t i = 0; i < obj_num; i++)
{
avr32_i2c_gpio_init(&i2c_obj[i]);
}
}
/**
* This function sets the SDA pin.
*
@ -102,13 +112,15 @@ static rt_int32_t avr32_get_scl(void *data)
static const struct rt_i2c_bit_ops avr32_bit_ops_default =
{
.data = RT_NULL,
.pin_init = avr32_i2c_pin_init,
.set_sda = avr32_set_sda,
.set_scl = avr32_set_scl,
.get_sda = avr32_get_sda,
.get_scl = avr32_get_scl,
.udelay = rt_hw_us_delay,
.delay_us = 1,
.timeout = 100
.timeout = 100,
.i2c_pin_init_flag = RT_FALSE
};
/**
@ -151,7 +163,7 @@ int rt_sw_i2c_init(void)
i2c_obj[i].ops = avr32_bit_ops_default;
i2c_obj[i].ops.data = (void*)&soft_i2c_config[i];
i2c_obj[i].i2c_bus.priv = &i2c_obj[i].ops;
avr32_i2c_gpio_init(&i2c_obj[i]);
result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c_bus, soft_i2c_config[i].bus_name);
RT_ASSERT(result == RT_EOK);
avr32_i2c_bus_unlock(&soft_i2c_config[i]);

View File

@ -58,6 +58,16 @@ static void ab32_i2c_gpio_init(struct ab32_i2c *i2c)
rt_pin_write(cfg->sda, PIN_HIGH);
}
static void ab32_i2c_pin_init(void)
{
rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct ab32_i2c);
for(rt_size_t i = 0; i < obj_num; i++)
{
ab32_i2c_gpio_init(&i2c_obj[i]);
}
}
/**
* This function sets the sda pin.
*
@ -145,13 +155,15 @@ static rt_int32_t ab32_get_scl(void *data)
static const struct rt_i2c_bit_ops ab32_bit_ops_default =
{
.data = RT_NULL,
.pin_init = ab32_i2c_pin_init,
.set_sda = ab32_set_sda,
.set_scl = ab32_set_scl,
.get_sda = ab32_get_sda,
.get_scl = ab32_get_scl,
.udelay = rt_hw_us_delay,
.delay_us = 1,
.timeout = 100
.timeout = 100,
.i2c_pin_init_flag = RT_FALSE
};
/**
@ -189,12 +201,12 @@ int rt_hw_i2c_init(void)
rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct ab32_i2c);
rt_err_t result;
for (int i = 0; i < obj_num; i++)
for (rt_size_t i = 0; i < obj_num; i++)
{
i2c_obj[i].ops = ab32_bit_ops_default;
i2c_obj[i].ops.data = (void*)&soft_i2c_config[i];
i2c_obj[i].i2c_bus.priv = &i2c_obj[i].ops;
ab32_i2c_gpio_init(&i2c_obj[i]);
result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c_bus, soft_i2c_config[i].bus_name);
RT_ASSERT(result == RT_EOK);
ab32_i2c_bus_unlock(&soft_i2c_config[i]);

View File

@ -140,6 +140,16 @@ static void bl_i2c_gpio_init(struct bl_i2c *i2c)
rt_pin_write(cfg->sda, PIN_HIGH);
}
static void bl_i2c_pin_init(void)
{
rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct bl_i2c);
for(rt_size_t i = 0; i < obj_num; i++)
{
bl_i2c_gpio_init(&i2c_obj[i]);
}
}
/**
* This function sets the sda pin.
*
@ -213,13 +223,15 @@ static void bl_udelay(rt_uint32_t us)
static const struct rt_i2c_bit_ops bl_bit_ops_default =
{
.data = RT_NULL,
.pin_init = bl_i2c_pin_init,
.set_sda = bl_set_sda,
.set_scl = bl_set_scl,
.get_sda = bl_get_sda,
.get_scl = bl_get_scl,
.udelay = bl_udelay,
.delay_us = 1,
.timeout = 100
.timeout = 100,
.i2c_pin_init_flag = RT_FALSE
};
/**
@ -261,7 +273,7 @@ int rt_hw_i2c_init(void)
i2c_obj[i].ops = bl_bit_ops_default;
i2c_obj[i].ops.data = (void*)&soft_i2c_config[i];
i2c_obj[i].i2c_bus.priv = &i2c_obj[i].ops;
bl_i2c_gpio_init(&i2c_obj[i]);
result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c_bus, soft_i2c_config[i].bus_name);
RT_ASSERT(result == RT_EOK);
bl_i2c_bus_unlock(&soft_i2c_config[i]);

View File

@ -53,6 +53,16 @@ static void gd32_i2c_gpio_init(struct gd32_i2c *i2c)
rt_pin_write(cfg->sda, PIN_HIGH);
}
static void gd32_i2c_pin_init(void)
{
rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct gd32_i2c);
for(rt_size_t i = 0; i < obj_num; i++)
{
gd32_i2c_gpio_init(&i2c_obj[i]);
}
}
/**
* @brief This function sets the sda pin.
* @param data, state
@ -129,13 +139,15 @@ static void gd32_udelay(rt_uint32_t us)
static const struct rt_i2c_bit_ops gd32_bit_ops_default =
{
.data = RT_NULL,
.pin_init = gd32_i2c_pin_init,
.set_sda = gd32_set_sda,
.set_scl = gd32_set_scl,
.get_sda = gd32_get_sda,
.get_scl = gd32_get_scl,
.udelay = gd32_udelay,
.delay_us = 1,
.timeout = 100
.timeout = 100,
.i2c_pin_init_flag = RT_FALSE
};
/**
@ -175,12 +187,11 @@ int rt_hw_i2c_init(void)
rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct gd32_i2c);
rt_err_t result;
for (int i = 0; i < obj_num; i++)
for (rt_size_t i = 0; i < obj_num; i++)
{
i2c_obj[i].ops = gd32_bit_ops_default;
i2c_obj[i].ops.data = (void*)&soft_i2c_config[i];
i2c_obj[i].i2c_bus.priv = &i2c_obj[i].ops;
gd32_i2c_gpio_init(&i2c_obj[i]);
result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c_bus, soft_i2c_config[i].bus_name);

View File

@ -53,6 +53,16 @@ static void gd32_i2c_gpio_init(struct gd32_i2c *i2c)
rt_pin_write(cfg->sda, PIN_HIGH);
}
static void gd32_i2c_pin_init(void)
{
rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct gd32_i2c);
for(rt_size_t i = 0; i < obj_num; i++)
{
gd32_i2c_gpio_init(&i2c_obj[i]);
}
}
/**
* @brief This function sets the sda pin.
* @param data, state
@ -129,13 +139,15 @@ static void gd32_udelay(rt_uint32_t us)
static const struct rt_i2c_bit_ops gd32_bit_ops_default =
{
.data = RT_NULL,
.pin_init = gd32_i2c_pin_init,
.set_sda = gd32_set_sda,
.set_scl = gd32_set_scl,
.get_sda = gd32_get_sda,
.get_scl = gd32_get_scl,
.udelay = gd32_udelay,
.delay_us = 1,
.timeout = 100
.timeout = 100,
.i2c_pin_init_flag = RT_FALSE
};
/**
@ -175,12 +187,11 @@ int rt_hw_i2c_init(void)
rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct gd32_i2c);
rt_err_t result;
for (int i = 0; i < obj_num; i++)
for (rt_size_t = 0; i < obj_num; i++)
{
i2c_obj[i].ops = gd32_bit_ops_default;
i2c_obj[i].ops.data = (void*)&soft_i2c_config[i];
i2c_obj[i].i2c_bus.priv = &i2c_obj[i].ops;
gd32_i2c_gpio_init(&i2c_obj[i]);
result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c_bus, soft_i2c_config[i].bus_name);

View File

@ -84,6 +84,16 @@ static void hc32_i2c_gpio_init(struct hc32_soft_i2c *i2c)
rt_pin_write(cfg->sda_pin, PIN_HIGH);
}
static void hc32_i2c_pin_init(void)
{
rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct hc32_soft_i2c);
for(rt_size_t i = 0; i < obj_num; i++)
{
hc32_i2c_gpio_init(&i2c_obj[i]);
}
}
/**
* This function sets the sda pin.
*
@ -184,13 +194,15 @@ static void hc32_udelay(rt_uint32_t us)
static const struct rt_i2c_bit_ops hc32_bit_ops_default =
{
.data = RT_NULL,
.pin_init = hc_i2c_pin_init,
.set_sda = hc32_set_sda,
.set_scl = hc32_set_scl,
.get_sda = hc32_get_sda,
.get_scl = hc32_get_scl,
.udelay = hc32_udelay,
.delay_us = 1,
.timeout = 100
.timeout = 100,
.i2c_pin_init_flag = RT_FALSE
};
/**
@ -228,12 +240,12 @@ int hc32_soft_i2c_init(void)
rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct hc32_soft_i2c);
rt_err_t result;
for (int i = 0; i < obj_num; i++)
for (rt_size_t i = 0; i < obj_num; i++)
{
i2c_obj[i].ops = hc32_bit_ops_default;
i2c_obj[i].ops.data = (void *)&soft_i2c_config[i];
i2c_obj[i].i2c_bus.priv = &i2c_obj[i].ops;
hc32_i2c_gpio_init(&i2c_obj[i]);
result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c_bus, soft_i2c_config[i].bus_name);
RT_ASSERT(result == RT_EOK);
hc32_i2c_bus_unlock(&soft_i2c_config[i]);

View File

@ -55,6 +55,16 @@ static void hk32_i2c_gpio_init(struct hk32_i2c *i2c)
rt_pin_write(cfg->sda, PIN_HIGH);
}
static void hk32_i2c_pin_init(void)
{
rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct hk32_i2c);
for(rt_size_t i = 0; i < obj_num; i++)
{
hk32_i2c_gpio_init(&i2c_obj[i]);
}
}
/**
* This function sets the sda pin.
*
@ -152,13 +162,15 @@ static void hk32_udelay(rt_uint32_t us)
static const struct rt_i2c_bit_ops hk32_bit_ops_default =
{
.data = RT_NULL,
.pin_init = hk32_i2c_pin_init,
.set_sda = hk32_set_sda,
.set_scl = hk32_set_scl,
.get_sda = hk32_get_sda,
.get_scl = hk32_get_scl,
.udelay = hk32_udelay,
.delay_us = 1,
.timeout = 100
.timeout = 100,
.i2c_pin_init_flag = RT_FALSE
};
/**
@ -201,7 +213,7 @@ int rt_hw_i2c_init(void)
i2c_obj[i].ops = hk32_bit_ops_default;
i2c_obj[i].ops.data = (void *)&soft_i2c_config[i];
i2c_obj[i].i2c_bus.priv = &i2c_obj[i].ops;
hk32_i2c_gpio_init(&i2c_obj[i]);
result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c_bus, soft_i2c_config[i].bus_name);
RT_ASSERT(result == RT_EOK);
hk32_i2c_bus_unlock(&soft_i2c_config[i]);

View File

@ -81,17 +81,18 @@ static rt_int32_t ls1c_get_scl(void *data)
}
static const struct rt_i2c_bit_ops bit_ops = {
static const struct rt_i2c_bit_ops bit_ops =
{
.data = RT_NULL,
.pin_init = ls1c_i2c_gpio_init,
.set_sda = ls1c_set_sda,
.set_scl = ls1c_set_scl,
.get_sda = ls1c_get_sda,
.get_scl = ls1c_get_scl,
.udelay = ls1c_udelay,
.delay_us = 20, // 此值为周期(us)
.timeout = 10, // 单位为tick
.i2c_pin_init_flag = RT_FALSE
};
@ -102,8 +103,6 @@ int ls1c_i2c_init(void)
bus.priv = (void *)&bit_ops;
ls1c_i2c_gpio_init();
rt_i2c_bit_add_bus(&bus, "i2c3");
return RT_EOK;

View File

@ -60,6 +60,16 @@ static void n32_i2c_gpio_init(struct n32_i2c *i2c)
rt_pin_write(cfg->sda, PIN_HIGH);
}
static void n32_i2c_pin_init(void)
{
rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct n32_i2c);
for(rt_size_t i = 0; i < obj_num; i++)
{
n32_i2c_gpio_init(&i2c_obj[i]);
}
}
/**
*\*\name n32_set_sda
*\*\fun sets the sda pin.
@ -166,13 +176,15 @@ static void n32_udelay(rt_uint32_t us)
static const struct rt_i2c_bit_ops n32_bit_ops_default =
{
.data = RT_NULL,
.pin_init = n32_i2c_pin_init,
.set_sda = n32_set_sda,
.set_scl = n32_set_scl,
.get_sda = n32_get_sda,
.get_scl = n32_get_scl,
.udelay = n32_udelay,
.delay_us = 1,
.timeout = 100
.timeout = 100,
.i2c_pin_init_flag = RT_FALSE
};
@ -391,13 +403,12 @@ int rt_hw_i2c_init(void)
rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct n32_i2c);
rt_err_t result;
for(int i = 0; i < obj_num; i++)
for(rt_size_t i = 0; i < obj_num; i++)
{
i2c_obj[i].ops = n32_bit_ops_default;
i2c_obj[i].ops.data = (void*)&soft_i2c_config[i];
i2c_obj[i].i2c_bus.priv = &i2c_obj[i].ops;
n32_i2c_gpio_init(&i2c_obj[i]);
result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c_bus, soft_i2c_config[i].bus_name);
RT_ASSERT(result == RT_EOK);

View File

@ -55,6 +55,16 @@ static void n32_i2c_gpio_init(struct n32_i2c *i2c)
rt_pin_write(cfg->sda, PIN_HIGH);
}
static void n32_i2c_pin_init(void)
{
rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct n32_i2c);
for(rt_size_t i = 0; i < obj_num; i++)
{
n32_i2c_gpio_init(&i2c_obj[i]);
}
}
/**
* This function sets the sda pin.
*
@ -152,13 +162,15 @@ static void n32_udelay(rt_uint32_t us)
static const struct rt_i2c_bit_ops n32_bit_ops_default =
{
.data = RT_NULL,
.pin_init = n32_i2c_pin_init,
.set_sda = n32_set_sda,
.set_scl = n32_set_scl,
.get_sda = n32_get_sda,
.get_scl = n32_get_scl,
.udelay = n32_udelay,
.delay_us = 1,
.timeout = 100
.timeout = 100,
.i2c_pin_init_flag = RT_FALSE
};
/**
@ -196,12 +208,12 @@ int rt_hw_i2c_init(void)
rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct n32_i2c);
rt_err_t result;
for (int i = 0; i < obj_num; i++)
for (rt_size_t i = 0; i < obj_num; i++)
{
i2c_obj[i].ops = n32_bit_ops_default;
i2c_obj[i].ops.data = (void *)&soft_i2c_config[i];
i2c_obj[i].i2c_bus.priv = &i2c_obj[i].ops;
n32_i2c_gpio_init(&i2c_obj[i]);
result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c_bus, soft_i2c_config[i].bus_name);
RT_ASSERT(result == RT_EOK);
n32_i2c_bus_unlock(&soft_i2c_config[i]);

View File

@ -38,6 +38,16 @@ static void pico_i2c_gpio_init(struct pico_i2c *i2c)
rt_pin_write(cfg->sda, PIN_HIGH);
}
static void pico_i2c_pin_init(void)
{
rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct pico_i2c);
for(rt_size_t i = 0; i < obj_num; i++)
{
pico_i2c_gpio_init(&i2c_obj[i]);
}
}
/**
* This function sets the sda pin.
*
@ -111,13 +121,15 @@ static void pico_udelay(rt_uint32_t us)
static const struct rt_i2c_bit_ops pico_bit_ops_default =
{
.data = RT_NULL,
.pin_init = pico_i2c_pin_init,
.set_sda = pico_set_sda,
.set_scl = pico_set_scl,
.get_sda = pico_get_sda,
.get_scl = pico_get_scl,
.udelay = pico_udelay,
.delay_us = 1,
.timeout = 100
.timeout = 100,
.i2c_pin_init_flag = RT_FALSE
};
/**
@ -159,7 +171,7 @@ int rt_soft_i2c_init(void)
i2c_obj[i].ops = pico_bit_ops_default;
i2c_obj[i].ops.data = (void*)&soft_i2c_config[i];
i2c_obj[i].i2c_bus.priv = &i2c_obj[i].ops;
pico_i2c_gpio_init(&i2c_obj[i]);
result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c_bus, soft_i2c_config[i].bus_name);
RT_ASSERT(result == RT_EOK);
pico_i2c_bus_unlock(&soft_i2c_config[i]);

View File

@ -57,6 +57,16 @@ static void ra_i2c_gpio_init(struct ra_i2c *i2c)
rt_pin_write(cfg->sda, PIN_HIGH);
}
static void ra_i2c_pin_init(void)
{
rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct ra_i2c);
for(rt_size_t i = 0; i < obj_num; i++)
{
ra_i2c_gpio_init(&i2c_obj[i]);
}
}
/**
* This function sets the sda pin.
*
@ -154,13 +164,15 @@ static void ra_udelay(rt_uint32_t us)
static const struct rt_i2c_bit_ops ra_bit_ops_default =
{
.data = RT_NULL,
.pin_init = ra_i2c_pin_init,
.set_sda = ra_set_sda,
.set_scl = ra_set_scl,
.get_sda = ra_get_sda,
.get_scl = ra_get_scl,
.udelay = ra_udelay,
.delay_us = 1,
.timeout = 100
.timeout = 100,
.i2c_pin_init_flag = RT_FALSE
};
/**
@ -198,12 +210,11 @@ int rt_hw_i2c_init(void)
rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct ra_i2c);
rt_err_t result;
for (int i = 0; i < obj_num; i++)
for (rt_size_t i = 0; i < obj_num; i++)
{
i2c_obj[i].ops = ra_bit_ops_default;
i2c_obj[i].ops.data = (void *)&soft_i2c_config[i];
i2c_obj[i].i2c_bus.priv = &i2c_obj[i].ops;
ra_i2c_gpio_init(&i2c_obj[i]);
result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c_bus, soft_i2c_config[i].bus_name);
RT_ASSERT(result == RT_EOK);
ra_i2c_bus_unlock(&soft_i2c_config[i]);

View File

@ -56,6 +56,16 @@ static void stm32_i2c_gpio_init(struct stm32_i2c *i2c)
rt_pin_write(cfg->sda, PIN_HIGH);
}
static void stm32_i2c_pin_init(void)
{
rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct stm32_i2c);
for(rt_size_t i = 0; i < obj_num; i++)
{
stm32_i2c_gpio_init(&i2c_obj[i]);
}
}
/**
* This function sets the sda pin.
*
@ -119,13 +129,15 @@ static rt_int32_t stm32_get_scl(void *data)
static const struct rt_i2c_bit_ops stm32_bit_ops_default =
{
.data = RT_NULL,
.pin_init = stm32_i2c_pin_init,
.set_sda = stm32_set_sda,
.set_scl = stm32_set_scl,
.get_sda = stm32_get_sda,
.get_scl = stm32_get_scl,
.udelay = rt_hw_us_delay,
.delay_us = 1,
.timeout = 100
.timeout = 100,
.i2c_pin_init_flag = RT_FALSE
};
/**
@ -167,7 +179,6 @@ int rt_hw_i2c_init(void)
i2c_obj[i].ops = stm32_bit_ops_default;
i2c_obj[i].ops.data = (void*)&soft_i2c_config[i];
i2c_obj[i].i2c_bus.priv = &i2c_obj[i].ops;
stm32_i2c_gpio_init(&i2c_obj[i]);
result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c_bus, soft_i2c_config[i].bus_name);
RT_ASSERT(result == RT_EOK);
stm32_i2c_bus_unlock(&soft_i2c_config[i]);

View File

@ -91,6 +91,16 @@ static void swm_i2c_gpio_init(struct swm_soft_i2c_device *i2c)
rt_pin_write(soft_i2c_cfg->sda, PIN_HIGH);
}
static void swm_i2c_pin_init(void)
{
rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct swm_soft_i2c_device);
for(rt_size_t i = 0; i < obj_num; i++)
{
swm_i2c_gpio_init(&i2c_obj[i]);
}
}
/**
* This function sets the sda pin.
*
@ -191,27 +201,30 @@ static void swm_i2c_udelay(rt_uint32_t us)
}
static const struct rt_i2c_bit_ops swm_i2c_bit_ops =
{
{
.data = RT_NULL,
.pin_init = swm_i2c_pin_init,
.set_sda = swm_i2c_set_sda,
.set_scl = swm_i2c_set_scl,
.get_sda = swm_i2c_get_sda,
.get_scl = swm_i2c_get_scl,
.udelay = swm_i2c_udelay,
.delay_us = 1,
.timeout = 100};
.timeout = 100,
.i2c_pin_init_flag = RT_FALSE
};
/* I2C initialization function */
int swm_i2c_init(void)
{
rt_err_t result;
for (int i = 0; i < sizeof(i2c_obj) / sizeof(struct swm_soft_i2c_device); i++)
for (rt_size_t i = 0; i < sizeof(i2c_obj) / sizeof(struct swm_soft_i2c_device); i++)
{
i2c_obj[i].ops = swm_i2c_bit_ops;
i2c_obj[i].ops.data = (void *)&swm_soft_i2c_cfg[i];
i2c_obj[i].i2c_bus.priv = &i2c_obj[i].ops;
swm_i2c_gpio_init(&i2c_obj[i]);
result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c_bus, swm_soft_i2c_cfg[i].name);
RT_ASSERT(result == RT_EOK);

View File

@ -61,6 +61,16 @@ static void w60x_i2c_gpio_init(struct w60x_i2c *i2c)
tls_gpio_write((enum tls_io_name)sda, 1);
}
static void w60x_i2c_pin_init(void)
{
rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct w60x_i2c);
for(rt_size_t i = 0; i < obj_num; i++)
{
w60x_i2c_gpio_init(&i2c_obj[i]);
}
}
/**
* This function sets the sda pin.
*
@ -143,13 +153,15 @@ static rt_int32_t w60x_get_scl(void *data)
static const struct rt_i2c_bit_ops w60x_bit_ops_default =
{
.data = RT_NULL,
.pin_init = w60x_i2c_pin_init,
.set_sda = w60x_set_sda,
.set_scl = w60x_set_scl,
.get_sda = w60x_get_sda,
.get_scl = w60x_get_scl,
.udelay = rt_hw_us_delay,
.delay_us = 1,
.timeout = 100
.timeout = 100,
.i2c_pin_init_flag = RT_FALSE
};
/**
@ -187,12 +199,12 @@ int rt_soft_i2c_init(void)
rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct w60x_i2c);
rt_err_t result;
for (int i = 0; i < obj_num; i++)
for (rt_size_t i = 0; i < obj_num; i++)
{
i2c_obj[i].ops = w60x_bit_ops_default;
i2c_obj[i].ops.data = (void*)&soft_i2c_config[i];
i2c_obj[i].i2c_bus.priv = &i2c_obj[i].ops;
w60x_i2c_gpio_init(&i2c_obj[i]);
result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c_bus, soft_i2c_config[i].bus_name);
RT_ASSERT(result == RT_EOK);
w60x_i2c_bus_unlock(&soft_i2c_config[i]);

View File

@ -50,6 +50,16 @@ static void ch32_i2c_gpio_init(struct ch32_i2c *i2c)
rt_pin_write(cfg->sda, PIN_HIGH);
}
static void ch32_i2c_pin_init(void)
{
rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct ch32_i2c);
for(rt_size_t i = 0; i < obj_num; i++)
{
ch32_i2c_gpio_init(&i2c_obj[i]);
}
}
/*
* This function sets the sda pin.
*
@ -148,13 +158,15 @@ static void ch32_udelay(rt_uint32_t us)
static const struct rt_i2c_bit_ops ch32_bit_ops_default =
{
.data = RT_NULL,
.pin_init = ch32_i2c_pin_init,
.set_sda = ch32_set_sda,
.set_scl = ch32_set_scl,
.get_sda = ch32_get_sda,
.get_scl = ch32_get_scl,
.udelay = ch32_udelay,
.delay_us = 1,
.timeout = 100
.timeout = 100,
.i2c_pin_init_flag = RT_FALSE
};
/*
@ -192,12 +204,12 @@ int rt_hw_i2c_init(void)
rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct ch32_i2c);
rt_err_t result;
for (int i = 0; i < obj_num; i++)
for (rt_size_t i = 0; i < obj_num; i++)
{
i2c_obj[i].ops = ch32_bit_ops_default;
i2c_obj[i].ops.data = (void*)&soft_i2c_config[i];
i2c_obj[i].i2c_bus.priv = &i2c_obj[i].ops;
ch32_i2c_gpio_init(&i2c_obj[i]);
result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c_bus, soft_i2c_config[i].bus_name);
RT_ASSERT(result == RT_EOK);
ch32_i2c_bus_unlock(&soft_i2c_config[i]);

View File

@ -376,6 +376,12 @@ static rt_ssize_t i2c_bit_xfer(struct rt_i2c_bus_device *bus,
rt_uint32_t i;
rt_uint16_t ignore_nack;
if((ops->i2c_pin_init_flag == RT_FALSE) && (ops->pin_init != RT_NULL))
{
ops->pin_init();
ops->i2c_pin_init_flag = RT_TRUE;
}
if (num == 0) return 0;
for (i = 0; i < num; i++)

View File

@ -27,6 +27,9 @@ struct rt_i2c_bit_ops
rt_uint32_t delay_us; /* scl and sda line delay */
rt_uint32_t timeout; /* in tick */
void (*pin_init)(void);
rt_bool_t i2c_pin_init_flag;
};
rt_err_t rt_i2c_bit_add_bus(struct rt_i2c_bus_device *bus,