2017-12-04 18:52:02 +08:00
|
|
|
/*
|
2021-03-29 07:20:47 +08:00
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
2017-12-04 18:52:02 +08:00
|
|
|
*
|
2021-03-29 07:20:47 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2017-12-04 18:52:02 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2017-12-04 Haley the first version
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rtthread.h>
|
|
|
|
#include <rtdevice.h>
|
|
|
|
#include "am_mcu_apollo.h"
|
|
|
|
|
|
|
|
/* I2C0 */
|
|
|
|
#define AM_I2C0_IOM_INST 0
|
|
|
|
|
|
|
|
#define I2C0_GPIO_SCL 5
|
|
|
|
#define I2C0_GPIO_CFG_SCK AM_HAL_PIN_5_M0SCL
|
|
|
|
#define I2C0_GPIO_SDA 6
|
|
|
|
#define I2C0_GPIO_CFG_SDA AM_HAL_PIN_6_M0SDA
|
|
|
|
|
|
|
|
/* I2C2 */
|
|
|
|
#define AM_I2C2_IOM_INST 2
|
|
|
|
|
|
|
|
#define I2C2_GPIO_SCL 27
|
|
|
|
#define I2C2_GPIO_CFG_SCK AM_HAL_PIN_27_M2SCL
|
|
|
|
#define I2C2_GPIO_SDA 25
|
|
|
|
#define I2C2_GPIO_CFG_SDA AM_HAL_PIN_25_M2SDA
|
|
|
|
|
2018-09-21 16:10:44 +08:00
|
|
|
/* I2C3 */
|
|
|
|
#define AM_I2C3_IOM_INST 3
|
|
|
|
|
|
|
|
#define I2C3_GPIO_SCL 42
|
|
|
|
#define I2C3_GPIO_CFG_SCK AM_HAL_PIN_42_M3SCL
|
|
|
|
#define I2C3_GPIO_SDA 43
|
|
|
|
#define I2C3_GPIO_CFG_SDA AM_HAL_PIN_43_M3SDA
|
|
|
|
|
2017-12-04 18:52:02 +08:00
|
|
|
/* I2C4 */
|
|
|
|
#define AM_I2C4_IOM_INST 4
|
|
|
|
|
|
|
|
#define I2C4_GPIO_SCL 39
|
|
|
|
#define I2C4_GPIO_CFG_SCK AM_HAL_PIN_39_M4SCL
|
|
|
|
#define I2C4_GPIO_SDA 40
|
|
|
|
#define I2C4_GPIO_CFG_SDA AM_HAL_PIN_40_M4SDA
|
|
|
|
|
|
|
|
static am_hal_iom_config_t g_sIOMConfig =
|
|
|
|
{
|
|
|
|
AM_HAL_IOM_I2CMODE, // ui32InterfaceMode
|
|
|
|
AM_HAL_IOM_100KHZ, // ui32ClockFrequency
|
|
|
|
0, // bSPHA
|
|
|
|
0, // bSPOL
|
|
|
|
4, // ui8WriteThreshold
|
|
|
|
60, // ui8ReadThreshold
|
|
|
|
};
|
|
|
|
|
|
|
|
/* AM i2c driver */
|
|
|
|
struct am_i2c_bus
|
|
|
|
{
|
|
|
|
struct rt_i2c_bus_device parent;
|
|
|
|
rt_uint32_t u32Module;
|
|
|
|
};
|
|
|
|
|
|
|
|
//connect am drv to rt drv.
|
|
|
|
rt_size_t rt_i2c_master_xfer(struct rt_i2c_bus_device *bus,
|
|
|
|
struct rt_i2c_msg *msgs,
|
|
|
|
rt_uint32_t num)
|
|
|
|
{
|
|
|
|
struct am_i2c_bus * am_i2c_bus = (struct am_i2c_bus *)bus;
|
|
|
|
struct rt_i2c_msg *msg;
|
|
|
|
int i;
|
2018-09-21 16:10:44 +08:00
|
|
|
rt_uint32_t msg_len = 0;
|
2017-12-04 18:52:02 +08:00
|
|
|
|
|
|
|
for (i = 0; i < num; i++)
|
|
|
|
{
|
|
|
|
msg = &msgs[i];
|
|
|
|
if (msg->flags == RT_I2C_RD)
|
|
|
|
{
|
|
|
|
am_hal_iom_i2c_read(am_i2c_bus->u32Module, msg->addr, (uint32_t *)msg->buf, msg->len, AM_HAL_IOM_RAW);
|
2018-09-21 16:10:44 +08:00
|
|
|
msg_len += msg->len;
|
2017-12-04 18:52:02 +08:00
|
|
|
}
|
|
|
|
else if(msg->flags == RT_I2C_WR)
|
|
|
|
{
|
|
|
|
am_hal_iom_i2c_write(am_i2c_bus->u32Module, msg->addr, (uint32_t *)msg->buf, msg->len, AM_HAL_IOM_RAW);
|
2018-09-21 16:10:44 +08:00
|
|
|
msg_len += (msg->len - 1);
|
2017-12-04 18:52:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-21 16:10:44 +08:00
|
|
|
return msg_len;
|
2017-12-04 18:52:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
rt_err_t rt_i2c_bus_control(struct rt_i2c_bus_device *bus,
|
2023-07-14 09:57:40 +08:00
|
|
|
int cmd,
|
|
|
|
void *args)
|
2017-12-04 18:52:02 +08:00
|
|
|
{
|
|
|
|
struct am_i2c_bus * am_i2c_bus = (struct am_i2c_bus *)bus;
|
2023-07-14 09:57:40 +08:00
|
|
|
//rt_uint32_t ctrl_arg = *(rt_uint32_t *)args;
|
2017-12-04 18:52:02 +08:00
|
|
|
|
|
|
|
RT_ASSERT(bus != RT_NULL);
|
|
|
|
am_i2c_bus = (struct am_i2c_bus *)bus->parent.user_data;
|
|
|
|
|
|
|
|
RT_ASSERT(am_i2c_bus != RT_NULL);
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
/* I2C config */
|
|
|
|
case RT_DEVICE_CTRL_CONFIG :
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct rt_i2c_bus_device_ops am_i2c_ops =
|
|
|
|
{
|
|
|
|
rt_i2c_master_xfer,
|
|
|
|
RT_NULL,
|
|
|
|
rt_i2c_bus_control
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef RT_USING_I2C0
|
2021-03-29 07:20:47 +08:00
|
|
|
static struct am_i2c_bus am_i2c_bus_0 =
|
2017-12-04 18:52:02 +08:00
|
|
|
{
|
|
|
|
{0},
|
|
|
|
AM_I2C0_IOM_INST
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RT_USING_I2C1
|
2021-03-29 07:20:47 +08:00
|
|
|
static struct am_i2c_bus am_i2c_bus_1 =
|
2017-12-04 18:52:02 +08:00
|
|
|
{
|
2018-09-21 16:10:44 +08:00
|
|
|
{1},
|
2017-12-04 18:52:02 +08:00
|
|
|
AM_I2C1_IOM_INST
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RT_USING_I2C2
|
2021-03-29 07:20:47 +08:00
|
|
|
static struct am_i2c_bus am_i2c_bus_2 =
|
2017-12-04 18:52:02 +08:00
|
|
|
{
|
2018-09-21 16:10:44 +08:00
|
|
|
{2},
|
2017-12-04 18:52:02 +08:00
|
|
|
AM_I2C2_IOM_INST
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RT_USING_I2C3
|
2021-03-29 07:20:47 +08:00
|
|
|
static struct am_i2c_bus am_i2c_bus_3 =
|
2017-12-04 18:52:02 +08:00
|
|
|
{
|
2018-09-21 16:10:44 +08:00
|
|
|
{3},
|
2017-12-04 18:52:02 +08:00
|
|
|
AM_I2C3_IOM_INST
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RT_USING_I2C4
|
2021-03-29 07:20:47 +08:00
|
|
|
static struct am_i2c_bus am_i2c_bus_4 =
|
2017-12-04 18:52:02 +08:00
|
|
|
{
|
2018-09-21 16:10:44 +08:00
|
|
|
{4},
|
2017-12-04 18:52:02 +08:00
|
|
|
AM_I2C4_IOM_INST
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int rt_i2c_init(void)
|
|
|
|
{
|
|
|
|
struct am_i2c_bus* am_i2c;
|
|
|
|
|
|
|
|
#ifdef RT_USING_I2C0
|
|
|
|
/* init i2c gpio */
|
|
|
|
am_hal_gpio_pin_config(I2C0_GPIO_SCL, I2C0_GPIO_CFG_SCK | AM_HAL_GPIO_PULL6K);
|
|
|
|
am_hal_gpio_pin_config(I2C0_GPIO_SDA, I2C0_GPIO_CFG_SDA | AM_HAL_GPIO_PULL6K);
|
|
|
|
|
|
|
|
/* Initialize IOM 0 in I2C mode at 100KHz */
|
|
|
|
am_hal_iom_pwrctrl_enable(AM_I2C0_IOM_INST);
|
|
|
|
g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_100KHZ;
|
|
|
|
am_hal_iom_config(AM_I2C0_IOM_INST, &g_sIOMConfig);
|
|
|
|
am_hal_iom_enable(AM_I2C0_IOM_INST);
|
|
|
|
|
|
|
|
/* init i2c bus device */
|
|
|
|
am_i2c = &am_i2c_bus_0;
|
|
|
|
am_i2c->parent.ops = &am_i2c_ops;
|
|
|
|
rt_i2c_bus_device_register(&am_i2c->parent, "i2c0");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RT_USING_I2C2
|
|
|
|
/* init i2c gpio */
|
|
|
|
am_hal_gpio_pin_config(I2C2_GPIO_SCL, I2C2_GPIO_CFG_SCK | AM_HAL_GPIO_PULL6K);
|
|
|
|
am_hal_gpio_pin_config(I2C2_GPIO_SDA, I2C2_GPIO_CFG_SDA | AM_HAL_GPIO_PULL6K);
|
|
|
|
|
|
|
|
/* Initialize IOM 2 in I2C mode at 400KHz */
|
|
|
|
am_hal_iom_pwrctrl_enable(AM_I2C2_IOM_INST);
|
|
|
|
g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_400KHZ;
|
|
|
|
am_hal_iom_config(AM_I2C2_IOM_INST, &g_sIOMConfig);
|
|
|
|
am_hal_iom_enable(AM_I2C2_IOM_INST);
|
|
|
|
|
|
|
|
/* init i2c bus device */
|
|
|
|
am_i2c = &am_i2c_bus_2;
|
|
|
|
am_i2c->parent.ops = &am_i2c_ops;
|
|
|
|
rt_i2c_bus_device_register(&am_i2c->parent, "i2c2");
|
|
|
|
#endif
|
|
|
|
|
2018-09-21 16:10:44 +08:00
|
|
|
#ifdef RT_USING_I2C3
|
|
|
|
/* init i2c gpio */
|
|
|
|
am_hal_gpio_pin_config(I2C3_GPIO_SCL, I2C3_GPIO_CFG_SCK | AM_HAL_GPIO_PULL6K);
|
|
|
|
am_hal_gpio_pin_config(I2C3_GPIO_SDA, I2C3_GPIO_CFG_SDA | AM_HAL_GPIO_PULL6K);
|
|
|
|
|
|
|
|
/* Initialize IOM 3 in I2C mode at 400KHz */
|
|
|
|
am_hal_iom_pwrctrl_enable(AM_I2C3_IOM_INST);
|
|
|
|
g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_400KHZ;
|
|
|
|
am_hal_iom_config(AM_I2C3_IOM_INST, &g_sIOMConfig);
|
|
|
|
am_hal_iom_enable(AM_I2C3_IOM_INST);
|
|
|
|
|
|
|
|
/* init i2c bus device */
|
|
|
|
am_i2c = &am_i2c_bus_3;
|
|
|
|
am_i2c->parent.ops = &am_i2c_ops;
|
|
|
|
rt_i2c_bus_device_register(&am_i2c->parent, "i2c3");
|
|
|
|
#endif
|
|
|
|
|
2017-12-04 18:52:02 +08:00
|
|
|
#ifdef RT_USING_I2C4
|
|
|
|
/* init i2c gpio */
|
|
|
|
am_hal_gpio_pin_config(I2C4_GPIO_SCL, I2C4_GPIO_CFG_SCK | AM_HAL_GPIO_PULL6K);
|
|
|
|
am_hal_gpio_pin_config(I2C4_GPIO_SDA, I2C4_GPIO_CFG_SDA | AM_HAL_GPIO_PULL6K);
|
|
|
|
|
|
|
|
/* Initialize IOM 4 in I2C mode at 400KHz */
|
|
|
|
am_hal_iom_pwrctrl_enable(AM_I2C4_IOM_INST);
|
|
|
|
g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_400KHZ;
|
|
|
|
am_hal_iom_config(AM_I2C4_IOM_INST, &g_sIOMConfig);
|
|
|
|
am_hal_iom_enable(AM_I2C4_IOM_INST);
|
|
|
|
|
|
|
|
/* init i2c bus device */
|
|
|
|
am_i2c = &am_i2c_bus_4;
|
|
|
|
am_i2c->parent.ops = &am_i2c_ops;
|
|
|
|
rt_i2c_bus_device_register(&am_i2c->parent, "i2c4");
|
|
|
|
#endif
|
|
|
|
|
2018-09-21 16:10:44 +08:00
|
|
|
//rt_kprintf("i2c_init!\n");
|
2017-12-04 18:52:02 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#ifdef RT_USING_COMPONENTS_INIT
|
|
|
|
INIT_BOARD_EXPORT(rt_i2c_init);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*@}*/
|