#include "common.h"
/**
* UART3 LIN 相关变量
*/
short GV_Uart3DataReceived = 0;
short GV_Uart3BreakReceived = 0;
short ResponseDataByteIndex = 0;
volatile u8 GV_Uart3ReceptionError = 0;
volatile u8 GV_Uart3DataReceiveByte = 0;
UART3_LIN_FSM_STATE GV_Uart3LinFSMState = Idle;
/**
* UART3 初始化
*/
void UART3_CInit()
{
// 拉高 NSLP 口电平,唤醒 TJA1020 芯片
GPIO_Init(UART3_LIN_NSLP_PORT, UART3_LIN_NSLP_PIN, GPIO_MODE_OUT_PP_HIGH_SLOW);
// 配置成从节点
UART3_LINConfigSlave();
}
/**
* UART3 发送字节
*
* @param byte 8位字节
*/
void UART3_SendByte( u8 byte )
{
// 发送字节
UART3_SendData8(byte);
// 等待发送完成
// RESET = 0; SET = 1;
// UART3_FLAG_TXE 表示 发送数据寄存器空标志(即表示UART3没有发送),
// 如果其等于 1,那么表示 URAR1 发送完成或者闲置中;如果其等于 0,那么表示 UART3 正在发送
while( UART3_GetFlagStatus(UART3_FLAG_TXE) == RESET );
}
/**
* UART3 发送字节集
*
* @param bytes 8位字节集
* @param length 字节集长度
*/
void UART3_SendBytes( u8 *bytes, short length )
{
for( short i = 0; i < length; i++ ) {
UART3_SendByte(bytes[i]);
}
}
/**
* UART3 LIN 配置成从节点
*/
void UART3_LINConfigSlave()
{
// 初始 UART3
UART3_DeInit();
UART3_Init((u32)9600, UART3_WORDLENGTH_8D, UART3_STOPBITS_1, UART3_PARITY_NO, UART3_MODE_RX_ENABLE);
// 配置 UART3 LIN
UART3_LINConfig(UART3_LIN_MODE_SLAVE, UART3_LIN_AUTOSYNC_ENABLE, UART3_LIN_DIVUP_LBRR1);
UART3_LINBreakDetectionConfig(UART3_LINBREAKDETECTIONLENGTH_11BITS);
// 启用 UART3 读中断,启用 Lin Break 中断
UART3_ITConfig(UART3_IT_RXNE_OR, ENABLE);
UART3_ITConfig(UART3_IT_LBDF, ENABLE);
// 启用 UART3 LIN
UART3_LINCmd(ENABLE);
}
/**
* UART3 LIN 配置成主节点
*/
void UART3_LINConfigMaster()
{
// 初始 UART3
UART3_DeInit();
UART3_Init((u32)9600, UART3_WORDLENGTH_8D, UART3_STOPBITS_1, UART3_PARITY_NO, UART3_MODE_TXRX_ENABLE);
// 配置 UART3 LIN
UART3_LINConfig(UART3_LIN_MODE_MASTER, UART3_LIN_AUTOSYNC_DISABLE, UART3_LIN_DIVUP_LBRR1);
UART3_LINBreakDetectionConfig(UART3_LINBREAKDETECTIONLENGTH_11BITS);
// 启用 UART3 LIN
UART3_LINCmd(ENABLE);
// 启用 UART3 读中断,关闭 Lin Break 中断
UART3_ITConfig(UART3_IT_RXNE_OR, ENABLE);
UART3_ITConfig(UART3_IT_LBDF, DISABLE);
}
/**
* UART3 LIN 主任务
*/
void UART3_LINMainTask()
{
switch( GV_Uart3LinFSMState ){
/* ------------- */
/* STATE Idle : */
/* ------------- */
case Idle :
// 关闭数据接收中断并开启 Break 检测中断,等待 Break
UART3_ITConfig(UART3_IT_RXNE_OR, DISABLE);
UART3_ITConfig(UART3_IT_LBDF, ENABLE);
GV_Uart3LinFSMState = Break;
ResponseDataByteIndex=0;
break;
/* -------------------------------------------------- */
/* STATE Break : Waiting for Synch Break(间隔域)
/* -------------------------------------------------- */
case Break:
if( GV_Uart3BreakReceived ){
GV_Uart3BreakReceived = 0;
// 关闭 Break 检测中断并开启数据接收中断,等待数据
UART3_ITConfig(UART3_IT_LBDF, DISABLE);
UART3_ITConfig(UART3_IT_RXNE_OR, ENABLE);
GV_Uart3LinFSMState = SynchField;
}
else if( GV_Uart3DataReceived ){
GV_Uart3DataReceived = 0;
// 关闭数据接收中断并开启 Break 检测中断
UART3_ITConfig(UART3_IT_RXNE_OR, DISABLE);
UART3_ITConfig(UART3_IT_LBDF, ENABLE);
}
break;
/* ---------------------------------------------------- */
/* STATE SynchField : Waiting for Synch Field(同步域)
/* ---------------------------------------------------- */
case SynchField:
if( GV_Uart3DataReceived & !GV_Uart3ReceptionError ){
GV_Uart3DataReceived = 0;
GV_Uart3LinFSMState = GV_Uart3DataReceiveByte == 0x55 ? Identifier : Idle;
}
else if( GV_Uart3ReceptionError ){
GV_Uart3ReceptionError = 0;
GV_Uart3LinFSMState = Idle;
}
break;
/* ------------------------------------------------ */
/* STATE IDField : Waiting for ID Field(标识符域) */
/* ------------------------------------------------ */
case Identifier:
// 重置数据接收标记
if ( GV_Uart3DataReceived & !GV_Uart3ReceptionError ) GV_Uart3DataReceived = 0;
/* Store ID for enhanced checksum calculation */
//LINChecksumBuffer[0]=UART3Data;
//ResponseDataByteIndex=1; /* index for 1st data */
/*
switch( GV_Uart3DataByte ){
case MS0ID:
GV_Uart3LinFSMState = DataReception;
DLC=8;
break;
case MS2ID:
for (Index=0;Index<8;Index++)
{
DataToSend[Index]=DataBuffer[1][Index];
}
GV_Uart3LinFSMState = DataTransmission;
DLC=8;
break;
case SM1ID:
GV_Uart3LinFSMState = DataReception;
DLC=4;
break;
case SM3ID:
for (Index=0;Index<8;Index++)
{
DataToSend[Index]=DataBuffer[3][Index];
}
LIN_FSM = DataTransmission;
DLC=4;
break;
default:
LIN_FSM = Idle;
break;
}
}
#ifdef LINUART
else if (ReceptionError|IdentifierParityError)
{
ReceptionError=0;
IdentifierParityError=0;
LINError();
LIN_FSM = Idle;
}
#else
else if (ReceptionError)
{
ReceptionError=0;
LINError();
LIN_FSM = Idle;
}
#endif
*/
break;
}
}
评论0