13. Programming the STM32F4-DiscoveryThe programming environment including the settings for compiling and programming are described.3.1. Hardware - The programming interfaceA program for a microcontroller is prepared on a personal computer (PC) using a suitable set ofprograms. First the source code of the program is written, than this code is translated into the machinecode for a particular microcontroller, the STM32F407VG in our case. The developer then loads thecode into the memory of the microcontroller. Several methods for loading are known. Most universalprogrammers (devices that fit between a PC and the target microcontroller, and allow the transfer ofthe code into the microcontroller) support JTAG method. The company ST introduced a method calledserial wire debug (SWD), which uses fewer wires than JTAG method. The method SWD is supported bySTM32F4DISCOVERY board, and the programmer that comes between a USB connector and the SWDpins of the microcontroller is available on the Discovery board.The STM32F4DISCOVERY board hosts additional connector for SWD signals and can also be used toprogram external free standing microcontroller of the same family.3.2. Software – The IAR EWB for ARM microcontrollersThe complete procedure of writing, compiling and testing the user program can be done in a set ofprograms called the development suite. We are going to use the IAR (the name of the company)Electronic Work Bench (EWB) for ARM microcontrollers. The current version is 6.7. All demo programswill be prepared using the free version of the software; this version is size-limited, but our needs areway below the limits of the suite. The current version IAR EWB and instructions for the installation canbe downloaded from IAR website.The IAR EWB requires at least the following files to create the machine code for the microcontroller:--The user program, a text file written in “C” language.The file “startup stm32f4xx.s”, a text file written in assembly language; this file containsinstructions for the initial set-up of the microcontroller (stack, program counter, interruptvector table, initial system clock ; the description of the file is given in its header).The file “system stm32f4xx.c”, a text file written in “C”; this file contains functions for thedetailed microcontroller set-up (system clock, clock distribution ; the description of this file isgiven in its header).In our case the file may have different names; since the file contains clock configurationcommands it is best to prepare several files in advance, each for a different configuration of theclock and then simply include in the process of compiling the file that corresponds to thecurrent needs. We will run the microcontroller at its maximum speed, and the correspondingfile is named “system stm32f4xx CLK168 HSE8.c”.
Programming the STM32F4-Discovery--2The header file “stm32f4xx.h”; this file defines processor used, names the registers and bitsinside the STM32F4xx microcontroller, and defines some register structures. This is the onlyfile that must be included from the user program in the process of compilation. The file hasbeen modified from the original (obtained from the ST site):o by uncommenting the line 68 (select the microcontroller STM32F40xx by default),o by uncommenting the line 88 (allow the use of standard peripheral driver) ando by changing the HSE frequency in line 100 (from 25000000 to 8000000, as used at theSTM32F4-Discovery board).The header file “system stm32f4xx.h”; allows the definition of some stuff for the use of CMSIS.The header file “stm32F4xx conf.h”; defines and includes some files mandatory for the project.The peripherals included in microcontrollers STM32F4xx are complex, and their operation isconfigured and monitored using several control registers for each individual peripheral, the details aregiven in reference manual RM0090. Bits in control registers define the operation of peripherals, andcan be manipulated from the “C” language by simple writes into these registers. However, theprogrammer must know the exact location and function of each individual bit, which may be anoverwhelming task for such a complex microcontroller. To overcome this problem a standard to easethe use of control registers has been developed. It iscalled ‘Cortex Microcontroller Standard InterfaceSystem’, or CMSIS for short. The standard defines a set offunctions written in “C” language, each of themmanipulating exactly the bits to define the operation of aperipheral. When using the CMSIS the programmer doesnot need to know the exact location and function of bits,he/she must only know what the peripheral is capable ofand call appropriate functions, which are located in CMSISlibrary; functions take care of setting bits in controlregisters correctly. The standard covers several families ofmicrocontrollers, and also eases porting of software fromone type of microcontroller to another. The completelibrary for STM32F4xx series microcontrollers is availableat ST site as “STM32F4xx DSP StdPeriph Lib V1.1.0” atthe time of writing. The directory structure of theexpanded library is given in Fig. 3.2.The use of CMSIS standard is the reason that additionalfiles are mandatory during the process of compilationbeside the ones listed above. Two files are associated withevery peripheral in the microcontroller:- The header file defines the data structures used inaccessing the peripheral and names the registers andconstants (“stm32f4xx .h”).- The source file contains the actual functions to accessthe registers responsible for the behavior ofperipherals (“stm32f4xx .c”).Here “ ” stands for the name of the peripheral.The compiler must know the location of all header andFig. 3.2: The directory structure of theCMSIS library
Programming the STM32F4-Discovery3include files used, and this must be specified in the compiler setup before the compilation process.All files mentioned are available in the CMSIS library, see Fig. 3.2 for the expanded directorystructure of the library. The files “system stm32f4xx.h” and “stm32f4xx.h” are located in directory“Include”, upper black arrow. The programming environment dependent file “startup stm32f40x.s” islocated in directory “IAR”, lower black arrow. Please note that these files are original, unmodified ones,and it is most convenient to store the modified versions to a separate directory and call them fromthere. CMSIS header (*.h) and source (*.c) files describing peripherals are located in directories “inc”and “src” respectively, both red arrows. Some examples on the use of CMSIS library are given indirectory “STM32F4xx StdPeriph Examples”, and templates for different compilers are given in“STM32F4xx StdPeriph Templates”, blue arrow.The process of downloading the compiled program into the microcontroller requires one additionalfile with the description of downloading procedure and the destination memory for the program, thisfile is called linker configuration file. There are three options to choose from as far as the destinationmemory is concerned: the file may be programmed into the flash memory, the file may be downloadedinto the random access memory inside the microcontroller or the program may be loaded into anexternal RAM. All three configuration files have the extension “.icf”, and can be found in“STM32F4xx StdPeriph templates\EWARM”. Since we want to make programming permanent we willonly use the file named “stm32f4xx flash.icf”.Two additional files were prepared for these experiments, “LCD2x16.c” and “dd.h”. The firstcontains functions to display character strings and numbers at the alphanumerical LCD display, thesecond one defines some constants and interfacing pins. Both files should be made available duringthe compilation process.3.4. Creating of a new workspace and a project withinPrograms for microcontroller are prepared in a workspace, an imaginary working environmentcontaining all the user supplied stuff to compose the final machine code. The workspace is associatedwith a folder on a computer disk. The workspace contains projects;each project holds a complete set of files (or references to filescommon to several projects) that constitute one user program.Typically all files needed for a project are stored in a designated subfolder within a folder for the workspace to avoid the namingconfusion of individual files within the workspace.Figure 3.3: Folders createdThe procedure for creating a new workspace and a project withinto host the workspace andwill be described step-by-step. The directory structure to hold thethree projectsworkspace and projects is created first using the Windows Explorer,Figure 3.4: The mandatory files are copied into the project directory
Programming the STM32F4-Discovery4the directory structure is given in Fig. 3.3, for hosting three projects (Project1, Project2 and Project3)within a workspace called Workspace1. Additional directories can be added later to host more projectsin the same workspace. A set of files as listed in Fig. 3.4. is copied into each of the project directoriesfor easy access and inclusion into the user program. Note that some of these files are modified versionsof the originals given in the CMSIS library.Create a workspaceInitially the “IAR Embedded Workbench” is opened resulting in an empty space in the center of theprogram window and empty workspace in the left part of the window, Fig. 3.5. This state can beachieved also by clicking the “File” option from the menu, followed by “New” and “Workspace”.Figure 3.5: A newly opened »IAR Embedded Workbench« with an empty workspace on the leftCreate a project-Click “Project” option in the menu, and then click “Create New Project ” to get a windowas shown in Fig. 3.6.Figure 3.6: A window to create a new project
Programming the STM32F4-Discovery--5Select “Empty project” and click “OK” to get a window “Save As”. Navigate to the folderwhere the project is to be saved (The “Workspace1\Project1” in our case). Under “Filename:” type in the name of new project, like “MyFirstProject”, and click “OK”. The skeletonof the new project will be created in the stated directory, and the Workspace window (Fig.3.5, left part of the window) of IAW EWB will contain the name of the newly created project.Save the newly created workspace by clicking “File” option in the menu and then clicking“Save Workspace”, navigate to the appropriate folder for the workspace (“Workspace1” inour case), state the name (“MyFirstWorkspace” for instance) of the newly createdworkspace in “File name:” and confirm saving by clicking “Save”.Add files to projectEvery project must contain at least files listed in the Fig. 3.4. Some of them are included from theuser program, some are specified at the time of compilation, and some must be added in the projectitself. The last are “startup stm32f4xx.s”, “system stm32f4xx CLK168 HSE8.c” and the “C” filecontaining the user program.--The first two files are added to a project by right-clicking the name of the project in theworkspace window and selecting option “Add” - “Add Files” from a drop-down menu. Thisopens a window where appropriate files can be selected (from the “Workspace1\Project1”folder in our case).Additionally, a new file with the user program must be created. A new file is created byclicking the leftmost icon in the toolbar (a symbol for empty pages). An empty page appearsin the right IAR window, and the user can write new text onto the page. The user programmust start with the line “#include “stm32f4xx.h”” to allow the use of predefined names forregister in the microcontroller. The file must include at least the function “void main (void)”.The file should be saved in the folder of the choice (“Workspace1\Project1” in our case) andcan have any name, like “MyFirstFile.c”. This file must also be added to the project using thesame procedure as stated previously.The result is the IAR EWB window shown in Fig. 3.7.Figure 3.7: A fully defined minimal project within a workspace
Programming the STM32F4-Discovery6Define options for a projectThe process of altering the user provided source code into machine code consists of compiling andlinking. The IAR Embedded Workbench does both, but needs additional information for the process.This information can be supplied by clicking the option “Project” in the menu and then “Options.”.This brings out a window where options for the operation of the compiler and linker can be configured.-The IAR Embedded Workbench needs to know the microcontroller to compile for. Under “GeneralOptions” click “Device” and select the appropriate ST microcontroller (STM32F407VG) as shown inFig. 3.8.Figure 3.8: Select the microcontroller-Enable the CMSIS standard and libraries by ticking the appropriate tick box, Fig. 3.9.Figure 3.9: Enable the use of CMSIS standard
Programming the STM32F4-Discovery-7Set the level of optimization for compiler, Fig. 3.10. The default is “Low”, but we can safely select“Medium” to speed-up the execution of the program. Selecting the option “High” enables thecompiler to actually modify the user program in order to speed-up the execution, and is to be usedwith caution.Figure 3.10: Enable optimization, level “Medium”-The IAR Embedded Workbench needs to know the location of header and include files conformingto CMSIS standard to handle the peripherals, Fig. 3.11. The folders for these files are specifiedrelatively to the directory where the project is located (variable PROJ DIR ). From this directoryFigure 3.11: State the location of CMSIS header and include files
Programming the STM32F4-Discovery-8the relative path leads two levels up (“\.\.”) and then downwards to directory named“\STM32F4xx DSP StdPeriph Lib V1.1.0\Libraries\STM32F4xx StdPeriph Driver\inc”and“\STM32F4xx DSP StdPeriph Lib V1.1.0\Libraries\STM32F4xx StdPeriph Driver\src”, see Fig.3.2 for reference.The machine code can be prepared to run from flash memory, from RAM or from external RAM,but the linker must kno