src/lifegrd.c
changeset 71 95cd3376cc9f
parent 53 73bc47a0db55
child 149 fe50ada8020b
equal deleted inserted replaced
70:f36f09f08b62 71:95cd3376cc9f
    21 */
    21 */
    22 
    22 
    23 #include <data.h>
    23 #include <data.h>
    24 #include "lifegrd.h"
    24 #include "lifegrd.h"
    25 
    25 
    26 // Prototypes for internals functions
    26 /* Prototypes for internals functions */
    27 void ConsumerHearbeatAlarm(CO_Data* d, UNS32 id);
    27 void ConsumerHearbeatAlarm(CO_Data* d, UNS32 id);
    28 void ProducerHearbeatAlarm(CO_Data* d, UNS32 id);
    28 void ProducerHearbeatAlarm(CO_Data* d, UNS32 id);
    29 
    29 
    30 
    30 
    31 /*****************************************************************************/
    31 /*****************************************************************************/
    34 	e_nodeState networkNodeState = d->NMTable[nodeId];
    34 	e_nodeState networkNodeState = d->NMTable[nodeId];
    35 	return networkNodeState;
    35 	return networkNodeState;
    36 }
    36 }
    37 
    37 
    38 /*****************************************************************************/
    38 /*****************************************************************************/
    39 // The Consumer Timer Callback
    39 /* The Consumer Timer Callback */
    40 void ConsumerHearbeatAlarm(CO_Data* d, UNS32 id)
    40 void ConsumerHearbeatAlarm(CO_Data* d, UNS32 id)
    41 {
    41 {
    42         //MSG_WAR(0x00, "ConsumerHearbeatAlarm", 0x00);
    42         /*MSG_WAR(0x00, "ConsumerHearbeatAlarm", 0x00);*/
    43 	
    43 	
    44 	// call heartbeat error with NodeId
    44 	/* call heartbeat error with NodeId */
    45 	(*d->heartbeatError)((UNS8)( ((d->ConsumerHeartbeatEntries[id]) & (UNS32)0x00FF0000) >> (UNS8)16 ));
    45 	(*d->heartbeatError)((UNS8)( ((d->ConsumerHeartbeatEntries[id]) & (UNS32)0x00FF0000) >> (UNS8)16 ));
    46 }
    46 }
    47 
    47 
    48 /*****************************************************************************/
    48 /*****************************************************************************/
    49 void proceedNODE_GUARD(CO_Data* d, Message* m )
    49 void proceedNODE_GUARD(CO_Data* d, Message* m )
    50 {
    50 {
    51   UNS8 nodeId = (UNS8) GET_NODE_ID((*m));
    51   UNS8 nodeId = (UNS8) GET_NODE_ID((*m));
    52   
    52   
    53   if((m->rtr == 1) ) /* Notice that only the master can have sent this node guarding request */
    53   if((m->rtr == 1) ) /* Notice that only the master can have sent this node guarding request */
    54   { // Receiving a NMT NodeGuarding (request of the state by the master)
    54   { /* Receiving a NMT NodeGuarding (request of the state by the master) */
    55     //  only answer to the NMT NodeGuarding request, the master is not checked (not implemented)
    55     /*  only answer to the NMT NodeGuarding request, the master is not checked (not implemented) */
    56     if (nodeId == *d->bDeviceNodeId )
    56     if (nodeId == *d->bDeviceNodeId )
    57     {
    57     {
    58       Message msg;
    58       Message msg;
    59       msg.cob_id.w = *d->bDeviceNodeId + 0x700;
    59       msg.cob_id.w = *d->bDeviceNodeId + 0x700;
    60       msg.len = (UNS8)0x01;
    60       msg.len = (UNS8)0x01;
    65         msg.data[0] |= 0x80 ;
    65         msg.data[0] |= 0x80 ;
    66         d->toggle = 0 ;
    66         d->toggle = 0 ;
    67       }
    67       }
    68       else
    68       else
    69         d->toggle = 1 ; 
    69         d->toggle = 1 ; 
    70       // send the nodeguard response.
    70       /* send the nodeguard response. */
    71       MSG_WAR(0x3130, "Sending NMT Nodeguard to master, state: ", d->nodeState);
    71       MSG_WAR(0x3130, "Sending NMT Nodeguard to master, state: ", d->nodeState);
    72       (*d->canSend)(&msg );
    72       (*d->canSend)(&msg );
    73     }  
    73     }  
    74 
    74 
    75   }else{ // Not a request CAN  
    75   }else{ /* Not a request CAN */
    76 
    76 
    77     MSG_WAR(0x3110, "Received NMT nodeId : ", nodeId);
    77     MSG_WAR(0x3110, "Received NMT nodeId : ", nodeId);
    78     /* the slave's state receievd is stored in the NMTable */
    78     /* the slave's state receievd is stored in the NMTable */
    79       // The state is stored on 7 bit
    79       /* The state is stored on 7 bit */
    80     d->NMTable[nodeId] = (e_nodeState) ((*m).data[0] & 0x7F) ;
    80     d->NMTable[nodeId] = (e_nodeState) ((*m).data[0] & 0x7F) ;
    81     
    81     
    82     /* Boot-Up frame reception */
    82     /* Boot-Up frame reception */
    83     if ( d->NMTable[nodeId] == Initialisation)
    83     if ( d->NMTable[nodeId] == Initialisation)
    84       {
    84       {
    85         // The device send the boot-up message (Initialisation)
    85         /* The device send the boot-up message (Initialisation) */
    86         // to indicate the master that it is entered in pre_operational mode
    86         /* to indicate the master that it is entered in pre_operational mode */
    87         // Because the  device enter automaticaly in pre_operational mode,
    87         /* Because the  device enter automaticaly in pre_operational mode, */
    88         // the pre_operational mode is stored 
    88         /* the pre_operational mode is stored */
    89 //        NMTable[bus_id][nodeId] = Pre_operational;
    89 /*        NMTable[bus_id][nodeId] = Pre_operational; */
    90         MSG_WAR(0x3100, "The NMT is a bootup from node : ", nodeId);
    90         MSG_WAR(0x3100, "The NMT is a bootup from node : ", nodeId);
    91       }
    91       }
    92       
    92       
    93     if( d->NMTable[nodeId] != Unknown_state ) {
    93     if( d->NMTable[nodeId] != Unknown_state ) {
    94         UNS8 index, ConsummerHeartBeat_nodeId ;
    94         UNS8 index, ConsummerHeartBeat_nodeId ;
    96         {
    96         {
    97             ConsummerHeartBeat_nodeId = (UNS8)( ((d->ConsumerHeartbeatEntries[index]) & (UNS32)0x00FF0000) >> (UNS8)16 );
    97             ConsummerHeartBeat_nodeId = (UNS8)( ((d->ConsumerHeartbeatEntries[index]) & (UNS32)0x00FF0000) >> (UNS8)16 );
    98             if ( nodeId == ConsummerHeartBeat_nodeId )
    98             if ( nodeId == ConsummerHeartBeat_nodeId )
    99             {
    99             {
   100                 TIMEVAL time = ( (d->ConsumerHeartbeatEntries[index]) & (UNS32)0x0000FFFF ) ;
   100                 TIMEVAL time = ( (d->ConsumerHeartbeatEntries[index]) & (UNS32)0x0000FFFF ) ;
   101             	// Renew alarm for next heartbeat.
   101             	/* Renew alarm for next heartbeat. */
   102             	DelAlarm(d->ConsumerHeartBeatTimers[index]);
   102             	DelAlarm(d->ConsumerHeartBeatTimers[index]);
   103             	d->ConsumerHeartBeatTimers[index] = SetAlarm(d, index, &ConsumerHearbeatAlarm, MS_TO_TIMEVAL(time), 0);
   103             	d->ConsumerHeartBeatTimers[index] = SetAlarm(d, index, &ConsumerHearbeatAlarm, MS_TO_TIMEVAL(time), 0);
   104             }
   104             }
   105         }
   105         }
   106     }
   106     }
   107   }
   107   }
   108 }
   108 }
   109 
   109 
   110 /*****************************************************************************/
   110 /*****************************************************************************/
   111 // The Consumer Timer Callback
   111 /* The Consumer Timer Callback */
   112 void ProducerHearbeatAlarm(CO_Data* d, UNS32 id)
   112 void ProducerHearbeatAlarm(CO_Data* d, UNS32 id)
   113 {
   113 {
   114 	if(*d->ProducerHeartBeatTime)
   114 	if(*d->ProducerHeartBeatTime)
   115 	{
   115 	{
   116 		Message msg;
   116 		Message msg;
   117 		// Time expired, the heartbeat must be sent immediately
   117 		/* Time expired, the heartbeat must be sent immediately
   118 		// generate the correct node-id: this is done by the offset 1792
   118 		 * generate the correct node-id: this is done by the offset 1792
   119 		// (decimal) and additionaly
   119 		 * (decimal) and additionaly
   120 		// the node-id of this device.
   120 		 * the node-id of this device.
       
   121 		 */
   121 		msg.cob_id.w = *d->bDeviceNodeId + 0x700;
   122 		msg.cob_id.w = *d->bDeviceNodeId + 0x700;
   122 		msg.len = (UNS8)0x01;
   123 		msg.len = (UNS8)0x01;
   123 		msg.rtr = 0;
   124 		msg.rtr = 0;
   124 		msg.data[0] = d->nodeState; // No toggle for heartbeat !
   125 		msg.data[0] = d->nodeState; /* No toggle for heartbeat !*/
   125 		// send the heartbeat
   126 		/* send the heartbeat */
   126       		MSG_WAR(0x3130, "Producing heartbeat: ", d->nodeState);
   127       		MSG_WAR(0x3130, "Producing heartbeat: ", d->nodeState);
   127       		(*d->canSend)(&msg );
   128       		(*d->canSend)(&msg );
   128 	}else{
   129 	}else{
   129 		d->ProducerHeartBeatTimer = DelAlarm(d->ProducerHeartBeatTimer);
   130 		d->ProducerHeartBeatTimer = DelAlarm(d->ProducerHeartBeatTimer);
   130 	}
   131 	}
   131 }
   132 }
   132 
   133 
   133 /*****************************************************************************/
   134 /*****************************************************************************/
   134 void heartbeatInit(CO_Data* d)
   135 void heartbeatInit(CO_Data* d)
   135 {
   136 {
   136     UNS8 index; // Index to scan the table of heartbeat consumers
   137     UNS8 index; /* Index to scan the table of heartbeat consumers */
   137 
   138 
   138     d->toggle = 0;
   139     d->toggle = 0;
   139 
   140 
   140     for( index = (UNS8)0x00; index < *d->ConsumerHeartbeatCount; index++ )
   141     for( index = (UNS8)0x00; index < *d->ConsumerHeartbeatCount; index++ )
   141     {
   142     {
   142         TIMEVAL time = (UNS16) ( (d->ConsumerHeartbeatEntries[index]) & (UNS32)0x0000FFFF ) ;
   143         TIMEVAL time = (UNS16) ( (d->ConsumerHeartbeatEntries[index]) & (UNS32)0x0000FFFF ) ;
   143         //MSG_WAR(0x3121, "should_time : ", should_time ) ;
   144         /* MSG_WAR(0x3121, "should_time : ", should_time ) ; */
   144         if ( time )
   145         if ( time )
   145         {
   146         {
   146             	d->ConsumerHeartBeatTimers[index] = SetAlarm(d, index, &ConsumerHearbeatAlarm, MS_TO_TIMEVAL(time), 0);
   147             	d->ConsumerHeartBeatTimers[index] = SetAlarm(d, index, &ConsumerHearbeatAlarm, MS_TO_TIMEVAL(time), 0);
   147         }
   148         }
   148     }
   149     }