cancel
Showing results for 
Search instead for 
Did you mean: 

[STM32F3] UART cannot print if handler is on !?

chenping
Associate II
Posted on April 07, 2015 at 05:31

As bright as the title suggests, I'm gonna post my setting over here~ Anyone please help me check it.. You have my utmost gratitude 🙂

My UART4_Handler is empty, there's only a STM_EVAL_LEDToggle(LED4) to check its condition.

void UARTInit(USART_TypeDef* USARTx, unsigned Handler)

{

    /* Configure UARTx pins:  Rx and Tx */

    if(USARTx == UART4){

        GPIOSET(GPIOC, alternate, 10,11, stop);

        GPIO_PinAFConfig(GPIOC, GPIO_PinSource10,  GPIO_AF_5); /*TX*/

        GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_5); /*RX*/

        NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn ;

        USART_ITConfig(USARTx, USART_IT_RXNE, Handler);

        //NVIC_EnableIRQ(UART4_IRQn);

    }else if(USARTx == UART5){

        GPIOSET(GPIOD, alternate, 2, stop);

        GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_5); /*RX*/

        GPIOSET(GPIOC, alternate, 12, stop);

        GPIO_PinAFConfig(GPIOC, GPIO_PinSource12,  GPIO_AF_5); /*TX*/

        NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn ;

        USART_ITConfig(USARTx, USART_IT_RXNE, Handler);

    }else if (USARTx == USART1){

        GPIOSET(GPIOC, alternate, 4, 5, stop);

        GPIO_PinAFConfig(GPIOC, GPIO_PinSource4, GPIO_AF_7); /*TX*/

        GPIO_PinAFConfig(GPIOC, GPIO_PinSource5, GPIO_AF_7); /*RX*/

        NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn ;

        USART_ITConfig(USARTx, USART_IT_RXNE, Handler);

    }else if (USARTx == USART2){

        GPIOSET(GPIOD, alternate, 5, 6, stop);

        GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_7); /*TX*/

        GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_7); /*RX*/

        NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn ;

        USART_ITConfig(USARTx, USART_IT_RXNE, Handler);

    }else if (USARTx == USART3){

        GPIOSET(GPIOB, alternate, 10, 11, stop);

        GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_7); /*TX*/

        GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_7); /*RX*/

        NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn ;

        USART_ITConfig(USARTx, USART_IT_RXNE, Handler);

    }

         /* Configure UARTx settings */

     // USART_StructInit(&USART_InitStructure);

        USART_InitStructure.USART_BaudRate = baudrate;

        USART_InitStructure.USART_WordLength = USART_WordLength_8b;

        USART_InitStructure.USART_StopBits = USART_StopBits_1;

        USART_InitStructure.USART_Parity  = USART_Parity_No;

        USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

        USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

        USART_Init(USARTx, &USART_InitStructure);

        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

        NVIC_InitStructure.NVIC_IRQChannelCmd = Handler;

        NVIC_Init(&NVIC_InitStructure);

        USART_Cmd(USARTx , ENABLE);             // Enable USART1

//        NVIC_EnableIRQ(USART1_IRQn);            /*!< USART1 global Interrupt*/ /*havent test */

}
1 REPLY 1
chenping
Associate II
Posted on April 07, 2015 at 06:36

Another version of UART interrupt setting

void UARTInit (u8 uart, unsigned Handler)

{

    /* Configure UART4 pins:  Rx and Tx */

    GPIO_StructInit(&GPIO_InitStructure);

    if(uart == 1)

        GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_4 | GPIO_Pin_5;

    else if(uart == 2)

        GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_5 | GPIO_Pin_6;

    else if(uart == 3)

        GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_10 | GPIO_Pin_11;

    else if(uart == 4)

        GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_10 | GPIO_Pin_11;

    else if(uart == 5)

        GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_2;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

    if(uart == 1){

        GPIO_Init(GPIOC, &GPIO_InitStructure);

        GPIO_PinAFConfig(GPIOC, GPIO_PinSource4,  GPIO_AF_7); /*TX*/

        GPIO_PinAFConfig(GPIOC, GPIO_PinSource5, GPIO_AF_7); /*RX*/

        NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn ;

        USART_ITConfig(USART1, USART_IT_RXNE, Handler);

    }else if(uart == 2){

        GPIO_Init(GPIOD, &GPIO_InitStructure);

        GPIO_PinAFConfig(GPIOD, GPIO_PinSource5,  GPIO_AF_7); /*TX*/

        GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_7); /*RX*/

        NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn ;

        USART_ITConfig(USART2, USART_IT_RXNE, Handler);

    }else if(uart == 3){

        GPIO_Init(GPIOB, &GPIO_InitStructure);

        GPIO_PinAFConfig(GPIOB, GPIO_PinSource10,  GPIO_AF_7); /*TX*/

        GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_7); /*RX*/

        NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn ;

        USART_ITConfig(USART3, USART_IT_RXNE, Handler);

    }else if(uart == 4){

        GPIO_Init(GPIOC, &GPIO_InitStructure);

    /* Configure Alternate Function pin muxing fabric to escape USART3 Rx and Tx */

        GPIO_PinAFConfig(GPIOC, GPIO_PinSource10,  GPIO_AF_5); /*TX*/

        GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_5); /*RX*/

        NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn ;

        USART_ITConfig(UART4, USART_IT_RXNE, Handler);

    }else if(uart == 5){

        GPIO_Init(GPIOD, &GPIO_InitStructure);

        GPIO_PinAFConfig(GPIOD, GPIO_PinSource2,  GPIO_AF_5); /*TX*/

        GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_12;

        GPIO_Init(GPIOC, &GPIO_InitStructure);

        GPIO_PinAFConfig(GPIOC, GPIO_PinSource12,  GPIO_AF_5); /*TX*/

        NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn ;

        USART_ITConfig(UART5, USART_IT_RXNE, Handler);

    }

    USART_InitStructure.USART_BaudRate = 115200;

    USART_InitStructure.USART_WordLength = USART_WordLength_8b;

    USART_InitStructure.USART_StopBits = USART_StopBits_1;

    USART_InitStructure.USART_Parity  = USART_Parity_No;

    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

    if(Handler == ENABLE){

        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

        NVIC_InitStructure.NVIC_IRQChannelCmd = Handler;

        NVIC_Init(&NVIC_InitStructure);

    }

    if(uart == 1){

        USART_Init(USART1, &USART_InitStructure);

        USART_Cmd(USART1, ENABLE); // Enable UART4

    }else if(uart == 2){

        USART_Init(USART2, &USART_InitStructure);

        USART_Cmd(USART2, ENABLE); // Enable UART4

    }else if(uart == 3){

        USART_Init(USART3, &USART_InitStructure);

        USART_Cmd(USART3, ENABLE); // Enable UART4

    }else if(uart == 4){

        USART_Init(UART4, &USART_InitStructure);

        USART_Cmd(UART4, ENABLE); // Enable UART4

    }else if(uart == 5){

        USART_Init(UART5, &USART_InitStructure);

        USART_Cmd(UART5, ENABLE); // Enable UART4

    }

}