Skip to main content
rbenesh
Associate II
October 21, 2015
Question

HAL UART issue

  • October 21, 2015
  • 4 replies
  • 1312 views
Posted on October 21, 2015 at 18:36

I tested the HAL function inside of main using something like this:

  uint8_t Heading[]=''\n\r Send some text\n\r'';

  if(HAL_UART_Transmit(&UartHandle, (uint8_t *)Heading, sizeof Heading -1, 0xFFFF) != HAL_OK)

{

    Error_Handler();

}

This implementation worked great.

However, I wanted to use the function call within a secondary function so I didn't have to write out the whole thing over and over throughout the code. So I created this:

void Usart_Send_String (uint8_t *str)

{

if(HAL_UART_Transmit(&UartHandle, str, sizeof str -1, 0xFFFF) != HAL_OK)

{

   Error_Handler(Err_Tx);

}

} /* end of function Usart_Send_String */

When I use the following, it stops working:

  uint8_t Heading[]=''\n\r Send some text\n\r'';

Usart_Send_String(Heading);

I've tried changing the pointers and such but not much luck. Am I missing something? There isn't much documentation to the use of the HAL.

I'm used to the old STDFWLIB and USART_SendData used to work on prior systems. 
    This topic has been closed for replies.

    4 replies

    Tesla DeLorean
    Guru
    October 21, 2015
    Posted on October 21, 2015 at 19:01

    I've tried changing the pointers and such but not much luck. Am I missing something? There isn't much documentation to the use of the HAL.

     

    What do you think sizeof str does? How many bytes does a 32-bit pointer use?

    For a string use strlen(), this is C stuff.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    rbenesh
    rbeneshAuthor
    Associate II
    October 21, 2015
    Posted on October 21, 2015 at 20:55

    I reviewed the example again.

    I saw that COUNTOF(aTxBuffer) - 1 was how the size was calculated in the example where COUNTOF is defined as 

    #define COUNTOF(__BUFFER__)   (sizeof(__BUFFER__) / sizeof(*(__BUFFER__)))

    So it essentially does calculate the overall buffer length using a macro.

     Odd that ''sizeof'' worked fine outside of the function though, not within.

     Using COUNTOF within the function didn't work when I retried it within the function but, typing in the actual length of the string manually did. So now I know where my issue is occurring.

    strlen() requires a const char. The HAL requires a length as an unsigned short integer. This would require a cast to make this work.

    Tesla DeLorean
    Guru
    October 21, 2015
    Posted on October 21, 2015 at 21:49

    sizeof() is a compile time operation, with a one-to-one measurement of a specific object. You can't apply it to random parameters passed to a subroutine, with data from multiple sources. You could pass the size as a second parameter, ie foo(bar, sizeof(bar));

    Yes, if you have different types you might have to cast them, strlen() specifies a const type because it only reads the characters, it doesn't not change anything.

    In the function sizeof(str) is going to tell you how big an (char *) pointer is, as that's the type it's being used on.
    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    rbenesh
    rbeneshAuthor
    Associate II
    October 23, 2015
    Posted on October 23, 2015 at 15:49

    Yes. That explains it. Thank you sir.