0

I am working with the following evaluation board: SZWB-sail, STM32f103VET6 KIT v3.1

I want to use the stm32f103 usart in synchronous mode, and I used STM32F10x_StdPeriph_Lib_V3.5.0\Project\STM32F10x_StdPeriph_Examples\USART\Synchronous

I modified the code in an attempt to use USART2/SPI1, as opposed to the working code provided via STMicro, which uses USART1/SPI1.

The README for this example states that:

"USARTy and SPIy can be USART1 and SPI1 or USART2 and SPI3, depending on the STMicroelectronics EVAL board you are using."

Despite this, I attempted to physically connect USART2 Tx/Rx/Ck pins (PA2,PA3,PA4) to SPI1 SCK/MISO/MOSI (PA5,PA6,PA7). Is there a software reason for why this won't work? Or might it be hardware connections on the eval board?

Here is my code:

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);
}
4

2 回答 2

1

您正在混合轮询模式和中断模式。此 SPI 配置代码适用于 SPI 中断模式。因此,SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE)不应使用,因为这是用于轮询模式的功能。

相反,我相信您可以与andSPI_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_Send_Byte()代码,以便我们知道它到底在做什么,以及它是否正在调用任何其他函数......但是请先尝试一下,看看它是否能解决您的问题。

于 2014-07-14T14:13:39.907 回答
0

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

于 2020-12-15T07:06:34.293 回答