Actions

EmSys

Developing Software for the Cortex-M Family Using Keil

From EdWiki


Introduction to Keil-MDK

In this tutorial we are going to use the Keil MDK-ARM (Microcontroller Development Kit for ARM) toolchain. The Keil MDK-ARM provides a complete development environment for all Cortex-M-based microcontrollers.

Keil mdk arm.png

Fig:The MDK-ARM Core installation contains an IDE, compiler, and debugger. Device and middleware support is added through software packs.


The MDK-ARM includes its own development environment called μVision (Micro Vision) which acts as an editor project manager and debugger. One of the great strengths of the MDK-ARM is that it uses the ARM “C” compiler. This is a very widely used “C\C11” compiler which has been continuously developed by ARM since their first CPU’s were created. The MDK-ARM also includes an integrated Real-Time Operating System (RTOS) called RTX. The MDK-ARM includes a DSP library which can be used on the Cortex-M4 and also the Cortex-M3. It also includes a software simulator which models each of the Cortex-M processors and the peripherals for a range of Cortex-M-based microcontrollers. While we can use the simulator to experiment with the different Cortex-M processors there comes a point when you will want to run your code on some real hardware.

Software Packs

The MDK-ARM is installed as a core toolchain. This consists of the μVision IDE, the compiler and debugger plus a utility called the pack installer. The core toolchain does not contain any support for specific Cortex-M microcontrollers. Support for a specific family of Cortex-M microcontrollers is installed through a software pack system. The pack installer allows you to select and install support for a family of microcontrollers. Once selected, a “Device Family Pack” will be downloaded from a Pack Repository website and installed into the toolchain. This software pack system can also be used to distribute software libraries and other software components.

Install Keil MDK-ARM

We need to install the MDK-ARM lite toolchain and the “Device Family Support” packs for the microcontrollers we will use in this tutorial.

  • First Download the Keil MDK-ARM lite from this link
    The MDK-ARM lite is a free version of the full toolchain which allows you to build application code up to a 32K image size. It includes the fully working compiler, RTOS and simulator and works with compatible hardware debuggers.
  • This is the direct link for the download
  • Run the downloaded executable to install the MDK-ARM onto your PC.


Start the μVision IDE Keil icon.png


Open the Pack Installer Keil pack icon.png

In the pack installer, click on the Devices tab and navigate through to select the Texas Instruments, Tiva C Series as shown in the following Fig:

Keil pack devices.png

Use the pack installer to download support files for the Tiva C Series family of microcontrollers.

In the Packs tab, select the Keil::TM4C_DFP and press the Install button.

In the Generic section of the Pack window select and install or update the following packs

Keil pack tm4c.png


Generic Pack
ARM::CMSIS
ARM::CMIS-Driver_Validation
Keil::ARM_Compiler
Table:Required generic software packs



Building a First Program

Now that the toolchain and the Packs are installed, we can look at setting up a project for a typical small Cortex-M4-based microcontroller. Once the project is configured, we can get familiar with the μVision IDE, build the code, and take our first steps with the debugger.

Create New uVision Project

  • Start a new project by selecting Project New μVision project...
    This will open a menu asking for a project name and directory. You can give the project any name you want but make sure you select the path. This directory contains the “C” source code files which we will use in our project.
  • Enter your project name and click Save.


Once you have selected the project directory and saved the project name, a new dialog with a device database will be launched. Here, we must select the microcontroller that we are going to use for this project. Navigate the device database and select Tiva C Series, the “TM4C123x Series” and then the TM4C123GH6PM and click OK. This will configure the project for this device; this includes setting up the correct compiler options, linker script file, simulation model, debugger connection, and Flash programming algorithms.

  • Next, select the microcontroller to use in the project
Keil project select device.png
Fig:: Select the TM4C123GH6PM from the device database.
  • When you have selected the TM4C123GH6PM, click OK.


Now, the IDE will display the “Run-Time Environment” (RTE) Manager. The RTE allows you to select software components that have been installed through the pack system and add them to our project. This allows you to build complex software platforms very quickly. For help on any of the software components, click on the blue link in the description column.

Keil project select rte.png
Fig:: The Run-Time Environment manager allows you to add software components to your project to rapidly build a development “platform.”


For now, we need to add the minimum support for our initial project. Select the “Core” and “Startup” components.

  • In the RTE, tick the “CMSIS::Core” box and the “Device::Startup box.”
    This adds the initial Startup code and also support functions for the Cortex-M processor.

Now that we have selected all of the components required by our project, press the OK button, and the support files will be added to the project window.

  • Double click on the startup_TM4C123.s file to open it in the editor.
  • Click on the configuration Wizard tab at the bottom of the editor window (Configuration files can be viewed as Text or as configuration wizards). This converts the plain text source file to a view that shows the configuration options within the file.
Keil startup config.png
Fig:: The configuration wizard allows you to view and modify #defines within a header or source file.

Changing the values in the configuration wizard modifies the underlying source code. In this case we can set the size of the stack space and heap space.

Add New Item to Project

  • Download blinky.c file
  • Switch back to the project view and add “C” source files to project.
  • Highlight the Source Group 1 folder in the project window
  • Right click and select Add Existing Files Group 'Source Group 1'..
Keil add file to group.png


  • This will open an “Add files to Group” dialog. In the dialog, locate/select blinky.c
  • Click on Add button followed by Close button


Keil add blinky file.png


The project should now contain blinky.c and the RTE components.

Keil blinky project window.png

Set Options for Target

  • Switch to Project Window and Open the Options for Target dialog. Place the cursor on the Target 1 and right click (Alt+F7)
  • Select Options for Target "Target 1"...
Keil target options menu.png
Fig:: Open the project global options.

This can be done in the project menu by right clicking the project name and selecting “Options for Target” or by selecting the same option in the project menu from the main toolbar.

  • The “Options for Target” dialog holds all of the global project settings.
Keil target options.png
Fig: The target menu defines the project memory map.
  • Set Optimization level to 0
Keil target optimization options.png

Build Project

  • Build the project by selecting Project Build Target
Keil project build target.png

This will compile each of the “.c” modules in turn and then link them together to make a final application program. The output window shows the result of the build process and reports any errors or warnings.

Keil build output window.png
Fig:: The final program size is reported in the Build Output window.

The program size is also reported

Section Description
Code Size of the executable image
RO Data Size of the code constants in the Flash memory
RW Data Size of the initialized variable in SRAM
ZI Size on uninitialized variables in the SRAM


If errors or warnings are reported in the build window clicking on them will take you to the line of code in the editor window.



Debugging Using Simulator

Set Simulator Options

Switch to Project Window and Open the Options for Target dialog.

  • Place the cursor on the Target 1 and right click
  • Select Options for Target "Target 1"...
Keil target options menu.png
Fig: Open the project global options.
  • Now select the Debug tab
  • The Debug menu is split into two halves. The simulator options are on the left and the hardware debugger is on the right.
Keil target options debug simu.png
Fig: Select the simulator and the TM4C123GH6PM simulation model.


  • Click the Use Simulator radio box.
  • Download LaunchPadDLL.dll and Place this file in the Drive:/Keil5_x/ARM/BIN folder.
  • Append to the Simulator Dialog DLL parameter -dLaunchPadDLL
  • Click OK to close the options for Target dialog.

Start Debugger

  • Now start the Debugger and run the code
Keil start debug menu.png
  • This will connect μVision to the Simulation model and download the project image into the simulated memory of the microcontroller. Once the program image has been loaded, the microcontroller is reset and the code is run until it reaches the main() or Reset_Handler ready to start debugging.
  • If it stops at Reset_Handler, set a breakpoint at main().
    You can set a breakpoint by moving the mouse cursor into a dark gray block next to the line number and left clicking. A breakpoint is marked by a red dot.
  • Start the code execution Keil run tool icon.png
  • Now It should stop at main()
Keil break at main.png


You can control execution of the code through icons on the toolbar. The code can be single stepped “C” or assembled a line at a time or run at full speed and halted. The same commands are available through the debug menu that also shows the function key shortcuts which you may prefer.

Keil debug tool bar.png

Debugger Windows

Register Window

The μVision debugger is divided into a number of windows that allow you to examine and control the execution of your code. The key windows are shown in the following Fig:

Keil register window.png

The Register window displays the current contents of the CPU register file (R0-R15). The Program Status Register (xPSR) and also the Main Stack Pointer (MSP) and the Process Stack Pointer (PSP).

Disassembler Window

Keil disasm window.png

As its name implies, the Disassembly window will show you the low-level assembler listing interleaved with the high-level “C” code listing. It is useful to be able to “read” the low-level assembly code to see what the compiler is doing. The disassembly window shows the absolute address of the current instruction, next, it shows the OP Code, this is either a 16-bit instruction or 32-bit instruction. The raw OP Code is then displayed as an assembler mnemonic. The current location of the program counter is shown by the yellow arrow in the left hand margin. The dark gray blocks indicate the location of executable lines of code.

Debug Command Window

Keil debug command window.png


The command window allows you to enter debugger commands to directly configure and control the debugger features. These commands can also be stored in a text file and executed as a script when the debugger starts.


You can control execution of the code through icons on the toolbar. The code can be single stepped “C” or assembled a line at a time or run at full speed and halted. The same commands are available through the debug menu that also shows the function key shortcuts which you may prefer.

Keil run commands.png


  • Now spend a few minutes exploring the debugger run control
    Use the single step commands, set a breakpoint and start the simulator running at full speed. If you lose what’s going on, exit the debugger by selecting debug/start/stop the debugger and then restart again.

View peripherals

  • Now view the state of the user peripherals.
    The simulator has a model of the whole microcontroller not just the Cortex-M processor, so it is possible to examine the state of the microcontroller peripherals directly.
  • Select Peripherals System Viewer GPIO GPIOF
Keil gpioportf window.png


This will open a window that displays the current state of the microcontroller GPIO port F. As the simulation runs we can see the state of the port pins. If the pins are configured as inputs, we can manually set and clear them by clicking the individual “Pins” boxes.

  • Select Peripherals TExaS Port F


Keil debug sim sw1.png
Keil debug sim sw1sw2.png












  • Toggle SW1 and SW2, watch for LEDs get Selected
  • You can do the same for the other Peripherals also!


UART0 Polled Mode Simulation

In this article, we will set up UART0 to do terminal echo. When a key is pressed at the terminal window of UART #1, the character is received by UART0 and it is sent out of UART0 back to the terminal.

  1. Create a New uVision Project as described here
    It's a good practice to create each project in a new folder.
    Name : uart0_polled_mode
  2. Download uart0_polled.c
  3. Add uart0_polled.c file to 'Source Group 1'..
    How to Add Existing Files to 'Source Group 1'..? You may refer here
  4. Set Options for Target as given here
  5. Build the Project
    How to Build the Project? You may refer here
  6. Configure Debugger to use simulator
    To run the program using Simulator, we have to configure the Debugger to use the simulator as given here
  7. Start the Debugger
Keil start debug menu.png

This will connect μVision to the Simulation model and downloads the project image into the simulated memory of the microcontroller. Once the program image has been loaded, the microcontroller is reset and the code is run until it reaches the main() function.

  • Open Serial Windows, and Select UART #1 ( View Serial Windows UART #1 )
Keil serial window menu.png

This opens a console type window that displays the output from a selected UART and also allows you to input values.

Keil uart1 window.png


  • Start code execution Keil run tool icon.png


Initial message is displayed in the UART #1 terminal window.

Keil uart1 emsys msg window.png


Now, Type any any character in the UART #1 terminal window, the same character is echoed on the terminal.

Keil uart1 emsys echo window.png


  • Before you stop/exit the Debugger
Keil start debug menu.png


Type ESC in the UART #1 terminal window. This will disable the UART0, otherwise keil unexpectedly closes!

ADC Simulation

In this article, we will use ADC0 which converts the analog input from AIN6 (J3.4 of LaunchPad) using sample sequencer 3 and software trigger continuously. AIN6 channel is on PD1 pin.

  • For more details, click on this link, then select ADC Tab.
  1. Create a New uVision Project as described here
    It's a good practice to create each project in a new folder.
    Name : adc0_ain6_ss3
  2. Download adc_ain6_ss3.c
  3. Add adc_ain6_ss3.c file to 'Source Group 1'..
    How to Add Existing Files to 'Source Group 1'..? You may refer here
  4. Set Options for Target as given here
  5. Build the Project
    How to Build the Project? You may refer here
  6. Configure Debugger to use simulator
    To run the program using Simulator, we have to configure the Debugger to use the simulator as given here
  7. Start the Debugger
Keil start debug menu.png

This will connect μVision to the Simulation model and downloads the project image into the simulated memory of the microcontroller. Once the program image has been loaded, the microcontroller is reset and the code is run until it reaches the main() function

  • Open Watch Windows (Watch #1) ( View Watch Windows Watch #1 )
Keil watch window menu.png
  • Just above main() is the declaration for a variable called adc_value. Highlight this variable, right click, and select “Add adc_value” to Watch #1
Keil adc watch window 1.png
  • Select Peripherals TExaS ADC
Keil texas adc menu.png


  • Start code execution Keil run tool icon.png


  • Now, move the Slide pot left-to-right/right-to-left


Keil texas adc sim win.png
Keil texas adc diagram.png











  • Observe the ADC value changing in the watch window


Keil adc watch window.png