2

I am trying to enable UART for STM32 Nucleo - F429ZI. I have gone through user manual and it says by default USART 3 can be configured for virtual com port purpose. Here is my code. I can see that USART 3 has pins D8 and D9. Here is my code. What am I doing wrong here, I don't see prints on my com port.

Data sheet refered - https://www.st.com/resource/en/data_brief/nucleo-f429zi.pdf https://www.st.com/resource/en/user_manual/dm00244518-stm32-nucleo-144-boards-stmicroelectronics.pdf

//test print msg
char usr_msg[256];
//Ends

void printmsg(char *msg)
{
    for(uint32_t i = 0; i < strlen(msg); i++)
    {
        while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) != SET);
        USART_SendData(USART3, msg[i]);
    }
}

    static void prvSetupUart(void)
    {
        GPIO_InitTypeDef gpio_uart_pins;
        USART_InitTypeDef uart3_init;
    
        //GPIO D is connected to AHB1 bus.
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
    
        // Enable the UART 3 peripheral clock -- connected to APB1 bus.
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
    
        // zero the local variable
        memset(&gpio_uart_pins,0,sizeof(gpio_uart_pins));
        memset(&uart3_init,0,sizeof(uart3_init));
        // GPIO port D - pin 8 like TX
        // GPIO port D - ping 9 like RX
        gpio_uart_pins.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
        gpio_uart_pins.GPIO_Mode = GPIO_Mode_AF; //AF - Alternate function i.e. TX and RX.
        gpio_uart_pins.GPIO_PuPd = GPIO_PuPd_UP; //Pull up so that we see some default voltage.
    
        GPIO_Init(GPIOD, &gpio_uart_pins);
    
        uart3_init.USART_BaudRate = 115200;
        uart3_init.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
        uart3_init.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
        uart3_init.USART_Parity = USART_Parity_No;
        uart3_init.USART_StopBits = USART_StopBits_1;
        uart3_init.USART_WordLength = USART_WordLength_8b;
        USART_Init(USART3,&uart3_init);
        //Enable USART 3 peripheral.
        USART_Cmd(USART3, ENABLE);
    
    }

    int main(void)
    {
        //Step number 1 - DeInit the RCC - Reset Control Clock so that we don't use Pre-scaler or PLL.
        // Basically, we want to use lower speed.
        RCC_DeInit();
        // update the System Core Clock
    
        //Step number 2 -- Update the clock to use the default clock.
        SystemCoreClockUpdate();
    
        prvSetupUart();
    
        sprintf(usr_msg, "This is Hello World\r\\n");
    
        printmsg(usr_msg);
    
        for(;;);
    }
tannoy connect
  • 185
  • 4
  • 14

1 Answers1

2

you forgot to set what AF mode to be used (there are possible 16 AF modes for every pin). At the moment AF registers are set to the 0 and it is wrong. You need to make it 7.

enter image description here

How can it be archived using the SPL library (depreciated and not supported anymore) I do not know.

Here is the register version

#define GPIO_AFRL_AFRL0_Msk     (GPIO_AFRL_AFRL0_0 | GPIO_AFRL_AFRL0_1 | GPIO_AFRL_AFRL0_2 | GPIO_AFRL_AFRL0_3)

void GPIO_SetAF(GPIO_TypeDef *gpio, unsigned pin, unsigned AF)
{
    volatile uint32_t *AFreg = &gpio -> AFR[pin >= 8];

    if(AF <= 15)
    {
        if(pin > 7) pin -= 8;

        *AFreg &= ~(GPIO_AFRL_AFRL0_Msk << (4 * pin));
        *AFreg |= (AF << (4 * pin));
    }
}
0___________
  • 60,014
  • 4
  • 34
  • 74
  • It worked, I just added two lines after GPIO initialization - GPIO_PinAFConfig(GPIOD,GPIO_PinSource8 , GPIO_AF_USART3); GPIO_PinAFConfig(GPIOD,GPIO_PinSource9 , GPIO_AF_USART3); – tannoy connect Jun 25 '20 at 21:05