STM32F10x_SPI与I2S(5)

2019-03-03 12:58

* - I2S_FLAG_UDR: Underrun Error flag. * - I2S_FLAG_CHSIDE: Channel Side flag. * Output : None

* Return : The new state of SPI_I2S_FLAG (SET or RESET).

*******************************************************************************/ SPI_FLAG:Table 441. 给出了所有可以被函数SPI_GetFlagStatus检查的标志位列表 Table.20-1 SPI_FLAG值

SPI_FLAG SPI_I2S_FLAG_BSY SPI_I2S_FLAG_OVR SPI_I2S_FLAG_MODF SPI_I2S_FLAG_CRCERR SPI_I2S_FLAG_UDR SPI_I2S_FLAG_CHSIDE SPI_I2S_FLAG_TXE SPI_I2S_FLAG_RXNE 描述/SR.bit7-0 忙标志位 超出标志位(overrun) 模式错位标志位 CRC错误标志位 发生下溢(underrun) 需要传输或者接收右声道 发送缓存空标志位 接受缓存非空标志位 #Val 0x0080 0x0040 0x0020 0x0010 ERRIE 0x0008 0x0004 0x0002 TXEIE 0x0001 RXNEIE 控制位 —— 函数原型如下:

FlagStatus SPI_I2S_GetFlagStatus(SPI_TypeDef* SPIx, u16 SPI_I2S_FLAG) {

FlagStatus bitstatus = RESET;

/* Check the parameters */

assert_param(IS_SPI_ALL_PERIPH(SPIx));

assert_param(IS_SPI_I2S_GET_FLAG(SPI_I2S_FLAG));

/* Check the status of the specified SPI/I2S flag */ if ((SPIx->SR & SPI_I2S_FLAG) != (u16)RESET) {

/* SPI_I2S_FLAG is set */ bitstatus = SET; } else

{

/* SPI_I2S_FLAG is reset */ bitstatus = RESET; }

/* Return the SPI_I2S_FLAG status */ return bitstatus; }

/*【21】函数SPI_I2S_ClearFlag

****************************************************************************** * Function Name : SPI_I2S_ClearFlag

* Description : Clears the SPIx/I2Sx pending flags. * 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 clear. * This parameter can be one of the following values:

* - SPI_I2S_FLAG_OVR: Overrun flag * - SPI_FLAG_MODF: Mode Fault flag. * - SPI_FLAG_CRCERR: CRC Error flag. * - I2S_FLAG_UDR: Underrun Error flag.

* Note: Before clearing OVR flag, it is mandatory to read

* SPI_I2S_DR register, so that the last data is not lost. * Output : None

* Return : None

*******************************************************************************/ 可以清除的Flag只有4个:如下表:

Table.21-1可以清除的中断标志位如下:(对照Table.20-1)

SPI_I2S_FLAG SPI_I2S_FLAG_BSY SPI_I2S_FLAG_OVR SPI_FLAG_MODF SPI_FLAG_CRCERR I2S_FLAG_UDR I2S_FLAG_CHSIDE SPI_I2S_FLAG_TXE SPI_I2S_FLAG_RXNE 函数原型如下: void SPI_I2S_ClearFlag(SPI_TypeDef* SPIx, u16 SPI_I2S_FLAG) {

/* Check the parameters */

assert_param(IS_SPI_ALL_PERIPH(SPIx));

assert_param(IS_SPI_I2S_CLEAR_FLAG(SPI_I2S_FLAG));

/* SPI_FLAG_MODF flag clear */ if(SPI_I2S_FLAG == SPI_FLAG_MODF) {

/* Read SR register */

(void)SPIx->SR;//[1]读或写SR

/* Write on CR1 register */

SPIx->CR1 |= CR1_SPE_Set; //[2]写CR1。 }//通过[1][2]步骤,可以清除MODF标志位

/* SPI_I2S_FLAG_OVR flag or I2S_FLAG_UDR flag clear */

else if ((SPI_I2S_FLAG == SPI_I2S_FLAG_OVR) || (SPI_I2S_FLAG == I2S_FLAG_UDR))

{

/* Read SR register (Before clearing OVR flag, it is mandatory to read SPI_I2S_DR register)*/ (void)SPIx->SR;//对于上溢标志OVR和下溢标志UDR,读SR即可清除这两位中断标志 }

else /* SPI_FLAG_CRCERR flag clear */ {

/* Clear the selected SPI flag */

SPIx->SR = (u16)~SPI_I2S_FLAG;//对于CRCERR,只要直接写0即可清除。 } }

描述/SR.bit7-0 忙标志位 超出标志位(overrun) 模式错位标志位 CRC错误标志位 发生下溢(underrun) 发送缓存空标志位 接收缓存非空标志位 #Val 0x0080 0x0040 0x0020 0x0010 0x0008 0x0002 0x0001 可否清除 清除方式 —— 读SR 读SR->写CR1 直接写0 读SR 读SR,但要求OVR、UDR、ERRIE被清除之后。 写数据到DR 或令I2SE =0 控制位 —— ERRIE 需要传输或者接收右声道 0x0004 从DR读数据 TXEIE RXNEIE

/*【22】函数SPI_I2S_GetITStatus

****************************************************************************** * Function Name : SPI_I2S_GetITStatus

* Description : Checks whether the specified SPI/I2S interrupt has occurred or not. * Input : - SPIx: where x can be : * - 1, 2 or 3 in SPI mode

* - 2 or 3 in I2S mode

* - SPI_I2S_IT: specifies the SPI/I2S interrupt source to check. * This parameter can be one of the following values: * - SPI_I2S_IT_TXE: Transmit buffer empty interrupt. * - SPI_I2S_IT_RXNE: Receive buffer not empty interrupt. * - SPI_I2S_IT_OVR: Overrun interrupt. * - SPI_IT_MODF: Mode Fault interrupt. * - SPI_IT_CRCERR: CRC Error interrupt. * - I2S_IT_UDR: Underrun Error interrupt.

* Output : None

* Return : The new state of SPI_I2S_IT (SET or RESET).

*******************************************************************************/ SPI_I2S_IT:下表给出了所有可以被函数SPI_ GetITStatus检查的中断标志位列表。 Table.22-1 SPI_IT值(SR中的CHSIDE和BUSY位本函数不检查)

SPI_I2S_IT SPI_I2S_IT_TXE SPI_I2S_IT_RXNE SPI_I2S_IT_OVR SPI_IT_MODF SPI_IT_CRCERR I2S_IT_UDR 例: /* Test if the SPI1 Overrun interrupt has occurred or not */ ITStatus Status;

Status = SPI_GetITStatus(SPI1, SPI_IT_OVR);

函数原型如下:

ITStatus SPI_I2S_GetITStatus(SPI_TypeDef* SPIx, u8 SPI_I2S_IT) {

ITStatus bitstatus = RESET;

u16 itpos = 0, itmask = 0, enablestatus = 0;

/* Check the parameters */

assert_param(IS_SPI_ALL_PERIPH(SPIx));

assert_param(IS_SPI_I2S_GET_IT(SPI_I2S_IT));

/* Get the SPI/I2S IT index */

itpos = (u16)((u16)0x01 << (SPI_I2S_IT & (u8)0x0F));//取出中断标志在SR中的Bit位置。

/* Get the SPI/I2S IT mask */

itmask = SPI_I2S_IT >> 4;//取出中断产生的允许位在CR2中的bit位置 /* Set the IT mask */

itmask = (u16)((u16)0x01 << itmask);

描述/SR 发送缓存空中断标志位 超出中断标志位 模式错误标志位 CRC错误标志位 下溢中断标志位 #Value (u8)0x71 (u8)0x56 (u8)0x55 (u8)0x54 (u8)0x53 SR中的位置 在CR2中控制位位置 bit1 bit0 bit6 bit5 bit4 bit3 bit7=TXEIE bit6=RXNEIE bit5=ERRIE OVR、MODF、CRCERR、UDR标志位只有在ERRIE=1时才能产生 接受缓存非空中断标志位 (u8)0x60 /* Get the SPI_I2S_IT enable bit status */ enablestatus = (SPIx->CR2 & itmask) ;

/* Check the status of the specified SPI/I2S interrupt */

if (((SPIx->SR & itpos) != (u16)RESET) && enablestatus)

//SR中的标志位与CR2对应的控制位同时为1时,表明中断已被发生,并且有效。 {

/* SPI_I2S_IT is set */ bitstatus = SET; } else

{

/* SPI_I2S_IT is reset */ bitstatus = RESET; }

/* Return the SPI_I2S_IT status */ return bitstatus; }

/*【23】函数SPI_I2S_ClearITPendingBit

****************************************************************************** * Function Name : SPI_I2S_ClearITPendingBit

* Description : Clears the SPIx/I2Sx interrupt pending bits. * Input : - SPIx: where x can be : * - 1, 2 or 3 in SPI mode

* - 2 or 3 in I2S mode

* - SPI_I2S_IT: specifies the SPI/I2S interrupt pending bit to clear. * This parameter can be one of the following values: * - SPI_I2S_IT_OVR: Overrun interrupt. * - SPI_IT_MODF: Mode Fault interrupt. * - SPI_IT_CRCERR: CRC Error interrupt. * - I2S_IT_UDR: Underrun Error interrupt. * Output : None * Return : None

*******************************************************************************/ 下表给出了可以被清除的中断标志:

Table.23-1 可被清除的中断标志如下表。

[1]、本表完全采用Table.22-1 给出的参数值。 [2]、清除方式,参考Table.20-1。

SPI_I2S_IT SPI_I2S_IT_TXE SPI_I2S_IT_RXNE SPI_I2S_IT_OVR SPI_IT_MODF SPI_IT_CRCERR I2S_IT_UDR 函数原型如下: {

描述/SR 发送缓存空中断标志位 #Value (u8)0x71 SR中的位置 在CR2中控制位位置 可清除 bit1 bit0 bit6 bit5 bit4 bit3 bit7=TXEIE bit6=RXNEIE 间接 间接 接受缓存非空中断标志位 (u8)0x60 超出中断标志位 (u8)0x56 模式错误标志位 CRC错误标志位 下溢中断标志位 (u8)0x55 (u8)0x54 (u8)0x53 bit5=ERRIE 本函数 OVR、MODF、CRCERR、直接清UDR标志位只有在除。 ERRIE=1时才能产生 void SPI_I2S_ClearITPendingBit(SPI_TypeDef* SPIx, u8 SPI_I2S_IT)

u16 itpos = 0;

/* Check the parameters */

assert_param(IS_SPI_ALL_PERIPH(SPIx));

assert_param(IS_SPI_I2S_CLEAR_IT(SPI_I2S_IT));

/* SPI_IT_MODF pending bit clear */ if(SPI_I2S_IT == SPI_IT_MODF) {

/* Read SR register */ (void)SPIx->SR;

/* Write on CR1 register */ SPIx->CR1 |= CR1_SPE_Set;

}//清除MODF位的操作方式:先读SR,再写CR1。

/* SPI_I2S_IT_OVR or I2S_IT_UDR pending bit clear */

else if((SPI_I2S_IT == SPI_I2S_IT_OVR) || (SPI_I2S_IT == I2S_IT_UDR)) {

/* Read SR register */

(void)(SPIx->SR);//对于清除OVR和UDR位的操作方式:只要读SR即可。 }

else /* SPI_IT_CRCERR pending bit clear */ {

/* Get the SPI/I2S IT index */

itpos = (u16)((u16)0x01 << (SPI_I2S_IT & (u8)0x0F));//取出CRCERR在SR中的位值( = bit4) /* Clear the selected SPI/I2S interrupt pending bits */

SPIx->SR = (u16)~itpos;//对于清除CRCERR位的操作方式:直接写0

//本语句不必采用“SPIx->SR &= (u16)~itpos;”的“&”操作: //写‘1’不能改变SR中其他7位中断标志的状态。 } }


STM32F10x_SPI与I2S(5).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:Gossip girl

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: