From fcda752b65e27ce42a109b373c1b44362bd3f095 Mon Sep 17 00:00:00 2001 From: ThinkPad-T460P Date: Fri, 12 Mar 2021 13:38:28 +0800 Subject: [PATCH] update --- App/Src/th_button.c | 4 ++-- App/Src/th_cdc.c | 6 +++--- App/Src/th_coulomb.c | 2 +- App/Src/th_demo.c | 8 ++++---- App/Src/th_elog.c | 2 +- App/Src/th_iwdg.c | 5 +++-- App/Src/th_led.c | 4 ++-- App/Src/th_power.c | 4 ++-- Core/Inc/main.h | 5 +++++ Core/Inc/navikit.h | 14 +++++++++----- Core/Src/freertos.c | 33 ++++++++++++++++++--------------- Core/Src/main.c | 23 +++++++++++++---------- Core/Src/navikit.c | 2 ++ README.md | 6 +++++- 14 files changed, 70 insertions(+), 48 deletions(-) diff --git a/App/Src/th_button.c b/App/Src/th_button.c index 8bb9a52..6c579bd 100644 --- a/App/Src/th_button.c +++ b/App/Src/th_button.c @@ -15,7 +15,7 @@ const osThreadAttr_t ButtonDetectTask_attributes = { .name = "ButtonDetectTask", .priority = (osPriority_t) osPriorityBelowNormal, - .stack_size = 128 * 4 + .stack_size = 128 * 8 }; //Timer @@ -45,7 +45,7 @@ void StartButtonDetectTask(void *argument) { /* USER CODE BEGIN StartButtonDetect */ /* Infinite loop */ - log_v("Start Button Detect Task"); + log_d("Start Button Detect Task"); for(;;) { //power button diff --git a/App/Src/th_cdc.c b/App/Src/th_cdc.c index 5c45cf7..4508ca2 100644 --- a/App/Src/th_cdc.c +++ b/App/Src/th_cdc.c @@ -15,8 +15,8 @@ extern uint32_t number_restart; //task attributes const osThreadAttr_t cdcMonitorTask_attributes = { .name = "cdcMonitorTask", - .priority = (osPriority_t) osPriorityNormal, - .stack_size = 128 * 4 + .priority = (osPriority_t) osPriorityBelowNormal7, + .stack_size = 128 * 8 }; //task instance @@ -24,7 +24,7 @@ void StartCdcMonitorTask(void *argument){ uint8_t port_restart_temp[64]={0}; uint32_t number_restart_temp =0; bool beep_flag = false; - log_v("Start CDC Monitor Task"); + log_d("Start CDC Monitor Task"); for(;;){ if(number_restart){ number_restart_temp = number_restart; diff --git a/App/Src/th_coulomb.c b/App/Src/th_coulomb.c index aaf8867..b90ad01 100644 --- a/App/Src/th_coulomb.c +++ b/App/Src/th_coulomb.c @@ -30,7 +30,7 @@ const osThreadAttr_t CoulombRead_attributes = { void StartCoulombRead(void *argument) { /* USER CODE BEGIN StartCoulombRead */ - log_v("Start Coulomb Read Task"); + log_d("Start Coulomb Read Task"); //写寄存器方法 coulomb_write_config_load(); coulomb_write_config_actual_to_raw(); diff --git a/App/Src/th_demo.c b/App/Src/th_demo.c index 00f5823..00e856b 100644 --- a/App/Src/th_demo.c +++ b/App/Src/th_demo.c @@ -23,17 +23,17 @@ const osThreadAttr_t DemoTask2_attributes = { }; void StartDemoTask1(void *argument){ uint8_t i=0; - log_v("Start Demo Task1"); + log_d("Start Demo Task1"); for(;;){ - log_v("demo task 1 [%d].",i++); + log_v("DemoTask1 counter [%d].",i++); osDelay(100); } } void StartDemoTask2(void *argument){ uint8_t i=0; - log_v("Start Demo Task2"); + log_d("Start Demo Task2"); for(;;){ - log_v("demo task 2 [%d].",i++); osDelay(100); + log_v("DemoTask2 counter [%d].",i++); } } diff --git a/App/Src/th_elog.c b/App/Src/th_elog.c index 3710f1f..18c7431 100644 --- a/App/Src/th_elog.c +++ b/App/Src/th_elog.c @@ -40,7 +40,7 @@ void StartElogInitTask(void *argument){ void StartElogFlushTask(void *argument){ uint8_t i=0; - log_v("Start Elog Flush Task"); + log_d("Start Elog Flush Task"); for(;;){ elog_flush(); osDelay(1); diff --git a/App/Src/th_iwdg.c b/App/Src/th_iwdg.c index e139427..54b779b 100644 --- a/App/Src/th_iwdg.c +++ b/App/Src/th_iwdg.c @@ -15,13 +15,14 @@ const osThreadAttr_t IWDGTask_attributes = { .name = "IWDGTask", .priority = (osPriority_t) osPriorityHigh, - .stack_size = 128 * 4 + .stack_size = 128 * 8 }; void StartIWDGTask(void *argument){ - log_v("Start IWDG Task"); + log_d("Start IWDG Task"); for(;;){ HAL_IWDG_Refresh(&hiwdg); + log_v("IWDG Counter Refreshed"); osDelay(1000); } } diff --git a/App/Src/th_led.c b/App/Src/th_led.c index 07cf763..a00549c 100644 --- a/App/Src/th_led.c +++ b/App/Src/th_led.c @@ -13,7 +13,7 @@ const osThreadAttr_t LedBlinkTask_attributes = { .name = "LedBlinkTask", - .priority = (osPriority_t) osPriorityBelowNormal, + .priority = (osPriority_t) osPriorityLow7, .stack_size = 128 * 8 }; @@ -28,7 +28,7 @@ void StartLedBlinkTask(void *argument) { /* USER CODE BEGIN StartLedBlinkTask */ /* Infinite loop */ - log_v("Start LED Blink Task"); + log_d("Start LED Blink Task"); for(;;) { diff --git a/App/Src/th_power.c b/App/Src/th_power.c index a02d238..811b6a4 100644 --- a/App/Src/th_power.c +++ b/App/Src/th_power.c @@ -15,7 +15,7 @@ const osThreadAttr_t PowerMonitTask_attributes = { .name = "PowerMonitTask", .priority = (osPriority_t) osPriorityBelowNormal, - .stack_size = 128 * 4 + .stack_size = 128 * 8 }; @@ -29,7 +29,7 @@ const osThreadAttr_t PowerMonitTask_attributes = { void StartPowerMonitTask(void *argument) { /* USER CODE BEGIN StartPowerMonitTask */ - log_v("Start Power Monitor Task"); + log_d("Start Power Monitor Task"); HAL_ADCEx_Calibration_Start(&hadc1); HAL_ADC_Start_DMA(&hadc1, (uint32_t*)&(NaviKit.pmb.rails.adc), ADC_CH_COUNT); osDelay(100); diff --git a/Core/Inc/main.h b/Core/Inc/main.h index 91c0b3a..d89a0d3 100644 --- a/Core/Inc/main.h +++ b/Core/Inc/main.h @@ -188,8 +188,13 @@ void Error_Handler(void); #define IIC1_SDA_GPIO_Port GPIOB /* USER CODE BEGIN Private defines */ +//define application software version #define APP_VERSION "v0.9.6" +// set iwdg enable or disable +//#define IWDG_ENABLE +#undef IWDG_ENABLE + /* USER CODE END Private defines */ #ifdef __cplusplus diff --git a/Core/Inc/navikit.h b/Core/Inc/navikit.h index 65dd71c..911c984 100644 --- a/Core/Inc/navikit.h +++ b/Core/Inc/navikit.h @@ -124,13 +124,17 @@ typedef struct extern NaviKit_t NaviKit; //device on board -enum Device_t{USB2_Port1,USB2_Port2,USB2_Port3,USB2_Port4,USB2_Port5,USB2_Port6, //USB2.0 Port +enum Device_t{USB2_Port1=0x00,USB2_Port2,USB2_Port3,USB2_Port4,USB2_Port5,USB2_Port6, //USB2.0 Port USB3_Port5,USB3_Port6,USB3_Port1,USB3_Port2,USB3_Port3,USB3_Port4, //USB3.0 Port - SOC_USB2_HUB,SOC_USB3_HUB,SOC_USB3_HOST,SOC_USB3_GEC,SOC_GE_SW, //SOC on Board - SYS_FAN1,SYS_FAN2,SYS_FAN3, //Fan on Board + + SOC_USB2_HUB=0x10,SOC_USB3_HUB,SOC_USB3_HOST,SOC_USB3_GEC,SOC_GE_SW, //SOC on Board + + SYS_FAN1=0x20,SYS_FAN2,SYS_FAN3, //Fan on Board SYS_RUN_LED,SYS_PWR_LED, //LED on Board - SOM_PWR_EN,SOM_DFU,SOM_RESET,SOM_SLEEP, //SOM Control - PMB_PS_ON}; //Power Management Board + + SOM_PWR_EN=0x30,SOM_DFU,SOM_RESET,SOM_SLEEP, //SOM Control + + PMB_PS_ON=0x40}; //Power Management Board void PWR_Enable(enum Device_t device,bool en,uint16_t delay); bool PWR_Status(enum Device_t device); diff --git a/Core/Src/freertos.c b/Core/Src/freertos.c index b9488ee..1730c5d 100644 --- a/Core/Src/freertos.c +++ b/Core/Src/freertos.c @@ -206,7 +206,9 @@ void MX_FREERTOS_Init(void) { /* USER CODE BEGIN RTOS_THREADS */ /* add threads, ... */ //IWDG-------------------------------------------- +#ifdef IWDG_ENABLE IWDGTaskHandle = osThreadNew(StartIWDGTask, NULL, &IWDGTask_attributes); +#endif //Power-------------------------------------------- // PowerMonitTaskHandle = osThreadNew(StartPowerMonitTask, NULL, &PowerMonitTask_attributes); @@ -217,7 +219,7 @@ void MX_FREERTOS_Init(void) { //LED-------------------------------------------- - LedBlinkTaskHandle = osThreadNew(StartLedBlinkTask, NULL, &LedBlinkTask_attributes); + LedBlinkTaskHandle = osThreadNew(StartLedBlinkTask, NULL, &LedBlinkTask_attributes); //CDC-------------------------------------------- @@ -235,8 +237,8 @@ void MX_FREERTOS_Init(void) { #endif //Demo-------------------------------------------- - DemoTask1Handle = osThreadNew(StartDemoTask1, NULL, &DemoTask1_attributes); - DemoTask2Handle = osThreadNew(StartDemoTask2, NULL, &DemoTask2_attributes); +DemoTask1Handle = osThreadNew(StartDemoTask1, NULL, &DemoTask1_attributes); +DemoTask2Handle = osThreadNew(StartDemoTask2, NULL, &DemoTask2_attributes); /* USER CODE END RTOS_THREADS */ @@ -276,14 +278,14 @@ osVersion_t osVersion; else{ NaviKit.sys.power_btn = true; } - log_i("EC Reset source :Power Button WakeUP"); + log_i("EC Reset source: [Power Button WakeUP]"); }else{ NaviKit.sys.next_sta = idle; - log_i("EC Reset source :RTC WakeUP"); + log_i("EC Reset source: [RTC WakeUP]"); enter_standby_state(); } }else{//judge reset source "power on" - log_i("EC Reset source :PowerON"); + 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; @@ -302,16 +304,16 @@ osVersion_t osVersion; #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()); + 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 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("OS Kernel Tick Frequency: %d Hz",osKernelGetTickFreq()); + log_i("System Timer Frequency : %d Hz",osKernelGetSysTimerFreq()); + log_i("Log Library Version : V%s",ELOG_SW_VERSION); log_i("Core initial successfully"); @@ -368,7 +370,8 @@ osVersion_t osVersion; // } if(!osTimerIsRunning(IdleStateHoldTimerHandle)){ -// osTimerStart(IdleStateHoldTimerHandle,5000); + osTimerStart(IdleStateHoldTimerHandle,5000); + log_i("Idle state hold timer started."); } } @@ -382,7 +385,7 @@ osVersion_t osVersion; void IdleStateHoldTimerCallback(void *argument){ if(NaviKit.sys.sta == idle && NaviKit.sys.next_sta == idle){ - log_v("Idle state duration more than 5000ms."); + log_i("Idle state duration more than 5000ms."); NaviKit.sys.next_sta = standby; } } diff --git a/Core/Src/main.c b/Core/Src/main.c index bc9f09a..35fc979 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -97,7 +97,7 @@ int main(void) MX_GPIO_Init(); MX_DMA_Init(); MX_I2C1_Init(); - MX_IWDG_Init(); +// MX_IWDG_Init(); MX_UART4_Init(); MX_ADC1_Init(); MX_USART1_UART_Init(); @@ -107,16 +107,19 @@ int main(void) MX_NVIC_Init(); /* USER CODE BEGIN 2 */ //print log before kernel start -// uint8_t log_head[]="Autolabor EC Booting...\r\n"; -// HAL_UART_Transmit_DMA(&huart1, log_head, sizeof(log_head)); - if( hiwdg.Instance->SR & IWDG_KEY_ENABLE){ - uint8_t log_head[]="iwdg enable\r\n"; - HAL_UART_Transmit_DMA(&huart1, log_head, sizeof(log_head)); - }else{ - uint8_t log_head[]="iwdg disable\r\n"; - HAL_UART_Transmit_DMA(&huart1, log_head, sizeof(log_head)); - } +#ifdef IWDG_ENABLE + __HAL_DBGMCU_FREEZE_IWDG(); //stop iwdg counter while cpu pause by debug mode +#endif + +#ifdef DEBUG + HAL_DBGMCU_EnableDBGSleepMode(); + HAL_DBGMCU_EnableDBGStopMode(); + HAL_DBGMCU_EnableDBGStandbyMode(); +#endif + + uint8_t log_head[]="Autolabor EC Booting...\r\n"; + HAL_UART_Transmit_DMA(&huart1, log_head, sizeof(log_head)); /* USER CODE END 2 */ /* Init scheduler */ diff --git a/Core/Src/navikit.c b/Core/Src/navikit.c index 46cac33..2cfdfe5 100644 --- a/Core/Src/navikit.c +++ b/Core/Src/navikit.c @@ -12,6 +12,7 @@ * Author: oarap */ +#define LOG_TAG "NAVIKIT" #include "navikit.h" #include "cmsis_os2.h" // ::CMSIS:RTOS2 @@ -254,6 +255,7 @@ void PWR_Enable(enum Device_t device,bool en,uint16_t delay){ case PMB_PS_ON: {HAL_GPIO_WritePin(PMB_PS_ON_GPIO_Port, PMB_PS_ON_Pin, en); }break; default: {log_e("PWR_Enable device parameter is invalid."); }break; } + log_v("Device 0x%X has been %s",device,en ? "enabled" : "disabled"); osDelay(delay); } diff --git a/README.md b/README.md index ae0bcbd..a29477f 100644 --- a/README.md +++ b/README.md @@ -27,12 +27,16 @@ * 电池管理部分 # Note +* 官方的HAL中USB库已知问题 在使用usb的时候, 文件`stm32f1xx_hal_pcd.c` 函数`void HAL_PCD_IRQHandler(PCD_HandleTypeDef *hpcd)` 代码段`HAL_PCD_SuspendCallback(hpcd); ` 务必屏蔽,否则导致枚举失败,提示获取设备描述符失败。 - +* FreeRTOS调试指南 + * 莫名进入`HardFault`错误 + * 一般是堆栈大小不足导致,可通过`Static Stack Analyzer`中的`Call graph`来分析每个任务所占用的栈大小(并且以大纲视图看出任务中占用栈较大的函数),从而修改每个任务的`stack_size`为合适大小 + * 如`elog_output()`该函数占用栈大小为400字节,如果有任务调用了该日志输出接口,那么freertos的默认`128*4=512`字节显然非常容易溢出 # 维护指南 * 为提高可移植性、可维护性、可阅读性,开发(应用层)时尽量使用`CMSIS_OS2`的抽象接口,特殊情况时再考虑使用RTOS的原生接口