* which will be initialized. * Output : None
* Return : None
*******************************************************************************/ void I2S_StructInit(I2S_InitTypeDef* I2S_InitStruct) {
/*--------------- Reset I2S init structure parameters values -----------------*/ /* Initialize the I2S_Mode member */
I2S_InitStruct->I2S_Mode = I2S_Mode_SlaveTx;
/* Initialize the I2S_Standard member */
I2S_InitStruct->I2S_Standard = I2S_Standard_Phillips;
/* Initialize the I2S_DataFormat member */
I2S_InitStruct->I2S_DataFormat = I2S_DataFormat_16b;
/* Initialize the I2S_MCLKOutput member */
I2S_InitStruct->I2S_MCLKOutput = I2S_MCLKOutput_Disable;
/* Initialize the I2S_AudioFreq member */
I2S_InitStruct->I2S_AudioFreq = I2S_AudioFreq_Default;
/* Initialize the I2S_CPOL member */
I2S_InitStruct->I2S_CPOL = I2S_CPOL_Low; }
/*【06】函数SPI_Cmd
****************************************************************************** * Function Name : SPI_Cmd
* Description : Enables or disables the specified SPI peripheral.
* Input : - SPIx: where x can be 1, 2 or 3 to select the SPI peripheral. * - NewState: new state of the SPIx peripheral. * This parameter can be: ENABLE or DISABLE. * Output : None
* Return : None
*******************************************************************************/ void SPI_Cmd(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 peripheral */
SPIx->CR1 |= CR1_SPE_Set;// 0x0040,开启SPI设备 } else {
/* Disable the selected SPI peripheral */ SPIx->CR1 &= CR1_SPE_Reset;// 0xFFBF } }
/*【07】函数I2S_Cmd
****************************************************************************** * Function Name : I2S_Cmd
* Description : Enables or disables the specified SPI peripheral (in I2S mode). * Input : - SPIx: where x can be 2 or 3 to select the SPI peripheral. * - NewState: new state of the SPIx peripheral. * This parameter can be: ENABLE or DISABLE. * Output : None * Return : None
*******************************************************************************/ void I2S_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState) {
/* Check the parameters */
assert_param(IS_SPI_23_PERIPH(SPIx)); assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the selected SPI peripheral (in I2S mode) */ SPIx->I2SCFGR |= I2SCFGR_I2SE_Set;// 0x0400,开启I2S } else
{
/* Disable the selected SPI peripheral (in I2S mode) */ SPIx->I2SCFGR &= I2SCFGR_I2SE_Reset;// 0xFBFF }
}
/*【08】函数SPI_I2S_ITConfig
****************************************************************************** * Function Name : SPI_I2S_ITConfig
* Description : Enables or disables the specified SPI/I2S interrupts. * 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 be enabled or disabled. * This parameter can be one of the following values: * - SPI_I2S_IT_TXE: Tx buffer empty interrupt mask
* - SPI_I2S_IT_RXNE: Rx buffer not empty interrupt mask * - SPI_I2S_IT_ERR: Error interrupt mask
* - NewState: new state of the specified SPI/I2S interrupt. * This parameter can be: ENABLE or DISABLE. * Output : None * Return : None
*******************************************************************************/
SPI_IT:输入参数SPI_IT使能或者失能SPI的中断。可以取下表的一个或者多个取值的组合作为该参数的值。 Table.08-1 SPI_IT值 SPI_I2S_IT 描述/CR2 SPI_I2S_IT_TXE 发送缓存空中断屏蔽 SPI_I2S_IT_RXNE 接收缓存非空中断屏蔽 SPI_I2S_IT_ERR 错误中断屏蔽 例:
/* Enable SPI2 Tx buffer empty interrupt */ SPI_ITConfig(SPI2, SPI_IT_TXE, ENABLE); 函数原型如下:
void SPI_I2S_ITConfig(SPI_TypeDef* SPIx, u8 SPI_I2S_IT, FunctionalState NewState) {
u16 itpos = 0, itmask = 0 ;
/* Check the parameters */
assert_param(IS_SPI_ALL_PERIPH(SPIx));
assert_param(IS_FUNCTIONAL_STATE(NewState)); assert_param(IS_SPI_I2S_CONFIG_IT(SPI_I2S_IT));
/* Get the SPI/I2S IT index */
itpos = SPI_I2S_IT >> 4;//取出IT在CR2的位置 /* Set the IT mask */
itmask = (u16)((u16)1 << itpos);
if (NewState != DISABLE) {
/* Enable the selected SPI/I2S interrupt */ SPIx->CR2 |= itmask;//开启中断允许 } else {
/* Disable the selected SPI/I2S interrupt */ SPIx->CR2 &= (u16)~itmask; } }
/*【09】函数SPI_I2S_DMACmd
****************************************************************************** * Function Name : SPI_I2S_DMACmd
* Description : Enables or disables the SPIx/I2Sx DMA interface. * Input : - SPIx: where x can be : * - 1, 2 or 3 in SPI mode
* - 2 or 3 in I2S mode
* - SPI_I2S_DMAReq: specifies the SPI/I2S DMA transfer request * to be enabled or disabled.
* This parameter can be any combination of the following values: * - SPI_I2S_DMAReq_Tx: Tx buffer DMA transfer request * - SPI_I2S_DMAReq_Rx: Rx buffer DMA transfer request
#Val CR2中的位置 0x71 bit7 0x60 bit6 0x50 bit5 * - NewState: new state of the selected SPI/I2S DMA transfer * request.
* This parameter can be: ENABLE or DISABLE. * Output : None
* Return : None
*******************************************************************************/ SPI_DMAReq:SPI_DMAReq使能或者失能SPI Tx和/或SPI Rx的DMA传输请求。 Table.09-1 SPI_DMAReq值 SPI_I2S_DMAReq 描述/CR2 SPI_I2S_DMAReq_Tx 选择Tx缓存DMA传输请求 0x0002 bit1 SPI_I2S_DMAReq_Rx 选择Rx缓存DMA传输请求 0x0001 bit0 例:
/* Enable SPI2 Rx buffer DMA transfer request */ SPI_DMACmd(SPI2, SPI_DMAReq_Rx, ENABLE); 函数原型如下:
void SPI_I2S_DMACmd(SPI_TypeDef* SPIx, u16 SPI_I2S_DMAReq, FunctionalState NewState) {
/* Check the parameters */
assert_param(IS_SPI_ALL_PERIPH(SPIx));
assert_param(IS_FUNCTIONAL_STATE(NewState)); assert_param(IS_SPI_I2S_DMAREQ(SPI_I2S_DMAReq));
if (NewState != DISABLE)
{
/* Enable the selected SPI/I2S DMA requests */ SPIx->CR2 |= SPI_I2S_DMAReq;//开启DMA请求。 } else
{
/* Disable the selected SPI/I2S DMA requests */ SPIx->CR2 &= (u16)~SPI_I2S_DMAReq; }
}
/*【10】函数SPI_I2S_SendData
****************************************************************************** * Function Name : SPI_I2S_SendData
* Description : Transmits a Data through the SPIx/I2Sx peripheral. * Input : - SPIx: where x can be :
* - 1, 2 or 3 in SPI mode * - 2 or 3 in I2S mode * - Data : Data to be transmitted.. * Output : None
* Return : None
*******************************************************************************/ void SPI_I2S_SendData(SPI_TypeDef* SPIx, u16 Data) {
/* Check the parameters */
assert_param(IS_SPI_ALL_PERIPH(SPIx));
/* Write in the DR register the data to be sent */ SPIx->DR = Data; }
/*【11】函数SPI_I2S_ReceiveData
****************************************************************************** * Function Name : SPI_I2S_ReceiveData
* Description : Returns the most recent received data by the SPIx/I2Sx peripheral. * Input : - SPIx: where x can be : * - 1, 2 or 3 in SPI mode * - 2 or 3 in I2S mode
* Output : None
* Return : The value of the received data.
*******************************************************************************/ u16 SPI_I2S_ReceiveData(SPI_TypeDef* SPIx) {
/* Check the parameters */
assert_param(IS_SPI_ALL_PERIPH(SPIx));
/* Return the data in the DR register */ return SPIx->DR; }
/*【12】函数SPI_NSSInternalSoftwareConfig
****************************************************************************** * Function Name : SPI_NSSInternalSoftwareConfig
* Description : Configures internally by software the NSS pin for the selected * SPI.
* Input : - SPIx: where x can be 1, 2 or 3 to select the SPI peripheral. * - SPI_NSSInternalSoft: specifies the SPI NSS internal state. * This parameter can be one of the following values: * - SPI_NSSInternalSoft_Set: Set NSS pin internally
* - SPI_NSSInternalSoft_Reset: Reset NSS pin internally * Output : None
* Return : None
* Note : NSS = Slave Select
*******************************************************************************/ SPI_NSSInternalSoft:内部设置或者重置NSS管脚。 Table.12-1 SPI_DMAReq值
SPI_NSSInternalSoft SPI_NSSInternalSoft_Set SPI_NSSInternalSoft_Reset 例: 描述/CR1.SSI/bit8 内部设置NSS管脚 内部重置NSS管脚 #Val 0x0100 0xFEFF /* Set internaly by software the SPI1 NSS pin */
SPI_NSSInternalSoftwareConfig(SPI1, SPI_NSSInternalSoft_Set); /* Reset internaly by sofwtare the SPI2 NSS pin */
SPI_NSSInternalSoftwareConfig(SPI2, SPI_NSSInternalSoft_Reset);
函数原型如下:
void SPI_NSSInternalSoftwareConfig(SPI_TypeDef* SPIx, u16 SPI_NSSInternalSoft)