Author: Nitin S Senior Engineer www.Tataelxsi.co.in UML compliant C code generation from RTW About this document Now a day UML or rather real time UML is the buzzword in the embedded community. We can also see many Japanese companies adopting embedded C++ for their next generation embedded systems. This trend if ought to catch up if you look into the last pages of history. When C came embedded community thought that it won’t do any good in embedded and we will live with assembly. But that was a misconception. Real time UML is become a word commonly coined by those in the embedded field. As you know MATLAB can generate C code from models once the system is modeled in stateflow or Simulink. But what about the nature of C code. It must meet the requirements of designers. Or in other words designers will give the design in UML and C code must comply with that. I spent some amount of time thinking in this direction. Compiled here are my views about that. Some details presented here have been taken from other documents. I am working in MATLAB6.1 as well in 7.0 versions. For any clarifications please feel free to contact me. nitins@Tataelxsi.co.in What is UML? Object oriented approach better for decomposing complex systems More robust and error free coding results (crucial in safety critical systems) Efficient reusability of modules Better approach to system design – easy to understand and flexible design methodology UML is object-oriented concept based modeling language. This is not a programming language at all. The implementation of UML can be done in any programming language but it is easier if you go for programming languages that inherently supports objectoriented concept. With UML it is easy for system designers to communicate with developers. The use of UML leads to better organized and more robust embedded code. The use of UML is a must, as the system becomes fairly complex. In complex systems it is not possible to bring out all the specifications in detail. UML establishes a link between design phase and development phase. If at all any changes need to incorporated in design at a later stage this will also be accounted in UML. It is easy to understand UML based models. So UML can be considered a graphical design language. UML diagrams represent a high level of abstraction. In a usual development cycle of an embedded system architectural and design documents are brought out initially. During coding and testing changes will be brought in system architecture and design that will not be propagated back to design phase. So design and architecture documents thus become invalid. This problem is eliminated in UML based design. UML model can easily be implemented in MATLAB. Here we have got a big advantage that not only we can make the model but also we can test the model by simulation. So the typical development cycle of embedded systems with MATLAB products is simple and hassle free. See the figure-depicting model driven development As you can see the design and development cycles are integrated now. This is a significant advantage as you can see. Basic UML concepts Basic terminologies in UML are class, object and relationship between classes. Each class in UML diagram represents a collection of data and functions working on that data. A class can represent an embedded module. Modules can be organized based on functionality, reusability etc. UML used diagrams to show relationship and behavior of objects. Two types of diagrams exist. 1. Structural diagrams: represents code structure 2. Behavioral diagrams: represents code behavior Structural diagrams involve the following: 1. Class diagrams - shows relationships between classes 2. Collaboration diagrams – shows how objects communicate 3. Component diagrams – shows relationship between components 4. Deployment diagrams - shows relationship between physical entities Behavioral diagrams include: 1. Use cases – represent system functionality 2. Sequence diagrams – shows object communication in a time ordered fashion 3. State diagrams (state charts) – shows event driven behavior 4. Activity diagram - shows flow of control through various system activities UML implementation in MATLAB SIMULINK – is a MDD tool UML diagrams can be implemented using SIMULINK & Stateflow Class diagrams can be implemented in SIMULINK State diagrams can be implemented in Stateflow Advantage of using MATLAB: MATLAB is an integrated platform for design, implementation and testing UML diagrams implemented in MATLAB can be tested by simulation The models will automatically generate C code using Real time Workshop Changes in design are easy to bring about The code generated can be tested in host pc Optimized embedded code can be generated from model with real time workshop embedded coder The code generated will comply with UML model How to implement UML in C code? C code as such does not support UML implementation, as it is not an object-oriented language. How ever it is possible to make C code compliant with UML by following some specific guidelines. This how ever does not mean that all functionality of UML can be implemented in C. this compromise is reached due to the fact that in embedded environments almost all cross compliers support C only. The situation will change later when vendors come up with C++ cross compilers for all processors. C++ is derived from C so all functionality of C++ can be implemented in C with some difficulty. Let us see how to implement UML in C. Each class must constitute of a separate C file and an associated header file All private variables must be defined in the C file with keyword static so as to limit its scope inside the file. All public variables must be defined in C file and extern declaration provided in the header file. All function definitions must be provided in the C file All function declarations should be given in the header file. Nature of Code generated with RTW-Embedded coder The code generated will have the following properties: Code is generated for a particular Simulink mdl file. Separate functions can be generated for each subsystem if needed. MATLAB7.0 can even generate reusable functions. There are three variables in a model that are 1. States 2. Parameters 3. Signals When code is generated all these variables will become variables with appropriate type in C code. By default all MATLAB variables will have the storage class auto. If storage class is auto it means that these variables will be in fact members of an auto generated structure ex: parameters will be members of Structure called Parameters, signals members of BlockIO structure etc. These structures will be declared globally and hence it can be accessed by any program element in global scope. This is contrary to UML requirements. All functions generated by RTW will have default prototype as – o Void function name (void), this implies that function is incapable of returning values. In UML functions act on private data and public program can only indirectly work on private data through associated function. So whatever data required after working on private data should be returned by the function. This is not possible in MATLAB. Input and output to functions must come only through global variables Embedded coder will generate a function modelname_step () which contain all the functionality that will come in one simulation step of the model UML compliant C code from MATLAB 6.1 The following things need to be done: 1. Make the complete model in MATLAB. Provide subsystems for each functions 2. Make each class a separate model 3. Identify global variables and assign storage class as exported global if it model output and imported extern if it is model input parameter. 4. Generate code from each model 5. Integrate the model code. Problems involved: There is considerable number of problems involved that needs to be sorted out. They are listed below. 1. The structures are by global and accessible to all. There is no way to change this feature 2. For each model code generated the structure names are same so this will come as problem when different model code are integrated later 3. Large numbers of files associated with each model Solutions: Only way to solve this is to play with system target files and modify the code structure as such. I was able to do the same with some degree of reliability since it was not meant to be taken as a serious work at this point of time. I have submitted tutorials on TLC’s please refer to that. MATLAB 7.0 The RTW EC associated with latest versions of MATLAB has got many more features. However modifying system target files in this version is very difficult as far as it seems and very time consuming as the structure is very complex. So I will explain how to use existing features effectively for the same. 1. Make the complete model in MATLAB. Provide subsystems for each functions 2. Make each class a separate model 3. Identify global variables and assign storage class as exported global if it model output and imported extern if it is model input parameter. 4. Make custom storage class (see custom storage classes) for making variables static. 5. Identify all variables in the model. This must include parameters and signals. 6. Assign the new storage class to variables that must be made static 7. Generate code from each model 8. Integrate all the codes For any further suggestions or information feel free to contact me