cancel
Showing results for 
Search instead for 
Did you mean: 

regarding the reference manual insufficient data about initialization

vasikarans
Associate II

I am working on register level programming, and I have gone thoroughly the reference manual it just contains about the features of the peripherals and registers and something about its working so how can I program if it does not contain the procedure for initializing and setting up the peripherals for usage if it is present tell me which document contains it so it will be very helpful.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Garnett.Robert
Senior III

Hi,

 

Use CubeMX to create a project with the processor and peripherals you need.  Open it up in your IDE I use STM32CubeIDE.

You can then step through the startup code, peripheral initialisations etc.

This is a good way to understanding the system functions and stepping through the HAL or LL drivers will let you see how registers are set up.  You can also enter the dis-assembler and step through the machine code.

Keep the MCU reference manual handy open at the section on the relevant peripherals as you go.

 

When I want to tackle a new peripheral I go into CubeMX and search for examples.  I then build the system and look at how the peripheral is used.  There are a lot of useful examples.

Regards

Rob

 

View solution in original post

8 REPLIES 8
Issamos
Lead II

Hello @vasikarans 

Personally, I always use the LL_exemples of the Stm32Cubexx drive to find out exactly how to initialise and configure each  peripheral.

Best regards.

II

Foued_KH
ST Employee

Hello @vasikarans , 

Each peripheral chapter of the Reference Manual contains a functional description.
And for more details, you can check the Application Note.

Foued

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

@Foued_KH I have tried looking to application notes so for example if I want to configure GPIO I looked at as you said at application note but I could not find related topic to configuration, but it contains an application note as named "STM32 microcontroller GPIO hardware settings
and low-power consumption" 
but it does not contain the configuration process. explain me how to proceed. 

have you checked the Reference Manual ? 

Foued

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Garnett.Robert
Senior III

Hi,

 

Use CubeMX to create a project with the processor and peripherals you need.  Open it up in your IDE I use STM32CubeIDE.

You can then step through the startup code, peripheral initialisations etc.

This is a good way to understanding the system functions and stepping through the HAL or LL drivers will let you see how registers are set up.  You can also enter the dis-assembler and step through the machine code.

Keep the MCU reference manual handy open at the section on the relevant peripherals as you go.

 

When I want to tackle a new peripheral I go into CubeMX and search for examples.  I then build the system and look at how the peripheral is used.  There are a lot of useful examples.

Regards

Rob

 

YES  as you can see the GPIO content has introduction, features, functional description, registers but is does not contain initialization procedure.

vasikarans_0-1695300783749.png

 

TDK
Guru

This section of the RM tells you how to configure pins for the desired mode:

TDK_0-1695303528041.png

There is also Table 114. Port bit configuration which has additional details.

If you feel a post has answered your question, please click "Accept as Solution".
Garnett.Robert
Senior III

What app are you using that generated the menu you have posted? Is that the reference manual?

The reference manual doesn't have a step by step process for GPIO initialisation because it is trivial.  You basically have to enable the clock, and set up the registers as specified in  the GPIO register section.  For more complicated peripherals the ref manual does give broad step by step procedures, but not detail other than the register description, but to see how its done in detail try using CubeMX to generate a project and look at the code.

That is how I learned.  The reference manual is not a coding manual, it's about hardware.  Have you access to a computer and a nucleo or discovery that you can set up to build a system?

 

Here is a CubeMX tutorial on YT: CubeMX Getting started 

And another: CubeIDE 

I have just been playing with an STM32 Nucleo-32 development board with STM32G431KB MCU, :it only costs $19 Australian, has a170 MHz speed limit and some kick-ass peripherals, I think it even makes coffee.  It has an on-board st-link debugger with SWI. I'm using this processor in an industrial project for electrical relay monitoring,

Here is an initalisation procedure for GPIO

 

 

 

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    gpio.c
  * @brief   This file provides code for the configuration
  *          of all used GPIO pins.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2023 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "gpio.h"

/*----------------------------------------------------------------------------*/
/* Configure GPIO                                                             */
/*----------------------------------------------------------------------------*/
/** Configure pins
     PA2   ------> USART2_TX
     PA3   ------> USART2_RX
*/
void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);

  /*Configure GPIO pins : PAPin PAPin */
  GPIO_InitStruct.Pin = USART2_TX_Pin|USART2_RX_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /*Configure GPIO pin : PtPin */
  GPIO_InitStruct.Pin = LD2_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);

}

 

 

 

This code is called in main.c as part of the startup.

 

 

 

  /* Initialize all configured peripherals */
  MX_GPIO_Init(); /* Init GPIO */
  MX_DMA_Init();
  MX_ADC1_Init();
  MX_USART2_UART_Init();

 

 

 

This code was generated by CubeMX.

As you can see it is bog-standard C, not C++.  Basically if you use the HAL drivers they all have a structure that maps the peripherals registers and parameters. These are called handles.  The procedure is: You create a handle of the correct type for that peripheral, fill out the fields then call the appropriate initialization function. This function calls another function called the msp and Boom, your peripheral is ready for action.

A handle for an ADC if usually called hadc1, but it could be called Alice or Bob, but that could lead to confusion, so CubeMX generates handles with meaningful names. The GPIO drivers don't need handles because they are trivial, but peripherals like the ADCs, uarts etc all use them. A lot of people aren't that keen on HAL and use the Low Level drivers, but I have had good success with HAL and use them all the time.

Of course you can write your own drivers, but I want to produce systems that work fast and without to much bit-bashing so I use OP's code.  There is something nice about other peoples code, and that is you don't have to write it.

To get the peripheral to do things you then have to start it, stop it, tell it to transmit or receive using other HAL_Functions.

Hope this helps

Regards

Rob