Now that CAN Signals and Messages have been defined and configured, it is time to encapsulate them in the application. Please refer to the previous section if CAN Signals and Messages haven't been defined and configured.
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.
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] |
1] Initialize the CAN Signal Layer. 2] If dynamic signal handling is enabled, jump around creating all constant CAN signal parameter listings and signals. this can be done by setting CANSIG_STATIC_CONFIG > 0 in 3] Initialize the CAN Message Layer. 4] Loop through all constant CAN message parameter listings and create the messages. 5] Start the CAN communication Bus as described in 'Enable the CAN Bus'.
|
void App_Startup(void) { CPU_INT16S err; CANSIG_PARA *s; CANMSG_PARA *m; CanSigInit(0L); -1- #if CANSIG_STATIC_CONFIG == 0 s = CanSig; while (s < &CanSig[S_MAX]) { -2- err = CanSigCreate(s); if (err < 0) { /* failure handling */ } s++; } #endif CanMsgInit(0L); -3- m = CanMsg; while (m < &CanMsg[M_MAX]) { -4- err = CanMsgCreate(m); if (err < 0) { /* failure handling */ } m++; } Com_Start(); -5- /* start application task */ }; |
/* Application */ void App_Task(void) { /* Task initialisation */ /* Endless task loop */ while (1) { /* perform your application actions */ /* set CPU load, e.g. in µC/OS-II: */ CanSigWrite(S_CPULOAD, &OSCPUUsage, 1); -1- Send_Status(); -2- } } /* Signal Callback Function */ void StatusChange(void *Signal, CANSIG_VAL_T *NewVal, CPU_INT32U CallbackId) -3- { if (CallbackId == CANSIG_CALLBACK_READ_ID) { -4- if (*NewVal == 1) { -5- /* start Load-Task */ } else { /* stop Load-Task */ } } } |