Skip to content

Commit 8a7ef41

Browse files
authored
[SPI]Update and fixup the SPI
* Make a priv data read API for probed SPI device * Fixup the SPI device pre-alloc
1 parent 29033ab commit 8a7ef41

File tree

5 files changed

+54
-27
lines changed

5 files changed

+54
-27
lines changed

components/drivers/include/drivers/dev_spi.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,11 @@ rt_err_t rt_spi_driver_register(struct rt_spi_driver *driver);
313313
rt_err_t rt_spi_device_register(struct rt_spi_device *device);
314314

315315
#define RT_SPI_DRIVER_EXPORT(driver) RT_DRIVER_EXPORT(driver, spi, BUILIN)
316+
317+
rt_inline const void *rt_spi_device_id_data(struct rt_spi_device *device)
318+
{
319+
return device->id ? device->id->data : (device->ofw_id ? device->ofw_id->data : RT_NULL);
320+
}
316321
#endif /* RT_USING_DM */
317322

318323
/**
@@ -598,7 +603,7 @@ rt_err_t rt_qspi_bus_register(struct rt_spi_bus *bus, const char *name, const st
598603
*
599604
* @return the actual length of transmitted.
600605
*/
601-
rt_size_t rt_qspi_transfer_message(struct rt_qspi_device *device, struct rt_qspi_message *message);
606+
rt_ssize_t rt_qspi_transfer_message(struct rt_qspi_device *device, struct rt_qspi_message *message);
602607

603608
/**
604609
* @brief This function can send data then receive data from QSPI device
@@ -611,7 +616,7 @@ rt_size_t rt_qspi_transfer_message(struct rt_qspi_device *device, struct rt_qsp
611616
*
612617
* @return the status of transmit.
613618
*/
614-
rt_err_t rt_qspi_send_then_recv(struct rt_qspi_device *device, const void *send_buf, rt_size_t send_length,void *recv_buf, rt_size_t recv_length);
619+
rt_ssize_t rt_qspi_send_then_recv(struct rt_qspi_device *device, const void *send_buf, rt_size_t send_length,void *recv_buf, rt_size_t recv_length);
615620

616621
/**
617622
* @brief This function can send data to QSPI device
@@ -622,7 +627,7 @@ rt_err_t rt_qspi_send_then_recv(struct rt_qspi_device *device, const void *send_
622627
*
623628
* @return the status of transmit.
624629
*/
625-
rt_err_t rt_qspi_send(struct rt_qspi_device *device, const void *send_buf, rt_size_t length);
630+
rt_ssize_t rt_qspi_send(struct rt_qspi_device *device, const void *send_buf, rt_size_t length);
626631

627632
#ifdef __cplusplus
628633
}

components/drivers/spi/Kconfig

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
config RT_USING_SPI
1+
menuconfig RT_USING_SPI
22
bool "Using SPI Bus/Device device drivers"
33
default n
44

@@ -235,3 +235,7 @@ config RT_USING_SPI
235235
select RT_USING_LWIP
236236
default n
237237
endif
238+
239+
if RT_USING_DM && RT_USING_SPI
240+
osource "$(SOC_DM_SPI_DIR)/Kconfig"
241+
endif

components/drivers/spi/dev_qspi_core.c

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010

1111
#include "drivers/dev_spi.h"
1212

13+
extern rt_err_t spi_bus_register(struct rt_spi_bus *bus,
14+
const char *name,
15+
const struct rt_spi_ops *ops);
16+
1317
rt_err_t rt_qspi_configure(struct rt_qspi_device *device, struct rt_qspi_configuration *cfg)
1418
{
1519
RT_ASSERT(device != RT_NULL);
@@ -67,21 +71,15 @@ rt_err_t rt_qspi_configure(struct rt_qspi_device *device, struct rt_qspi_configu
6771

6872
rt_err_t rt_qspi_bus_register(struct rt_spi_bus *bus, const char *name, const struct rt_spi_ops *ops)
6973
{
70-
rt_err_t result = RT_EOK;
71-
72-
result = rt_spi_bus_register(bus, name, ops);
73-
if(result == RT_EOK)
74-
{
75-
/* set SPI bus to qspi modes */
76-
bus->mode = RT_SPI_BUS_MODE_QSPI;
77-
}
74+
/* set SPI bus to qspi modes */
75+
bus->mode = RT_SPI_BUS_MODE_QSPI;
7876

79-
return result;
77+
return spi_bus_register(bus, name, ops);
8078
}
8179

82-
rt_size_t rt_qspi_transfer_message(struct rt_qspi_device *device, struct rt_qspi_message *message)
80+
rt_ssize_t rt_qspi_transfer_message(struct rt_qspi_device *device, struct rt_qspi_message *message)
8381
{
84-
rt_err_t result;
82+
rt_ssize_t result;
8583

8684
RT_ASSERT(device != RT_NULL);
8785
RT_ASSERT(message != RT_NULL);
@@ -130,7 +128,7 @@ rt_size_t rt_qspi_transfer_message(struct rt_qspi_device *device, struct rt_qsp
130128
return result;
131129
}
132130

133-
rt_err_t rt_qspi_send_then_recv(struct rt_qspi_device *device, const void *send_buf, rt_size_t send_length, void *recv_buf, rt_size_t recv_length)
131+
rt_ssize_t rt_qspi_send_then_recv(struct rt_qspi_device *device, const void *send_buf, rt_size_t send_length, void *recv_buf, rt_size_t recv_length)
134132
{
135133
RT_ASSERT(send_buf);
136134
RT_ASSERT(recv_buf);
@@ -139,7 +137,7 @@ rt_err_t rt_qspi_send_then_recv(struct rt_qspi_device *device, const void *send_
139137
struct rt_qspi_message message;
140138
unsigned char *ptr = (unsigned char *)send_buf;
141139
rt_size_t count = 0;
142-
rt_err_t result = 0;
140+
rt_ssize_t result = 0;
143141

144142
message.instruction.content = ptr[0];
145143
message.instruction.qspi_lines = 1;
@@ -208,23 +206,23 @@ rt_err_t rt_qspi_send_then_recv(struct rt_qspi_device *device, const void *send_
208206
{
209207
result = -RT_EIO;
210208
}
211-
else
209+
else if (result > 0)
212210
{
213211
result = recv_length;
214212
}
215213

216214
return result;
217215
}
218216

219-
rt_err_t rt_qspi_send(struct rt_qspi_device *device, const void *send_buf, rt_size_t length)
217+
rt_ssize_t rt_qspi_send(struct rt_qspi_device *device, const void *send_buf, rt_size_t length)
220218
{
221219
RT_ASSERT(send_buf);
222220
RT_ASSERT(length != 0);
223221

224222
struct rt_qspi_message message;
225223
unsigned char *ptr = (unsigned char *)send_buf;
226224
rt_size_t count = 0;
227-
rt_err_t result = 0;
225+
rt_ssize_t result = 0;
228226

229227
message.instruction.content = ptr[0];
230228
message.instruction.qspi_lines = 1;
@@ -294,7 +292,7 @@ rt_err_t rt_qspi_send(struct rt_qspi_device *device, const void *send_buf, rt_si
294292
{
295293
result = -RT_EIO;
296294
}
297-
else
295+
else if (result > 0)
298296
{
299297
result = length;
300298
}

components/drivers/spi/dev_spi_bus.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,19 @@ void spi_bus_scan_devices(struct rt_spi_bus *bus)
3434
continue;
3535
}
3636

37-
spi_dev = rt_calloc(1, sizeof(*spi_dev));
37+
if ((bus->mode & RT_SPI_BUS_MODE_SPI) == RT_SPI_BUS_MODE_SPI)
38+
{
39+
spi_dev = rt_calloc(1, sizeof(struct rt_spi_device));
40+
}
41+
else if ((bus->mode & RT_SPI_BUS_MODE_QSPI) == RT_SPI_BUS_MODE_QSPI)
42+
{
43+
spi_dev = rt_calloc(1, sizeof(struct rt_qspi_device));
44+
}
45+
else
46+
{
47+
LOG_E("Unknown bus mode = %x", bus->mode);
48+
RT_ASSERT(0);
49+
}
3850

3951
if (!spi_dev)
4052
{

components/drivers/spi/dev_spi_core.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
extern rt_err_t rt_spi_bus_device_init(struct rt_spi_bus *bus, const char *name);
2727
extern rt_err_t rt_spidev_device_init(struct rt_spi_device *dev, const char *name);
2828

29-
rt_err_t rt_spi_bus_register(struct rt_spi_bus *bus,
30-
const char *name,
31-
const struct rt_spi_ops *ops)
29+
rt_err_t spi_bus_register(struct rt_spi_bus *bus,
30+
const char *name,
31+
const struct rt_spi_ops *ops)
3232
{
3333
rt_err_t result;
3434

@@ -42,8 +42,6 @@ rt_err_t rt_spi_bus_register(struct rt_spi_bus *bus,
4242
bus->ops = ops;
4343
/* initialize owner */
4444
bus->owner = RT_NULL;
45-
/* set bus mode */
46-
bus->mode = RT_SPI_BUS_MODE_SPI;
4745

4846
#ifdef RT_USING_DM
4947
if (!bus->slave)
@@ -77,6 +75,16 @@ rt_err_t rt_spi_bus_register(struct rt_spi_bus *bus,
7775
return RT_EOK;
7876
}
7977

78+
rt_err_t rt_spi_bus_register(struct rt_spi_bus *bus,
79+
const char *name,
80+
const struct rt_spi_ops *ops)
81+
{
82+
/* set bus mode */
83+
bus->mode = RT_SPI_BUS_MODE_SPI;
84+
85+
return spi_bus_register(bus, name, ops);
86+
}
87+
8088
rt_err_t rt_spi_bus_attach_device_cspin(struct rt_spi_device *device,
8189
const char *name,
8290
const char *bus_name,

0 commit comments

Comments
 (0)