SPI 設備驅動
【設備】聲明在設備樹中
注意:設備的聲明,slave device node 應該包含在你所要掛載的 &spi node 下,將 device 綁定在 master 上。然后通過 pinctrl 方式指定 GPIO,并在驅動中操作 pinctrl 句柄。
【驅動】demo
Linux 內核使用 spi_driver 結構體來表示 spi 設備驅動,我們在編寫 SPI 設備驅動的時候需要實現 spi_driver。spi_driver 結構體定義在 include/linux/spi/spi.h 文件中。
spi_register_driver:注冊 spi_driver
spi_unregister_driver:銷掉 spi_driver
/* probe 函數 */
static int xxx_probe(struct spi_device *spi)
{
/* 具體函數內容 */
return 0;
}
/* remove 函數 */
static int xxx_remove(struct spi_device *spi)
{
/* 具體函數內容 */
return 0;
}
/* 傳統匹配方式 ID 列表 */
static const struct spi_device_id xxx_id[] = {
{"xxx", 0},
{}
};
/* 設備樹匹配列表 */
static const struct of_device_id xxx_of_match[] = {
{ .compatible = "xxx" },
{ /* Sentinel */ }
};
/* SPI 驅動結構體 */
static struct spi_driver xxx_driver = {
.probe = xxx_probe,
.remove = xxx_remove,
.driver = {
.owner = THIS_MODULE,
.name = "xxx",
.of_match_table = xxx_of_match,
},
.id_table = xxx_id,
};
/* 驅動入口函數 */
static int __init xxx_init(void)
{
return spi_register_driver(&xxx_driver);
}
/* 驅動出口函數 */
static void __exit xxx_exit(void)
{
spi_unregister_driver(&xxx_driver);
}
module_init(xxx_init);
module_exit(xxx_exit);
在驅動入口函數中調用 spi_register_driver 來注冊 spi_driver。
在驅動出口函數中調用 spi_unregister_driver 來注銷 spi_driver。
spi 讀寫數據demo
/* SPI 多字節發送 */
static int spi_send(struct spi_device *spi, u8 *buf, int len)
{
int ret;
struct spi_message m;
struct spi_transfer t = {
.tx_buf = buf,
.len = len,
};
spi_message_init(&m); /* 初始化 spi_message */
spi_message_add_tail(t, &m);/* 將 spi_transfer 添加到 spi_message 隊列 */
ret = spi_sync(spi, &m); /* 同步傳輸 */
return ret;
}
/* SPI 多字節接收 */
static int spi_receive(struct spi_device *spi, u8 *buf, int len)
{
int ret;
struct spi_message m;
struct spi_transfer t = {
.rx_buf = buf,
.len = len,
};
spi_message_init(&m); /* 初始化 spi_message */
spi_message_add_tail(t, &m);/* 將 spi_transfer 添加到 spi_message 隊列 */
ret = spi_sync(spi, &m); /* 同步傳輸 */
return ret;
}
除了 init、exit、probe、remove、read、write 函數外,其他的函數看需求實現,這幾個是最基本的。
-
驅動
+關注
關注
12文章
1882瀏覽量
86385 -
SPI
+關注
關注
17文章
1755瀏覽量
94282 -
系統
+關注
關注
1文章
1026瀏覽量
21667
發布評論請先 登錄
【DragonBoard 410c試用體驗】9.DB410c開發板SPI驅動加載測試與nfc設備(PN532)-spi方式測試(failed)
如何使用RT-Thread SPI設備驅動
嵌入式Linux系統的驅動原理和使用ARM Linux實現SPI驅動程序的說明

SPI檢測是什么,SPI檢測設備的作用又是什么
嵌入式Linux SPI驅動

SPI控制器驅動層功能介紹

Vision Board上的SPI設備驅動配置和SPI主控的外部loopback功能測試

評論