Skip to main content

CAPL

CAPL – CAN Access Programming Language

Designed specifically to suit the CAN Applications
The CAPL language allows you to write programs for individual applications (ECUs). 
The system environment can be emulated with the help of CAPL in CANoe, e.g. the data traffic of all remaining stations can be emulated.

CAPL Program Structure:

Event Driven Procedural language with Special Purpose Functions
Events:
  • Time Events (Software Timers)
  • I/O Events (Key Board, Parallel port, Serial Port )
  • CAN Communication Events (Messages Errors)
CAPL Browser Provides the Complete Development environment for CAPL Programs.
A CAPL program consists of two parts:
  • Declaration of global variables
  • Declaration of user-defined functions and event procedures
Declaration and Initialization of Global Variables:
  • Global variables are declared in the global variables pane at the upper right. The data types DWORD, LONG, WORD, INT, BYTE and CHAR can be used analogously to their use in the C programming language.
  • Variables of the type timer or msTimer are used to create time events.
  • All the System’s Network Related Database (Developed using CANDB++) is included for the Effective development and consistency.
  • Instances of the CAN DB Messages are declared in the Global variables Section.
  • CAN messages to be transmitted on the bus from the CAPL program are declared with the key word message.

 CAPL Functions:

  • CAPL functions are used among other things to modularize code for repeated use, and to form interfaces. The syntax is C-like, but there are a number of supplemental features not included in C:
  • A missing result type is interpreted as void
  • An empty parameter list is permitted as in C++
  • Overloading of functions (i.e. multiple functions with the same name but different parameter lists) is possible as in C++
  • A parameter check is performed as in C++
  • Arrays of arbitrary dimension and size may be passed
  • CAPL provides you with a library of intrinsic functions.

 Expressions in CAPL:

CAPL syntax is based on the programming language C. The following expressions are permitted as in C:
  • Instruction blocks: { … }
  • if { … } and if {…} else { … }
  • switch, case, default
  • for.., while.., do. while loops
  • continue and break
  • return
  • ?: Expression
  • In CAPL, the same arithmetic, logical and bitwise operators are provided to you ( +,*, +=, *=, ||, ++, etc.) as in C.
  • You can perform a type conversion explicitly with the Cast instruction (as in C).

 Arrays in CAPL:

  • Declaration of arrays (arrays, vectors, matrices) is permitted in CAPL, including for variables of the type message.
  • They are used and initialized in a manner analogous to C language. A particular feature is that arrays of the type CHAR can be initialized by assigning a string.
  • Local variables are always created statically in CAPL (in contrast to C). This means that an initialization is only executed at the program start, and when variables enter the procedure they assume the value they had when they last left the procedure.

 Event procedures:

  • Event procedures serve as inputs into CAPL. They make it possible to react to external events (such as the occurrence of certain messages).
  • You can send messages by calling the output() function.
  • These language tools and symbolic access to the various variables defined in the database make it possible to create simple prototypical models of the nodes.
  • The event procedures can be edited in a user-friendly Browser.
  • CAPL is a procedural language in which the execution of program blocks is controlled by events. These program blocks are referred to as Event procedures.
  • The program code that you define in event procedures is executed when the event occurs.
Even                                                         Event Procedure
Program start                                                     on start{}
Receipt of a CAN message                               on message{}
Press of a key                                                       on key{}
Elapse of a timer                                                 on timer{}

 Examples of event procedures:

  • When one of the following events occurs (Message with ID 100 is received, key a is pressed, or Timer Uhr_1 elapses) then, in this example
  • an on message procedure,
  • an on key procedure or
  • an on Timer procedure is called.

 on message Procedures:

  • The event procedure type on message is available to have CAPL nodes react to the receipt of CAN messages.
  • To access the control information you would use selectors. The key word this is available within an on message procedure, to access the data of the message that has just been received.

Message Selectors:

You can access control information of the CAN message objects using the following selectors:
  • ID      –>Message identifier
  • CAN  –>Chip number
  • DLC   –>Data Length Code
  • DIR    –>Direction of transmission, event classification; possible values: RX, TX, TXREQUEST.
  • RTR   –>Remote Transmission Request; possible values: 0 (no RTR), 1 (RTR)

Key Word this:

  • Within an event procedure for receiving a CAN object or an environment variable, the data structure of the object is designated by the key word this.
  • For example, you could access the first data byte of message 100 which was just received by means of the following:
on message 100
{
byte byte_0;
byte_0 = this.byte(0);
}

 on key Procedures:

  • The code for a key press can be entered either as a character, a number or a predefined identifier for a function keys. The event procedure on key * reacts to all key presses.
  • In on key procedures the key word this serves to determine the active key code.
  • With on timer procedures you can define time-delayed or cyclic program sequences.
  • To do this you would start a timer which you have already defined with the function setTimer().
  • After the timer elapses the associated on timer procedure is called.
Example:
The message 100 transmitted on the bus one second after pressing the key a
msTimer my Timer;
message 100 msg;
on key ‘a’ {
   setTimer(myTimer,100);
}
on timer my Timer {
   output(msg);
}

 on timer Procedures:

  • You can define time events in CAPL. When this event occurs, i.e. when a certain period of time elapses, the associated on timer procedure is called.
  • You can program cyclic program sequences by resetting the same time event within the on timer procedure.
  • You would start a previously-defined timer with the function setTimer().
In CAPL has the following variable types for timer:
timer – timer based on seconds
msTimer – timer based on milliseconds
  • With the function cancelTimer() you can stop a timer which has already been started and thereby prevent the associated on timer procedure from being called.
  • A variable of the type timer can only be accessed by the predefined functions setTimer() and cancelTimer().
  • A timer is created with timer. The timer does not begin to run until it has been started in a on timer event procedure. After the timer has elapsed the associated event procedure is called.

Examples of on timer Procedure:

In the following example, message 100 is always sent on the bus 20 milliseconds after the a key has been pressed:
msTimer myTimer;
message 100 msg;
on key ‘a’ {
   setTimer(myTimer,20);
}
on timer myTimer {
   output(msg);
}

 Symbolic Signal Access:

  • When using a project database individual signals can be accessed by entering their names. The compiler assumes the task of determining the bit offset and bit length as well as the task of converting the machine format (Intel <-> Motorola). The values are interpreted in raw format.
  • If a physical interpretation is to be used, the operator phys must be appended. The compiler then scales the value according to the conversion formula (factor, offset) indicated in the database. You can assign symbolic descriptions to the signal values using value tables in the CAN database.

Declaration of Messages:

Messages to be output from the CAPL program are declared with the key word message. The complete declaration includes the message identifier or message name when working with symbolic databases. For example, to output on the bus the messages with identifiers A (hex) and 100 (dec) and the message EngineData defined in the database you would write:
message 0xA m1;
message 100 m2;
message EngineData m3;
output(m1);
output(m2);
output(m3);

Comments