src/lss.c
changeset 381 854c43cdc24a
parent 370 6fecf36df407
child 384 83793fc7ce48
equal deleted inserted replaced
380:fe47568b8281 381:854c43cdc24a
    60 
    60 
    61 /* Returns the LSS FastScan LSSNext field from a Message struct */
    61 /* Returns the LSS FastScan LSSNext field from a Message struct */
    62 #define getLSSNext(msg) msg->data[7]
    62 #define getLSSNext(msg) msg->data[7]
    63 
    63 
    64 /* Prototypes for internals functions */
    64 /* Prototypes for internals functions */
    65 void LssAlarm(CO_Data* d, UNS32 id);
    65 UNS8 sendMasterLSSMessage(CO_Data* d, UNS8 command,void *dat1,void *dat2);
    66 
    66 void LssAlarmMSG(CO_Data* d, UNS32 id);
    67 #define StopLSS_TIMER(id){\
    67 void LssAlarmSDELAY(CO_Data* d, UNS32 id);
    68  MSG_WAR(0x3D01, "StopLSS_TIMER for timer : ", id);\
    68 
    69  d->lss_transfer.timers[id] = DelAlarm(d->lss_transfer.timers[id]);}
    69 
       
    70 #define StopLSS_MSG_TIMER(){\
       
    71  MSG_WAR(0x3D01, "StopLSS_MSG_TIMER", 0);\
       
    72  d->lss_transfer.timerMSG = DelAlarm(d->lss_transfer.timerMSG);}
    70 
    73 
    71 #define StartLSS_MSG_TIMER(){\
    74 #define StartLSS_MSG_TIMER(){\
    72  MSG_WAR(0x3D02, "StartLSS_TIMER for MSG_TIMER",0);\
    75  MSG_WAR(0x3D02, "StartLSS_MSG_TIMER",0);\
    73  d->lss_transfer.timers[LSS_MSG_TIMER] = SetAlarm(d,LSS_MSG_TIMER,&LssAlarm,MS_TO_TIMEVAL(LSS_TIMEOUT_MS),0);}
    76  d->lss_transfer.timerMSG = SetAlarm(d,0,&LssAlarmMSG,MS_TO_TIMEVAL(LSS_TIMEOUT_MS),0);}
       
    77  
       
    78 #define StopLSS_SDELAY_TIMER(){\
       
    79  MSG_WAR(0x3D03, "StopLSS_SDELAY_TIMER", 0);\
       
    80  d->lss_transfer.timerSDELAY = DelAlarm(d->lss_transfer.timerSDELAY);}
    74 
    81 
    75 #define StartLSS_SDELAY_TIMER(){\
    82 #define StartLSS_SDELAY_TIMER(){\
    76  MSG_WAR(0x3D03, "StartLSS_TIMER for SDELAY_TIMER",0);\
    83  MSG_WAR(0x3D04, "StartLSS_SDELAY_TIMER",0);\
    77  d->lss_transfer.timers[LSS_SWITCH_DELAY_TIMER] = SetAlarm(d,LSS_SWITCH_DELAY_TIMER,&LssAlarm,MS_TO_TIMEVAL(d->lss_transfer.switchDelay),MS_TO_TIMEVAL(d->lss_transfer.switchDelay));}
    84  d->lss_transfer.timerSDELAY= SetAlarm(d,0,&LssAlarmSDELAY,MS_TO_TIMEVAL(d->lss_transfer.switchDelay),MS_TO_TIMEVAL(d->lss_transfer.switchDelay));}
       
    85 
       
    86 
       
    87 #ifdef CO_ENABLE_LSS_FS
       
    88 /* Prototypes for internals functions */
       
    89 void LssAlarmFS(CO_Data* d, UNS32 id);
       
    90 
       
    91 #define StopLSS_FS_TIMER(){\
       
    92  MSG_WAR(0x3D05, "StopLSS_FS_TIMER", id);\
       
    93  d->lss_transfer.timerFS = DelAlarm(d->lss_transfer.timerFS);}
    78 
    94 
    79 #define StartLSS_FS_TIMER(){\
    95 #define StartLSS_FS_TIMER(){\
    80  MSG_WAR(0x3D04, "StartLSS_TIMER for FS_TIMER",0);\
    96  MSG_WAR(0x3D06, "StartLSS_FS_TIMER",0);\
    81  d->lss_transfer.timers[LSS_FS_TIMER] = SetAlarm(d,LSS_FS_TIMER,&LssAlarm,MS_TO_TIMEVAL(LSS_FS_TIMEOUT_MS),0);}
    97  d->lss_transfer.timerFS = SetAlarm(d,0,&LssAlarmFS,MS_TO_TIMEVAL(LSS_FS_TIMEOUT_MS),0);}
    82 
    98 #endif
    83 UNS8 sendMasterLSSMessage(CO_Data* d, UNS8 command,void *dat1,void *dat2);
    99 
    84 
   100 
       
   101 void LssAlarmMSG(CO_Data* d, UNS32 id)
       
   102 {	
       
   103  	StopLSS_MSG_TIMER();
       
   104 #ifdef CO_ENABLE_LSS_FS
       
   105 	if(d->lss_transfer.command==LSS_IDENT_FASTSCAN){
       
   106 		if(d->lss_transfer.FastScan_SM==LSS_FS_RESET){
       
   107    			/* if at least one node had answered before the timer expired, start the FastScan protocol*/
       
   108    			if(d->lss_transfer.LSSanswer!=0){
       
   109    				d->lss_transfer.LSSanswer=0;
       
   110    				d->lss_transfer.BitChecked=31;
       
   111    				d->lss_transfer.IDNumber=0;
       
   112    				d->lss_transfer.FastScan_SM=LSS_FS_PROCESSING;
       
   113    				StartLSS_FS_TIMER();
       
   114    				sendMasterLSSMessage(d,LSS_IDENT_FASTSCAN,0,0);
       
   115    				return;
       
   116    			}
       
   117    			else{ 
       
   118    			
       
   119     			d->lss_transfer.state = LSS_FINISHED;
       
   120     			/* Inform the application that there aren't not configured nodes in the net  */
       
   121     			d->lss_transfer.dat1=1;
       
   122    			}
       
   123    		}
       
   124    		else{
       
   125 			/* This should not happen, an error ocurred*/
       
   126 			MSG_ERR(0x1D07, "LSS FastScan timeout. FastScan_SM inconsisten state.", d->lss_transfer.FastScan_SM);
       
   127    		}
       
   128 	}
       
   129 	else
       
   130 #endif
       
   131 	if(d->lss_transfer.command==LSS_IDENT_REMOTE_NON_CONF){
       
   132 			MSG_WAR(0x2D08, "LSS timeout. There are not no-configured slaves in the net", 0);
       
   133 			d->lss_transfer.state = LSS_FINISHED;
       
   134 			d->lss_transfer.dat1=1;
       
   135 	}
       
   136 	else{
       
   137 		MSG_ERR(0x1D09, "LSS timeout. LSS response not received.", 0);
       
   138     	MSG_WAR(0x2D0A, "LSS timeout command specifier : ", d->lss_transfer.command);
       
   139     	/* Set aborted state */
       
   140     	d->lss_transfer.state = LSS_ABORTED_INTERNAL;
       
   141     }
       
   142     	
       
   143     /* Call the user function to inform of the problem.*/
       
   144     if(d->lss_transfer.Callback){
       
   145 	   	/*If there is a callback, it is responsible of the error*/
       
   146     	(*d->lss_transfer.Callback)(d,d->lss_transfer.command);
       
   147     }
       
   148 }
       
   149     
       
   150     
    85 /*!                                                                                                
   151 /*!                                                                                                
    86 **                                                                                                 
   152 **                                                                                                 
    87 **                                                                                                 
   153 **                                                                                                 
    88 ** @param d                                                                                        
   154 ** @param d                                                                                        
    89 ** @param id                                                                                       
   155 ** @param id                                                                                       
    90 **/   
   156 **/   
    91 //struct timeval current_time,init_time;
   157 void LssAlarmSDELAY(CO_Data* d, UNS32 id)
    92 void LssAlarm(CO_Data* d, UNS32 id)
       
    93 {	
   158 {	
    94 	/*unsigned long time_period;
   159 	
    95 	
   160 	/* The first switch_delay period expired. Store the node state, change it 
    96 	gettimeofday(&current_time,NULL);
   161  	 * so no CAN messages will be sent or received, call the ChangeBaudRate function*/
    97  	time_period=(current_time.tv_sec - init_time.tv_sec)* 1000000 + current_time.tv_usec - init_time.tv_usec;
   162    	if(d->lss_transfer.switchDelayState==SDELAY_FIRST){
    98  	printf("%3ld.%3ld.%3ld --",time_period/1000000,(time_period%1000000)/1000,time_period%1000);*/
   163    		MSG_WAR(0x3D0B, "LSS switch delay first period expired",0);
    99  
   164     	d->lss_transfer.switchDelayState=SDELAY_SECOND;
   100 	switch(id){
   165     	(*d->lss_ChangeBaudRate)(d,d->lss_transfer.baudRate);
   101 	case LSS_MSG_TIMER:
   166     }
   102 	 	StopLSS_TIMER(LSS_MSG_TIMER);
   167     else{ /* d->lss_transfer.switchDelayState==SDELAY_SECOND */
       
   168     	MSG_WAR(0x3D0C, "LSS switch delay second period expired",0);
       
   169     	d->lss_transfer.switchDelayState=SDELAY_OFF;
       
   170     	StopLSS_SDELAY_TIMER();
       
   171     		
       
   172     	if (*(d->iam_a_slave))
       
   173     		d->canHandle=d->lss_transfer.canHandle_t;
       
   174     	else{
       
   175     		d->lss_transfer.dat1=0;
       
   176     		d->lss_transfer.state=LSS_FINISHED;
       
   177     		/* Call the user function */
       
   178     		if(d->lss_transfer.Callback){
       
   179 	    		(*d->lss_transfer.Callback)(d,d->lss_transfer.command);
       
   180     		}
       
   181     	}
       
   182     }
       
   183 } 
       
   184 
   103 #ifdef CO_ENABLE_LSS_FS
   185 #ifdef CO_ENABLE_LSS_FS
   104 		if(d->lss_transfer.command==LSS_IDENT_FASTSCAN){
   186 /*!                                                                                                
   105 			if(d->lss_transfer.FastScan_SM==LSS_FS_RESET){
   187 **                                                                                                 
   106    				/* if at least one node had answered before the timer expired, start the FastScan protocol*/
   188 **                                                                                                 
   107    				if(d->lss_transfer.LSSanswer!=0){
   189 ** @param d                                                                                        
   108    					d->lss_transfer.LSSanswer=0;
   190 ** @param id                                                                                       
   109    					d->lss_transfer.BitChecked=31;
   191 **/   
   110    					d->lss_transfer.IDNumber=0;
   192 void LssAlarmFS(CO_Data* d, UNS32 id)
   111    					d->lss_transfer.FastScan_SM=LSS_FS_PROCESSING;
   193 {	
   112    					StartLSS_FS_TIMER();
   194 	StopLSS_FS_TIMER();
   113    					sendMasterLSSMessage(d,LSS_IDENT_FASTSCAN,0,0);
   195 		
   114    					return;
   196 	switch(d->lss_transfer.FastScan_SM){
   115    				}
   197    	case LSS_FS_RESET:
   116    				else{ 
   198    	{
   117    				
   199    	   	/* This should not happen, an error ocurred*/
   118     				d->lss_transfer.state = LSS_FINISHED;
   200 		MSG_ERR(0x1D0D, "LSS FastScan timeout. FastScan_SM inconsisten state.", d->lss_transfer.FastScan_SM);
   119     				/* Inform the application that there aren't not configured nodes in the net  */
   201    	}
   120     				d->lss_transfer.dat1=1;
   202    	break;
   121    				}
   203 	case LSS_FS_PROCESSING:
   122    			}
   204 	{
   123    			else{
   205 		/* If there isn't any answer, set the bit */
   124 				/* This should not happen, an error ocurred*/
   206 		if(d->lss_transfer.LSSanswer==0){
   125 				MSG_ERR(0x1D05, "LSS FastScan timeout. FastScan_SM inconsisten state.", d->lss_transfer.FastScan_SM);
   207 			UNS32 Mask=0x1;
   126    			}
   208 			Mask<<=d->lss_transfer.BitChecked;
   127 		}
   209 			d->lss_transfer.IDNumber|=Mask;
   128 		else
   210 		}
       
   211 		
       
   212 		if(d->lss_transfer.BitChecked==0){
       
   213 			/* We finished with the current LSS-ID[sub], confirm it */
       
   214 			d->lss_transfer.FastScan_SM=LSS_FS_CONFIRMATION;
       
   215 			if(d->lss_transfer.LSSNext<3)d->lss_transfer.LSSNext++;
       
   216 		}
       
   217 		else{
       
   218 			d->lss_transfer.BitChecked--;
       
   219 		}
       
   220    			
       
   221    		d->lss_transfer.LSSanswer=0;
       
   222   		StartLSS_FS_TIMER();
       
   223    		sendMasterLSSMessage(d,LSS_IDENT_FASTSCAN,0,0);
       
   224    		return;
       
   225    	}
       
   226 	break;
       
   227 	case LSS_FS_CONFIRMATION:
       
   228 	{
       
   229 		if(d->lss_transfer.LSSanswer!=0){
       
   230 			d->lss_transfer.LSSanswer=0;
       
   231 			
       
   232 			if(d->lss_transfer.LSSSub==3){
       
   233 				/* The LSS FastScan protocol finished correctly. Restore the parameters */
       
   234 				d->lss_transfer.BitChecked=128;
       
   235 				d->lss_transfer.FastScan_SM=LSS_FS_RESET;
       
   236 				d->lss_transfer.LSSSub=0;
       
   237 				d->lss_transfer.LSSNext=0;
       
   238    				d->lss_transfer.IDNumber=0;
       
   239 				
       
   240 				/* Inform the application that the FastScan finished correctly */
       
   241 				d->lss_transfer.state = LSS_FINISHED;
       
   242 				d->lss_transfer.dat1=0;
       
   243 			}
       
   244 			else{
       
   245 				/* Start with the next LSS-ID[sub] */
       
   246 				d->lss_transfer.LSSSub++;
       
   247 				d->lss_transfer.BitChecked=31;
       
   248    				d->lss_transfer.IDNumber=0;
       
   249    				d->lss_transfer.FastScan_SM=LSS_FS_PROCESSING;
       
   250    				StartLSS_FS_TIMER();
       
   251    				sendMasterLSSMessage(d,LSS_IDENT_FASTSCAN,0,0);
       
   252    				return;
       
   253 			}
       
   254 		}
       
   255 		else{
       
   256 			/* This should not happen, an error ocurred*/
       
   257 			MSG_ERR(0x1D0E, "LSS FastScan timeout. FastScan response not received.", 0);
       
   258 			/* Set aborted state */
       
   259     		d->lss_transfer.state = LSS_ABORTED_INTERNAL;
       
   260 		}
       
   261 	}
       
   262 	break;
       
   263 	}
       
   264 
       
   265    	/* Call the user function to inform of the problem.*/
       
   266    	if(d->lss_transfer.Callback){
       
   267     	/*If there is a callback, it is responsible of the error*/
       
   268    		(*d->lss_transfer.Callback)(d,d->lss_transfer.command);
       
   269    	}
       
   270 }
   129 #endif
   271 #endif
   130 		{
   272 
   131 			MSG_ERR(0x1D06, "LSS timeout. LSS response not received.", 0);
   273 	
   132     		MSG_WAR(0x2D07, "LSS timeout command specifier : ", d->lss_transfer.command);
       
   133     		/* Set aborted state */
       
   134     		d->lss_transfer.state = LSS_ABORTED_INTERNAL;
       
   135     	}
       
   136     	
       
   137     	/* Call the user function to inform of the problem.*/
       
   138     	if(d->lss_transfer.Callback){
       
   139 	    	/*If there is a callback, it is responsible of the error*/
       
   140     		(*d->lss_transfer.Callback)(d,d->lss_transfer.command);
       
   141     	}
       
   142     break;
       
   143     case LSS_SWITCH_DELAY_TIMER:
       
   144   		/* The first switch_delay period expired. Store the node state, change it 
       
   145  		 * so no CAN messages will be sent or received, call the ChangeBaudRate function*/
       
   146    		if(d->lss_transfer.switchDelayState==SDELAY_FIRST){
       
   147    			MSG_WAR(0x3D08, "LSS switch delay first period expired",0);
       
   148     		d->lss_transfer.switchDelayState=SDELAY_SECOND;
       
   149     		(*d->lss_ChangeBaudRate)(d->lss_transfer.baudRate);
       
   150     	}
       
   151     	else{ /* d->lss_transfer.switchDelayState==SDELAY_SECOND */
       
   152     		MSG_WAR(0x3D09, "LSS switch delay second period expired",0);
       
   153     		d->lss_transfer.switchDelayState=SDELAY_OFF;
       
   154     		StopLSS_TIMER(LSS_SWITCH_DELAY_TIMER);
       
   155     		
       
   156     		setState(d, d->lss_transfer.currentState);
       
   157     	}
       
   158      break;
       
   159 #ifdef CO_ENABLE_LSS_FS
       
   160      case LSS_FS_TIMER:
       
   161 	 	StopLSS_TIMER(LSS_FS_TIMER);
       
   162 		
       
   163 		switch(d->lss_transfer.FastScan_SM){
       
   164    		case LSS_FS_RESET:
       
   165    		{
       
   166    		   	/* This should not happen, an error ocurred*/
       
   167 			MSG_ERR(0x1D0A, "LSS FastScan timeout. FastScan_SM inconsisten state.", d->lss_transfer.FastScan_SM);
       
   168    		}
       
   169    		break;
       
   170 		case LSS_FS_PROCESSING:
       
   171 		{
       
   172 			/* If there isn't any answer, set the bit */
       
   173 			if(d->lss_transfer.LSSanswer==0){
       
   174 				UNS32 Mask=0x1;
       
   175 				Mask<<=d->lss_transfer.BitChecked;
       
   176 				d->lss_transfer.IDNumber|=Mask;
       
   177 			}
       
   178 			
       
   179 			if(d->lss_transfer.BitChecked==0){
       
   180 				/* We finished with the current LSS-ID[sub], confirm it */
       
   181 				d->lss_transfer.FastScan_SM=LSS_FS_CONFIRMATION;
       
   182 				if(d->lss_transfer.LSSNext<3)d->lss_transfer.LSSNext++;
       
   183 			}
       
   184 			else{
       
   185 				d->lss_transfer.BitChecked--;
       
   186 			}
       
   187    			
       
   188    			d->lss_transfer.LSSanswer=0;
       
   189   			StartLSS_FS_TIMER();
       
   190    			sendMasterLSSMessage(d,LSS_IDENT_FASTSCAN,0,0);
       
   191    			return;
       
   192    		}
       
   193 		break;
       
   194 		case LSS_FS_CONFIRMATION:
       
   195 		{
       
   196 			if(d->lss_transfer.LSSanswer!=0){
       
   197 				d->lss_transfer.LSSanswer=0;
       
   198 				
       
   199 				if(d->lss_transfer.LSSSub==3){
       
   200 					/* The LSS FastScan protocol finished correctly. Restore the parameters */
       
   201 					d->lss_transfer.BitChecked=128;
       
   202 					d->lss_transfer.FastScan_SM=LSS_FS_RESET;
       
   203 					d->lss_transfer.LSSSub=0;
       
   204 					d->lss_transfer.LSSNext=0;
       
   205    					d->lss_transfer.IDNumber=0;
       
   206 					
       
   207 					/* Inform the application that the FastScan finished correctly */
       
   208 					d->lss_transfer.state = LSS_FINISHED;
       
   209 					d->lss_transfer.dat1=0;
       
   210 				}
       
   211 				else{
       
   212 					/* Start with the next LSS-ID[sub] */
       
   213 					d->lss_transfer.LSSSub++;
       
   214 					d->lss_transfer.BitChecked=31;
       
   215    					d->lss_transfer.IDNumber=0;
       
   216    					d->lss_transfer.FastScan_SM=LSS_FS_PROCESSING;
       
   217    					StartLSS_FS_TIMER();
       
   218    					sendMasterLSSMessage(d,LSS_IDENT_FASTSCAN,0,0);
       
   219    					return;
       
   220 				}
       
   221 			}
       
   222 			else{
       
   223 				/* This should not happen, an error ocurred*/
       
   224 				MSG_ERR(0x1D0B, "LSS FastScan timeout. FastScan response not received.", 0);
       
   225 				/* Set aborted state */
       
   226     			d->lss_transfer.state = LSS_ABORTED_INTERNAL;
       
   227 			}
       
   228 		}
       
   229 		break;
       
   230    		}
       
   231 
       
   232     	/* Call the user function to inform of the problem.*/
       
   233     	if(d->lss_transfer.Callback){
       
   234 	    	/*If there is a callback, it is responsible of the error*/
       
   235     		(*d->lss_transfer.Callback)(d,d->lss_transfer.command);
       
   236     	}
       
   237     break;
       
   238 #endif
       
   239 	}
       
   240 }
       
   241 
       
   242 /*!                                                                                                
   274 /*!                                                                                                
   243 **                                                                                                 
   275 **                                                                                                 
   244 **                                                                                                 
   276 **                                                                                                 
   245 ** @param d                                                                                        
   277 ** @param d                                                                                        
   246 **/ 
   278 **/ 
   271 {
   303 {
   272   Message m;
   304   Message m;
   273   UNS8 i;
   305   UNS8 i;
   274   
   306   
   275   if (!d->CurrentCommunicationState.csLSS){
   307   if (!d->CurrentCommunicationState.csLSS){
   276   	MSG_WAR(0x2D0C, "unable to send the LSS message, not in the proper state =>", d->nodeState);
   308   	MSG_WAR(0x2D17, "unable to send the LSS message, not in the proper state =>", d->nodeState);
   277   	return 0xFF;
   309   	return 0xFF;
   278   }
   310   }
   279    
   311    
   280   for(i=1;i<8;i++)m.data[i]=0;
   312   for(i=1;i<8;i++)m.data[i]=0;
   281   m.len = 8;
   313   m.len = 8;
   306   case LSS_SM_SELECTIVE_RESP: /* Switch Mode Selective response*/
   338   case LSS_SM_SELECTIVE_RESP: /* Switch Mode Selective response*/
   307   case LSS_IDENT_SLAVE: /* LSS Identify Slave */
   339   case LSS_IDENT_SLAVE: /* LSS Identify Slave */
   308   case LSS_IDENT_NON_CONF_SLAVE: /* LSS identify non-configured remote slave */
   340   case LSS_IDENT_NON_CONF_SLAVE: /* LSS identify non-configured remote slave */
   309   	break;
   341   	break;
   310   default:
   342   default:
   311   	MSG_ERR(0x1D0D, "send Slave LSS command not implemented", command);
   343   	MSG_ERR(0x1D18, "send Slave LSS command not implemented", command);
   312   	return 0xFF;
   344   	return 0xFF;
   313   }
   345   }
   314   
   346   
   315   return canSend(d->canHandle,&m);
   347   return canSend(d->canHandle,&m);
   316 }
   348 }
   325 **/  
   357 **/  
   326 UNS8 sendMasterLSSMessage(CO_Data* d, UNS8 command,void *dat1,void *dat2)
   358 UNS8 sendMasterLSSMessage(CO_Data* d, UNS8 command,void *dat1,void *dat2)
   327 {
   359 {
   328   Message m;
   360   Message m;
   329   UNS8 i;
   361   UNS8 i;
   330   
   362   UNS8 res;
       
   363   UNS8 hasResponse=0;
       
   364    
   331   for(i=1;i<8;i++)m.data[i]=0;
   365   for(i=1;i<8;i++)m.data[i]=0;
   332   m.len = 8;
   366   m.len = 8;
   333   m.rtr = NOT_A_REQUEST;
   367   m.rtr = NOT_A_REQUEST;
   334   m.data[0]=command;
   368   m.data[0]=command;
   335   m.cob_id=UNS16_LE(MLSS_ADRESS);
   369   m.cob_id=UNS16_LE(MLSS_ADRESS);
   336   
   370   
   337   /* Tha data sent with the msg depends on the command */	
   371   /* Tha data sent with the msg depends on the command */	
   338   switch(command){
   372   switch(command){
       
   373   case LSS_CONF_NODE_ID: /* Configure Node-ID */
       
   374   	hasResponse=1;
   339   case LSS_SM_GLOBAL: /* Switch Mode Global */
   375   case LSS_SM_GLOBAL: /* Switch Mode Global */
   340   	d->lss_transfer.state=LSS_FINISHED;
       
   341   case LSS_CONF_NODE_ID: /* Configure Node-ID */
       
   342   	m.data[1]=*(UNS8 *)dat1;
   376   	m.data[1]=*(UNS8 *)dat1;
   343   	break;
   377   	break;
   344   case LSS_CONF_BIT_TIMING: /* Configure Bit Timing Parameters */
   378   case LSS_CONF_BIT_TIMING: /* Configure Bit Timing Parameters */
   345   	m.data[1]=*(UNS8 *)dat1;
   379   	m.data[1]=*(UNS8 *)dat1;
   346   	m.data[2]=*(UNS8 *)dat2;
   380   	m.data[2]=*(UNS8 *)dat2;
       
   381   	if(d->lss_ChangeBaudRate){
       
   382 		/* If a baud rate is not supported just comment the line. */
       
   383 		switch(m.data[2]){
       
   384 			case 0x00:d->lss_transfer.baudRate="1M";break;
       
   385 			case 0x01:d->lss_transfer.baudRate="800K";break;
       
   386 			case 0x02:d->lss_transfer.baudRate="500K";break;
       
   387 			case 0x03:d->lss_transfer.baudRate="250K";break;
       
   388 			case 0x04:d->lss_transfer.baudRate="125K";break;
       
   389 			case 0x05:d->lss_transfer.baudRate="100K";break;
       
   390 			case 0x06:d->lss_transfer.baudRate="50K";break;
       
   391 			case 0x07:d->lss_transfer.baudRate="20K";break;
       
   392 			case 0x08:d->lss_transfer.baudRate="10K";break;
       
   393 			default:
       
   394 				MSG_ERR(0x1D19, "Master-> Baud rate not supported",0);
       
   395 				d->lss_transfer.dat1=0xFF;
       
   396 				goto ErrorBitRateMaster;
       
   397 				break; 
       
   398 			}		
       
   399 		hasResponse=1;
       
   400 		break;
       
   401 	}
       
   402 	else{
       
   403 		MSG_ERR(0x1D1A, "Master-> Bit timing not supported",0);
       
   404 		d->lss_transfer.dat1=0x01;
       
   405 	}
       
   406 	
       
   407 ErrorBitRateMaster:
       
   408 	d->lss_transfer.dat2=0;
       
   409  	/* If there is a callback, it is responsible of the error */
       
   410 	if(d->lss_transfer.Callback)
       
   411     	(*d->lss_transfer.Callback)(d,d->lss_transfer.command);
       
   412 	return 0xFF;
   347   	break;
   413   	break;
   348   case LSS_CONF_ACT_BIT_TIMING: /* Activate Bit Timing Parameters */
   414   case LSS_CONF_ACT_BIT_TIMING: /* Activate Bit Timing Parameters */
   349 	m.data[1]=(UNS8)(*(UNS32*)dat1 & 0xFF);
   415 	m.data[1]=(UNS8)(*(UNS32*)dat1 & 0xFF);
   350 	m.data[2]=(UNS8)(*(UNS32*)dat1>>8 & 0xFF);
   416 	m.data[2]=(UNS8)(*(UNS32*)dat1>>8 & 0xFF);
       
   417 	if(d->lss_transfer.baudRate!="none"){
       
   418 		d->lss_transfer.switchDelay=(UNS16)(*(UNS32*)dat1 & 0xFFFF);
       
   419 		d->lss_transfer.switchDelayState=SDELAY_FIRST;
       
   420 		res=canSend(d->canHandle,&m);
       
   421   		if(res==0){
       
   422   			StartLSS_SDELAY_TIMER();
       
   423   			d->lss_transfer.state=LSS_TRANS_IN_PROGRESS;
       
   424   		}
       
   425   		return res;	
       
   426 	}
       
   427 	else{
       
   428 		MSG_ERR(0x1D1B, "Master-> Baud rate not specified",0);
       
   429 		d->lss_transfer.dat1=1;
       
   430     	/* If there is a callback, it is responsible of the error */
       
   431     	if(d->lss_transfer.Callback){
       
   432 	    	(*d->lss_transfer.Callback)(d,d->lss_transfer.command);
       
   433     	}
       
   434 		return 0xFF;
       
   435 	}
   351   	break;
   436   	break;
       
   437   case LSS_SM_SELECTIVE_SERIAL:
       
   438   case LSS_IDENT_REMOTE_SERIAL_HIGH:
       
   439   	hasResponse=1;
   352   case LSS_SM_SELECTIVE_VENDOR: /* Switch Mode Selective */
   440   case LSS_SM_SELECTIVE_VENDOR: /* Switch Mode Selective */
   353   case LSS_SM_SELECTIVE_PRODUCT:
   441   case LSS_SM_SELECTIVE_PRODUCT:
   354   case LSS_SM_SELECTIVE_REVISION:
   442   case LSS_SM_SELECTIVE_REVISION:
   355   case LSS_SM_SELECTIVE_SERIAL:
       
   356   case LSS_IDENT_REMOTE_VENDOR: /* LSS Identify Remote Slaves */
   443   case LSS_IDENT_REMOTE_VENDOR: /* LSS Identify Remote Slaves */
   357   case LSS_IDENT_REMOTE_PRODUCT:
   444   case LSS_IDENT_REMOTE_PRODUCT:
   358   case LSS_IDENT_REMOTE_REV_LOW:
   445   case LSS_IDENT_REMOTE_REV_LOW:
   359   case LSS_IDENT_REMOTE_REV_HIGH:
   446   case LSS_IDENT_REMOTE_REV_HIGH:
   360   case LSS_IDENT_REMOTE_SERIAL_LOW:
   447   case LSS_IDENT_REMOTE_SERIAL_LOW:
   361   case LSS_IDENT_REMOTE_SERIAL_HIGH:
       
   362 	m.data[1]=(UNS8)(*(UNS32*)dat1 & 0xFF);
   448 	m.data[1]=(UNS8)(*(UNS32*)dat1 & 0xFF);
   363 	m.data[2]=(UNS8)(*(UNS32*)dat1>>8 & 0xFF);
   449 	m.data[2]=(UNS8)(*(UNS32*)dat1>>8 & 0xFF);
   364 	m.data[3]=(UNS8)(*(UNS32*)dat1>>16 & 0xFF);
   450 	m.data[3]=(UNS8)(*(UNS32*)dat1>>16 & 0xFF);
   365 	m.data[4]=(UNS8)(*(UNS32*)dat1>>24 & 0xFF);
   451 	m.data[4]=(UNS8)(*(UNS32*)dat1>>24 & 0xFF);
   366 	break;
   452 	break;
       
   453 	
   367   case LSS_CONF_STORE: /* Store Configured Parameters */
   454   case LSS_CONF_STORE: /* Store Configured Parameters */
   368   case LSS_IDENT_REMOTE_NON_CONF: /* LSS identify non-configured remote slave */
   455   case LSS_IDENT_REMOTE_NON_CONF: /* LSS identify non-configured remote slave */
   369   case LSS_INQ_VENDOR_ID: /* Inquire Identity Vendor-ID */
   456   case LSS_INQ_VENDOR_ID: /* Inquire Identity Vendor-ID */
   370   case LSS_INQ_PRODUCT_CODE: /* Inquire Identity Product-Code */
   457   case LSS_INQ_PRODUCT_CODE: /* Inquire Identity Product-Code */
   371   case LSS_INQ_REV_NUMBER: /* Inquire Identity Revision-Number */
   458   case LSS_INQ_REV_NUMBER: /* Inquire Identity Revision-Number */
   372   case LSS_INQ_SERIAL_NUMBER: /* Inquire Identity Serial-Number */
   459   case LSS_INQ_SERIAL_NUMBER: /* Inquire Identity Serial-Number */
   373   case LSS_INQ_NODE_ID: /* Inquire Node-ID */
   460   case LSS_INQ_NODE_ID: /* Inquire Node-ID */
       
   461 	 hasResponse=1;
   374   	break;
   462   	break;
   375 #ifdef CO_ENABLE_LSS_FS
   463 #ifdef CO_ENABLE_LSS_FS
   376   case LSS_IDENT_FASTSCAN:
   464   case LSS_IDENT_FASTSCAN:
   377 		m.data[1]=(UNS8)(d->lss_transfer.IDNumber & 0xFF);
   465 		m.data[1]=(UNS8)(d->lss_transfer.IDNumber & 0xFF);
   378 		m.data[2]=(UNS8)(d->lss_transfer.IDNumber>>8 & 0xFF);
   466 		m.data[2]=(UNS8)(d->lss_transfer.IDNumber>>8 & 0xFF);
   379 		m.data[3]=(UNS8)(d->lss_transfer.IDNumber>>16 & 0xFF);
   467 		m.data[3]=(UNS8)(d->lss_transfer.IDNumber>>16 & 0xFF);
   380 		m.data[4]=(UNS8)(d->lss_transfer.IDNumber>>24 & 0xFF);
   468 		m.data[4]=(UNS8)(d->lss_transfer.IDNumber>>24 & 0xFF);
   381 		m.data[5]=d->lss_transfer.BitChecked;
   469 		m.data[5]=d->lss_transfer.BitChecked;
   382 		m.data[6]=d->lss_transfer.LSSSub;
   470 		m.data[6]=d->lss_transfer.LSSSub;
   383 		m.data[7]=d->lss_transfer.LSSNext;
   471 		m.data[7]=d->lss_transfer.LSSNext;
       
   472 		/* it will generate a response only if it is the start of the FastScan protocol*/
       
   473 		if(d->lss_transfer.FastScan_SM==LSS_FS_RESET)hasResponse=1;
   384 	break;
   474 	break;
   385 #endif
   475 #endif
   386   default:
   476   default:
   387    	MSG_ERR(0x1D0E, "send Master LSS command not implemented", command);
   477    	MSG_ERR(0x1D1C, "send Master LSS command not implemented", command);
   388   	return 0xFF;
   478   	return 0xFF;
   389   }
   479   }
   390 	
   480 	
   391   return canSend(d->canHandle,&m);
   481   res=canSend(d->canHandle,&m);
       
   482   if(res==0 && hasResponse==1){
       
   483   	StartLSS_MSG_TIMER();
       
   484   	d->lss_transfer.state=LSS_TRANS_IN_PROGRESS;
       
   485   }
       
   486   return res;
   392 }
   487 }
   393 
   488 
   394 /*!                                                                                                
   489 /*!                                                                                                
   395 **                                                                                                 
   490 **                                                                                                 
   396 **                                                                                                 
   491 **                                                                                                 
   433 		//MSG_WAR(0x3D0D, "MasterLSS proceedLSS; unexpected message arrived;command ", m->data[0]);
   528 		//MSG_WAR(0x3D0D, "MasterLSS proceedLSS; unexpected message arrived;command ", m->data[0]);
   434 		//return 0;
   529 		//return 0;
   435 		goto ErrorProcessMaster;
   530 		goto ErrorProcessMaster;
   436 	}
   531 	}
   437 	
   532 	
   438 	//#ifdef CO_ENABLE_LSS_FS
   533  	MSG_WAR(0x3D1E, "MasterLSS proceedLSS; command ", m->data[0]);
   439 	/* The FastScan protocol doesn't stops the timers when a message has been received */
       
   440 	/*if(d->lss_transfer.command!=LSS_IDENT_FASTSCAN)
       
   441 #endif
       
   442 	{
       
   443 		StopLSS_TIMER(LSS_MSG_TIMER);
       
   444     	d->lss_transfer.state = LSS_FINISHED;
       
   445 	}*/
       
   446 	
       
   447  	MSG_WAR(0x3D0F, "MasterLSS proceedLSS; command ", m->data[0]);
       
   448 	
   534 	
   449    	switch(msg_cs=m->data[0]){
   535    	switch(msg_cs=m->data[0]){
   450    		case LSS_INQ_NODE_ID: /* Inquire Node-ID */
   536    		case LSS_INQ_NODE_ID: /* Inquire Node-ID */
   451    			if(d->lss_transfer.command!=LSS_INQ_NODE_ID)goto ErrorProcessMaster;
   537    			if(d->lss_transfer.command!=LSS_INQ_NODE_ID)goto ErrorProcessMaster;
   452    			Dat1=m->data[1];
   538    			Dat1=m->data[1];
   491  			break;
   577  			break;
   492    		case LSS_IDENT_NON_CONF_SLAVE: /* LSS identify non-configured remote slave */
   578    		case LSS_IDENT_NON_CONF_SLAVE: /* LSS identify non-configured remote slave */
   493    			if(d->lss_transfer.command!=LSS_IDENT_REMOTE_NON_CONF)goto ErrorProcessMaster;
   579    			if(d->lss_transfer.command!=LSS_IDENT_REMOTE_NON_CONF)goto ErrorProcessMaster;
   494    			break;
   580    			break;
   495    		default:
   581    		default:
   496    			MSG_ERR(0x1D10, "Master LSS command not implemented", msg_cs);
   582    			MSG_ERR(0x1D1F, "Master LSS command not implemented", msg_cs);
   497   			return 0xFF;
   583   			return 0xFF;
   498    	}
   584    	}
   499 	
   585 	
   500 	StopLSS_TIMER(LSS_MSG_TIMER);
   586 	StopLSS_MSG_TIMER();
   501     d->lss_transfer.state = LSS_FINISHED;
   587     d->lss_transfer.state = LSS_FINISHED;
   502     	
   588     	
   503 	d->lss_transfer.dat1=Dat1;
   589 	d->lss_transfer.dat1=Dat1;
   504 	d->lss_transfer.dat2=Dat2;
   590 	d->lss_transfer.dat2=Dat2;
   505  	/* If there is a callback, it is responsible of the received response */
   591  	/* If there is a callback, it is responsible of the received response */
   507     	(*d->lss_transfer.Callback)(d,d->lss_transfer.command);
   593     	(*d->lss_transfer.Callback)(d,d->lss_transfer.command);
   508     			
   594     			
   509    return 0;
   595    return 0;
   510    
   596    
   511 ErrorProcessMaster:
   597 ErrorProcessMaster:
   512     MSG_WAR(0x3D11, "MasterLSS proceedLSS; unexpected message arrived;command ", m->data[0]);
   598     MSG_WAR(0x3D20, "MasterLSS proceedLSS; unexpected message arrived;command ", m->data[0]);
   513 	return 0xFF;
   599 	return 0xFF;
   514 		
   600 		
   515 }
   601 }
   516 
   602 
   517 /*!                                                                                                
   603 /*!                                                                                                
   524 **/ 
   610 **/ 
   525 UNS8 proceedLSS_Slave(CO_Data* d, Message* m )
   611 UNS8 proceedLSS_Slave(CO_Data* d, Message* m )
   526 {  
   612 {  
   527 	UNS8 msg_cs;
   613 	UNS8 msg_cs;
   528 	
   614 	
   529   	MSG_WAR(0x3D12, "SlaveLSS proceedLSS; command ", m->data[0]);
   615   	MSG_WAR(0x3D21, "SlaveLSS proceedLSS; command ", m->data[0]);
   530   	
   616   	
   531    	switch(msg_cs=m->data[0]){
   617    	switch(msg_cs=m->data[0]){
   532    	case LSS_SM_GLOBAL:		/* Switch Mode Global */
   618    	case LSS_SM_GLOBAL:		/* Switch Mode Global */
   533    		/* if there is not a mode change break*/
   619    		/* if there is not a mode change break*/
   534    		if(m->data[1] == d->lss_transfer.mode){
   620    		if(m->data[1] == d->lss_transfer.mode){
   535    			MSG_WAR(0x3D13, "SlaveLSS already in the mode ", m->data[1]);
   621    			MSG_WAR(0x3D22, "SlaveLSS already in the mode ", m->data[1]);
   536    			break;
   622    			break;
   537    		}
   623    		}
   538    		
   624    		
   539 		if(m->data[1]==LSS_CONFIGURATION_MODE)	{
   625 		if(m->data[1]==LSS_CONFIGURATION_MODE)	{
   540 			MSG_WAR(0x3D14, "SlaveLSS switching to configuration mode ", 0);
   626 			MSG_WAR(0x3D23, "SlaveLSS switching to configuration mode ", 0);
   541 			/* Store the NodeId in case it will be changed */
   627 			/* Store the NodeId in case it will be changed */
   542 			d->lss_transfer.nodeID=getNodeId(d);
   628 			//d->lss_transfer.nodeID=getNodeId(d);
   543 			d->lss_transfer.mode=LSS_CONFIGURATION_MODE;
   629 			d->lss_transfer.mode=LSS_CONFIGURATION_MODE;
   544 		}
   630 		}
   545 		else if(m->data[1]==LSS_WAITING_MODE){
   631 		else if(m->data[1]==LSS_WAITING_MODE){
   546 			MSG_WAR(0x3D15, "SlaveLSS switching to operational mode ", 0);
   632 			MSG_WAR(0x3D24, "SlaveLSS switching to operational mode ", 0);
   547 			
   633 			
   548 			if(d->lss_transfer.switchDelayState==SDELAY_OFF){
   634 			/* If the nodeID has changed update it and put the node state to Initialisation. */
   549 				/* If the nodeID has changed update it and put the node state to Initialisation. */
   635 			if(d->lss_transfer.nodeID!=getNodeId(d)){
   550 				if(d->lss_transfer.nodeID!=getNodeId(d)){
   636 				if(getNodeId(d)==0xFF){/* The nodeID was 0xFF; initialize the application*/
   551 					MSG_WAR(0x3D16, "The node Id has changed. Reseting to Initialisation state",0);
   637 					MSG_WAR(0x3D25, "The node Id has changed. Reseting to Initialisation state",0);
   552 					setNodeId(d, d->lss_transfer.nodeID);
   638 					setNodeId(d, d->lss_transfer.nodeID);
   553 					setState(d, Initialisation);
   639 					setState(d, Initialisation);
       
   640 				}
       
   641 				else{/* The nodeID will be changed on NMT_Reset Request*/
       
   642 					setNodeId(d, d->lss_transfer.nodeID);
   554 				}
   643 				}
   555 			}
   644 			}
   556 			d->lss_transfer.mode=LSS_WAITING_MODE;
   645 			d->lss_transfer.mode=LSS_WAITING_MODE;
   557 		}
   646 		}
   558 	break;
   647 	break;
   561 		UNS8 error_code=0;
   650 		UNS8 error_code=0;
   562 		UNS8 spec_error=0;
   651 		UNS8 spec_error=0;
   563 			
   652 			
   564 		if(d->lss_transfer.mode==LSS_CONFIGURATION_MODE){
   653 		if(d->lss_transfer.mode==LSS_CONFIGURATION_MODE){
   565 			if(m->data[1]>127 && m->data[1]!=0xFF){
   654 			if(m->data[1]>127 && m->data[1]!=0xFF){
   566 				MSG_ERR(0x1D17, "NodeID out of range",0);
   655 				MSG_ERR(0x1D26, "NodeID out of range",0);
   567 				error_code=1; /* NodeID out of range */
   656 				error_code=1; /* NodeID out of range */
   568 			}
   657 			}
   569 			else{
   658 			else{
   570 				d->lss_transfer.nodeID=m->data[1];
   659 				d->lss_transfer.nodeID=m->data[1];
   571 			}
   660 			}
   572 		}
   661 		}
   573 		else{
   662 		else{
   574 			MSG_WAR(0x3D18, "SlaveLSS not in configuration mode",0);
   663 			MSG_WAR(0x3D27, "SlaveLSS not in configuration mode",0);
   575 			//error_code=0xFF;
   664 			//error_code=0xFF;
   576 			break;
   665 			break;
   577 		}
   666 		}
   578 		sendSlaveLSSMessage(d,msg_cs,&error_code,&spec_error);
   667 		sendSlaveLSSMessage(d,msg_cs,&error_code,&spec_error);
   579 	}	
   668 	}	
   598 				case 0x05:d->lss_transfer.baudRate="100K";break;
   687 				case 0x05:d->lss_transfer.baudRate="100K";break;
   599 				case 0x06:d->lss_transfer.baudRate="50K";break;
   688 				case 0x06:d->lss_transfer.baudRate="50K";break;
   600 				case 0x07:d->lss_transfer.baudRate="20K";break;
   689 				case 0x07:d->lss_transfer.baudRate="20K";break;
   601 				case 0x08:d->lss_transfer.baudRate="10K";break;
   690 				case 0x08:d->lss_transfer.baudRate="10K";break;
   602 				default:
   691 				default:
   603 					MSG_ERR(0x1D19, "Baud rate not supported",0);
   692 					MSG_ERR(0x1D28, "Baud rate not supported",0);
   604 					error_code=0xFF; /* Baud rate not supported*/
   693 					error_code=0xFF; /* Baud rate not supported*/
   605 					break; 
   694 					break; 
   606 				}		
   695 				}		
   607 			}
   696 			}
   608 			else
   697 			else
   609 			{
   698 			{
   610 				MSG_ERR(0x1D1A, "Bit timing not supported",0);
   699 				MSG_ERR(0x1D29, "Bit timing not supported",0);
   611 				error_code=0x01; /* bit timing not supported */
   700 				error_code=0x01; /* bit timing not supported */
   612 			}
   701 			}
   613 		}
   702 		}
   614 		else{
   703 		else{
   615 			MSG_WAR(0x3D1B, "SlaveLSS not in configuration mode",0);
   704 			MSG_WAR(0x3D2A, "SlaveLSS not in configuration mode",0);
   616 			//error_code=0xFF;
   705 			//error_code=0xFF;
   617 			break;
   706 			break;
   618 		}
   707 		}
   619 		
   708 		
   620 		sendSlaveLSSMessage(d,msg_cs,&error_code,&spec_error);
   709 		sendSlaveLSSMessage(d,msg_cs,&error_code,&spec_error);
   621 	}
   710 	}
   622 	break;
   711 	break;
   623 	case LSS_CONF_ACT_BIT_TIMING: /* Activate Bit Timing Parameters */
   712 	case LSS_CONF_ACT_BIT_TIMING: /* Activate Bit Timing Parameters */
   624 		
   713 		
   625 		if(d->lss_transfer.mode!=LSS_CONFIGURATION_MODE){
   714 		if(d->lss_transfer.mode!=LSS_CONFIGURATION_MODE){
   626 			MSG_ERR(0x3D1C, "SlaveLSS not in configuration mode",0);
   715 			MSG_ERR(0x3D2B, "SlaveLSS not in configuration mode",0);
   627 			break;
   716 			break;
   628 		}
   717 		}
   629 		
   718 		
   630 		if(d->lss_transfer.baudRate!="none"){
   719 		if(d->lss_transfer.baudRate!="none"){
   631 			d->lss_transfer.switchDelay=getLSSDelay(m);
   720 			d->lss_transfer.switchDelay=getLSSDelay(m);
   632 			MSG_WAR(0x3D1D, "Slave Switch Delay set to: ",d->lss_transfer.switchDelay);
   721 			MSG_WAR(0x3D2C, "Slave Switch Delay set to: ",d->lss_transfer.switchDelay);
   633 			d->lss_transfer.switchDelayState=SDELAY_FIRST;
   722 			d->lss_transfer.switchDelayState=SDELAY_FIRST;
   634 			d->lss_transfer.currentState=getState(d);
   723 			//d->lss_transfer.currentState=getState(d);
   635 			setState(d, LssTimingDelay);
   724 			//setState(d, LssTimingDelay);
       
   725 			d->lss_transfer.canHandle_t=d->canHandle;
       
   726 			d->canHandle=NULL;
   636 			StartLSS_SDELAY_TIMER();
   727 			StartLSS_SDELAY_TIMER();
   637 		}
   728 		}
   638 	break;
   729 	break;
   639 	case LSS_CONF_STORE: /* Store Configured Parameters */
   730 	case LSS_CONF_STORE: /* Store Configured Parameters */
   640 	{
   731 	{
   642 		UNS8 spec_error=0;
   733 		UNS8 spec_error=0;
   643 		
   734 		
   644 		if(d->lss_transfer.mode==LSS_CONFIGURATION_MODE){ 
   735 		if(d->lss_transfer.mode==LSS_CONFIGURATION_MODE){ 
   645 			if(d->lss_StoreConfiguration){
   736 			if(d->lss_StoreConfiguration){
   646 				 /* call lss_StoreConfiguration with NodeId */
   737 				 /* call lss_StoreConfiguration with NodeId */
   647 	  			(*d->lss_StoreConfiguration)(&error_code,&spec_error);
   738 	  			(*d->lss_StoreConfiguration)(d,&error_code,&spec_error);
   648 			}
   739 			}
   649 			else{
   740 			else{
   650 				MSG_ERR(0x1D1E, "Store configuration not supported",0);
   741 				MSG_ERR(0x1D2E, "Store configuration not supported",0);
   651 				error_code=1; /* store configuration is not supported */
   742 				error_code=1; /* store configuration is not supported */
   652 			}	
   743 			}	
   653 		}
   744 		}
   654 		else{
   745 		else{
   655 			MSG_WAR(0x3D1F, "SlaveLSS not in configuration mode",0);
   746 			MSG_WAR(0x3D2F, "SlaveLSS not in configuration mode",0);
   656 			//error_code=0xFF;
   747 			//error_code=0xFF;
   657 			break;
   748 			break;
   658 		}
   749 		}
   659 		sendSlaveLSSMessage(d,msg_cs,&error_code,&spec_error);
   750 		sendSlaveLSSMessage(d,msg_cs,&error_code,&spec_error);
   660 	}
   751 	}
   669   		ODCallback_t *Callback;
   760   		ODCallback_t *Callback;
   670   		UNS32 _SpecificNodeInfo;
   761   		UNS32 _SpecificNodeInfo;
   671   
   762   
   672   		if(d->lss_transfer.mode==LSS_CONFIGURATION_MODE)
   763   		if(d->lss_transfer.mode==LSS_CONFIGURATION_MODE)
   673   		{
   764   		{
   674   			MSG_ERR(0x1D20, "Switch Mode Selective only supported in operational mode",0);
   765   			MSG_ERR(0x1D30, "Switch Mode Selective only supported in operational mode",0);
   675   			break;
   766   			break;
   676   		}
   767   		}
   677   			
   768   			
   678 		_SpecificNodeInfo=getLSSIdent(m);
   769 		_SpecificNodeInfo=getLSSIdent(m);
   679 				
   770 				
   682 			
   773 			
   683 			d->lss_transfer.addr_sel_match|=(0x01<<(msg_cs-LSS_SM_SELECTIVE_VENDOR));
   774 			d->lss_transfer.addr_sel_match|=(0x01<<(msg_cs-LSS_SM_SELECTIVE_VENDOR));
   684 			/* If all the fields has been set */
   775 			/* If all the fields has been set */
   685 			if(d->lss_transfer.addr_sel_match==0x0F){
   776 			if(d->lss_transfer.addr_sel_match==0x0F){
   686 				
   777 				
   687 				MSG_WAR(0x3D21, "SlaveLSS switching to configuration mode ", 0);
   778 				MSG_WAR(0x3D31, "SlaveLSS switching to configuration mode ", 0);
   688 				d->lss_transfer.addr_sel_match=0;
   779 				d->lss_transfer.addr_sel_match=0;
   689 				d->lss_transfer.nodeID=getNodeId(d);
   780 				d->lss_transfer.nodeID=getNodeId(d);
   690 				d->lss_transfer.mode=LSS_CONFIGURATION_MODE;
   781 				d->lss_transfer.mode=LSS_CONFIGURATION_MODE;
   691 
   782 
   692 				sendSlaveLSSMessage(d,LSS_SM_SELECTIVE_RESP,0,0);
   783 				sendSlaveLSSMessage(d,LSS_SM_SELECTIVE_RESP,0,0);
   693 			}
   784 			}
   694 		}	
   785 		}	
   695 		else {
   786 		else {
   696 			MSG_WAR(0x3D22, "LSS identity field doesn't match ", _SpecificNodeInfo);
   787 			MSG_WAR(0x3D32, "LSS identity field doesn't match ", _SpecificNodeInfo);
   697 			d->lss_transfer.addr_sel_match=0;
   788 			d->lss_transfer.addr_sel_match=0;
   698 		}	
   789 		}	
   699 	}	
   790 	}	
   700 	break;
   791 	break;
   701 	case LSS_IDENT_REMOTE_VENDOR: /* LSS Identify Remote Slaves */
   792 	case LSS_IDENT_REMOTE_VENDOR: /* LSS Identify Remote Slaves */
   723 		case LSS_IDENT_REMOTE_SERIAL_LOW:d->lss_transfer.addr_ident_match=(_SpecificNodeInfo <= *(UNS32*)ptrTable->pSubindex[4].pObject)? d->lss_transfer.addr_ident_match|0x10:0;	break;
   814 		case LSS_IDENT_REMOTE_SERIAL_LOW:d->lss_transfer.addr_ident_match=(_SpecificNodeInfo <= *(UNS32*)ptrTable->pSubindex[4].pObject)? d->lss_transfer.addr_ident_match|0x10:0;	break;
   724 		case LSS_IDENT_REMOTE_SERIAL_HIGH:d->lss_transfer.addr_ident_match=(_SpecificNodeInfo >= *(UNS32*)ptrTable->pSubindex[4].pObject)? d->lss_transfer.addr_ident_match|0x20:0;	break;
   815 		case LSS_IDENT_REMOTE_SERIAL_HIGH:d->lss_transfer.addr_ident_match=(_SpecificNodeInfo >= *(UNS32*)ptrTable->pSubindex[4].pObject)? d->lss_transfer.addr_ident_match|0x20:0;	break;
   725 		}
   816 		}
   726 		/* If all the fields has been set.. */
   817 		/* If all the fields has been set.. */
   727 		if(d->lss_transfer.addr_ident_match==0x3F){
   818 		if(d->lss_transfer.addr_ident_match==0x3F){
   728 			MSG_WAR(0x3D23, "SlaveLSS identified ", 0);
   819 			MSG_WAR(0x3D33, "SlaveLSS identified ", 0);
   729 			d->lss_transfer.addr_ident_match=0;
   820 			d->lss_transfer.addr_ident_match=0;
   730 			sendSlaveLSSMessage(d,LSS_IDENT_SLAVE,0,0);
   821 			sendSlaveLSSMessage(d,LSS_IDENT_SLAVE,0,0);
   731 		}
   822 		}
   732 		else if(d->lss_transfer.addr_ident_match==0){
   823 		else if(d->lss_transfer.addr_ident_match==0){
   733 			MSG_WAR(0x3D24, "LSS identify field doesn't match ", _SpecificNodeInfo);
   824 			MSG_WAR(0x3D34, "LSS identify field doesn't match ", _SpecificNodeInfo);
   734 		}
   825 		}
   735 	}
   826 	}
   736 	break;
   827 	break;
   737 	case LSS_IDENT_REMOTE_NON_CONF: /* LSS identify non-configured remote slave */
   828 	case LSS_IDENT_REMOTE_NON_CONF: /* LSS identify non-configured remote slave */
   738 	{
   829 	{
   739 		if(getNodeId(d)==0xFF){		
   830 		if(getNodeId(d)==0xFF){		
   740 			MSG_WAR(0x3D25, "SlaveLSS non-configured ", 0);
   831 			MSG_WAR(0x3D35, "SlaveLSS non-configured ", 0);
   741 			sendSlaveLSSMessage(d,LSS_IDENT_NON_CONF_SLAVE,0,0);
   832 			sendSlaveLSSMessage(d,LSS_IDENT_NON_CONF_SLAVE,0,0);
   742 		}
   833 		}
   743 		else{
   834 		else{
   744 			MSG_WAR(0x3D26, "SlaveLSS already configured ", 0);
   835 			MSG_WAR(0x3D36, "SlaveLSS already configured ", 0);
   745 		}
   836 		}
   746 	}
   837 	}
   747 	break;
   838 	break;
   748 	case LSS_INQ_VENDOR_ID: /* Inquire Identity Vendor-ID */
   839 	case LSS_INQ_VENDOR_ID: /* Inquire Identity Vendor-ID */
   749 	case LSS_INQ_PRODUCT_CODE: /* Inquire Identity Product-Code */
   840 	case LSS_INQ_PRODUCT_CODE: /* Inquire Identity Product-Code */
   757   		ODCallback_t *Callback;
   848   		ODCallback_t *Callback;
   758   		UNS32 _SpecificNodeInfo;
   849   		UNS32 _SpecificNodeInfo;
   759   
   850   
   760   		ptrTable = (*d->scanIndexOD)(0x1018, &errorCode, &Callback);
   851   		ptrTable = (*d->scanIndexOD)(0x1018, &errorCode, &Callback);
   761   		_SpecificNodeInfo=*(UNS32*)ptrTable->pSubindex[msg_cs-(LSS_INQ_VENDOR_ID-1)].pObject;
   852   		_SpecificNodeInfo=*(UNS32*)ptrTable->pSubindex[msg_cs-(LSS_INQ_VENDOR_ID-1)].pObject;
   762   		MSG_WAR(0x3D27, "SlaveLSS identity field inquired ", _SpecificNodeInfo);
   853   		MSG_WAR(0x3D37, "SlaveLSS identity field inquired ", _SpecificNodeInfo);
   763 			
   854 			
   764 		sendSlaveLSSMessage(d,msg_cs,&_SpecificNodeInfo,0);
   855 		sendSlaveLSSMessage(d,msg_cs,&_SpecificNodeInfo,0);
   765 	}
   856 	}
   766 	break;
   857 	break;
   767 	case LSS_INQ_NODE_ID: /* Inquire Node-ID */
   858 	case LSS_INQ_NODE_ID: /* Inquire Node-ID */
   768 		if(d->lss_transfer.mode==LSS_CONFIGURATION_MODE)
   859 		if(d->lss_transfer.mode==LSS_CONFIGURATION_MODE)
   769 		{
   860 		{
   770 			UNS8 NodeID;
   861 			UNS8 NodeID;
   771 	
   862 	
   772 			NodeID=getNodeId(d);
   863 			NodeID=getNodeId(d);
   773 			MSG_WAR(0x3D28, "SlaveLSS Node ID inquired ", NodeID);
   864 			MSG_WAR(0x3D38, "SlaveLSS Node ID inquired ", NodeID);
   774 			sendSlaveLSSMessage(d,msg_cs,&NodeID,0);
   865 			sendSlaveLSSMessage(d,msg_cs,&NodeID,0);
   775 		}
   866 		}
   776 		else{
   867 		else{
   777 			MSG_WAR(0x3D29, "SlaveLSS not in configuration mode",0);
   868 			MSG_WAR(0x3D39, "SlaveLSS not in configuration mode",0);
   778 		}
   869 		}
   779 	break;
   870 	break;
   780 #ifdef CO_ENABLE_LSS_FS
   871 #ifdef CO_ENABLE_LSS_FS
   781 	case LSS_IDENT_FASTSCAN:
   872 	case LSS_IDENT_FASTSCAN:
   782 	{
   873 	{
   789    		{
   880    		{
   790    			UNS32 errorCode;
   881    			UNS32 errorCode;
   791   			const indextable *ptrTable;
   882   			const indextable *ptrTable;
   792   			ODCallback_t *Callback;
   883   			ODCallback_t *Callback;
   793   				
   884   				
   794 			MSG_WAR(0x3D2A, "SlaveLSS Reseting LSSPos", 0);
   885 			MSG_WAR(0x3D3A, "SlaveLSS Reseting LSSPos", 0);
   795 			d->lss_transfer.LSSPos=0;
   886 			d->lss_transfer.LSSPos=0;
   796 			d->lss_transfer.FastScan_SM=LSS_FS_PROCESSING;
   887 			d->lss_transfer.FastScan_SM=LSS_FS_PROCESSING;
   797 			
   888 			
   798   			ptrTable = (*d->scanIndexOD)(0x1018, &errorCode, &Callback);
   889   			ptrTable = (*d->scanIndexOD)(0x1018, &errorCode, &Callback);
   799   			d->lss_transfer.IDNumber=*(UNS32*)ptrTable->pSubindex[d->lss_transfer.LSSPos+1].pObject;
   890   			d->lss_transfer.IDNumber=*(UNS32*)ptrTable->pSubindex[d->lss_transfer.LSSPos+1].pObject;
   804 		case LSS_FS_PROCESSING:/*if(getLSSBitCheck(m)<32)*/
   895 		case LSS_FS_PROCESSING:/*if(getLSSBitCheck(m)<32)*/
   805 			if(d->lss_transfer.LSSPos==getLSSSub(m))
   896 			if(d->lss_transfer.LSSPos==getLSSSub(m))
   806 			{
   897 			{
   807 				UNS32 Mask=0xFFFFFFFF<<getLSSBitCheck(m);
   898 				UNS32 Mask=0xFFFFFFFF<<getLSSBitCheck(m);
   808 				
   899 				
   809 				MSG_WAR(0x3D2B, "SlaveLSS FastScan IDNumber", getLSSIdent(m));
   900 				MSG_WAR(0x3D3B, "SlaveLSS FastScan IDNumber", getLSSIdent(m));
   810 				MSG_WAR(0x3D2C, "SlaveLSS FastScan BitMask ", Mask);
   901 				MSG_WAR(0x3D3C, "SlaveLSS FastScan BitMask ", Mask);
   811 				MSG_WAR(0x3D2D, "SlaveLSS FastScan LSS-ID  ", d->lss_transfer.IDNumber);
   902 				MSG_WAR(0x3D3D, "SlaveLSS FastScan LSS-ID  ", d->lss_transfer.IDNumber);
   812 				
   903 				
   813 				if((getLSSIdent(m) & Mask)==(d->lss_transfer.IDNumber & Mask))
   904 				if((getLSSIdent(m) & Mask)==(d->lss_transfer.IDNumber & Mask))
   814 				{
   905 				{
   815 					sendSlaveLSSMessage(d,LSS_IDENT_SLAVE,0,0);
   906 					sendSlaveLSSMessage(d,LSS_IDENT_SLAVE,0,0);
   816 				}
   907 				}
   825 			if(d->lss_transfer.LSSPos==getLSSSub(m))
   916 			if(d->lss_transfer.LSSPos==getLSSSub(m))
   826 			{
   917 			{
   827 				if(getLSSIdent(m)==d->lss_transfer.IDNumber)
   918 				if(getLSSIdent(m)==d->lss_transfer.IDNumber)
   828 				{
   919 				{
   829 					/* Current LSS-ID[sub] confirmed correctly */
   920 					/* Current LSS-ID[sub] confirmed correctly */
   830 					MSG_WAR(0x3D2E, "SlaveLSS FastScan IDNumber and LSS-ID match=>", d->lss_transfer.IDNumber);
   921 					MSG_WAR(0x3D3E, "SlaveLSS FastScan IDNumber and LSS-ID match=>", d->lss_transfer.IDNumber);
   831 					if(d->lss_transfer.LSSPos==3)
   922 					if(d->lss_transfer.LSSPos==3)
   832 					{
   923 					{
   833 						/* All LSS-ID[sub] identified correctly, switching to configuration mode */
   924 						/* All LSS-ID[sub] identified correctly, switching to configuration mode */
   834 						MSG_WAR(0x3D2F, "SlaveLSS switching to configuration mode ", 0);
   925 						MSG_WAR(0x3D3F, "SlaveLSS switching to configuration mode ", 0);
   835 			   			d->lss_transfer.nodeID=getNodeId(d);
   926 			   			d->lss_transfer.nodeID=getNodeId(d);
   836 			   			d->lss_transfer.mode=LSS_CONFIGURATION_MODE;
   927 			   			d->lss_transfer.mode=LSS_CONFIGURATION_MODE;
   837 			    		d->lss_transfer.FastScan_SM=LSS_FS_RESET;
   928 			    		d->lss_transfer.FastScan_SM=LSS_FS_RESET;
   838 			    		d->lss_transfer.LSSPos=0xFF;
   929 			    		d->lss_transfer.LSSPos=0xFF;
   839 					}		
   930 					}		
   856 		}
   947 		}
   857 	}	
   948 	}	
   858 	break;
   949 	break;
   859 #endif
   950 #endif
   860    	default:
   951    	default:
   861    		MSG_ERR(0x1D30, "SlaveLSS command not implemented", msg_cs);
   952    		MSG_ERR(0x1D40, "SlaveLSS command not implemented", msg_cs);
   862   		return 0xFF;
   953   		return 0xFF;
   863    	}
   954    	}
   864    
   955    
   865     return 0;
   956     return 0;
   866 }
   957 }
   867 
   958 
   868 UNS8 configNetworkNode(CO_Data* d, UNS8 command, void *dat1, void* dat2)
   959 /*UNS8 configNetworkNode(CO_Data* d, UNS8 command, void *dat1, void* dat2)
   869 {
   960 {
   870 	return sendMasterLSSMessage(d,command,dat1,dat2);
   961 	return sendMasterLSSMessage(d,command,dat1,dat2);
   871 }
   962 }*/
   872 
   963 
   873 UNS8 configNetworkNodeCallBack (CO_Data* d, UNS8 command, void *dat1, void* dat2, LSSCallback_t Callback)
   964 UNS8 configNetworkNode (CO_Data* d, UNS8 command, void *dat1, void* dat2, LSSCallback_t Callback)
   874 {
   965 {
   875 	d->lss_transfer.state=LSS_TRANS_IN_PROGRESS;
   966 	//d->lss_transfer.state=LSS_TRANS_IN_PROGRESS;
   876 	d->lss_transfer.Callback=Callback;
   967 	d->lss_transfer.Callback=Callback;
   877 	d->lss_transfer.command=command;
   968 	d->lss_transfer.command=command;
   878 	
   969 	
   879 	StopLSS_TIMER(LSS_MSG_TIMER);
   970 	StopLSS_MSG_TIMER();
   880   	StartLSS_MSG_TIMER();
   971   	//StartLSS_MSG_TIMER();
   881   	
   972   	
   882 	return sendMasterLSSMessage(d,command,dat1,dat2);
   973 	return sendMasterLSSMessage(d,command,dat1,dat2);
   883 }
   974 }
   884 
   975 
   885 UNS8 getConfigResultNetworkNode (CO_Data* d, UNS8 command, UNS32* dat1, UNS8* dat2)
   976 UNS8 getConfigResultNetworkNode (CO_Data* d, UNS8 command, UNS32* dat1, UNS8* dat2)