HS550/ECU_APP/user/appTask.c
2024-05-23 17:02:37 +08:00

506 lines
11 KiB
C

#include "appTask.h"
#include "iodefine.h"
#include "extern.h"
#include "r_cg_adc.h"
#include "r_cg_intc.h"
#include "PINdef.h"
#include "hwCtrl.h"
#include "crc.h"
#include "pfdl.h"
u8 can_data_init_flag = 0;
unsigned long Can_1ms_count_alive;
unsigned char rollingcounter = 0;
unsigned char IGN_Voltage_error_flag, IGN_Voltage_error_count;
unsigned char Timer_1ms_flag;
unsigned char Timer_5ms_flag;
unsigned char Timer_10ms_flag;
unsigned char Timer_20ms_flag;
unsigned char Timer_50ms_flag;
unsigned char Timer_1000ms_flag;
unsigned int CAN_LostCount;
unsigned char key_count, key_last;
unsigned char IGN_Lost, IGN_Lost_Count;
unsigned char IGN_On, IGN_On_Count;
/********************* Network Manage ****************************/
unsigned char ucIgnStus;
unsigned int uiBusOffCnt; // ms
unsigned char ucBusOffModeState; // 0:Normal 1~4:Fast Rec 5:Slow Rec
unsigned char BusoffFlag;
/***************************************************/
uint16_t VehicleSpeedRaw,VehicleSpeed;
uint8_t nouse;//Relocation value is odd number
void Apply_task(void)
{
// DiagnosticSession_Present=MngDcm_GetDiagnosticSession();
App28sTxRxStatus = GetNormalMsgSts();
Nw28sTxRxStatus = GetNetworkMsgSts();
// DiagnosticSession_Present=MngDcm_GetDiagnosticSession();
if(CAN_STB == 0 && ucBusOffModeState == 0)//Tx enable
{
TxServe();
}
if (Timer_1ms_flag == 1)
{
Timer_1ms_flag = 0;
MngDcm_MainFunction();
MngCanTp_MainFunction();
if (BusOff_Detect_delay_count == 0 /*&& (ad_voltage_fact[2]>=85 && ad_voltage_fact[2]<=165)*/)
{
BusOff_Detect();
}
KeyScan();
}
if (Timer_5ms_flag == 1)
{
Timer_5ms_flag = 0;
CAN_RX_Data_Handle();
Can_Msg_Change();
KeyPro();
}
if (Timer_10ms_flag == 1)
{
Timer_10ms_flag = 0;
}
if (Timer_20ms_flag == 1)
{
Timer_20ms_flag = 0;
IGN_Voltage_Detect();
}
if (Timer_50ms_flag == 1)
{
Timer_50ms_flag = 0;
TxTestMsg();
//SleepDetect();//TODO
}
}
#define KEY_LONGPRESS_500MS 100
void KeyPro(void)//5ms
{
}
void Can_Msg_Change(void)//
{
uint8_t i;
if (IGN_Voltage_error_flag != 0)
{
for (i = 0; i < 7; i++)
{
CAN_101.Msg_Byte[i] = 0x00;
CAN_220.Msg_Byte[i] = 0x00;
}
CAN_101.Msg_Bit.checksum = CRC_Calc_Table(CAN_101.Msg_Byte, 7);
CAN_220.Msg_Bit.checksum = CRC_Calc_Table(CAN_220.Msg_Byte, 7);
}
else
{
CAN_101.Msg_Bit.TCS_TractionControl = GetKeyState(KEYID_4_P82);
CAN_101.Msg_Bit.reverse11 = GetKeyState(KEYID_9_P30);
CAN_101.Msg_Bit.reverse12 = GetKeyState(KEYID_7_P63);
CAN_101.Msg_Bit.HandleHeating = GetKeyState(KEYID_5_P83);
CAN_101.Msg_Bit.DifferentialLock = GetKeyState(KEYID_1_P125);
CAN_101.Msg_Bit.RightSteering = GetKeyState(KEYID_2_P34);
CAN_101.Msg_Bit.EngineIgnitionStopControl = GetKeyState(KEYID_3_P81);
CAN_101.Msg_Bit.CrankingMotor = GetKeyState(KEYID_6_P80);
CAN_101.Msg_Bit.DIST_SET = GetKeyState(KEYID_8_P17);
CAN_101.Msg_Bit.checksum = CRC_Calc_Table(CAN_101.Msg_Byte, 7);
CAN_220.Msg_Bit.SwitchButton_NearOrFarLight = GetKeyState(KEYID_4_P82);
CAN_220.Msg_Bit.FogLampSwitch = GetKeyState(KEYID_1_P125);
CAN_220.Msg_Bit.HeadLampSwitch = GetKeyState(KEYID_3_P81);
CAN_220.Msg_Bit.FAxle_2_4WD_DifLock = GetKeyState(KEYID_6_P80);
CAN_220.Msg_Bit.Trumpet = GetKeyState(KEYID_7_P63);
CAN_220.Msg_Bit.LeftSteering = GetKeyState(KEYID_9_P30);
CAN_220.Msg_Bit.WarningAgainstDanger = GetKeyState(KEYID_8_P17);
CAN_220.Msg_Bit.TurnOnTheHeater = GetKeyState(KEYID_2_P34);
CAN_220.Msg_Bit.MODEL_SEL = GetKeyState(KEYID_5_P83);
CAN_220.Msg_Bit.checksum = CRC_Calc_Table(CAN_220.Msg_Byte, 7);
}
}
void EEL_READ(void)
{
}
#define SC_500MS_PC_1MS 500
void EEL_WRITE(void)
{
}
void EEL_Write_Enable_Check(void)
{
}
void DID_data_save_init(void)
{
unsigned char DID_init_Buf[33], i;
ReadFlashData(DID_init_Buf, 0x000F1000 + 0x400, 1);
if (DID_init_Buf[0] == 0xff)
{
for (i = 0; i < 33; i++)
{
if (i < 8)
{
DID_init_Buf[i] = DID_0xF110_VehicleNetworkConfiguration[i];
}
else if (i < 25)
{
DID_init_Buf[i] = DID_0xF190_vehicleIdentificationNumber[i - 8];
}
else if (i < 33)
{
DID_init_Buf[i] = DID_0xF197_SystemNameOrEngineType[i - 25];
}
}
WriteDataflash(DID_init_Buf, 0x000F1000 + 0x400, 33);
}
ReadFlashData(DID_init_Buf, 0x000F1000 + 0x800, 1);
if (DID_init_Buf[0] == 0xff)
{
for (i = 0; i < 32; i++)
{
if (i < 16)
{
DID_init_Buf[i] = DID_0xF198_RepairShopCodeOrTesterSerialNumber[i];
}
else if (i < 20)
{
DID_init_Buf[i] = DID_0xF199_ProgrammingOrConfigurationDate[i - 16];
}
else if (i < 24)
{
DID_init_Buf[i] = DID_0xF19D_ECUInstallationDateDataIdentifier[i - 20];
}
else if (i < 32)
{
DID_init_Buf[i] = DID_0xF112_VehicleName[i - 24];
}
}
WriteDataflash(DID_init_Buf, 0x000F1000 + 0x800, 32);
}
}
unsigned char BusOffTimeCnt = 0;
unsigned short BusOff1msCnt = 0;
unsigned char CanBusOffPro(void)
{
unsigned char ret = 0;
if (C0ERFLLL & 0x08) // Bus-Off Stutas
{
ret = 1;
}
if ((C0CTRLL & 0x02) == 0x02) // Channal Halt Mode
{
if (C0ERFLLL & 0x08) // Bus-Off Stutas
{
BusOff1msCnt++;
ret = 1;
}
if ((BusOffTimeCnt < 10) && (BusOff1msCnt >= 100))
{
BusOffTimeCnt++;
BusOff1msCnt = 0;
C0ERFLL = 0;
C0CTRL &= ~0x03; /* CHMDC[1:0] = 00B change to communication mode. */
}
else if ((BusOffTimeCnt >= 10) && (BusOff1msCnt >= 1000))
{
BusOffTimeCnt = 10;
BusOff1msCnt = 0;
C0ERFLL = 0;
C0CTRL &= ~0x03; /* CHMDC[1:0] = 00B change to communication mode. */
}
}
return ret;
}
#define FAST_BUSOFF_THRESHOULD 100 // 100MS
#define SLOW_BUSOFF_THRESHOULD 1000 // 100MS
void BusOff_Recovery(void);
void BusOff_Detect(void)
{
if ((C0ERFLL & 0x08) && (R_CAN_ReadChStatus_CH0() & 0x02))
{
if (uiBusOffCnt == 0)
{
if (ucBusOffModeState == 0)
{
uiBusOffCnt = FAST_BUSOFF_THRESHOULD;
// BusOff_Recovery();
}
if ((ucBusOffModeState > 0) && (ucBusOffModeState < 5))
{
BusOff_Recovery();
uiBusOffCnt = FAST_BUSOFF_THRESHOULD; //fast recover
}
else if (ucBusOffModeState >= 5)
{
BusOff_Recovery();
uiBusOffCnt = SLOW_BUSOFF_THRESHOULD; //soft recover
}
else
{
;
}
if (ucBusOffModeState < 5)
{
ucBusOffModeState++;
}
}
}
else
{
if (ucBusOffModeState > 0)
{
if (uiBusOffCnt == 0)
{
ucBusOffModeState = 0;
// 原来程序的标志位
}
}
else
{
;
}
BusOff_flag = 0;
}
}
void BusOff_Recovery(void)
{
CFCCL0L &= ~0x01;
NOP();
CFCCL0L |= 0x01;
if (C0ERFLL != 0)
{
C0ERFLL = 0;
}
C0CTRL &= 0xfffc; // bus off
}
uint16_t SupplyVoltage;
void IGN_Voltage_Detect(void)
{
uint32_t adval;
adval = getAdval(ADCH_IGN);
SupplyVoltage = (adval*57*5)>>10;
if (adval <= 305 )
{
IGN_Voltage_error_count++;
if (IGN_Voltage_error_count >= 250)
{
IGN_Voltage_error_count = 0;
IGN_Voltage_error_flag = 1;
}
}
else if (adval >= 592 )//16.5
{
IGN_Voltage_error_count++;
if (IGN_Voltage_error_count >= 250)
{
IGN_Voltage_error_count = 0;
IGN_Voltage_error_flag = 2;
}
}
else if (adval >= 323 && adval <= 574)
{
IGN_Voltage_error_count = 0;
IGN_Voltage_error_flag = 0;
}
if (IGN_Voltage_error_flag != 0)
{
// TAU0_Channel1_ChangeDuty(0);
}
}
void value_init(void)
{
uint8_t i;
CAN_LostCount = 500;
BusOff_Detect_delay_count = 2000;
ucIgnStus = IGN_OFF;
for (i = 0; i < 8; i++)
{
CAN_101.Msg_Byte[i] = 0;
CAN_220.Msg_Byte[i] = 0;
}
}
void CAN_RX_Data_Handle(void)
{
}
void SleepDetect(void)
{
// if(CAN_LostCount==0 && IGN_Lost == 1)
{
R_TAU0_Channel0_Stop();
// R_TAU0_Channel2_Stop();
// R_TAU0_Channel6_Stop();
// R_TAU1_Channel0_Stop();
TO0 = 0;
TO1 = 0;
TOE0 = 0;
TOE1 = 0;
TO0 = 0;
TO1 = 0;
// R_IICA0_Stop();
R_ADC_Stop();
ADCE = 0;
IICA0EN = 0;
TAU0EN = 0;
TAU1EN = 0;
P6_bit.no2 = 0;
P6_bit.no3 = 0;
P12_bit.no0 = 0;
P3_bit.no3 = 0;
P1_bit.no3 = 0;
P1_bit.no4 = 0;
P3_bit.no0 = 0;
CAN_STB = 1;
C0CTRL |= (uint16_t)(CAN_STP_BIT_ON);
GCTRL |= 0x01;
NOP();
GCTRL |= (uint16_t)(CAN_GLB_STP_BIT_ON);
PLLON = 0;
//R_INTC3_Start();
CAN0WUPIF = 0U;
CAN0WUPMK = 0U;
if (I_IGN_SIGN != IGN_ON)
STOP();
WakeUP();
}
}
void WakeUP(void)
{
// stop can and ex interrupt
CAN0WUPIF = 0U;
CAN0WUPMK = 1U;
//R_INTC3_Stop();
// WDTE = 0xff; //reset
CAN_STB = 0;
hdwinit();
R_MAIN_UserInit();
R_TAU0_Channel0_Start(); // timer
}
void Signal_Lost_Detect(unsigned char Pin,
unsigned char Lost_state,
unsigned char *Signal_Lost,
unsigned char *Signal_Lost_count,
unsigned char Lost_time)
{
if (Pin == Lost_state)
{
(*Signal_Lost_count)++;
if ((*Signal_Lost_count) >= Lost_time)
{
*Signal_Lost_count = 0;
*Signal_Lost = 1;
}
}
else
{
*Signal_Lost_count = 0;
*Signal_Lost = 0;
}
}
void IGN_Detect(void)
{
static unsigned char IGN_last;
Signal_Lost_Detect(I_IGN_SIGN, 1, &IGN_Lost, &IGN_Lost_Count, 5);
Signal_Lost_Detect(I_IGN_SIGN, 0, &IGN_On, &IGN_On_Count, 5);
if (IGN_On == 1)
{
ucIgnStus = IGN_ON;
}
else if (IGN_Lost == 1)
{
ucIgnStus = IGN_OFF;
PWN_KEY = 0;
}
if (IGN_last != ucIgnStus)
{
IGN_last = ucIgnStus;
if (IGN_ON == ucIgnStus)
C0CTRH &= ~0x0300; // 设置正常模式
}
}
void LED_Light_Ctrl(void)
{
}
/*****************diagnostic******************/
void getIGNVoltage(unsigned char* data)
{
data[0] = (uint8_t)SupplyVoltage;
}