]> rtime.felk.cvut.cz Git - pes-rpp/rpp-simulink.git/blob - doc/rpp_simulink.tex
Merge branch 'maint-rm48' into rm48/master
[pes-rpp/rpp-simulink.git] / doc / rpp_simulink.tex
1 \newcommand{\superscript}[1]{\ensuremath{^{\textrm{\small#1}}}}
2 \newcommand{\subscript}[1]{\ensuremath{_{\textrm{\small#1}}}}
3
4 \documentclass{scrreprt}
5
6 \usepackage{graphicx} % images and graphics
7 \usepackage{paralist} % needed for compact lists
8 \usepackage[normalem]{ulem} % needed by strike
9 \usepackage{listings} % required for code blocks
10 \usepackage[urlcolor=blue,colorlinks=true,hyperfootnotes=false]{hyperref} % links
11 \usepackage[utf8x]{inputenc}  % char encoding
12 \usepackage[bottom]{footmisc} % footnotes
13 \usepackage{todonotes}
14
15 % header
16 \usepackage[top=2.5cm, bottom=2.5cm, left=2.5cm, right=2.5cm]{geometry}
17 \usepackage{float} % To fix images position
18
19 % Prettify code documentation
20 \usepackage{color}
21 \definecolor{gray97}{gray}{.97}
22 \definecolor{gray75}{gray}{.75}
23 \definecolor{gray45}{gray}{.45}
24 \lstset{ frame=Ltb,
25      framerule=0pt,
26      aboveskip=0.5cm,
27      framextopmargin=3pt,
28      framexbottommargin=3pt,
29      framexleftmargin=0.4cm,
30      framesep=0pt,
31      rulesep=.4pt,
32      backgroundcolor=\color{gray97},
33      rulesepcolor=\color{black},
34      %
35      stringstyle=\ttfamily,
36      showstringspaces = false,
37      basicstyle=\small\ttfamily,
38      commentstyle=\color{gray45},
39      keywordstyle=\bfseries,
40      %
41      numbers=left,
42      numbersep=15pt,
43      numberstyle=\tiny,
44      numberfirstline = false,
45      breaklines=true,
46      xleftmargin=20px,
47    }
48
49 % Spacing
50 \linespread{1.15} % Lines spacing
51 \setlength{\plitemsep}{0.5\baselineskip} % List items spacing
52 \definecolor{deepblue}{RGB}{0,0,61}
53 \definecolor{deepgreen}{RGB}{0,80,0}
54 \hypersetup{linkcolor=deepblue,citecolor=deepgreen,}
55
56 % Table of content depth
57 \setcounter{tocdepth}{2}
58
59 % Landscape pages
60 \usepackage{lscape}
61 \usepackage{pdflscape}
62
63 % Change page
64 \usepackage{changepage}
65
66 % Font options
67 % Sans-serif
68 % \renewcommand{\familydefault}{\sfdefault}
69 % Better PDF font
70 \usepackage{lmodern}
71
72 % Multiple columns
73 \usepackage{multicol}
74 \usepackage{caption}
75
76 \newcommand{\repo}{$\langle$repo$\rangle$}
77
78 \begin{document}
79
80 % Title
81 \newcommand{\HRule}{\rule{\linewidth}{0.5mm}}
82
83 \begin{titlepage}
84 \begin{center}
85
86 % Upper part of the page
87 \includegraphics[width=0.40\textwidth]{images/logo_ctu.pdf}\\[1cm]
88 \textsc{\LARGE Czech Technical University in Prague}\\[1.5cm]
89
90
91 % Document title
92 \HRule
93 \vspace{0.4cm}
94 {\huge \bfseries Simulink code generation target for Texas~Instruments
95   RM48 platform\par}
96 \vspace{0.4cm}
97 \HRule \\[1.5cm]
98
99
100 % Author
101 \emph{Authors:}\\
102 Carlos \textsc{Jenkins}\\
103 Michal \textsc{Horn}\\
104 Michal \textsc{Sojka}\\[\baselineskip]
105
106 \vfill
107
108 \emph{Version:}
109 \input{version.tex}
110
111 % Bottom of the page
112 {\large \today}
113
114 \end{center}
115 \end{titlepage}
116 % Title end
117
118 \tableofcontents
119
120 \newpage
121
122 \listoffigures
123
124 \newpage
125
126 %\addtolength{\parskip}{\baselineskip} % Paragraph spacing
127
128 \chapter{Introduction}
129 \label{chap-introduction}
130
131 This text serves as a documentation for the Rapid Prototyping Platform
132 (RPP) project based on Texas Instruments RM48 safety microcontroller.
133 Originally, the RPP project was created for TMS570 microcontroller and
134 the port to RM48 was derived from it under a contract with Eaton
135 Corporation. As this document is still in beta version, some
136 references to the original TMS570-based RPP remain in it.
137
138
139 The document contains step-by-step instructions for installation of
140 development tools, information about Simulink Coder configuration,
141 describes how to create new models as well as how to download the
142 resulting firmware to the hardware. It can also be used as a reference
143 for a testing tool, Matlab Simulink blocks and RPP Matlab Simulink
144 Code generator. The document also provides an overall description of
145 the hardware design and architecture of its control software.
146
147 \section{Background}
148 \label{sec-background}
149
150 The Rapid Prototyping Platform is an ECU (Electronic Control Unit) designed for
151 use in Automotive, thus requirements for Automotive communication interfaces,
152 durable IO ports and analog-digital converters were applied.
153
154 The ECU is controlled by TMS570LS5137ZWT MCU, which is an ARM Cortex R4 based
155 microcontroller developed by Texas Instruments. This MCU contains several
156 protective mechanisms (two cores in lockstep, error correction mechanisms for
157 SRAM and Flash memory, voltage monitoring, etc.) to fulfill the requirements for
158 safety critical applications. See~\cite{rm48xtechnicalreferencemanual2013}
159 for details.
160
161 In order to develop non-trivial applications for the RPP, an operating
162 system is necessary. The RPP is based on FreeRTOS -- an opensource operating
163 system real-time kernel, aimed not only at embedded systems. The FreeRTOS
164 provides an API for creation and managing multiple tasks, scheduler, memory
165 manager, semaphores, queues, mutexes, timers and lots of other features wich can
166 be used in the applications. See~\cite{usingthefreertos2009} for more details.
167
168 Even with the operating system it is quite hard and non-intuitive to
169 control the hardware directly. That is the point when abstraction
170 comes into the play. The RPP software is made of several layers
171 implementing, from the bottom to the top, low-level device drivers,
172 hardware abstraction for common functionality on different hardware
173 and an API which is easy for the use in applications. The operating
174 system and the control software, can be compiled as a library and
175 easily used in any project. More details about the library can be
176 found in Chapter~\ref{chap-c-support-library} and in~\cite{michalhorn2013}.
177
178 Because humans make mistakes and in safety critical applications any mistake can
179 cause damage, looses of money or in the worst case even death of other people,
180 the room for making mistakes has to be as small as possible. An approach called
181 Model-based \cite{modelbasedwiki2013} development has been introduced. In
182 Simulink  the application is developed as a model made of blocks connected
183 together. Every block implements some functionality. For example one block
184 receives a voltage from an analog-digital converter, provides the value as an
185 input to another block which implements some clever algorithm and the product is
186 passed as an input to another block, which sends the value as a CAN message to
187 some other MCU. The final model can be simulated and tested even before the real
188 hardware exists. Finally a C code is generated from the model by a Simulink Code
189 Generator. The code can be compiled by the MCU compatible compiler and
190 downloaded to the MCU Flash memory on the device. Because every block and code
191 generated from the block has to pass a series of tests during their development,
192 the room for making mistakes during the application development has
193 significantly decreased and developers can focus on the functionality instead of
194 the hardware and control software implementation. More information about Code
195 generation can be found in Chapter \ref{chap-simulink-coder-target}. For a
196 reference about Matlab Simulink, Embedded Coder and Simulink Coder,
197 refer to
198 \cite{embeddedcoderreference2013, ebmeddedcoderusersguide2013,
199   simulinkcoderreference2013, targetlanguagecompiler2013,
200   simulinkcoderusersguide2013, simulinkdevelopingsfunctions2013}.
201
202 \section{Software architecture}
203 \label{sec-software-architecture}
204 The basic RPP software, also called the RPP library, is structured
205 into 5 layers, depicted in Figure~\ref{fig-layers}. The architecture
206 design was driven by the following guidelines:
207
208 \begin{compactitem}
209         \item Top-down dependency only. No lower layer depends on anything from
210 upper layers.
211 %       \item 1-1 layer dependency only. The top layer depends
212 % exclusively on the bottom layer, not on any lower level layer (except for a
213 % couple of exceptions).
214         \item Each layer should provide a unified layer interface
215 (\textsc{rpp.h}, \textsc{drv.h}, \textsc {hal.h}, \textsc{sys.h} and
216 \textsc{os.h}), so top layers depends on that layer  interface and not on
217 individual elements from that layer.  \end{compactitem}
218
219 \begin{figure}[H]
220 \begin{center}
221 \noindent
222 \includegraphics[width=250px]{images/layers.pdf}
223 \caption{The RPP library layers.}
224 \label{fig-layers}
225 \end{center}
226 \end{figure}
227
228 As a consequence of this division the source code files and interface files are
229 placed in private directories like \textsc{drv/din.h}. With this organization
230 user applications only needs to include the top layer interface files (for
231 example \textsc{rpp/rpp\_can.h}) to be able to use the selected library API.
232
233 The rest of the section provides basic description of each layer.
234
235 \subsection{Operating System layer}
236 \label{sec-operating-system-layer}
237 This is an operating system interchangeable layer, containing the FreeRTOS
238 source files. The system can be easily replaced by another version thanks to
239 this layer. For example it is possible to compile the library for Linux (using
240 POSIX version of the FreeRTOS), which can be desirable for some testing. The
241 source files can be found in the \textsc{$\langle$rpp\_lib\_root$\rangle$/os}
242 folder.
243
244 The following FreeRTOS versions are distributed:
245 \begin{description}
246         \item[6.0.4\_posix] POSIX version, usable for compilation of the library
247 for Linux system.
248         \item[7.0.2] Prefered version of the FreeRTOS, distributed by
249 Texas Instruments. This version is in use in the current version of the
250 library.
251         \item[7.4.0] Newest version distributed by the Texas Instruments.
252         \item[7.4.2] Newer version available from FreeRTOS pages. Slightly
253 modified to run on RM48 MCU.
254 \end{description}
255
256 \subsection{System Layer} 
257 \label{sec-system-layer}
258 This layer contains system files with data types definitions, clock definitions,
259 interrupts mapping, MCU startup sequence, MCU selftests, and other low level
260 code for controlling some of the MCU peripherals. The source files can be found
261 in \textsc{$\langle$rpp\_lib\_root$\rangle$/rpp/src/sys}, the header files can
262 be found in \textsc{$\langle$rpp\_lib\_root$\rangle$/rpp/include/sys}
263 folder.
264
265 Large part of this layer was generated with the HalCoGen tool. % TODO ref
266
267 \subsection{HAL abstraction layer}
268 \label{sec-hal-abstraction-layer}
269 Hardware Abstraction Layer (HAL) creates an abstraction over the real hardware.
270 For example imagine an IO port with 8 pins. First four pins are connected to the
271 MCU directly to GPIO pins, another four pins are connected to an external
272 integrated circuit, communicating with the MCU via SPI. It would be annoying to
273 have to remember which group is which and to have two sets of control functions
274 for one peripheral. This layer maps every pin of the port to a control function
275 from the System Layer, groups the pins together, names them in a unified style
276 and provides common read, write and configure functions.
277
278 As a result, the higher layers do not have to know anything about the wiring of
279 the peripherals, they can just call read, write or configure function with a pin
280 name as a parameter and the HAL handles all the details.
281
282 The source files can be found in
283 \textsc{$\langle$rpp\_lib\_root$\rangle$/rpp/src/hal} and the header files can
284 be found in \textsc{$\langle$rpp\_lib\_root$\rangle$/rpp/include/hal} folder.
285
286 \subsection{Drivers layer} 
287 \label{sec-drivers-layer}
288 The Drivers layer contains code for controlling the RPP peripherals. The layer
289 benefits from the lower layers thus it is not too low level, but still there are
290 some peripherals like ADC or H-bridge, which needs some special procedure for
291 initialization and running, that would not be very intuitive for the user. For
292 example the H-bridge needs a watchdog reset task to be started before the bridge
293 is enabled.
294
295 The source files can be found in
296 \textsc{$\langle$rpp\_lib\_root$\rangle$/rpp/src/drv} and the header files can
297 be found in \textsc{$\langle$rpp\_lib\_root$\rangle$/rpp/include/drv} folder.
298
299 \subsection{RPP Layer}
300 \label{sec-rpp-layer} 
301 The RPP Layer is the highest layer of the control software. This layer provides
302 an easy to use set of functions for every peripheral and requires only basic
303 knowledge about them. For example, to control the H-bridge,
304 the user can  just call  \textsc{rpp\_hbr\_init()} function to enable the H-bridge
305 and the function calls a sequence of Drivers layer functions to start the
306 Watchdog and configure the peripheral.
307
308 The source files can be found in
309 \textsc{$\langle$rpp\_lib\_root$\rangle$/rpp/src/rpp} and the header files can
310 be found in \textsc{$\langle$rpp\_lib\_root$\rangle$/rpp/include/rpp}.
311
312 \section{Hardware description}
313 \label{sec-hardware-description}
314
315 This section provides reference documentation for the RPP board:
316
317 %%TODO: operating conditions as a reference to the Ti documentation
318
319 \begin{compactitem}
320         \item Layout description.
321         \item Connectors pinout.
322         \item Modules capabilities and features.
323 \end{compactitem}
324
325 The Rapid Prototyping Platform is based on a TMDSRM48HDK development kit developed and distributed by Texas Instruments. The kit is depicted in Figure \ref{fig-board_photo}.
326
327 \begin{figure}[H]\begin{center}
328         \noindent
329         \includegraphics[width=300px]{images/board.png}
330         \caption{The RPP board \cite[p. 8]{rm48hdkusersguide2013}}
331         \label{fig-board_photo}
332 \end{center}\end{figure}
333
334 Only a subset of the peripherals available on the kit is implemented in the control software. A block scheme in Figure \ref{fig-blocks} ilustrates the implemented peripherals and their connection with the MCU, expansion connectors and other components on the development kit.
335
336 \begin{figure}[H]\begin{center}
337         \noindent
338         \includegraphics[width=400px]{images/blocks.png}
339         \caption{The peripherals block scheme.}
340         \label{fig-blocks}
341 \end{center}\end{figure}
342
343 For pinout description of the implemented peripherals refer the RM48HDK User's Guide \cite{rm48hdkusersguide2013}.
344
345 \subsection{Logic IO}
346 \label{sec-logic-io}
347
348 \subsubsection{Digital Inputs and Outputs (DIN and DOUT)}
349 \label{par-digital-inputs-outputs}
350  \begin{compactitem}
351         \item 46 pins available on Expansion connector J11.
352         \item 8 pins available on GIOA
353         \item 8 pins available on GIOB
354         \item 30 pins available on NHET1. Pins NHET1 6 and NHET1 13 are disabled.
355         \item All the pins are configurable as inputs and outputs with different modes:
356          \begin{compactitem}
357                 \item Push/Pull or Open Drain for Output configuration.
358                 \item Pull up, Pull down or tri-stated for Input configuration.
359          \end{compactitem}
360         \item Some of the pins are connected to LEDs or to a button. See Figure \ref{fig-blocks} or refer \cite{rm48hdkusersguide2013} for the connection description.
361  \end{compactitem}
362
363 \subsubsection{Analog Input (ADC)}
364 \label{par-analog-input}
365  \begin{compactitem}
366         \item 16 channels available on the Expansion connector J9.
367         \item Range for 0-5 volts.
368         \item 12 bits resolution.
369         \item Using CPU ADC.
370  \end{compactitem}
371
372 \subsection{Communication}
373 \label{sec-communication}
374 \subsubsection{CAN bus (CAN)}
375 \label{par-can}
376 \begin{compactitem}
377         \item 2 ports available ready for connection to the CAN bus on connector J2 and J3.
378         \item 3 ports available as an interface for external CAN bus drivers on the Expansion connector J11.
379         \item High speed.
380         \item Recover from error.
381         \item Detection of network errors.
382 \end{compactitem}
383
384 \subsubsection{Serial Comm. Interface (SCI)}
385 \label{par-sci}
386 \begin{compactitem}
387         \item 1 port available on connector J7.
388         \item Variable baud rate. Tested on 9600 and 115200.
389         \item RS232 standard compatible.
390 \end{compactitem}
391
392 \section{Document structure}
393 \label{sec-document-structure}
394 The structure of this document is as follows:
395 Chapter~\ref{chap-getting-started} gets you started using the RPP.
396 Chapter~\ref{chap-c-support-library} describes the RPP library.
397 Chapter~\ref{chap-simulink-coder-target} covers the Simulink code
398 generation target and finally Chapter~\ref{chap-rpp-test-software}
399 documents a tool for interactive testing of RPP functionality.
400
401 \chapter{Getting started}
402 \label{chap-getting-started}
403
404 \section{Software requirements}
405 \label{sec-software-requirements}
406 The RPP software stack was developed to be functional on Windows and
407 Linux platforms. The following sections mention the recommended
408 versions of the required software tools/packages.
409
410 \subsection{Linux environment} 
411 \label{sec-linux-environment}
412 \begin{itemize}
413         \item Debian based 64b Linux distribution (Debian 7.0 or Ubuntu 14.4 for
414 example).
415         \item Kernel version 3.11.0-12.
416         \item GCC version 4.8.1
417         \item GtkTerm 0.99.7-rc1
418         \item TI Code Composer Studio 5.5.0.00077
419         \item Matlab 2013b 64b
420         \item Uncrustify 0.59 (optional, see Section \ref{sec-compilation})
421         \item Doxygen 1.8.4 (optional, see Section \ref{sec-compiling-api-documentation})
422         \item Git 1.7.10.4 (optional)
423 \end{itemize}
424
425 \subsection{Windows environment}
426 \label{sec-windows-environment}
427 \begin{itemize}
428         \item Windows 7 Enterprise 64b Service Pack 1.
429         \item Microsoft Windows SDK v7.1
430         \item Bray Terminal v1.9b
431         \item TI Code Composer Studio 5.5.0.00077
432         \item Matlab 2013b 64b
433         \item Doxygen 1.8.4 (optionaly, see Section \ref{sec-compiling-api-documentation}) 
434         \item Uncrustify 0.59 (optionally, see Section \ref{sec-compilation})
435         \item Git 1.9.4.msysgit.2 (optional)
436 \end{itemize}
437
438 \section{Software and tools} 
439 \label{sec-software-and-tools}
440
441 \subsection{TI Code Composer Studio}
442 \label{sec-ti-ccs}
443 Code Composer Studio (CCS) is the official Integrated Development Environment
444 (IDE) for developing applications for Texas Instruments embedded processors. CCS
445 is multiplatform Software based on
446 Eclipse Open Source IDE.
447
448 CCS includes Texas Instruments Code Generation Tools (CGT)
449 \cite{armoptimizingccppcompiler2012, armassemblylanguagetools2012}
450 (compiler, linker, etc). Simulink code generation requires the CGT to be
451 available in the system, and thus, even if no library development
452 will be done or the IDE is not going to be used CCS is still required.
453
454 You can find documentation for CGT compiler in \cite{armoptimizingccppcompiler2012} and
455 for CGT archiver in \cite{armassemblylanguagetools2012}.
456
457 \subsubsection{Installation on Linux} 
458 \label{sec-installation-on-linux}
459 Download CCS for Linux from:
460
461         \begin{quotation}
462 \url{http://processors.wiki.ti.com/index.php/Category:Code\_Composer\_Studio\_v5}
463         \end{quotation}
464
465 Once downloaded, add executable permission to the installation file and launch
466 the installation by executing it. Installation must be done as root in order to
467 install a driver set.
468
469 \lstset{language=bash}
470 \begin{lstlisting}
471 chmod +x ccs_setup_5.5.0.00077.bin
472 sudo ./ccs_setup_5.5.0.00077.bin
473 \end{lstlisting}
474
475 After installation the application can be executed with:
476
477 \lstset{language=bash}
478 \begin{lstlisting}
479 cd <ccs>/ccsv5/eclipse/
480 ./ccstudio
481 \end{lstlisting}
482
483 The first launch on 64bits systems might fail. This can happen because CCS5 is
484 32b application and thus requires 32bits libraries:
485
486 \lstset{language=bash}
487 \begin{lstlisting}
488 sudo apt-get install libgtk2.0-0:i386 libxtst6:i386
489 \end{lstlisting}
490
491 If the application crashes with a segmentation fault edit file:
492
493 \lstset{language=bash}
494 \begin{lstlisting}
495 nano
496 <ccs>/ccsv5/eclipse/plugins/com.ti.ccstudio.branding_<version>/plugin_customization.ini
497 \end{lstlisting}
498
499 And change key \textsc{org.eclipse.ui/showIntro} to false.
500
501 \subsubsection{Installation on Windows}
502 \label{sec-installation-on-windows}
503 The installation for Windows is more straightforward than the previous procedure
504 for Linux.
505
506 Download CCS for Windows from:
507
508         \begin{quotation}
509 \url{http://processors.wiki.ti.com/index.php/Category:Code\_Composer\_Studio\_v5}
510         \end{quotation}
511
512 Once downloaded run the ccs\_setup\_5.5.0.00077.exe and install the CCS.
513
514 \subsubsection{First launch} 
515 \label{sec-first-launch}
516 If another licence is not available, choose ``FREE License -- for use with XDS100
517 JTAG Emulators'' from the licensing options. Code download for the board is using
518 that particular hardware.
519
520 \subsection{Matlab/Simulink}
521 \label{sec-matlab-simulink}
522 Matlab Simulink is a set of tools, runtime environment and development
523 environment for Model-Based \cite{modelbasedwiki2013} applications development,
524 simulations and generation code for target platforms.  Supported Matlab Simulink
525 version is R2013b for 64 bits Linux and Windows.
526
527 \subsection{GtkTerm and Bray Terminal}
528 \label{sec-gtkterm-bray-terminal}
529 Most of the interaction with the board is done through a RS-232 serial
530 connection. The terminal Software used for communication is called GtkTerm for
531 Linux and Bray terminal for Windows.
532
533 To install GtkTerm execute:
534
535 \lstset{language=bash}
536 \begin{lstlisting}
537 sudo apt-get install gtkterm
538 \end{lstlisting}
539
540 The Bray Terminal does not require any installation and the executable file is
541 available at 
542
543 \begin{quotation}
544 \url{https://sites.google.com/site/terminalbpp/}
545 \end{quotation}
546
547 \subsection{C Compiler}
548 \label{sec-c-compiler}
549 In order to be able to compile Matlab Simulink blocks S-functions a C language
550 compiler has to be available on the development system.
551
552 For Linux a GCC 4.8.1 compiler is recommended and can be installed with a
553 command
554
555 \lstset{language=bash}
556 \begin{lstlisting}
557 sudo apt-get install gcc
558 \end{lstlisting}
559
560 For Windows, the C/C++ compiler is a part of Windows SDK, which installation
561 file is available at 
562
563 \begin{quotation}
564 \url{http://www.microsoft.com/en-us/download/details.aspx?id=8279}
565 \end{quotation}
566
567 \section{Project Installation}
568 \label{sec-project-installation}
569 The RPP project is distributed in three packages and a standalone pdf file,
570 containing this documentation. Every package is named like
571 \textsc{package\_name-version.zip}. The three packages are: 
572
573 \begin{description}
574         \item[rpp-lib] The package contains the source codes of the RPP Library,
575 described in Chapter \ref{chap-c-support-library}. If you want to make any
576 changes in the drivers or RPP API, this library has to be compiled and linked
577 with the other two applications in the other two packages. The compile procedure
578 can be found in Section \ref{sec-compilation}.  
579         \item[rpp-simulink] Contains the distributed binary, header files and
580 linker script of the RPP Library static library, source codes of the Matlab
581 Simulink blocks, demo models and scripts for downloading the resulting firmware
582 to the target. Details can be found in Chapter \ref{chap-simulink-coder-target} 
583         \item[rpp-test-sw]  Contains the distributed binary, header files and
584 linker script of the RPP Library static library and source codes of a Command
585 line testing tool. This application is designed for direct cotroling of the
586 target over Serial Interface. More details can be found in Chapter
587 \ref{chap-rpp-test-software}.
588 \end{description}
589
590 \subsection{rpp-lib}
591 \label{sec-rpp-lib-installation}
592 You may not need to deal with this package at all as the compiled library is
593 distributed with the applications. But in case of contributions or further
594 development done by your team, this subsection describes how to open the project
595 in development environment and how to use the resulting static library in an
596 application.
597
598 \begin{enumerate}
599         \item Unzip the \textsc{rpp-lib-version.zip} file.
600         \item Open the Code Composer Studio (see Section \ref{sec-ti-ccs}).
601         \item Follow the procedure for openning the projects in CCS in Section
602 \ref{sec-openning-of-existing-project} and open the rpp-lib project.  \item
603 Compile the static library using a procedure in Section \ref{sec-compilation}.
604 The binary file will appear in the project root directory.
605         \item Copy the binary file to the application, where you want to test
606 and use the new library version.  
607         \begin{itemize}
608                 \item In the rpp-simulink application the library binary file is
609 present at \textsc{rpp/lib} folder. 
610                 \item In the rpp-test-sw application the library binary file is
611 present at \textsc{rpp-lib} folder.  
612         \end{itemize}
613 \end{enumerate}
614
615 \subsection{rpp-simulink}
616 \label{sec-rpp-simulink-installation}
617 If you want to access the demos, build your own models using the RPP blocks, if
618 you want to generate a C code, build a firmware for the RPP board and download
619 it to the real hardware, you have to install this application.
620
621 \begin{enumerate}
622         \item Unzip the \textsc{rpp-simulink-version.zip} file.
623         \item Follow the procedure in Section
624 \ref{sec-configuration-simulink-for-rpp} for configuring the Matlab Simulink for
625 the RPP project.  
626         \item Follow the procedure in Section \ref{sec-crating-new-model} for
627 instruction about creating your own model which will be using the RPP Simulink
628 blocks or follow the instructions in Section \ref{sec-running-model-on-hw}
629 for download the firmware to the real RPP hardware.
630 \end{enumerate}
631
632 \subsection{rpp-test-sw}
633 \label{sec-test-sw-installation}
634 If you want a direct controll over the hardware for example to test your
635 modifications in the RPP Library follow this procedure for the rpp-test-sw
636 application installation.
637
638 \begin{enumerate}
639         \item Unzip the \textsc{rpp-test-sw-version.zip} file.
640         \item Open the Code Composer Studio (see Section \ref{sec-ti-ccs}).
641         \item Follow the procedure for openning the projects in CCS in Section
642 \ref{sec-openning-of-existing-project} and open both, the \textsc{rpp-lib} and
643 the \textsc{rpp-test-sw} project.  
644         \item Right click on the \textsc{rpp-test-sw} project in the
645 \textsc{Project Explorer} and select \textsc{Build Project}. Ignore any errors
646 in the \textsc{rpp-lib} project.  
647         \item Follow the instructions in Section
648 \ref{sec-running-software-on-hw} to download, debug and run the software on the
649 target hardware. CCS will ask you wether to proceed with the detected errors in
650 \textsc{rpp-lib} project. Do not mind them and click to the \textsc{Proceed} button
651 for continue.
652 \end{enumerate}
653
654 \section{Code Composer Studio usage}
655 \label{sec-code-composerpstudio-usage}
656
657 \subsection{Opening of existing project}
658 \label{sec-openning-of-existing-project}
659 The project opening procedure is similar to standard Eclipse project opening.
660
661 \begin{enumerate}
662         \item Launch Code Composer Studio
663         \item Select \textsc{File$\rightarrow$Import}
664         \item In the dialog window select \textsc{Code Composer
665 Studio$\rightarrow$Existing CCS Eclipse project} as an import source as depicted
666 in Figure \ref{fig-import-project}.  
667         \item In the next dialog window click on
668 \textsc{Browse} button and find the root directory of the project.
669         \item Select the requested project in the \textsc{Discovered project}
670 section so that the result looks like in Figure \ref{fig-select-project}.  
671         \item Click on \textsc{Finish} button.
672 \end{enumerate}
673
674 \begin{figure}[H]\begin{center}
675         \includegraphics[width=350px]{images/import_project.png}
676         \caption{Import project dialog}
677         \label{fig-import-project}
678 \end{center}\end{figure}
679
680 \begin{figure}[H]\begin{center}
681         \includegraphics[width=350px]{images/select_project.png}
682         \caption{Select project dialog}
683         \label{fig-select-project}
684 \end{center}\end{figure}
685
686 \subsection{Creating new project}
687 \label{sec-creating-new-project}
688 In \textsc{\repo/rpp/lib/apps/} there are two RPP based
689 applications, \textsc{helloworld} and \textsc{helloworld\_posix}, that are
690 already configured for the RPP Library. It is advised that new applications use
691 this project as a foundation.
692
693 To create a new application copy this directory and rename it. Now open files
694 \textsc{.project}, \textsc{.cproject} and \textsc{.ccsproject} (if available)
695 and change any occurrence of the work \textsc{helloworld}
696 with the name of your project. Use lower case ASCII letters and underscores only.
697
698 \textbf{Steps to configure a new CCS (ARM, using CGT) RPP application:}
699
700 \begin{compactenum}
701         \item Create a new CCS project. \newline{}
702 \noindent\includegraphics[width=400px]{images/base_1.png}
703         \item Create a normal folder \textsc{include}.
704         \item Create a source folder \textsc{src}.
705         \item Add common \textsc{.gitignore} to the root of that project:
706 \lstset{language=}
707 \begin{lstlisting}
708 Debug
709 Release
710 .settings/*
711 \end{lstlisting}
712 \newpage
713         \item Add new variable \textsc{RPP\_LIB\_ROOT} and point to this
714 repository branch root.\newline{}
715 \noindent\includegraphics[width=400px]{images/base_2.png}
716         \item Add \textsc{rpp-lib.lib} static library to linker libraries and
717 add \textsc{RPP\_LIB\_ROOT} to the library search path.\newline{}
718 \noindent\includegraphics[width=400px]{images/base_3.png}
719 \newpage
720         \item Configure linker to retain \textsc{.intvecs} from RPP static
721 library.\newline{} \noindent\includegraphics[width=350px]{images/base_4.png}
722         \item Configure compiler to include local includes, OS includes for
723 RM48 and RPP includes, in that order.\newline{}
724 \noindent\includegraphics[width=350px]{images/base_5.png}
725 \newpage
726         \item Configure compiler to allow GCC extensions.\newline{}
727 \noindent\includegraphics[width=400px]{images/base_6.png}
728         \item Import and link (\underline{do not copy!}) linker file and board
729 upload descriptor.\newline{}
730 \noindent\includegraphics[width=200px]{images/base_7.png}
731 \end{compactenum}
732
733 \textbf{Steps to configure a new GCC (x86(\_64)) RPP simulated application:}
734
735 \begin{compactenum}
736         \item Create a new managed C project that uses Linux GCC toolchain.
737         \item Create a source folder \textsc{src}. Link all files from original
738 CCS application to this folder.
739         \item Create a normal folder \textsc{include}. Create a folder
740 \textsc{rpp} inside of it.  
741         \item Add common \textsc{.gitignore} to the root of that project:
742 \lstset{language=}
743 \begin{lstlisting}
744 Debug
745 Release
746 .settings/*
747 \end{lstlisting}
748 \newpage
749         \item Add new variable \textsc{RPP\_LIB\_ROOT} and point to this
750 repository branch root.\newline{}
751 \noindent\includegraphics[width=400px]{images/base_posix_1.png}
752         \item Configure compiler to include local includes, CCS application
753 includes, OS includes for POSIX and RPP includes, in that order.\newline{}
754 \noindent\includegraphics[width=400px]{images/base_posix_2.png}
755 \newpage
756         \item Add \textsc{rpp} and \textsc{pthread}to linker libraries and add
757 \textsc{RPP\_LIB\_ROOT} to the library search path.\newline{}
758 \noindent\includegraphics[width=400px]{images/base_posix_3.png}
759 \end{compactenum}
760
761 \textbf{In general any RPP application uses the layout/template:}
762
763 \begin{enumerate}
764         \item Include RPP library header file. You may also include only a
765 selected modules to save space.  \lstset{language=c++}
766 \begin{lstlisting}
767 #include "rpp/rpp.h"
768 \end{lstlisting}
769
770 You may not want to include the whole library to save space and compile time. In
771 this case you may include only those modules, that you want to use. Inclusion
772 selected modules only requires two more
773 header files to be included as well: base.h and, in case sci is used for printing, rpp/sci.h.
774 \begin{lstlisting}
775 #include "rpp/hbr.h" /* We want to use H-bridge */
776 #include <base.h>       /* This is a necessary base header file of the rpp library. */
777 #include "rpp/sci.h" /* This is needed, because we are using rpp_sci_printf in following examples. */
778 \end{lstlisting}
779
780 \newpage
781 \item Create one or as many FreeRTOS task function definitions as required. Those tasks should use
782   functions from this library.
783 \lstset{language=c++}
784 \begin{lstlisting}
785 void my_task(void* p)
786 {
787     static const portTickType freq_ticks = 1000 / portTICK_RATE_MS;
788     portTickType last_wake_time = xTaskGetTickCount();
789     while (TRUE) {
790         /* Wait until next step */
791         vTaskDelayUntil(&last_wake_time, freq_ticks);
792         rpp_sci_printf((const char*)"Hello RPP.\r\n");
793     }
794 }
795 \end{lstlisting}
796
797 \item Create the main function that will:
798  \begin{itemize}
799         \item Initialize the RPP board. If you have included only selected
800 modules in step 1, initialize only those modules by calling their init
801 functions, for
802 example rpp\_hbr\_init\(\).
803         \item Spawn the tasks the application requires. Refer to FreeRTOS API
804 for details.
805         \item Start the FreeRTOS Scheduler. Refer to FreeRTOS API for details.
806         \item Catch if idle task could not be created.
807
808 \lstset{language=c++}
809 \begin{lstlisting}
810 void main(void)
811 {
812     /* In case whole library is included: */
813         /* Initialize RPP board */
814         rpp_init();
815     /* In case only selected modules are included: */
816         /* Initialize HBR */
817             rpp_hbr_init();
818         /* Initialize sci for printf */
819         rpp_sci_init();
820         /* Enable interrups */
821         _enable_IRQ();
822
823     /* Spawn tasks */
824     if (xTaskCreate(my_task, (const signed char*)"my_task",
825             512, NULL, 0, NULL) != pdPASS) {
826         #ifdef DEBUG
827         rpp_sci_printf((const char*)
828             "ERROR: Cannot spawn control task.\r\n"
829         );
830         #endif
831         while (TRUE) { asm(" nop"); }
832     }
833
834     /* Start the FreeRTOS Scheduler */
835     vTaskStartScheduler();
836
837     /* Catch scheduler start error */
838     #ifdef DEBUG
839     rpp_sci_printf((const char*)
840             "ERROR: Problem allocating memory for idle task.\r\n"
841         );
842     #endif
843     while (TRUE) { asm(" nop"); }
844 }
845 \end{lstlisting}
846
847  \end{itemize}
848 \item Create hook functions for FreeRTOS:
849  \begin{itemize}
850  \item \textsc{vApplicationMallocFailedHook()} allows to catch memory allocation
851 errors.
852  \item \textsc{vApplicationStackOverflowHook()} allows to catch if a task
853 overflows it's stack.
854
855 \lstset{language=c++}
856 \begin{lstlisting}
857 #if configUSE_MALLOC_FAILED_HOOK == 1
858 /**
859  * FreeRTOS malloc() failed hook.
860  */
861 void vApplicationMallocFailedHook(void) {
862     #ifdef DEBUG
863     rpp_sci_printf((const char*)
864             "ERROR: manual memory allocation failed.\r\n"
865         );
866     #endif
867 }
868 #endif
869
870
871 #if configCHECK_FOR_STACK_OVERFLOW > 0
872 /**
873  * FreeRTOS stack overflow hook.
874  */
875 void vApplicationStackOverflowHook(xTaskHandle xTask,
876                                    signed portCHAR *pcTaskName) {
877     #ifdef DEBUG
878     rpp_sci_printf((const char*)
879             "ERROR: Stack overflow : \"%s\".\r\n", pcTaskName
880         );
881     #endif
882 }
883 #endif
884 \end{lstlisting}
885
886 \newpage
887  \end{itemize}
888 \end{enumerate}
889
890
891 \subsection{Running the software on the HW}
892 \label{sec-running-software-on-hw}
893 \subsubsection{Code Composer Studio Project}
894 \label{sec-ccs-run-project}
895 When the application is distributed as a CCS project, you have to open the
896 project in the CCS as described in the Section
897 \ref{sec-openning-of-existing-project}. Once the project is opened and built, it
898 can be easily downloaded to the target hardware with the following procedure:
899
900 \begin{enumerate}
901         \item Connect the Texas Instruments XDS100v2 USB emulator to the JTAG
902 port.  
903         \item Connect a USB cable to the XDS100v2 USB emulator and the
904 development computer.
905         \item Plug in the power supply.  
906         \item In the Code Composer Studio click on the
907 \textsc{Run$\rightarrow$Debug}. The project will  be optionaly built and
908 the download process will start. The Code Composer Studio will switch into debug
909 mode, when the download is finished.
910         \item Run the program by clicking on the \textsc{Run} button, with the
911 green arrow.  
912 \end{enumerate}
913
914 \subsubsection{Binary File}
915 \label{sec-binary-file}
916 If the application is distributed as a binary file, without source codes and CCS
917 project files, you can download and run just the binary file by creating a new
918 empty CCS project and configuring the debug session according the following
919 procedure:
920
921 \begin{enumerate}
922         \item In Code Composer Studio click on
923 \textsc{File$\rightarrow$New$\rightarrow$CCS Project}.  
924         \item In the dialog window, type in a project name, for example
925 myBinaryLoad, Select \textsc{Device
926 variant} (ARM, Cortex R, RM48L952, Texas Instruments XDS100v2 USB Emulator)
927 and select project template to \textsc{Empty Project}. The filled dialog should
928 look like on the Figure \ref{fig-new-empty-project}
929         \item Click on the \textsc{Finish} button and new empty project will be
930 created.  
931         \item In the \textsc{Project Explorer} click on the project with a right
932 mouse button and in the context menu select \textsc{Debug
933 as$\rightarrow$Debug configurations}.
934         \item Click on a button \textsc{New launch configuration}
935         \item Rename the New\_configuration, for example to myConfiguration.
936         \item Select configuration target file by clicking the \textsc{File
937 System} button, finding and selecting the TMS5703137.ccxml file. The result
938 should look like on the Figure \ref{fig-debug-conf-main-diag}.  
939         \item On the \textsc{program} pane select the binary file you want to
940 download to the board.
941 Click on the \textsc{File System} button, find and select the binary file.
942 Rpp-test-sw.out for example. The result should look like on the Figure
943 \ref{fig-debug-conf-program-diag}.
944         \item You may also tune the target configuration like in the Section
945 \ref{sec-target-configuration}. 
946         \item Finish the configuration by clicking on the \textsc{Apply} button
947 and download the code by clicking on the \textsc{Debug} button. You can later
948 invoke the download also from the \textsc{Run$\rightarrow$Debug} CCS menu. You
949 may not need to create more Debug configurations and CCS empty project as you
950 can easily change the binary file in the Debug configuration to load different
951 binary file.  
952 \end{enumerate}
953
954 \begin{figure}[H]\begin{center}
955         \includegraphics[width=350px]{images/new_empty_project.png}
956         \caption{New empty project dialog}
957         \label{fig-new-empty-project}
958 \end{center}\end{figure}
959
960 \begin{figure}[H]\begin{center}
961         \includegraphics[width=350px]{images/debug_configuration_main.png}
962         \caption{Debug Configuration Main dialog}
963         \label{fig-debug-conf-main-diag}
964 \end{center}\end{figure}
965
966 \subsubsection{Target configuration}
967 \label{sec-target-configuration}
968 Default target configuration erases the whole Flash memory, before downloading
969 the code. This is however not needed in most cases. You may disable this feature
970 by the following procedure: 
971 \begin{enumerate}
972         \item Right click on the project name in the \textsc{Project Browser}
973         \item Select \textsc{Debug as$\rightarrow$Debug Configurations}
974         \item In the dialog window select \textsc{Target} pane.
975         \item In the \textsc{Flash Settings}, \textsc{Erase Options} select
976 \textsc{Necessary sectors only}.
977         \item Save the configuration by clicking on the \textsc{Apply} button
978 and close the dialog.
979 \end{enumerate}
980
981 \begin{figure}[H]\begin{center}
982         \includegraphics[width=350px]{images/debug_configuration_program.png}
983         \caption{Configuration Program dialog}
984         \label{fig-debug-conf-program-diag}
985 \end{center}\end{figure}
986
987 \section{Matlab Simulink usage}
988 \label{sec-matlab-simulink-usage}
989
990 \subsection{Configuring Simulink for RPP}
991 \label{sec-configuration-simulink-for-rpp}
992 \begin{enumerate}
993 \item Download and install CCS for Linux:
994
995 Details on how to setup CCS are available in Section \ref{sec-ti-ccs}.
996
997 \item Install RPP Target:
998
999 Open Matlab and type on command window:
1000
1001 \lstset{language=Matlab}
1002 \begin{lstlisting}
1003 cd <rpp-simulink>/rpp/rpp/
1004 rpp_setup
1005 \end{lstlisting}
1006
1007 This will launch the RPP setup script. This script will ask the user to provide
1008 the path to the CCS compiler root directory (the directory where \textsc{armcl}
1009 binary is located), normally:
1010
1011 \begin{verbatim}
1012 <ccs>/tools/compiler/arm_5.X.X/
1013 \end{verbatim}
1014
1015 This script will, among other things, ask the user to provide the location of
1016 the armcl parent directory, infer and save some relevant CCS paths, add paths to
1017 Matlab path and build S-Function
1018 blocks for user's architecture (using Matlab's mex command line tool).
1019
1020 \item Create a new model or load a demo:
1021
1022 Demos are located on \textsc{\repo/rpp/demo} or you can start a new model and
1023 configure target to RPP.  For new models see \htmladdnormallink{Target
1024 Reference}{\#target\_reference} section below.
1025 \end{enumerate}
1026
1027
1028 \subsection{Working with demo models}
1029 \label{sec-openning-demo-models}
1030 The demo models are available from the directory \textsc{\repo/rpp/demos}. To
1031 access the demo models for reference or for downloading to the RPP board you
1032 have to change the directory to the one, containing the desired demo. For
1033 example to open the cantransmit demo you have to type these commands into the
1034 Matlab command line:
1035
1036 \begin{lstlisting}[language=Matlab]
1037 cd <rpp-simulink>/rpp/demos
1038 open cantransmit.slx
1039 \end{lstlisting}
1040
1041 The same procedure can be used to open any other models.
1042
1043 \subsection{Creating a new model}
1044 \label{sec-crating-new-model}
1045 \begin{enumerate}
1046         \item Open or create a model you want to generate code from.  \item Make
1047 sure that the model is configured (\textsc{Simulation $\rightarrow$ Model
1048 Configuration Parameters}) as described later in this section.
1049         \item From Matlab command window change the current directory to where
1050 you want your generated code to appear, e.g.:
1051 \begin{lstlisting}[language=Matlab]
1052 cd /tmp/my-code
1053 \end{lstlisting}
1054   The code will be generated in a subdirectory of that directory. The name of
1055 the subdirectory will be \textsc{<model>\_rpp}, where \textsc{model} is the name
1056 of the Simulink model.
1057         \item Generate the code by choosing \textsc{Code $\rightarrow$ C/C++
1058 Code  $\rightarrow$ Build Model}.
1059 \end{enumerate}
1060
1061 If you want to run the model on the RPP board, see Section
1062 \ref{sec-running-model-on-hw}.
1063
1064 The new Simulink model needs to be configured in the following way:
1065
1066 \begin{compactitem}
1067 \item Solver (Figure \ref{fig-solver}):
1068  \begin{compactitem}
1069  \item Options: \emph{Fixed-step, discrete}
1070  \item Tasking mode set to \textit{SingleTasking}.
1071    \begin{figure}
1072      \centering
1073      \includegraphics[width=400px]{images/simulink_solver.png}
1074      \caption{Solver settings}
1075          \label{fig-solver}
1076 \end{figure}
1077  \end{compactitem}
1078 \item Diagnostics $\rightarrow$ Sample Time (Figure~\ref{fig-sample-time-settings}):
1079  \begin{compactitem}
1080  \item Disable warning source block specifies -1 sampling time. It's ok for the source blocks to run
1081    once per tick.
1082    \begin{figure}
1083      \centering
1084      \includegraphics[width=400px]{images/simulink_diagnostics.png}
1085      \caption{Sample Time settings}
1086          \label{fig-sample-time-settings}
1087 \end{figure}
1088 \end{compactitem}
1089 \item Code generation (Figure~\ref{fig-code-gen-settings}):
1090  \begin{compactitem}
1091  \item Set to \textsc{rpp.tlc}.
1092    \begin{figure}
1093      \centering
1094      \includegraphics[width=400px]{images/simulink_code.png}
1095      \caption{Code Generation settings}
1096          \label{fig-code-gen-settings}
1097 \end{figure}
1098 \end{compactitem}
1099 \end{compactitem}
1100
1101
1102 \subsection{Running model on the RPP board}
1103 \label{sec-running-model-on-hw}
1104 To run the model on the target RPP hardware you have to enable the download
1105 feature and build the model by following this procedure: \begin{enumerate}
1106         \item Open the model you want to run (See Section
1107 \ref{sec-openning-demo-models}.)
1108         \item Click on \textsc{Simulation$\rightarrow$Model Configuration
1109 Parameters}.
1110         \item In the \textsc{Code Generation$\rightarrow$RPP Options} pane
1111 check \textsc{Download compiled binary to RPP} checkbox.
1112         \item Click on \textsc{OK} button, connect the target HW to the computer
1113 like in the Section \ref{sec-ccs-run-project} and build the model by \textsc{Code $\rightarrow$ C/C++
1114 Code  $\rightarrow$ Build Model}. If the build
1115 ends with a success, the download process will start and once the downloading is
1116 finished, the application will run immediatelly.
1117 \end{enumerate}
1118
1119 The code downloading is supported for Windows and Linux development systems. If
1120 you are using Linux, you may also try to use the OpenOCD by checking Use OpenOCD
1121 to download the compiled binary checkbox in addition. Refer 
1122
1123 \begin{verbatim}
1124 http://rtime.felk.cvut.cz/hw/index.php/TMS570LS3137#OpenOCD_setup_and_Flashing
1125 \end{verbatim}
1126 fore more information about the OpenOCD configuration.
1127
1128   Note: You should end the Code Composer Studio debug session before
1129   downloading the generated code to the RPP board. Otherwise the
1130   download fails.
1131
1132  
1133 \section{Configuring serial interface}
1134 \label{sec-configuration-serial-interface}
1135 The main interface for communicating with the RPP board is the serial interface.
1136 The application may define its own interface settings, but the following
1137 settings is the default one:
1138
1139 \begin{itemize}
1140         \item Baudrate: 115200
1141         \item Parity: none
1142         \item Bits: 8
1143         \item Stopbits: 1
1144         \item Flow control: none
1145 \end{itemize}
1146
1147 Use GtkTerm in Linux or Bray Terminal for accessing the serial interface. See
1148 Section \ref{sec-hardware-description} for reference about the position of the
1149 serial interface connector on the RPP board.
1150
1151 \section{Bug reporting}
1152 \label{sec-bug-reporting}
1153
1154 Please report any problems at CTU's bug tracking system at
1155 \url{https://redmine.felk.cvut.cz/projects/eaton-rm48}. New users have
1156 to register in the system and notify Michal Sojka about their
1157 registration via $\langle{}sojkam1@fel.cvut.cz\rangle{}$ email
1158 address.
1159
1160 \chapter{C Support Library}
1161 \label{chap-c-support-library}
1162
1163 This chapter describes the implementation of the C support library
1164 (RPP Library), which is used both for Simulink code generation target
1165 and command line testing tool.
1166
1167 \section{Description}
1168 \label{sec-description}
1169 The RPP C Support Library (also called RPP library) defines the API for
1170 working with the board. It includes drivers and an operating system.
1171 The library is
1172 designed from the board user perspective and exposes a simplified high-level API
1173 to handle the board's peripheral modules in a safe manner. The library is
1174 compiled as a static library named \texttt{rpp-lib.lib} and can be found in
1175 \textsc{\repo/rpp/lib}.
1176
1177 The RPP library can be used in any project, where the RPP hardware support is
1178 required and it is also used in two modules, described later in this document. The
1179 Command line testing tool, described in Chapter \ref{chap-rpp-test-software} and
1180 Simulink Coder target, described in Chapter \ref{chap-simulink-coder-target}.
1181
1182 For details about the library architecture, refer to Section
1183 \ref{sec-software-architecture}.
1184
1185 \section{API development guidelines}
1186 \label{sec-api-development-guidlines}
1187
1188 The following are the development guidelines used for developing the RPP API:
1189
1190 \begin{compactitem}
1191         \item User documentation should be placed in header files, not in source
1192 code, and should be Doxygen formatted using autobrief. Documentation for each
1193 function present is mandatory.
1194         \item Function declarations in the headers files is for public functions
1195 only. Do not declare local/static/private functions in the header.
1196         \item Documentation in source code files should be non-doxygen formatted
1197 and intended for developers, not users. Documentation here is optional and at
1198 the discretion of the developer.
1199         \item Always use standard data types for IO when possible. Use custom
1200 structs as very last resort.  \item Use prefix based functions names to avoid
1201 clash. The prefix is of the form \textsc{$\langle$layer$\rangle$\_$\langle$module$\rangle$\_}, for example
1202 \textsc{rpp\_din\_update()} for the update function of the DIN module in the RPP
1203 Layer.  
1204         \item Be very careful about symbol export. Because it is used as a
1205 static library the modules should not export any symbol that is not intended to
1206 be used (function) or \textsc{extern}'ed (variable) from application. As a rule
1207 of thumb declare all global variables as static. 
1208         \item Only the RPP Layer symbols are available to user applications. All
1209 information related to lower layers is hidden for the application. This is
1210 accomplished by the inclusion of the rpp.h or rpp\_\{mnemonic\}.h file on the
1211 implementations files only and never on the interface files. Never expose any
1212 other layer to the application or to the whole system below the RPP layer. In
1213 other words, never \textsc{\#include "foo/foo.h"} in any RPP Layer interface
1214 file. 
1215 \end{compactitem}
1216
1217 \section{Coding style}
1218 \label{sec-coding-style}
1219 In order to keep the code as clean as possible, a unified coding style
1220 should be followed by any contributor to the code. The used coding
1221 style is based on the default configuration of Code Composer Studio
1222 editor. Most notable rule is that the Tab character is 4 spaces.
1223
1224 The RPP library project is prepared for a usage of a program named
1225 Uncrustify. The Uncrustify program checks the code and fixes those
1226 lines that does not match the coding style. However, keep in mind that
1227 the program is not perfect and sometimes some lines can be modified
1228 even when the suggested coding style has been followed. This does not
1229 causes problems as long as the contributor follows the committing
1230 procedure described in next paragraph.
1231
1232 When contributing to the code, the contributor should learn the current coding
1233 style from the existing code. When a new feature is implemented, before
1234 committing to the repository, a command: 
1235
1236 \lstset{language=bash}
1237 \begin{lstlisting}
1238 make uncrustify
1239 \end{lstlisting}
1240 in a Linux terminal should be called. This command fixes any coding style
1241 errors. After that all changes can be committed.
1242
1243 \section{Subdirectory content description}
1244 \label{sec-rpp-lib-subdirectory-content-description}
1245 \begin{description}
1246 \item[librpp.a and rpp-lib.lib] static RPP libraries.
1247
1248 The first one is for POSIX simulation, the second one for Simulink models and
1249 other ARM/RM48 applications. This files are placed here by the Makefile, when
1250 the library is built.
1251
1252         \item[apps/] Demo applications related to the RPP library.
1253
1254 This include the CCS studio project for generation of the static library and the
1255 a test suite. The test suit in this directory does not match the test suite
1256 mentioned later in Chapter 5 and those two suits are going to be merged in the
1257 future. Also other demo Hello World like applications are included as a
1258 reference about how to create a RM48 application.
1259         \item[os/] OS layers directory. See OS interchangeable layer for more
1260 information.  
1261         \item[rpp/] Main directory for the RPP Library.
1262         \item[rpp/doc/] Documentation directory for the RPP Library.
1263         \item[rpp/TMS570LS3137.ccxml] Descriptor for code download.
1264
1265 This file is used by all the projects including the Simulink RPP Target for code
1266 download. It is configured to use the Texas Instruments XDS100v2 USB Emulator.
1267         \item[rpp/TMS570LS313xFlashLnk.cmd] CGT Linker command file.
1268
1269 This file is used by all applications linked for the board, including the
1270 Simulink models, static library and test suite. It includes instructions for the
1271 CGT Linker on where to place sections and size of some sections.
1272         \item[rpp/include/\{layer\} and rpp/src/\{layer\}] Interface files and
1273 implementations files for given \textsc{\{layer\}}. See Section
1274 \ref{sec-software-architecture} in Chapter \ref{chap-introduction} for details
1275 on the RPP Layer.  
1276         \item[rpp/include/rpp/rpp.h] Main library header file.
1277
1278 To use this library with all its modules, just include this file and this file
1279 only. Also, before using any library function please call \textsc{rpp\_init()}
1280 function for hardware initialization.
1281
1282         \item[rpp/include/rpp/rpp\_\{mnemonic\}.h] Header file for
1283 \textsc{\{mnemonic\}} module.
1284
1285 This files includes function definitions, pin definitions, etc, specific to
1286 \{mnemonic\} module. See Section \ref{sec-api-development-guidlines}.
1287
1288 You may include only selected rpp\_\{mnemonic\}.h header files and call the
1289 specific rpp\_\{mnemonic\}\_init functions, instead of the rpp.h and rpp\_init
1290 function, if you want to use only a subset of the library functions.
1291
1292         \item[rpp/src/rpp/rpp\_\{mnemonic\}.c] Module implementation.
1293
1294 Implementation of \textsc{rpp\_\{mnemonic\}.h}'s functions on top of the DRV
1295 library. 
1296         \item[rpp/src/rpp/rpp.c] Implementation of library-wide functions.
1297 \end{description}
1298
1299 \section{Compilation}
1300 \label{sec-compilation}
1301
1302 The RPP Library can be compiled as a static library using a Code
1303 Composer Studio project. The compilation process will automatically
1304 update the static library in the library root directory.
1305
1306 For the compilation of the library as a static library open the Code
1307 Composer studio project for the rpp-lib (see
1308 Section~\ref{sec-openning-of-existing-project}) and build the project.
1309 If the build process is successful, the \textsc{rpp-lib.lib} file will
1310 appear in the library root directory.
1311
1312 Because compilation of the libraries in Eclipse IDE can be error
1313 prone, there is a \textsc{Makefile} that allows to compile the
1314 libraries from the Linux or Windows command line:
1315
1316 \begin{lstlisting}[language=bash]
1317 cd <library-root>
1318 make
1319 \end{lstlisting}
1320
1321 On Windows use \texttt{gmake.exe} supplied with CCS instead of
1322 \texttt{make}. The rpp-lib CCS project is configured to invoke this
1323 build procedure from IDE rather than using a building compilation
1324 procedure. One of the results is that the compilation is much faster
1325 this way.
1326
1327 Note that this Makefile still requires the Code Composer Studio (ARM
1328 compiler) to be installed.
1329
1330 The relevant aspects for compiling and linking an application using the static
1331 libraries are: 
1332
1333 \begin{itemize}
1334         \item \textbf{ARM compilation using CCS for the RPP board:}
1335         \begin{compactitem}
1336                 \item Include headers files of the OS for which the library was
1337 compiled against. At the time of this writing the OS is FreeRTOS 7.0.2. See
1338 Section \ref{sec-software-architecture}
1339                 \item Include header files for the RPP library or for modules
1340 you want to use rpp\_can.h for CAN module for example.  
1341                 \item Add library \textsc{rpp-lib.lib} to the linker libraries.
1342 The RPP library \textbf{MUST} be looked for  before Texas Instruments support
1343 library \textsc{rtsv7R4\_T\_be\_v3D16\_eabi.lib}.
1344                 \item Configure linker to retain \textsc{.intvecs} section from
1345 RPP Library:\newline{}
1346 \textsc{--retain="rpp-lib.lib$\langle$sys\_intvecs.obj$\rangle$(.intvecs)"}
1347                 \item Use the provided linker command file
1348 \textsc{TMS570LS313xFlashLnk.cmd}.  
1349         \end{compactitem}
1350         \item \textbf{x86(\_64) compilation using GCC for Simulation:}
1351         \begin{compactitem}
1352                 \item Include headers files of the OS for Simulation. At the
1353 time of this writing the OS is POSIX FreeRTOS 6.0.4.  
1354                 \item Include header files for the RPP library or for modules
1355 you want to use (rpp\_can.h for CAN module for example).  
1356                 \item Add library \textsc{librpp.a} to the linker libraries.
1357                 \item Add \textsc{pthread} to the linker libraries.
1358         \end{compactitem}
1359 \end{itemize}
1360
1361 As an important note, all the models compiled using Simulink will link against
1362 \textsc{rpp-lib.lib}.  When compiling a Simulink model, neither Simulink nor the
1363 \textsc{make} invoked during the build process, will update the generated binary
1364 if the model hasn't changed, and then if the source code hasn't changed. Static
1365 libraries changes are not considered for re-compilation and re-linking. If
1366 library development is being done and static library is updated, in order for
1367 the Simulink model to generate a newly linked version of the binary the whole
1368 code generation folder needs to be deleted in order to force code generation,
1369 compilation and linking with the new static library.
1370
1371 \section{Compiling API documentation}
1372 \label{sec-compiling-api-documentation}
1373 The RPP Layer is formatted using Doxygen documentation generator. This allows to
1374 generate a high quality API reference. To generate the API reference run in a
1375 Linux terminal:
1376
1377 \lstset{language=bash}
1378 \begin{lstlisting}
1379 cd <repo>/rpp/doc/api
1380 make
1381 xdg-open html/index.html
1382 \end{lstlisting}
1383
1384 The files under \textsc{\repo/rpp/lib/rpp/doc/api/content} are used for the API
1385 reference generation are their name is self-explanatory:
1386
1387 \begin{verbatim}
1388 blocks_map.html
1389 blocks.png
1390 cvut.png
1391 footer.html
1392 main_page.dox
1393 \end{verbatim}
1394
1395 \chapter{Simulink Coder Target}
1396 \label{chap-simulink-coder-target}
1397
1398 The Simulink Coder Target allows to convert Simulink models to C code,
1399 compile it and download to the board.
1400
1401 \section{Introduction}
1402 \label{sec-introduction}
1403
1404 The Simulink RPP Target provides support for C source code generation from Simulink models and
1405 compilation of that code on top of the RPP library and the FreeRTOS operating system. This target
1406 uses Texas Instruments ARM compiler (\textsc{armcl}) included in the Code Generation Tools distributed with
1407 Code Composer Studio, and thus it depends on it for proper functioning.
1408
1409 This target also provides support for automatic download of the compiled binary to the RPP
1410 board.
1411
1412 \begin{figure}[H]\begin{center}
1413 \noindent
1414 \includegraphics[width=300px]{images/tlc_process.png}
1415 \caption{TLC code generation process. \cite[p. 1-6]{targetlanguagecompiler2013}}
1416 \end{center}\end{figure}
1417
1418 \section{Features and limitations}
1419 \label{sec-features}
1420
1421 \begin{itemize}
1422 \item Sampling frequencies up to 1\,kHz.
1423 \item Supports only single-tasking and single-rate systems. Support
1424   for single-rate systems will be available in the final version.
1425   Support for multitasking system will require careful audit of the
1426   RPP library with respect to thread-safe code.
1427 \item No External mode support yet. We work on it.
1428 \item Custom compiler options, available via OPTS variable in
1429   \emph{Make command} at \emph{Code Generation} tab (see Figure
1430   \ref{fig-code-gen-settings}). For example \textsc{make\_rtw
1431     OPTS="-O0 -g"}.
1432 \end{itemize}
1433
1434 \section{RPP Options pane}
1435 \label{sec-rpp-target-options}
1436
1437 The RPP Target includes the following configuration options, all of them
1438 configurable per model under  \textsc{Code Generation} \noindent$\rightarrow$
1439 \textsc{RPP Options}:
1440
1441 \begin{itemize}
1442 \item \textbf{C system stack size}: this parameter is passed directly
1443   to the linker for the allocation of the stack. Note that this stack
1444   is used only for initializing the application and FreeRTOS. Once
1445   everything is initialized, another stack is used by the generated
1446   code. See below. Default value is 4096.
1447
1448 \item \textbf{C system heap size}:
1449   \label{sec-rpp-target-options-heap-size} this parameter is passed
1450   directly to the linker for the allocation of the heap. Currently,
1451   the heap is not used, but will be used by the external mode in the future.
1452 Note that FreeRTOS uses its own heap whose size is independent of this
1453 parameter. 
1454 \item \textbf{Model step task stack size}: this parameter will be
1455 passed to the \textsc{xTaskCreate()} that
1456   creates the task for the model to run. In a Simulink model there are always two tasks:
1457  \begin{itemize}
1458  \item The worker task. This task is the one that executes the model
1459    step. This task requires enough stack memory to execute the step.
1460    If your model does not run, it might be caused by too small stack.
1461    The memory needed for the stack depends on the size and structure
1462    of the model.
1463  \item The control task. This task controls when the worker task should execute and controls overruns.
1464
1465  \end{itemize}
1466 \item \textbf{Download compiled binary to RPP}: if set, this option will download the generated binary to
1467   the board after the model is successfully built. Note that this option is unaware of the option
1468   \textit{Generate code only} in the \textit{Code Generation} options panel, so it will try to download even if
1469   only source code has been generated, failing graciously or uploading an old binary laying around
1470   in the build directory. This option calls the \textsc{rpp\_download.m} script, which is in turn a
1471   wrapper on the \textsc{loadti.sh}, \textsc{loadti.bat} and \textsc{loadopenocd.sh} script. More information on the \textsc{loadti.sh}
1472   script can be found in:
1473 \begin{verbatim}
1474 <css>/ccs_base/scripting/examples/loadti/readme.txt
1475 http://processors.wiki.ti.com/index.php/Loadti
1476 \end{verbatim}
1477
1478   The \textsc{loadti.sh} and\textsc{loadti.bat} script will close after the
1479 download of the generated program, leaving the loaded program running.
1480
1481   The \textsc{loadopenocd.sh} script will close after the download of the
1482 generated program as well, but the program will be stopped.  In order to test
1483 the loaded program a manual reset of the board is required.
1484
1485 \item \textbf{Download compiled binary to SDRAM}: This feature is not yet
1486 implemented for the simulink target.
1487
1488 \item \textbf{Use OpenOCD to download the compiled binary}: This option switches
1489 from Ti loading script \textsc{loadti.sh} to OpenOCD script
1490 \textsc{loadopenocd.sh}. The benefit of using OpenOCD, besides that it is open
1491 source
1492   software, is much shorter loading time. More information about the right
1493 OpenOCD version and its installation can be found at:
1494 \begin{verbatim}
1495 http://rtime.felk.cvut.cz/hw/index.php/TMS570LS3137#OpenOCD_setup_and_Flashing
1496 \end{verbatim}
1497 This feature is available for Linux system only.
1498
1499 \item \textbf{Print model metadata to SCI at start}: if set this option will
1500 print a message to the Serial Communication Interface when the model start
1501 execution on the board. This is very helpful to identify the model running on
1502 the board. The message is in the form: 
1503
1504 \begin{verbatim}
1505 `model_name' - generated_date (TLC tlc_version)
1506 \end{verbatim}
1507
1508   For example:
1509 \begin{verbatim}
1510 `hbridge_analog_control' - Wed Jun 19 14:10:44 2013 (TLC 8.3 (Jul 20 2012))
1511 \end{verbatim}
1512 \end{itemize}
1513
1514 \section{Subdirectory  content description}
1515 \label{sec-simulink-subdirectory-content-description}
1516 This section describes the directories of the Simulink Coder. If you are
1517 interested in particular file, refer the description at the beginning of the
1518 file.
1519
1520 \begin{description}
1521         \item[doc/] Contains the sources of the documentation, you are now
1522 reading.  
1523         \item[refs/] Contains third party references, which license allows the
1524 distribution.
1525         \item[rpp/blocks] Contains the TLC files, which defines the blocks for
1526 the Matlab Simulink and \textsc{rpp\_lib.slx}, which is the Simulink RPP
1527 Library, containing all the Simulink blocks for RPP.
1528         \item[rpp/blocks/tlc\_c]Contains the templates for C code generation from the
1529 Matlab Simulink model.
1530         \item[rpp/demos] Contains demo models, which purpose is to serve as a
1531 reference for the usage and for testing.  
1532         \item[rpp/lib] Contains the C Support Library. See Chapter
1533 \ref{chap-c-support-library}.  \item[rpp/loadopenocd] Contains download scripts
1534 for Linux support of the OpenOCD, for code downloading to the target.
1535         \item[rpp/loadti] Contains download scripts for Linux and Windows
1536 support for code downloading to the target, using Texas Instruments CCS code
1537 downloader.  
1538         \item[rpp/rpp] Contains set of support script for the Code Generator.
1539 \end{description}
1540
1541 \section{Block Library Overview}
1542 \label{sec-block-library-overview}
1543 The Simulink Block Library is a set of blocks that allows Simulink models to use
1544 board IO and communication peripherals. The available blocks are summarized in
1545 Table~\ref{tab:block-lib-status} and more detailed description is
1546 given in Section~\ref{sec-blocks-description}.
1547
1548 \begin{table}
1549 \begin{center}\begin{tabular}{|lp{5cm}lll|}
1550 \hline
1551 \textbf{Category} & \textbf{Name} & \textbf{Status} & \textbf{Mnemonic} & \textbf{Header} \\
1552 \hline
1553 \input{block_table.tex}
1554 \hline
1555 \end{tabular}\end{center}
1556
1557   \caption{Block library overview}
1558   \label{tab:block-lib-status}
1559 \end{table}
1560
1561 \label{sec-blocks-implementation}
1562 All of the blocks are implemented as manually created C Mex S-Function . In this section the 
1563 approach taken is briefly explained.
1564
1565 \subsection{C MEX S-Functions}
1566 \label{sec-c-mex-functions}
1567  \begin{compactitem}
1568  \item C : Implemented in C language. Other options are Fortran and Matlab language itself.
1569  \item MEX: Matlab Executable. They are compiled by Matlab - C compiler wrapper called MEX.
1570  \item S-Function: System Function, as opposed to standard functions, or user functions.
1571  \end{compactitem}
1572
1573 A C-MEX S-Function is a structured C file that implements some mandatory and
1574 optional  callbacks for a specification of a number of inputs, outputs, data
1575 types, parameters, rate, validity checking, etc.  A complete list of callbacks
1576 can be found in:
1577         \begin{quotation}
1578 \htmladdnormallink{http://www.mathworks.com/help/simulink/create-cc-s-functions.html}{http://www.mathworks.com/help/simulink/create-cc-s-functions.html}
1579 \end{quotation}
1580
1581 The way a C-MEX S-Function participates in a Simulink simulation is shown on the
1582 diagram \ref{fig-sfunctions-process}:
1583
1584 \begin{figure}[H]\begin{center}
1585 \noindent
1586 \includegraphics[width=250px]{images/sfunctions_process.png}
1587 \caption{Simulation cycle of a S-Function. \cite[p. 57]{simulinkdevelopingsfunctions2013}}
1588 \label{fig-sfunctions-process}
1589 \end{center}\end{figure}
1590
1591 In general, the S-Function can perform calculations, inputs and outputs for simulation. Because 
1592 the RPP blocks are for hardware peripherals control and IO the blocks are 
1593 implemented as pure sink or pure source, the S-Function is just a descriptor of
1594 the block and does not perform any calculation and does not provide any input or
1595 output for simulations. 
1596
1597 The implementation of the S-Functions in the RPP project has following layout:
1598
1599 \begin{itemize}
1600   \item Define S-Function name \textsc{S\_FUNCTION\_NAME}.
1601   \item Include header file \textsc{header.c}, which in connection with
1602 \textsc{trailer.c} creates a miniframework for writing S-Functions.  
1603   \item In \textsc{mdlInitializeSizes} define:
1604   \begin{itemize}
1605         \item Number of \textit{dialog} parameter.
1606         \item Number of input ports.
1607         \begin{compactitem}
1608                 \item Data type of each input port.
1609         \end{compactitem}
1610         \item Number of output ports.
1611         \begin{compactitem}
1612                 \item Data type of each output port.
1613         \end{compactitem}
1614         \item Standard options for driver blocks.
1615   \end{itemize}
1616   \item In \textsc{mdlCheckParameters}:
1617   \begin{itemize}
1618         \item Check data type of each parameter.
1619         \item Check range, if applicable, of each parameter.
1620   \end{itemize}
1621   \item In \textsc{mdlSetWorkWidths}:
1622   \begin{compactitem}
1623         \item Map \textit{dialog} parameter to \textit{runtime} parameters.
1624         \begin{itemize}
1625                 \item Data type of each \textit{runtime} parameter.
1626         \end{itemize}
1627   \end{compactitem}
1628   \item Define symbols for unused functions.
1629   \item Include trailer file \textsc{trailer.c}.
1630 \end{itemize}
1631
1632 The C-MEX S-Function implemented can be compiled with the following command:
1633
1634 \lstset{language=bash}
1635 \begin{lstlisting}
1636 <matlabroot>/bin/mex sfunction_{mnemonic}.c
1637 \end{lstlisting}
1638
1639 As noted the standard is to always prefix S-Function with \textsc{sfunction\_}
1640 and use lower case mnemonic of the block.
1641
1642 Also a script called \textsc{compile\_blocks.m} is included. The script that
1643 allows all \textsc{sfunctions\_*.c} to be fed to the \textsc{mex} compiler so
1644 all S-Functions are compiled at once. To use this script, in Matlab do:
1645
1646 \lstset{language=Matlab}
1647 \begin{lstlisting}
1648 cd <repo>/rpp/blocks/
1649 compile_blocks()
1650 \end{lstlisting}
1651
1652 \subsection{Target Language Compiler files}
1653 \label{sec-target-language-compiler-files}
1654
1655 In order to generate code for each one of the S-Functions, every S-Function implements a TLC file
1656 for \textit{inlining} the S-Function on the generated code. The TLC files describe how to 
1657 generate code for a specific C-MEX S-Function block. They are programmed using TLC own language and 
1658 include C code within TLC instructions, just like LaTeX files include normal text in between LaTeX 
1659 macros.
1660
1661 The standard for a TLC file is to be located under the \textsc{tlc\_c} subfolder from where the 
1662 S-Function is located and to use the very exact file name as the S-Function but with the \textsc{.tlc}
1663 extension: \textsc{sfunction\_foo.c} \noindent$\rightarrow$ \textsc{tlc\_c/sfunction\_foo.tlc}
1664
1665 The TLC files implemented for this project use 3 hook functions in particular (other are available, 
1666 see TLC reference documentation):
1667 \begin{itemize}
1668 \item \textsc{BlockTypeSetup}: \newline{}
1669   BlockTypeSetup executes once per block type before code generation begins.
1670   This function can be used to include elements required by this block type, like includes or
1671   definitions.
1672 \item \textsc{Start}: \newline{}
1673   Code here will be placed in the \textsc{void
1674 $\langle$modelname$\rangle$\_initialize(void)}. Code placed here will execute
1675 only once.
1676 \item \textsc{Outputs}: \newline{}
1677   Code here will be placed in the \textsc{void
1678 $\langle$modelname$\rangle$\_step(void)} function. Should be used to get the
1679 inputs o a block and/or to set the outputs of that block.
1680 \end{itemize}
1681
1682 The general layout of the TLC files implemented for this project are:
1683 \begin{itemize}
1684 \item In \textsc{BlockTypeSetup}: \newline{}
1685   Call common function \textsc{\%$<$RppCommonBlockTypeSetup(block, system)$>$} that will include the 
1686   \textsc{rpp/rpp\i\_mnemonic.h} header file (can be called multiple times but header is included only once).
1687 \item \textsc{Start}: \newline{}
1688   Call setup routines from RPP Layer for the specific block type, like HBR enable, DIN pin setup, 
1689   DAC value initialization, SCI baud rate setup, among others.
1690 \item \textsc{Outputs}: \newline{}
1691   Call common IO routines from RPP Layer, like DIN read, DAC set, etc. Success of this functions
1692   is checked and in case of failure error is reported to the block using ErrFlag.
1693 \end{itemize}
1694
1695 C code generated from a Simulink model is placed on a file called
1696 \textsc{$\langle$modelname$\rangle$.c} along with other support files in a
1697 folder called \textsc{$\langle$modelname$\rangle$\_$\langle$target$\rangle$/}.
1698 For example, the source code generated for model \textsc{foobar} will be placed
1699 in current Matlab directory \textsc{foobar\_rpp/foobar.c}.
1700
1701 The file \textsc{$\langle$modelname$\rangle$.c} has 3 main functions:
1702 \begin{compactitem}
1703 \item \textsc{void $\langle$modelname$\rangle$\_step(void)}: \newline{}
1704   This function recalculates all the outputs of the blocks and should be called once per step. This
1705   is the main working function.
1706 \item \textsc{void $\langle$modelname$\rangle$\_initialize(void)}: \newline{}
1707   This function is called only once before the first step is issued. Default values for blocks IOs
1708   should be placed here.
1709 \item \textsc{void $\langle$modelname$\rangle$\_terminate(void)}: \newline{}
1710   This function is called when terminating the model. This should be used to free memory of revert 
1711   other operations made on the initialization function. With current implementation this function
1712   should never be called unless an error is detected and in most models it is empty.
1713 \end{compactitem}
1714
1715 \section{Block reference}
1716 \label{sec-blocks-description}
1717
1718 This section describes each one of the Simulink blocks present in the Simulink
1719 RPP block library, shown in Figure \ref{fig-block-library}.
1720
1721 \begin{figure}[h]
1722   \begin{center}
1723     \includegraphics[width=\textwidth]{images/block_library.png}
1724   \end{center}
1725 \caption{Simulink RPP Block Library.}
1726 \label{fig-block-library}
1727 \end{figure}
1728 \clearpage
1729 \input{block_desc.tex}
1730
1731 \section{Demos}
1732 The Simulink RPP Demo Library is a set of Simulink models that use blocks from
1733 the Simulink RPP Block Library and generates code using the Simulink RPP Target.
1734
1735 This demos library is used as a test suite for the Simulink RPP Block Library
1736 but they are also intended to show basic programs built using it. Because of
1737 this, the demos try to use more than one
1738 type of block and more than one block per block type.
1739
1740 In the reference below you can find a complete description for each of the demos.
1741
1742 \subsection{ADC demo}
1743 \begin{figure}[H]\begin{center}
1744 \noindent
1745 \includegraphics[width=450px]{images/demo_adc.png}
1746 \caption{Example of the usage of the Analog Input blocks for RPP.}
1747 \end{center}\end{figure}
1748
1749 \textbf{Description:}
1750
1751 Demostrates how to use Analog Input blocks in order to measure voltage. This demo
1752 measures voltage on every available Analog Input and prints the values on the
1753 Serial Interface.
1754
1755 \subsection{Simple CAN demo}
1756 \begin{figure}[H]\begin{center}
1757 \noindent
1758 \includegraphics[width=450px]{images/demo_simple_can.png}
1759 \caption{The simplest CAN demonstration.}
1760 \end{center}\end{figure}
1761
1762 \textbf{Description:}
1763
1764 The simplest possible usage of the CAN bus. This demo is above all designed for
1765 testing the CAN configuration and transmission.
1766
1767 \subsection{CAN transmit}
1768 \begin{figure}[H]\begin{center}
1769 \noindent
1770 \includegraphics[width=450px]{images/demo_cantransmit.png}
1771 \caption{Example of the usage of the CAN blocks for RPP.}
1772 \end{center}\end{figure}
1773
1774 \textbf{Description:}
1775
1776 Demostrates how to use CAN Transmit blocks in order to:
1777
1778 \begin{compactenum}
1779 \item Send unpacked data with data type uint8, uint16 and uint32.
1780 \item Send single and multiple signals packed into CAN\_MESSAGE by CAN Pack block.
1781 \item Send a message as extended frame type to be received by CAN Receive
1782 configured to receive both, standard and extended frame types.
1783 \end{compactenum}
1784
1785 Demostrates how to use CAN Receive blocks in order to:
1786
1787 \begin{compactenum}
1788 \item Receive unpacked data of data types uint8, uint16 and uint32.
1789 \item Receive and unpack received CAN\_MESSAGE by CAN Unpack block.
1790 \item Configure CAN Receive block to receive Standard, Extended and both frame types.
1791 \item Use function-call mechanism to process received messages
1792 \end{compactenum}
1793
1794 \subsection{Simulink Demo model}
1795 \begin{figure}[H]\begin{center}
1796 \noindent
1797 \includegraphics[width=450px]{images/demo_board.png}
1798 \caption{Model of the complex demonstration of the boards peripherals.}
1799 \end{center}\end{figure}
1800
1801 \textbf{Description:}
1802
1803 This model demonstrates the usage of RPP Simulink blocks in a complex and interactive
1804 application. The Ti HDK kit has eight LEDs placed around the MCU. The application
1805 rotates the light around the MCU in one direction. Every time the user presses the button
1806 on the HDK, the direction is switched.
1807
1808 The state of the LEDs is sent on the CAN bus as a message with ID 0x1. The button can
1809 be emulated by CAN messages with ID 0x0. The message 0x00000000 simulates button release
1810 and the message 0xFFFFFFFF simulates the button press.
1811
1812 Information about the state of the application are printed on the Serial Interface. 
1813
1814 \subsection{Echo char}
1815 \begin{figure}[H]\begin{center}
1816 \noindent
1817 \includegraphics[width=450px]{images/demo_echo_char.png}
1818 \caption{Echo Character Simulink demo for RPP.}
1819 \end{center}\end{figure}
1820
1821 \textbf{Description:}
1822
1823 This demo will echo (print back) any character received through the Serial Communication
1824 Interface (115200-8-N-1).
1825
1826 Note that the send subsystem is implemented a as \textit{triggered} subsystem and will execute only
1827 if data is received, that is, Serial Receive output is non-negative. Negative values are errors.
1828
1829 \subsection{GIO demo}
1830 \begin{figure}[H]\begin{center}
1831 \noindent
1832 \includegraphics[width=450px]{images/demo_gio.png}
1833 \caption{Demonstration of DIN and DOUT blocks}
1834 \end{center}\end{figure}
1835
1836 \textbf{Description:}
1837
1838 The model demonstrates how to use the DIN blocks and DOUT blocks, configured in every mode. The DOUTs
1839 are pusched high and low with period 1 second. The DINs are reading inputs and printing the values
1840 on the Serial Interface with the same period.
1841
1842 \subsection{Hello world}
1843 \begin{figure}[H]\begin{center}
1844 \noindent
1845 \includegraphics[width=450px]{images/demo_hello_world.png}
1846 \caption{Hello World Simulink demo for RPP.}
1847 \end{center}\end{figure}
1848
1849 \textbf{Description:}
1850
1851 This demo will print \textsc{"Hello Simulink"} to the Serial Communication Interface (115200-8-N-1) one
1852 character per second. The output speed is driven by the Simulink model step which is set to one
1853 second.
1854
1855 \chapter{Command line testing tool}
1856 \label{chap-rpp-test-software}
1857 The \textsc{rpp-test-suite} is a RPP application developed testing and direct
1858 control of the RPP hardware. The test suite implements a command processor,
1859 which is listening for a commands and prints some output related to the commands
1860 on the serial interface. The command processor is modular and each peripheral
1861 has its commands in a separated module.
1862
1863 The command processor is implemented in <rpp-test-sw>/cmdproc and commands
1864 modules are implemented in <rpp-test-sw>/commands directory.
1865
1866 The application enables a command processor using the SCI at
1867 \textbf{115200-8-N-1}. When the software starts, the received welcome message
1868 and prompt should look like:
1869
1870 \begin{verbatim}
1871 TODO: FILL
1872 \end{verbatim}
1873
1874 Type in command help for a complete list of available command, or help command
1875 for a description of concrete command.
1876
1877 \section{Command description}
1878
1879 This section contains the description of the available commands. The
1880 same description is also available in the program itself via the
1881 \texttt{help} command.
1882
1883 \input{rpp-test-sw-cmds.tex}
1884
1885 \chapter{Glossary}
1886
1887 \begin{description}
1888 \item[ADC]
1889   \textit{Analog to Digital Converter.} \newline{}
1890   Hardware circuitry that converts a continuous physical quantity (usually voltage) to a
1891   digital number that represents the quantity's amplitude.
1892
1893 \item[AIN]
1894   \textit{Analog Input.} \newline{}
1895   Mnemonic to refer to or something related to the analog input (ADC) hardware module.
1896
1897 \item[AOUT]
1898   \textit{Analog Output.} \newline{}
1899   Mnemonic to refer to or something related to the analog output (DAC) hardware module.
1900
1901 \item[CAN]
1902   \textit{Controller Area Network.} \newline{}
1903   The CAN Bus is a vehicle bus standard designed to allow microcontrollers and devices to
1904   communicate with each other within a vehicle without a host computer.
1905   In this project it is also used as mnemonic to refer to or something related to the CAN
1906   hardware module.
1907
1908 \item[CGT]
1909   \textit{Code Generation Tools.} \newline{}
1910   Name given to the tool set produced by Texas Instruments used to compile, link, optimize,
1911   assemble, archive, among others. In this project is normally used as synonym for
1912   ``Texas Instruments ARM compiler and linker."
1913
1914 \item[DAC]
1915   \textit{Digital to Analog Converter.} \newline{}
1916   Hardware circuitry that converts a digital (usually binary) code to an analog signal
1917   (current, voltage, or electric charge).
1918
1919 \item[DIN]
1920   \textit{Digital Input.} \newline{}
1921   Mnemonic to refer to or something related to the digital input hardware module.
1922
1923 \item[ECU]
1924   \textit{Engine Control Unit.} \newline{}
1925   A type of electronic control unit that controls a series of actuators on an internal combustion
1926   engine to ensure the optimum running.
1927
1928 \item[ETH]
1929   \textit{Ethernet.} \newline{}
1930   Mnemonic to refer to or something related to the Ethernet hardware module.
1931
1932 \item[FR]
1933   \textit{FlexRay.} \newline{}
1934   FlexRay is an automotive network communications protocol developed to govern on-board automotive
1935   computing.
1936   In this project it is also used as mnemonic to refer to or something related to the FlexRay
1937   hardware module.
1938
1939 \item[GPIO]
1940   \textit{General Purpose Input/Output.} \newline{}
1941   Generic pin on a chip whose behavior (including whether it is an input or output pin) can be
1942   controlled (programmed) by the user at run time.
1943
1944 \item[HBR]
1945   \textit{H-Bridge.} \newline{}
1946   Mnemonic to refer to or something related to the H-Bridge hardware module. A H-Bridge is
1947   an electronic circuit that enables a voltage to be applied across a load in either direction.
1948
1949 \item[HOUT]
1950   \textit{High-Power Output.} \newline{}
1951   Mnemonic to refer to or something related to the 10A, PWM, with current sensing, high-power
1952   output hardware module.
1953
1954 \item[IDE]
1955   \textit{Integrated Development Environment.} \newline{}
1956   An IDE is a Software application that provides comprehensive facilities to computer programmers
1957   for software development.
1958
1959 \item[LCT]
1960   \textit{Legacy Code Tool.} \newline{}
1961   Matlab tool that allows to generate source code for S-Functions given the descriptor of a C 
1962   function call.
1963
1964 \item[LIN]
1965   \textit{Local Interconnect Network.} \newline{}
1966   The LIN is a serial network protocol used for communication between components in vehicles.
1967   In this project it is also used as mnemonic to refer to or something related to the LIN
1968   hardware module.
1969
1970 \item[LOUT]
1971   \textit{Logic Output.} \newline{}
1972   Mnemonic to refer to or something related to the digital output hardware module.
1973   It is logic output (100mA), as opposed to power outputs (2A, 10A).
1974
1975 \item[MBD]
1976   \textit{Model-Based Design.} \newline{}
1977   Model-Based Design (MBD) is a mathematical and visual method of addressing problems associated
1978   with designing complex control, signal processing and communication systems. \cite{modelbasedwiki2013}
1979
1980 \item[MEX]
1981   \textit{Matlab Executable.} \newline{}
1982   Type of binary executable that can be called within Matlab. In this document the common term
1983   used is `C MEX S-Function", which means Matlab executable written in C that implements a system
1984   function.
1985
1986 \item[MOUT]
1987   \textit{(Motor) Power Output.} \newline{}
1988   Mnemonic to refer to or something related to the 2A push/pull power output hardware module.
1989
1990 \item[PWM]
1991   \textit{Pulse-width modulation.} \newline{}
1992   Technique for getting analog results with digital means. Digital control is used to create a
1993   square wave, a signal switched between on and off. This on-off pattern can simulate voltages
1994   in between full on and off by changing the portion of the time the signal spends on versus
1995   the time that the signal spends off. The duration of ``on time" is called the pulse width or
1996   \textit{duty cycle}.
1997
1998 \item[RPP]
1999   \textit{Rapid Prototyping Platform.} \newline{}
2000   Name of the automotive hardware board. Also generic term to define something related
2001   to the board, like the RPP Library, RPP Layer, RPP API, etc.
2002
2003 \item[SCI]
2004   \textit{Serial Communication Interface.} \newline{}
2005   Serial Interface for communication through hardware's UART using communication standard RS-232.
2006   In this project it is also used as mnemonic to refer to or something related to the Serial
2007   Communication Interface hardware module.
2008
2009 \item[SDC]
2010   \textit{SD-Card.} \newline{}
2011   Mnemonic to refer to or something related to the SD-Card hardware module.
2012
2013 \item[SDR]
2014   \textit{SD-RAM.} \newline{}
2015   Mnemonic to refer to or something related to the SD-RAM hardware module for logging.
2016
2017 \item[TLC]
2018   \textit{Target Language Compiler.} \newline{}
2019   Technology and language used to generate code in Matlab/Simulink.
2020
2021 \item[UART]
2022   \textit{Universal Asynchronous Receiver/Transmitter.} \newline{}
2023   Hardware circuitry that translates data between parallel and serial forms.
2024 \end{description}
2025
2026 \bibliography{rpp_simulink}
2027 \bibliographystyle{plain}
2028
2029 \end{document}
2030
2031 %  LocalWords:  FreeRTOS RPP POSIX