® ® Tom Erkkinen Embedded Applications Manager The MathWorks, Inc. © 2008 The MathWorks, Inc. Production Code Generation (PCG) Time Machine ® ® NACA “Production Coders” - 1949 Wayback Machine (www.archive.org) First Production Coders 2 ® ® Wayback Machine MathWorks – Oct 12 1999 (Release 11) New 500 staff First MathWorks Production Coder Y2K 3 ® ® Simulink Code Code Generation Generation -- 1999 Simulink 2005 “Now supports two forms of code generation: rapid prototyping and production” 4 ® ® Agenda PCG Historical Review 1990s (Circa Release 11) 2000s (Circa Release 14) Today (R2008a) New Features and Trends Executable Specification Detailed Design Code Generation and Integration Standards and Certification 5 ® ® Code Generation – 1990s Real-Time Workshop® and Stateflow® Coder Simulink and Stateflow Generate code from Simulink and Stateflow that Algorithm and System Design Tune Tune Generate Generate is easy to interact, tune, and experiment with Simulation Acceleration Rapid Prototyping Hardware in Loop (HIL) Embedded deployment (some examples) C code Real-Time System You can deploy code on any microprocessor using Real-Time Workshop because it generates ANSI-C. 6 ® ® ECU Development – 1990s Automatic Code Generation usage Paper Specs Field Tests & CAL Modeling Simulation Acceleration HIL Test Rapid Prototyping Manual Integration Coding 7 ® ® Code Generation (R11) Simulink Code Generation (R14) 8 ® ® Agenda PCG Historical Review 1990s (Circa Release 11) 2000s (Circa Release 14) Today (R2008a) New features and trends Executable Specification Detailed Design Code Generation and Integration Standards and Certification 9 ® ® ECU Development – 2000s Using Model-Based Design with Automatic Code Generation System Integration System Specification Simulation Acceleration HIL Test Component Integration Component Design Rapid Prototyping Processor-in-loop (PIL) Test Software Integration Software Design On Target Rapid Prototyping Software-in-loop (SIL) Test Coding Coding • Source • Source • Object Production Code Generation 10 ® ® Code Generation – Early 2000s Real-Time Workshop® Embedded Coder Supports all Real-Time Workshop options Plus generates production code Component integration Golden model Verify Tune Verify Tune Code readability Algorithm and System Design Generate Generate Floating- and fixed-point efficiency Simulink and Stateflow C code MCU DSP 11 ® ® MAC 2005 - GMPT More Efficient Than Hand 12 ® ® MAC 2005 - Visteon 13 ® ® MAC 2005 – Golden Model 14 ® ® Demo Executable specification Multidomain Rapid Simulation Interaction options Detailed Design Data Dictionary Code generation RP/HIL PCG AUTOSAR Verification and Validation Bidirectional trace Model Advisor 15 ® ® Code Generation – Early 2000s Simulink and Stateflow Embedded Targets Provide target specific blocks/features for On-Target Rapid Prototyping C code MCU Tune Verify Tune Verify Algorithm and System Design Generate Generate Real-Time Workshop® Embedded Coder Generates efficient code that can be customized to look like hand code for Production Code Generation DSP 16 ® ® On-Target Rapid Prototyping Simulink Plant Model Code Generation Controller Model Harness Embedded Target (MPC5xx) 17 ® ® Rapid Prototyping Comparison Traditional Bypass On-Target Purpose New ideas, green field research Refine designs, production focused Production Constraints (efficiency) Few More Hardware (Cost) Fl ex ity iebl From off-the-shelf PCs id ili ty F To custom bypass HW From Existing ECUs To eval board HW 18 ® ® Code Generation – Early to Mid 2000s Verify Tune Verify Tune Links and Targets Provide target specific blocks/features for On-Target Rapid Prototyping SIL and PIL support Algorithm and System Design Generate Generate Real-Time Workshop® Embedded Coder Generates efficient code that can be customized to look like hand code for Production Code Generation Simulink and Stateflow C code MCU DSP 19 ® ® MAC 2004 – DC Trucks 20 ® ® SIL Simulink Code Generation Controller Model Host-compiled C, Plant Model Options 1. Emulate target word sizes (w/code change) 2. Use target word sizes (w/o code change) With S-Function Wrapper (DLL) 21 ® ® PIL Simulink Code Generation Controller Model Plant Model PIL Options 1. Using ISS 2. To HW (via IDE) 3. Direct to HW Cross-Compiled C (IDE, ISS) 22 ® ® In-the-Loop Comparison SIL PIL (ISS) PIL (HW) HIL Description Software-in-the-Loop Processor-in-the-Loop Hardware-in-the-Loop Purpose Verify Source Code Component Verify Object Code Component Verify Object Code Component Verify Complete System Functionality Fidelity Emulated: same source code, not bit accurate Actual: Different source code, bit accurate (fix-pt) Same object code; bit accurate (fix-pt); not cycle accurate since uses ISS Same object code; bit accurate (fix-pt); cycle accurate since runs on HW Same executable code; bit accurate (fix-pt); cycle accurate; emulated I/O Platform (Plant, ECU) Host, Host Convenience Desktop convenient; executes just in Simulink; no HW cost Real-Time Engineers Co nv ty Host, Target eenli i d i en F cExecutes Desktop convenient; e on desk or test Host, Host (ISS) Real-Time System, Target Executes in test bench or lab; $$ for processor, ECU, I/O, cables executes just on host computer w/Simulink and ISS; no HW cost bench; $ for processor board and cables Non real-time Non real-time (between samples) Non real-time (between samples) Hard real-time Systems or Software Engineers Software or Test Engineers Software or Test Engineers Systems or Test Engineers 23 ® ® Link and Target Products – R2007a Simulink® Links PIL via IDEs Altium TASKING® Analog Devices VisualDSP++® TASKING TI’s Code Composer Studio™ Mentor Graphics ModelSim® Cadence® Incisive® Project creation Optimizations Many third party offerings ST10 Targets Add-ons to Links Device driver blocks Optimizations Target 1 Tricore Target 2 Link 24 ® ® Embedded IDE Link™ MU - R2007b+ (for Green Hills® MULTI®) Simulink Automates build and test of embedded code using MATLAB®, Simulink® and MULTI® (from Green Hills®) For systems and software engineers who want to deploy and verify automatically generated code quickly Supports key processors including: Freescale™ MPC5554 and MPC7447 NEC V850 Analog Devices® Blackfin® MULTI Processor 25 ® ® Code Generation – Mid 2000s Real-Time Workshop Embedded Coder C and C++ Production Code Generation Algorithm and System Design Generate Generate Generate Generate Simulink® HDL Coder Verilog and VHDL Code Generation Simulink, Stateflow, and Embedded MATLAB Functions C/C++ code MCU DSP Verilog/VHDL FPGA ASIC You can deploy code on any microprocessor or hardware device. 26 ® ® Code Generation Verification –– Mid Mid 2000s 2000s Real-Time Workshop Embedded Coder C and C++ Production Code Generation MCU DSP Verify Verify C/C++ code Generate Generate Verify Verify Links and Targets On-Target Prototyping SIL and PIL Algorithm and System Design Generate Generate Simulink® HDL Coder Verilog and VHDL Code Generation Simulink, Stateflow, and Embedded MATLAB Functions Verilog/VHDL FPGA ASIC deploycode codeon onany anymicroprocessor microprocessoror orhardware hardwaredevice. device. You can verify 27 ® ® Demo PIL Testing 28 ® ® Simulation is not the only way to do Verification and Validation 29 ® ® Introduced in 2007… Simulink, Stateflow, and Embedded MATLAB Functions Simulink Design Verifier Generate tests and prove model properties using formal methods Algorithm and System Design MCU DSP Verify Verify C/C++ code Generate Generate Verify Verify Generate Generate GeHan ne dra te Verilog/VHDL FPGA ASIC 30 ® ® ECU Development – 2005 Today (R2008a) Using Model-Based Design with Automatic Code Generation System Integration System Specification Simulation Acceleration HIL Test Component Integration Component Design Rapid Prototyping Processor-in-loop (PIL) Test Software Integration Software Design On Target Rapid Prototyping Software-in-loop (SIL) Test Coding • Source • Object Production Code Generation 31 ® ® Agenda PCG Historical Review 1990s (Circa Release 11) 2000s (Circa Release 14) Today (R2007a to 2008a) New features and trends Executable Specification Detailed Design Code Generation and Integration Standards and Certification 32 ® ® Embedded MATLAB Functions -–2007a R14 Number of Supported Functions 300 Fi Non-Fi 250 200 150 100 50 0 R14 R14sp1 R14sp2 R14sp3 R2006a R2006b R2007a >>eml_frames, eml_nd 33 ® ® Embedded MATLAB™ Improvements – R2007b Generate C using emlc in MATLAB® Requires Real-Time Workshop® m-files on path supported for Embedded MATLAB in Simulink® and Stateflow® 34 ® ® Agenda PCG Historical Review 1990s (Circa Release 11) 2000s (Circa Release 14) Today (R2008a) New features Executable Specification Detailed Design Code Generation and Integration Standards and Certification 35 ® ® 2007a Fixed Point Tool – 2006b Includes: Data type override Automated scaling Over/under flow detection Fixed vs. float plots >>fxpdemo_feedback 36 ® ® Fixed-Point Tool - R2008a Design Min/Max Problem Fixed-Point Tool scaling based on design min and max data ranges was not supported Solution Fixed-Point Tool now leverages design minimum and maximum from blocks and Stateflow data Benefit Better fits user workflow Provides autoscaling that does not require simulation 37 ® ® Fixed-Point Advisor – R2008a Float- to fixed-point conversion Problem Obtain an initial float- to fixed- point conversion with least effort Solution Fixed-Point Advisor helps Set model parameters Set block parameters Perform fixed-point conversion Validate conversion using floating point results Prepare for code generation Complement Fixed-Point Tools, which optimizes fixed-point scaling >>fxpdemo_fpa 38 ® ® Demo Fixed Point Advisor 39 ® ® Agenda PCG Historical Review 1990s (Circa Release 11) 2000s (Circa Release 14) Today (R2008a) New features Executable Specification Detailed Design Code Generation and Integration Standards and Certification 40 ® Code Efficiency -–R14SP2 R13 R2007a ® real_T rtb_Switch[10] Wide Signals (for-loops) for real_T rtb_Switch[10] for real_T tmp for for for 41 ® ® Improved Bus Code – R2008a 1 <start> Out1 Dead code <end> 1 R2007b in in <size> 2 <signal1> Gain2 8 8 U U(E) 2 void s_step(void) { <p_data> <p_data> Out4 bus_2 rtb_BusCreator; Selector int32_T rtb_size; int32_T rtb_signal1; Unnecessary copies uint8_T rtb_p_data[8]; rtb_size = s_U.in.size; (void) memcpy(&(rtb_p_data[0]),&s_U.in.p_data, 8*sizeof(uint8_T)); void s_step(void) { s_Y.Out1 = s_U.in.start - s_U.in.end; s_Y.Out1 = s_U.in.start - s_U.in.end; rtb_signal1 = rtb_size << 1U; s_Y.Out4 = s_U.in.p_data[2]; rtb_BusCreator.size = rtb_signal1; (void) memcpy(&(rtb_BusCreator.p_data),rtb_p_data, 8*sizeof(uint8_T)); } (void) memcpy(&(rtb_p_data[0]),&rtb_BusCreator.p_data, 8*sizeof(uint8_T)); R2008a s_Y.Out4 = rtb_p_data[2]; } Improving bus code efficiency was a top request for this release 42 ® ® Coder Infrastructure Circa 1999 IR – Intermediate Representation TLC – Template Language Compiler Simulink IR TLC Simulink C, makefiles Stateflow IR Stateflow Deployment Support Only TLC-based Interfaces (S-Functions, System Targets (e.g. GRT, ERT)) MATLAB 43 ® ® Coder Infrastructure 2008 - Unified for all domains, highly efficient, extensible Multiple IR analyses and optimizations S-functions Simulink Stateflow Embedded MATLAB Simscape Legacy Code Tool Simulink Design Verifier Cross product optimizations TLC Analysis/ Transform Analysis/ Transform C, C++ VHDL, Verilog Makfiles, Projects Unified IR IR-based Interfaces (e.g., BuildInfo, Target Function Library, …) TLC-based Interfaces (e.g., System Targets, Code Templates, …) 44 ® ® Cross Product Optimizations – R2008a Optimizations are now enabled across code segments generated for Simulink and Stateflow R2007b real_T rtb_Gain[10]; … for (i = 0; i < 10; i++) { rtb_Gain[i] = 2.0 * in[i]; } if (cu_DWork.is_active_c1_cu == 0) { … } else { for (sf_i1 = 0; sf_i1 < 10; sf_i1++) { out[sf_i1] = rtb_Gain[sf_i1]; } } R2008a if (cu_DWork.is_active_c1_cu == 0) { … } else { for (i = 0; i < 10; i++) { out[i] = 2.0 * in[i]; } } 45 ® ® Target Specific Optimizations – R2007b Example Results Processor Algorithm Function Tricore® Fixed-pt By Infineon® Math (int32, int32, saturated) C6416™ DSP By TI FIR 128-sample (int32, 50-taps, saturated) Target Specific Generated Code* int32_T tricore_add_s32_s32_s32_sat(int32_T a, int32_T b) { return (__sat int)a + b; } int32_T TI_C6000_s32_add_s32_s32_sat(int32_T a, int32_T b) { return _sadd(a, b); } Speedup (over ANSI-C) 17.1X 6.2X *Implemented using Target Function Libraries 46 ® ® Agenda PCG Historical Review 1990s (Circa Release 11) 2000s (Circa Release 14) Today (R2008a) New features Executable Specification Detailed Design Code Generation and Integration Standards and Certification 47 ® ® Code Architecture - Integration Controller Model Communication Interfaces Comm Drivers Generated Algorithm Code Output Drivers M Actuators Sensors Input Drivers A B Special Device Drivers C ® BLACK BOX ® V RGBSplit-4 RCS INPUT BLUE GREEN RED Special Interfaces POWER Legacy Code Target Code Tuning Scheduler/Operating System and Support Utilities ® 48 ® ® Subsystem Integration w/Export Functions – R2006a Supports a popular scheduling technique in production Streamlines code generated No scheduler, no model step function >> rtwdemo_export_functions 49 ® ® Model Reference – Introduced in Release 14 Only rebuilt if interface dependent models rebuilt is changed (R2008a) model changed and rebuilt Incremental code generation is supported via Model Reference When a model is changed, only the dependent model regenerate their code Normal mode model reference in R2007b 50 ® ® Model Integration w/Prototype Control – R2008a Problem Previously, non-reentrant code only supported void/void function interfaces Solution Provide function control for top model Pass inports/outports as arguments Pass arguments by value or pointer Control argument names and order No wrapper Model Reference (R2008a) Benefit Simplifies code integration and testing Reduces global RAM usage extern int32_T application(int32_T In); Generated step function definition 51 ® ® Pack-and-Go – Introduced in R2006b Packages generated code and all dependencies (via zip) Includes Referenced Models – R2008a Code, Pack (Computer A) Go Unpack, Build (Computer B) 52 ® ® Legacy Code Tool – Introduced in R2006b Integrates external code for simulation and code generation (e.g., legacy lookup tables) Registration File Earlier version of LCT on MATLAB Central Simulation Model Generated Code >> sldemo_lct_lut 53 ® ® Legacy Code Tool – R2007b Enhanced for non-computational data integration (device driver, file descriptor,…) S-Function IO S-Function Parameter S-Function DWork Y Y Y Y N/A Y Y Y Y Y Y Y Y Y Y N/A Y N/A Y Y Y Scalar, 1D array, and 2D array Y Y Y ND array Y Y Y Void* & Void ** N N R2007b Built-in data types Simulink.Bus 1 (scalar only) Simulink.AliasType 1 Simulink.NumericType Fixed-point data types 2 3 Fi object Complex number 4 54 ® ® Agenda PCG Historical Review 1990s (Circa Release 11) 2000s (Circa Release 14) Today (R2008a) New features Executable Specification Detailed Design Code Generation and Integration Certification and Standards 55 ® ® Standards and Certification AUTOSAR (previously discussed) IEC 61508 Safety Guidelines (discussed tomorrow) MAAB Model Guidelines (v2) MISRA C® Code Guidelines (2004) 56 ® ® Model Standards Checks in Simulink Verification and Validation™ MAAB v2 checks in R2007b IEC 61508 checks in R2008a 57 ® ® MISRA-C Compliance History of Generated Code • Our MISRA-C test suite consists of several example models • Results shown for most frequently violated rules Improving MISRA-C compliance with each release, such as: Compliant code generated from enabled subsystems (R2007b) Eliminate Stateflow® goto statements (R2007a) Compliant parentheses option available (R2006b) Generate default case for switch-case statements (R2006b) MathWorks MISRA-C Compliance Package available upon request www.mathworks.com/support/solutions/data/1-1IFP0W.html 58 ® ® Stateflow and Embedded MATLAB Traceability – R2008a Problem No automated way to trace Stateflow chart and Embedded MATLAB code to generated code Solution Generate comments that automatically link Stateflow objects and Embedded MATLAB functions to the generated code Allow model-to-code and code-tomodel bidirectional navigation Benefit Bidirectional traceability helps code reviews, code verification, and software certification 59 ® ® Agenda PCG Historical Review 1990s (Circa Release 11) 2000s (Circa Release 14) Today (R2008a) New Features Executable Specification Detailed Design Code Generation and Integration Certification and Standards The Future? 60 ® ® Way Forward Machine John Deere Production Code Generation - SAE CV 07 www.mathworks.com/mason/tag/proxy.html?dataid=9939&fileid=44540 61 ® ® Way Forward Machine 20 MLOC in 2013 Christopher Davey, Ford Motor Co, et al. 62 ® ® More Info – Product web page Supported Hardware - All links and targets - Incl. Third Parties Demos and Webinars - Short web clips (2-5 minutes) Technical Literature - Conference papers PCG Eval Kit for Eclipse (Europa) - Step by step tuturial (3-4 hours) - Earlier version in >>rtwdemos www.mathworks.com/rtwembedded 63 ® ® More Info – MATLAB Central Demos - Lego NXT Robots - Task Profiling (5554) - Dual Core (Blackfin) Tools and Blocksets - Code Coverage Tool - Model Assistant Tool - Target Blocksets www.mathworks.com/matlabcentral/fileexchange (Production Code category) 64 ® ® More Info – Support Examples Categories - Design Patterns - Integration with Targets - Scheduling and Multirate - Standards and Guidelines New for 2008a Stateflow Pattern Wizard Fixed Point Tips for R2008a Model Patterns for C constructs Best Practices for Date Stores www.mathworks.com/support/product/examples.html?product=SL&category=all (Production Code category) 65 ® ® We are very interested in learning about your production code needs. Thank you! [email protected] 66 ® ® One More Thing … Are you working on or planning for an IEC 61508 project? 67 ® ® Validated by TÜV 68
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
advertisement