move default task position

master
ThinkPad-T460P 2021-03-10 14:42:09 +08:00
parent c1c0b6bfbf
commit 237bf3fd38
3 changed files with 149 additions and 214 deletions

View File

@ -1,24 +0,0 @@
/*
* @Description:
* @Date: 2021-03-10 11:44:17
* @LastEditors: CK.Zh
* @LastEditTime: 2021-03-10 11:46:08
* @FilePath: \NaviKit_EC_stm32\App\Inc\th_default.h
*/
#ifndef __TH_DEFAULT_H__
#define __TH_DEFAULT_H__
#include "cmsis_os2.h"
/* Definitions for defaultTask */
osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes;
void StartDefaultTask(void *argument);
osTimerId_t IdleStateHoldTimerHandle;
const osTimerAttr_t IdleStateHoldTimer_attributes;
void IdleStateHoldTimerCallback(void *argument);
#endif

View File

@ -1,164 +0,0 @@
/*
* @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;
}
}

View File

@ -1,10 +1,3 @@
/*
* @Description:
* @Date: 2020-04-02 21:44:31
* @LastEditors: CK.Zh
* @LastEditTime: 2021-03-10 11:07:48
* @FilePath: \NaviKit_EC_stm32\Core\Src\freertos.c
*/
/* USER CODE BEGIN Header */
/**
******************************************************************************
@ -25,11 +18,10 @@
*/
//TODO:拆分该文件中的功能到不同的文件中,按照任务进行模块化拆分
#define LOG_TAG "RTOS"
#define LOG_TAG "TH-Default"
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
@ -38,7 +30,7 @@
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "timers.h"
#include <th_default.h>
#include "navikit.h"
#include <th_cdc.h>
#include <th_demo.h>
#include <th_elog.h>
@ -68,19 +60,33 @@
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */
osTimerId_t IdleStateHoldTimerHandle;
const osTimerAttr_t IdleStateHoldTimer_attributes = {
.name = "IdleStateHoldTimer"
};
/* USER CODE END Variables */
/* Definitions for defaultTask */
osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes = {
.name = "defaultTask",
.stack_size = 128 * 4,
.priority = (osPriority_t) osPriorityNormal,
};
/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */
void IdleStateHoldTimerCallback(void *argument);
bool isWakeUpFromReset() { return __HAL_PWR_GET_FLAG(PWR_FLAG_WU);}
/* USER CODE END FunctionPrototypes */
void StartDefaultTask(void *argument);
extern void MX_USB_DEVICE_Init(void);
void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */
/* Hook prototypes */
@ -192,24 +198,11 @@ void MX_FREERTOS_Init(void) {
/* Create the thread(s) */
/* creation of defaultTask */
/* creation of LedBlinkTask */
// LedBlinkTaskHandle = osThreadNew(StartLedBlinkTask, NULL, &LedBlinkTask_attributes);
/* creation of CoulombRead */
// CoulombReadHandle = osThreadNew(StartCoulombRead, NULL, &CoulombRead_attributes);
/* creation of PowerMonitTask */
// PowerMonitTaskHandle = osThreadNew(StartPowerMonitTask, NULL, &PowerMonitTask_attributes);
/* creation of EventDetect */
// EventDetectHandle = osThreadNew(StartEventDetect, NULL, &EventDetect_attributes);
defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);
/* USER CODE BEGIN RTOS_THREADS */
/* add threads, ... */
//Default Task
defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);
//Power--------------------------------------------
// PowerMonitTaskHandle = osThreadNew(StartPowerMonitTask, NULL, &PowerMonitTask_attributes);
@ -252,16 +245,146 @@ void MX_FREERTOS_Init(void) {
}
/* 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 */
}
/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */
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;
}
}
/* USER CODE END Application */