stm32中usart的C同步模式(spi模式)



我正在使用以下评估板:SZWB-sail, STM32f103VET6 KIT v3.1

我想在同步模式下使用stm32f103 usart,我使用了STM32F10x_StdPeriph_Lib_V3.5.0 STM32F10x_StdPeriph_Examples USART 项目同步

我修改了代码,试图使用USART2/SPI1,而不是通过STMicro提供的工作代码,它使用USART1/SPI1

这个示例的README声明:

"USARTy和SPIy可以是USART1和SPI1或USART2和SPI3,具体取决于您使用的意法半导体EVAL板。"

尽管如此,我试图物理连接USART2 Tx/Rx/Ck引脚(PA2,PA3,PA4)到SPI1 SCK/MISO/MOSI (PA5,PA6,PA7)。是否有软件原因导致这不起作用?或者可能是eval板上的硬件连接?

下面是我的代码:
int main(void)
{
    SystemInit();
    Init_NVIC();
    /* System Clocks Configuration */
    RCC_Configuration();
    /* Configure the GPIO ports */
    GPIO_Configuration();
    SPI_Configuration();
    USART_Configuration();

    while(NbrOfDataToRead2--)
    {
        USART2_Send_Byte(TxBuffer1[TxCounter1++]);
        while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET)
        {
        }
        while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET)
        {
        }
        RxBuffer2[RxCounter2++] = SPI_I2S_ReceiveData(SPI1);
    }

    USART2_Receive_Byte();
    while(NbrOfDataToRead1--)
    {
        while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)== RESET)
        {
        }
        SPI_I2S_SendData(SPI1, TxBuffer2[TxCounter2++]);

        USART2_Send_Byte(DYMMY_BYTE);
        while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET)
        {
        }
        while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET)
        {
        }
        RxBuffer1[RxCounter1++] = USART2_Receive_Byte();
    }

    TransferStatus1 = Buffercmp(TxBuffer1, RxBuffer2, TxBufferSize1);
    TransferStatus2 = Buffercmp(TxBuffer2, RxBuffer1, TxBufferSize2);

    while(1)
    {
    }
}
void Init_NVIC(void)
{
    NVIC_InitTypeDef    NVIC_InitStructure;
    #ifdef  VECT_TAB_RAM
    NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
    #else
    NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
    #endif
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}
void RCC_Configuration(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE );
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 , ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 , ENABLE);
}
void GPIO_Configuration(void)
{
    GPIO_InitTypeDef GPIO_InitStructure1,GPIO_InitStructure2;
    GPIO_InitTypeDef  GPIO_InitStructure3,GPIO_InitStructure4,GPIO_InitStructure5,GPIO_InitStructure6;
    /* Configure USART1 Rx as input floating */
    GPIO_InitStructure1.GPIO_Pin =GPIO_Pin_10;
    GPIO_InitStructure1.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure1.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure1);
    /* Configure USART1 Tx as alternate function push-pull */
    GPIO_InitStructure2.GPIO_Pin =GPIO_Pin_9;
    GPIO_InitStructure2.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructure2);
    /* Configure USART2 Rx as input floating */
    GPIO_InitStructure3.GPIO_Pin =GPIO_Pin_3;
    GPIO_InitStructure3.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure3.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure3);
    /* Configure USART2 Tx as alternate function push-pull */
    GPIO_InitStructure4.GPIO_Pin =GPIO_Pin_2;
    GPIO_InitStructure4.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure4.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructure4);
    /* Configure USART2 Ck as alternate function push-pull */
    GPIO_InitStructure5.GPIO_Pin = GPIO_Pin_4;
    GPIO_InitStructure5.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure5.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure5);
    /* Configure SPI1 pins: SCK, MISO and MOSI */
    GPIO_InitStructure6.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
    GPIO_Init(GPIOA, &GPIO_InitStructure6);
}
void USART_Configuration(void)
{
    USART_InitTypeDef USART_InitStructure1,USART_InitStructure2;
    USART_ClockInitTypeDef USART_ClkInitStructure;
    USART_InitStructure1.USART_BaudRate = 115200;
    USART_InitStructure1.USART_WordLength =USART_WordLength_8b ;
    USART_InitStructure1.USART_StopBits = USART_StopBits_1;
    USART_InitStructure1.USART_Parity = USART_Parity_No;
    USART_InitStructure1.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure1.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    /* Configure USART1 */
    USART_Init(USART1,&USART_InitStructure1);
    //USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
    /* Enable the USART1 */
    USART_Cmd(USART1,ENABLE);
    USART_ClkInitStructure.USART_Clock=USART_Clock_Enable;
    USART_ClkInitStructure.USART_CPOL=USART_CPOL_High;
    USART_ClkInitStructure.USART_CPHA=USART_CPHA_2Edge;
    USART_ClkInitStructure.USART_LastBit=USART_LastBit_Enable;
    USART_ClockInit(USART2, &USART_ClkInitStructure);
    USART_InitStructure2.USART_BaudRate = 115200;
    USART_InitStructure2.USART_WordLength =USART_WordLength_8b ;
    USART_InitStructure2.USART_StopBits = USART_StopBits_1;
    USART_InitStructure2.USART_Parity = USART_Parity_No;
    USART_InitStructure2.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure2.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    /* Configure USART2 */
    USART_Init(USART2,&USART_InitStructure2);
    //USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
    /* Enable the USART2 */
    USART_Cmd(USART2,ENABLE);
}
void SPI_Configuration(void)
{
    SPI_InitTypeDef SPI_InitStructure;
    SPI_StructInit(&SPI_InitStructure);
    SPI_I2S_DeInit(SPI1);
    /* SPIy Config */
    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
    SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
    SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
    SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;
    /* Configure SPIy */
    SPI_Init(SPI1, &SPI_InitStructure);
    SPI_I2S_ITConfig(SPI1,SPI_I2S_IT_RXNE,ENABLE);
    /* SPIy enable */
    SPI_Cmd(SPI1, ENABLE);
}

您正在将轮询模式与中断模式混合。这个SPI配置代码用于SPI中断模式。因此,不应该使用SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE),因为这是用于轮询模式的函数。

相反,我相信您可以将SPI_I2S_GetITStatus(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT);SPI_I2S_ReceiveData(SPI_TypeDef* SPIx)SPI_I2S_ClearITPendingBit(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT)结合使用(以清除任何潜在的Error pending bits,以防万一)。

另外,你可能想发布你的USART2_Send_Byte()代码,这样我们就知道它到底在做什么,以及它是否调用任何其他函数…但请先尝试一下,看看它是否能解决您的问题。

SPI1与USART2同步模式冲突。SPI2与USART3同步方式冲突。SPI1/2和USART1同步模式不冲突

最新更新