Discovering the STM32 CUBE IDE: A Simple LED Blink and GPIO Project
2023-08-25 | By DWARAKAN RAMANATHAN
License: General Public License ARM mbed
Introduction:
Discover the world of STM32 CUBE IDE in this concise blog post. Uncover the powerful features that streamline embedded application creation for STM32 microcontrollers. From code generation to seamless peripherals configuration and advanced debugging tools, explore how this versatile IDE empowers developers of all levels. Whether you're a seasoned pro or a newcomer, unlock the full potential of your STM32 microcontrollers with STM32 CUBE IDE. Fast-track your embedded development and embark on a journey of innovation and efficiency with this essential tool.
Downloading:
To download STM32 CUBE IDE go to: My ST Registration - STMicroelectronics
Click on the Get Latest button against your preferred installer. This will take you to a login/register page. If you already have a MyST Account, then log in to your account and download. If not, please register yourselves and download the installer.
After logging in and downloading the installer, install the STM32 CUBE IDE on your device (Skipping the Installation steps). Open the software (STM32 CUBE IDE) and click on myST to login.
Creating a new project:
After logging in, click Start new STM32 project on the left. A new window will open for you to select the board or MCU. In this project, we will use B-L4S5I-IOT01A; the part is also linked below. Go to Board Selector and select the board by typing in the commercial part number (B-L4S5I-IOT01A) and click Next.
After clicking Next, Type the project name (In this case: Led_gpio) and click Finish.
Then, the STM32 CUBE IDE will automatically download the software packages that your board requires.
After the download is complete, you will see a workspace something like this (a .ioc file will be opened):
Assigning ports and generating code:
In this project, we will be controlling the built-in LED using the USER BUTTON. Before that, learn about the board. To do so, go to: B-L4S5I-IOT01A - STM32L4+ Discovery kit IoT node, low-power wireless, BLE, NFC, WiFi - STMicroelectronics
Now, to select the port for LED, in the Schematic that appears on your workspace click on PB14 and make it as GPIO output (This is the LED that we will control; you can try other built-in LEDs).
Now it's time to select the USER_BUTTON. To do so, go to PC13 and make it as GPIO_Input.
Since we have selected all the ports that we require, we can now generate the code for the board. To do so, go to Project>Generate Code. You will have a Code generated in C language. This is the code that will run on the board.
The code contains all the libraries, Variables, and Functions that are required by the LED and the USER_BUTTON (The code will be generated as per your changes made in the .ioc file before generating the code).
Writing the Code:
Go to the While loop (This will contain the user code) and type the code for LED and USER_BUTTON.
Note: You should always type your code only between the mentioned comments (In this case between USER CODE BEGIN 3) because if you need to regenerate code again by changing some pins you will not lose the code.
Type the code inside the while loop and below /* USER CODE BEGIN 3 */
if(HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13)==0)
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_RESET);
HAL_Delay(100);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
HAL_Delay(100);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_SET);
HAL_Delay(100);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_RESET);
}
HAL_Delay(125);
Understanding:
HAL : Hardware Abstraction Layer
GIPIO_x : Here, x means the port
GPIO_Pin: Here, you should mention the PIN
PinState : Here, you should mention the state of the PIN either SET or RESET
To type quicker use Ctrl + Space.
Building the project:
To check if there are any errors or warnings in your code, go to Project> Build Project.
If there are no errors in your code, you are good to debug the code after connecting your board to the device. But there are a few steps that you should take before connecting the board and debugging it.
Debugging the Code:
Connect your board to your device using the USB cable. Go to Device Manager and check whether you have all the devices as shown in the picture below:
If everything is fine, go to the CUBE IDE and Debug the code. If you do not see anything, please download, and install the driver. To debug the code, go to Run > Debug. You will see that the software debugs the code for you which means the code is being uploaded to the device (STM32 Board). Click OK in the Edit Configuration window.
Now, you will see "Download verified successfully" in the Console so click on Resume on the top.
Enjoy seeing the Output:
Since we have coded the program in a way that it'll work only if we press the User Button. So, press the User Button. That would be a surprise for you because we configured just one LED in the .ioc file, but all three LEDs do work because we initialized every peripheral when we created the project (Go to the Creating project section and create a new project if you need to see it).
You can find the next blog here : Discovering the STM32 CUBE IDE Part 2: Learning the UART
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum