User
...
User type objects are provided to implement special behavior of read and/or write access to an object entry. Several system types are using this mechanism to provide the specified behavior of several pre-defined CANopen object entries.
...
An example user type (called "DemoType") is declared with the following structure.
Code Block | ||
---|---|---|
| ||
CO_OBJ_TYPE DemoType = {
0, /* runtime reference to object directory */
0, /* 32bit value for usage within type */
DemoSize, /* type function to get object size */
DemoCtrl, /* type function to control type object */
DemoRead, /* type function to read object content */
DemoWrite /* type function to write object content */
}; |
Note: if a new user type don't need to have special behavior on accessing (e.g. get size, control, read data or write data), the corresponding type function can be set to 0 to switch this access to basic behavior.
The following list shows the type function prototypes. The return value of the size type function (e.g. DemoSize()
) shall return the size of the user type in bytes. The other type functions shall return CO_ERR.NONE
after successful operation. If an error is detected the corresponding error codes must be returned: CO_ERR_TYPE.RD
, CO_ERR_TYPE.WR
or CO_ERR_TYPE.CTRL
.
Code Block | ||
---|---|---|
| ||
CPU_INT32U DemoSize (CO_OBJ *obj, CPU_INT32U width);
CPU_INT16S DemoRead (CO_OBJ *obj, void *buf, CPU_INT32U len);
CPU_INT16S DemoWrite(CO_OBJ *obj, void *buf, CPU_INT32U len);
CPU_INT16S DemoCtrl (CO_OBJ *obj, CPU_INT16U func, CPU_INT32U para); |
To integrate the user type "DemoType" into the code generation system and keep the object directory definition readable, the following lines should be added within the corresponding header file:
Code Block | ||
---|---|---|
| ||
extern CO_OBJ_TYPE DemoType;
#define USR_TDEMOTYPE ((CO_OBJ_TYPE *)&DemoType) |
The following diagram shows the internal behavior of read access to the user type object:
Panel | ||||
---|---|---|---|---|
| ||||