Now that CAN Signals and Messages have been defined and configured, it is time to encapsulate initialize them in the application. Please refer to the previous section if CAN Signals and Messages haven't been defined and configured.
Application Startup
Before using CAN Signals and Messages, both Signals and Messages should be initialized and created during Startup. The following code snippets are taken from can_demo.c
found in $:\Micrium\Software\uC-CAN\Examples\<OS> where 'OS' is either NONE (for No-OS), uCOS-II or uCOS-III.
Initialization and Creation
Before using CAN Signals and Messages, both Signals and Messages should be initialized and created during Startup.
Code Block | ||||
---|---|---|---|---|
| ||||
void App_CAN_Startup (void) { CPU_INT16S can_err; CANMSG_PARA *m; #if (CANSIG_STATIC_CONFIG == 0u) CANSIG_PARA *s; #endif ... /* ------------------- uC/CAN SETUP ------------------- */ CanSigInit(0L); [1] /* Initialize CAN Signals. */ #if (CANSIG_STATIC_CONFIG == 0u) [2] s = CanSig; while (s < &CanSig[S_MAX]) { /* Create CAN Signals */ can_err = CanSigCreate(s); if (can_err < 0) { while (1); /* Failure Handling Here. */ } s++; } #endif CanMsgInit(0L); [3] /* Initialize CAN Messages. */ m = (CANMSG_PARA *)CanMsg; while (m < &CanMsg[CANMSG_N]) { [4] /* Create CAN Messages. */ can_err = CanMsgCreate(m); if (can_err < 0) { while (1); /* Failure Handling Here. */ } m++; } ... [5] |
Description
- Initialize the CAN signal layer
- If dynamic signal handling is enabled loop through all constant CAN signal parameter list and create the signals
- Initialize the CAN message layer
- Loop through all constant CAN message parameter list and create the messages
- Start the communication bus as described in chapter 6.2 Enable the CAN bus
Source code (Application Task)
Description
...
Application Signals
The following will explain additional details about CAN Signals used in the Application Layer, including the use of the Signal Callback Function initially mentioned in the 'Signal Configuration' section. Please note that write-access to a CAN Signal is possible anywhere in an application. The following will show a snippet of how to write to a Signal and the use of the Callback function for a given signal.
Code Block | ||||
---|---|---|---|---|
| ||||
void App_Task (void *argp)
{
(void)&argp; /* Suppress Compiler Warning. */
... [1]
while (DEF_ON) { /* Endless while loop. */
...
CanSigWrite( S_NODESTATUS, /* Write to the 'Nodestatus' Signal. */
&StatusUpdateVar,
1);
Send_Status(); [2] /* Send Signal Status on CAN bus */
...
}
}
...
void StatusChange (void *Signal, [3]
CANSIG_VAL_T *NewVal,
CPU_INT32U CallbackId)
{
(void)&Signal; /* Suppress Compiler Warning. */
if (CallbackId == CANSIG_CALLBACK_READ_ID) { [4]
if (*NewVal == 1) { [5]
/* $$$ - Start Load-Task - $$$ */
} else {
/* $$$ - Stop Load-Task - $$$ */
}
}
} |