SPI 广泛用于 ADC LCD MCU 全双工
{
全双工:在A给B发信号的同时,B也可以给A发信号
半双工:指A能发信号给B,B也能发信号给A,但这两个过程不能同时进行
单工:指A只能发信号,而B只能接收信号,通信是单向的
}
SPI 物理层特点 :一主机多从机 通过ss片选线确定从机,低电平有效,并作为起始信号。高电平结束信号。
上升沿触发,下降沿采样
spi模式 CPOL CPHA 空闲SCK时钟 采样时刻
0 0 0 低电平 奇数边沿
1 0 1 低电平 偶数边沿
2 1 0 高电平 奇数边沿
3 1 1 高电平 偶数边沿
SPI特性:
STM32 SPI外设可用作主机或从机,支持最高的SCLK时钟频率为f/2,数据帧可为8位或16位。时钟是由主机控制
flash存储特性:
1 在写入数据之前必须先擦除置1
2 写入数据时只能将数据位1改为0
3 擦出须以最小单位(一个扇区一般4096byte)
SPI架构:
spi
BR 位控制时钟频率,作为主机有效
DR 数据寄存器
CR 主从,单双向,spi模式
SR spi工作状态。
发送标志位TXE,接收标志位RXE BSY硬件标志位
SPI 库函数
SPI_Init();//结构体初始化
SPI_Cmd();//使能
typedef struct
{
uint16_t SPI_Direction; /*!< Specifies the SPI unidirectional or bidirectional data mode.
This parameter can be a value of @ref SPI_data_direction */
uint16_t SPI_Mode; /*!< Specifies the SPI operating mode.
This parameter can be a value of @ref SPI_mode */
uint16_t SPI_DataSize; /*!< Specifies the SPI data size.
This parameter can be a value of @ref SPI_data_size */
uint16_t SPI_CPOL; /*!< Specifies the serial clock steady state.
This parameter can be a value of @ref SPI_Clock_Polarity */
uint16_t SPI_CPHA; /*!< Specifies the clock active edge for the bit capture.
This parameter can be a value of @ref SPI_Clock_Phase */
uint16_t SPI_NSS; /*!< Specifies whether the NSS signal is managed by
hardware (NSS pin) or by software using the SSI bit.
This parameter can be a value of @ref SPI_Slave_Select_management */
uint16_t SPI_BaudRatePrescaler; /*!< Specifies the Baud Rate prescaler value which will be
used to configure the transmit and receive SCK clock.
This parameter can be a value of @ref SPI_BaudRate_Prescaler.
@note The communication clock is derived from the master
clock. The slave clock does not need to be set. */
uint16_t SPI_FirstBit; /*!< Specifies whether data transfers start from MSB or LSB bit.
This parameter can be a value of @ref SPI_MSB_LSB_transmission */
uint16_t SPI_CRCPolynomial; /*!< Specifies the polynomial used for the CRC calculation. */
}SPI_InitTypeDef;