2012-09-06 15:23:11 +08:00
|
|
|
/*
|
2021-03-08 18:19:04 +08:00
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
2012-09-06 15:23:11 +08:00
|
|
|
*
|
2018-10-14 19:37:18 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2012-09-06 15:23:11 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rtthread.h>
|
|
|
|
#include <drivers/spi.h>
|
|
|
|
|
|
|
|
/* SPI bus device interface, compatible with RT-Thread 0.3.x/1.0.x */
|
2023-02-06 07:35:33 +08:00
|
|
|
static rt_ssize_t _spi_bus_device_read(rt_device_t dev,
|
2012-09-06 15:23:11 +08:00
|
|
|
rt_off_t pos,
|
|
|
|
void *buffer,
|
|
|
|
rt_size_t size)
|
|
|
|
{
|
|
|
|
struct rt_spi_bus *bus;
|
|
|
|
|
|
|
|
bus = (struct rt_spi_bus *)dev;
|
|
|
|
RT_ASSERT(bus != RT_NULL);
|
|
|
|
RT_ASSERT(bus->owner != RT_NULL);
|
|
|
|
|
|
|
|
return rt_spi_transfer(bus->owner, RT_NULL, buffer, size);
|
|
|
|
}
|
|
|
|
|
2023-02-06 07:35:33 +08:00
|
|
|
static rt_ssize_t _spi_bus_device_write(rt_device_t dev,
|
2012-09-06 15:23:11 +08:00
|
|
|
rt_off_t pos,
|
|
|
|
const void *buffer,
|
|
|
|
rt_size_t size)
|
|
|
|
{
|
|
|
|
struct rt_spi_bus *bus;
|
|
|
|
|
|
|
|
bus = (struct rt_spi_bus *)dev;
|
|
|
|
RT_ASSERT(bus != RT_NULL);
|
|
|
|
RT_ASSERT(bus->owner != RT_NULL);
|
|
|
|
|
|
|
|
return rt_spi_transfer(bus->owner, buffer, RT_NULL, size);
|
|
|
|
}
|
|
|
|
|
2018-06-10 17:59:17 +08:00
|
|
|
#ifdef RT_USING_DEVICE_OPS
|
2021-03-08 18:19:04 +08:00
|
|
|
const static struct rt_device_ops spi_bus_ops =
|
2018-06-10 17:59:17 +08:00
|
|
|
{
|
|
|
|
RT_NULL,
|
|
|
|
RT_NULL,
|
|
|
|
RT_NULL,
|
|
|
|
_spi_bus_device_read,
|
|
|
|
_spi_bus_device_write,
|
2022-05-30 18:05:05 +08:00
|
|
|
RT_NULL
|
2018-06-10 17:59:17 +08:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2012-09-06 15:23:11 +08:00
|
|
|
rt_err_t rt_spi_bus_device_init(struct rt_spi_bus *bus, const char *name)
|
|
|
|
{
|
|
|
|
struct rt_device *device;
|
|
|
|
RT_ASSERT(bus != RT_NULL);
|
|
|
|
|
|
|
|
device = &bus->parent;
|
|
|
|
|
|
|
|
/* set device type */
|
|
|
|
device->type = RT_Device_Class_SPIBUS;
|
|
|
|
/* initialize device interface */
|
2018-06-10 17:59:17 +08:00
|
|
|
#ifdef RT_USING_DEVICE_OPS
|
|
|
|
device->ops = &spi_bus_ops;
|
|
|
|
#else
|
2014-08-03 14:29:46 +08:00
|
|
|
device->init = RT_NULL;
|
2012-09-06 15:23:11 +08:00
|
|
|
device->open = RT_NULL;
|
|
|
|
device->close = RT_NULL;
|
|
|
|
device->read = _spi_bus_device_read;
|
|
|
|
device->write = _spi_bus_device_write;
|
2022-05-30 18:05:05 +08:00
|
|
|
device->control = RT_NULL;
|
2018-06-10 17:59:17 +08:00
|
|
|
#endif
|
2012-09-06 15:23:11 +08:00
|
|
|
|
|
|
|
/* register to device manager */
|
|
|
|
return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* SPI Dev device interface, compatible with RT-Thread 0.3.x/1.0.x */
|
2023-02-06 07:35:33 +08:00
|
|
|
static rt_ssize_t _spidev_device_read(rt_device_t dev,
|
2012-09-06 15:23:11 +08:00
|
|
|
rt_off_t pos,
|
|
|
|
void *buffer,
|
|
|
|
rt_size_t size)
|
|
|
|
{
|
|
|
|
struct rt_spi_device *device;
|
|
|
|
|
|
|
|
device = (struct rt_spi_device *)dev;
|
|
|
|
RT_ASSERT(device != RT_NULL);
|
|
|
|
RT_ASSERT(device->bus != RT_NULL);
|
|
|
|
|
|
|
|
return rt_spi_transfer(device, RT_NULL, buffer, size);
|
|
|
|
}
|
|
|
|
|
2023-02-06 07:35:33 +08:00
|
|
|
static rt_ssize_t _spidev_device_write(rt_device_t dev,
|
2012-09-06 15:23:11 +08:00
|
|
|
rt_off_t pos,
|
|
|
|
const void *buffer,
|
|
|
|
rt_size_t size)
|
|
|
|
{
|
|
|
|
struct rt_spi_device *device;
|
|
|
|
|
|
|
|
device = (struct rt_spi_device *)dev;
|
|
|
|
RT_ASSERT(device != RT_NULL);
|
|
|
|
RT_ASSERT(device->bus != RT_NULL);
|
|
|
|
|
|
|
|
return rt_spi_transfer(device, buffer, RT_NULL, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static rt_err_t _spidev_device_control(rt_device_t dev,
|
2017-10-15 22:56:46 +08:00
|
|
|
int cmd,
|
2012-09-06 15:23:11 +08:00
|
|
|
void *args)
|
|
|
|
{
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case 0: /* set device */
|
|
|
|
break;
|
2021-03-08 18:19:04 +08:00
|
|
|
case 1:
|
2012-09-06 15:23:11 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2018-06-10 17:59:17 +08:00
|
|
|
#ifdef RT_USING_DEVICE_OPS
|
2021-03-08 18:19:04 +08:00
|
|
|
const static struct rt_device_ops spi_device_ops =
|
2018-06-10 17:59:17 +08:00
|
|
|
{
|
|
|
|
RT_NULL,
|
|
|
|
RT_NULL,
|
|
|
|
RT_NULL,
|
|
|
|
_spidev_device_read,
|
|
|
|
_spidev_device_write,
|
|
|
|
_spidev_device_control
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2012-09-06 15:23:11 +08:00
|
|
|
rt_err_t rt_spidev_device_init(struct rt_spi_device *dev, const char *name)
|
|
|
|
{
|
|
|
|
struct rt_device *device;
|
|
|
|
RT_ASSERT(dev != RT_NULL);
|
|
|
|
|
|
|
|
device = &(dev->parent);
|
|
|
|
|
|
|
|
/* set device type */
|
|
|
|
device->type = RT_Device_Class_SPIDevice;
|
2018-06-10 17:59:17 +08:00
|
|
|
#ifdef RT_USING_DEVICE_OPS
|
|
|
|
device->ops = &spi_device_ops;
|
|
|
|
#else
|
2014-08-03 14:29:46 +08:00
|
|
|
device->init = RT_NULL;
|
2012-09-06 15:23:11 +08:00
|
|
|
device->open = RT_NULL;
|
|
|
|
device->close = RT_NULL;
|
|
|
|
device->read = _spidev_device_read;
|
|
|
|
device->write = _spidev_device_write;
|
|
|
|
device->control = _spidev_device_control;
|
2018-06-10 17:59:17 +08:00
|
|
|
#endif
|
|
|
|
|
2012-09-06 15:23:11 +08:00
|
|
|
/* register to device manager */
|
|
|
|
return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR);
|
|
|
|
}
|