Things embedded principle of the SPI interface and configuration

  This experiment chip W25Q64

  W25Q64 Winbond company launched a large-capacity SPI FLASH products with a capacity of 64Mb. The 25Q series of devices in terms of performance and flexibility far more than ordinary serial flash devices. The W25Q64 8M bytes capacity is divided into 128 blocks, each block size is 64K bytes, each block is divided into 16 sectors, each sector 4K bytes. W25Q64 minimum unit of erase of a sector must be erased is 4K bytes. So, it needs to W25Q64 open up a buffer zone of at least 4K, so must require more than 4K SRAM chip in order to have a good operation.

  W25Q64 of up to 10W erase cycle times, data can be stored up to 20 years, 2.7 ~ 3.6V voltage support, support for standard SPI, SPI also supports dual output / quad output, the maximum SPI clock up to 80Mhz.

  One. SPI interface schematic

  (I. Overview

  

55.SPI interface schematic and Configuration

 

  High-speed, full-duplex, synchronous communication bus.

  Full-duplex: can send and receive at the same time, it requires two pins

  Synchronization: Need clock pin

  Chip Select: SPI can easily hang a plurality of interface devices.

  A total of four pins.

  (Ii) SPI internal configuration of simplicity FIG.

  

55.SPI interface schematic and Configuration

 

  MISO: as the host when the input, output do from the time machine

  MOSI: master do when output, so input from the time machine

  Master and slave has a shift register, a clock under control of the same maximum displacement of the host machine to the most significant bit, while the most significant bit from a machine moved forward, moved to the lowest position of the host. Under the control of a clock master and a slave are swapped bits, then under the control of eight clocks to exchange 8, the end result is the data shift register two complete exchange.

  8 under the control of the clock, two bytes from the host and exchange machine, a byte that is sent to the host while 8 bit slave, the slave is also passed back to the host 8 bits, that is a byte.

  (C) SPI Interface Block Diagram

  

55.SPI interface schematic and Configuration

 

  The left upper part is how to transfer data under control of the clock, the right is the control unit, further comprising a left lower baud rate generator.

  (Iv) SPI works summary

  

55.SPI interface schematic and Configuration

 

  Wherein (e) SPI of

  

55.SPI interface schematic and Configuration

 

  (Vi) selected from (NSS) foot management

  

55.SPI interface schematic and Configuration

 

  两个SPI通信首先有2个数据线,一个时钟线,还有一个片选线,只有把片选拉低,SPI芯片才工作,片选引脚可以是SPI规定的片选引脚,还可以通过软件的方式选择任意一个IO口作为片选引脚,这样做的好处是:比如一个SPI接口上挂多个设备,比如挂了4个设备,第二个用PA2,第三个用PA3,第四个用PA4作为片选,我们 跟第二个设备进行通信的时候,只需要把第二个片选选中,比如拉低,其他设备的片选都拉高,这样就实现了一个SPI接口可以连接个SPI设备,战舰开发板上就是通过这种方法来实现的。

  (七)时钟信号的相位和极性

  

55.SPI interface schematic and Configuration

 

  时钟信号的相位和极性是通过CR寄存器的 CPOL 和 CPHA两个位确定的。

  CPOL:时钟极性,设置在没有数据传输时时钟的空闲状态电平。CPOL置0,SCK引脚在空闲时为低电平,CPOL置1,SCK引脚在空闲时保持高电平。

  CPHA:时钟相位 设置时钟信号在第几个边沿数据被采集

  CPHA=1时:在时钟信号的第二个边沿

  

55.SPI interface schematic and Configuration

 

  CPOL=1,CPHA=1, CPOL=1表示时钟信号在没有数据传输时即空闲时的状态为高电平。如果CPHA=1,那么数据就在时钟信号的第二个边沿即上升沿的时候被采集。

  CPOL= 0,CPHA=1, CPOL=0表示时钟信号在没有数据传输时即空闲时的状态为低电平。 如果CPHA=1,那么数据就在时钟信号的第二个边沿即下降沿的时候被采集。

  CPHA=0时:在时钟信号的第一个边沿

  

55.SPI interface schematic and Configuration

 

  CPOL=1,CPHA=0, CPOL=1表示时钟信号在没有数据传输时即空闲时的状态为高电平。如果CPHA=1,那么数据就在时钟信号的第一个边沿即下降沿的时候被采集。

  CPOL= 0,CPHA=0, CPOL=0表示时钟信号在没有数据传输时即空闲时的状态为低电平。 如果CPHA=1,那么数据就在时钟信号的第一个边沿即上升沿的时候被采集。

  为什么要配置这两个参数?

  因为SPI外设的从机的时钟相位和极性都是有严格要求的。所以我们要根据选择的外设的时钟相位和极性来配置主机的相位和极性。必须要与从机匹配。

  (八)数据帧的格式和状态标志

  

55.SPI interface schematic and Configuration

 

  数据帧格式:根据CR1寄存器的LSBFIRST位的设置,数据可以MSB在前也可以LSB在前。

  根据CR1寄存器的DEF位,每个数据帧可以是8位或16位。

  (九)SPI中断

  

55.SPI interface schematic and Configuration

 

  (十)SPI引脚配置 (3个SPI)

  

55.SPI interface schematic and Configuration

 

  引脚的工作模式设置

  

55.SPI interface schematic and Configuration

 

  引脚必须要按照这个表格配置。

  二。SPI寄存器库函数配置

  (一)常用寄存器

  

55.SPI interface schematic and Configuration

 

  (二)SPI相关库函数

  

55.SPI interface schematic and Configuration

 

  STM32的SPI接口可以配置为支持SPI协议或者支持I2S音频协议。默认是SPI模式,可以通过软件切换到I2S方式。

  常用的函数:

  1. void SPI_Init(SPI_TypeDef* SPIx, SPI_InitTypeDef* SPI_InitStruct);//SPI的初始化

  2. void SPI_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState); //SPI使能

  3. void SPI_I2S_ITConfig(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT, FunctionalState NewState); //开启中断

  4. void SPI_I2S_DMACmd(SPI_TypeDef* SPIx, uint16_t SPI_I2S_DMAReq, FunctionalState NewState);//通 过DMA传输数据

  5. void SPI_I2S_SendData(SPI_TypeDef* SPIx, uint16_t Data); //发送数据

  6. uint16_t SPI_I2S_ReceiveData(SPI_TypeDef* SPIx); //接收数据

  7. void SPI_DataSizeConfig(SPI_TypeDef* SPIx, uint16_t SPI_DataSize); //设置数据是8位还是16位

  8. 其他几个状态函数

  void SPI_Init(SPI_TypeDef* SPIx, SPI_InitTypeDef* SPI_InitStruct);//SPI的初始化

  

55.SPI interface schematic and Configuration

 

  结构体成员变量比较多,这里我们挑取几个重要的成员变量讲解一下:

  第一个参数 SPI_Direction 是用来设置 SPI 的通信方式,可以选择为半双工,全双工,以及串行发和串行收方式,这里我们选择全双工模式 SPI_Direction_2Lines_FullDuplex。

  第二个参数 SPI_Mode 用来设置 SPI 的主从模式,这里我们设置为主机模式 SPI_Mode_Master,当然有需要你也可以选择为从机模式 SPI_Mode_Slave。

  第三个参数 SPI_DataSiz 为 8 位还是 16 位帧格式选择项,这里我们是 8 位传输,选择SPI_DataSize_8b。

  第四个参数 SPI_CPOL 用来设置时钟极性,我们设置串行同步时钟的空闲状态为高电平所以我们选择 SPI_CPOL_High。

  第五个参数 SPI_CPHA 用来设置时钟相位,也就是选择在串行同步时钟的第几个跳变沿(上升或下降)数据被采样,可以为第一个或者第二个条边沿采集,这里我们选择第二个跳变沿,所以选择 SPI_CPHA_2Edge

  第六个参数 SPI_NSS 设置 NSS 信号由硬件(NSS 管脚)还是软件控制,这里我们通过软件控

  制 NSS 关键,而不是硬件自动控制,所以选择 SPI_NSS_Soft。

  第七个参数 SPI_BaudRatePrescaler 很关键,就是设置 SPI 波特率预分频值也就是决定 SPI 的时

  钟的参数 , 从不分频道 256 分频 8 个可选值,初始化的时候我们选择 256 分频值

  SPI_BaudRatePrescaler_256, 传输速度为 36M/256=140.625KHz。

  第八个参数 SPI_FirstBit 设置数据传输顺序是 MSB 位在前还是 LSB 位在前, ,这里我们选择

  SPI_FirstBit_MSB 高位在前。

  第九个参数 SPI_CRCPolynomial 是用来设置 CRC 校验多项式,提高通信可靠性,大于 1 即可。

  设置好上面 9 个参数,我们就可以初始化 SPI 外设了。

  初始化的范例格式为:

  SPI_InitTypeDef SPI_InitStructure;

  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //双线双向全双工

  SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //主 SPI

  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; // SPI 发送接收 8 位帧结构

  SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;//串行同步时钟的空闲状态为高电平

  371

  SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;//第二个跳变沿数据被采样

  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSS 信号由软件控制

  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; //预分频 256

  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //数据传输从 MSB 位开始

  SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC 值计算的多项式

  SPI_Init(SPI2, &SPI_InitStructure); //根据指定的参数初始化外设 SPIx 寄存器

  (三)程序配置步骤

  

55.SPI interface schematic and Configuration

 

  三。W25Qxx配置讲解

  (一)电路图

  

55.SPI interface schematic and Configuration

 

  片选用的PB12

  W25Q64 是华邦公司推出的大容量SPI FLASH 产品,W25Q64 的容量为 64Mb,该系列还有 W25Q80/16/32 等。ALIENTEK 所选择的 W25Q64 容量为 64Mb,也就是 8M 字节。(1M=1024K)

  W25Q64 将 8M 的容量分为 128 个块(Block),每个块大小为 64K 字节,每个块又分为 16个扇区(Sector),每个扇区 4K 个字节。W25Q64 的最少擦除单位为一个扇区,也就是每次必须擦除 4K 个字节。这样我们需要给 W25Q64 开辟一个至少 4K 的缓存区,这样对 SRAM 要求比较高,要求芯片必须有 4K 以上 SRAM 才能很好的操作。

  W25Q64 的擦写周期多达 10W 次,具有 20 年的数据保存期限,支持电压为 2.7~3.6V,W25Q64 支持标准的 SPI,还支持双输出/四输出的 SPI,最大 SPI 时钟可以到 80Mhz(双输出时相当于 160Mhz,四输出时相当于 320M),更多的 W25Q64 的介绍,请参考 W25Q64 的DATASHEET。

  在往一个地址写数据之前,要先把这个扇区的数据全部读出来保存在缓存里,然后再把这个扇区擦除,然后在缓存中修改要写的数据,然后再把整个缓存中的数据再重新写入刚才擦除的扇区中。

便于学习和参考再给大家分享些spi 的资料

The SPI communication stm32
http://www.makeru.com.cn/live/3523_1795.html?s=45051
SPI communication protocol driver NORFLASH
http://www.makeru.com.cn/live/4034_2151.html?s= 45051

Guess you like

Origin www.cnblogs.com/923327iu/p/11865852.html