Download Developing Embedded Control System Platform using

Transcript
11.3. S-FUNCTIONS FOR I/O-CARD
91
this function should be added are marked in the original file with a comment saying that
“interrupts should be enabled here”.
227
228
229
230
231
232
233
234
235
236
/∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗
∗ AVR32 Timing i n t e r r u p t ∗
∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗/
StartStopWatch (&timerStopWatch ) ;
usecWork += StopStopWatch(&workStopWatch ) ;
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 ) ;
usecTimer += StopStopWatch(&timerStopWatch ) ;
StartStopWatch (&timerStopWatch ) ;
StartStopWatch (&workStopWatch ) ;
count++;
The WaitPeriodicTask function is a function that wait until the next period before returning. 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
638
639
640
/∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗
∗ I n i t i a l i z e AVR32 Timing ∗
∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗/
InitPeriodicTasks () ;
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
714
715
/∗ For d e b u g g i n g ∗/
p r i n t f ( ”Avrage p e r i o d e : %i \n ” , usecTimer / count ) ;
p r i n t f ( ”Avrage work : %i \n ” , usecWork / count ) ;
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