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

[drivers/spi]修复spi configure会执行两次的问题 #9972

Merged
merged 3 commits into from
Feb 11, 2025
Merged
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
16 changes: 9 additions & 7 deletions components/drivers/spi/dev_spi_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ rt_err_t rt_spi_bus_register(struct rt_spi_bus *bus,
for (int i = 0; i < pin_count; ++i)
{
bus->pins[i] = rt_pin_get_named_pin(&bus->parent, "cs", i,
RT_NULL, RT_NULL);
RT_NULL, RT_NULL);
}
}
else if (pin_count == 0)
Expand Down Expand Up @@ -103,12 +103,15 @@ rt_err_t rt_spi_bus_attach_device_cspin(struct rt_spi_device *device,
{
device->bus = (struct rt_spi_bus *)bus;

if (device->bus->owner == RT_NULL)
device->bus->owner = device;

/* initialize spidev device */
result = rt_spidev_device_init(device, name);
if (result != RT_EOK)
return result;

if(cs_pin != PIN_NONE)
if (cs_pin != PIN_NONE)
{
rt_pin_mode(cs_pin, PIN_MODE_OUTPUT);
}
Expand Down Expand Up @@ -140,7 +143,7 @@ rt_err_t rt_spi_bus_configure(struct rt_spi_device *device)
result = rt_mutex_take(&(device->bus->lock), RT_WAITING_FOREVER);
if (result == RT_EOK)
{
if (device->bus->owner == RT_NULL || device->bus->owner == device)
if (device->bus->owner == device)
{
/* current device is using, re-configure SPI bus */
result = device->bus->ops->configure(device, &device->config);
Expand All @@ -157,7 +160,6 @@ rt_err_t rt_spi_bus_configure(struct rt_spi_device *device)
*/
result = -RT_EBUSY;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • 这里的判定是没问题的.
  • 不是当前使用的设备尝试配置总线,返回RT_EBUSY既可

Copy link
Member Author

@Rbb666 Rbb666 Feb 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

发现当前使用的设备尝试配置总线也会返回busy的,比如下面的测试代码:

/* 挂载到指定SPI总线 */
rt_hw_spi_device_attach("spi1", "wspi", -1);
spi_device = (struct rt_spi_device *)rt_device_find("wspi");
/* 返回的res会是-RT_EBUSY  */
rt_err_t res = rt_spi_configure(rw007_spi.spi_device, &cfg);

因为rt_spi_configure里面的 device->bus->owner 是 RT_NULL,在

加上device->bus->owner = device; 给owner设备赋值可以解决问题。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

发现当前使用的设备尝试配置总线也会返回busy的,比如下面的测试代码:

/* 挂载到指定SPI总线 */
rt_hw_spi_device_attach(”spi1“, "wspi", -1);
spi_device = (struct rt_spi_device *)rt_device_find("wspi");
/* 返回的res会是-RT_EBUSY  */
rt_err_t res = rt_spi_configure(rw007_spi.spi_device, &cfg);

因为rt_spi_configure里面的 device->bus->owner 是 RT_NULL,在

加上device->bus->owner = device; 给owner设备赋值可以解决问题。

  • 这个位置添加这个逻辑,我觉得合理.
  • 应该加一个判断,避免每个attach device都修改owner
        if(device->bus->owner == RT_NULL)
        {
            device->bus->owner = device;
        }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

发现当前使用的设备尝试配置总线也会返回busy的,比如下面的测试代码:

/* 挂载到指定SPI总线 */
rt_hw_spi_device_attach(”spi1“, "wspi", -1);
spi_device = (struct rt_spi_device *)rt_device_find("wspi");
/* 返回的res会是-RT_EBUSY  */
rt_err_t res = rt_spi_configure(rw007_spi.spi_device, &cfg);

因为rt_spi_configure里面的 device->bus->owner 是 RT_NULL,在

加上device->bus->owner = device; 给owner设备赋值可以解决问题。

  • 这个位置添加这个逻辑,我觉得合理.
  • 应该加一个判断,避免每个attach device都修改owner
        if(device->bus->owner == RT_NULL)
        {
            device->bus->owner = device;
        }

已修改

}

/* release lock */
rt_mutex_release(&(device->bus->lock));
}
Expand Down Expand Up @@ -451,7 +453,7 @@ rt_err_t rt_spi_sendrecv16(struct rt_spi_device *device,
}

len = rt_spi_transfer(device, &senddata, recvdata, 2);
if(len < 0)
if (len < 0)
{
return (rt_err_t)len;
}
Expand Down Expand Up @@ -578,7 +580,7 @@ rt_err_t rt_spi_take(struct rt_spi_device *device)
message.cs_take = 1;

result = device->bus->ops->xfer(device, &message);
if(result < 0)
if (result < 0)
{
return (rt_err_t)result;
}
Expand All @@ -598,7 +600,7 @@ rt_err_t rt_spi_release(struct rt_spi_device *device)
message.cs_release = 1;

result = device->bus->ops->xfer(device, &message);
if(result < 0)
if (result < 0)
{
return (rt_err_t)result;
}
Expand Down