You must have Keil MDK installed.
You can perform code coverage on any project that runs on a suitable target. Use one of the standard RTX examples that runs on the supplied Cortex-M FVP.
Open the uVision IDE, and click to open the Pack Installer
.
In the Devices tree, select ARM > ARM Cortex M3 > ARMCM3 (use search text box if necessary).
With this Device selected, click on the Examples tab, and Copy the CMSIS-RTOS2 Blinky (uVision Simulator) example.
Open the project in MDK, and build.
By default, this project uses the legacy simulator provided with MDK, which does not support code coverage.
To run on the FVP, open Options for Target
, navigate to the Debug pane, and select Models Cortex-M Debugger from the Use: pull-down.
Click Settings, and under Command, browse to one of the supplied FVPs in the Keil_v5/ARM/avh-fvp/bin/models folder of your MDK installation (for example, FVP_MPS2_Cortex-M3_MDK.exe).
Depending on the version of MDK you have installed the path to FVP executable may differ.
MDK versions before 5.37: Keil_v5/ARM/FVP
MDK versions 5.38, 5.39: Keil_v5/ARM/VHT
MDK version 5.40 or later: Keil_v5/ARM/avh-fvp/bin/models
Specify a Coverage File, and enable Load and Store for that file. Because of the way the data is created, code coverage can’t be updated in real-time.
If you’re using real hardware, no coverage file is needed. The data generates directly from the hardware, and the coverage updates in real-time.
Click OK to save settings, then start a debug session (Ctrl+F5).
If not already open, navigate to View > Analysis Windows > Code Coverage to enable that view.
Run the application for a few seconds, going through several iterations of the LEDs turning on and off. Observe that the Code Coverage data doesn’t update (with FVP). Close the debug session and start a new one to load the coverage data. The code coverage data now displays in the Code Coverage window.
Observe the report, which details the percentage of instructions that have been executed for each function. Browse for an interesting record, for example osRtxThreadDelayTick, within rtx_thread.c. You should see less than 100% of the instructions executed, including one or more instructions not fully executed.
Click on that function (in the Code Coverage window) to put the source and disassembly views in focus on that function.
Code that has been fully executed is highlighted in green.
Code that hasn’t been executed (such as some of the switch-cases) isn’t highlighted.
Code that hasn’t been fully executed is highlighted in orange or blue. This means, for example, a conditional branch instruction where only one of the true and false conditions has been met, so the other branch path of that instruction hasn’t been tested by that run. Orange means the branch is never taken, blue means it has always been taken.
It is also possible to output the code coverage information on the MDK command line. To show all, simply use:
coverage
You can also filter the report by source file or function. In the Code Coverage view by the Module pulldown, else on the command line with, for example:
coverage \blinky.c
coverage \blinky.c\main