CANopen FD Master Library
Making your systems precise, reliable and high-quality - 5.10.01
canopen_main.c

The following example shows how to start the CANopen FD Master library under Linux. The program runs until CTRL-C is pressed. After transmission of the boot-up message (i.e. identifier 77Fh in this example) the application will check for another instance of a CANopen Master on the same bus be calling ComNmtMasterDetection(). A hearbeat with a cycle time of 2 seconds is configured by calling ComNmtSetHbProdTime().

//----------------------------------------------------------------------------//
// main() //
// //
//----------------------------------------------------------------------------//
int main(int argc, char *argv[])
{
uint32_t ulTickOneSecondT = 0;
ubProgRunS = 1;
//----------------------------------------------------------------
// Print application information
//
printf("\n\n");
printf("###############################################################################\n");
printf("# uMIC.200 CANopen Master Example #\n");
printf("###############################################################################\n");
printf("\n");
printf("Use CTRL-C to quit this demo.\n");
printf("\n");
//----------------------------------------------------------------
// Initialise the signal handler for timer and CTRL-C
//
init_signal_handler();
//----------------------------------------------------------------
// Initialise the CANopen master stack
// The bit-rate value is a dummy here, since the bit-rate is
// set via the socketcan configuration file.
//
//----------------------------------------------------------------
// start the CANopen master stack
//
//----------------------------------------------------------------
// start master detection procedure
//
//----------------------------------------------------------------
// CANopen master is heartbeat producer, 2s
//
//----------------------------------------------------------------
// this is the main loop of the application
//
while(ubProgRunS)
{
while(ulTickOneSecondT == ulCounterOneSecondS)
{
sleep(10);
}
ulTickOneSecondT = ulCounterOneSecondS;
}
//----------------------------------------------------------------
// reset all nodes when master quits
//
printf("\n");
printf("Quit CANopen Master demo.\n");
return(0);
}

Signal handler for timing

After initialisation of the CANopen FD Master library, the function ComMgrTimerEvent() must be called periodically. The timer period can be configured to the application via ComTmrSetPeriod().

Under Linux, we use a simple alarm handler for that purpose. The alarm handler is initialised inside the function init_signal_handler().

//----------------------------------------------------------------------------//
// init_signal_handler() //
// install signal handler for CTRL-C (SIGINT) and alarm (SIGALRM) //
//----------------------------------------------------------------------------//
void init_signal_handler(void)
{
struct sigaction tsSigActionT;
struct itimerval tsTimerValT;
//----------------------------------------------------------------
// Configure a handler for CTRL-C
//
tsSigActionT.sa_handler = sig_handler_quit;
sigemptyset(&tsSigActionT.sa_mask);
tsSigActionT.sa_flags = 0;
sigaction(SIGINT, &tsSigActionT, 0);
//----------------------------------------------------------------
// Configure a handler for an alarm triggered by a timer
//
tsSigActionT.sa_handler = sig_handler_time;
sigemptyset(&tsSigActionT.sa_mask);
tsSigActionT.sa_flags = 0;
sigaction(SIGALRM, &tsSigActionT, 0);
//----------------------------------------------------------------
// setup a 10 ms timer interval
//
tsTimerValT.it_value.tv_sec = 1;
tsTimerValT.it_value.tv_usec = 0;
tsTimerValT.it_interval.tv_sec = 0;
tsTimerValT.it_interval.tv_usec = 10000;
setitimer(ITIMER_REAL, &tsTimerValT, NULL);
}

The signal SIGALRM will call the function sig_handler_time() within a 10 ms period.

//----------------------------------------------------------------------------//
// sig_handler_time() //
// timer //
//----------------------------------------------------------------------------//
void sig_handler_time(int slSignalV)
{
static uint32_t ulCounterTenMillisecondsS = 0;
if(slSignalV == SIGALRM)
{
ulCounterTenMillisecondsS++;
if(ulCounterTenMillisecondsS == 100)
{
ulCounterOneSecondS++;
ulCounterTenMillisecondsS = 0;
}
}
}