src/lss.c
changeset 384 83793fc7ce48
parent 381 854c43cdc24a
child 399 8d22c323fe0f
equal deleted inserted replaced
383:7d8afedb8769 384:83793fc7ce48
   160 	/* The first switch_delay period expired. Store the node state, change it 
   160 	/* The first switch_delay period expired. Store the node state, change it 
   161  	 * so no CAN messages will be sent or received, call the ChangeBaudRate function*/
   161  	 * so no CAN messages will be sent or received, call the ChangeBaudRate function*/
   162    	if(d->lss_transfer.switchDelayState==SDELAY_FIRST){
   162    	if(d->lss_transfer.switchDelayState==SDELAY_FIRST){
   163    		MSG_WAR(0x3D0B, "LSS switch delay first period expired",0);
   163    		MSG_WAR(0x3D0B, "LSS switch delay first period expired",0);
   164     	d->lss_transfer.switchDelayState=SDELAY_SECOND;
   164     	d->lss_transfer.switchDelayState=SDELAY_SECOND;
   165     	(*d->lss_ChangeBaudRate)(d,d->lss_transfer.baudRate);
   165     	//(*d->lss_ChangeBaudRate)(d,d->lss_transfer.baudRate);
       
   166     	canChangeBaudRate(d->lss_transfer.canHandle_t, d->lss_transfer.baudRate);
   166     }
   167     }
   167     else{ /* d->lss_transfer.switchDelayState==SDELAY_SECOND */
   168     else{ /* d->lss_transfer.switchDelayState==SDELAY_SECOND */
   168     	MSG_WAR(0x3D0C, "LSS switch delay second period expired",0);
   169     	MSG_WAR(0x3D0C, "LSS switch delay second period expired",0);
   169     	d->lss_transfer.switchDelayState=SDELAY_OFF;
   170     	d->lss_transfer.switchDelayState=SDELAY_OFF;
   170     	StopLSS_SDELAY_TIMER();
   171     	StopLSS_SDELAY_TIMER();
   344   	return 0xFF;
   345   	return 0xFF;
   345   }
   346   }
   346   
   347   
   347   return canSend(d->canHandle,&m);
   348   return canSend(d->canHandle,&m);
   348 }
   349 }
       
   350 			
       
   351 /* If a baud rate is not supported just comment the line. */
       
   352 static UNS8 CO_TranslateBaudRate(char* optarg){
       
   353 	if(!strcmp( optarg, "1M")) return 0x00;
       
   354 	if(!strcmp( optarg, "800K")) return 0x01;
       
   355 	if(!strcmp( optarg, "500K")) return 0x02;
       
   356 	if(!strcmp( optarg, "250K")) return 0x03;
       
   357 	if(!strcmp( optarg, "125K")) return 0x04;
       
   358 	if(!strcmp( optarg, "100K")) return 0x05;
       
   359 	if(!strcmp( optarg, "50K")) return 0x06;
       
   360 	if(!strcmp( optarg, "20K")) return 0x07;
       
   361 	if(!strcmp( optarg, "10K")) return 0x08;
       
   362 	return -1;
       
   363 }
   349 
   364 
   350 /*!                                                                                                
   365 /*!                                                                                                
   351 **                                                                                                 
   366 **                                                                                                 
   352 **                                                                                                 
   367 **                                                                                                 
   353 ** @param d                                                                                        
   368 ** @param d                                                                                        
   374   	hasResponse=1;
   389   	hasResponse=1;
   375   case LSS_SM_GLOBAL: /* Switch Mode Global */
   390   case LSS_SM_GLOBAL: /* Switch Mode Global */
   376   	m.data[1]=*(UNS8 *)dat1;
   391   	m.data[1]=*(UNS8 *)dat1;
   377   	break;
   392   	break;
   378   case LSS_CONF_BIT_TIMING: /* Configure Bit Timing Parameters */
   393   case LSS_CONF_BIT_TIMING: /* Configure Bit Timing Parameters */
       
   394   	
   379   	m.data[1]=*(UNS8 *)dat1;
   395   	m.data[1]=*(UNS8 *)dat1;
   380   	m.data[2]=*(UNS8 *)dat2;
   396   	d->lss_transfer.baudRate=*(char **)dat2;
   381   	if(d->lss_ChangeBaudRate){
   397   	
   382 		/* If a baud rate is not supported just comment the line. */
   398   	if((m.data[2]=CO_TranslateBaudRate(d->lss_transfer.baudRate))>0){
   383 		switch(m.data[2]){
   399   		hasResponse=1;
   384 			case 0x00:d->lss_transfer.baudRate="1M";break;
   400 		break;	 
   385 			case 0x01:d->lss_transfer.baudRate="800K";break;
   401   	}
   386 			case 0x02:d->lss_transfer.baudRate="500K";break;
   402   		
   387 			case 0x03:d->lss_transfer.baudRate="250K";break;
   403 	MSG_ERR(0x1D19, "Master-> Baud rate not supported",0);
   388 			case 0x04:d->lss_transfer.baudRate="125K";break;
   404 	d->lss_transfer.dat1=0xFF;
   389 			case 0x05:d->lss_transfer.baudRate="100K";break;
   405 	
   390 			case 0x06:d->lss_transfer.baudRate="50K";break;
   406 	/* if bit timing is not supported comment the previous code and uncomment the following one*/
   391 			case 0x07:d->lss_transfer.baudRate="20K";break;
   407 	/*{
   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);
   408 		MSG_ERR(0x1D1A, "Master-> Bit timing not supported",0);
   404 		d->lss_transfer.dat1=0x01;
   409 		d->lss_transfer.dat1=0x01;
   405 	}
   410 	}*/
   406 	
   411 	
   407 ErrorBitRateMaster:
       
   408 	d->lss_transfer.dat2=0;
   412 	d->lss_transfer.dat2=0;
   409  	/* If there is a callback, it is responsible of the error */
   413  	/* If there is a callback, it is responsible of the error */
   410 	if(d->lss_transfer.Callback)
   414 	if(d->lss_transfer.Callback)
   411     	(*d->lss_transfer.Callback)(d,d->lss_transfer.command);
   415 	   	(*d->lss_transfer.Callback)(d,d->lss_transfer.command);
   412 	return 0xFF;
   416 	return 0xFF;
   413   	break;
   417   	break;
   414   case LSS_CONF_ACT_BIT_TIMING: /* Activate Bit Timing Parameters */
   418   case LSS_CONF_ACT_BIT_TIMING: /* Activate Bit Timing Parameters */
   415 	m.data[1]=(UNS8)(*(UNS32*)dat1 & 0xFF);
   419 	m.data[1]=(UNS8)(*(UNS32*)dat1 & 0xFF);
   416 	m.data[2]=(UNS8)(*(UNS32*)dat1>>8 & 0xFF);
   420 	m.data[2]=(UNS8)(*(UNS32*)dat1>>8 & 0xFF);
   417 	if(d->lss_transfer.baudRate!="none"){
   421 	if(d->lss_transfer.baudRate!="none"){
   418 		d->lss_transfer.switchDelay=(UNS16)(*(UNS32*)dat1 & 0xFFFF);
   422 		d->lss_transfer.switchDelay=(UNS16)(*(UNS32*)dat1 & 0xFFFF);
   419 		d->lss_transfer.switchDelayState=SDELAY_FIRST;
   423 		d->lss_transfer.switchDelayState=SDELAY_FIRST;
       
   424 		d->lss_transfer.canHandle_t=d->canHandle;
   420 		res=canSend(d->canHandle,&m);
   425 		res=canSend(d->canHandle,&m);
   421   		if(res==0){
   426   		if(res==0){
   422   			StartLSS_SDELAY_TIMER();
   427   			StartLSS_SDELAY_TIMER();
   423   			d->lss_transfer.state=LSS_TRANS_IN_PROGRESS;
   428   			d->lss_transfer.state=LSS_TRANS_IN_PROGRESS;
   424   		}
   429   		}
   671 	{
   676 	{
   672 		UNS8 error_code=0;
   677 		UNS8 error_code=0;
   673 		UNS8 spec_error=0;
   678 		UNS8 spec_error=0;
   674 			
   679 			
   675 		if(d->lss_transfer.mode==LSS_CONFIGURATION_MODE){
   680 		if(d->lss_transfer.mode==LSS_CONFIGURATION_MODE){
   676 			/* Change the baud rate only if the function lss_ChangeBaudRate 
   681 			/* If a baud rate is not supported just comment the line. */
   677 			 * has been implemented. If not send an error.
   682 			switch(m->data[2]){
   678 			 */
   683 			case 0x00:d->lss_transfer.baudRate="1M";break;
   679 			if(d->lss_ChangeBaudRate){
   684 			case 0x01:d->lss_transfer.baudRate="800K";break;
   680 				/* If a baud rate is not supported just comment the line. */
   685 			case 0x02:d->lss_transfer.baudRate="500K";break;
   681 				switch(m->data[2]){
   686 			case 0x03:d->lss_transfer.baudRate="250K";break;
   682 				case 0x00:d->lss_transfer.baudRate="1M";break;
   687 			case 0x04:d->lss_transfer.baudRate="125K";break;
   683 				case 0x01:d->lss_transfer.baudRate="800K";break;
   688 			case 0x05:d->lss_transfer.baudRate="100K";break;
   684 				case 0x02:d->lss_transfer.baudRate="500K";break;
   689 			case 0x06:d->lss_transfer.baudRate="50K";break;
   685 				case 0x03:d->lss_transfer.baudRate="250K";break;
   690 			case 0x07:d->lss_transfer.baudRate="20K";break;
   686 				case 0x04:d->lss_transfer.baudRate="125K";break;
   691 			case 0x08:d->lss_transfer.baudRate="10K";break;
   687 				case 0x05:d->lss_transfer.baudRate="100K";break;
   692 			default:
   688 				case 0x06:d->lss_transfer.baudRate="50K";break;
   693 				MSG_ERR(0x1D28, "Baud rate not supported",0);
   689 				case 0x07:d->lss_transfer.baudRate="20K";break;
   694 				error_code=0xFF; /* Baud rate not supported*/
   690 				case 0x08:d->lss_transfer.baudRate="10K";break;
   695 				break; 		
   691 				default:
       
   692 					MSG_ERR(0x1D28, "Baud rate not supported",0);
       
   693 					error_code=0xFF; /* Baud rate not supported*/
       
   694 					break; 
       
   695 				}		
       
   696 			}
       
   697 			else
       
   698 			{
       
   699 				MSG_ERR(0x1D29, "Bit timing not supported",0);
       
   700 				error_code=0x01; /* bit timing not supported */
       
   701 			}
   696 			}
   702 		}
   697 		}
   703 		else{
   698 		else{
   704 			MSG_WAR(0x3D2A, "SlaveLSS not in configuration mode",0);
   699 			MSG_WAR(0x3D2A, "SlaveLSS not in configuration mode",0);
   705 			//error_code=0xFF;
   700 			//error_code=0xFF;
   706 			break;
   701 			break;
   707 		}
   702 		}
   708 		
   703 		
       
   704 		/* if bit timing is not supported comment the previous code and uncomment the following */
       
   705 		/*{
       
   706 			MSG_ERR(0x1D29, "Bit timing not supported",0);
       
   707 			error_code=0x01; // bit timing not supported 
       
   708 		}*/
       
   709 			
   709 		sendSlaveLSSMessage(d,msg_cs,&error_code,&spec_error);
   710 		sendSlaveLSSMessage(d,msg_cs,&error_code,&spec_error);
   710 	}
   711 	}
   711 	break;
   712 	break;
   712 	case LSS_CONF_ACT_BIT_TIMING: /* Activate Bit Timing Parameters */
   713 	case LSS_CONF_ACT_BIT_TIMING: /* Activate Bit Timing Parameters */
   713 		
   714 		
   979   *dat2=d->lss_transfer.dat2;
   980   *dat2=d->lss_transfer.dat2;
   980   return d->lss_transfer.state;
   981   return d->lss_transfer.state;
   981 }
   982 }
   982 
   983 
   983 //void _lss_StoreConfiguration(UNS8 *error, UNS8 *spec_error){printf("_lss_StoreConfiguration\n");}
   984 //void _lss_StoreConfiguration(UNS8 *error, UNS8 *spec_error){printf("_lss_StoreConfiguration\n");}
   984 //void _lss_ChangeBaudRate(char *BaudRate){printf("_lss_ChangeBaudRate\n");}
       
   985 
   985 
   986 #endif
   986 #endif