Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Bare-Metal Embedded C Programming

You're reading from   Bare-Metal Embedded C Programming Develop high-performance embedded systems with C for Arm microcontrollers

Arrow left icon
Product type Paperback
Published in Sep 2024
Publisher Packt
ISBN-13 9781835460818
Length 438 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Israel Gbati Israel Gbati
Author Profile Icon Israel Gbati
Israel Gbati
Arrow right icon
View More author details
Toc

Table of Contents (21) Chapters Close

Preface 1. Chapter 1: Setting Up the Tools of the Trade FREE CHAPTER 2. Chapter 2: Constructing Peripheral Registers from Memory Addresses 3. Chapter 3: Understanding the Build Process and Exploring the GNU Toolchain 4. Chapter 4: Developing the Linker Script and Startup File 5. Chapter 5: The “Make” Build System 6. Chapter 6: The Common Microcontroller Software Interface Standard (CMSIS) 7. Chapter 7: The General-Purpose Input/Output (GPIO) Peripheral 8. Chapter 8: System Tick (SysTick) Timer 9. Chapter 9: General-Purpose Timers (TIM) 10. Chapter 10: The Universal Asynchronous Receiver/Transmitter Protocol 11. Chapter 11: Analog-to-Digital Converter (ADC) 12. Chapter 12: Serial Peripheral Interface (SPI) 13. Chapter 13: Inter-Integrated Circuit (I2C) 14. Chapter 14: External Interrupts and Events (EXTI) 15. Chapter 15: The Real-Time Clock (RTC) 16. Chapter 16: Independent Watchdog (IWDG) 17. Chapter 17: Direct Memory Access (DMA) 18. Chapter 18: Power Management and Energy Efficiency in Embedded Systems 19. Index 20. Other Books You May Enjoy

Essential development tools for microcontrollers

In this section, we will explore the essential tools that form the backbone of our development process. Understanding these tools is important, as they will be our companions in transforming ideas into functioning firmware.

When selecting tools for firmware development, we have two primary options.

  • IDEs: An IDE is a unified software application offering a Graphical User Interface (GUI) tailored to crafting software – in our context, firmware. Popular IDEs for microcontroller firmware development include the following:
  • Keil uVision (also known as Keil MDK): Developed by ARM Holdings
  • STM32CubeIDE: Developed by STMicroelectronics
  • IAR Embedded Workbench: Developed by IAR Systems

    These IDEs boast a GUI-centric design, enabling users to conveniently create new files, build, compile, and step through code lines interactively. For the demonstrations and exercises in this book, we’ll use the STM32CubeIDE. It has all the requisite features and is generously available for free, without any code size constraints.

  • Toolchains: At its core, a toolchain is a cohesive set of development tools, sequenced in distinct stages, to produce the final firmware build for the target microcontroller. This approach bypasses the comfort of a GUI. Instead, firmware is written using basic text editors such as Notepad or Notepad++, with the command line used to execute the various phases of the build process –commands such as assemble, compile, and link are often used. In this book, we’ll use the open source GNU Arm Embedded Toolchain. Based on the renowned open source GNU Compiler Collection (GCC), this integrates a GCC compiler tailored for ARM, the GNU Debugger (GDB) debugger, and several other invaluable utilities.

In the following section, we will carefully go through the process of setting up our preferred IDE, the STM32CubeIDE.

Setting up the STM32CubeIDE

Throughout this book, we’ll use both the STM32CubeIDE and the GNU Arm Embedded Toolchain to develop our firmware. Leveraging an IDE such as STM32CubeIDE enables us to easily analyze and compare the linker script and startup files, autogenerated by the IDE, against those we’ll construct from the ground up.

Let’s start by downloading and installing STM32CubeIDE:

  1. Launch your web browser and navigate to st.com.
  2. Click on STM32 Developer Zone, and then select STM32CubeIDE.
Figure 1.1: The home page of st.com

Figure 1.1: The home page of st.com

  1. Scroll down to the All software versions section of the page and click on Download Software. You’ll need to log into your ST account before proceeding with the download.
Figure 1.2: The All software versions section of the stm32cubeide page

Figure 1.2: The All software versions section of the stm32cubeide page

  1. If you don’t have an account, click on Login/Register to sign up. If you already have one, simply log in.
  2. Complete the registration form with your first name, last name, and email address.
  3. Click on Download to start the download process. A .zip file will be downloaded into your Downloads folder.

Let’s install the STM32CubeIDE:

  1. Unzip the downloaded package.
  2. Double-click the st-stm32cubeide file to initiate the installer.
  3. Retain default settings by clicking Next throughout the setup process.
  4. On the Choose Components page, ensure that both SEGGER J-Link drivers and ST-LINK drivers are selected. Then, click Install.
Figure 1.3: The installer showing the Choose Components page

Figure 1.3: The installer showing the Choose Components page

Having successfully installed STM32CubeIDE on our computer, we will now proceed to configure our alternate development tool, the GNU Arm Embedded Toolchain.

Setting up the GNU Arm Embedded Toolchain

In this section, we will go through the process of setting up the GNU Arm Embedded Toolchain – an important tool for developing firmware for ARM-based microcontrollers:

  1. Launch your web browser and navigate to https://developer.arm.com/downloads/-/gnu-rm.
  2. Scroll down the page to find the download link appropriate for your operating system. For those of you using Windows, like myself, opt for the .exe version. For Linux or macOS users, choose the corresponding .tar file for your operating system.
  3. After the download completes, double-click the installer to begin the installation process.
  4. Read through the license agreement. Then, choose to install in the default folder location by clicking Install.
Figure 1.4: The GNU Arm Embedded Toolchain installer

Figure 1.4: The GNU Arm Embedded Toolchain installer

  1. When the installation is complete, ensure that you check the Add path to environment variable option.

Figure 1.5: The installer showing the Add path to environment variable option

Figure 1.5: The installer showing the Add path to environment variable option

  1. Click Finish to finalize the setup.

Setting up OpenOCD

For firmware development with the GNU Arm Toolchain, OpenOCD plays an integral role, facilitating both the downloading of firmware into our microcontroller and the debugging of code in real time.

Let’s set up OpenOCD:

  1. Launch your web browser and navigate to https://openocd.org/pages/getting-openocd.html.
  2. Scroll to the section titled Unofficial binary packages.
  3. Click on the link for multiplatform binaries.
Figure 1.6: The Unofficial binary packages section

Figure 1.6: The Unofficial binary packages section

  1. Identify the latest version compatible with your operating system. For an exhaustive list, click on Show all 14 assets.
Figure 1.7: The OpenOCD packages

Figure 1.7: The OpenOCD packages

  1. For Windows users, download the win32-x64.zip version. For Linux or macOS users, download the corresponding .tar file for your operating system.
  2. Once downloaded, unzip the package.
  3. Navigate to the extracted folder, and then the bin subfolder. Here, you’ll find the openocd.exe application. This is the application we shall call in the command prompt together with the specific script of our chosen microcontroller, in order to debug or download code onto the microcontroller.

Within the xpack-openocd-0.12.0-2 | openocd | scripts directory structure, you’ll find scripts tailored for various microcontrollers and development boards.

Next, we need to add OpenOCD to our environment variables:

  1. Begin by relocating the entire openocd folder to your Program Files directory.
Figure 1.8: OpenOCD moved to Program Files, showing the path to the bin folder

Figure 1.8: OpenOCD moved to Program Files, showing the path to the bin folder

  1. Copy the path to the openocd bin folder.
  2. Right-click on This PC, and then choose Properties.
  3. Search for and select Edit the system environment variables.
Figure 1.9: The This PC properties page

Figure 1.9: The This PC properties page

  1. Click the Environment Variables button in the System Properties pop-up window.
Figure 1.10: The System Properties pop-up window

Figure 1.10: The System Properties pop-up window

  1. Under the User variables section of the Environment Variables popup, double-click the Path entry.
Figure 1.11: The Environment Variables popup

Figure 1.11: The Environment Variables popup

  1. In the Edit environment variable popup, click on New to create a row for a new path entry.
  2. Paste the previously copied OpenOCD path into this new row.
  3. Confirm your changes by clicking OK on the various pop-up windows.
Figure 1.12: The Edit environment variable popup

Figure 1.12: The Edit environment variable popup

Finally, we have successfully completed the setup process. We have configured two essential, standalone tools to develop firmware for STM32 microcontrollers – the STM32CubeIDE for an IDE, and the GNU Arm Embedded Toolchain, complemented by OpenOCD, to develop and debug our firmware without an IDE.

Next, we will turn our attention to our development board.

You have been reading a chapter from
Bare-Metal Embedded C Programming
Published in: Sep 2024
Publisher: Packt
ISBN-13: 9781835460818
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image