2012-05-23 01:42:34 +08:00
|
|
|
/*
|
2021-04-09 10:52:34 +08:00
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
2012-05-23 01:42:34 +08:00
|
|
|
*
|
2021-04-09 10:52:34 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2012-05-23 01:42:34 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
2021-04-09 10:52:34 +08:00
|
|
|
* Date Author Notes
|
|
|
|
* 2012-04-25 weety first version
|
2012-05-23 01:42:34 +08:00
|
|
|
*/
|
|
|
|
|
2012-06-14 01:00:09 +08:00
|
|
|
#include <rtdevice.h>
|
2012-05-23 01:42:34 +08:00
|
|
|
#include <rthw.h>
|
|
|
|
#include <at91sam926x.h>
|
|
|
|
|
|
|
|
|
2024-04-30 08:46:12 +08:00
|
|
|
static void at91_i2c_gpio_init(void)
|
2012-05-23 01:42:34 +08:00
|
|
|
{
|
2021-04-09 10:52:34 +08:00
|
|
|
at91_sys_write(AT91_PMC_PCER, 1 << AT91SAM9260_ID_PIOA); //enable PIOA clock
|
|
|
|
at91_sys_write(AT91_PIOA + PIO_PUER, (1 << 23));
|
|
|
|
at91_sys_write(AT91_PIOA + PIO_PER, (1 << 23));
|
|
|
|
at91_sys_write(AT91_PIOA + PIO_MDER, (1 << 23));
|
|
|
|
at91_sys_write(AT91_PIOA + PIO_PUER, (1 << 24));
|
|
|
|
at91_sys_write(AT91_PIOA + PIO_PER, (1 << 24));
|
|
|
|
at91_sys_write(AT91_PIOA + PIO_MDER, (1 << 24));
|
|
|
|
|
|
|
|
at91_sys_write(AT91_PIOA + PIO_OER, (1 << 23));
|
|
|
|
at91_sys_write(AT91_PIOA + PIO_OER, (1 << 24));
|
|
|
|
|
|
|
|
at91_sys_write(AT91_PIOA + PIO_SODR, (1 << 23));
|
|
|
|
at91_sys_write(AT91_PIOA + PIO_SODR, (1 << 24));
|
2012-05-23 01:42:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void at91_set_sda(void *data, rt_int32_t state)
|
|
|
|
{
|
2021-04-09 10:52:34 +08:00
|
|
|
if (state)
|
|
|
|
{
|
|
|
|
at91_sys_write(AT91_PIOA + PIO_SODR, (1 << 23));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
at91_sys_write(AT91_PIOA + PIO_CODR, (1 << 23));
|
|
|
|
}
|
2012-05-23 01:42:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void at91_set_scl(void *data, rt_int32_t state)
|
|
|
|
{
|
2021-04-09 10:52:34 +08:00
|
|
|
if (state)
|
|
|
|
{
|
|
|
|
at91_sys_write(AT91_PIOA + PIO_SODR, (1 << 24));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
at91_sys_write(AT91_PIOA + PIO_CODR, (1 << 24));
|
|
|
|
}
|
2012-05-23 01:42:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static rt_int32_t at91_get_sda(void *data)
|
|
|
|
{
|
2021-04-09 10:52:34 +08:00
|
|
|
return at91_sys_read(AT91_PIOA + PIO_PDSR) & (1 << 23);
|
2012-05-23 01:42:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static rt_int32_t at91_get_scl(void *data)
|
|
|
|
{
|
2021-04-09 10:52:34 +08:00
|
|
|
return at91_sys_read(AT91_PIOA + PIO_PDSR) & (1 << 24);
|
2012-05-23 01:42:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void at91_udelay (rt_uint32_t us)
|
|
|
|
{
|
2021-04-09 10:52:34 +08:00
|
|
|
rt_int32_t i;
|
|
|
|
for (; us > 0; us--)
|
|
|
|
{
|
|
|
|
i = 50000;
|
|
|
|
while(i > 0)
|
|
|
|
{
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
}
|
2012-05-23 01:42:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct rt_i2c_bit_ops bit_ops = {
|
2021-04-09 10:52:34 +08:00
|
|
|
RT_NULL,
|
|
|
|
at91_set_sda,
|
|
|
|
at91_set_scl,
|
|
|
|
at91_get_sda,
|
|
|
|
at91_get_scl,
|
|
|
|
at91_udelay,
|
|
|
|
5,
|
2024-04-30 08:46:12 +08:00
|
|
|
100,
|
|
|
|
at91_i2c_gpio_init,
|
|
|
|
RT_FALSE
|
2012-05-23 01:42:34 +08:00
|
|
|
};
|
|
|
|
|
2017-10-19 23:46:17 +08:00
|
|
|
int at91_i2c_init(void)
|
2012-05-23 01:42:34 +08:00
|
|
|
{
|
2021-04-09 10:52:34 +08:00
|
|
|
struct rt_i2c_bus_device *bus;
|
|
|
|
|
|
|
|
bus = rt_malloc(sizeof(struct rt_i2c_bus_device));
|
|
|
|
if (bus == RT_NULL)
|
|
|
|
{
|
|
|
|
rt_kprintf("rt_malloc failed\n");
|
|
|
|
return -RT_ENOMEM;
|
|
|
|
}
|
2012-05-23 01:42:34 +08:00
|
|
|
|
2021-04-09 10:52:34 +08:00
|
|
|
rt_memset((void *)bus, 0, sizeof(struct rt_i2c_bus_device));
|
2012-05-23 01:42:34 +08:00
|
|
|
|
2021-04-09 10:52:34 +08:00
|
|
|
bus->priv = (void *)&bit_ops;
|
2012-05-23 01:42:34 +08:00
|
|
|
|
2021-04-09 10:52:34 +08:00
|
|
|
rt_i2c_bit_add_bus(bus, "i2c0");
|
2012-05-23 01:42:34 +08:00
|
|
|
|
2021-04-09 10:52:34 +08:00
|
|
|
return 0;
|
2012-05-23 01:42:34 +08:00
|
|
|
}
|
|
|
|
|
2017-10-19 23:46:17 +08:00
|
|
|
INIT_DEVICE_EXPORT(at91_i2c_init);
|
2012-05-23 01:42:34 +08:00
|
|
|
|