报文发送的流程
1. LIN_DRV_MasterSendHeader 发送报头 报头 = 间隔场 + 同步场 + 标志符场
2.回调函数中 调用 LIN_DRV_SendFrameData 接口发送数据,或调用LIN_DRV_ReceiveFrameData 接口接收数据
lin配置
1初始化
2 报头处理回调函数
3 时基回调函数。
1初始化
void lin1_init(void){
LIN_DRV_Init(INST_LIN1, &lin1_InitConfig0, &lin1_State);
LIN_DRV_InstallCallback(INST_LIN1,(lin_callback_t)lin1CallbackHandler);
}
2 报头处理回调函数 LIN_DRV_InstallCallback(INST_LIN1,(lin_callback_t)lin1CallbackHandler);
lin_callback_t lin1CallbackHandler(uint32_t instance, lin_state_t * lin_State)
{
lin_callback_t callbackCurrent;
callbackCurrent = lin_State->Callback;
(void)instance;
switch (lin_State->currentEventId)
{
case LIN_PID_OK:
LIN_DRV_SetTimeoutCounter(INST_LIN1, TIMEOUT);
if(instance == INST_LIN1)
{
// user handle
// LIN_DRV_MasterSendHeader()函数会进入到这里,在这里通过ID的不同进行不同的操作
if(lin_State->currentId == 0x10)
{
// 主机写
LIN_DRV_SendFrameData(INST_LIN1, linTxBuff, sizeof(linTxBuff));
}
else if(lin_State->currentId == 0x20)
{
// 主机读
LIN_DRV_ReceiveFrameData(INST_LIN1, linRxBuff, sizeof(linRxBuff));
}
}
break;
case LIN_PID_ERROR: LIN_DRV_GoToSleepMode(INST_LIN1); break;
case LIN_TX_COMPLETED:
case LIN_RX_COMPLETED: LIN_DRV_GotoIdleState(INST_LIN1); break;
case LIN_CHECKSUM_ERROR:
case LIN_READBACK_ERROR:
case LIN_FRAME_ERROR:
case LIN_RECV_BREAK_FIELD_OK: LIN_DRV_SetTimeoutCounter(INST_LIN1, TIMEOUT); break;
case LIN_WAKEUP_SIGNAL: wakeupSignalFlag = true; break;
case LIN_SYNC_ERROR:
case LIN_BAUDRATE_ADJUSTED:
case LIN_NO_EVENT:
case LIN_SYNC_OK:
default: break;
}
return callbackCurrent;
}
3 时基回调函数。(需要用到lptmr系统定时器设置时基)
(1)lptmr,初始化,设置中断函数
void lptmr1_init(){
LPTMR_DRV_Init(INST_LPTMR1, &lpTmr1_config0,0);
INT_SYS_InstallHandler(LPTMR0_IRQn,&LPTMR0_IRQn_Handler,NULL);
INT_SYS_EnableIRQ(LPTMR0_IRQn);
INT_SYS_SetPriority(LPTMR0_IRQn,2);
LPTMR_DRV_StartCounter(INST_LPTMR1);
}
void LPTMR0_IRQn_Handler(void){
LIN_DRV_TimeoutService(INST_LIN1);
lin1timerOverflowInterruptCount++;
LPTMR_DRV_ClearCompareFlag(INST_LPTMR1);
}
(2)设置lin时间回调函数
// lin1时基回调函数,uint32_t lin1TimerGetTimeIntervalCallback0(uint32_t *ns)
{
static uint32_t previousCountValue = 0UL;
uint32_t counterValue;
counterValue = LPTMR_DRV_GetCounterValueByCount(INST_LPTMR1); // lptmr提供时基
*ns = ((uint32_t)(counterValue + lin1timerOverflowInterruptCount * TIMER_COMPARE_VAL - previousCountValue)) * 1000 / TIMER_TICKS_1US;
lin1timerOverflowInterruptCount = 0UL;
previousCountValue = counterValue;
return 0UL;
}
最后可以使用
LIN_DRV_MasterSendHeader(INST_LIN1, 0x01);发送报头,会进入报头处理回调函数 LIN_DRV_InstallCallback(INST_LIN1,(lin_callback_t)lin1CallbackHandler);进行判断