本帖最后由 yyHHyyYyyHHh 于 2022-10-28 16:34 编辑
运行n32g43x_EVAL\examples\SPI\CRC这个例程。
将修改SPI1的SCK、MOSI和MISO三个引脚,修改为PB3、PB5和PB4(这三个引脚复用确定为SPI1)。
其余没有修改,部分程序如下:
void GPIO_Configuration(void)
{
GPIO_InitType GPIO_InitStructure;
GPIO_InitStruct(&GPIO_InitStructure);
GPIO_InitStruct(&GPIO_InitStructure);
/* Configure SPI1 pins: SCK, MISO and MOSI ---------------------------------*/
/* Confugure SCK and MOSI pins as Alternate Function Push Pull */
GPIO_InitStructure.Pin = GPIO_PIN_3 | GPIO_PIN_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Alternate = GPIO_AF0_SPI1;
GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
/* Confugure MISO pin as Input Floating */
GPIO_InitStructure.Pin = GPIO_PIN_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Input;
GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
/* Configure SPI2 pins: SCK, MISO and MOSI ---------------------------------*/
/* Confugure SCK and MOSI pins as Input Floating */
GPIO_InitStructure.Pin = GPIO_PIN_13 | GPIO_PIN_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Input;
GPIO_InitStructure.GPIO_Alternate = GPIO_AF0_SPI2;
GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
/* Confugure MISO pin as Alternate Function Push Pull */
GPIO_InitStructure.Pin = GPIO_PIN_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
}
void RCC_Configuration(void)
{
/* PCLK2 = HCLK/2 */
RCC_ConfigPclk2(RCC_HCLK_DIV2);
/* Enable peripheral clocks --------------------------------------------------*/
/* GPIOA and GPIOB clock enable */
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA | RCC_APB2_PERIPH_GPIOB | RCC_APB2_PERIPH_AFIO , ENABLE);
/* SPI1 and SPI2 Periph clock enable */
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_SPI1 | RCC_APB2_PERIPH_SPI2, ENABLE);
}
int main(void)
{
/*!< At this stage the microcontroller clock setting is already configured,
this is done through SystemInit() function which is called from startup
file (startup_n32g43x.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_n32g43x.c file
*/
/* System clocks configuration ---------------------------------------------*/
RCC_Configuration();
/* GPIO configuration ------------------------------------------------------*/
GPIO_Configuration();
/* SPI1 configuration ------------------------------------------------------*/
SPI_InitStructure.DataDirection = SPI_DIR_DOUBLELINE_FULLDUPLEX;
SPI_InitStructure.SpiMode = SPI_MODE_MASTER;
SPI_InitStructure.DataLen = SPI_DATA_SIZE_16BITS;
SPI_InitStructure.CLKPOL = SPI_CLKPOL_LOW;
SPI_InitStructure.CLKPHA = SPI_CLKPHA_SECOND_EDGE;
SPI_InitStructure.NSS = SPI_NSS_SOFT;
SPI_InitStructure.BaudRatePres = SPI_BR_PRESCALER_8;
SPI_InitStructure.FirstBit = SPI_FB_MSB;
SPI_InitStructure.CRCPoly = 7;
SPI_Init(SPI1, &SPI_InitStructure);
/* SPI2 configuration ------------------------------------------------------*/
SPI_InitStructure.SpiMode = SPI_MODE_SLAVE;
SPI_Init(SPI2, &SPI_InitStructure);
/* Enable SPI1 CRC calculation */
SPI_EnableCalculateCrc(SPI1, ENABLE);
/* Enable SPI2 CRC calculation */
SPI_EnableCalculateCrc(SPI2, ENABLE);
/* Enable SPI1 */
SPI_Enable(SPI1, ENABLE);
/* Enable SPI2 */
SPI_Enable(SPI2, ENABLE);
/* Transfer procedure */
/* Transfer procedure */
while (TxIdx < BufferSize - 1)
{
/* Wait for SPI1 Tx buffer empty */
while (SPI_I2S_GetStatus(SPI1, SPI_I2S_TE_FLAG) == RESET)
;
while(1)
{
/* Send SPI2 data */
SPI_I2S_TransmitData(SPI2, SPI2_Buffer_Tx[TxIdx]);
/* Send SPI1 data */
SPI_I2S_TransmitData(SPI1, SPI1_Buffer_Tx[TxIdx++]);
if(TxIdx == 32)
{
TxIdx = 0;
}
}
/* Wait for SPI2 data reception */
while (SPI_I2S_GetStatus(SPI2, SPI_I2S_RNE_FLAG) == RESET)
;
/* Read SPI2 received data */
SPI2_Buffer_Rx[RxIdx] = SPI_I2S_ReceiveData(SPI2);
/* Wait for SPI1 data reception */
while (SPI_I2S_GetStatus(SPI1, SPI_I2S_RNE_FLAG) == RESET)
;
/* Read SPI1 received data */
SPI1_Buffer_Rx[RxIdx++] = SPI_I2S_ReceiveData(SPI1);
}
.....
.....
.....
}
我用示波器看了SPI1的SCK是没有时序信号的,是我哪里设置错了么?
谢谢
|