⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lin_driver08.c

📁 此程序为DP256的SPI实验程序,已经通过实验证明
💻 C
📖 第 1 页 / 共 4 页
字号:
			//LIN_SCI_FEIE		= 0;
		}
		else if(LIN_Rx_Counter	== (uMsgLen + 1))
		{
			//We receive checksum field know
			//First, We calculate checksum
			uMsgID	= 	(LIN_RX_Buf[LIN_INDEX_ID] & 0x3f);
			
			uIndex	=	(LIN_Msg_Index[uMsgID] & 0x3f);
			
			//Bug  fixed by wangjunpeng, 10th july,2005
			//paramenter transfered to function LIN_Calc_Rx_Checksum_EN
			//is message datalength,not include chhecksum byte
			//paramenter ndata modified to message data length uMsgLen
			
			LIN_RX_Buf[LIN_INDEX_CHECKSUM]	= LIN_Calc_Rx_Checksum_EN(uMsgLen);
		
			if(LIN_RX_Buf[LIN_INDEX_CHECKSUM] == LIN_TmpByte)
			{
			//OK!!,we get a correct message, and we can use the data know
//----------------------------------------------------------------------------------
				uMsgID	= (LIN_RX_Buf[LIN_INDEX_ID] & 0x3f);
#if LIN_MSG_10
#define	REC_IF_EXIST	1			
				if(uMsgID == 0x10)
				{
					Msg_10_Decode();
				}
#endif
#if	LIN_MSG_11			
#ifndef REC_IF_EXIST		
	#define	REC_IF_EXIST	1	
#else			
	else 
#endif
				if(uMsgID == 0x11)
				{
					Msg_11_Decode();
				}
#endif			
#if	LIN_MSG_12			
#ifndef REC_IF_EXIST		
	#define	REC_IF_EXIST	1			
	else 
#endif
				if(uMsgID == 0x12)
				{
					Msg_12_Decode();
				}
#endif			
#if	LIN_MSG_13			
#ifndef REC_IF_EXIST		
	#define	REC_IF_EXIST	1			
#else			
	else 
#endif
				if(uMsgID == 0x13)
				{
					Msg_13_Decode();
				}
#endif			
#if	LIN_MSG_14			
#ifndef REC_IF_EXIST		
	#define	REC_IF_EXIST	1			
#else			
	else 
#endif
				if(uMsgID == 0x14)
				{
					Msg_14_Decode();
				}
#endif			
#if	LIN_MSG_15			
#ifndef REC_IF_EXIST		
	#define	REC_IF_EXIST	1			
#else			
	else 
#endif
				if(uMsgID == 0x15)
				{
					Msg_15_Decode();
				}
#endif			
#if	LIN_MSG_16			
#ifndef REC_IF_EXIST		
	#define	REC_IF_EXIST	1			
#else			
	else 
#endif
				if(uMsgID == 0x16)
				{
					Msg_16_Decode();
				}
#endif			
#if	LIN_MSG_17			
#ifndef REC_IF_EXIST		
	#define	REC_IF_EXIST	1			
#else			
	else 
#endif
				if(uMsgID == 0x17)
				{
					Msg_17_Decode();
				}
#endif			
#if	LIN_MSG_18			
#ifndef REC_IF_EXIST		
	#define	REC_IF_EXIST	1			
#else			
	else 
#endif
				if(uMsgID == 0x18)
				{
					Msg_18_Decode();
				}
#endif			
#if	LIN_MSG_19			
#ifndef REC_IF_EXIST		
	#define	REC_IF_EXIST	1			
#else			
	else 
#endif
				if(uMsgID == 0x19)
				{
					Msg_19_Decode();
				}
#endif			
#if	LIN_MSG_1A			
#ifndef REC_IF_EXIST		
	#define	REC_IF_EXIST	1			
#else			
	else 
#endif
				if(uMsgID == 0x1a)
				{
					Msg_1A_Decode();
				}
#endif			
#if	LIN_MSG_1B			
#ifndef REC_IF_EXIST		
	#define	REC_IF_EXIST	1			
#else			
	else 
#endif
				if(uMsgID == 0x1b)
				{
					Msg_1B_Decode();
					//PTE_PTE3	= ~PTE_PTE3;	
				}
#endif			
#if	LIN_MSG_1C			
#ifndef REC_IF_EXIST		
	#define	REC_IF_EXIST	1			
#else			
	else 
#endif
				if(uMsgID == 0x1c)
				{
					Msg_1C_Decode();
				}
#endif			

//----------------------------------------------------------------------------------							
				//So,Set Message Updated Flag, user can use the received message know				
				//LIN_MsgStatus[uIndex]	= LIN_MSG_UPDATED;
				
				//Set LIN_Error_Status as LIN_SUCCESS_TRANSFER
				LIN_Error_Status	|= LIN_SUCCESS_TRANSFER;
				LIN_Err_RxCounter	<<= 1;			
				LIN_Err_RxCounter	&= 0xfe;
				
				//Wait for next Message transfer
				LIN_Transceiver_Status	= LIN_FLAG_IGNORE;
				
				//reset LIN_Rx_Counter and LIN_Tx_Counter
				LIN_Rx_Counter	= 0;
				LIN_Tx_Counter	= 0;
				
				//Update environment variable	
			}//if(LIN_RX_Buf[LIN_INDEX_CHECKSUM] == LIN_TmpByte)
			else
			{//LIN_RX_Checksum is error, set error flag & ignore this message
				LIN_Transceiver_Status	= LIN_FLAG_IGNORE;

				LIN_Err_RxCounter	<<= 1;			
				LIN_Err_RxCounter	|= 1;
				
				LIN_Error_Status		&= ~LIN_SUCCESS_TRANSFER;
			}//LIN_RX_Checksum error		
		}// if(LIN_Rx_Counter	== (uMsgLen + 1))
		//end of message receiving process
//		else
//		{
//			LIN_Transceiver_Status	= LIN_FLAG_IGNORE;
//
//			LIN_Err_RxCounter	<<= 1;			
//			LIN_Err_RxCounter	|= 1;
			
//			LIN_Error_Status		&= ~LIN_SUCCESS_TRANSFER;
//		}
	}//else if((LIN_Transceiver_Status & LIN_FLAG_REC_DATA) == LIN_FLAG_REC_DATA)
	//data receiving process
}


void	LIN_Wakeup(void)
{
	if((LIN_Node_Status & LIN_FLAG_WAKEUP) == LIN_FLAG_WAKEUP)
	{
		LIN_SendData(LIN_WAKEUP_CHAR);
		//LIN_Wakeup_Phase	= 0;
		LIN_Node_Status		= LIN_FLAG_WAKEUP_TIMEOUT;
		LIN_SCI_FEIE		= 1;
		LIN_SCIRIE			= 1;
		//LIN_SetTimer(LIN_Wakeup_Timer[LIN_Wakeup_Phase]);
		LIN_SetTimer();
	}
	//Current LIN Bus is In which state
	//Set wakeup timeout value
}


/*
	Usage:
		LIN_SetBaudrate(LIN_BAUD_NORMAL);
		LIN_SetBaudrate(LIN_BAUD_SENDBREAK);
*/
void	LIN_SetBaudrate(void)
{
	LIN_SCIBD	= 0x50;//SCI_BPD_DIV + SCI_BD_DIV + SCI_BRK_R;
	LIN_SCPSC	= SCI_PD_DIV + SCI_PDFA_DIV;
}

void	LIN_SendData(L_U8	data)
{
	//while((LIN_SCISR1	 & SCI_TBE) != SCI_TBE) 
	//{
	//bug fixed, 
	//reason: abnormal lin message on the lin cluster,
	//this disturbed the lin communication
	//reserlution: add send data validation check logic
	//modified date: 17th, July, 2005
	//Author: Wangjunpeng
	if((LIN_Transceiver_Status & LIN_FLAG_SEND_DATA) == LIN_FLAG_SEND_DATA)
	{
		LIN_SCIDRL	= data;
	}
	//}
#if	LIN_DEBUG_MODE	== 1
	LIN_TEST_PIN_SCI	= ~LIN_TEST_PIN_SCI;
#endif		
}

L_U8	LIN_Calc_Tx_Checksum(L_U8	uBytes)
{
	L_U8	i;
	L_U8	uRet;
	L_U16	sum;
	if(uBytes < 1) return 0;
	uRet	= LIN_TX_Buf[0];
	sum		= uRet;
	for( i = 1; i< uBytes; i++)
	{
		sum	+= LIN_TX_Buf[i];
		if(sum > 0xff) sum -= 0xff;
	}
	
	uRet	= (L_U8)sum;
	uRet	= ~uRet;
	return uRet;
}

L_U8	LIN_Calc_Tx_Checksum_EN(L_U8	uBytes)
{
	L_U8	i;
	L_U8	uRet;
	L_U16	sum;
	if(uBytes < 1) return 0;
	uRet	= LIN_TX_Buf[0];
	sum		= uRet;
	for( i = 1; i< uBytes; i++)
	{
		sum	+= LIN_TX_Buf[i];
		if(sum > 0xff) sum -= 0xff;
	}
	
	//sum		+= LIN_TX_Buf[LIN_INDEX_ID]; Masked for test LIN_TX_Buf[LIN_INDEX_ID]'s value
	//Because we cann't see its value
	sum		+= LIN_RX_Buf[LIN_INDEX_ID];
	if(sum	> 0xff)	uRet	= sum - 0xff;
	else	uRet	= (L_U8)sum;
	uRet	= ~uRet;
	return uRet;
}

L_U8	LIN_Calc_Rx_Checksum(L_U8	uBytes)
{
	L_U8	i;
	L_U8	uRet;
	L_U16	sum;
	if(uBytes < 1) return 0;
	uRet	= LIN_RX_Buf[0];
	sum		= uRet;
	for( i = 1; i< uBytes; i++)
	{
		sum	+= LIN_RX_Buf[i];
		if(sum > 0xff) sum -= 0xff;
	}
	
	uRet	= (L_U8)sum;
	uRet	= ~uRet;
	return uRet;
}

L_U8	LIN_Calc_Rx_Checksum_EN(L_U8	uBytes)
{
	L_U8	i;
	L_U8	uRet;
	L_U16	sum;
	if(uBytes < 1) return 0;
	uRet	= LIN_RX_Buf[0];
	sum		= uRet;
	for( i = 1; i< uBytes; i++)
	{
		sum	+= LIN_RX_Buf[i];
		if(sum > 0xff) sum -= 0xff;
	}
	
	sum		+= LIN_RX_Buf[LIN_INDEX_ID];
	if(sum	> 0xff)	uRet	= sum - 0xff;
	else	uRet	= (L_U8)sum;
	uRet	= ~uRet;
	return uRet;
}

L_U8	LIN_GetID(L_U8	id)
{
	L_U8	uRet;
	uRet	= (id & 0x3f);
	return uRet;
}


L_U8	LIN_Read_Node_Status(void)
{
	return	LIN_Node_Status;
}

L_U8	LIN_Read_Error_Status(void)
{
	return LIN_Error_Status;
}

L_U8	LIN_Read_Transceiver_Status(void)
{
	return LIN_Transceiver_Status;
}

L_U8	LIN_Read_Pending_Status(void)
{	
	return LIN_Pending_Status;
}

void	LIN_Init_Timer(void)
{
	wSysTime		= 0;
	wTimerIncrement	= 1;
	LIN_TMR_TBR		= LIN_TMR_DIV1MS;
	LIN_TMR_IE		= 1;
	LIN_TMR_ACK		= 1;
	LIN_TMR_ON		= 1;
	
}

void StartTimer(void)
{
	LIN_TMR_ON	= 1;
}
void StopTimer(void)
{
	LIN_TMR_ON	= 0;
}


L_BOOL LIN_CheckTimeOut(L_U16 wTimeStart, L_U16 wTimeNow, L_U16 wTimeOut)
{
#define wActivateCounter ((L_U16)(wTimeOut+wTimeStart))
  if (wActivateCounter > wTimeStart)
  {
    if ((wTimeNow >= wActivateCounter) ||
        (wTimeNow < wTimeStart))
    {
      return TRUE;
    }
  }
  else if ((wTimeNow >= wActivateCounter) &&
           (wTimeNow < wTimeStart))
  {
    return TRUE;
  }
  return FALSE;
}

L_U16 LIN_GetCurrentTime(void)
{
  return wSysTime;
} /* Tim_GetCurrentTime */


void	LIN_TIC_Init(void)
{
	LIN_TIC_SC		= TIC_STOP + TIC_RST;
	LIN_TIC_PRESCAL	= TIC_TIC_DIV2;
#if	LIN_DEBUG_MODE	== 0	
	LIN_TIC_SC0		= TIC_IE + TIC_IC_FALLING;
#else
	LIN_TIC_SC0		= TIC_IE + TIC_IC_BOTH;
#endif
}

void	LIN_TIC_Enable(L_U8 en)
{
	if(en == 0)
	{
		LIN_TIC_STOP	= 1;		
	}
	else if(en == LIN_TIC_EN_SYNCH)
	{
		LIN_TIC_Counter	= LIN_TIC_COUNTS_SYNCH;
		LIN_TIC_SC0		= TIC_IE + TIC_IC_FALLING;
		LIN_TIC_STOP	= 0;
	}
	else if(en == LIN_TIC_EN_WAKEUP)
	{
		//LIN_TIC_Counter	= LIN_TIC_COUNTS_WAKEUP;
		LIN_TIC_SC0		= TIC_IE + TIC_IC_FALLING;
		LIN_TIC_STOP	= 0;
	}
}

void LIN_QueryTimeoutEvents(void)
{
	
	if((LIN_Node_Status & LIN_FLAG_WAKEUP) == LIN_FLAG_WAKEUP)
	{
		if(LIN_CheckTimeOut(LIN_Timeout, LIN_GetCurrentTime(), LIN_Wakeup_Timer[LIN_Wakeup_Phase]))
		{
			//LIN network was not waked up, send next wakeup signal.
			LIN_Wakeup();
			
			//Update Wakeup phase
			LIN_Wakeup_Phase++;
			if(LIN_Wakeup_Phase	== LIN_SLEEP_TIME_INDEX) LIN_Wakeup_Phase	= 0;
		}
	}
	else if(LIN_CheckTimeOut(LIN_Timeout, LIN_GetCurrentTime(), LIN_Wakeup_Timer[LIN_SLEEP_TIME_INDEX]))
	{
		//we has not received any message from the network for 4 seconds, we come into sleep
		//1. set sleep flag
		LIN_Node_Status	= LIN_FLAG_SLEEP;
		
		//2. do hardware preparation
		//EnableKeyboard Interrupt, so can wakeup by switch status changed events
		
		//3. excute sleep command
		LIN_ENTER_SLEEP;
	}
}

void LIN_SetTimer()
{
	DisableInterrupts;
	LIN_Timeout	= LIN_GetCurrentTime();
	EnableInterrupts;
}
/*
void LIN_SetTimer(L_U16 ticks)
{
	DisableInterrupts;
	LIN_Timeout	= ticks;
	EnableInterrupts;
}
*/
void	LIN_Enable(L_U8	en)
{
	if(en < 2)
	{
		LIN_EN_MASK		= 1;
		LIN_EN			= en;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -