第二十三讲 Linux SPI驱动实验
一、SPI驱动框架
1.1 裸机下的SPI驱动框架
首先编写SPI控制器驱动,bsp_spi.c和bsp_spi.h。最终提供SPI读写函数:spich0_readwrite_byte
spi具体芯片驱动:ICM20608
1.2、Linux下的SPI驱动框架
主机控制器驱动:SOC的SPI外设驱动,此驱动是半导体原厂编写好的,为spi-imx.c,当spi控制器的设备和驱动匹配以后,spi_imx_probe函数就会执行,完善I.MX6ULL的SPI控制器驱动。
SPI控制器驱动核心就是spi_master的构建,spi_master里面就有如何通过SPI控制器与SPI外设进行通信的函数,此函数是原厂编写的。
spi_master->transfer
spi_master-> transfer_one_message 6ULL主机控制器使用此函数,
设备驱动:具体的SPI芯片驱动
1.3、SPI主机驱动
1、spi_master
struct spi_master {
struct device dev;
struct list_head list;
/* other than negative (== assign one dynamically), bus_num is fully
* board-specific. usually that simplifies to being SOC-specific.
* example: one SOC has three SPI controllers, numbered 0..2,
* and one board's schematics might show it using SPI-2. software
* would normally use bus_num=2 for that controller.
*/
s16 bus_num;
/* chipselects will be integral to many controllers; some others
* might use board-specific GPIOs.
*/
u16 num_chipselect;
/* some SPI controllers pose alignment requirements on DMAable
* buffers; let protocol drivers know about these requirements.
*/
u16 dma_alignment;
/* spi_device.mode flags understood by this controller driver */
u16 mode_bits;
/* bitmask of supported bits_per_word for transfers */
u32 bits_per_word_mask;
#define SPI_BPW_MASK(bits) BIT((bits) - 1)
#define SPI_BIT_MASK(bits) (((bits) == 32) ? ~0U : (BIT(bits) - 1))
#define SPI_BPW_RANGE_MASK(min, max) (SPI_BIT_MASK(max) - SPI_BIT_MASK(min - 1))
/* limits on transfer speed */
u32 min_speed_hz;
u32 max_speed_hz;
/* other constraints relevant to this driver */
u16 flags;
#define SPI_MASTER_HALF_DUPLEX BIT(0) /* can't do full duplex */
#define SPI_MASTER_NO_RX BIT(1) /* can't do buffer read */
#define SPI_MASTER_NO_TX