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