/*******************************************************************************
* 函数名称: SPI_I2S_DeInit
* 功能描述: 将SPIx外设寄存器重置为他们的缺省值(Affects also the I2Ss).
* 输入参数: SPIx x为1,2或3用于选定SPI外设。
* 输出参数: 无
* 返回参数: 无
*******************************************************************************/
void SPI_I2S_DeInit(SPI_TypeDef* SPIx)
{
/* Check the parameters [检查参数]*/
assert_param(IS_SPI_ALL_PERIPH(SPIx));
switch (*(u32*)&SPIx)
{
case SPI1_BASE:
/* Enable SPI1 reset state [使能SPI1复位状态]*/
RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, ENABLE);
/* Release SPI1 from reset state [解除SPI1复位状态]*/
RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, DISABLE);
break;
case SPI2_BASE:
/* Enable SPI2 reset state [使能SPI2复位状态]*/
RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, ENABLE);
/* Release SPI2 from reset state [解除SPI2复位状态]*/
RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, DISABLE);
break;
case SPI3_BASE:
/* Enable SPI3 reset state [使能SPI3复位状态]*/
RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3, ENABLE);
/* Release SPI3 from reset state [解除SPI3复位状态]*/
RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3, DISABLE);
break;
default:
break;
}
}
/*******************************************************************************
* 函数名称: SPI_Init
* 功能描述: 根据SPI_InitStruct.中的特定参数初始化SPIx外设.
* 输入参数: (1)SPIx :x为1,2或3用于选定SPI外设。
* (2)SPI_InitStruct:指向一个包含特定SPI外设配置信息的SPI_InitTypeDef结构体的指针。
* 输出参数: 无
* 返回参数: 无
******************************************************************************/
void SPI_Init(SPI_TypeDef* SPIx, SPI_InitTypeDef* SPI_InitStruct)
{
u16 tmpreg = 0;
/* Check the parameters [检查参数]*/
assert_param(IS_SPI_ALL_PERIPH(SPIx));
/* Check the SPI parameters [检查SPI的参数]*/
assert_param(IS_SPI_DIRECTION_MODE(SPI_InitStruct->SPI_Direction));
assert_param(IS_SPI_MODE(SPI_InitStruct->SPI_Mode));
assert_param(IS_SPI_DATASIZE(SPI_InitStruct->SPI_DataSize));
assert_param(IS_SPI_CPOL(SPI_InitStruct->SPI_CPOL));
assert_param(IS_SPI_CPHA(SPI_InitStruct->SPI_CPHA));
assert_param(IS_SPI_NSS(SPI_InitStruct->SPI_NSS));
assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_InitStruct->SPI_BaudRatePrescaler));
assert_param(IS_SPI_FIRST_BIT(SPI_InitStruct->SPI_FirstBit));
assert_param(IS_SPI_CRC_POLYNOMIAL(SPI_InitStruct->SPI_CRCPolynomial));
/*---------------------------- SPIx CR1 Configuration [SPIx CR1配置]*/
/* Get the SPIx CR1 value [取得SPIx CR1的值]*/
tmpreg = SPIx->CR1;
/* Clear BIDIMode, BIDIOE, RxONLY, SSM, SSI, LSBFirst, BR, MSTR, CPOL and CPHA bits
清BIDIMode, BIDIOE, RxONLY, SSM, SSI, LSBFirst, BR, MSTR, CPOL 和CPHA 位*/
tmpreg &= CR1_CLEAR_Mask;
/* Configure SPIx[配置SPIx]: direction[方向], NSS management[NSS管理], first transmitted bit[第一个发送位], BaudRate prescaler[波特率预分频]
master/salve mode[主/从模式], CPOL and CPHA */
/* Set BIDImode, BIDIOE and RxONLY bits according to SPI_Direction value [依照SPI_Direction的值置位BIDImode, BIDIOE和RxONLY]*/
/* Set SSM, SSI and MSTR bits according to SPI_Mode and SPI_NSS values [依照SPI_Mode 和SPI_NSS的值置位SSM, SSI 和 MSTR]*/
/* Set LSBFirst bit according to SPI_FirstBit value [依照SPI_FirstBit的值置位LSBFirst]*/
/* Set BR bits according to SPI_BaudRatePrescaler value [依照SPI_BaudRatePrescaler的值置位BR]*/
/* Set CPOL bit according to SPI_CPOL value [依照SPI_CPOL的值置位CPOL]*/
/* Set CPHA bit according to SPI_CPHA value [依照SPI_CPHA的值置位CPHA]*/
tmpreg |= (u16)((u32)SPI_InitStruct->SPI_Direction | SPI_InitStruct->SPI_Mode |
SPI_InitStruct->SPI_DataSize | SPI_InitStruct->SPI_CPOL |
SPI_InitStruct->SPI_CPHA | SPI_InitStruct->SPI_NSS |
SPI_InitStruct->SPI_BaudRatePrescaler | SPI_InitStruct->SPI_FirstBit);
/* Write to SPIx CR1 [写SPIx CR1]*/
SPIx->CR1 = tmpreg;
/* Activate the SPI mode (Reset I2SMOD bit in I2SCFGR register) */
SPIx->I2SCFGR &= SPI_Mode_Select;
/*---------------------------- SPIx CRCPOLY Configuration --------------------*/
/* Write to SPIx CRCPOLY [写SPIx CRCPOLY]*/
SPIx->CRCPR = SPI_InitStruct->SPI_CRCPolynomial;
}
用户1483310 2010-5-15 22:52
sunke9_998892717 2009-4-10 08:03
用户1441579 2009-4-1 17:34