{
/* Check the parameters */
assert_param(IS_SPI_ALL_PERIPH(SPIx));
assert_param(IS_SPI_NSS_INTERNAL(SPI_NSSInternalSoft));
if (SPI_NSSInternalSoft != SPI_NSSInternalSoft_Reset) {
/* Set NSS pin internally by software */ SPIx->CR1 |= SPI_NSSInternalSoft_Set; } else
{
/* Reset NSS pin internally by software */ SPIx->CR1 &= SPI_NSSInternalSoft_Reset; } }
/*【13】函数SPI_SSOutputCmd
****************************************************************************** * Function Name : SPI_SSOutputCmd
* Description : Enables or disables the SS output for the selected SPI. * Input : - SPIx: where x can be 1, 2 or 3 to select the SPI peripheral. * - NewState: new state of the SPIx SS output. * This parameter can be: ENABLE or DISABLE. * Output : None
* Return : None
*******************************************************************************/ void SPI_SSOutputCmd(SPI_TypeDef* SPIx, FunctionalState NewState) {
/* Check the parameters */
assert_param(IS_SPI_ALL_PERIPH(SPIx)); assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the selected SPI SS output */
SPIx->CR2 |= CR2_SSOE_Set;// 0x0004,开启在主模式下SS输出,该设备不能工作在多主设备模式 } else {
/* Disable the selected SPI SS output */
SPIx->CR2 &= CR2_SSOE_Reset;// 0xFFFB,禁止在主模式下SS输出,该设备可以工作在多主设备模式 } }
/*【14】函数SPI_DataSizeConfig
****************************************************************************** * Function Name : SPI_DataSizeConfig
* Description : Configures the data size for the selected SPI.
* Input : - SPIx: where x can be 1, 2 or 3 to select the SPI peripheral. * - SPI_DataSize: specifies the SPI data size.
* This parameter can be one of the following values:
* - SPI_DataSize_16b: Set data frame format to 16bit. * - SPI_DataSize_8b: Set data frame format to 8bit. * Output : None * Return : None
*******************************************************************************/ SPI_DataSize:设置8 位或者16位数据帧结构。 Table.14-1 SPI_DMAReq值
SPI_DataSize SPI_DataSize_8b 例: /* Set 8bit data frame format for SPI1 */ SPI_DataSizeConfig(SPI1, SPI_DataSize_8b); /*Set 16bit data frame format for SPI2 */ SPI_DataSizeConfig(SPI2, SPI_DataSize_16b);
函数原型如下:
void SPI_DataSizeConfig(SPI_TypeDef* SPIx, u16 SPI_DataSize) {
/* Check the parameters */
assert_param(IS_SPI_ALL_PERIPH(SPIx)); assert_param(IS_SPI_DATASIZE(SPI_DataSize));
/* Clear DFF bit */
SPIx->CR1 &= (u16)~SPI_DataSize_16b;//用“16b”值复位DFF位值。 /* Set new DFF bit value */ SPIx->CR1 |= SPI_DataSize; }
/*【15】函数SPI_TransmitCRC
****************************************************************************** * Function Name : SPI_TransmitCRC
* Description : Transmit the SPIx CRC value.
* Input : - SPIx(Not Include I2Sx): where x can be 1, 2 or 3 to select the SPI peripheral. * Output : None * Return : None
*******************************************************************************/ void SPI_TransmitCRC(SPI_TypeDef* SPIx) {
/* Check the parameters */
assert_param(IS_SPI_ALL_PERIPH(SPIx));
/* Enable the selected SPI CRC transmission */
SPIx->CR1 |= CR1_CRCNext_Set;// 0x1000,下一个发送CRC:1--下一个发送的值来自发送CRC寄存器 }//若为0--下一个发送的值来自发送缓冲区
/*【16】函数SPI_CalculateCRC
描述/CR1.DFF/bit11 设置数据帧格式为8位 #Value 0x0000 0x0800 SPI_DataSize_16b 设置数据帧格式为16位 ****************************************************************************** * Function Name : SPI_CalculateCRC
* Description : Enables or disables the CRC value calculation of the * transfered bytes.
* Input : - SPIx: where x can be 1, 2 or 3 to select the SPI peripheral. * - NewState: new state of the SPIx CRC value calculation. * This parameter can be: ENABLE or DISABLE. * Output : None * Return : None
*******************************************************************************/ void SPI_CalculateCRC(SPI_TypeDef* SPIx, FunctionalState NewState) {
/* Check the parameters */
assert_param(IS_SPI_ALL_PERIPH(SPIx)); assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE) {
/* Enable the selected SPI CRC calculation */
SPIx->CR1 |= CR1_CRCEN_Set;// 0x2000,硬件CRC校验使能:开启CRC计算 } else
{
/* Disable the selected SPI CRC calculation */ SPIx->CR1 &= CR1_CRCEN_Reset;// 0xDFFF } }
/*【17】函数SPI_GetCRC
****************************************************************************** * Function Name : SPI_GetCRC
* Description : Returns the transmit or the receive CRC register value for * the specified SPI.
* Input : - SPIx: where x can be 1, 2 or 3 to select the SPI peripheral. * - SPI_CRC: specifies the CRC register to be read. * This parameter can be one of the following values: * - SPI_CRC_Tx: Selects Tx CRC register * - SPI_CRC_Rx: Selects Rx CRC register * Output : None
* Return : The selected CRC register value..
*******************************************************************************/ SPI_CRC:SPI_CRC选择SPI Rx 或SPI Tx的 CRC 寄存器。 Table.17-1 SPI_CRC值 SPI_CRC SPI_CRC_Tx SPI_CRC_Rx 例:
描述 选择Tx CRC寄存器 选择Rx CRC寄存器 (u8)0x00 (u8)0x01 /* Returns the SPI1 transmit CRC register */ u16 CRCValue;
CRCValue = SPI_GetCRC(SPI1, SPI_CRC_Tx); 函数原型如下:
u16 SPI_GetCRC(SPI_TypeDef* SPIx, u8 SPI_CRC) {
u16 crcreg = 0;
/* Check the parameters */
assert_param(IS_SPI_ALL_PERIPH(SPIx)); assert_param(IS_SPI_CRC(SPI_CRC));
if (SPI_CRC != SPI_CRC_Rx)//如果是TXCRC,读TXCRCR {
/* Get the Tx CRC register */ crcreg = SPIx->TXCRCR; }
else//否则是RXCRC,读RXCRCR {
/* Get the Rx CRC register */ crcreg = SPIx->RXCRCR; }
/* Return the selected CRC register */ return crcreg; }
/*【18】函数SPI_GetCRCPolynomial
****************************************************************************** * Function Name : SPI_GetCRCPolynomial
* Description : Returns the CRC Polynomial register value for the specified SPI. * Input : - SPIx: where x can be 1, 2 or 3 to select the SPI peripheral. * Output : None
* Return : The CRC Polynomial register value.
*******************************************************************************/ u16 SPI_GetCRCPolynomial(SPI_TypeDef* SPIx) {
/* Check the parameters */
assert_param(IS_SPI_ALL_PERIPH(SPIx));
/* Return the CRC polynomial register */ return SPIx->CRCPR;//u16 }
/*【19】函数SPI_BiDirectionalLineConfig
****************************************************************************** * Function Name : SPI_BiDirectionalLineConfig
* Description : Selects the data transfer direction in bi-directional mode * for the specified SPI.
* Input : - SPIx: where x can be 1, 2 or 3 to select the SPI peripheral. * - SPI_Direction: specifies the data transfer direction in
* bi-directional mode.
* This parameter can be one of the following values: * - SPI_Direction_Tx: Selects Tx transmission direction * - SPI_Direction_Rx: Selects Rx receive direction * Output : None * Return : None
*******************************************************************************/ SPI_Direction:SPI_Direction选择SPI在双向模式下的数据传输方向。 Table.19-1 SPI_CRC值
SPI_Direction 描述/CR1. SPI_Direction_Tx 选择Tx发送方向 SPI_Direction_Rx 选择Rx接受方向 例:
bit14 #Val 0x4000 0xBFFF /* Set the SPI2 in bidirectional transmit only mode */
SPI_BiDirectionalLineConfig(SPI_Direction_Tx); 函数原型如下:
void SPI_BiDirectionalLineConfig(SPI_TypeDef* SPIx, u16 SPI_Direction) {
/* Check the parameters */
assert_param(IS_SPI_ALL_PERIPH(SPIx));
assert_param(IS_SPI_DIRECTION(SPI_Direction));
if (SPI_Direction == SPI_Direction_Tx)
{
/* Set the Tx only mode */// 该位和BIDIMODE(=1)位一起决定在“单线双向”模式下数据的传输方向 SPIx->CR1 |= SPI_Direction_Tx;// 1(输出使能(只发模式)) } else {
/* Set the Rx only mode */
SPIx->CR1 &= SPI_Direction_Rx;// 0(输出禁止(只收模式)) } }
/*【20】函数SPI_I2S_GetFlagStatus
****************************************************************************** * Function Name : SPI_I2S_GetFlagStatus
* Description : Checks whether the specified SPI/I2S flag is set or not. * Input : - SPIx: where x can be :
* - 1, 2 or 3 in SPI mode
* - 2 or 3 in I2S mode
* - SPI_I2S_FLAG: specifies the SPI/I2S flag to check. * This parameter can be one of the following values: * - SPI_I2S_FLAG_TXE: Transmit buffer empty flag.
* - SPI_I2S_FLAG_RXNE: Receive buffer not empty flag. * - SPI_I2S_FLAG_BSY: Busy flag. * - SPI_I2S_FLAG_OVR: Overrun flag. * - SPI_FLAG_MODF: Mode Fault flag. * - SPI_FLAG_CRCERR: CRC Error flag.