Difference between revisions of "STM32StepByStep:Step2 Blink LED"

[quality revision] [quality revision]
m (Fix buttons mismatch)
m
 

Blinking LED with STM32CubeMX and HALClock.png 30min

Target description
This tutorial shows how to use the STM32CubeMX tool to initialize the peripherals, build and generate your starting projects with the initialization C code using HAL libraries.

After this tutorial, you should be able to:

  • create and configure the STM32CubeMX project and generate the initialization code
  • program and use HAL functions to blink a LED on the NUCLEO-L476RG board.

Prerequisites

Hardware
The hardware requirements to start the application are the following:

  • NUCLEO-L476RG[1] board (64-pin)
  • USB Type-A to Mini-B cable

Literature

  • STM32L476xx Datasheet
  • UM1724 User manual STM32 Nucleo-64 boards
  • UM1884 Description of STM32L4/L4+ HAL and low-layer drivers
  • UM1718 User manual STM32CubeMX for STM32 configuration and initialization C code generation


In this tutorial, we explain step-by-step how to blink a LED on the NUCLEO-L476RG board, using the STM32CubeMX tool, HAL, and STM32CubeIDE. Steps to follow:

1 Create New Project using STM32CubeMX[edit]

Clock.png 5min

  • Run STM32CubeMX tool.
  • Click New Project or Menu -> File -> New Project.
  • From Board Selector section, filter to select and use NUCLEO-L476RG board:
    Board selector
    • Check Nucleo64 type.
    • Check STM32L4 on MCU Series.
    • Select NUCLEO-L476RG board using Board selector:
      Select a board thanks to filters
    • Click Start Project to continue.
    • Answer Yes Initialize all peripherals with their default Mode ? popup.

2 Pinout Configuration[edit]

Clock.png 5min

Verify in Pinout tab, under SYS peripheral, that Serial Wire is selected as Debug interface:
Pin Out Tab
The corresponding pins PA13 and PA14 are assigned and configured automatically.
When a board is selected, STM32CubeMX allows automatically the pinout setting for the board with the pin assignments for the communication interfaces, LEDs, and other functions.
(To configure LED pins, check in the STM32 Nucleo-64 boards User Manual and STM32L476xx Datasheet which LED pins to use).

Info white.png Information
To see alternate pins for a signal, drag and drop the signal to a pin while keeping the Ctrl key pressed.

This example shows the use of the green LED pin LD2 present on the NUCLEO-L476RG board as GPIO_Output.
To verify that LD2 is set to GPIO_Output mode:

  • Type “LED” in the Find field and check that LD2 (green Led) is enabled to PA5 pin as GPIO_Output.
  • When found, the pin that matches the search criteria blinks on the Chip view.
  • Click on Chip view to stop the blinking. The signals can be set directly from the pinout view:
    Pin Out
Info white.png Information
Pinout search field allows the user to search for a pin name, signal name, or signal label in the Pinout view. When found, the pin or set of pins that matches the search criteria blinks on the Chip view. Click on the Chip view, to stop blinking.


3 Clock Configuration[edit]

Clock.png 4min
In the Clock Configuration tab, check that STM32CubeMX automatically configures the internal oscillator in the clock system with PLL @80MHz and proposes the PLL configuration as follows:
1. HSI selected in PLL Source Mux (HSI – High Speed Internal clock)
2. PLLCLK selected in the System Clock Mux
3. HCLK set to 80
Clock Tab

4 GPIO Configuration[edit]

Clock.png 4min
To configure the GPIOs, click the GPIO button in the Configuration Tab to open the GPIO Configuration window.
Pin Out

In the GPIO Tab, select Pin Name column PA5 to display the corresponding GPIO parameters and configuration to drive the NUCLEO-L476RG LED:

  • GPIO Output level: it is set to Low by default and can be changed to High.
  • GPIO mode automatically configures the pins with the relevant alternate function and GPIOs into Output Push Pull mode.
  • GPIO Pull-up/Pull-dow set to No pull-up and no pull-down by default can be configured when other choices are allowed.
  • GPIO Maximum output speed set to Low by default for the power consumption optimization can be changed to a higher frequency to fit the application requirements.
  • User Label is a name assigned to a GPIO. The GPIO can be found under this name via the Find menu.

Pin Out
Click Apply then OK to close the window.

5 Configure project and generate source code[edit]

Clock.png 4min
Switch to the Project Manager tab to configure the project. In the Project tab:

  • Fill the Project Name and Project Location fields
  • Set Toolchain/IDE to STM32CubeIDE.
    Project Tab

In the Code Generator tab, ensure that the following options are checked:

  • In STM32Cube Firmware Library Package section: Copy all used libraries into the project folder.
  • In Generated files section: Keep user code when regenerating the C code option, which only applies to the user sections within the STM32CubeMX generated files.
    Code generation

To generate the project in STM32CubeIDE:

  • Click on Generate Code.
    Generate Code
  • Click on Open Project to open the project with STM32CubeIDE.
    Open Project


6 Edit main.c to toggle the LED[edit]

Clock.png 4min
In STM32CubeIDE, from the Project Explorer tab, open the main.c file, in Src folder and add the adequate functions for the LED blinking, using HAL functions with the STM32CubeL4 firmware package.
STM32CubeIDE

To get an idea about the usage of HAL functions, refer to the UM1884 “Description of STM32L4/L4+ HAL and low-layer drivers” user manual, which provides the common and generic functions to use.
HAL help

The user code can be added in the main.c file, inside the while (1) loop between /* USER CODE BEGIN 3 */ and /* USER CODE END 3 */ section (this will preserve your code after regeneration).

For the LED toggling , use these functions:
HAL_GPIO_TogglePin (GPIOA, GPIO_PIN_5);
HAL_Delay (100);   /* Insert delay 100 ms */

Code to blink LED

7 Build the project[edit]

Clock.png 2min
To power the NUCLEO-L476RG, use its CN1 connector to connect in with a computer through a USB Type-A to Mini-B cable.
Click on the project from the project explorer, then right click and select Build Project, to compile the project (or click on Build button on the toolbar).

8 Debug the project[edit]

Clock.png 2min

Click on the Build toolbar icon Built Button.png, then on the Debug button Debug Button.png to build the project and start the debug session (or in the menu, select Project > Build Project then Run > Debug).
Click on Resume icon Run Button.png to continue the execution.
Now watch the green LED (LD2) toggling on the Nucleo-L476RG board.

Now you are able to:

  • create a new project using STM32CubeMX.
  • configure a project in STM32CubeMx and generate the initialization code.
  • update the project code in STM32CubeIDE using HAL functions.
  • execute a project in debug mode.
  • make a LED blink.

9 References[edit]



Previous step Arrow left.png Arrow right.png Next step


<big><big><big>'''Blinking LED with STM32CubeMX and HAL'''</big></big></big>[[File:Clock.png|40px|middle]] 30min<br><br>
<big><big>'''Target description'''</big></big><br>

This tutorial shows how to use the STM32CubeMX tool to initialize the peripherals, build and generate your starting projects with the initialization C code using HAL libraries.<br>


After this tutorial, you should be able to:<br>

* create and configure the STM32CubeMX project and generate the initialization code<br>

* program and use HAL functions to blink a LED on the NUCLEO-L476RG board.<br> 

<big><big>'''Prerequisites'''</big></big><br>

* Previous Tutorial: [[STM32StepByStep:Step1 Tools installation | Step1: Tools Installation and First test.]].<br>

<big><big>'''Hardware'''</big></big><br>

The hardware requirements to start the application are the following:<br>

* NUCLEO-L476RG<ref>[https://www.st.com/en/evaluation-tools/nucleo-l476rg.html NUCLEO-L476RG board (64-pin)]</ref> board (64-pin)<br>

* USB Type-A to Mini-B cable<br>

<big><big>'''Literature'''</big></big><br>

* [http://www.st.com/content/ccc/resource/technical/document/datasheet/c5/ed/2f/60/aa/79/42/0b/DM00108832.pdf/files/DM00108832.pdf/jcr:content/translations/en.DM00108832.pdf STM32L476xx Datasheet]<br>

* [http://www.st.com/content/ccc/resource/technical/document/user_manual/98/2e/fa/4b/e0/82/43/b7/DM00105823.pdf/files/DM00105823.pdf/jcr:content/translations/en.DM00105823.pdf UM1724]  User manual STM32 Nucleo-64 boards <br>

* [http://www.st.com/content/ccc/resource/technical/document/user_manual/63/a8/8f/e3/ca/a1/4c/84/DM00173145.pdf/files/DM00173145.pdf/jcr:content/translations/en.DM00173145.pdf UM1884]  Description of STM32L4/L4+ HAL and low-layer drivers<br>

* [http://www.st.com/content/ccc/resource/technical/document/user_manual/10/c5/1a/43/3a/70/43/7d/DM00104712.pdf/files/DM00104712.pdf/jcr:content/translations/en.DM00104712.pdf UM1718]  User manual STM32CubeMX for STM32 configuration and initialization C code generation<br>


* [[File:pc_videol.png|middle|20px|link=https://youtu.be/6RqUkFIeN6w]] [https://youtu.be/6RqUkFIeN6w  How to build a “Blink LED” project from STM32CubeMX for ST/Atollic TrueSTUDIO® for STM32]. <br>

{{Highlight|In this tutorial, we explain step-by-step how to blink a LED on the NUCLEO-L476RG board, using the STM32CubeMX tool, HAL, and STM32CubeIDE. Steps to follow:}}<br>


==Create New Project using STM32CubeMX ==
[[File:Clock.png|40px|middle]] 5min<br>

* Run '''STM32CubeMX''' tool.<br>

* Click '''''New Project or Menu''''' -> '''''File''''' -> '''''New Project'''''.<br>

* From '''''Board Selector''''' section, filter to select and use NUCLEO-L476RG board:<br>[[File:Board_Selector.png|Board selector|500px]]<br>

** Check '''Nucleo64''' type.<br>

** Check '''STM32L4''' on MCU Series.<br>

** Select '''NUCLEO-L476RG''' board using Board selector: <br>[[File:Board_Selection.png|Select a board thanks to filters|500px]]<br>

** Click '''''Start Project''''' to continue.<br>

** Answer '''''Yes''''' '''Initialize all peripherals with their default Mode ?''' popup.<br>


==Pinout Configuration==
[[File:Clock.png|40px|middle]] 5min<br>


Verify in '''''Pinout''''' tab, under '''''SYS peripheral''''', that '''''Serial Wire''''' is selected as Debug interface:<br>[[File:PinOut_Tab.png|Pin Out Tab|500px]]<br>

The corresponding pins PA13 and PA14 are assigned and configured automatically.<br>

When a board is selected, STM32CubeMX allows automatically the pinout setting for the board with the pin assignments for the communication interfaces, LEDs, and other functions.<br>

(To configure LED pins, check in the [http://www.st.com/content/ccc/resource/technical/document/user_manual/98/2e/fa/4b/e0/82/43/b7/DM00105823.pdf/files/DM00105823.pdf/jcr:content/translations/en.DM00105823.pdf STM32 Nucleo-64 boards User Manual] and [http://www.st.com/content/ccc/resource/technical/document/datasheet/c5/ed/2f/60/aa/79/42/0b/DM00108832.pdf/files/DM00108832.pdf/jcr:content/translations/en.DM00108832.pdf STM32L476xx Datasheet] which LED pins to use).<br>

{{info| To see alternate pins for a signal, drag and drop the signal to a pin while keeping the Ctrl key pressed.}}

This example shows the use of the green LED pin LD2 present on the NUCLEO-L476RG board as GPIO_Output.<br>

To verify that LD2 is set to GPIO_Output mode:<br>

* Type “LED” in the Find field and check that LD2 (green Led) is enabled to PA5 pin as GPIO_Output. <br>

* When found, the pin that matches the search criteria blinks on the Chip view. <br>

* Click on Chip view to stop the blinking. The signals can be set directly from the pinout view:<br>[[File:PinOut_Focus.png|Pin Out|500px]]
{{info| Pinout search field allows the user to search for a pin name, signal name, or signal label in the Pinout view. When found, the pin or set of pins that matches the search criteria blinks on the Chip view. Click on the Chip view, to stop blinking.}}<br>


==Clock Configuration==
[[File:Clock.png|40px|middle]] 4min<br>

In the '''''Clock Configuration''''' tab, check that STM32CubeMX automatically configures the internal oscillator in the clock system with PLL @80MHz and proposes the PLL configuration as follows:<br>

1. HSI selected in PLL Source Mux (HSI – High Speed Internal clock)<br>

2. PLLCLK selected in the System Clock Mux<br>

3. HCLK set to 80<br>

[[File:Clock_Tab.png|Clock Tab|500px]]<br>
<br>

==GPIO Configuration==
[[File:Clock.png|40px|middle]] 4min<br>

To configure the GPIOs, click the GPIO button in the '''''Configuration''''' Tab to open the GPIO Configuration window.<br>[[File:PinOut_Conf.png|Pin Out|500px]]<br><br>

In the '''''GPIO''''' Tab, select Pin Name column '''''PA5''''' to display the corresponding GPIO parameters and configuration to drive the NUCLEO-L476RG LED:<br>

* '''''GPIO Output level:''''' it is set to Low by default and can be changed to High. <br>

* '''''GPIO mode''''' automatically configures the pins with the relevant alternate function and GPIOs into Output '''''Push Pull''''' mode. <br>

* '''''GPIO Pull-up/Pull-dow''''' set to '''''No pull-up and no pull-down''''' by default can be configured when other choices are allowed. <br>

* GPIO '''''Maximum output speed''''' set to '''''Low''''' by default for the power consumption optimization can be changed to a higher frequency to fit the application requirements. <br>

* '''''User Label''''' is a name assigned to a GPIO. The GPIO can be found under this name via the '''''Find''''' menu. <br>

[[File:PinOut_Set.png|Pin Out|500px]]<br>

Click '''''Apply''''' then '''''OK''''' to close the window. <br>
<br>


==Configure project and generate source code==
[[File:Clock.png|40px|middle]] 4min<br>

Switch to the '''''Project Manager''''' tab to configure the project. In the '''''Project''''' tab:<br>

* Fill the '''''Project Name''''' and '''''Project Location''''' fields <br>

* Set '''''Toolchain/IDE''''' to '''''STM32CubeIDE'''''.<br>[[File:Project_Tab.png|Project Tab|500px]]<br>


In the '''''Code Generator''''' tab, ensure that the following options are checked:<br> 

* In '''''STM32Cube Firmware Library Package''''' section: '''Copy''' '''''all used libraries into the project folder'''''.<br>

* In '''''Generated files''''' section: '''''Keep user code when regenerating the C code''''' option,  which only applies to the user sections within the STM32CubeMX generated files.<br>[[File:CodeGenerator_Tab.png|Code generation|500px]]<br>


To generate the project in STM32CubeIDE:<br>

* Click on '''''Generate Code'''''.<br>[[File:Generator_title.png|Generate Code|300px]]<br>

* Click on '''''Open Project''''' to open the project with STM32CubeIDE.<br>[[File:CodeGenerator_Popup.png|Open Project|400px]]<br>
<br>


==Edit main.c to toggle the LED==
[[File:Clock.png|40px|middle]] 4min<br>

In STM32CubeIDE, from the Project Explorer tab, open the main.c file, in Src folder and add the adequate functions for the LED blinking, using HAL functions with the STM32CubeL4 firmware package.<br>[[File:Code_Panel.png|STM32CubeIDE|500px]]<br><br>


To get an idea about the usage of HAL functions, refer to the [http://www.st.com/content/ccc/resource/technical/document/user_manual/63/a8/8f/e3/ca/a1/4c/84/DM00173145.pdf/files/DM00173145.pdf/jcr:content/translations/en.DM00173145.pdf UM1884] “Description of STM32L4/L4+ HAL and low-layer drivers” user manual, which provides the common and generic functions to use.<br>[[File:HAL_GPIO_Help.png|HAL help|500px]]<br>


The user code can be added in the main.c file, inside the '''''while (1)'''''
loop between '''''/* USER CODE BEGIN 3 */''''' and '''''/* USER CODE END 3 */''''' section (this will preserve your code after regeneration).<br>

For the LED toggling , use these functions:<syntaxhighlight lang="c">HAL_GPIO_TogglePin (GPIOA, GPIO_PIN_5);
HAL_Delay (100);   /* Insert delay 100 ms */</syntaxhighlight>


[[File:Code_Panel_Highlight.png|Code to blink LED|500px]]<br>
<br>


==Build the project==
[[File:Clock.png|40px|middle]] 2min<br>

To power the NUCLEO-L476RG, use its CN1 connector to connect in with a computer through a USB Type-A to Mini-B cable.<br>

Click on the project from the project explorer, then right click and select '''''Build Project''''', to compile the project (or click on '''''Build button''''' on the toolbar).<br>
<br>


==Debug the project==
[[File:Clock.png|40px|middle]] 2min<br>


Click on the '''''Build''''' toolbar icon [[File:Built_Button.png|20px|middle]], then on the '''''Debug''''' button [[File:Debug_Button.png|20px|middle]] to build the project and start the debug session (or in the menu, select '''''Project''''' > '''''Build Project''''' then '''''Run''''' > '''''Debug''''').<br>

Click on '''''Resume''''' icon [[File:Run_Button.png|20px|middle]] to continue the execution.<br>

Now watch the green LED (LD2) toggling on the Nucleo-L476RG board.<br>
<br>

{{Highlight|Now you are able to:<br>

* create a new project using STM32CubeMX.<br>

* configure a project in STM32CubeMx and generate the initialization code. <br>

* update the project code in STM32CubeIDE using HAL functions.<br>

* execute a project in debug mode.<br>

* make a LED blink.<br> }}

==References==<references />
<br><br>

{|class="st-table" style="margin: auto; text-align:center"
|style="border-style: hidden; background-color: white;width:200px; text-align:right"|'''[[STM32StepByStep:Step1 Tools installation | Previous step]]'''
|style="border-style: hidden; background-color: white;width:100px; text-align:left"|[[File:Arrow left.png|20px|link=STM32StepByStep:Step1 Tools installation]]

|style="border-style: hidden; background-color: white;width:100px; text-align:right"|[[File:Arrow right.png|20px|link=STM32StepByStep:Step3 Introduction to the UART]]''
|style="border-style: hidden; background-color: white;width:200px; text-align:left"|'''[[STM32StepByStep:Step3 Introduction to the UART | Next step]]'''
|}

{{PublicationRequestId | 15905 | 2020-04-29 | Alain FONTBONNE}}<noinclude>

[[Category:STM32 step by stepGetting started with STM32 : STM32 step by step|30]]</noinclude>
Line 136: Line 136:
 
{{PublicationRequestId | 15905 | 2020-04-29 | Alain FONTBONNE}}
 
{{PublicationRequestId | 15905 | 2020-04-29 | Alain FONTBONNE}}
 
<noinclude>
 
<noinclude>
[[Category:STM32 step by step]]
+
[[Category:Getting started with STM32 : STM32 step by step|30]]
 
</noinclude>
 
</noinclude>