PowerManag_user.c 6.54 KB
Newer Older
时昊's avatar
时昊 committed
1 2
#include "PowerManagement.h"
#include "Components.h"
3
#include "FaultCode.h"
时昊's avatar
时昊 committed
4

5
RTC_Information_st_t g_stRTCInformation;
时昊's avatar
时昊 committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

static void Power_KL30_Init(void);
static void Power_Wakeup_Init(void);
static void Power_LVP_Init(void);
static void Power_OVP_Init(void);
static void Power_IG_OFF_Init(void);
static void Power_IG_ON_Init(void);
static void Power_Sleep_Init(void);
static Power_Status_em Power_Stay_ON(void);
static Power_Status_em Power_Stay_OFF(void);
static Power_Status_em Power_Stay_Protect(void);
static Power_Status_em Power_Stay_Sleep(void);

static const st_PowerGroup g_stPwrTasks = {
    Power_KL30_Init,
    Power_IG_OFF_Init,
    Power_Stay_OFF,
    Power_Sleep_Init,
    Power_Stay_Sleep,
    Power_Wakeup_Init,
    Power_IG_ON_Init,
    Power_Stay_ON,
    Power_LVP_Init,
    Power_OVP_Init,
    Power_Stay_Protect,
};

void PwrMemInit(void)
{
    PowerMemInit(&g_stPwrTasks);
}

static void Power_KL30_Init(void)
{
//    ClearODO_Flag = 0;
41
    UART_Channel_Config_st_t loc_config;
时昊's avatar
时昊 committed
42
    eeprom_StoreInfo_Init();
43 44 45 46
    loc_config.u32UARTChEn            = 1U;
    loc_config.u32UARTbps             = 115200U;
    loc_config.pfnUARTConfirmCallBack = NULL;
    loc_config.pfnUARTReadMsgCallBack = Amt630hUartRecvData;
47
    UART_DeInit(UART2);
48
    RTE_UART_Init(UART_CH2, &loc_config);
49
    Common_DataInit();
时昊's avatar
时昊 committed
50
    Analog_Signal_Conv_Init();
51
    Sys_KL30_Init();
52 53
    Data_Vehicle_Speed_KL30_Wakeup_Init();
    Data_Engine_Speed_KL30_Wakeup_Init();
54 55
    LINE_IN_Init();
    Key_KL30_Init_EXample();
时昊's avatar
时昊 committed
56
    Data_User_Mileage_KL30Init();
57
    Fuel_KL30_Init();
58
    Backlight_KL30_Wakeup_Init();
59
    Amt630hInit();
时昊's avatar
时昊 committed
60
    Telltales_Init();
时昊's avatar
时昊 committed
61
    TimerM_PWM_counter_Output_Init(TIMERM_COUNTER1, 20000, 64000000);
62
    TimerM_PWM_CH_Output_init(TIMERM_COUNTER1, TIMERM_CHB, ActiveLevel_High);
63 64
    g_stRTCInformation.u8RTCSecond = 0;
	g_stRTCInformation.u8RTCMinute = 0;
65
	g_stRTCInformation.u8RTCHour = 0;          
66 67 68
	g_stRTCInformation.u8RTCDayOfMonth = 1;
	g_stRTCInformation.u8RTCMonth = 1;   
	g_stRTCInformation.u8RTCYear = 20; 
时昊's avatar
时昊 committed
69
    RTE_RTC_Init(g_stRTCInformation);
时昊's avatar
时昊 committed
70
    Menu_User_Init();
71
    Can_Init();
时昊's avatar
时昊 committed
72
    Protocol_KL30_Wakeup_Init();
时昊's avatar
时昊 committed
73
    TimerM_PWM_set_duty(TIMERM_COUNTER1, TIMERM_CHB, 500);
74
    RTE_DEEPSLEEP_GPIO_Interrupt_Enable(KL15_AD_IN, Trigger_Rising);
75
    FaultCode_Init();
时昊's avatar
时昊 committed
76
}
77

时昊's avatar
时昊 committed
78 79
static void Power_Wakeup_Init(void)
{
80
    UART_Channel_Config_st_t loc_config;
81
    Common_DataInit();
时昊's avatar
时昊 committed
82 83 84 85
    Gpio_Init(Gpio_WakeUp_Init);
    eeprom_StoreInfo_Init();
    Analog_Signal_Conv_Init();
    Data_User_Mileage_WakeupInit();
86
    Sys_WakeUp_Init();
87
    RTE_CLOCK_Select_Start();          /* 由于休眠的时候时钟关闭,唤醒的时候打开时钟,才能进行下一步 */
88 89 90 91 92 93
    Data_Vehicle_Speed_KL30_Wakeup_Init();
    Data_Engine_Speed_KL30_Wakeup_Init();
    LINE_IN_Init();
    Key_Wakeup_Init_EXample();
    Line_In_KL15_ON_Init();
    Fuel_KL30_Init();
94
    Backlight_KL30_Wakeup_Init();
95 96 97 98 99 100 101
    RTE_UART_Sleep_Init(UART_CH2);
    loc_config.u32UARTChEn            = 1U;
    loc_config.u32UARTbps             = 115200U;
    loc_config.pfnUARTConfirmCallBack = NULL;
    loc_config.pfnUARTReadMsgCallBack = Amt630hUartRecvData;
    UART_DeInit(UART2);
    RTE_UART_Init(UART_CH2, &loc_config); /* 配置串口,630H上电发送0x0D,但是底板没回复 */
102
    Amt630hInit();
时昊's avatar
时昊 committed
103
    TimerM_PWM_counter_Output_Init(TIMERM_COUNTER1, 20000, 64000000);
104
    TimerM_PWM_CH_Output_init(TIMERM_COUNTER1, TIMERM_CHB, ActiveLevel_High);
时昊's avatar
时昊 committed
105
    Telltales_Init();
时昊's avatar
时昊 committed
106 107
    Key_KL30_Init_EXample();    
    Menu_User_WAKEUP();
108
    Can_Init();
时昊's avatar
时昊 committed
109
    Protocol_KL30_Wakeup_Init();
110
    RTE_DEEPSLEEP_GPIO_Interrupt_Enable(KL15_AD_IN, Trigger_Rising);
111
    FaultCode_Init();
时昊's avatar
时昊 committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125
}

static void Power_LVP_Init(void)
{
    
}
static void Power_OVP_Init(void)
{
    
}

static void Power_IG_OFF_Init(void)
{
   Line_In_KL15_OFF_Init();
126
   TimerM_PWM_set_duty(TIMERM_COUNTER1, TIMERM_CHB, 0);
时昊's avatar
时昊 committed
127 128 129 130 131 132
}

static void Power_IG_ON_Init(void)
{
//    ClearODO_Flag = 0;
   Line_In_KL15_ON_Init();
133
   Telltales_KL15_Init();
时昊's avatar
时昊 committed
134
   Fuel_KL15_Init();
时昊's avatar
时昊 committed
135
   //RTE_GPIO_Set_Level(Blacklight_PWM, 1);
136
   FaultCode_Init();
137 138
   AMT630H_Animation_IGON();
//    Amt630hInit();
139
   AMT630H_GUI_SELFCHECK_STS_INIT();
140
   Menu_Service(MENU_KEY_TIMEOUT); /* 菜单回到初始界面 */
时昊's avatar
时昊 committed
141 142 143 144
}

static void Power_Sleep_Init(void)
{
时昊's avatar
时昊 committed
145
    TimerM_PWM_counter_Output_Init(TIMERM_COUNTER1, 20000, 0);
146
	rte_can_deinit(CAN_CH_0);
时昊's avatar
时昊 committed
147 148 149
	TMM0_Stop();
	TMM1_Stop();
    TMM_All_Stop();
150
    RTE_ADC_DeInit();
时昊's avatar
时昊 committed
151
    Gpio_Init(Gpio_Sleep_Init);
152
    AMT630H_Sleep();
153
    Analog_Signal_Conv_Stop();
154 155 156 157 158
    RTE_UART_Sleep_Init(UART_CH2);
    CGC_HSI_CFG_AS_FCLK();       /* 配置时钟生成控制器(cgc)以将内部高速振荡器(HSI)作为CPU系统时钟(FCLK)以及可能的其他硬件电路的时钟源。 */
    //SystemCoreClockUpdate();   /* 更新时钟频率,暂时用不上,先注释 */
    RTE_DEEPSLEEP_Enable();
    // RTE_CLOCK_Select_Sleep(); /* 这边代码是开启时钟,暂时用不上,先注释 */
时昊's avatar
时昊 committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
}

static Power_Status_em Power_Stay_ON(void)
{
    Power_Status_em u8PowerSts;
    u8PowerSts = EM_IGN_ON;

   if (SYS_OPR_STAT_IGN_OFF)
   {
       u8PowerSts = EM_IGN_OFF_Init;
   }

   if (SYS_OPR_STAT_HALT)
   {
       u8PowerSts = EM_IGN_LIMIT;
   }

    return u8PowerSts;
}
static Power_Status_em Power_Stay_OFF(void)
{
    Power_Status_em u8PowerSts;
    u8PowerSts = EM_IGN_OFF;

183 184 185 186 187 188
    if (SYS_OPR_STAT_IGN_ON)
    {
        u8PowerSts = EM_IGN_ON_Init;
    }
    else
    {
189
        if ((Common_GetIgnOffTime() < 20000))
190 191 192
        {
            u8PowerSts = EM_IGN_OFF;
        }
193 194 195 196
        else
        {
            u8PowerSts = EM_IGN_Sleep_Init;
        }
197
    }
时昊's avatar
时昊 committed
198 199 200 201 202 203

    return u8PowerSts;
}

static Power_Status_em Power_Stay_Protect(void)
{
204
    Power_Status_em u8PowerSts;
时昊's avatar
时昊 committed
205
//    static uint8_t  i;
206
    static uint8_t  Delaycnt;
时昊's avatar
时昊 committed
207 208


209 210
    /* 关闭所有外设,坪保留AD采集,电溝状思切�? */
    {
时昊's avatar
时昊 committed
211
        TimerM_PWM_set_duty(TIMERM_COUNTER1, TIMERM_CHB, 0);
212
        //CAN_TX_SetEnable(&CAN_CH0_CanMsgTxOp, CAN_N_TX_Disable);
213 214 215 216 217 218 219 220 221 222 223 224
    }
    u8PowerSts = EM_IGN_OFF_Init;
    /* 电溝模弝正常 */
    for ( ;; )
    {
        RTE_WDT_Clear();
        Gen_TimeDelay(2000u, 50u);
        Analog_Signal_Conv_Service();
        Sys_Status_Update_Service();
 
        if ( SYS_OPR_STAT_RUN )
        {
225
            Amt630hInit();
226
            //CAN_TX_SetEnable(&CAN_CH0_CanMsgTxOp, CAN_N_TX_Enable);
227 228 229 230
            return u8PowerSts;
        }
 
    }
时昊's avatar
时昊 committed
231 232 233 234 235 236
}

static Power_Status_em Power_Stay_Sleep(void)
{
    Power_Status_em u8PowerSts = EM_IGN_Sleep;

237 238 239 240 241 242 243 244 245
    while (1)
    {
        RTE_WDT_Clear();
        if (RTE_GPIO_Get_Level(KL15_AD_IN))/* KL15 */
        {
            u8PowerSts = EM_IGN_Wakeup;
            break;
        }
    }
时昊's avatar
时昊 committed
246 247 248 249 250

    return u8PowerSts;
}