Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DM/SPI] Make CS pin config fixed in system #9977

Merged
merged 2 commits into from
Feb 11, 2025
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
7 changes: 5 additions & 2 deletions components/drivers/include/drivers/dev_spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ extern "C"{
#define RT_SPI_BUS_MODE_SPI (1<<0)
#define RT_SPI_BUS_MODE_QSPI (1<<1)

#define RT_SPI_CS_CNT_MAX 16

/**
* @brief SPI message structure
*/
Expand Down Expand Up @@ -175,7 +177,8 @@ struct rt_spi_bus
const struct rt_spi_ops *ops;

#ifdef RT_USING_DM
rt_base_t *pins;
rt_base_t cs_pins[RT_SPI_CS_CNT_MAX];
rt_uint8_t cs_active_vals[RT_SPI_CS_CNT_MAX];
rt_bool_t slave;
int num_chipselect;
#endif /* RT_USING_DM */
Expand Down Expand Up @@ -220,7 +223,7 @@ struct rt_spi_device
const struct rt_spi_device_id *id;
const struct rt_ofw_node_id *ofw_id;

rt_uint8_t chip_select;
rt_uint8_t chip_select[RT_SPI_CS_CNT_MAX];
struct rt_spi_delay cs_setup;
struct rt_spi_delay cs_hold;
struct rt_spi_delay cs_inactive;
Expand Down
5 changes: 5 additions & 0 deletions components/drivers/spi/dev_qspi_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ rt_err_t rt_qspi_configure(struct rt_qspi_device *device, struct rt_qspi_configu
device->config.parent.mode = cfg->parent.mode;
device->config.parent.max_hz = cfg->parent.max_hz;
device->config.parent.data_width = cfg->parent.data_width;
#ifdef RT_USING_DM
device->config.parent.data_width_tx = cfg->parent.data_width_tx;
device->config.parent.data_width_rx = cfg->parent.data_width_rx;
#else
device->config.parent.reserved = cfg->parent.reserved;
#endif
device->config.medium_size = cfg->medium_size;
device->config.ddr_mode = cfg->ddr_mode;
device->config.qspi_dl_width = cfg->qspi_dl_width;
Expand Down
7 changes: 2 additions & 5 deletions components/drivers/spi/dev_spi_bus.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ void spi_bus_scan_devices(struct rt_spi_bus *bus)

rt_ofw_foreach_available_child_node(np, spi_dev_np)
{
rt_uint64_t reg_offset;
struct rt_spi_device *spi_dev;

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

rt_ofw_get_address(spi_dev_np, 0, &reg_offset, RT_NULL);

spi_dev->parent.ofw_node = spi_dev_np;
spi_dev->parent.type = RT_Device_Class_Unknown;
spi_dev->name = rt_ofw_node_name(spi_dev_np);
Expand Down Expand Up @@ -137,9 +134,9 @@ static rt_err_t spi_probe(rt_device_t dev)

bus = device->bus;

if (bus->pins)
if (bus->cs_pins[0] >= 0)
{
device->cs_pin = bus->pins[device->chip_select];
device->cs_pin = bus->cs_pins[device->chip_select[0]];

rt_pin_mode(device->cs_pin, PIN_MODE_OUTPUT);
}
Expand Down
17 changes: 3 additions & 14 deletions components/drivers/spi/dev_spi_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,14 @@ rt_err_t rt_spi_bus_register(struct rt_spi_bus *bus,
if (pin_count > 0)
{
pin_count = rt_max_t(int, pin_count, bus->num_chipselect);
bus->pins = rt_malloc(sizeof(bus->pins[0]) * pin_count);

if (!bus->pins)
{
rt_device_unregister(&bus->parent);
return -RT_ENOMEM;
}

for (int i = 0; i < pin_count; ++i)
{
bus->pins[i] = rt_pin_get_named_pin(&bus->parent, "cs", i,
RT_NULL, RT_NULL);
bus->cs_pins[i] = rt_pin_get_named_pin(&bus->parent, "cs", i,
RT_NULL, &bus->cs_active_vals[i]);
}
}
else if (pin_count == 0)
{
bus->pins = RT_NULL;
}
else
else if (pin_count < 0)
{
result = pin_count;

Expand Down
13 changes: 10 additions & 3 deletions components/drivers/spi/dev_spi_dm.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ static void ofw_parse_delay(struct rt_ofw_node *np, struct rt_spi_delay *delay,
rt_err_t spi_device_ofw_parse(struct rt_spi_device *spi_dev)
{
rt_err_t err;
rt_uint32_t value;
rt_uint32_t value, cs[RT_SPI_CS_CNT_MAX];
struct rt_spi_bus *spi_bus = spi_dev->bus;
struct rt_ofw_node *np = spi_dev->parent.ofw_node;
struct rt_spi_configuration *conf = &spi_dev->config;
Expand Down Expand Up @@ -84,13 +84,20 @@ rt_err_t spi_device_ofw_parse(struct rt_spi_device *spi_dev)
return RT_EOK;
}

if ((err = rt_ofw_prop_read_u32(np, "reg", &value)))
value = rt_ofw_prop_read_u32_array_index(np, "reg", 0, RT_SPI_CS_CNT_MAX, cs);

if ((rt_int32_t)value < 0)
{
err = (rt_err_t)value;
LOG_E("Find 'reg' failed");

return err;
}
spi_dev->chip_select = value;

for (int i = 0; i < value; ++i)
{
spi_dev->chip_select[i] = cs[i];
}

if (!rt_ofw_prop_read_u32(np, "spi-max-frequency", &value))
{
Expand Down
Loading