Skip to content

Commit cf539b8

Browse files
committed
[DM/SPI] Make CS pin config fixed in system
Make a max CS pin value (16) for SPI, that will not alloc `*cs_pins` by malloc, because drivers call `rt_device_unregister` may not free item. Fixup the QSPI init configure member in DM mode. Make SoC Kconfig import easy. Signed-off-by: GuEe-GUI <[email protected]>
1 parent c5a79de commit cf539b8

File tree

5 files changed

+25
-24
lines changed

5 files changed

+25
-24
lines changed

components/drivers/include/drivers/dev_spi.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ extern "C"{
132132
#define RT_SPI_BUS_MODE_SPI (1<<0)
133133
#define RT_SPI_BUS_MODE_QSPI (1<<1)
134134

135+
#define RT_SPI_CS_CNT_MAX 16
136+
135137
/**
136138
* @brief SPI message structure
137139
*/
@@ -175,7 +177,8 @@ struct rt_spi_bus
175177
const struct rt_spi_ops *ops;
176178

177179
#ifdef RT_USING_DM
178-
rt_base_t *pins;
180+
rt_base_t cs_pins[RT_SPI_CS_CNT_MAX];
181+
rt_uint8_t cs_active_vals[RT_SPI_CS_CNT_MAX];
179182
rt_bool_t slave;
180183
int num_chipselect;
181184
#endif /* RT_USING_DM */
@@ -220,7 +223,7 @@ struct rt_spi_device
220223
const struct rt_spi_device_id *id;
221224
const struct rt_ofw_node_id *ofw_id;
222225

223-
rt_uint8_t chip_select;
226+
rt_uint8_t chip_select[RT_SPI_CS_CNT_MAX];
224227
struct rt_spi_delay cs_setup;
225228
struct rt_spi_delay cs_hold;
226229
struct rt_spi_delay cs_inactive;

components/drivers/spi/dev_qspi_core.c

+5
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@ rt_err_t rt_qspi_configure(struct rt_qspi_device *device, struct rt_qspi_configu
5252
device->config.parent.mode = cfg->parent.mode;
5353
device->config.parent.max_hz = cfg->parent.max_hz;
5454
device->config.parent.data_width = cfg->parent.data_width;
55+
#ifdef RT_USING_DM
56+
device->config.parent.data_width_tx = cfg->parent.data_width_tx;
57+
device->config.parent.data_width_rx = cfg->parent.data_width_rx;
58+
#else
5559
device->config.parent.reserved = cfg->parent.reserved;
60+
#endif
5661
device->config.medium_size = cfg->medium_size;
5762
device->config.ddr_mode = cfg->ddr_mode;
5863
device->config.qspi_dl_width = cfg->qspi_dl_width;

components/drivers/spi/dev_spi_bus.c

+2-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ void spi_bus_scan_devices(struct rt_spi_bus *bus)
2727

2828
rt_ofw_foreach_available_child_node(np, spi_dev_np)
2929
{
30-
rt_uint64_t reg_offset;
3130
struct rt_spi_device *spi_dev;
3231

3332
if (!rt_ofw_prop_read_bool(spi_dev_np, "compatible"))
@@ -46,8 +45,6 @@ void spi_bus_scan_devices(struct rt_spi_bus *bus)
4645
return;
4746
}
4847

49-
rt_ofw_get_address(spi_dev_np, 0, &reg_offset, RT_NULL);
50-
5148
spi_dev->parent.ofw_node = spi_dev_np;
5249
spi_dev->parent.type = RT_Device_Class_Unknown;
5350
spi_dev->name = rt_ofw_node_name(spi_dev_np);
@@ -137,9 +134,9 @@ static rt_err_t spi_probe(rt_device_t dev)
137134

138135
bus = device->bus;
139136

140-
if (bus->pins)
137+
if (bus->cs_pins[0] >= 0)
141138
{
142-
device->cs_pin = bus->pins[device->chip_select];
139+
device->cs_pin = bus->cs_pins[device->chip_select[0]];
143140

144141
rt_pin_mode(device->cs_pin, PIN_MODE_OUTPUT);
145142
}

components/drivers/spi/dev_spi_core.c

+3-14
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,14 @@ rt_err_t rt_spi_bus_register(struct rt_spi_bus *bus,
5353
if (pin_count > 0)
5454
{
5555
pin_count = rt_max_t(int, pin_count, bus->num_chipselect);
56-
bus->pins = rt_malloc(sizeof(bus->pins[0]) * pin_count);
57-
58-
if (!bus->pins)
59-
{
60-
rt_device_unregister(&bus->parent);
61-
return -RT_ENOMEM;
62-
}
6356

6457
for (int i = 0; i < pin_count; ++i)
6558
{
66-
bus->pins[i] = rt_pin_get_named_pin(&bus->parent, "cs", i,
67-
RT_NULL, RT_NULL);
59+
bus->cs_pins[i] = rt_pin_get_named_pin(&bus->parent, "cs", i,
60+
RT_NULL, &bus->cs_active_vals[i]);
6861
}
6962
}
70-
else if (pin_count == 0)
71-
{
72-
bus->pins = RT_NULL;
73-
}
74-
else
63+
else if (pin_count < 0)
7564
{
7665
result = pin_count;
7766

components/drivers/spi/dev_spi_dm.c

+10-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ static void ofw_parse_delay(struct rt_ofw_node *np, struct rt_spi_delay *delay,
3838
rt_err_t spi_device_ofw_parse(struct rt_spi_device *spi_dev)
3939
{
4040
rt_err_t err;
41-
rt_uint32_t value;
41+
rt_uint32_t value, cs[RT_SPI_CS_CNT_MAX];
4242
struct rt_spi_bus *spi_bus = spi_dev->bus;
4343
struct rt_ofw_node *np = spi_dev->parent.ofw_node;
4444
struct rt_spi_configuration *conf = &spi_dev->config;
@@ -84,13 +84,20 @@ rt_err_t spi_device_ofw_parse(struct rt_spi_device *spi_dev)
8484
return RT_EOK;
8585
}
8686

87-
if ((err = rt_ofw_prop_read_u32(np, "reg", &value)))
87+
value = rt_ofw_prop_read_u32_array_index(np, "reg", 0, RT_SPI_CS_CNT_MAX, cs);
88+
89+
if ((rt_int32_t)value < 0)
8890
{
91+
err = (rt_err_t)value;
8992
LOG_E("Find 'reg' failed");
9093

9194
return err;
9295
}
93-
spi_dev->chip_select = value;
96+
97+
for (int i = 0; i < value; ++i)
98+
{
99+
spi_dev->chip_select[i] = cs[i];
100+
}
94101

95102
if (!rt_ofw_prop_read_u32(np, "spi-max-frequency", &value))
96103
{

0 commit comments

Comments
 (0)