1 底层SPI配置见《TC397 之 TLF35584 SPI配置 代码实现章节》博客
2 TLF35584
2.1 TLF35584获取SPI通信状态
static uint8 SpiTask_GetTlf35584SpiState(void)
{
uint8 ret = 0;
ret = SpiDrv_Qspi0DataExchange(0, 0x1F, 0x00);
if(ret == TRUE)
{
s_SpiState = (g_Qspi0RxData >> 1) & 0xFF;
return s_SpiState;
}
else
{
return 0xFF;
}
}
2.2 保护寄存器解锁和上锁
static uint8 SpiTask_SwitchProtectionReg(boolean State)
{
uint8 ret;
if(State == FALSE)
{
ret = SpiDrv_Qspi0DataExchange(1, 0x03, 0xAB);
if(ret == TRUE)
{
ret = SpiDrv_Qspi0DataExchange(1, 0x03, 0xEF);
if(ret == TRUE)
{
ret = SpiDrv_Qspi0DataExchange(1, 0x03, 0x56);
if(ret == TRUE)
{
SpiDrv_Qspi0DataExchange(1, 0x03, 0x12);
}
}
}
}
else
{
ret = SpiDrv_Qspi0DataExchange(1, 0x03, 0xDF);
if(ret == TRUE)
{
ret = SpiDrv_Qspi0DataExchange(1, 0x03, 0x34);
if(ret == TRUE)
{
ret = SpiDrv_Qspi0DataExchange(1, 0x03, 0xBE);
if(ret == TRUE)
{
SpiDrv_Qspi0DataExchange(1, 0x03, 0xCA);
}
}
}
}
return (ret==1) ? 0:0xFF;
}
2.3 控制QST电源开关
static uint8 SpiTask_SwitchTlf35584StandbyQST(boolean State)
{
uint8 regData = 0;
uint8 ret = 0;
for(uint8 i =0; i<5; i++)
{
ret = SpiDrv_Qspi0DataExchange(0, 0x0B, 0x00);
if(ret == TRUE)
{
regData = (g_Qspi0RxData >> 1) & 0xFF;
if((regData & 0x1) != State)
{
regData = State;
SpiDrv_Qspi0DataExchange(1, 0x04, regData);
}
else
{
return 0;
}
}
}
return 0xFF;
}
2.4 获取当前工作状态
static uint8 SpiTask_GetTlf35584State(void)
{
uint8 regData = 0;
uint8 ret = 0;
ret = SpiDrv_Qspi0DataExchange(0, 0x27, 0x00);
if(ret == TRUE)
{
regData = (g_Qspi0RxData >> 1) & 0xFF;
return (regData & 0x3);
}
else
{
return 0xFF;
}
}
2.5 工作状态切换(NORMAL STANDBY)
static uint8 SpiTask_SetTlf35584State(uint8 StateReq)
{
uint8 regData = 0;
uint8 ret = 0;
for(uint8 i =0; i<5; i++)
{
ret = SpiTask_GetTlf35584State();
if(ret != 0xFF)
{
if(ret != StateReq)
{
regData = StateReq;
if(StateReq == 4)
{
if(ret != 2)
{
SpiDrv_Qspi0DataExchange(1, 0x15, 0x02);
SpiDrv_Qspi0DataExchange(1, 0x16, (~0x02));
SpiTask_SwitchProtectionReg(0);
SpiTask_SwitchTlf35584StandbyQST(0);
SpiTask_SwitchProtectionReg(1);
}
}
else if(StateReq == 2)
{
regData |= 0x80;
}
SpiDrv_Qspi0DataExchange(1, 0x15, regData);
SpiDrv_Qspi0DataExchange(1, 0x16, (~regData));
}
else
{
return 0;
}
}
}
return 0xFF;
}
2.6 关闭看门狗
static uint8 SpiTask_CloseTlf35584Wdgs(void)
{
uint8 regData = 0;
uint8 ret = 0;
for(uint8 i =0; i<5; i++)
{
ret = SpiDrv_Qspi0DataExchange(0, 0x0D, 0x00);
if(ret == TRUE)
{
regData = (g_Qspi0RxData >> 1) & 0xFF;
if(regData & 0xC)
{
regData &= (~0xC);
SpiDrv_Qspi0DataExchange(1, 0x06, regData);
}
else
{
return 0;
}
}
}
return 0xFF;
}
2.7 关闭EER PIN 监测
static uint8 SpiTask_CloseTlf35584ErrMonitor(void)
{
uint8 regData = 0;
uint8 ret = 0;
for(uint8 i =0; i<5; i++)
{
ret = SpiDrv_Qspi0DataExchange(0, 0x0C, 0x00);
if(ret == TRUE)
{
regData = (g_Qspi0RxData >> 1) & 0xFF;
if(regData & 0x8)
{
regData &= (~0x8);
SpiDrv_Qspi0DataExchange(1, 0x05, regData);
}
else
{
return 0;
}
}
}
return 0xFF;
}
2.8 初始化状态切换至NORMAL状态
static void SpiTask_Tlf35584SwitchState_INIT_NORMAL(void)
{
uint8 ret =0, ret1 = 0, ret2 =0, ret3 = 0;
const IfxPort_Io_ConfigPin configPin[] =
{
{&IfxPort_P21_6, IfxPort_Mode_inputPullUp, IfxPort_PadDriver_cmosAutomotiveSpeed1},
{&IfxPort_P00_6, IfxPort_Mode_outputPushPullGeneral, IfxPort_PadDriver_cmosAutomotiveSpeed1},
};
const IfxPort_Io_Config conf =
{
sizeof(configPin)/sizeof(IfxPort_Io_ConfigPin),
(IfxPort_Io_ConfigPin *)configPin
};
IfxPort_Io_initModule(&conf);
IfxPort_setPinHigh(&MODULE_P00, 6);
for(uint8 i=0; i<10; i++)
{
ret = SpiTask_SwitchProtectionReg(0);
if(ret == 0)
{
ret1 = SpiTask_CloseTlf35584Wdgs();
ret2 = SpiTask_CloseTlf35584ErrMonitor();
ret3 = SpiTask_SwitchTlf35584StandbyQST(0);
if((ret1 == 0) && (ret2 == 0) && (ret3 == 0))
{
SpiTask_SwitchProtectionReg(1);
break;
}
}
}
SpiTask_SetTlf35584State(2);
}
2.9 设备初始化函数
void SpiTask_Init(void)
{
SpiDrv_Qspi0Init();
SpiTask_Tlf35584SwitchState_INIT_NORMAL();
SpiTask_GetTlf35584SpiState();
}
2.10 TLF35584 根据实时的KL15/ACC状态判断是否需要切换到STANDBY模式
void SpiTask_Tlf35584SwitchState_NORMAL_STANDBY(void)
{
boolean Kl15PinDetState = IfxPort_getPinState(&MODULE_P21, 6);
static uint8 Kl15HighTime = 0, Kl15LowTime = 0;
if(Kl15PinDetState == TRUE)
{
Kl15HighTime++;
if(Kl15LowTime != 0)
{
Kl15LowTime = 0;
}
}
else
{
Kl15LowTime++;
if(Kl15HighTime != 0)
{
Kl15HighTime = 0;
}
}
if(((Kl15PinDetState == TRUE) && (g_Kl15State == FALSE)) ||\
((Kl15PinDetState == FALSE) && (g_Kl15State == TRUE)))
{
if(Kl15HighTime != 0)
{
Kl15HighTime = 0;
}
if(Kl15LowTime != 0)
{
Kl15LowTime = 0;
}
}
if(( Kl15HighTime > 10 ) && (Kl15LowTime == 0))
{
if(g_Kl15State != FALSE)
{
g_Kl15State = FALSE;
SpiTask_SetTlf35584State(4);
}
}
else if (( Kl15HighTime == 0 ) && (Kl15LowTime > 10))
{
if(g_Kl15State != TRUE)
{
g_Kl15State = TRUE;
}
}
}