this function should be added are marked in the original file with a comment saying that “interrupts should be enabled here”.
227 /∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ 228 ∗ AVR32 Timing i n t e r r u p t ∗ 229 ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗/ 230 S t a r t S t o p Wa t c h (& timerStopWatch ) ; 231 usecWork += StopStopWatch(&workStopWatch ) ; 232 W a i t P e r i o d i c T a s k (& m a t l a b S i m u l a t i o n P e r i o d e ) ; 233 u s e c T i m e r += StopStopWatch(& timerStopWatch ) ; 234 S t a r t S t o p Wa t c h (& timerStopWatch ) ; 235 S t a r t S t o p Wa t c h (&workStopWatch ) ; 236 c o u n t ++;
The WaitPeriodicTask function is a function that wait until the next period before re- turning. This function is defined in the periodicTask library. Two different stopwatches (two different stopwatch structures) are used to measure the time used in each period, and how much of the period the processor are working (the time it doesn’t wait for next period).
The following code are in the start of the main function, and it initialize and start the timer. rtmGetStepSize(S) will return the time step in seconds, and this is used to set the period time. Since that function needs the variable S to work, these lines have to be added last in the initialization part of the main function. This means just above the big comment block which is named “Execute the model”.
636 /∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ 637 ∗ I n i t i a l i z e AVR32 Timing ∗ 638 ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗/ 639 I n i t P e r i o d i c T a s k s ( ) ;
640 S t a r t P e r i o d i c T a s k (& m a t l a b S i m u l a t i o n P e r i o d e , ( i n t ) ( r t m G e t S t e p S i z e ( S ) ∗ 1 0 0 0 ) ) ;
The last bit of code is used to print out the results of the time measurements. This code has to be added in the main function after the simulation is finished. It’s not dependent on any RTW code, so it can be added just above the return statement.
713 /∗ For d e b u g g i n g ∗/
714 p r i n t f ( ”Avrage p e r i o d e : %i \n ” , u s e c T i m e r / c o u n t ) ; 715 p r i n t f ( ”Avrage work : %i \n ” , usecWork / c o u n t ) ;
11.3
S-functions for I/O-card
11.2 described the changes needed to compile the generated code for AVR32, and make the code execute the time steps periodically. This means that systems made in Simulink can run on AVR32, but it doesn’t make it able to use the I/O-card. S-functions were made to control the I/O-card. They are, as explained in 11.1.2, Simulink blocks that are coded in a programming language.
There are different types of S-functions, but here only fully inlined S-functions were used. First of all, it’s important that the program are as effective as possible, since the AVR32 aren’t very fast and STK1000 have a very limited amount of RAM. That meant that noninlined functions wouldn’t be a good choice. Fully inlined was preferred before wrapper, since these S-functions should behave different when running simulations in Simulink and
when running a RTW generated program on STK1000. It wouldn’t make sense to try and contact the I/O-card via SPI from a workstation computer.
The following S-functions has been created:
◦ Analog input, reads one or more analog input channel of the I/O-card and gives values between 0 and 5.
◦ Threaded analog input, does the same as the normal analog input, except that it uses the threads for I/O operations.
◦ Analog output, takes value between 0 and 5 and writes it to the used subchannels. Can also select resolution between 8-bit and 12-bit.
◦ Threaded analog output, does the same as the normal analog output, except that it uses the threads for I/O operations.
◦ Digital input, reads all digital input channels of the I/O-card and gives either low (0) or high (5) values.
◦ Digital output, writes values to digital outputs of the I/O-card. Limit values can be selected for each channel, and are used to decide if the digital output should be high or low.
11.3.1 Simulink S-function File
The Simulink S-function file is the file that defines how the S-function works in Simulink. sfuntmpl_basic.c is a template file for simple S-functions, and it will be used here. Since the workstation computer that runs Simulink, can’t use the I/O-card, the S-functions will have Simulink S-function files that doesn’t do anything during simulation. This means that the only function from sfuntmpl_basic.c that will be modified is the mdlInitial- izeSizes(), that defines different sizes of the S-function.
The sizes that are changed from the template is the number of input, outputs and param- eters used by the S-function. These numbers are different for the different S-functions, as described in table 11.1. Notice that the input S-functions has outputs and visa verca, since a input S-function reads from the I/O-card and send the signals out of the block.
Table 11.1: Sizes of the different S-functions S-function Inputs Outputs Parameters
Analog input 0 8 0
Threaded analog input 0 8 8 (sampling time) Analog output 6 0 2 (resolution) Threaded analog output 6 0 2 (resolution) Digital input 0 8 0
Digital output 1 0 8 (limit value)
Below is the mdlInitializeSizes() function of the analog input S-function. This is an example of what these functions look like. The whole files are on the CD-ROM
45 s t a t i c void m d l I n i t i a l i z e S i z e s ( S i m S t r u c t ∗S ) 46 {