src/states.c
changeset 378 d2abf6c8c27b
parent 370 6fecf36df407
child 381 854c43cdc24a
equal deleted inserted replaced
377:bea5a1576cbd 378:d2abf6c8c27b
    27 ** @brief
    27 ** @brief
    28 **
    28 **
    29 **
    29 **
    30 */
    30 */
    31 
    31 
    32 #include "states.h"
    32 #include "data.h"
    33 #include "def.h"
       
    34 #include "dcf.h"
       
    35 #include "nmtSlave.h"
       
    36 #include "emcy.h"
       
    37 #ifdef CO_ENABLE_LSS
       
    38 #include "lss.h"
       
    39 #endif
       
    40 #include "sysdep.h"
    33 #include "sysdep.h"
    41 
    34 
    42 /** Prototypes for internals functions */
    35 /** Prototypes for internals functions */
    43 /*!                                                                                                
    36 /*!                                                                                                
    44 **                                                                                                 
    37 **                                                                                                 
   162 **                                                                                                 
   155 **                                                                                                 
   163 ** @return                                                                                         
   156 ** @return                                                                                         
   164 **/  
   157 **/  
   165 UNS8 setState(CO_Data* d, e_nodeState newState)
   158 UNS8 setState(CO_Data* d, e_nodeState newState)
   166 {
   159 {
   167 	while(newState != d->nodeState){
   160 	if(newState != d->nodeState){
   168 		switch( newState ){
   161 		switch( newState ){
   169 			case Initialisation:
   162 			case Initialisation:
   170 			{
   163 			{
   171 				s_state_communication newCommunicationState = {1, 0, 0, 0, 0, 0, 0};
   164 				s_state_communication newCommunicationState = {1, 0, 0, 0, 0, 0, 0};
   172 				/* This will force a second loop for the state switch */
       
   173 				d->nodeState = Initialisation;
   165 				d->nodeState = Initialisation;
   174 				newState = Pre_operational;
   166 				switchCommunicationState(d, &newCommunicationState);
   175 				switchCommunicationState(d, &newCommunicationState);
   167 				/* call user app init callback now. */
   176 				/* call user app related state func. */
   168 				/* d->initialisation MUST NOT CALL SetState */
   177 				(*d->initialisation)();
   169 				(*d->initialisation)(d);				
   178 				
   170 			}
   179 			}
   171 
   180 			break;
   172 			/* Automatic transition - No break statement ! */
       
   173 			/* Transition from Initialisation to Pre_operational */
       
   174 			/* is automatic as defined in DS301. */
       
   175 			/* App don't have to call SetState(d, Pre_operational) */
   181 								
   176 								
   182 			case Pre_operational:
   177 			case Pre_operational:
   183 			{
   178 			{
   184 				
   179 				
   185 				s_state_communication newCommunicationState = {0, 1, 1, 1, 1, 0, 1};
   180 				s_state_communication newCommunicationState = {0, 1, 1, 1, 1, 0, 1};
   186 				d->nodeState = Pre_operational;
   181 				d->nodeState = Pre_operational;
   187 				newState = Pre_operational;
       
   188 				switchCommunicationState(d, &newCommunicationState);
   182 				switchCommunicationState(d, &newCommunicationState);
   189 				if (!(*(d->iam_a_slave)))
   183 				if (!(*(d->iam_a_slave)))
   190 				{
   184 				{
   191 					//send_consise_dcf(d,0x01);
       
   192 					masterSendNMTstateChange (d, 0, NMT_Reset_Node);
   185 					masterSendNMTstateChange (d, 0, NMT_Reset_Node);
   193                     (*d->preOperational)(); 
       
   194 				}
   186 				}
   195 				else 
   187                 (*d->preOperational)(d);
   196 				{
       
   197 					(*d->preOperational)();
       
   198 				}
       
   199 			}
   188 			}
   200 			break;
   189 			break;
   201 								
   190 								
   202 			case Operational:
   191 			case Operational:
   203 			if(d->nodeState == Initialisation) return 0xFF;
   192 			if(d->nodeState == Initialisation) return 0xFF;
   204 			{
   193 			{
   205 				s_state_communication newCommunicationState = {0, 1, 1, 1, 1, 1, 0};
   194 				s_state_communication newCommunicationState = {0, 1, 1, 1, 1, 1, 0};
   206 				d->nodeState = Operational;
   195 				d->nodeState = Operational;
   207 				newState = Operational;
   196 				newState = Operational;
   208 				switchCommunicationState(d, &newCommunicationState);
   197 				switchCommunicationState(d, &newCommunicationState);
   209 				(*d->operational)();
   198 				(*d->operational)(d);
   210 			}
   199 			}
   211 			break;
   200 			break;
   212 						
   201 						
   213 			case Stopped:
   202 			case Stopped:
   214 			if(d->nodeState == Initialisation) return 0xFF;
   203 			if(d->nodeState == Initialisation) return 0xFF;
   215 			{
   204 			{
   216 				s_state_communication newCommunicationState = {0, 0, 0, 0, 1, 0, 1};
   205 				s_state_communication newCommunicationState = {0, 0, 0, 0, 1, 0, 1};
   217 				d->nodeState = Stopped;
   206 				d->nodeState = Stopped;
   218 				newState = Stopped;
   207 				newState = Stopped;
   219 				switchCommunicationState(d, &newCommunicationState);
   208 				switchCommunicationState(d, &newCommunicationState);
   220 				(*d->stopped)();
   209 				(*d->stopped)(d);
   221 			}
   210 			}
   222 			break;
   211 			break;
   223 #ifdef CO_ENABLE_LSS
   212 #ifdef CO_ENABLE_LSS
   224 			case LssTimingDelay:
   213 			case LssTimingDelay:
   225 			{
   214 			{
   228 				newState = LssTimingDelay;
   217 				newState = LssTimingDelay;
   229 				switchCommunicationState(d, &newCommunicationState);
   218 				switchCommunicationState(d, &newCommunicationState);
   230 			}
   219 			}
   231 			break;
   220 			break;
   232 #endif
   221 #endif
   233 			
       
   234 			default:
       
   235 				return 0xFF;
       
   236 		}/* end switch case */
   222 		}/* end switch case */
   237 	
   223 	
   238 	}
   224 	}
   239 	return 0;
   225 	/* d->nodeState contains the final state */
       
   226 	/* may not be the requested state */
       
   227     return d->nodeState;  
   240 }
   228 }
   241 
   229 
   242 /*!                                                                                                
   230 /*!                                                                                                
   243 **                                                                                                 
   231 **                                                                                                 
   244 **                                                                                                 
   232 **                                                                                                 
   325 
   313 
   326   /* bDeviceNodeId is defined in the object dictionary. */
   314   /* bDeviceNodeId is defined in the object dictionary. */
   327   *d->bDeviceNodeId = nodeId;
   315   *d->bDeviceNodeId = nodeId;
   328 }
   316 }
   329 
   317 
   330 void _initialisation(){}
   318 void _initialisation(CO_Data* d){}
   331 void _preOperational(){}
   319 void _preOperational(CO_Data* d){}
   332 void _operational(){}
   320 void _operational(CO_Data* d){}
   333 void _stopped(){}
   321 void _stopped(CO_Data* d){}