Skip to content

PREPARATION WORK

Note

"Initialization settings" is the first step in setting up the main control system, enabling the system to operate its basic functions. This section focuses on the basic configuration of the main control board. Note that for ease of reference and portability, this section only covers the configuration of the core board, while peripheral configurations are addressed in corresponding chapters. When adding peripheral hardware and software, you only need to enable the relevant pins and functions in CUBEMX, without affecting the existing functions.

HARDWARE PREPARATION

To program the main control board, we need three components:

  • The main control board - the development board that embeds the MCU
  • A USB-TTL adapter - to connect the main control board to the computer
  • A DEBUGGER (ST-LINK V2, J-LINK, etc.) - to program the main control board

COMPONENT 1 - The Main Control Board - FANKE743

MAIN_CONTROL

COMPONENT 2 - USB-TTL Adapter

USB_TTL_ADAPTER

COMPONENT 3 - Debugger

DEBUGGER

Info

CMSIS-DAP, ST-LINK, and J-LINK are the most commonly used debuggers. Any of these debuggers can be used to program the main control board.

HARDWARE CONNECTION

CONNECTION

Connect the three components as shown in the figure above. Then connect the USB-TTL adapter and the debugger to the computer.

SOFTWARE PREPARATION

STM32CUBEMX

STM32CUBEMX is a graphical tool that allows configuring the STM32 microcontroller very easily and generating the corresponding initialization C code through a step-by-step process. Download and install STM32CubeMX from the official website.

KEIL MDK

KEIL MDK is a complete software development environment for a wide range of ARM Cortex-M based microcontroller devices. Download and install KEIL MDK from the official website.

SOFTWARE SETUP

CREATE A NEW PROJECT IN STM32CUBEMX

New Project

STM32CUBEMX

Open STM32CubeMX and click the 'File' tab and then click the option 'New Project'.

Select MCU Series and Model

NEW PROJECT

Select the MCU series and the MCU model. Then click 'Start Project'.

Enable Memory Protection Unit (MPU)

Memory Protection

Enable the 'Memory Protection Unit' and click 'YES'.

Configure Project Information and Code Generation

Project Information

Project Code Generation

Setup the project information and code generation configuration first as shown in the above figures.Then click 'Generate Code'.

SYSTEM CONFIGURATION

Let's go back to the Pinout & Configuration tab and configure the system as shown below.

System Core - SYS: Timebase Source

SYS

Select the 'SYS' tab and then select the 'Timebase Source' as 'SysTick'.

System Core - RCC: High Speed Clock (HSE) & Low Speed Clock (LSE)

RCC

Select the 'RCC' tab and configure the 'High Speed Clock (HSE)' and 'Low Speed Clock (LSE)' as shown in the above figure. Both of them are set to 'Crystal/Ceramic Resonator'. Generally speaking, the external crystal oscillators are more accurate than the internal RC oscillators.

System Core - CORETEX_M7: Enable I-Cache & D-Cache & MPU

CORETEX_M7

Select the 'CORETEX_M7' tab and enable the 'I-Cache', 'D-Cache', and 'MPU' as shown in the above figure. Some advanced features require the I-Cache, D-Cache, and MPU to be enabled, e.g., X-CUBE-AI.

Timer - RTC: Enable RTC

RTC

Select the 'RTC' tab and enable the 'RTC' as shown in the above figure. The RTC is a real-time clock that can be used to keep track of the current time and date. Here, we enable RTC for future potential applications.

Trace and Debug - DEBUG: Enable Serial Wire

DEBUG

Select the 'DEBUG' tab and enable the 'Serial Wire' as shown in the above figure. The Serial Wire is a debug interface that allows the debugger to communicate with the MCU.

Clock Configuration

CLOCK

Configure the clock as shown in the above figure. The clock configuration is crucial for the MCU to operate correctly.

Note

So far, we have finished the basic setup up for MCU. You can have check of all configuration again and then click 'Generate Code' to generate the initialization code for the MCU. We will continue to set up the software environment in the next section.

Tip

For simplicity, I did not include all the CubeMX configuration steps in this section. In stead, the configuration is divided into several sections according to the function of the configuration. In practice, one can configure all the settings in one go.

GENERATE AND UPDATE CODE

Finally, let's generate the code to save the current progress and to make it ready for following programming steps. Click the 'Generate Code' button at the top right corner of the screen.

PROJECT STRUCTURE

The project files in the system explorer looks like this:

FILE STRUCTURE

The project structure in Keil looks like this:

PROJECT STRUCTURE

The include path is set up as shown in the figure below:

INCLUDE PATH