II. RTW BUILD METHODS In order to understand how RTW generates C code and produces an executable image, the build process can be divided into three st

Similar documents
SIMULATION OF MCU HARDWARE PERIPHERALS

Development of Simulink blockset for embedded system with complex peripherals

FMI Kit for Simulink version by Dassault Systèmes

Rapid Prototyping System for Teaching Real-Time Digital Signal Processing

Introduction to Microchip-SIMULINK Blocksets and MATLAB Plug-in for MPLAB IDE

Generating ASAP2 Files

Chapter 7. Hardware Implementation Tools

TRC File Changes with dspace Release 2014-A and Later. Migration Guide

Real-Time Windows Target

Simulink 를이용한 효율적인레거시코드 검증방안

Automatic Code Generation Technology Adoption Lessons Learned from Commercial Vehicle Case Studies

Introducing Simulink Release 2012b for Control System Development Mark Walker MathWorks

Embedding Simulink Algorithms into ifix and GE Fanuc Series 90 PLCs

Simulink to Embedded Hardware Paul Peeling MathWorks

Improvement of the Communication Protocol Conversion Equipment Based on Embedded Multi-MCU and μc/os-ii

Combining the Power of DAVE and SIMULINK

Processor Expert Software RAppID Suite Overview

Real-Time Workshop Embedded Coder 4

Tutorial - Exporting Models to Simulink

xpc Target 4 Device Drivers

Production Code Generation Introduction and New Technologies

for Freescale MPC55xx/MPC56xx Microcontrollers V2.10 Quick Start

Embedded Target for TI C6000 DSP 2.0 Release Notes

BASICS OF THE RENESAS SYNERGY PLATFORM

Team-Based Collaboration in Simulink

Optimize DSP Designs and Code using Fixed-Point Designer

Better than Hand Generating Highly Optimized Code using Simulink and Embedded Coder

BASICS OF THE RENESAS SYNERGY TM

RTA-OSEK Freescale MPC55xx/56xx with the WindRiver Compiler

Model Based Design Development Environment for Simulink /Stateflow Product Specification

Real-Time Simulation Engine Implementation in Automotive FlexRay Communication

AN OPEN-SOURCE VHDL IP LIBRARY WITH PLUG&PLAY CONFIGURATION

RTA-OSEK Texas Instruments TMS570 with the TI Compiler

HCS12 BDM Getting Started V4.3

Embedded Target for Motorola MPC555. For Use with Real-Time Workshop. Modeling. Simulation. Implementation. User s Guide Version 1

Usable Simulink Embedded Coder Target for Linux

Copyright 2014 Xilinx

Model Based Development Toolbox MagniV for S12ZVC Family of Processors

How Real-Time Testing Improves the Design of a PMSM Controller

ConfigurationDesk/RTI. Compatibility with Toolboxes and Blocksets Provided by MathWorks

ConfigurationDesk/RTI. Compatibility with Toolboxes and Blocksets Provided by MathWorks

ERIKA Enterprise LWIP Tutorial

Avnet Speedway Design Workshop

Feaser

Accelerating Innovative Design Using Low-Cost Hardware Andrew Bennett

What s New with the MATLAB and Simulink Product Families. Marta Wilczkowiak & Coorous Mohtadi Application Engineering Group

8438/8838 User Manual User Manual of the 8438/8838 MATLAB Embedded Controllers

RX Smart Configurator

Model-Based Design Toolbox

HANcoder STM32 Target Getting Started

Embedded Coder Getting Started Guide. R2011b

Integrated Environment for Embedded Control Systems Design

EUROScope lite 16FX Reference Manual

3.1. User Guide. Processor Expert and Device Initialization PROCESSOR EXPERT FOR FREESCALE HCS12 & HCS12X FAMILY. Freescale Semiconductor, Inc.

NEW CEIBO DEBUGGER. Menus and Commands

Vision Toolbox for MATLAB

DT Measure Foundry TM is a powerful software package designed for test and measurement,

A MATLAB TOOLBOX FOR REAL-TIME CONTROL USING C167 MICROCONTROLLERS

SKP16C26 Tutorial 1 Software Development Process using HEW. Renesas Technology America Inc.

Release Notes. S32 Design Studio for ARM v1.1

A Matlab/Simulink Simulation Approach for Early Field-Programmable Gate Array Hardware Evaluation

DS1103 PPC Controller Board

QuaRC 2.0. Installation Guide. Single-User and Network Installations/License Configuration (Windows, QNX, and Gumstix Verdex)

Quick Start Guide TWR-S08PT60. 5-Volt S08P Family of 8-bit MCUs for Industrial and Appliance Applications TOWER SYSTEM

CodeWarrior Development Studio for Freescale HCS12(X) Microcontrollers

Creating support for fully automatic code generation for Cerebot MX7cK hardware from Simulink environment

RTA-OSEK Infineon TriCore with the Green Hills Software Compiler

Embedded Training MakeICT Workshop

Addressing Fixed Point Design Challenges

Quick Installation Guide: QUARC TM on NI ELVIS III

MODEL BASED HARDWARE DESIGN WITH SIMULINK HDL CODER

Automated Generation Of Code From Simulink To An Event-Based Simulation Framework

Prototyping and Deployment of Real- Time Signal Processing Algorithms for Engine Control and Diagnosis

CodeWarrior Plug-in for Freescale HCS12(X) Device Initialization User Manual

Misc. Third Generation Batch Multiprogramming. Fourth Generation Time Sharing. Last Time Evolution of OSs

Managing HIL, SIL, and MIL Simulation with SIMulation Worbench TM

Fredrick M. Cady. Assembly and С Programming forthefreescalehcs12 Microcontroller. шт.

Accelerating FASGW(H) / ANL Image Processing with Model-Based Design Issue 2

From Design to Production

The Development of Mobile Shopping System Based on Android Platform

Real Time Testing of PMSM Controller using xpc Target Turnkey solution

Component-based Software Development for Microcontrollers. Zhang Zheng FAE, ARM China

QTP Course Content; Introduction

A Novel Digital Relay Model Based on SIMULINK and Its Validation Based on Expert System

1.1 The hand written header file

A Software Development Toolset for Multi-Core Processors. Yuichi Nakamura System IP Core Research Labs. NEC Corp.

VALLIAMMAI ENGINEERING COLLEGE

Hands-On Workshop: Xtrinsic FXLC95000CL Sensing Platform with Intelligent Sensing Framework (ISF)

Research and Design of Universal Proportional Valve Controller

Model-based Design/Simulation

ConfigurationDesk/RTI. Compatibility with Toolboxes and Blocksets Provided by MathWorks

What s New MATLAB and Simulink

Contents. Cortex M On-Chip Emulation. Technical Notes V

DEVELOPMENT OF USER FRIENDLY DATA ACQUISITION AND ACTUATION SYSTEM ON EMBEDDED PLATFORM

Automatic Code Generation at Northrop Grumman

Reuse MATLAB Functions and Simulink Models in UVM Environments with Automatic SystemVerilog DPI Component Generation

Model-based Design/Simulation

Research on Automotive UDS Diagnostic Protocol Stack Test System

AscTec Simulink toolkit

Implementing MATLAB Algorithms in FPGAs and ASICs By Alexander Schreiber Senior Application Engineer MathWorks

Transcription:

Embedded C Code Generation and Embedded Target Development Based on RTW-EC Feng Luo and Zhihui Huang College of Automotive Engineering, Tongji University, Shanghai, China, E-mail: luo_feng@tongji.edu.cn College of Automotive Engineering, Tongji University, Shanghai, China, E-mail: hzh86127@sina.com Abstract This paper focus on the methods of the build process of Real-Time Workshop (RTW), the influencing factors on code efficiency and development of a custom embedded target. In order to generate embedded C code and download it to a user-specified micro-controller (Freescale MC9S08DZ60 for example in this paper) conveniently, an embedded target is developed. The embedded target developing consists of the target device drivers and the interface to CodeWarrior IDE. The user can develop custom device drivers by following the steps of creating a C MEX S-function, masking the blocks and writing TLC files. In particular, this paper provides the ways of designing two special blocks for interrupt service routine (ISR) and target resource configuration. Through the comparison between automatic generation codes and codes written by bands in PC environment and embedded system, it is concluded that automatic generation codes is accurate and efficient, but the execution time is slower than the code written by hand in micro-controller. Keywords-RTW-EC; embedded system; code generation I. INTRODUCTION AND BACKGRAND Real-Time Workshop (RTW) can generate C code for target micro-controllers from the models in Simulink/Stateflow. The codes can be used in real-time and non-real-time occasions, such as accelerator mode in simulation, rapid prototyping and hardware-in-the-loop (HIL) testing. Users can modify the generated code to execute in an environment independent form Matlab environment. C code generation is the basis of the application of RTW. It can generate C codes that are ANSI/ISO compatible, which can run in the target micro-controller and real-time operations system (RTOS) [1]. Another very important extended product is Real-Time Workshop Embedded Coder (RTW-EC). It can generate efficient and professional handwriting C code for special target micro-controllers. To enable users to generate executable production code for a specific CPU or development board, using a specific development environment, the targets bundled with the RTW-EC product are suitable for many different applications and development environments. Although Target Supports PackageTM supports the products from many famous companies (Analog Devices Blackfin, Freescale MPC5xx, Infineon C166, TI's C2000, C5000 and C6000), it does not provide target support package for each kind of micro-controller, especially for those 8-bit micro-controllers. This means that the development of embedded systems can t be completely supported. So, for other micro-controller, users need to develop a custom embedded target. Paper [2] presented the library modules developed for an intelligent robot named NXTway by RWTH-Aachen University in Germany through self-made S-functions and Target Language Compiler (TLC) file. The micro-controller is provided by LEGO in USA. It belongs to 32-bit ARM7 series, in which an OSEK RTOS is embedded. The micro-controller also supports USB and Bluetooth communications. The RTW-EC library blocks are customized through S-function and TLC files. S-function is used to define Simulink blocks, while TLC is used to insert C codes of corresponding API functions. In addition to the above mentioned method, paper [3] introduces a method, which uses Microsoft Component Object Model (COM) to connect RTW and the Processor Expert blocks in CodeWarrior. Users don t have to write C code by themselves. All the C code from the hardware layer to the application layer can be automatic generated. Figure 1 shows the PEERTTM library file used in the paper. Figure 1. PEERT TM library block Another method is developed to transplant C code in the paper [4], which combines the automatic code generation for the application layer and handwriting for the hardware layer. It is a simple method to apply the code generated by RTW-EC in the micro-controller, but not a convenient way in practical application. 532

II. RTW BUILD METHODS In order to understand how RTW generates C code and produces an executable image, the build process can be divided into three steps as flow: Figure 2. RTW build process The build process begins with the Simulink software compiling the block diagram. During this stage, Simulink compiles an intermediate representation of the model. This intermediate description is stored in a language-independent format in the ASCII file model.rtw. This file can be modified by the user to deliver important parameters. The RTW code generator uses the target language compiler and a supporting TLC function library to transform the intermediate model description stored in model.rtw into target-specific code (e.g. model.c). The TLC scripts specify how to generate code from the model, using the model.rtw file as input. The user can also insert API functions into the TLC file for special embedded target. The RTW creates model.mk from a system template file, system.tmf, which is designed for your target environment. You have the option of modifying the template makefile to specify compilers and additional information used during the creation of the executable. But for some special embedded targets, such FreescaleTM micro-controllers mentioned in this paper, CodeWarrior Integrated Development Environment (IDE) project file is used as the interface. III. THE INFLUENCING FACTORS ON CODE EFFICIENCY The code efficiency in this paper mainly refers to the ROM and RAM consumption and the execution time required executing the RTW generated code from the model. Reducing the ROM and RAM consumption and the execution time indicates improving the code efficiency. In order to improve the efficiency of the code generated by RTW, data type, data structure and code format must be considered. Take the data type for example. The data type definition has a great influence on code efficiency. In the paper [5] provided by MathWorks Inc, several tips are proposed to reduce the ROM and RAM consumption and the execution time. The most important and simple way to achieve that is to use integral data instead of decimal data, or use fixed-point data instead of floating-point data. However, using integral data can not absolutely avoid floating-point operation. take the flowing operation for example: sf_lt = ( t - trace_m ) * ratio / 90; Because division operation relate to floating-point operation, RTW is able to generate a complex function, which increase the ROM and RAM consumption and the execution time: sf_lt = div_s16_sr((t - (int16_t)trace_m) * (int16_t)ratio, 45, (uint16_t)1); The user can also optimize memory usage and performance of code generated from your model by the RTW product a number of ways [6]. One of the most important ways is unrolling for-loop, which means, the code can be generated as a separate statement for each element of the signal. According to a test using freescale micro-controller, unrolling for-loop can reduce execution time by 6-7 times. IV. EMBEDDED TARGET DEVELOPMENT Developing a custom embedded target enable the user to to generate executable production code for a specific CPU or development board, support I/O devices on the target hardware by incorporating custom device driver blocks into your models and configure the build process for a special compiler or development/debugging environment. There are several types of targets intended for different use cases. This paper mainly focuses on the baseline target, which integrates RTW-EC with one or more popular crossdevelopment environments. According to the RTW build process, there are lots of files of a custom target are located in a hierarchy of directories. Figure 3 illustrates the components of a custom target. To modify every file (such as control files) brings heavy workload. The user can inherit the options from Embedded Real-Time (ERT) target, which is the RTW-EC system target file (STF). So developing a simple embedded target only requires the following two components: I/O devices on the target hardware Interface to a special compiler or development/ debugging environment. 533

Figure 5. simplified digital I/O block Figure 3. The components of a custom target A. Device Driver Developing Device drivers that communicate with target hardware are essential to many real-time development projects. In the build process, the device drivers can be generated to C code which is used to write/read the registers of the target micro-controller. In traditional way, setting the registers by hand may be troublesome and waste a lots of time. What s more, the registers of different micro-controllers are distinct from each other. The following figure demonstrates the DZ60 library block for Freescale MC9S08DZ60. Take the digital I/O block for example, after activating the I/O port dialog by double clicking the block, the user can choose the I/O port in the pop-up and enable/disable output and pull-up function in the check box conveniently. Figure 4. DZ60 library block According to the RTW build process, the creation of device drivers specifically for embedded targets must implement the C MEX S-function and the TLC file. Taking a simplified digital I/O block for example, you can create it by the following steps: Creating a C MEX S-function. This paper assumes that you are implementing a driver as a fully inline S-function. You can build the C MEX S-function by using the RTW S-function template or S-Function Builder, then modify it. Considering the role of your drivers in both simulation and code generation, you can implement pass-through behavior in your simulation drivers. In order to deliver the parameters specified by the user for code generation, you must use sssetsfcnparamnottunable function to specify that the parameters don't change during the simulation, and then use mdlrtw callback methods to write the parameters into a structure named SFcnParamSettings in the modle.rtw file. The following codes write the port name into the SFcnParamSettings structure: #if defined (MATLAB_MEX_FILE) #define MDL_RTW static void mdlrtw(simstruct *S) { real_t port_name = mxgetpr(ssgetsfcnparam(s, 0))[0]; if (!sswritertwparamsettings(s, 1, SSWRITE_VALUE_NUM, "port_name", port_name)) { return; } } #endif Building and masking. After building a MEX-file from your driver source code for use in a Simulink model, you should add a custom icon, dialog box, and initialization commands to the S-Function block by masking it. This provides an easy-to-use graphical user interface for your device driver in the Simulink environment. Writing a TLC file. TLC file enables the user to customize the generated codes and produce platform-specific codes. Make sure the TLC file and the corresponding C MEX S-function has the same name. From the parameters stored in the SFcnParamSettings structure in 534

modle.rtw file, the TLC file could generate the platform-specific codes to set the registers of the micro-controller. Here is simple example: %function Outputs(block, system) Output %assign py0 = LibBlockOutputSignal (0, "", "", 0) %assign port_name=sfcnparamsettings.port_name %if port_name==1 %<py0>=porta; %elseif port_name==2 %<py0>=portb; %else %<py0>=portc; %endif %endfunction Here are two special blocks designed for interrupt service routine (ISR) and target resource configuration. The ISR block has a trigger to a Function-Call subsystem, and the target resource configuration has neither input nor output ports. Figure 6 demonstrates how to use ISR and target resource configuration blocks. The timer ISR is responsible for operations that must be completed within a single clock period. You can specify both of the clock period for code generation and the sample time for simulation. In CGM (Clock Generation Module) block you can choose the bus clock frequency to your micro-controller. For same traditional development tools which can be controlled with makefiles and a make utility, it is simple to adapt existing target files (such as the ert.tlc and ert.tmf files provided by RTW-EC) to your requirements. But for same modern IDEs, such as Freescale CodeWarrior mentioned in this paper, you can implement the following ways: 1) XML Project Import CodeWarrior IDE requires a compatible project file for compilation. Figure 7 illustrates how to use the TLC to generate an Extensible Markup Language (XML) file which can be imported into the CodeWarrior IDE [6]. But if you make changes to the CodeWarrior project after importing the generated XML file, the XML file must be exported and manually edited once again. Figure 7. XML project import process 2) COM Automation COM provides a framework for integrating reusable and can control any other application that includes a COM interface. CodeWarrior IDE supports some automation commands, so COM technology can accomplish certain repetitive tasks automatically. For example, you could use actxserver command to invoke CodeWarrior IDE and create the COM connection for later controlling. Then, call your COM functions from the exit method of your STF_make_rtw_hook.m file. Figure 6. Timer ISR and CGM blocks B. Interfacing to Development Environments Interfacing an embedded target's build process enables RTW-EC to compile the generated code in your development environment and download the executable to target hardware with a debugger or other utility. V. CODE TESTING After completing the development of the DZ60 target, here is an example of how RTW-EC generated embedded C code, compiles the code by interfacing the CodeWarrior IDE and download it to MC9S08DZ60. In this example, MC9S08DZ60 is the micro-controller of a smart car, which is used in the national smart car contest in China. Figure 8 illustrates the steering algorithm model established in Simulink/Stateflow, the goal of the algorithm is to control the steering angle of the smart car. Stateflow enable you easy to establish and simulate complex 535

event-driven systems, which can be modeled as finite-state machines. Figure 8. Statefl w model In order to make a comparison between automatic generation code and code written by band, we execute the generated code and the code written by band based on the same steering algorithm in visual C++ environment. Through different execution cycles, it is easy to get the corresponding results and CPU performance counters. QueryPerformanceCounter( ) function can retrieve the current value of the high-resolution performance counter. Figure 9. Comparison of computing result between code generated and code written by hand o Figure 10. Comparison of CPU performance counter between code generated and code written by hand Through the comparison above, it is clear that the accuracy and efficiency of the automatic generated code is very close to the code written by hand. VI. CONCLUSION The custom embedded target enables the user to generate embedded C codes from Simulink/Stateflow models and download them into the micro-controllers conveniently. Through the tests in visual C++ environment, it is concluded that automatic generation code is accurate and efficient. However, when the generated code is compiled in CodeWarrior and downloaded into micro-controller through BDM, the computing results are correct but the execution time is slower than the code written by hand. The main reason is: the code generated has lots of function callings, additional codes and extern variables, and the expressions are not concision. But these disadvantages could be reduced as the code length increase. Except for the code efficiency, designer also should take readability and the ability to adapt fast development into consideration. That s why the application of auto code generation plays a more and more important role in today s development process of embedded systems. ACKNOWLEDGMENT This paper is supported by Shanghai leading academic discipline project, the project number is B303. REFERENCES [1] Ren Chuanjun, Jiang Zhiwen, Research and Application of Real-Time Simulation on Real-Time Workshop, Computer Simulation, vol. 24, No. 8, August 2007. [2] Yorihisa Yamamoto. NXTway-GS Model-Based Design based on Embedded Coder Robot NXT. http: // www. mathworks. com/ matlabcentral/ fileexchange/ 19147. [3] R. Bartosinski 1, P. Struzka 2, L. Waszniowski 3, "PEERT TM -BLOCKSET FOR PROCESSOR EXPERT TM AND MATLAB @ /SIMULINK @ INTEGRATION". [4] Yang Xiaozhong 1, An Jinwen 1, Cui Weng 2, The Research on Application of Embedded Auto Code Generation, Journal of Projectiles, Rockets, Missiles and Guidance, vol. 28, No. 3, June 2008. [5] Vinod Reddy and Siva Nadarajah, Tips for Fixed-Point Modeling and Code Generation Release 14 with Service Pack 1. The Mathworks, Inc. [6] Matlab R2008b Help Document.The Mathworks, Inc 536