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.

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. 

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.
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 */
};

Description

  1. Initialize the CAN signal layer
  2. If dynamic signal handling is enabled loop through all constant CAN signal parameter list and create the signals
  3. Initialize the CAN message layer
  4. Loop through all constant CAN message parameter list and create the messages
  5. Start the communication bus as described in chapter 6.2 Enable the CAN bus

Source code (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 */

		}
	}
}

Description

  1. Anywhere in your application the write access to the CAN signal is possible.
  2. This function will send the status message to the CAN bus. The source code of the function is shown below.
  3. This callback-function is included in the signal configuration parameter list for the signal status. The function is called, when the change of the signal value is detected.
  4. The parameter CallbackId is used to identify where this callback-function was called from.
  5. The parameter NewVal holds the new CAN signal value and is used to determine the required start / stop action of the Load-Task.