Tutorial: Creating A Middleware Application Using CMSIS Components
Tutorial: Creating A Middleware Application Using CMSIS Components
Tutorial: Creating A Middleware Application Using CMSIS Components
Contents
Abstract ...................................................................................................................................................................................... 1
Introduction ............................................................................................................................................................................... 2
Software Stack ................................................................................................................................................................... 3
Prerequisites ............................................................................................................................................................................... 4
Set up the Workshop Environment .......................................................................................................................................... 4
Step 1: Create a Project............................................................................................................................................................. 5
Create a New Project for the Evaluation Board ....................................................................................................................... 5
Setup the Debug Adapter ......................................................................................................................................................... 6
Step 2: Add CMSIS-RTOS ....................................................................................................................................................... 7
Add and configure CMSIS-RTOS RTX for a simple Blinky application ................................................................................ 7
RTX Kernel Awareness ........................................................................................................................................................... 9
Step 3: Add USB Host with Mass Storage Support .............................................................................................................. 10
Configure the CMSIS-Driver for USB component ................................................................................................................ 10
Add the USB Host middleware component to the project ..................................................................................................... 11
Configure the CMSIS-Driver for the USB Host .................................................................................................................... 11
Configure the stack and thread memory resources ................................................................................................................ 12
Add the user code that accesses the USB storage device ....................................................................................................... 13
Step 4: Add the Graphical User Interface ............................................................................................................................. 17
Understanding the Hardware ................................................................................................................................................. 17
Add the Graphic Core and Graphics Display Interface.......................................................................................................... 17
Add the code to output “Hello World” to the LCD display ................................................................................................... 19
Step 5: Design and Add the Graphics to be Displayed on the LCD .................................................................................... 20
Configure GUIBuilder and Use it to Create the Graphics ..................................................................................................... 20
Add LogViewerDLG.c to the Project and Run the GUI ........................................................................................................ 21
Step 6: Add the Touch Screen Interface ................................................................................................................................ 22
Serial Wire Viewer Summary ................................................................................................................................................. 23
Document Resources ............................................................................................................................................................... 24
Books ..................................................................................................................................................................................... 24
Application Notes .................................................................................................................................................................. 24
Useful ARM Websites ........................................................................................................................................................... 24
Keil Products and Contact Information ................................................................................................................................ 25
Creating a Middleware Application using CMSIS Components with MDK Version 5
Introduction
This workshop explains how to create a software framework for a sophisticated microcontroller application using CMSIS and
Middleware components. During this workshop a demo application is created that implements the following functions:
Read the content of "Test.txt" file from a USB memory stick.
Show this content on a graphical display.
Provide an update button on a touch screen.
Software Stack
The application is created by using user code templates. These templates are part of software components such as the
Middleware, CMSIS-RTOS or the STM32F4xx Device Family Pack (DFP). Some other source files are automatically
generated such as the code that is creating the graphical user interface (GUI) on the external display (LogViewerDLG.c).
CMSIS-RTOS RTX is a real-time operating system that is part of MDK and adheres to the CMSIS specification. It is used
to control the application.
The board support files enable the user to quickly develop code for the hardware that is used here. It provides a simple API
to control LEDs, the touch screen and the LCD interface. Other components provide support for push buttons, joysticks, A/D
converters or other external devices.
Middleware provides stacks for TCP/IP networking, USB communication, graphics, and file access. The Middleware used
in this application is part of MDK-Professional and uses several CMSIS-Driver components.
CMSIS-Driver is an API that defines generic peripheral driver interfaces for middleware making it reusable across
compliant devices. It connects microcontroller peripherals with middleware that implements for example communication
stacks, file systems, or graphic user interfaces. CMSIS-Driver are available for several microcontroller families and are part
of the DFPs. The DFP contains the support for the device in terms of startup and system code, a configuration file for the
CMSIS-Driver and a device family specific software framework with hardware abstraction layer (HAL).
The basis for the software framework is CMSIS-Core that implements the basic run-time system for a Cortex-M device and
gives the user access to the processor core and the device peripherals. The device header files adhere to the CMSIS-Core
standard and help the user to access the underlying hardware.
The STM32F32F429IDiscovery Kit with the USB Stick connected to USB User OTG Connector.
The LCD displays the screen as created in the Graphical Display section in Step 4, 5 and 6. In the example given in this
tutorial, the display will be rotated 90 º from that shown above.
Prerequisites
To run through the workshop you need to install the following software. Directions are given below:
MDK-ARM Version 5.14 or later (https://www.keil.com/demo/eval/arm.htm).
A valid MDK-Professional license.
Keil::MDK-Middleware 6.3.0 or higher, ARM::CMSIS 4.3.0 or higher, Keil::ARM_Compiler 1.0.0 or higher
Keil::STM32F4_DFP 2.4.0 or later which includes the STM32F429I-Discovery Board Support Package (BSP). We
will download this from the Internet using Pack Installer.
STM32F429I-Discovery Kit (www.st.com/web/catalog/tools/FM116/SC959/SS1532/PF259090).
Note: Solder bridge SB9 must be bridged in order for the Serial Wire Viewer (SWV) to work. A soldering iron is
needed. If you do not solder SB9, the examples will work but the Event Viewer, Trace Records and Exception
windows will not display any information as these require SWV for their operation. See page 24.
Text snippets for copy and paste and completed projects are here: www.keil.com/appnotes/docs/apnt_268.asp
This tutorial assumes you have some experience with the MDK development tool and a basic knowledge of C.
6. Compile the project source files: There will be no errors or warnings displayed in the Build Output window. If
you get any errors or warnings, please correct this before moving on to configure the ST-Link V2 Debug Adapter.
What we have at this point: We have created a new MDK 5 project called USB.uvprojx. We have set the CPU clock speed,
added the CMSIS environment, a main.c file and compiled the source files to test everything.
8. Compile the project source files by clicking on the Rebuild icon . There will be no errors or warnings in the
Build Output window. If there are any errors or warnings, please correct them before continuing.
Demonstrating the Timer is Working:
1. Program the Flash and enter Debug mode: Click on the RUN icon.
TIP: To program the Flash manually, select the Load icon:
2. The program is running.
3. In Timer.c, near line 32, set a breakpoint by
clicking on the gray box. A red circle will appear.
The gray box indicates that assembly language
instructions are present and a hardware breakpoint
will be legal.
4. The program will soon stop here.
5. Click on RUN and in 1 second it will stop here again when the Timer2 is activated.
6. Remove the breakpoint for the next step.
What we have at this point: We added the RTX RTOS to your project. We enabled a periodic Timer and demonstrated that
the program is running.
5. Compile the project: There will be no errors or warnings in the Build Output window.
6. Program the Flash and enter Debug mode:
7. Click on RUN.
8. LED PG13 (green) will now blink according to the Timer you have created.
9. Leave the program running for the next steps.
TIP: In the LED_On function call: (0) is the green LED. Using (1) will blink the red LED.
What we have at this point: We have selected a LED driver from the CMSIS-Pack BSP to create a blinking a LED. We
have created a simple program that blinks this LED every 1 second using a timer.
The STM32F429I Discovery Kit provides a USB connector that interfaces with the USB OTG High-speed STM32F429
peripheral via the on-chip full-speed PHY (GPIOB.14 and GPIOB.15). The VBUS power on/off pin is active low on
GPIOC.4. The overcurrent detection pin is active low on GPIOC.5. Since
we are only using the USB Host interface we can ignore the remaining OTG
pins.
This schematic is part of the Software Pack for the STM32F4. You access
these documents using the Books tab. Other documents found here are
datasheets, STMicroelectronics Getting Started Guides, ARM compiler and
µVision manuals and more. The Books tab is located with the Project and
Functions tabs:
Add the user code that accesses the USB storage device
We will use a CMSIS-RTOS Thread to implement access to a file on the USB stick.
Add Thread.c:
1. Right click on Source Group 1 in the Project window. Select Add New item to Group 'Source Group1'…
2. Select User Code Template.
3. Under the CMSIS heading and in the Name column, select CMSIS-RTOS Thread.
4. Click on Add. This adds the file Thread.c to your project.
Add USBH_MSC.c and USBH_MSC.h:
1. Right click on Source Group 1 in the Project window again. Select Add New item to Group 'Source Group1'…
2. Select User Code Template.
3. Under the USB heading and in the Name column, select USB Host Mass Storage Access and click on Add.
4. The files USBH_MSC.c and USBH_MSC.h are now added to your project under the Source Group 1 heading.
5. These provide the relevant access functions for the USB storage device.
6. Select File/Save All or
Modify Thread.c:
To allow file access we add the following application code in the module Thread.c:
1. Double click on Thread.c to open it for editing.
2. Note near lines 17 and 18 there are two C lines: return(0); and }
3. Delete everything after these two lines but not including them. Start deleting with the void Thread (line 20).
Append this code to Thread.c:
#include "USBH_MSC.h"
char fbuf[200] = { 0 };
USBH_Initialize (0);
while (1) {
result = USBH_MSC_DriveMount ("U0:");
if (result == USBH_MSC_OK) {
f = fopen ("Test.txt", "r");
if (f) {
fread (fbuf, sizeof (fbuf), 1, f);
fclose (f);
}
}
osDelay (1000);
}
}
4. Make sure you have at least one newline (CR) at the end of the text. Otherwise, this will generate an easily fixed
warning at compilation time.
To start this new RTX Thread:
1. In main.c near line 77, add after extern void Init_Timers: extern void Init_Thread(void);
2. In main.c near line 112, add before osKernelStart ();: Init_Thread();
3. Select File/Save All.
What we have at this point: On this page we added the code to open, read and close the data in file Test.txt located in a
USB stick connected to USB User.
1. Compile the project: . You might get a warning from the USBH_MSC.c that can be safely ignored.
2. Enter Debug mode:
3. Click on the Memory 1 tab. Enter fbuf in this window:
4. Right click anywhere in the data field area and select
Ascii
5. Set a breakpoint in Thread.c on fclose (f) near
line 35.
6. Click on RUN. In a few seconds the text will
appear in the Memory 1 window.
7. The program will stop on the hardware breakpoint.
8. To repeat this sequence, click on the RESET icon and then RUN .
System and Thread Viewer:
1. Select the System and Thread
Viewer tab or select Debug
OS Support System and
Thread Viewer if it is not open.
Note the thread Thread is
running and the os_idle_demon
is Ready to run next. The other
other two threads are in wait
states.
2. Click on the RESET icon
and then RUN . You will see
the idle demon run as the
program runs and Thread go into
the running state when the breakpoint is hit.
3. Remove the breakpoint in Thread.c on the line fclose (f).
4. Click on RUN.
5. Leave the program running for the next steps.
Interfaces:
RGB
SPI
I2C
Modify main.c:
You can now demonstrate the display of the string “Hello World!” on the LCD:
1. In main.c near line 79 add: extern int Init_GUIThread (void);
2. In main.c near line 109 add: Init_GUIThread();
3. Select File/Save All or .
7. Click on Tools in µVision and the new GUIBuilder menu item will display like this:
8. Click on GUIBuilder and it will start.
Document Resources
Books
NEW! Getting Started MDK 5: www.keil.com/mdk5/.
A good list of books on ARM processors is found at: www.arm.com/support/resources/arm-books/index.php
µVision contains a window titled Books. Many documents including data sheets are located there.
A list of resources is located at: www.arm.com/products/processors/cortex-m/index.php (Resources tab).
The Definitive Guide to the ARM Cortex-M0/M0+ by Joseph Yiu. Search the web for retailers.
The Definitive Guide to the ARM Cortex-M3/M4 by Joseph Yiu. Search the web for retailers.
Embedded Systems: Introduction to Arm Cortex-M Microcontrollers (3 volumes) by Jonathan Valvano.
MOOC: Massive Open Online Class: University of Texas: http://users.ece.utexas.edu/~valvano/
Application Notes
1. Overview of application notes: www.keil.com/appnotes
2. NEW! Keil MDK for Functional Safety Applications: www.keil.com/safety
3. Using DAVE with µVision: www.keil.com/appnotes/files/apnt_258.pdf
1. Using Cortex-M3 and Cortex-M4 Fault Exceptions www.keil.com/appnotes/files/apnt209.pdf
2. CAN Primer using NXP LPC1700: www.keil.com/appnotes/files/apnt_247.pdf
3. CAN Primer using the STM32F Discovery Kit www.keil.com/appnotes/docs/apnt_236.asp
4. Segger emWin GUIBuilder with µVision™ www.keil.com/appnotes/files/apnt_234.pdf
5. Porting an mbed project to Keil MDK™ www.keil.com/appnotes/docs/apnt_207.asp
6. MDK-ARM™ Compiler Optimizations www.keil.com/appnotes/docs/apnt_202.asp
7. Using µVision with CodeSourcery GNU www.keil.com/appnotes/docs/apnt_199.asp
8. RTX CMSIS-RTOS in MDK 5 http://www.keil.com/pack/doc/cmsis_rtx/index.html
9. Lazy Stacking on the Cortex-M4 www.arm.com and search for DAI0298A
10. Sending ITM printf to external Windows applications: www.keil.com/appnotes/docs/apnt_240.asp
11. Barrier Instructions http://infocenter.arm.com/help/topic/com.arm.doc.dai0321a/index.html
12. Cortex Debug Connectors: http://www.keil.com/support/man/docs/ulinkpro/ulinkpro_cs_connectors.htm
For special promotional or quantity pricing and offers, please contact Keil Sales.
Contact sales.us@keil.com 800-348-8051 for USA prices.
Contact sales.intl@keil.com +49 89/456040-20 for pricing in other countries.
CMSIS-RTOS RTX is now provided under a BSD license. This makes it free.
All versions, including MDK-Lite, include CMSIS-RTOS RTX with source
code!
Keil includes free DSP libraries for the Cortex-M0, M0+, M3, M4 and M7.
Call Keil Sales for details on current pricing, specials and quantity discounts.
Sales can also provide advice about the various tools options available to you.
They will help you find various labs and appnotes that are useful.
All products are available from stock.
All products include Technical Support for 1 year. This is easily renewed.
Call Keil Sales for special university pricing. Go to www.arm.com/university
to view various programs and resources.
Keil supports many other Infineon processors including 8051 and C166 series
processors. See the Keil Device Database® on www.keil.com/dd for the complete list of Infineon support. This information is
also included in MDK.