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

C stm32中usart的同步模式(spi模式),c,synchronous,stm32,spi,usart,C,Synchronous,Stm32,Spi,Usart,我正在使用以下工具:SZWB sail,STM32f103VET6工具包v3.1 我想在同步模式下使用stm32f103 usart,我使用了 STM32F10x\u StdPeriph\u Lib\u V3.5.0\Project\STM32F10x\u StdPeriph\u Examples\USART\Synchronous 我修改代码是为了尝试使用USART2/SPI1,而不是使用USART1/SPI1的STMicro提供的工作代码 本示例的示例说明: USARTy和SPIy可以是US

我正在使用以下工具:SZWB sail,STM32f103VET6工具包v3.1

我想在同步模式下使用stm32f103 usart,我使用了 STM32F10x\u StdPeriph\u Lib\u V3.5.0\Project\STM32F10x\u StdPeriph\u Examples\USART\Synchronous

我修改代码是为了尝试使用
USART2
/
SPI1
,而不是使用
USART1
/
SPI1
的STMicro提供的工作代码

本示例的示例说明:

USARTy和SPIy可以是USART1和SPI1,也可以是USART2和SPI3,具体取决于您使用的STMicroelectronics评估板

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

这是我的密码:

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)
(以清除任何潜在的错误挂起位,以防万一)


另外,您可能希望发布
USART2\u Send\u Byte()
代码,以便我们知道它到底在做什么,以及它是否调用任何其他函数……但是请先尝试一下,看看它是否解决了您的问题

SPI1与USART2同步模式冲突。 SPI2与USART3同步模式冲突。
SPI1/2和USART1同步模式之间没有冲突。

您是否检查了示波器或逻辑分析仪生成的信号?这将允许您知道问题是在发送方还是接收方。然后先尝试不使用中断,然后在工作时启用它们。谢谢您的回复。是的,我查过了。我认为问题出在接收器上,因为SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE)从未更改,并且总是重置,当我启用SPI接收中断时,它永远不会工作。我是否忘记了代码中的某些内容???谢谢,我在上面更改了代码,它为USART1和SPI1工作。但是当我把它改成USART2时,它就不起作用了!!!!你知道为什么吗?我在上面解释了更多关于我的变化。根据我的代码,我认为APB1和APB2中存在问题。对吗?请不要在回答第一个问题后编辑第一个问题来提出新问题。如果这个答案回答了你的第一个问题,你应该接受它。(左边的绿色箭头)您仍然有
USART\u ITConfig(USART\u IT\u RXNE,ENABLE)
USART\u ITConfig(USART2,USART\u IT\u RXNE,启用)注释掉。如果您让USART1工作,那么我将创建两个版本的代码:一个是删除所有USART2引用/代码,另一个是USART1的等效版本,这样您就不会混淆配置过程。然后,一旦充分发挥作用,从那里开始整合两者。@Étienne我没有问新问题,我的问题仍然没有解决。我在SPI和Usart之间的交流中遇到了问题,Jesse的答案很有用,但并没有解决我的问题,所以我使用+1,这意味着答案很有用。我仍然无法与SPI1和USART2进行通信。你能解释一下你的答案吗