Skip to content

Commit 6f68ca7

Browse files
GuEe-GUIRbb666
authored andcommitted
[DM/SPI] Support DM mode in SPI
Signed-off-by: GuEe-GUI <[email protected]>
1 parent 49d18ec commit 6f68ca7

File tree

7 files changed

+519
-0
lines changed

7 files changed

+519
-0
lines changed

components/drivers/include/drivers/dev_spi.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include <stdlib.h>
1717
#include <rtthread.h>
1818
#include <drivers/dev_pin.h>
19+
#include <drivers/core/driver.h>
20+
1921
/**
2022
* @addtogroup Drivers RTTHREAD Driver
2123
* @defgroup SPI SPI
@@ -151,7 +153,12 @@ struct rt_spi_configuration
151153
{
152154
rt_uint8_t mode;
153155
rt_uint8_t data_width;
156+
#ifdef RT_USING_DM
157+
rt_uint8_t data_width_tx;
158+
rt_uint8_t data_width_rx;
159+
#else
154160
rt_uint16_t reserved;
161+
#endif
155162

156163
rt_uint32_t max_hz;
157164
};
@@ -167,6 +174,12 @@ struct rt_spi_bus
167174
rt_uint8_t mode;
168175
const struct rt_spi_ops *ops;
169176

177+
#ifdef RT_USING_DM
178+
rt_base_t *pins;
179+
rt_bool_t slave;
180+
int num_chipselect;
181+
#endif /* RT_USING_DM */
182+
170183
struct rt_mutex lock;
171184
struct rt_spi_device *owner;
172185
};
@@ -180,6 +193,20 @@ struct rt_spi_ops
180193
rt_ssize_t (*xfer)(struct rt_spi_device *device, struct rt_spi_message *message);
181194
};
182195

196+
#ifdef RT_USING_DM
197+
/**
198+
* @brief SPI delay info
199+
*/
200+
struct rt_spi_delay
201+
{
202+
#define RT_SPI_DELAY_UNIT_USECS 0
203+
#define RT_SPI_DELAY_UNIT_NSECS 1
204+
#define RT_SPI_DELAY_UNIT_SCK 2
205+
rt_uint16_t value;
206+
rt_uint8_t unit;
207+
};
208+
#endif /* RT_USING_DM */
209+
183210
/**
184211
* @brief SPI Virtual BUS, one device must connected to a virtual BUS
185212
*/
@@ -188,6 +215,17 @@ struct rt_spi_device
188215
struct rt_device parent;
189216
struct rt_spi_bus *bus;
190217

218+
#ifdef RT_USING_DM
219+
const char *name;
220+
const struct rt_spi_device_id *id;
221+
const struct rt_ofw_node_id *ofw_id;
222+
223+
rt_uint8_t chip_select;
224+
struct rt_spi_delay cs_setup;
225+
struct rt_spi_delay cs_hold;
226+
struct rt_spi_delay cs_inactive;
227+
#endif
228+
191229
struct rt_spi_configuration config;
192230
rt_base_t cs_pin;
193231
void *user_data;
@@ -252,6 +290,31 @@ struct rt_qspi_device
252290

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

293+
#ifdef RT_USING_DM
294+
struct rt_spi_device_id
295+
{
296+
char name[20];
297+
void *data;
298+
};
299+
300+
struct rt_spi_driver
301+
{
302+
struct rt_driver parent;
303+
304+
const struct rt_spi_device_id *ids;
305+
const struct rt_ofw_node_id *ofw_ids;
306+
307+
rt_err_t (*probe)(struct rt_spi_device *device);
308+
rt_err_t (*remove)(struct rt_spi_device *device);
309+
rt_err_t (*shutdown)(struct rt_spi_device *device);
310+
};
311+
312+
rt_err_t rt_spi_driver_register(struct rt_spi_driver *driver);
313+
rt_err_t rt_spi_device_register(struct rt_spi_device *device);
314+
315+
#define RT_SPI_DRIVER_EXPORT(driver) RT_DRIVER_EXPORT(driver, spi, BUILIN)
316+
#endif /* RT_USING_DM */
317+
255318
/**
256319
* @brief register a SPI bus
257320
*

components/drivers/spi/SConscript

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ if GetDepend('RT_USING_SFUD'):
3535
elif rtconfig.PLATFORM in ['armcc']:
3636
LOCAL_CFLAGS += ' --c99'
3737

38+
if GetDepend('RT_USING_DM'):
39+
src += ['dev_spi_dm.c', 'dev_spi_bus.c']
40+
3841
src += src_device
3942

4043
group = DefineGroup('DeviceDrivers', src, depend = ['RT_USING_SPI'], CPPPATH = CPPPATH, LOCAL_CFLAGS = LOCAL_CFLAGS)

components/drivers/spi/dev_spi.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
#include <rtthread.h>
1111
#include "drivers/dev_spi.h"
1212

13+
#define DBG_TAG "spi.dev"
14+
#define DBG_LVL DBG_INFO
15+
#include <rtdbg.h>
16+
17+
#ifdef RT_USING_DM
18+
#include "dev_spi_dm.h"
19+
#endif
20+
1321
/* SPI bus device interface, compatible with RT-Thread 0.3.x/1.0.x */
1422
static rt_ssize_t _spi_bus_device_read(rt_device_t dev,
1523
rt_off_t pos,
@@ -155,3 +163,66 @@ rt_err_t rt_spidev_device_init(struct rt_spi_device *dev, const char *name)
155163
/* register to device manager */
156164
return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR);
157165
}
166+
167+
#ifdef RT_USING_DM
168+
static rt_err_t spidev_probe(struct rt_spi_device *spi_dev)
169+
{
170+
const char *bus_name;
171+
struct rt_device *dev = &spi_dev->parent;
172+
173+
if (spi_dev->parent.ofw_node)
174+
{
175+
if (rt_dm_dev_prop_index_of_string(dev, "compatible", "spidev") >= 0)
176+
{
177+
LOG_E("spidev is not supported in OFW");
178+
179+
return -RT_EINVAL;
180+
}
181+
}
182+
183+
bus_name = rt_dm_dev_get_name(&spi_dev->bus->parent);
184+
rt_dm_dev_set_name(dev, "%s_%d", bus_name, spi_dev->chip_select);
185+
186+
return RT_EOK;
187+
}
188+
189+
static const struct rt_spi_device_id spidev_ids[] =
190+
{
191+
{ .name = "dh2228fv" },
192+
{ .name = "ltc2488" },
193+
{ .name = "sx1301" },
194+
{ .name = "bk4" },
195+
{ .name = "dhcom-board" },
196+
{ .name = "m53cpld" },
197+
{ .name = "spi-petra" },
198+
{ .name = "spi-authenta" },
199+
{ .name = "em3581" },
200+
{ .name = "si3210" },
201+
{ /* sentinel */ },
202+
};
203+
204+
static const struct rt_ofw_node_id spidev_ofw_ids[] =
205+
{
206+
{ .compatible = "cisco,spi-petra" },
207+
{ .compatible = "dh,dhcom-board" },
208+
{ .compatible = "lineartechnology,ltc2488" },
209+
{ .compatible = "lwn,bk4" },
210+
{ .compatible = "menlo,m53cpld" },
211+
{ .compatible = "micron,spi-authenta" },
212+
{ .compatible = "rohm,dh2228fv" },
213+
{ .compatible = "semtech,sx1301" },
214+
{ .compatible = "silabs,em3581" },
215+
{ .compatible = "silabs,si3210" },
216+
{ .compatible = "rockchip,spidev" },
217+
{ /* sentinel */ },
218+
};
219+
220+
static struct rt_spi_driver spidev_driver =
221+
{
222+
.ids = spidev_ids,
223+
.ofw_ids = spidev_ofw_ids,
224+
225+
.probe = spidev_probe,
226+
};
227+
RT_SPI_DRIVER_EXPORT(spidev_driver);
228+
#endif /* RT_USING_DM */

0 commit comments

Comments
 (0)