RT-Thread RTOS 1.2.0
An open source embedded real-time operating system
载入中...
搜索中...
未找到
dev_spi.h
浏览该文件的文档.
1/*
2 * Copyright (c) 2006-2024 RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2012-11-23 Bernard Add extern "C"
9 * 2020-06-13 armink fix the 3 wires issue
10 * 2022-09-01 liYony fix api rt_spi_sendrecv16 about MSB and LSB bug
11 */
12
13#ifndef __DEV_SPI_H__
14#define __DEV_SPI_H__
15
16#include <stdlib.h>
17#include <rtthread.h>
18#include <drivers/dev_pin.h>
19#include <drivers/core/driver.h>
20
90
95#ifdef __cplusplus
96extern "C"{
97#endif
98
111#define RT_SPI_CPHA (1<<0)
112#define RT_SPI_CPOL (1<<1)
113
114#define RT_SPI_LSB (0<<2)
115#define RT_SPI_MSB (1<<2)
116
117#define RT_SPI_MASTER (0<<3)
118#define RT_SPI_SLAVE (1<<3)
119
120#define RT_SPI_CS_HIGH (1<<4)
121#define RT_SPI_NO_CS (1<<5)
122#define RT_SPI_3WIRE (1<<6)
123#define RT_SPI_READY (1<<7)
124
125#define RT_SPI_MODE_MASK (RT_SPI_CPHA | RT_SPI_CPOL | RT_SPI_MSB | RT_SPI_SLAVE | RT_SPI_CS_HIGH | RT_SPI_NO_CS | RT_SPI_3WIRE | RT_SPI_READY)
126
127#define RT_SPI_MODE_0 (0 | 0)
128#define RT_SPI_MODE_1 (0 | RT_SPI_CPHA)
129#define RT_SPI_MODE_2 (RT_SPI_CPOL | 0)
130#define RT_SPI_MODE_3 (RT_SPI_CPOL | RT_SPI_CPHA)
131
132#define RT_SPI_BUS_MODE_SPI (1<<0)
133#define RT_SPI_BUS_MODE_QSPI (1<<1)
134
139{
140 const void *send_buf;
141 void *recv_buf;
144
145 unsigned cs_take : 1;
146 unsigned cs_release : 1;
147};
148
153{
156#ifdef RT_USING_DM
157 rt_uint8_t data_width_tx;
158 rt_uint8_t data_width_rx;
159#else
161#endif
162
164};
165
166struct rt_spi_ops;
167
172{
175 const struct rt_spi_ops *ops;
176
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
185};
186
191{
192 rt_err_t (*configure)(struct rt_spi_device *device, struct rt_spi_configuration *configuration);
193 rt_ssize_t (*xfer)(struct rt_spi_device *device, struct rt_spi_message *message);
194};
195
196#ifdef RT_USING_DM
200struct 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
214{
217
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
232};
233
238{
240
241 /* instruction stage */
242 struct
243 {
247
248 /* address and alternate_bytes stage */
249 struct
250 {
255
256 /* dummy_cycles stage */
258
259 /* number of lines in qspi data stage, the other configuration items are in parent */
261};
262
267{
269 /* The size of medium */
271 /* double data rate mode */
273 /* the data lines max width which QSPI bus supported, such as 1, 2, 4 */
275};
276
281{
283
285
286 void (*enter_qspi_mode)(struct rt_qspi_device *device);
287
288 void (*exit_qspi_mode)(struct rt_qspi_device *device);
289};
290
291#define SPI_DEVICE(dev) ((struct rt_spi_device *)(dev))
292
293#ifdef RT_USING_DM
294struct rt_spi_device_id
295{
296 char name[20];
297 void *data;
298};
299
300struct 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
312rt_err_t rt_spi_driver_register(struct rt_spi_driver *driver);
313rt_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
328 const char *name,
329 const struct rt_spi_ops *ops);
330
331
343 const char *name,
344 const char *bus_name,
345 void *user_data);
346
347
360 const char *name,
361 const char *bus_name,
363 void *user_data);
364
380
389
398
407
416
428 struct rt_spi_configuration *cfg);
429
430
443 const void *send_buf,
444 rt_size_t send_length,
445 void *recv_buf,
446 rt_size_t recv_length);
447
460 const void *send_buf1,
461 rt_size_t send_length1,
462 const void *send_buf2,
463 rt_size_t send_length2);
464
476 const void *send_buf,
477 void *recv_buf,
478 rt_size_t length);
479
490 rt_uint8_t senddata,
491 rt_uint8_t *recvdata);
492
503 rt_uint16_t senddata,
504 rt_uint16_t *recvdata);
505
516 struct rt_spi_message *message);
517
527rt_inline rt_size_t rt_spi_recv(struct rt_spi_device *device,
528 void *recv_buf,
530{
531 return rt_spi_transfer(device, RT_NULL, recv_buf, length);
532}
533
543rt_inline rt_size_t rt_spi_send(struct rt_spi_device *device,
544 const void *send_buf,
546{
547 return rt_spi_transfer(device, send_buf, RT_NULL, length);
548}
549
556rt_inline void rt_spi_message_append(struct rt_spi_message *list,
557 struct rt_spi_message *message)
558{
559 RT_ASSERT(list != RT_NULL);
560 if (message == RT_NULL)
561 return; /* not append */
562
563 while (list->next != RT_NULL)
564 {
565 list = list->next;
566 }
567
568 list->next = message;
569 message->next = RT_NULL;
570}
571
581
591rt_err_t rt_qspi_bus_register(struct rt_spi_bus *bus, const char *name, const struct rt_spi_ops *ops);
592
602
614rt_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);
615
626
627#ifdef __cplusplus
628}
629#endif
630
632
633#endif
#define RT_ASSERT(EX)
rt_err_t rt_spi_bus_attach_device_cspin(struct rt_spi_device *device, const char *name, const char *bus_name, rt_base_t cs_pin, void *user_data)
attach a device on SPI bus with CS pin
rt_ssize_t rt_spi_transfer(struct rt_spi_device *device, const void *send_buf, void *recv_buf, rt_size_t length)
This function transmits data to SPI device.
rt_err_t rt_qspi_bus_register(struct rt_spi_bus *bus, const char *name, const struct rt_spi_ops *ops)
This function can register a SPI bus for QSPI mode.
rt_err_t rt_spi_take_bus(struct rt_spi_device *device)
This function takes SPI bus.
rt_err_t rt_spi_bus_attach_device(struct rt_spi_device *device, const char *name, const char *bus_name, void *user_data)
attach a device on SPI bus
rt_size_t rt_qspi_transfer_message(struct rt_qspi_device *device, struct rt_qspi_message *message)
This function transmits data to QSPI device.
rt_inline void rt_spi_message_append(struct rt_spi_message *list, struct rt_spi_message *message)
This function appends a message to the SPI message list.
rt_err_t rt_spi_configure(struct rt_spi_device *device, struct rt_spi_configuration *cfg)
This function can set configuration on SPI device.
rt_err_t rt_qspi_configure(struct rt_qspi_device *device, struct rt_qspi_configuration *cfg)
This function can set configuration on QSPI device.
rt_err_t rt_spi_sendrecv16(struct rt_spi_device *device, rt_uint16_t senddata, rt_uint16_t *recvdata)
The SPI device transmits 16 bytes of data
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)
This function can send data then receive data from QSPI device
rt_err_t rt_spi_send_then_send(struct rt_spi_device *device, const void *send_buf1, rt_size_t send_length1, const void *send_buf2, rt_size_t send_length2)
This function can send data then send data from SPI device.
rt_err_t rt_spi_sendrecv8(struct rt_spi_device *device, rt_uint8_t senddata, rt_uint8_t *recvdata)
The SPI device transmits 8 bytes of data
rt_err_t rt_spi_bus_register(struct rt_spi_bus *bus, const char *name, const struct rt_spi_ops *ops)
register a SPI bus
rt_inline rt_size_t rt_spi_send(struct rt_spi_device *device, const void *send_buf, rt_size_t length)
This function sends data to SPI device.
rt_err_t rt_qspi_send(struct rt_qspi_device *device, const void *send_buf, rt_size_t length)
This function can send data to QSPI device
rt_err_t rt_spi_release_bus(struct rt_spi_device *device)
This function releases SPI bus.
struct rt_spi_message * rt_spi_transfer_message(struct rt_spi_device *device, struct rt_spi_message *message)
This function transfers a message list to the SPI device.
rt_err_t rt_spi_bus_configure(struct rt_spi_device *device)
Reconfigure the SPI bus for the specified device.
rt_err_t rt_spi_release(struct rt_spi_device *device)
This function releases SPI device (releases CS of SPI device).
rt_err_t rt_spi_send_then_recv(struct rt_spi_device *device, const void *send_buf, rt_size_t send_length, void *recv_buf, rt_size_t recv_length)
This function can send data then receive data from SPI device.
rt_err_t rt_spi_take(struct rt_spi_device *device)
This function take SPI device (takes CS of SPI device).
rt_inline rt_size_t rt_spi_recv(struct rt_spi_device *device, void *recv_buf, rt_size_t length)
This function receives data from SPI device.
rt_base_t rt_ssize_t
rt_int32_t rt_base_t
int rt_bool_t
rt_base_t rt_err_t
unsigned char rt_uint8_t
unsigned short rt_uint16_t
rt_ubase_t rt_size_t
unsigned int rt_uint32_t
#define RT_NULL
QSPI configuration structure
struct rt_spi_configuration parent
QSPI operators
struct rt_spi_device parent
void(* exit_qspi_mode)(struct rt_qspi_device *device)
void(* enter_qspi_mode)(struct rt_qspi_device *device)
struct rt_qspi_configuration config
QSPI message structure
struct rt_qspi_message::@255176026317266043011237255203006261277256257132 instruction
rt_uint8_t qspi_data_lines
rt_uint8_t content
struct rt_qspi_message::@003110057025021075065161337153040327357177277157 alternate_bytes
rt_uint8_t qspi_lines
struct rt_spi_message parent
struct rt_qspi_message::@003110057025021075065161337153040327357177277157 address
rt_uint32_t dummy_cycles
SPI bus structure
rt_uint8_t mode
struct rt_spi_device * owner
struct rt_device parent
struct rt_mutex lock
const struct rt_spi_ops * ops
SPI configuration structure
SPI Virtual BUS, one device must connected to a virtual BUS
struct rt_spi_configuration config
struct rt_device parent
struct rt_spi_bus * bus
rt_base_t cs_pin
SPI message structure
unsigned cs_release
const void * send_buf
struct rt_spi_message * next
SPI operators
rt_err_t(* configure)(struct rt_spi_device *device, struct rt_spi_configuration *configuration)
rt_ssize_t(* xfer)(struct rt_spi_device *device, struct rt_spi_message *message)