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:
- Declare a USART_HandleTypeDef structure
- Implement HAL_USART_MspInit()
- Enable USART & GPIO clocks
- Configure GPIOs
- Program the communication parameters in the USART_InitTypeDef
- 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:
USART_HandleTypeDef UsartHandle;
USART_InitTypeDef UsartInit;
UsartInit.BaudRate = 9600;
UsartInit.WordLength = USART_WORDLENGTH_8B;
UsartInit.StopBits = USART_STOPBITS_1;
UsartInit.Parity = USART_PARITY_NONE;
UsartInit.Mode = USART_MODE_TX_RX;
UsartHandle.Instance = USART6;
UsartHandle.Init = &UsartInit;
/* do I really have to init EVERY data field? */
HAL_USART_Init(&UsartHandle);
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:
UART_HandleTypeDef UartHandle;
UartHandle.Init.BaudRate = 9600;
UartHandle.Init.WordLength = UART_DATABITS_8;
UartHandle.Init.StopBits = UART_STOPBITS_1;
UartHandle.Init.Parity = UART_PARITY_NONE;
UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
UartHandle.Init.Mode = UART_MODE_TX_RX;
UartHandle.Init.Instance = USART1;
HAL_UART_Init(&UartHandle);
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
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.