簡體   English   中英

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

[英]synchronous mode of usart(spi mode) in stm32

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

我想在同步模式下使用stm32f103 usart,我用的是STM32F10x_StdPeriph_Lib_V3.5.0\\Project\\STM32F10x_StdPeriph_Examples\\USART\\Synchronous

我修改了代碼以嘗試使用USART2 / SPI1 ,而不是通過 STMicro 提供的工作代碼,它使用USART1 / SPI1

此示例的自述文件指出:

“USARTy 和 SPIy 可以是 USART1 和 SPI1 或 USART2 和 SPI3,具體取決於您使用的 STMicroelectronics EVAL 板。”

盡管如此,我還是嘗試將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_Send_Byte()代碼,以便我們知道它究竟在做什么,以及它是否正在調用任何其他函數……但是請先嘗試一下,看看它是否能解決您的問題。

SPI1 與 USART2 同步模式沖突。 SPI2 與 USART3 同步模式沖突。 SPI1/2 和 USART1 同步模式之間沒有沖突。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM