Initializing CAN Signals and Messages

Now that CAN Signals and Messages have been defined and configured, it is time to initialize them in the application. Please refer to the previous section if CAN Signals and Messages haven't been defined and configured. 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. 

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 can_cfg.h.

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'.

  • Please note that the CAN Bus must be configured before enabling the CAN Bus. Please refer to 'Configure the CAN Bus' for more details.

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.


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 - $$$ */
        }
    }
}

1] In this example, it is assumed that Signals and Messages have been configured and initialized as well as any other actions that the application task requires.

2] Once the Signal has been written to, this function will send the updated status on the CAN Bus.

3] The callback function used here must be the same callback function configured in the Signal Configuration for the given CAN Signal. The function will be called when a value change is detected on the Signal.

4] the 'CallbackId' parameter is used to identify where the callback function was called from.

5] The 'NewVal' parameter holds the updated CAN Signal value and is used to determine any required actions.