其他分享
首页 > 其他分享> > 基于硬件 SPI 的数据抽象实例

基于硬件 SPI 的数据抽象实例

作者:互联网

1.写在前面

spi(Serial Peripheral Interface)即串行外设接口。与i2c一样,spi也常用外设设备通信的总线,从事嵌入式开发必不可少的掌握。

根据本人以往的经历,对spi进行总结(主要是MCU范畴,Linux已有成熟的驱动设备),主要目的及实现:

  1. spi总线与spi设备分离;

  2. 快速使用新的硬件spi或者模拟spi;

  3. 方便移植spi总线设备及spi外设程序到不同mcu平台。

2.spi总线抽象

此部分实现源码为:spi_core.c spi_core.h

2.1 spi总线模型对外接口(API)

/*extern function*/
extern int spi_send_then_recv(struct spi_dev_device *spi_dev,const void *send_buff,
							  unsigned short send_size,void *recv_buff,unsigned short recv_size);
extern int spi_send_then_send(struct spi_dev_device *spi_dev,const void *send_buff1,
							  unsigned short send_size1,const void *send_buff2,unsigned short send_size2);
extern int spi_send_recv(struct spi_dev_device *spi_dev,const void *send_buff,void *recv_buff,
						 unsigned short data_size);
extern int spi_send(struct spi_dev_device *spi_dev,const void *send_buff,unsigned short send_size);

2.2 spi总线抽象API实现

以“spi_send_then_recv”函数为例:

另外3个函数,第一个参数都为spi设备指针,其他参数为发送/接收缓冲区,收发数据量等,通过变量名即可看出。

2.3 struct spi_de_device

该结构体为关键,调用API驱动一个外设时,需要先初始化(类似Linux的注册设备驱动)。一个完整的spi外设,包括片选和总线量部分,一个总线可和多个片选组成,驱动多个外设,因此struct spi_dev_device设计原型为:

struct spi_dev_device
{ 
    void (*spi_cs)(unsigned char state); 
    struct spi_bus_device *spi_bus; 
};

2.4 struct spi_bus_device *spi_bus

该结构体为底层硬件相关的spi总线实现,具体由实际需求实现,如用硬件spi还是用模拟spi。struct spi_bus_device*spi_bus原型为:

struct spi_bus_device
{ 
    int (*spi_bus_xfer)(struct spi_dev_device *spi_bus,struct spi_dev_message *msg);
    void *spi_phy;
    unsigned char data_width;
};

其他参数,如数据速率、spi模式等,其实也可以放在此处,只是个人觉得此类参数不常变动,为了节约内存,故不加入此结构体配置中。下面中断分析函数指针

int (*spi_bus_xfer) (struct spi_dev_device *spi_bus,struct spi_dev_message *msg)

2.5 spi_bus_xfer

该函数指针入口参数为spi设备指针(struct spi_dev_device )、spi设备信息帧指针(struct spi_dev_message)。struct spi_dev_device与前面提及的为同一类参数,struct spi_dev_message为收发数据信息帧,其原型如下:

struct spi_dev_message
{
    const void  *send_buf;
    void        *recv_buf;
    int  length;
    unsigned char cs_take    : 1;
    unsigned char cs_release : 1;
};

3. spi总线抽象实现

此部分实现源码为:spi_hw.c spi_hw.h

3.1 spi总线抽象API实现

int spi_send_then_recv(struct spi_dev_device *spi_dev,const void *send_buff,unsigned short send_size,void *recv_buff,unsigned short recv_size)
{
    struct spi_dev_message message;
 
    message.length     = send_size;
    message.send_buf   = send_buff;
    message.recv_buf   = 0;
    message.cs_take    = 1;
    message.cs_release = 0;
    spi_dev->spi_bus->spi_bus_xfer(spi_dev,&message);
    
    message.length     = recv_size;
    message.send_buf   = 0;
    message.recv_buf   = recv_buff;
    message.cs_take    = 0;
    message.cs_release = 1;
    spi_dev->spi_bus->spi_bus_xfer(spi_dev,&message);
     
    return 0;
}
实现的功能是,spi发送完一帧后再接收一帧数据。

1)spi_dev即是传入的设备指针;

2)收发参数主要传递给“spi_dev_message”;

3)对于第一帧“spi_dev_message”,不接收返回值,所以recv_buf设置空(0);此时片选拉低(cs_take=1),发送完还不能拉高片选(cs_release=0),待后面接收帧接收完再拉高片选(cs_release=1),从外设时序图也可以看出;

4)对于第二帧,此时发送数据为空,所以send_buf设置为0,此时的发送动作并非真的发送,只是用来产生接收数据的时钟信号。

int spi_send_recv(struct spi_dev_device *spi_dev,const void *send_buff,void *recv_buff,unsigned short data_size)
{
    message.length   = data_size;
    message.send_buf = send_buff;
    message.recv_buf = recv_buff;
    message.cs_take  = 1;
    message.cs_release = 1;
    spi_dev->spi_bus->spi_bus_xfer(spi_dev,&message);
 
    return 0;
}
实现功能是发送完同时接收完,或者只接收。
1)spi_dev即是传入的设备指针;
2)收发数据及长度由用户通过形参传入,只接收时,发送数据缓存可设置为空(0);
3)操作前拉低片选(cs_take=1),操作完成片选拉高(cs_release=1);
int spi_send(struct spi_dev_device *spi_dev,const void *send_buff,unsigned short send_size)
{
    struct spi_dev_message message;
 
    message.length    = send_size;
    message.send_buf  = send_buff;
    message.recv_buf  = 0;
    message.cs_take   = 1;
    message.cs_release = 1;
    spi_dev->spi_bus->spi_bus_xfer(spi_dev,&message);
    
    return 0;
}

该函数与spi_send_recv非常类似,但只有“发送”动作,无“接收”动作,故recv_buf设置为空(0)。

3.2 spi总线抽象底层实现(以stm32为例)

主要实现“struct spi_bus_device”中的“spi_bus_xfer”函数,此部分相当于平常裸机代码。以8bit模式为例,代码如下,详细代码看附件“spi_hw.c”。

static int stm32_spi_bus_xfer(struct spi_dev_device *spi_dev,struct spi_dev_message *msg)
{
   int size;
   SPI_TypeDef *SPI_NO;
   
   SPI_NO = (SPI_TypeDef *)spi_dev->spi_bus->spi_phy;
   size = msg->length;
   if(msg->cs_take)
   {/* take CS */
		spi_dev->spi_cs(0);
   }
   if(spi_dev->spi_bus->data_width <=8)
   {
       const unsigned short * send_ptr = msg->send_buf;
       unsigned short *recv_ptr = msg->recv_buf;
     
       while(size--)
       {
       	   unsigned short data = 0xFF;
	    
	       if(send_ptr != 0)
	       {
	           data = *send_ptr++;
	       }
	       while (SPI_I2S_GetFlagStatus(SPI_NO, SPI_I2S_FLAG_TXE) == RESET); 
	       SPI_I2S_SendData(SPI_NO, data);
	       while (SPI_I2S_GetFlagStatus(SPI_NO, SPI_I2S_FLAG_RXNE) == RESET); 
	       data = SPI_I2S_ReceiveData(SPI_NO); 
	    
	       if(recv_ptr != 0)
	       {
	           *recv_ptr++ = data;
	       }
	   }
   }
   if(msg->cs_release)
   {/* release CS */ 
       spi_dev->spi_cs(1);
   }
   return msg->length;
}

主要功能:

3.3 最后,执行相关初始化,如IO口、时钟、spi相关配置。

void stm32f1xx_spi_init(struct spi_bus_device *spi0,unsigned char byte_size0,struct spi_bus_device *spi1,unsigned char byte_size1)
{
    SPI_InitTypeDef  SPI_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure; 
 
    if(spi0)
    {//SPI1
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
    SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
    if(byte_size0 <= 8)
        SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;  
    else
        SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b; 
        
    SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;          
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;     
    SPI_InitStructure.SPI_CRCPolynomial = 7;
    SPI_Init(SPI1, &SPI_InitStructure);
    SPI_Cmd(SPI1, ENABLE); 
    //spi io
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO,ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    spi0->data_width = byte_size0;
    spi0->spi_bus_xfer = stm32_spi_bus_xfer;
    spi0->spi_phy = SPI1;
    }
}

注意问题点:

3.4 小结

至此,一个stm32硬件spi总线实现完毕,剩下的就是利用这个总线驱动一个spi外设。也可以通过io口模拟spi,后面再写一篇使用模拟spi的文章,主要改动也在此处,总线程序或者下面的外设程序都无需修改。

4. 使用spi抽象(以25aa256 EEPROM为例)

此部分实现源码为:25xx.c 25xx.h

4.1 初始化(注册设备)

采用stm32 SPI2驱动25aa256,步骤如下:

struct  spi_dev_device ee_25xx_spi_dev;
struct  spi_bus_device  spi_bus1;
static void spi1_cs(unsigned char state)
{
    if (state)
 		GPIO_SetBits(GPIOB, GPIO_Pin_12);
    else
 		GPIO_ResetBits(GPIOB, GPIO_Pin_12);
}

初始化25aa256.

void ee_25xx_init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
  
    /* SPI2 cs */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;          
    GPIO_Init(GPIOB, &GPIO_InitStructure); 
    GPIO_SetBits(GPIOB, GPIO_Pin_12);
      
    /* device init */
    st32f1xx_spi_init(0,&spi_bus1);
    ee_25xx_spi_dev.spi_cs  = spi1_cs;
    ee_25xx_spi_dev.spi_bus = &spi_bus1;   
}

“ee_25xx_spi_dev”即是我们“注册”的设备,下面可通过上面描述API操作25aa256,传入参数为“ee_25xx_spi_dev”地址(指针)。

4.2 操作(读写)25aa256

void ee_25xx_write_enable(uint8_t select)
{
    spi_send(&ee_25xx_spi_dev,&select,1);
}

该操作用到“spi_send”接口,无返回值,简单明了!

void ee_25xx_write_byte(uint16_t write_addr,uint8_t write_data)
{
    uint8_t send_buff[3];
 
    ee_25xx_write_enable(REG_WRITE_ENABLE);
    send_buff[0] = REG_WRITE_COMMAND;
    send_buff[1] = (write_addr>>8)&0xff;
    send_buff[2] = write_addr&0xff;
    spi_send_then_send(&ee_25xx_spi_dev,send_buff,3,&write_data,1);
    ee_25xx_write_enable(REG_WRITE_DISABLE);
}
该操作用到“spi_send_then_send”接口,从函数名称也可以很好地理解。基本步骤为:
  • 使能25aa256;
  • 发送缓存填充,此部分包括写命令、写地址;
  • 写数据填充,单个字节直接调用形参,无额外申请内存;
  • 调用“spi_send_then_send”,完成写操作。
void ee_25xx_read_bytes(uint16_t read_addr,uint8_t *read_buff,uint16_t read_bytes)
{
    uint8_t send_buff[3];
 
    send_buff[0] = REG_READ_COMMAND;
    send_buff[1] = (read_addr>>8)&0xff;
    send_buff[2] = read_addr&0xff;
    spi_send_then_recv(&ee_25xx_spi_dev,send_buff,3,read_buff,read_bytes);
}
该操作用到“spi_send_then_recv”接口。基本步骤为:
  • 发送缓存填充,此部分包括写命令、写地址;
  • 形参地址传递作为接收地址;
  • 调用“spi_send_then_send”,完成读操作。

4.3 25aa256驱动小结

至此,完成25aa256的驱动程序,所有操作通过上述4个API接口,移植到新的mcu平台时,该器件驱动程序几乎无须修改,只需修改spi底层相关的函数。驱动其他spi外设,与25aa256的流程步骤一致。

其实,通过此问题也可发现,驱动一个设备是相对简单,更多的难点是在应用,比如25aa256的页写算法。因此,把底层“轮子”造好后,不需再重复造轮子,把更多的时间花在研究应用上面。

5. 总结

本文主要描述mcu下spi总线的抽象分层,主要实现手段是充分利用结构体和函数指针。

struct  spi_dev_device adc_spi_dev;
static void spi1_cs1(unsigned char state)
{
    if (state)
        GPIO_SetBits(GPIOB, GPIO_Pin_11);
    else
 		GPIO_ResetBits(GPIOB, GPIO_Pin_11);
}
 
void adc_init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
  
    /* SPI cs */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;          
    GPIO_Init(GPIOB, &GPIO_InitStructure); 
    GPIO_SetBits(GPIOB, GPIO_Pin_11);
      
    /* device init */
    //st32f1xx_spi_init(0,&spi_bus1); /* 共用spi1,已经初始化过,无需重复初始化 */
    adc_spi_dev.spi_cs = spi1_cs1;    /* 片选函数必须独立 */
    adc_spi_dev.spi_bus = &spi_bus1;  /* 指向spi1 */  
}

6. 源码

[1] https://github.com/Prry/drivers-for-mcu

7. 参考

[1] https://github.com/RT-Thread/rt-thread

标签:struct,spi,dev,数据抽象,SPI,实例,send,GPIO,recv
来源: https://blog.csdn.net/helaisun/article/details/120729778