Create an USB Application Using CMSIS-Driver Klaus Koschinsky Senior Field Applications Engineer 1
Contents Project Overview CMSIS Technical Overview Development Tools Project Creation (Demo) 2
Create a USB Application Requirements Read data from a USB Memory Stick Display these data [to a display] Software Features USB Host Stack File System Graphic User Interface Provide a update button [on a touch screen] Touch Screen Support Implementation easy to expand for further feature requests Software Concept (RTOS Kernel) Drivers for Peripherals Scalable to different hardware 3
Software Stack for the USB Application Application Code Run-Time Environment Real Time OS Peripheral HAL (Device Specific) MDK Professional Middleware Graphics File System USB Host CMSIS-Driver: USB Host, I 2 C, SPI CMSIS-CORE MCU ARM Cortex -M Other Peripherals USB Host Controller Communication Peripherals Ready-to-use made Software Components to reduce application development time Run-Time Environment must be easy to combine, to configure, and to adapt for the MCU hardware/peripherals and for the application specific requirements Embedded Applications need access to all levels of the system 4
Target Hardware for the USB Application Evaluation Kit for Cortex-M Microcontroller 5 Discovery kit for STM32F429ZI MCU
6
7 CMSIS Technical Overview
Software Complexity The Challenge Well-known issues that drive software development costs Increasing product requirements that are implemented by software Hardware problems tend to become compensated by software Hardware uses defined interfaces that simplify re-use Software components are hard to integrate Software Standards and Software Components are key for productivity! 8
Cortex Microcontroller Standard (CMSIS) Vendor-independent Standard for hardware manufacturers and tool vendors Common Software layers and interfaces for all Cortex-M processor based devices CMSIS-CORE API for Cortex-M processor and core peripherals CMSIS-DSP DSP Math Library with more than 60 functions CMSIS-SVD XML system view description for peripheral debugging CMSIS-RTOS API for RTOS integration CMSIS-DAP Firmware for Debug Access Port CMSIS-DRIVER API for peripheral driver interfaces CMSIS-PACK XML description for software components, device parameters, board support 9
CMSIS-CORE Consistent API for Cortex-M processor core and device peripherals core_<cpu>.h CMSIS CPU & Core Access core_cminstr.h CPU Instruction Access core_cm4_simd.h SIMD Instruction Access (Cortex-M4 only) core_cmfunc.h Core Peripheral Functions startup_<device>.s CMSIS Device Startup Interrupt Vectors system_<device>.c CMSIS System & Clock Configuration <device>.h CMSIS Device Peripheral Access system_<device>.h CMSIS System & Clock Configuration 10 User Application #include <device>.h main() {... } CMSIS-CORE Device Files (Silicon Vendor) User Program CMSIS-CORE Standard Files (ARM)
CMSIS-CORE - Example #include <device.h> // File name depends on device used void SysTick_Handler (void) { // SysTick Interrupt Handler ; // Add user code here } void TIM1_UP_IRQHandler (void) { // Timer Interrupt Handler ; // Add user code here } void timer1_init(int frequency) { // Set up Timer (device specific) NVIC_SetPriority (TIM1_UP_IRQn, 1); // Set Timer priority NVIC_EnableIRQ (TIM1_UP_IRQn); // Enable Timer Interrupt } 11 // The processor clock is initialized by CMSIS startup + system file void main (void) { // user application starts here if (SysTick_Config (SystemCoreClock / 1000)) { : // Handle Error } timer1_init (); // setup device-specific timer while (1) {} // Endless Loop (the Super-Loop) } // BOLD: CMSIS Features
CMSIS-RTOS Generic RTOS Interface for Cortex-M Microcontrollers Implementation Based on RTX: 6KB memory foot print Includes Source Code under Open Source License (BSD) 12
CMSIS-RTOS: Mail Queue Memory Pool with Message Passing Thread or ISR can exchange data A single data owner at a time Mailbox FIFO for multiple messages Thread or ISR On overruns: forces task switch or error notification Deterministic time behavior (independent from current load) Timeout prevents from application hang-up put Mail Queue...... memory blocks get Thread or ISR extern osmailqid mailbox1; void job1 (void) { // Thread 1 struct msg *message; } message = osmailalloc (mailbox1, 12); // fill message content osmailput (mailbox1, message); timeout osmailqdef (mail1, 20, struct msg); osmailqid mailbox1; void job2 (void) { // Thread 2 osevent msg; mailbox1 = osmailcreate(osmailq (mail1), osthreadgetid ()); while (1) { msg = osmailget (mailbox1, 100); // process message content osmailfree (mailbox1, msg); timeout } } 13
Files for CMSIS-RTOS Layer RTOS_config.c CMSIS RTOS Configuration cmsis_os.h CMSIS Standard RTOS Interface RTOS.LIB CMSIS Compliant RTOS Library User Application #include cmsis_os.h main() {... } User Program CMSIS-RTOS Files (RTOS Vendor) 14
CMSIS-RTOS Example #include "cmsis_os.h" // CMSIS-RTOS header file void job1 (void const *argument) { // thread function 'job1' while (1) { : // execute some code osdelay (10); // delay execution for 10 milliseconds } } osthreaddef(job1, ospriorityabovenormal, 1, 0); // define job1 as thread function void job2 (void const *argument) { // thread function 'job2' while (1) { : // execute some code osdelay (20); // delay execution for 20 milliseconds } } osthreaddef(job2, osprioritynormal, 1, 0); // define job2 as thread function int main (void) { // program execution starts here oskernelinitialize (); // initialize RTOS kernel : // setup and initialize peripherals osthreadcreate (osthread(job1)); osthreadcreate (osthread(job2)); oskernelstart (); // start kernel with job1 execution } 15 // BOLD: CMSIS-RTOS Features
CMSIS-DSP Library Over 60 functions for various data types C Source Code, optimized for all Cortex-M based devices For CMSIS compliant C Compilers (ARM/Keil, IAR, GCC) 16
CMSIS-DAP Debug Access Port Firmware (typical use case: low-cost Starter Kits) Standard debug connection over USB to a MCU Cost reduction and multiple tool support on evaluation kits Running on all major development environments (MDK, DS-5, IAR, etc.) USB CMSIS-DAP Debug FW On Cortex-M MCU Main Cortex-based MCU or SoC Low-cost evaluation board 17
CMSIS-SVD System View Description for Peripherals Purpose Peripheral Views in debugger CMSIS Core device header files Scope } consistency Formalized description of the Programmer s View Covering peripherals, registers, bit fields and bit values Format Defined by written specification and XML Schema Validated for semantic consistency 18
CMSIS-Pack: Ready-to-Use Components Delivery Mechanism for software components, device parameters, and evaluation boards Variant Device Family Pack CMSIS Pack Middleware Pack Board Support Pack In-house Software Pack Source Silicon Vendor, Tool Vendor ARM Silicon Vendor, Tool Vendor, 3rd Party Board Vendor Tool User Use Case Deploy support for new MCU families Standard delivery of CMSIS components Simplify integration of pre-build middleware Support of evaluation boards with interfaces and example projects Supply and update software components within a company 19
CMSIS-Driver: Ready-to-Use Peripheral I/O API Interfaces Peripheral Driver Interfaces for Middleware and User Applications CMSIS-Driver USB Device Driver USART Driver Ethernet PHY Ethernet MAC SPI Driver MCI Driver NAND Flash Driver USB Host Driver RTE_Device.h Configuration File For generic USB device communication Universal Synchronous Asynchronous Receiver/Transmitter driver. Interface to Ethernet PHY Interface to Ethernet MAC Serial Peripheral Interface Bus driver Memory Card Interface for SD/MMC memory NAND Flash Memory interface driver For generic USB host communication Central driver configuration file (implementation specific) 20
CMSIS-Driver: Interfaces Attributes and Functions common in all CMSIS-Drivers Each CMSIS-Driver defines an Access Struct for calling the various driver functions Each peripheral (that is accessed via a CMSIS-Driver) has one Driver Instance Depending on the device a CMSIS-Driver implementation may support several Driver Instances Driver Functions: GetVersion: can be called at any time to obtain version information of the driver interface. GetCapabilities: can be called at any time to obtain capabilities of the driver interface. Initialize: must be called first. Initializes the software resources used by the interface. SignalEvent: is an optional callback function that is registered with the Initialize function. PowerControl: Controls the power profile of the peripheral and may be called after Initialize. Uninitialize: It will free all software resources used by the interface. Control: Function to configure communication parameters or execute control functions. Additional functions are specific to each driver interface (for example Send, Receive) 21
CMSIS-Driver: SPI: Functions and Access The CMSIS-Driver functions for each peripheral type are in a access structure: typedef struct _ARM_DRIVER_SPI { ARM_DRIVER_VERSION (*GetVersion) (void); ARM_SPI_CAPABILITIES (*GetCapabilities) (void); int32_t (*Initialize) (ARM_SPI_SignalEvent_t cb_event); int32_t (*Uninitialize) (void); int32_t (*PowerControl) (ARM_POWER_STATE state); int32_t (*Send) (const void *data, uint32_t num); int32_t (*Receive) (void *data, uint32_t num); int32_t (*Transfer) (const void *data_out, void *data_in, uint32_t num); uint32_t (*GetTxCount) (void); uint32_t (*GetRxCount) (void); int32_t (*Control) (uint32_t control, uint32_t arg); ARM_SPI_STATUS (*GetStatus) (void); } const ARM_DRIVER_SPI; Several peripherals of the same type use multiple instances of the driver access struct. The name of each driver instance reflects the name of the peripheral. ARM_DRIVER_SPI Driver_SPI1; // access functions for SPI1 interface ARM_DRIVER_SPI Driver_SPI2; // access functions for SPI2 interface ARM_DRIVER_SPI Driver_SPI3; // access functions for SPI3 interface 22
CMSIS-Driver: Non-Blocking Data Transmit Functions Driver Driver uses ISR or DMA to start data transfer using the peripheral. Once data transfer is started, the _Send function returns. Peripheral continues reading the transfer buffer. Once finished, the transfer complete event is signaled using the _SignalEvent callback. Application Code _Send Middleware fills a transfer buffer with data and calls the _Send function. Middleware continues execution or waits for the transfer complete event. Note that the transfer buffer cannot be refilled since it is still processed. _GetCount During data transfer, the number of sent items can be monitored. Middleware may partially fill the transfer buffer. _SignalEvent _Send Now, middleware can completely fill the transfer buffer and initiate the next _Send. Time 23
CMSIS-Driver Example #include "Driver_SPI.h" extern ARM_DRIVER_SPI Driver_SPI0; // SPI Driver void myspi_callback(uint32_t event) { switch (event) { case ARM_SPI_EVENT_TRANSFER_COMPLETE: ossignalset(tid_myspi_thread, 0x01); break; // Success : } const uint8_t testdata_out[8] = { 0, 1, 2, 3, 4, 5, 6, 7 }; // Test data buffers void myspi_thread(void const* arg) { } ARM_DRIVER_SPI* SPIdrv = &Driver_SPI0; SPIdrv->Initialize(mySPI_callback); SPIdrv->PowerControl(ARM_POWER_FULL); // Initialize the SPI driver // Power up the SPI peripheral SPIdrv->Control(ARM_SPI_MODE_MASTER ARM_SPI_CPOL1_CPHA1 ARM_SPI_MSB_LSB ARM_SPI_SS_MASTER_SW ARM_SPI_DATA_BITS(8), 10000000); // Configure the SPI to Master, 8-bit mode @10000 kbits/sec SPIdrv->Control(ARM_SPI_CONTROL_SS, ARM_SPI_SS_ACTIVE); while (1) { SPIdrv->Send(testdata_out, sizeof(testdata_in)); evt = ossignalwait(0x01, 100); : // Activate Slave Select to enable the connected SPI chip // Transmit some data // Wait for event: Transfer Complete // BOLD: CMSIS-Driver Feature 24
CMSIS-Driver Microcontroller Device Software Packs Middleware Startup/System Control Structs USB USB Controller USB Device Driver USBD0 USB Device Rx1/Tx1 USART USART Driver USART1 Ethernet Ethernet PHY Ethernet PHY ETH_PHY0 TCP/IP Networking Ethernet MAC Ethernet MAC ETH_MAC0 SPI1 SPI2 SPI #1 SPI #2 SPI Driver SPI1 SPI2 Graphics SDIO0 SDIO MCI Driver MCI0 File System I/O Memory Controller NAND Flash Driver NAND0 USB USB Controller USB Host Driver USBH0 USB Host 25 RTE_Device.h Configuration File
Files for CMSIS-Driver Layer USBD0_device.c CMSIS-Driver compliant Peripheral Driver Driver_USB.h CMSIS Standard Driver Interface Driver_Common.h CMSIS Standard Driver Interface RTE_Device.h Device Configuration File (Common for all drivers) User Application #include Driver_USB.h main() {... } CMSIS-Driver Device-Specific Files User Program CMSIS-Driver API Definition Files (ARM) 26
Development Tools for our Project 27
MDK Professional Best-in-class C/C++ Compiler Co-developed with ARM processors Proven in thousands of projects Pre-emptive Deterministic Source included EHCI/OHCI Interface Host & OTG mode Low & Full Speed ARM C/C++ Compiler µvision Project Manager, Editor & Debugger CMSIS-RTOS RTX USB Host Network File System Integrated Development Environment for edit, debug, and trace Tight integration of all MDK components Optimized for MCU systems Extensive protocol support Feature-rich applications Extensive media support FAT 12/16/32 Standard driver class support small memory footprint Low, Full, and High Speed USB Device Graphics Widget Library included Touch screen support Low level drivers 28
ULINKpro Debug and Trace Adapter JTAG SWD SWO 100 Mpbs ETM Streaming www.keil.com/coresight ULINKpro Cortex-M processors with up to 200 MHz Serial Wire Trace Capturing up to 100 Mbit/sec (Manchester Mode) ETM Trace Capturing up to 800 Mbit/sec Virtually un-limited Trace Buffer Unique Streaming Trace feature supports Code Coverage and Performance Analysis 29
Middleware: Network Add TCP/IP network connectivity via Ethernet or Serial Extensive range of service applications included Designed for Cortex-M devices with small memory footprint 30
Middleware: Graphic Add a graphical user interface (GUI) to an application quickly Compatible with hundreds of display controllers Touch-screen support for many TFT LCDs 31
Middleware: USB Host USB Host Component Class HID Human Interface Device MSC Mass Storage CORE USB Host Driver USBH High-Speed Full-Speed Connect common USB devices easily Support for various popular microcontroller host controllers High performance and small footprint USB 1.1 Low Speed (1.5 Mbit/s) and Full Speed (12 Mbit/s) Supports USB Flash drives and card storage devices 32
Middleware: USB Device USB Device Component Class HID Human Interface Device CDC Communication Device MSC Mass Storage ADC Audio Device CORE USB Device Driver USBD High-Speed Full-Speed Standard USB Device classes supported Plug N Play: No need to develop a driver for Windows or Linux hosts USB 1.1 Low Speed (1.5 Mbit/s) and Full Speed (12 Mbit/s) 33
Middleware: File System File System Component Drive RAM NAND Flash NOR Flash USB MSC Mass Storage Memory Card SD/SDHC/MMC CORE Variants: Long File Names Short File Names Driver NAND Flash NOR Flash MCI SPI Enable applications to work with locally stored data Storage devices: ROM, RAM, Flash, and SD/MMC/SDHC/eMMC FAT32 file system supported Simultaneous access to multiple storage devices 34
Web Portal for Software Packs 35
36 Project Creation
Keil MDK Workflow using Software Packs Install Select Configure Implement Download relevant Software Packs from Web Portal Choose device and select required middleware Setup parameters of the Run-Time Environment Use code templates for faster software development 37
Incremental Steps to Create the USB Application 1. Create Project for the STM32F429 Evaluation Board 2. Add and configure CMSIS-Core and CMSIS-RTOS for a simple blinky application 3. Add and configure USB Host middleware component Connect this component to the USB Host CMSIS-Driver Add and Configure Flash File System middleware component for USB Storage Connect the file system to the USB Host middleware component 4. Add and Configure Graphical User Interface Connect the LCD driver to SPI CMSIS-Driver 5. Create the dialog that is displayed 6. Add and Configure Touch Screen Interface Connect this component to the I 2 C CMSIS-Driver 38
Lets Do It! Demo Time 39
Thank You The trademarks featured in this presentation are registered and/or unregistered trademarks of ARM Limited (or its subsidiaries) in the EU and/or elsewhere. All rights reserved. Any other marks featured may be trademarks of their respective owners 40