Uart Driver C Code Syntax

Leader Board Leading Today Pts Helpful 1. How to wipe bb passport. 200 84% Leading this Month Pts Helpful 1. 200 84% Leading this Week Pts Helpful 1.

Active1 year, 5 months ago

A C demonstration program that provides examples of how to invoke the UART Wildcard driver functions. The ANSI C version of the demonstration program source code. A C demonstration program that provides examples of how to invoke the UART Wildcard driver functions. This section presents the ANSI C version of the demonstration program source code. (Universal Asynchronous Receiver Transmitter) Wildcard provides two full-duplex serial ports that can be configured for RS232, RS422, and RS485 protocols.

I am learning about STM32 programming and trying to implement simple asynchronous serial communication using USART peripheral on GPIO pins.

The HAL manual describes how to use HAL USART drivers:

  1. Declare a USART_HandleTypeDef structure
  2. Implement HAL_USART_MspInit()
    • Enable USART & GPIO clocks
    • Configure GPIOs
  3. Program the communication parameters in the USART_InitTypeDef
  4. Call HAL_USART_Init()


As I wrote my code, I declared the USART_HandleTypeDef, instinctively filled my USART_InitTypeDef structure and started to fill the HandleTypeDef:


I then noticed that there's many data fields to fill. Referring to code examples in the manual and on the web, I noticed nobody actually defines all of the USART_HandleTypeDef fields - they somehow combine the HandleTypeDef and InitTypeDef in one step, like this:


How does this work? What part of C syntax do I have to learn, to understand where did that UartHandle.Init.xxx come from?Is it possible to do it 'the long way', as I planned to? If I don't fill every datafield of HandleTypeDef, where do they get initialized?

PS. I am not using STM32 recommended IDEs or CubeMX, working on Linux, using PlatformIO. Board: STM32F746 discovery kit

We’ve provided the very latest edition, which we pull directly from Florida Highway Safety and Motor Vehicles, so you have the most current information to study as you prepare for getting your driver’s permit. https://luckyharmony.netlify.app/florida-driver39s-manual-in-chinese.html. Visit this page and study the handbook as often as you need to, using the easy navigation controls, until you feel comfortable with all the materials.

PPS. I am really unsure whether to put this question here or on electronics stack. Please correct me or move the question there if it's not suitable for this stackexchange.

Jurc192Jurc192

1 Answer

How does this work? What part of C syntax do I have to learn, to understand where did that UartHandle.Init.xxx come from?

This is basic C struct syntax. The USART_HandleTypeDef struct contains an instance of a USART_InitTypeDef struct named Init. You can think of it as a nested struct. You can reference the members of nested structs with repeated '.'s. Note that, the Init member is NOT a pointer to a USART_InitTypeDef struct. It's literally a complete USART_InitTypeDef instance contained within a USART_HandleTypeDef instance.

C++ Compiler

Is it possible to do it 'the long way', as I planned to?

Yes, except your code contains an error. You need to make the assignment like this.

Remember that the Init member of USART_HandleTypeDef is not a pointer but a complete struct. Therefore you need to assign it with a complete struct, not a pointer.

Viper1 Map updates can be purchased (software updates are free) from the Magellan website (MagellanGPS.com > Products > Maps and Software, as of Jan 20, 2010). Download magellan spring 2010 map upgrade v395. The latest update available right now is the 2009 Spring Map Update, available on an SD card for $80.

Code

But realize that when you define your UsartInit variable, you are allocating space for an instance of the struct. If UsartInit is a function local variable then that space is likely on the stack. Your initialization statements are initializing your copy of the struct. Then when you assign UsartInit to UsartHandle.Init the compiler creates code that will copy the entire contents of the struct. After the copy, if your UsartInit is a local variable, it will go out of scope and be deallocated.

It's really not necessary to define and allocate space for your own USART_InitTypeDef struct and then copy the entire struct into UsartHandle.Init. UsartHandle already contains space allocated for its USART_InitTypeDef member. So it's more efficient to simply initialize the UsartHandle.Init member directly, like the ST example code does.

If I don't fill every datafield of HandleTypeDef, where do they get initialized?

You don't need to fill every datafield of USART_HandleTypeDef. Refer to the HAL Reference Manual to learn what you need to initialize. You probably need to initialize only the Instance and Init members. The remaining members are used internally by the HAL USART driver and they will be initialized and used by the driver functions (you can think of them as private variables if that helps). The designer of the API named that struct member 'Init' as a cue to you that this is what you need to initialize. The ST example code provides further evidence of what you need to initialize.

[Several experienced developers on Stack Overflow advise against using the ST HAL and encourage people to develop their own drivers based on the device's Reference Manual. Realize that these developers have years of experience, they've worked with a variety of microcontroller families and peripherals, and they're capable of understanding the Reference Manual and writing drivers from scratch. I agree that the ST HAL adds some bloat that might be detrimental to some applications. But I disagree that beginners should avoid using the ST HAL. The ST HAL works well enough for many applications and it is easier for beginners to use than writing their own drivers from scratch (especially given the many examples provided with the HAL).]

Uart Driver C Code Syntaxkkrambo

C++ Code Examples

kkrambo
5,0041 gold badge11 silver badges22 bronze badges
C++ compiler
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.

Not the answer you're looking for? Browse other questions tagged csyntaxembeddedstm32hal or ask your own question.