cancel
Showing results for 
Search instead for 
Did you mean: 

Weird problem with UART

I work with STM32L475.

First I configure UART

int main(void)
{
    /* Configure the system clock to 80 MHz */
    SystemClock_Config();
    GPIO_Setup();
 
    USART_Setup(UART4, 115200);
 
    USART_SendInt(UART4, 1234, 1);
    USART_SendInt(UART4, 0, 1);
 
    while (1)
    {
       if (uart4_rx_ready)
        {
            uart4_rx_ready = 0;
            PARSER_ParseCommand(uart4_rx_buf);
        }
    }
}

And my print functions

void USART_SendString(USART_TypeDef *USARTx, const char *string)
{
    uint32_t timeout;
    
    while (*string)
    {
        timeout = USART_TIMEOUT;
        while ( (USARTx->ISR & USART_ISR_TXE) == 0) {  if(!timeout--) break; }
       
        USARTx->TDR = *string++; 
    }
}
 
void USART_SendInt(USART_TypeDef *USARTx, int num, int cr)
{
    uint32_t timeout;
    char str[10]; 
    int i = 0;
    uint32_t sign = 0;
   
    if (num<0)
    {
	num *= -1;
	sign = 1;
    }
    
    do str[i++] = num % 10 + '0'; while ((num /= 10) > 0);
   
    if (sign)
        USART_SendString(USARTx,"-"); 
    
    for (i--; i >= 0; i--)
    {
	timeout = USART_TIMEOUT;
	while( !(USARTx->ISR & USART_ISR_TXE) ) {  if(!timeout--) break; }
	USARTx->TDR = str[i];
    }
   
    if (cr)
        USART_SendString(USARTx, "\r");
}

 USART_SendString always works good. USART_SendInt - works only at startup

 USART_SendInt(SYS_USART, 1234, 1);

 USART_SendInt(SYS_USART, 0, 1);

prints me

1234

0

Then in run time if I invoke USART_SendInt - it doesn't print.

With other chips, like STM32F3xx USART_SendInt works without problem.

4 REPLIES 4
Danish1
Lead II

1) How strongly do you know you're actually calling USART_SendInt() "at run-time" on the L4?

2) What does it actually do? Print nothing? Get stuck? How strongly do you know that uart4_rx_ready gets set?

3) Can you attach a debugger to see what's going on e.g.by placing breakpoints? Or by driving LEDs at specific points in your code?

Remember that there are subtle differences between the different stm32 families, in terms of how their pins are arranged, their port alternate-functions are routed and many other things.

Hope this helps,

Danish

This is what I found

I stop the break point at

USARTx->TDR = str[i];

At startup I see the USART address - 0x40004C00, then if I call the function in run time it becomes 0x40013800, but if I call USART_SendString - it switches to 0x40004C00.

The first argument of both functions the same

#define SYS_USART UART4

USART_SendString (SYS_USART, "Hello\r");

USART_SendInt (SYS_USART, 1234, 1);

This sounds that you link objects (.o) built for a different STM32 model.

JW

In the run time?

Well....did Clean Build and it helped. Thank you.