2022-10-20 09:40:14 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2006-2022, RT-Thread Development Team
|
2022-07-13 19:56:14 +08:00
|
|
|
*
|
2022-10-20 09:40:14 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2022-07-13 19:56:14 +08:00
|
|
|
*
|
2022-10-20 09:40:14 +08:00
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2022-10-19 Nations first version
|
2022-07-13 19:56:14 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "drv_i2c.h"
|
|
|
|
|
|
|
|
#ifdef RT_USING_I2C
|
|
|
|
|
|
|
|
#define DBG_TAG "drv.I2C"
|
|
|
|
#ifdef RT_I2C_DEBUG
|
|
|
|
#define DBG_LVL DBG_LOG
|
|
|
|
#else
|
|
|
|
#define DBG_LVL DBG_INFO
|
|
|
|
#endif
|
|
|
|
#include <rtdbg.h>
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef RT_USING_I2C_BITOPS
|
2022-07-15 13:36:32 +08:00
|
|
|
static const struct n32_soft_i2c_config soft_i2c_config[] =
|
|
|
|
{
|
|
|
|
#ifdef BSP_USING_I2C1
|
|
|
|
I2C1_BUS_CONFIG,
|
|
|
|
#endif
|
2022-07-13 19:56:14 +08:00
|
|
|
|
2022-07-15 13:36:32 +08:00
|
|
|
#ifdef BSP_USING_I2C2
|
|
|
|
I2C2_BUS_CONFIG,
|
|
|
|
#endif
|
2022-07-13 19:56:14 +08:00
|
|
|
|
2022-07-15 13:36:32 +08:00
|
|
|
#ifdef BSP_USING_I2C3
|
|
|
|
I2C3_BUS_CONFIG,
|
|
|
|
#endif
|
2022-07-13 19:56:14 +08:00
|
|
|
|
2022-07-15 13:36:32 +08:00
|
|
|
#ifdef BSP_USING_I2C4
|
|
|
|
I2C4_BUS_CONFIG,
|
|
|
|
#endif
|
2022-07-13 19:56:14 +08:00
|
|
|
};
|
|
|
|
|
2022-07-15 13:36:32 +08:00
|
|
|
static struct n32_i2c i2c_obj[sizeof(soft_i2c_config) / sizeof(soft_i2c_config[0])];
|
|
|
|
|
|
|
|
/**
|
|
|
|
*\*\name n32_i2c_gpio_init
|
|
|
|
*\*\fun Initializes the i2c pin.
|
|
|
|
*\*\param i2c dirver class
|
|
|
|
*\*\return none
|
|
|
|
**/
|
|
|
|
static void n32_i2c_gpio_init(struct n32_i2c *i2c)
|
2022-07-13 19:56:14 +08:00
|
|
|
{
|
2022-07-15 13:36:32 +08:00
|
|
|
struct n32_soft_i2c_config* cfg = (struct n32_soft_i2c_config*)i2c->ops.data;
|
|
|
|
|
|
|
|
rt_pin_mode(cfg->scl, PIN_MODE_OUTPUT_OD);
|
|
|
|
rt_pin_mode(cfg->sda, PIN_MODE_OUTPUT_OD);
|
2022-07-13 19:56:14 +08:00
|
|
|
|
2022-07-15 13:36:32 +08:00
|
|
|
rt_pin_write(cfg->scl, PIN_HIGH);
|
|
|
|
rt_pin_write(cfg->sda, PIN_HIGH);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*\*\name n32_set_sda
|
|
|
|
*\*\fun sets the sda pin.
|
|
|
|
*\*\param data config class
|
|
|
|
*\*\param state sda pin state
|
|
|
|
*\*\return none
|
|
|
|
**/
|
|
|
|
static void n32_set_sda(void *data, rt_int32_t state)
|
|
|
|
{
|
|
|
|
struct n32_soft_i2c_config* cfg = (struct n32_soft_i2c_config*)data;
|
2022-07-13 19:56:14 +08:00
|
|
|
if (state)
|
|
|
|
{
|
2022-07-15 13:36:32 +08:00
|
|
|
rt_pin_write(cfg->sda, PIN_HIGH);
|
2022-07-13 19:56:14 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-07-15 13:36:32 +08:00
|
|
|
rt_pin_write(cfg->sda, PIN_LOW);
|
2022-07-13 19:56:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-15 13:36:32 +08:00
|
|
|
/**
|
|
|
|
*\*\name n32_set_scl
|
|
|
|
*\*\fun sets the scl pin.
|
|
|
|
*\*\param data config class
|
|
|
|
*\*\param state scl pin state
|
|
|
|
*\*\return none
|
|
|
|
**/
|
|
|
|
static void n32_set_scl(void *data, rt_int32_t state)
|
2022-07-13 19:56:14 +08:00
|
|
|
{
|
2022-07-15 13:36:32 +08:00
|
|
|
struct n32_soft_i2c_config* cfg = (struct n32_soft_i2c_config*)data;
|
2022-07-13 19:56:14 +08:00
|
|
|
if (state)
|
|
|
|
{
|
2022-07-15 13:36:32 +08:00
|
|
|
rt_pin_write(cfg->scl, PIN_HIGH);
|
2022-07-13 19:56:14 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-07-15 13:36:32 +08:00
|
|
|
rt_pin_write(cfg->scl, PIN_LOW);
|
2022-07-13 19:56:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-15 13:36:32 +08:00
|
|
|
/**
|
|
|
|
*\*\name n32_get_sda
|
|
|
|
*\*\fun gets the sda pin state.
|
|
|
|
*\*\param data config class
|
|
|
|
*\*\return sda pin state
|
|
|
|
**/
|
|
|
|
static rt_int32_t n32_get_sda(void *data)
|
2022-07-13 19:56:14 +08:00
|
|
|
{
|
2022-07-15 13:36:32 +08:00
|
|
|
struct n32_soft_i2c_config* cfg = (struct n32_soft_i2c_config*)data;
|
|
|
|
return rt_pin_read(cfg->sda);
|
2022-07-13 19:56:14 +08:00
|
|
|
}
|
|
|
|
|
2022-07-15 13:36:32 +08:00
|
|
|
/**
|
|
|
|
*\*\name n32_get_scl
|
|
|
|
*\*\fun gets the scl pin state.
|
|
|
|
*\*\param data config class
|
|
|
|
*\*\return scl pin state
|
|
|
|
**/
|
|
|
|
static rt_int32_t n32_get_scl(void *data)
|
2022-07-13 19:56:14 +08:00
|
|
|
{
|
2022-07-15 13:36:32 +08:00
|
|
|
struct n32_soft_i2c_config* cfg = (struct n32_soft_i2c_config*)data;
|
|
|
|
return rt_pin_read(cfg->scl);
|
2022-07-13 19:56:14 +08:00
|
|
|
}
|
|
|
|
|
2022-07-15 13:36:32 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
*\*\name n32_udelay
|
|
|
|
*\*\fun The time delay function.
|
|
|
|
*\*\param us
|
|
|
|
*\*\return none
|
|
|
|
**/
|
|
|
|
static void n32_udelay(rt_uint32_t us)
|
2022-07-13 19:56:14 +08:00
|
|
|
{
|
2022-07-15 13:36:32 +08:00
|
|
|
rt_uint32_t ticks;
|
|
|
|
rt_uint32_t told, tnow, tcnt = 0;
|
|
|
|
rt_uint32_t reload = SysTick->LOAD;
|
|
|
|
|
|
|
|
ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
|
|
|
|
told = SysTick->VAL;
|
|
|
|
|
2022-10-20 09:40:14 +08:00
|
|
|
while (1)
|
2022-07-13 19:56:14 +08:00
|
|
|
{
|
2022-07-15 13:36:32 +08:00
|
|
|
tnow = SysTick->VAL;
|
2022-10-20 09:40:14 +08:00
|
|
|
if (tnow != told)
|
2022-07-15 13:36:32 +08:00
|
|
|
{
|
2022-10-20 09:40:14 +08:00
|
|
|
if (tnow < told)
|
2022-07-15 13:36:32 +08:00
|
|
|
{
|
|
|
|
tcnt += told - tnow;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tcnt += reload - tnow + told;
|
|
|
|
}
|
|
|
|
told = tnow;
|
|
|
|
|
2022-10-20 09:40:14 +08:00
|
|
|
if (tcnt >= ticks)
|
2022-07-15 13:36:32 +08:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-07-13 19:56:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-15 13:36:32 +08:00
|
|
|
static const struct rt_i2c_bit_ops n32_bit_ops_default =
|
|
|
|
{
|
|
|
|
.data = RT_NULL,
|
|
|
|
.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
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
*\*\name n32_i2c_bus_unlock
|
|
|
|
*\*\fun If i2c is locked, this function will unlock it.
|
|
|
|
*\*\param cfg
|
|
|
|
*\*\return RT_EOK indicates successful unlock
|
|
|
|
**/
|
|
|
|
static rt_err_t n32_i2c_bus_unlock(const struct n32_soft_i2c_config *cfg)
|
2022-07-13 19:56:14 +08:00
|
|
|
{
|
2022-07-15 13:36:32 +08:00
|
|
|
rt_int32_t i = 0;
|
|
|
|
|
2022-10-20 09:40:14 +08:00
|
|
|
if (PIN_LOW == rt_pin_read(cfg->sda))
|
2022-07-15 13:36:32 +08:00
|
|
|
{
|
2022-10-20 09:40:14 +08:00
|
|
|
while (i++ < 9)
|
2022-07-15 13:36:32 +08:00
|
|
|
{
|
|
|
|
rt_pin_write(cfg->scl, PIN_HIGH);
|
|
|
|
n32_udelay(100);
|
|
|
|
rt_pin_write(cfg->scl, PIN_LOW);
|
|
|
|
n32_udelay(100);
|
|
|
|
}
|
|
|
|
}
|
2022-07-13 19:56:14 +08:00
|
|
|
|
2022-10-20 09:40:14 +08:00
|
|
|
if (PIN_LOW == rt_pin_read(cfg->sda))
|
2022-07-15 13:36:32 +08:00
|
|
|
{
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RT_EOK;
|
2022-07-13 19:56:14 +08:00
|
|
|
}
|
2022-07-15 13:36:32 +08:00
|
|
|
#endif /* RT_USING_I2C_BITOPS */
|
2022-07-13 19:56:14 +08:00
|
|
|
|
2022-07-23 11:53:42 +08:00
|
|
|
#ifdef RT_USING_HARDWARE_I2C
|
2022-07-13 19:56:14 +08:00
|
|
|
|
2022-10-20 09:40:14 +08:00
|
|
|
#define I2CT_FLAG_TIMEOUT ((uint32_t)0x1000)
|
|
|
|
#define I2CT_LONG_TIMEOUT ((uint32_t)(10 * I2CT_FLAG_TIMEOUT))
|
|
|
|
|
2022-07-13 19:56:14 +08:00
|
|
|
static uint32_t I2CTimeout = I2CT_LONG_TIMEOUT;
|
|
|
|
|
|
|
|
static int rt_i2c_read(rt_uint32_t i2c_periph, rt_uint16_t slave_address, rt_uint8_t* p_buffer, rt_uint16_t data_byte)
|
|
|
|
{
|
|
|
|
I2CTimeout = I2CT_LONG_TIMEOUT;
|
|
|
|
/* wait until I2C bus is idle */
|
2022-10-20 09:40:14 +08:00
|
|
|
while (I2C_GetFlag((I2C_Module*)i2c_periph, I2C_FLAG_BUSY))
|
2022-07-13 19:56:14 +08:00
|
|
|
{
|
|
|
|
if ((I2CTimeout--) == 0)
|
|
|
|
return 9;
|
|
|
|
};
|
|
|
|
|
|
|
|
I2C_ConfigAck((I2C_Module*)i2c_periph, ENABLE);
|
2022-07-23 11:53:42 +08:00
|
|
|
|
2022-07-13 19:56:14 +08:00
|
|
|
/** Send START condition */
|
|
|
|
I2C_GenerateStart((I2C_Module*)i2c_periph, ENABLE);
|
|
|
|
|
|
|
|
I2CTimeout = I2CT_LONG_TIMEOUT;
|
|
|
|
/* wait until SBSEND bit is set */
|
|
|
|
while (!I2C_CheckEvent((I2C_Module*)i2c_periph, I2C_EVT_MASTER_MODE_FLAG)) // EV5
|
|
|
|
{
|
|
|
|
if ((I2CTimeout--) == 0)
|
|
|
|
return 10;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* send slave address to I2C bus */
|
|
|
|
I2C_SendAddr7bit((I2C_Module*)i2c_periph, slave_address, I2C_DIRECTION_RECV);
|
2022-07-23 11:53:42 +08:00
|
|
|
|
2022-07-13 19:56:14 +08:00
|
|
|
I2CTimeout = I2CT_LONG_TIMEOUT;
|
|
|
|
while (!I2C_CheckEvent((I2C_Module*)i2c_periph, I2C_EVT_MASTER_RXMODE_FLAG)) // EV6
|
|
|
|
{
|
|
|
|
if ((I2CTimeout--) == 0)
|
|
|
|
return 6;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* while there is data to be read */
|
2022-10-20 09:40:14 +08:00
|
|
|
while (data_byte)
|
2022-07-13 19:56:14 +08:00
|
|
|
{
|
|
|
|
/* wait until the RBNE bit is set and clear it */
|
2022-10-20 09:40:14 +08:00
|
|
|
if (I2C_GetFlag((I2C_Module*)i2c_periph, I2C_FLAG_RXDATNE))
|
2022-07-13 19:56:14 +08:00
|
|
|
{
|
|
|
|
/* read a byte*/
|
|
|
|
*p_buffer = I2C_RecvData((I2C_Module*)i2c_periph);
|
|
|
|
|
|
|
|
/* point to the next location where the byte read will be saved */
|
2022-07-23 11:53:42 +08:00
|
|
|
p_buffer++;
|
2022-07-13 19:56:14 +08:00
|
|
|
|
|
|
|
/* decrement the read bytes counter */
|
|
|
|
data_byte--;
|
2022-10-20 09:40:14 +08:00
|
|
|
if (1 == data_byte)
|
2022-07-13 19:56:14 +08:00
|
|
|
{
|
|
|
|
/* disable acknowledge */
|
|
|
|
I2C_ConfigAck((I2C_Module*)i2c_periph, DISABLE);
|
|
|
|
/* send a stop condition to I2C bus */
|
|
|
|
I2C_GenerateStop((I2C_Module*)i2c_periph, ENABLE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* wait until the stop condition is finished */
|
2022-10-20 09:40:14 +08:00
|
|
|
while (I2C_GetFlag((I2C_Module*)i2c_periph, I2C_FLAG_STOPF))
|
2022-07-13 19:56:14 +08:00
|
|
|
{
|
|
|
|
if ((I2CTimeout--) == 0)
|
|
|
|
return 7;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* enable acknowledge */
|
|
|
|
I2C_ConfigAck((I2C_Module*)i2c_periph, ENABLE);
|
|
|
|
|
|
|
|
I2C_ConfigNackLocation((I2C_Module*)i2c_periph,I2C_NACK_POS_CURRENT);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rt_i2c_write(rt_uint32_t i2c_periph, uint16_t slave_address, uint8_t* p_buffer, uint16_t data_byte)
|
|
|
|
{
|
|
|
|
uint8_t* sendBufferPtr = p_buffer;
|
|
|
|
I2CTimeout = I2CT_LONG_TIMEOUT;
|
|
|
|
while (I2C_GetFlag((I2C_Module*)i2c_periph, I2C_FLAG_BUSY))
|
|
|
|
{
|
|
|
|
if ((I2CTimeout--) == 0)
|
|
|
|
return 4;
|
|
|
|
};
|
|
|
|
|
|
|
|
I2C_ConfigAck((I2C_Module*)i2c_periph, ENABLE);
|
|
|
|
I2C_GenerateStart((I2C_Module*)i2c_periph, ENABLE);
|
|
|
|
I2CTimeout = I2CT_LONG_TIMEOUT;
|
|
|
|
while (!I2C_CheckEvent((I2C_Module*)i2c_periph, I2C_EVT_MASTER_MODE_FLAG)) // EV5
|
|
|
|
{
|
|
|
|
if ((I2CTimeout--) == 0)
|
|
|
|
return 5;
|
|
|
|
};
|
|
|
|
|
|
|
|
I2C_SendAddr7bit((I2C_Module*)i2c_periph, slave_address, I2C_DIRECTION_SEND);
|
|
|
|
I2CTimeout = I2CT_LONG_TIMEOUT;
|
|
|
|
while (!I2C_CheckEvent((I2C_Module*)i2c_periph, I2C_EVT_MASTER_TXMODE_FLAG)) // EV6
|
|
|
|
{
|
|
|
|
if ((I2CTimeout--) == 0)
|
|
|
|
return 6;
|
|
|
|
};
|
|
|
|
|
2022-10-20 09:40:14 +08:00
|
|
|
/* send data */
|
2022-07-13 19:56:14 +08:00
|
|
|
while (data_byte-- > 0)
|
|
|
|
{
|
|
|
|
I2C_SendData((I2C_Module*)i2c_periph, *sendBufferPtr++);
|
|
|
|
I2CTimeout = I2CT_LONG_TIMEOUT;
|
|
|
|
while (!I2C_CheckEvent((I2C_Module*)i2c_periph, I2C_EVT_MASTER_DATA_SENDING)) // EV8
|
|
|
|
{
|
|
|
|
if ((I2CTimeout--) == 0)
|
|
|
|
return 7;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
I2CTimeout = I2CT_LONG_TIMEOUT;
|
|
|
|
while (!I2C_CheckEvent((I2C_Module*)i2c_periph, I2C_EVT_MASTER_DATA_SENDED)) // EV8-2
|
|
|
|
{
|
|
|
|
if ((I2CTimeout--) == 0)
|
|
|
|
return 8;
|
|
|
|
};
|
|
|
|
I2C_GenerateStop((I2C_Module*)i2c_periph, ENABLE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-02-06 07:35:33 +08:00
|
|
|
static rt_ssize_t rt_i2c_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg msgs[], rt_uint32_t num)
|
2022-07-13 19:56:14 +08:00
|
|
|
{
|
|
|
|
struct rt_i2c_msg *msg;
|
|
|
|
rt_uint32_t i;
|
2023-03-17 12:12:16 +08:00
|
|
|
rt_err_t ret = -RT_ERROR;
|
2022-07-13 19:56:14 +08:00
|
|
|
|
|
|
|
struct rt_i2c_bus *rt_i2c = (struct rt_i2c_bus *)bus;
|
|
|
|
|
|
|
|
for (i = 0; i < num; i++)
|
|
|
|
{
|
|
|
|
msg = &msgs[i];
|
|
|
|
|
|
|
|
if (msg->flags & RT_I2C_RD)
|
|
|
|
{
|
|
|
|
if (rt_i2c_read(rt_i2c->i2c_periph, msg->addr, msg->buf, msg->len) != 0)
|
|
|
|
{
|
|
|
|
LOG_E("i2c bus write failed,i2c bus stop!");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (rt_i2c_write(rt_i2c->i2c_periph, msg->addr, msg->buf, msg->len) != 0)
|
|
|
|
{
|
|
|
|
LOG_E("i2c bus write failed,i2c bus stop!");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-23 11:53:42 +08:00
|
|
|
|
2022-07-13 19:56:14 +08:00
|
|
|
ret = i;
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
out:
|
|
|
|
LOG_E("send stop condition\n");
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct rt_i2c_bus_device_ops i2c_ops =
|
2022-07-23 11:53:42 +08:00
|
|
|
{
|
2022-07-13 19:56:14 +08:00
|
|
|
rt_i2c_xfer,
|
|
|
|
RT_NULL,
|
|
|
|
RT_NULL
|
|
|
|
};
|
|
|
|
|
2022-07-15 13:36:32 +08:00
|
|
|
#endif /* RT_USING_HARDWARE_I2C */
|
2022-07-13 19:56:14 +08:00
|
|
|
|
|
|
|
int rt_hw_i2c_init(void)
|
|
|
|
{
|
|
|
|
#ifdef RT_USING_I2C_BITOPS
|
|
|
|
|
2022-10-20 09:40:14 +08:00
|
|
|
rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct n32_i2c);
|
2022-07-15 13:36:32 +08:00
|
|
|
rt_err_t result;
|
|
|
|
|
|
|
|
for(int 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].i2c2_bus.priv = &i2c_obj[i].ops;
|
2022-07-13 19:56:14 +08:00
|
|
|
|
2022-07-15 13:36:32 +08:00
|
|
|
n32_i2c_gpio_init(&i2c_obj[i]);
|
|
|
|
result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c2_bus, soft_i2c_config[i].bus_name);
|
2022-07-13 19:56:14 +08:00
|
|
|
|
2022-07-15 13:36:32 +08:00
|
|
|
RT_ASSERT(result == RT_EOK);
|
|
|
|
n32_i2c_bus_unlock(&soft_i2c_config[i]);
|
2022-07-13 19:56:14 +08:00
|
|
|
|
2022-07-15 13:36:32 +08:00
|
|
|
rt_kprintf("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);
|
|
|
|
}
|
|
|
|
#endif /* RT_USING_I2C_BITOPS */
|
2022-07-23 11:53:42 +08:00
|
|
|
|
2022-07-15 13:36:32 +08:00
|
|
|
#ifdef RT_USING_HARDWARE_I2C
|
2022-07-13 19:56:14 +08:00
|
|
|
|
2022-10-20 09:40:14 +08:00
|
|
|
GPIO_InitType GPIO_InitStructure;
|
|
|
|
I2C_InitType I2C_InitStructure;
|
2022-07-13 19:56:14 +08:00
|
|
|
#ifdef BSP_USING_I2C1
|
|
|
|
#define I2C1_SPEED 400000
|
|
|
|
|
|
|
|
static struct rt_i2c_bus i2c_bus1;
|
|
|
|
|
2022-10-20 09:40:14 +08:00
|
|
|
#if defined(SOC_N32G45X) || defined(SOC_N32WB452)
|
|
|
|
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB | RCC_APB2_PERIPH_AFIO, ENABLE);
|
|
|
|
RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_I2C1, ENABLE);
|
|
|
|
|
|
|
|
GPIO_InitStruct(&GPIO_InitStructure);
|
2022-07-13 19:56:14 +08:00
|
|
|
/* connect PB8 to I2C1_SCL, PB9 to I2C1_SDA */
|
2022-10-20 09:40:14 +08:00
|
|
|
GPIO_InitStructure.Pin = GPIO_PIN_8 | GPIO_PIN_9;
|
|
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
|
|
|
|
GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
|
2022-07-23 11:53:42 +08:00
|
|
|
|
2022-10-20 09:40:14 +08:00
|
|
|
GPIO_ConfigPinRemap(GPIO_RMP_I2C1, ENABLE);
|
|
|
|
#elif defined(SOC_N32L43X) || defined(SOC_N32L40X) || defined(SOC_N32G43X)
|
|
|
|
/* Enable I2C clock */
|
2022-07-13 19:56:14 +08:00
|
|
|
RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_I2C1, ENABLE);
|
2022-10-20 09:40:14 +08:00
|
|
|
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB | RCC_APB2_PERIPH_AFIO, ENABLE);
|
|
|
|
|
|
|
|
GPIO_InitStruct(&GPIO_InitStructure);
|
|
|
|
/* Confige I2C1_SCL(PB8) and I2C1_SDA(PB9) */
|
|
|
|
GPIO_InitStructure.Pin = GPIO_PIN_8 | GPIO_PIN_9;
|
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
|
|
|
|
GPIO_InitStructure.GPIO_Pull = GPIO_Pull_Up;
|
|
|
|
GPIO_InitStructure.GPIO_Alternate = GPIO_AF4_I2C1;
|
|
|
|
GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
|
|
|
|
#endif
|
2022-07-23 11:53:42 +08:00
|
|
|
|
2022-07-13 19:56:14 +08:00
|
|
|
I2C_DeInit(I2C1);
|
|
|
|
I2C_InitStructure.BusMode = I2C_BUSMODE_I2C;
|
|
|
|
I2C_InitStructure.FmDutyCycle = I2C_FMDUTYCYCLE_2;
|
|
|
|
I2C_InitStructure.OwnAddr1 = 0xff;
|
|
|
|
I2C_InitStructure.AckEnable = I2C_ACKEN;
|
|
|
|
I2C_InitStructure.AddrMode = I2C_ADDR_MODE_7BIT;
|
|
|
|
I2C_InitStructure.ClkSpeed = I2C1_SPEED; // 400000 400K
|
|
|
|
|
|
|
|
I2C_Init(I2C1, &I2C_InitStructure);
|
|
|
|
|
|
|
|
rt_memset((void *)&i2c_bus1, 0, sizeof(struct rt_i2c_bus));
|
|
|
|
i2c_bus1.parent.ops = &i2c_ops;
|
|
|
|
i2c_bus1.i2c_periph = (rt_uint32_t)I2C1;
|
|
|
|
rt_i2c_bus_device_register(&i2c_bus1.parent, "i2c1");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef BSP_USING_I2C2
|
|
|
|
#define I2C2_SPEED 100000
|
|
|
|
|
|
|
|
static struct rt_i2c_bus i2c_bus2;
|
|
|
|
|
2022-10-20 09:40:14 +08:00
|
|
|
#if defined(SOC_N32G45X) || defined(SOC_N32WB452)
|
|
|
|
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE);
|
2022-07-13 19:56:14 +08:00
|
|
|
RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_I2C2, ENABLE);
|
|
|
|
|
2022-10-20 09:40:14 +08:00
|
|
|
GPIO_InitStruct(&GPIO_InitStructure);
|
2022-07-13 19:56:14 +08:00
|
|
|
/* connect PB10 to I2C2_SCL, PB11 to I2C2_SDA */
|
2022-10-20 09:40:14 +08:00
|
|
|
GPIO_InitStructure.Pin = GPIO_PIN_10 | GPIO_PIN_11;
|
|
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
|
|
|
|
GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
|
|
|
|
#elif defined(SOC_N32L43X) || defined(SOC_N32L40X) || defined(SOC_N32G43X)
|
|
|
|
/* Enable I2C clock */
|
|
|
|
RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_I2C2, ENABLE);
|
|
|
|
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB | RCC_APB2_PERIPH_AFIO, ENABLE);
|
|
|
|
|
|
|
|
GPIO_InitStruct(&GPIO_InitStructure);
|
|
|
|
/* Confige I2C1_SCL(PB10) and I2C1_SDA(PB11) */
|
|
|
|
GPIO_InitStructure.Pin = GPIO_PIN_10 | GPIO_PIN_11;
|
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
|
|
|
|
GPIO_InitStructure.GPIO_Pull = GPIO_Pull_Up;
|
|
|
|
GPIO_InitStructure.GPIO_Alternate = GPIO_AF6_I2C2;
|
|
|
|
GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
|
|
|
|
#endif
|
2022-07-13 19:56:14 +08:00
|
|
|
|
|
|
|
I2C_DeInit(I2C2);
|
|
|
|
I2C_InitStructure.BusMode = I2C_BUSMODE_I2C;
|
|
|
|
I2C_InitStructure.FmDutyCycle = I2C_FMDUTYCYCLE_2;
|
|
|
|
I2C_InitStructure.OwnAddr1 = 0xff;
|
|
|
|
I2C_InitStructure.AckEnable = I2C_ACKEN;
|
|
|
|
I2C_InitStructure.AddrMode = I2C_ADDR_MODE_7BIT;
|
|
|
|
I2C_InitStructure.ClkSpeed = I2C2_SPEED; // 100000 100K
|
|
|
|
|
|
|
|
I2C_Init(I2C2, &I2C_InitStructure);
|
|
|
|
|
|
|
|
rt_memset((void *)&i2c_bus2, 0, sizeof(struct rt_i2c_bus));
|
|
|
|
i2c_bus2.parent.ops = &i2c_ops;
|
|
|
|
i2c_bus2.i2c_periph = (rt_uint32_t)I2C2;
|
|
|
|
rt_i2c_bus_device_register(&i2c_bus2.parent, "i2c2");
|
|
|
|
#endif
|
|
|
|
|
2022-10-20 09:40:14 +08:00
|
|
|
#if defined(SOC_N32G45X) || defined(SOC_N32WB452)
|
2022-07-13 19:56:14 +08:00
|
|
|
#ifdef BSP_USING_I2C3
|
|
|
|
#define I2C3_SPEED 100000
|
|
|
|
|
|
|
|
static struct rt_i2c_bus i2c_bus3;
|
|
|
|
|
2022-10-20 09:40:14 +08:00
|
|
|
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOC, ENABLE);
|
2022-07-13 19:56:14 +08:00
|
|
|
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_I2C3, ENABLE);
|
|
|
|
|
2022-10-20 09:40:14 +08:00
|
|
|
GPIO_InitStruct(&GPIO_InitStructure);
|
2022-07-13 19:56:14 +08:00
|
|
|
/* connect PC0 to I2C3_SCL, PC1 to I2C3_SDA */
|
2022-10-20 09:40:14 +08:00
|
|
|
GPIO_InitStructure.Pin = GPIO_PIN_0 | GPIO_PIN_1;
|
|
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
|
|
|
|
GPIO_InitPeripheral(GPIOC, &GPIO_InitStructure);
|
2022-07-23 11:53:42 +08:00
|
|
|
|
2022-07-13 19:56:14 +08:00
|
|
|
I2C_DeInit(I2C3);
|
|
|
|
I2C_InitStructure.BusMode = I2C_BUSMODE_I2C;
|
|
|
|
I2C_InitStructure.FmDutyCycle = I2C_FMDUTYCYCLE_2;
|
|
|
|
I2C_InitStructure.OwnAddr1 = 0xff;
|
|
|
|
I2C_InitStructure.AckEnable = I2C_ACKEN;
|
|
|
|
I2C_InitStructure.AddrMode = I2C_ADDR_MODE_7BIT;
|
|
|
|
I2C_InitStructure.ClkSpeed = I2C3_SPEED; // 100000 100K
|
|
|
|
|
|
|
|
I2C_Init(I2C3, &I2C_InitStructure);
|
|
|
|
|
|
|
|
rt_memset((void *)&i2c_bus3, 0, sizeof(struct rt_i2c_bus));
|
|
|
|
i2c_bus3.parent.ops = &i2c_ops;
|
|
|
|
i2c_bus3.i2c_periph = (rt_uint32_t)I2C3;
|
|
|
|
rt_i2c_bus_device_register(&i2c_bus3.parent, "i2c3");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef BSP_USING_I2C4
|
|
|
|
#define I2C4_SPEED 100000
|
|
|
|
|
|
|
|
static struct rt_i2c_bus i2c_bus4;
|
|
|
|
|
2022-10-20 09:40:14 +08:00
|
|
|
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOC, ENABLE);
|
2022-07-13 19:56:14 +08:00
|
|
|
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_I2C4, ENABLE);
|
|
|
|
|
2022-10-20 09:40:14 +08:00
|
|
|
GPIO_InitStruct(&GPIO_InitStructure);
|
2022-07-13 19:56:14 +08:00
|
|
|
/* connect PC6 to I2C4_SCL, PC7 to I2C4_SDA */
|
2022-10-20 09:40:14 +08:00
|
|
|
GPIO_InitStructure.Pin = GPIO_PIN_6 | GPIO_PIN_7;
|
|
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
|
|
|
|
GPIO_InitPeripheral(GPIOC, &GPIO_InitStructure);
|
2022-07-13 19:56:14 +08:00
|
|
|
|
|
|
|
I2C_DeInit(I2C4);
|
|
|
|
I2C_InitStructure.BusMode = I2C_BUSMODE_I2C;
|
|
|
|
I2C_InitStructure.FmDutyCycle = I2C_FMDUTYCYCLE_2;
|
|
|
|
I2C_InitStructure.OwnAddr1 = 0xff;
|
|
|
|
I2C_InitStructure.AckEnable = I2C_ACKEN;
|
|
|
|
I2C_InitStructure.AddrMode = I2C_ADDR_MODE_7BIT;
|
|
|
|
I2C_InitStructure.ClkSpeed = I2C4_SPEED; // 100000 100K
|
|
|
|
|
|
|
|
I2C_Init(I2C4, &I2C_InitStructure);
|
|
|
|
|
|
|
|
rt_memset((void *)&i2c_bus4, 0, sizeof(struct rt_i2c_bus));
|
|
|
|
i2c_bus4.parent.ops = &i2c_ops;
|
|
|
|
i2c_bus4.i2c_periph = (rt_uint32_t)I2C4;
|
|
|
|
rt_i2c_bus_device_register(&i2c_bus4.parent, "i2c4");
|
|
|
|
#endif
|
2022-10-20 09:40:14 +08:00
|
|
|
#endif
|
2022-07-15 13:36:32 +08:00
|
|
|
#endif /* RT_USING_HARDWARE_I2C */
|
2022-07-13 19:56:14 +08:00
|
|
|
|
2022-07-15 13:36:32 +08:00
|
|
|
return RT_EOK;
|
2022-07-13 19:56:14 +08:00
|
|
|
}
|
|
|
|
INIT_DEVICE_EXPORT(rt_hw_i2c_init);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
/* end of i2c driver */
|