Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions components/drivers/include/drivers/dev_spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include <stdlib.h>
#include <rtthread.h>
#include <drivers/dev_pin.h>
#include <drivers/core/driver.h>

/**
* @addtogroup Drivers RTTHREAD Driver
* @defgroup SPI SPI
Expand Down Expand Up @@ -151,7 +153,12 @@ struct rt_spi_configuration
{
rt_uint8_t mode;
rt_uint8_t data_width;
#ifdef RT_USING_DM
rt_uint8_t data_width_tx;
rt_uint8_t data_width_rx;
#else
rt_uint16_t reserved;
#endif

rt_uint32_t max_hz;
};
Expand All @@ -167,6 +174,12 @@ struct rt_spi_bus
rt_uint8_t mode;
const struct rt_spi_ops *ops;

#ifdef RT_USING_DM
rt_base_t *pins;
rt_bool_t slave;
int num_chipselect;
#endif /* RT_USING_DM */

struct rt_mutex lock;
struct rt_spi_device *owner;
};
Expand All @@ -180,6 +193,20 @@ struct rt_spi_ops
rt_ssize_t (*xfer)(struct rt_spi_device *device, struct rt_spi_message *message);
};

#ifdef RT_USING_DM
/**
* @brief SPI delay info
*/
struct rt_spi_delay
{
#define RT_SPI_DELAY_UNIT_USECS 0
#define RT_SPI_DELAY_UNIT_NSECS 1
#define RT_SPI_DELAY_UNIT_SCK 2
rt_uint16_t value;
rt_uint8_t unit;
};
#endif /* RT_USING_DM */

/**
* @brief SPI Virtual BUS, one device must connected to a virtual BUS
*/
Expand All @@ -188,6 +215,17 @@ struct rt_spi_device
struct rt_device parent;
struct rt_spi_bus *bus;

#ifdef RT_USING_DM
const char *name;
const struct rt_spi_device_id *id;
const struct rt_ofw_node_id *ofw_id;

rt_uint8_t chip_select;
struct rt_spi_delay cs_setup;
struct rt_spi_delay cs_hold;
struct rt_spi_delay cs_inactive;
#endif

struct rt_spi_configuration config;
rt_base_t cs_pin;
void *user_data;
Expand Down Expand Up @@ -252,6 +290,31 @@ struct rt_qspi_device

#define SPI_DEVICE(dev) ((struct rt_spi_device *)(dev))

#ifdef RT_USING_DM
struct rt_spi_device_id
{
char name[20];
void *data;
};

struct rt_spi_driver
{
struct rt_driver parent;

const struct rt_spi_device_id *ids;
const struct rt_ofw_node_id *ofw_ids;

rt_err_t (*probe)(struct rt_spi_device *device);
rt_err_t (*remove)(struct rt_spi_device *device);
rt_err_t (*shutdown)(struct rt_spi_device *device);
};

rt_err_t rt_spi_driver_register(struct rt_spi_driver *driver);
rt_err_t rt_spi_device_register(struct rt_spi_device *device);

#define RT_SPI_DRIVER_EXPORT(driver) RT_DRIVER_EXPORT(driver, spi, BUILIN)
#endif /* RT_USING_DM */

/**
* @brief register a SPI bus
*
Expand Down
3 changes: 3 additions & 0 deletions components/drivers/spi/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ if GetDepend('RT_USING_SFUD'):
elif rtconfig.PLATFORM in ['armcc']:
LOCAL_CFLAGS += ' --c99'

if GetDepend('RT_USING_DM'):
src += ['dev_spi_dm.c', 'dev_spi_bus.c']

src += src_device

group = DefineGroup('DeviceDrivers', src, depend = ['RT_USING_SPI'], CPPPATH = CPPPATH, LOCAL_CFLAGS = LOCAL_CFLAGS)
Expand Down
71 changes: 71 additions & 0 deletions components/drivers/spi/dev_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
#include <rtthread.h>
#include "drivers/dev_spi.h"

#define DBG_TAG "spi.dev"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>

#ifdef RT_USING_DM
#include "dev_spi_dm.h"
#endif

/* SPI bus device interface, compatible with RT-Thread 0.3.x/1.0.x */
static rt_ssize_t _spi_bus_device_read(rt_device_t dev,
rt_off_t pos,
Expand Down Expand Up @@ -155,3 +163,66 @@ rt_err_t rt_spidev_device_init(struct rt_spi_device *dev, const char *name)
/* register to device manager */
return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR);
}

#ifdef RT_USING_DM
static rt_err_t spidev_probe(struct rt_spi_device *spi_dev)
{
const char *bus_name;
struct rt_device *dev = &spi_dev->parent;

if (spi_dev->parent.ofw_node)
{
if (rt_dm_dev_prop_index_of_string(dev, "compatible", "spidev") >= 0)
{
LOG_E("spidev is not supported in OFW");

return -RT_EINVAL;
}
}

bus_name = rt_dm_dev_get_name(&spi_dev->bus->parent);
rt_dm_dev_set_name(dev, "%s_%d", bus_name, spi_dev->chip_select);

return RT_EOK;
}

static const struct rt_spi_device_id spidev_ids[] =
{
{ .name = "dh2228fv" },
{ .name = "ltc2488" },
{ .name = "sx1301" },
{ .name = "bk4" },
{ .name = "dhcom-board" },
{ .name = "m53cpld" },
{ .name = "spi-petra" },
{ .name = "spi-authenta" },
{ .name = "em3581" },
{ .name = "si3210" },
{ /* sentinel */ },
};

static const struct rt_ofw_node_id spidev_ofw_ids[] =
{
{ .compatible = "cisco,spi-petra" },
{ .compatible = "dh,dhcom-board" },
{ .compatible = "lineartechnology,ltc2488" },
{ .compatible = "lwn,bk4" },
{ .compatible = "menlo,m53cpld" },
{ .compatible = "micron,spi-authenta" },
{ .compatible = "rohm,dh2228fv" },
{ .compatible = "semtech,sx1301" },
{ .compatible = "silabs,em3581" },
{ .compatible = "silabs,si3210" },
{ .compatible = "rockchip,spidev" },
{ /* sentinel */ },
};

static struct rt_spi_driver spidev_driver =
{
.ids = spidev_ids,
.ofw_ids = spidev_ofw_ids,

.probe = spidev_probe,
};
RT_SPI_DRIVER_EXPORT(spidev_driver);
#endif /* RT_USING_DM */
Loading