SPI 通用接口層
- SPI 通用接口層把具體的 SPI 設備的協(xié)議驅動和 SPI 控制器驅動連接在一起。
- 負責 SPI 系統(tǒng)與 Linux 設備模型相關的初始化工作。
- 為協(xié)議驅動和控制器驅動提供一系列的標準接口 API 及其數(shù)據(jù)結構。
- SPI 設備、SPI 協(xié)議驅動、SPI 控制器的數(shù)據(jù)抽象
- 協(xié)助數(shù)據(jù)傳輸而定義的數(shù)據(jù)結構
kernel-4.14/drivers/spi/spi.c
static int __init spi_init(void)
{
int status;
buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
if (!buf) {
status = -ENOMEM;
goto err0;
}
// 創(chuàng)建 /sys/bus/spi 節(jié)點
status = bus_register(&spi_bus_type);
if (status < 0)
goto err1;
//創(chuàng)建 /sys/class/spi_master 節(jié)點
status = class_register(&spi_master_class);
if (status < 0)
goto err2;
if (IS_ENABLED(CONFIG_SPI_SLAVE)) {
status = class_register(&spi_slave_class);
if (status < 0)
goto err3;
}
......
}
在這里創(chuàng)建了 SPI 總線,創(chuàng)建 /sys/bus/spi 節(jié)點和 /sys/class/spi_master 節(jié)點。
重要數(shù)據(jù)結構:
spi_device
spi_driver
spi_board_info
spi_controller/spi_master
spi_transfer
spi_message
重要 API
spi_message_init
spi_message_add_tail
spi_sync
spi_async
spi_write
spi_read
接下來詳細解析結構體和API,只講解重點部分,完整解析請參考官方文檔
https://www.kernel.org/doc/html/v4.14//driver-api/spi.html
只有熟悉每個結構體存儲的是什么東西,才能真正搞懂 SPI 模塊。
spi_master/spi_controller:描述一個 spi 主機設備
struct spi_master {
//Linux 驅動模型中的設備
struct device dev;
//此 spi_master 設備在全局 spi_master 鏈表中的節(jié)點
struct list_head list;
//此 spi_master 編號
s16 bus_num;
//此 spi_master 支持的片選信號數(shù)量
u16 num_chipselect;
//dma 地址對齊
u16 dma_alignment;
//此 spi_master 支持傳輸?shù)?mode
u16 mode_bits;
u32 bits_per_word_mask;
/* limits on transfer speed */
u32 min_speed_hz;
u32 max_speed_hz;
/* other constraints relevant to this driver */
u16 flags;
/* lock and mutex for SPI bus locking */
spinlock_t bus_lock_spinlock;//總線自旋鎖
struct mutex bus_lock_mutex;//總線互斥鎖
//總線是否處于 lock 狀態(tài)
bool bus_lock_flag;
//準備傳輸,設置傳輸?shù)膮?shù)
int (*setup)(struct spi_device *spi);
//傳輸數(shù)據(jù)
int (*transfer)(struct spi_device *spi,
struct spi_message *mesg);
// 設備 release 時的清除工作
void (*cleanup)(struct spi_device *spi);
bool (*can_dma)(struct spi_master *master,
struct spi_device *spi,
struct spi_transfer *xfer);
bool queued;//是否采用系統(tǒng)的序列化傳輸
struct kthread_worker kworker;//序列化傳輸時的線程 worker
struct task_struct *kworker_task;//序列化傳輸?shù)木€程
struct kthread_work pump_messages;//序列化傳輸時的處理函數(shù)
spinlock_t queue_lock;//序列化傳輸時的queue_lock
struct list_head queue;//序列化傳輸時的 msg 隊列頭
struct spi_message *cur_msg;//序列化傳輸時當前的 msg
bool idling;
bool busy;//序列化傳輸時線程是否處于busy狀態(tài)
bool running;//序列化傳輸時線程是否在運行
bool rt;//是否實時傳輸
......
int (*prepare_transfer_hardware)(struct spi_master *master);
//一個 msg 的傳輸實現(xiàn)
int (*transfer_one_message)(struct spi_master *master,
struct spi_message *mesg);
......
/* gpio chip select */
int *cs_gpios;
......
};
spi_device:描述一個 spi 從機設備
struct spi_device {
//Linux驅動模型中的設備
struct device dev;
struct spi_master *master;//設備所連接的 spi 主機設備
u32 max_speed_hz;//該設備最大傳輸速率
u8 chip_select;//CS片選信號編號
u8 bits_per_word;//每次傳輸長度
u16 mode;//傳輸模式
......
int irq;//軟件中斷號
void *controller_state;//控制器狀態(tài)
void *controller_data;//控制參數(shù)
char modalias[SPI_NAME_SIZE];//設備名稱
//CS 片選信號對應的 GPIO number
int cs_gpio; /* chip select gpio */
/* the statistics */
struct spi_statistics statistics;
};
spi_driver:描述一個 spi 設備驅動
struct spi_driver {
//此driver所支持的 spi 設備 list
const struct spi_device_id *id_table;
int (*probe)(struct spi_device *spi);
int (*remove)(struct spi_device *spi);
//系統(tǒng) shutdown 時的回調函數(shù)
void (*shutdown)(struct spi_device *spi);
struct device_driver driver;
};
spi_board_info:描述一個 spi 從機設備板級信息,無設備樹時使用
struct spi_board_info {
//設備名稱
char modalias[SPI_NAME_SIZE];
const void *platform_data;//設備的平臺數(shù)據(jù)
void *controller_data;//設備的控制器數(shù)據(jù)
int irq;//設備的中斷號
u32 max_speed_hz;//設備支持的最大速率
u16 bus_num;//設備連接的 spi 總線編號
u16 chip_select;//設備連接的 CS 信號編號
u16 mode;//設備使用的傳輸 mode
};
spi_transfer:描述 spi 傳輸?shù)木唧w數(shù)據(jù)
struct spi_transfer {
const void *tx_buf;//spi_transfer 的發(fā)送 buf
void *rx_buf;//spi_transfer 的接收 buf
unsigned len;//spi_transfer 發(fā)送和接收的長度
dma_addr_t tx_dma;//tx_buf 對應的 dma 地址
dma_addr_t rx_dma;//rx_buf 對應的 dma 地址
struct sg_table tx_sg;
struct sg_table rx_sg;
//spi_transfer傳輸完成后是否要改變 CS 片選信號
unsigned cs_change:1;
unsigned tx_nbits:3;
unsigned rx_nbits:3;
......
u8 bits_per_word;//spi_transfer 中一個 word 占的bits
u16 delay_usecs;//兩個 spi_transfer 直接的等待延遲
u32 speed_hz;//spi_transfer 的傳輸速率
struct list_head transfer_list;//spi_transfer掛載到的 message 節(jié)點
};
spi_message:描述一次 spi 傳輸?shù)男畔?/p>
struct spi_message {
//掛載在此 msg 上的 transfer 鏈表頭
struct list_head transfers;
//此 msg 需要通信的 spi 從機設備
struct spi_device *spi;
//所使用的地址是否是 dma 地址
unsigned is_dma_mapped:1;
//msg 發(fā)送完成后的處理函數(shù)
void (*complete)(void *context);
void *context;//complete函數(shù)的參數(shù)
unsigned frame_length;
unsigned actual_length;//此 msg 實際成功發(fā)送的字節(jié)數(shù)
int status;//此 msg 的發(fā)送狀態(tài),0:成功,負數(shù),失敗
struct list_head queue;//此 msg 在所有 msg 中的鏈表節(jié)點
void *state;//此 msg 的私有數(shù)據(jù)
};
-
SPI
+關注
關注
17文章
1749瀏覽量
94168 -
接口層
+關注
關注
0文章
2瀏覽量
1032
發(fā)布評論請先 登錄
相關推薦
SPI接口硬件設計介紹

SPI協(xié)議層及固件庫
基于PC/104 總線與CPLD 的SPI 接口設計
SPI接口的工作原理

SPI接口總線介紹

一種通用SPI接口的FPGA設計與實現(xiàn)

《STM32從零開始學習歷程》——SPI協(xié)議層

SPI接口的相關介紹

評論