Motion_EC_Stm32_archived/App/Src/th_default.c

165 lines
4.8 KiB
C

/*
* @Description:
* @Date: 2021-03-10 11:43:58
* @LastEditors: CK.Zh
* @LastEditTime: 2021-03-10 11:46:56
* @FilePath: \NaviKit_EC_stm32\App\Src\th_default.c
*/
#define LOG_TAG "TH-Default"
#include <th_default.h>
#include "main.h"
#include "navikit.h"
extern void MX_USB_DEVICE_Init(void);
const osThreadAttr_t defaultTask_attributes = {
.name = "defaultTask",
.priority = (osPriority_t) osPriorityNormal,
.stack_size = 128 * 4
};
const osTimerAttr_t IdleStateHoldTimer_attributes = {
.name = "IdleStateHoldTimer"
};
bool isWakeUpFromReset() { return __HAL_PWR_GET_FLAG(PWR_FLAG_WU);}
/* USER CODE BEGIN Header_StartDefaultTask */
/**
* @brief Function implementing the defaultTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void *argument)
{
/* init code for USB_DEVICE */
MX_USB_DEVICE_Init();
/* USER CODE BEGIN StartDefaultTask */
osVersion_t osVersion;
char id_buf[20];
unsigned char id_size=20;
HAL_GPIO_WritePin(USB2_FS_ENUM_CTL_GPIO_Port,USB2_FS_ENUM_CTL_Pin, GPIO_PIN_SET);
if(isWakeUpFromReset()){//judge reset source form "wakeup event"
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_SB);
if(HAL_GPIO_ReadPin(SYS_POWER_BTN_GPIO_Port, SYS_POWER_BTN_Pin)==GPIO_PIN_SET){
if(HAL_GPIO_ReadPin(SYS_CUSTOM_BTN_GPIO_Port, SYS_CUSTOM_BTN_Pin)==GPIO_PIN_RESET)
NaviKit.sys.next_sta = dfu;
else{
NaviKit.sys.power_btn = true;
}
log_i("EC Reset source :Power Button WakeUP");
}else{
NaviKit.sys.next_sta = idle;
log_i("EC Reset source :RTC WakeUP");
enter_standby_state();
}
}else{//judge reset source "power on"
log_i("EC Reset source :PowerON");
if(HAL_GPIO_ReadPin(SYS_CUSTOM_BTN_GPIO_Port, SYS_CUSTOM_BTN_Pin)==GPIO_PIN_RESET){
log_d("EC next state isp");
NaviKit.sys.next_sta = isp;
}else{
NaviKit.sys.next_sta = idle;
}
// TaskBeep(50,1);
}
log_i("----------------------------------------------");
log_i("Copyright (c) Powered by www.autolabor.com.cn");
#ifdef DEBUG
log_i("EC Firmware: %s[DEBUG], build: %s, %s",APP_VERSION,__DATE__ ,__TIME__);
#else
log_i("EC Firmware: %s[RELEASE], build: %s, %s",APP_VERSION,__DATE__ ,__TIME__);
#endif
log_i("HAL Version: 0x%X ", HAL_GetHalVersion());
log_i("Revision ID: 0x%X ", HAL_GetREVID());
log_i("Device ID: 0x%X ", HAL_GetDEVID());
log_i("Chip UID: 0x%X%X%X ", HAL_GetUIDw0(),HAL_GetUIDw1(),HAL_GetUIDw2());
if(osOK == osKernelGetInfo(&osVersion,id_buf,id_size)){
log_i("OS Kernel Version: %u ", osVersion.kernel);
log_i("OS Kernel ID: %s ",id_buf);
}
log_i("OS Kernel Tick Frequence: %d ",osKernelGetTickFreq());
log_i("OS Kernel System Timer Frequence: %d ",osKernelGetSysTimerFreq());
log_i("Log Library Version: V%s",ELOG_SW_VERSION);
log_i("Core initial successfully");
log_i("----------------------------------------------");
/* Infinite loop */
for(;;)
{
// HAL_IWDG_Refresh(&hiwdg);
osDelay(20);
if(NaviKit.sys.sta != NaviKit.sys.next_sta){
switch(NaviKit.sys.next_sta){
case standby: {
enter_standby_state();
}break;
case idle: {
enter_idle_state(100);
NaviKit.sys.sta = NaviKit.sys.next_sta;
} break;
case run: {
// if(NaviKit.pmb.rails.main_pwr>19){
enter_run_state(100);
NaviKit.sys.sta = NaviKit.sys.next_sta;
// }else{
// NaviKit.sys.next_sta = NaviKit.sys.sta;
// TaskBeep(500,2);
// log_e(,"Main power not exist, retry after plug in it.");
// log_e("Main power is exception:%dmV",(uint32_t)(NaviKit.pmb.rails.main_pwr*1000));
// }
}break;
case sleep:{
enter_sleep_state(100);
NaviKit.sys.sta = NaviKit.sys.next_sta;
}break;
case dfu:{
// if(NaviKit.pmb.rails.main_pwr>19){
enter_dfu_state(100);
NaviKit.sys.sta = NaviKit.sys.next_sta;
// }else{
// NaviKit.sys.next_sta = NaviKit.sys.sta;
// TaskBeep(500,2);
// log_e(,"Main power not exist, retry after plug in it.");
// log_e("Main power is exception:%dmV",(uint32_t)(NaviKit.pmb.rails.main_pwr*1000));
// }
}break;
case isp:{
enter_isp_state();
NaviKit.sys.sta = NaviKit.sys.next_sta;
}break;
}
}
if(NaviKit.sys.sta == idle && NaviKit.sys.next_sta == idle){//idle state
// if((NaviKit.pmb.sta.chrg_stat1 && NaviKit.pmb.sta.chrg_stat2)
// || (NaviKit.pmb.sta.chrg_stat1 && !NaviKit.pmb.sta.chrg_stat2)){//Not Charge or float charge
// NaviKit.sys.next_sta = standby;
// }
if(!osTimerIsRunning(IdleStateHoldTimerHandle)){
osTimerStart(IdleStateHoldTimerHandle,5000);
}
}
}
/* USER CODE END StartDefaultTask */
}
void IdleStateHoldTimerCallback(void *argument){
if(NaviKit.sys.sta == idle && NaviKit.sys.next_sta == idle){
log_v("Idle state duration more than 5000ms.");
NaviKit.sys.next_sta = standby;
}
}