[Add] 新增SPI驱动 开发说明

This commit is contained in:
gaoyang3513
2024-07-01 21:22:26 +08:00
parent 3f3b436a9f
commit bb99d8110b

218
spi/ReadMe.md Normal file
View File

@ -0,0 +1,218 @@
## 参考
- linux_5.10/Documentation/spi/spi-summary.rst
## 概念
### 工作原理
SPI 请求将提交至IO对列。这些请求按照“先进先出(FIFO)”原则执行,在完成后将通过回调方式告知。
当然,也有一些简单的异步封装回调方法,包含发送命令并接收应答的一类接口。
### 两种SPI驱动
#### Controller drivers ...
此类驱动的对象为SOC片上的SPI控制器这些控制器通常支持“Master”与“Slave”角色切换。
此类驱动常直接访问硬件寄存器并使用DMA来提高数据传递效率或许也可以通过CPU控制数据的每一位来控制可编程GPIO管脚。
#### Protocol drivers ...
此类驱动的对象为SOC片外接的SPI设备这些设备以“Master”或“Slave”角色接入SPI控制器,进而完成数据的传输。
在SPI驱动之上可能被具象化为MTD、Audio等设备接入更上一级的子系统。
### 数据结构
`struct spi_device` 结构体是两种驱动的基类。
```c
#---- linux_5.10/include/linux/spi/spi.h
struct spi_device {
struct device dev;
struct spi_controller *controller;
struct spi_controller *master; /* compatibility layer */
...
```
### 框架
input layer, ALSA, networking, MTD, the character device framework,
or other Linux subsystems.
### Messages
If you like, spi_message_alloc() and spi_message_free() convenience
### 内存管理
SPI设备必须管理以下两种类型内存
- I/O 数据;
- spi_message\spi_transfer数据
可以使用spi_message_alloc()、spi_message_free()代为管理。
## 示例
### 静态定义
#### 板级设备
##### Master
"struct platform_device"
路径:`arch/.../mach-*/board-*.c`:
```c
#include <mach/spi.h> /* for mysoc_spi_data */
/* if your mach-* infrastructure doesn't support kernels that can
* run on multiple boards, pdata wouldn't benefit from "__init".
*/
static struct mysoc_spi_data pdata __initdata = { ... };
static __init board_init(void)
{
...
/* this board only uses SPI controller #2 */
mysoc_register_spi(2, &pdata);
...
}
```
```c
#include <mach/spi.h>
static struct platform_device spi2 = { ... };
void mysoc_register_spi(unsigned n, struct mysoc_spi_data *pdata)
{
struct mysoc_spi_data *pdata2;
pdata2 = kmalloc(sizeof *pdata2, GFP_KERNEL);
*pdata2 = pdata;
...
if (n == 2) {
spi2->dev.platform_data = pdata2;
register_platform_device(&spi2);
/* also: set up pin modes so the spi2 signals are
* visible on the relevant pins ... bootloaders on
* production boards may already have done this, but
* developer boards will often need Linux to do it.
*/
}
...
}
```
##### Slave
文件:`arch/.../mach-*/board-*.c`
```c
static struct ads7846_platform_data ads_info = {
.vref_delay_usecs = 100,
.x_plate_ohms = 580,
.y_plate_ohms = 410,
};
static struct spi_board_info spi_board_info[] __initdata = {
{
.modalias = "ads7846",
.platform_data = &ads_info,
.mode = SPI_MODE_0,
.irq = GPIO_IRQ(31),
.max_speed_hz = 120000 /* max sample rate at 3V */ * 16,
.bus_num = 1,
.chip_select = 0,
},
};
```
```c
spi_register_board_info(spi_board_info, ARRAY_SIZE(spi_board_info));
```
### 非静态定义
使用`spi_busnum_to_master()`查找目标SPI控制器
使用`spi_new_device()`创建;
使用`spi_unregister_device()`销毁;
### SPI Controller Driver
```c
struct spi_master *master;
struct CONTROLLER *c;
master = spi_alloc_master(dev, sizeof *c);
if (!master)
return -ENODEV;
c = spi_master_get_devdata(master);
```
- `spi_[un]register_master()`注册至系统;
#### 接口
- `master->setup(struct spi_device *spi)`, clock rate, SPI mode, and word sizes.
- `master->cleanup(struct spi_device *spi)`, 清除之前由`spi_device.controller_state`锁定的状态;
...
- `master->transfer_one_message(struct spi_master *master, struct spi_message *mesg)`, 发送一个mesg
### SPI Protocol Driver
```c
static struct spi_driver CHIP_driver = {
.driver = {
.name = "CHIP",
.owner = THIS_MODULE,
.pm = &CHIP_pm_ops,
},
.probe = CHIP_probe,
.remove = CHIP_remove,
};
static int CHIP_probe(struct spi_device *spi)
{
struct CHIP *chip;
struct CHIP_platform_data *pdata;
/* assuming the driver requires board-specific data: */
pdata = &spi->dev.platform_data;
if (!pdata)
return -ENODEV;
/* get memory for driver's per-chip state */
chip = kzalloc(sizeof *chip, GFP_KERNEL);
if (!chip)
return -ENOMEM;
spi_set_drvdata(spi, chip);
... etc
return 0;
}
```
spi_setup(),
## 属性
- `spi_message.is_dma_mapped`
## 调试
#### sys节点
- `/sys/bus/spi/devices/spiB.C` 总线B设备C, 都是从0开始编号
- `/sys/class/spi_master/spiB` spi_master类总线B
- `/sys/class/spi_slave/spiB`
## 备注
- `master->transfer(struct spi_device *spi, struct spi_message *message)`,
1. 已经被弃用;
2. 可以打断IO对列提前发送