diff --git a/.cproject b/.cproject index 210c5c8..e07bc2f 100644 --- a/.cproject +++ b/.cproject @@ -43,7 +43,7 @@ - + @@ -98,8 +98,8 @@ - + @@ -143,7 +143,7 @@ - + diff --git a/.settings/stm32cubeide.project.prefs b/.settings/stm32cubeide.project.prefs index 42c3797..6d54323 100644 --- a/.settings/stm32cubeide.project.prefs +++ b/.settings/stm32cubeide.project.prefs @@ -1,4 +1,4 @@ -2F62501ED4689FB349E356AB974DBE57=66ECD72EB9ECEB12193E2107C4BA39FF -8DF89ED150041C4CBC7CB9A9CAA90856=66ECD72EB9ECEB12193E2107C4BA39FF -DC22A860405A8BF2F2C095E5B6529F12=F6E7387302D54A097C31AB36FAAFE226 +2F62501ED4689FB349E356AB974DBE57=7721402F3CE8DC8D3A6A8AE537EFAC0B +8DF89ED150041C4CBC7CB9A9CAA90856=7721402F3CE8DC8D3A6A8AE537EFAC0B +DC22A860405A8BF2F2C095E5B6529F12=02D9A7110C849E46466CD5CF8816F30D eclipse.preferences.version=1 diff --git a/Core/Inc/FreeRTOSConfig.h b/Core/Inc/FreeRTOSConfig.h index 9e35702..7f4b630 100644 --- a/Core/Inc/FreeRTOSConfig.h +++ b/Core/Inc/FreeRTOSConfig.h @@ -162,6 +162,8 @@ standard names. */ /* USER CODE BEGIN Defines */ /* Section where parameter definitions can be added (for instance, to override default ones in FreeRTOS.h) */ +#define configUSE_TRACE_FACILITY 1 +#define configUSE_STATS_FORMATTING_FUNCTIONS 1 /* USER CODE END Defines */ #if defined(__ICCARM__) || defined(__CC_ARM) || defined(__GNUC__) diff --git a/Core/Inc/main.h b/Core/Inc/main.h index 2ef567e..d784fff 100644 --- a/Core/Inc/main.h +++ b/Core/Inc/main.h @@ -183,7 +183,9 @@ void ISP_Judge(); #define IIC1_SDA_Pin GPIO_PIN_9 #define IIC1_SDA_GPIO_Port GPIOB /* USER CODE BEGIN Private defines */ -#define ISP_Address 0x1fffb000 //In-System-Program address in flash +#define ISP_Address 0x1fffb000 //In-System-Program address in flash +#define ISP_BKP_DR RTC_BKP_DR1 +#define BKP_DR_Jump_to_ISP 0xa5a5 //jump to isp while ISP_BKP_DR's value equal this /* USER CODE END Private defines */ #ifdef __cplusplus diff --git a/Core/Inc/navikit.h b/Core/Inc/navikit.h index c04b81b..1801107 100644 --- a/Core/Inc/navikit.h +++ b/Core/Inc/navikit.h @@ -1,3 +1,10 @@ +/* + * @Description: + * @Date: 2020-12-16 18:05:14 + * @LastEditors: CK.Zh + * @LastEditTime: 2020-12-25 17:52:50 + * @FilePath: \NaviKit_stm32\Core\Inc\navikit.h + */ /* * navikit.h * @@ -18,21 +25,24 @@ #define ADC_CH_COUNT 5+2 //number of adc channels (include temp sensor and vrefint adc_in17) +typedef enum +{ + idle, //only mcu runing + run, //all function runing + sleep, //SOCs and FANs are stop + dfu, //device firmware update for SOM + isp //in system program for EC +}state_t; typedef struct { struct{ - enum { - shutdown, //only mcu runing - running, //all function runing - sleep, //SOCs and FANs are stop - dfu, //device firmware update for SOM - isp //in system program for BIOS - }sta,next_sta; //power state machine - bool power_btn; //power button - bool custom_btn;//custom button - bool pwr_led;//power led (on switch) - bool run_led;//run led (on board) + state_t sta,next_sta; //power state machine + bool power_btn; //power button pressed flag + bool custom_btn;//custom button pressed flag + // bool pwr_led;//power led (on switch) + // bool run_led;//run led (on board) + bool dbg_msg_out;//enable/disable debug message output }sys; struct{ bool mod_sleep; @@ -106,24 +116,37 @@ typedef struct // float sensor_3; // }temp; + + }NaviKit_t; extern NaviKit_t NaviKit; +//device on board +enum Device_t{USB2_Port1,USB2_Port2,USB2_Port3,USB2_Port4,USB2_Port5,USB2_Port6, + USB3_Port1,USB3_Port2,USB3_Port3,USB3_Port4,USB3_Port5,USB3_Port6, + SOC_USB2_HUB,SOC_USB3_HUB,SOC_USB3_HOST,SOC_USB3_GEC,SOC_GE_SW, + SYS_FAN1,SYS_FAN2,SYS_FAN3, + SYS_RUN_LED,SYS_PWR_LED, + SOM_PWR_EN,SOM_DFU, + PMB_PS_ON}; + +void PWR_Enable(enum Device_t device,bool en,uint16_t delay); +bool PWR_Status(enum Device_t device); + void NaviKit_var_init(); //power state machine switch function void enter_isp_state(); -void enter_runing_state(); -void enter_shutdown_state(); -void enter_sleep_state(); -void enter_dfu_state(); -void Beep(uint32_t time_ms); +void enter_run_state(uint16_t delay); +void enter_idle_state(uint16_t delay); +void enter_sleep_state(uint16_t delay); +void enter_dfu_state(uint16_t delay); +//non-blocking beep function with os delay +void TaskBeep(uint32_t time_ms, uint8_t n); void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc); +void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin); -void osEnterRuningState(); -void osEnterSleepState(); - #endif /* INC_NAVIKIT_H_ */ diff --git a/Core/Inc/stm32f1xx_it.h b/Core/Inc/stm32f1xx_it.h index a4b6409..aaa5ee8 100644 --- a/Core/Inc/stm32f1xx_it.h +++ b/Core/Inc/stm32f1xx_it.h @@ -66,6 +66,7 @@ void TIM1_UP_IRQHandler(void); void I2C1_EV_IRQHandler(void); void I2C1_ER_IRQHandler(void); void USART1_IRQHandler(void); +void RTC_Alarm_IRQHandler(void); void UART4_IRQHandler(void); void OTG_FS_IRQHandler(void); /* USER CODE BEGIN EFP */ diff --git a/Core/Src/adc.c b/Core/Src/adc.c index 46aa2ee..a8fafab 100644 --- a/Core/Src/adc.c +++ b/Core/Src/adc.c @@ -40,7 +40,7 @@ void MX_ADC1_Init(void) */ hadc1.Instance = ADC1; hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE; - hadc1.Init.ContinuousConvMode = ENABLE; + hadc1.Init.ContinuousConvMode = DISABLE; hadc1.Init.DiscontinuousConvMode = DISABLE; hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; diff --git a/Core/Src/freertos.c b/Core/Src/freertos.c index ebdb611..71f8210 100644 --- a/Core/Src/freertos.c +++ b/Core/Src/freertos.c @@ -1,3 +1,10 @@ +/* + * @Description: + * @Date: 2020-04-02 21:44:31 + * @LastEditors: CK.Zh + * @LastEditTime: 2021-01-04 17:49:58 + * @FilePath: \NaviKit_stm32\Core\Src\freertos.c + */ /* USER CODE BEGIN Header */ /** ****************************************************************************** @@ -26,6 +33,7 @@ /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ +#include "timers.h" #include "iwdg.h" #include "gpio.h" #include "usart.h" @@ -55,6 +63,17 @@ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN Variables */ + +osEventFlagsId_t buttonEventHandle; +const osEventFlagsAttr_t buttonEvent_attributes = { + .name = "buttonEvent" +}; +osThreadId_t StateSwitchTaskHandle; +const osThreadAttr_t StateSwitchTask_attributes = { + .name = "StateSwitchTask", + .priority = (osPriority_t) osPriorityNormal, + .stack_size = 128 * 4 +}; /* USER CODE END Variables */ /* Definitions for defaultTask */ osThreadId_t defaultTaskHandle; @@ -70,20 +89,6 @@ const osThreadAttr_t LedBlinkTask_attributes = { .priority = (osPriority_t) osPriorityLow, .stack_size = 128 * 4 }; -/* Definitions for IWDGRefreshTask */ -osThreadId_t IWDGRefreshTaskHandle; -const osThreadAttr_t IWDGRefreshTask_attributes = { - .name = "IWDGRefreshTask", - .priority = (osPriority_t) osPriorityHigh, - .stack_size = 128 * 4 -}; -/* Definitions for EventDetect */ -osThreadId_t EventDetectHandle; -const osThreadAttr_t EventDetect_attributes = { - .name = "EventDetect", - .priority = (osPriority_t) osPriorityLow, - .stack_size = 128 * 4 -}; /* Definitions for CoulombRead */ osThreadId_t CoulombReadHandle; const osThreadAttr_t CoulombRead_attributes = { @@ -91,13 +96,6 @@ const osThreadAttr_t CoulombRead_attributes = { .priority = (osPriority_t) osPriorityLow, .stack_size = 128 * 4 }; -/* Definitions for StateSwitchTask */ -osThreadId_t StateSwitchTaskHandle; -const osThreadAttr_t StateSwitchTask_attributes = { - .name = "StateSwitchTask", - .priority = (osPriority_t) osPriorityLow, - .stack_size = 128 * 4 -}; /* Definitions for PowerMonitTask */ osThreadId_t PowerMonitTaskHandle; const osThreadAttr_t PowerMonitTask_attributes = { @@ -105,20 +103,41 @@ const osThreadAttr_t PowerMonitTask_attributes = { .priority = (osPriority_t) osPriorityLow, .stack_size = 128 * 4 }; +/* Definitions for EventDetect */ +osThreadId_t EventDetectHandle; +const osThreadAttr_t EventDetect_attributes = { + .name = "EventDetect", + .priority = (osPriority_t) osPriorityLow, + .stack_size = 128 * 4 +}; +/* Definitions for PwrBtnTimer */ +osTimerId_t PwrBtnTimerHandle; +const osTimerAttr_t PwrBtnTimer_attributes = { + .name = "PwrBtnTimer" +}; +/* Definitions for CustBtnTimer */ +osTimerId_t CustBtnTimerHandle; +const osTimerAttr_t CustBtnTimer_attributes = { + .name = "CustBtnTimer" +}; /* Private function prototypes -----------------------------------------------*/ /* USER CODE BEGIN FunctionPrototypes */ -void osEnterSleepState(); -void osEnterRuningState(); +//void osEnterSleepState(); +//void osEnterRuningState(); +void StartStateSwitchTask(void *argument); +void StartChangeStateTask(state_t target_sta); +bool isWakeUpReset() { return __HAL_PWR_GET_FLAG(PWR_FLAG_WU);} + /* USER CODE END FunctionPrototypes */ void StartDefaultTask(void *argument); void StartLedBlinkTask(void *argument); -void StartIWDGRefreshTask(void *argument); -void StartEventDetect(void *argument); void StartCoulombRead(void *argument); -void StartStateSwitchTask(void *argument); void StartPowerMonitTask(void *argument); +void StartEventDetect(void *argument); +void PwrBtnTimerCallback(void *argument); +void CustBtnTimerCallback(void *argument); extern void MX_USB_DEVICE_Init(void); void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */ @@ -139,7 +158,7 @@ __weak void configureTimerForRunTimeStats(void) __weak unsigned long getRunTimeCounterValue(void) { -return 0; + return osKernelGetTickCount(); } /* USER CODE END 1 */ @@ -182,11 +201,13 @@ void vApplicationStackOverflowHook(xTaskHandle xTask, signed char *pcTaskName) __weak void PreSleepProcessing(uint32_t *ulExpectedIdleTime) { /* place for user code */ + } __weak void PostSleepProcessing(uint32_t *ulExpectedIdleTime) { /* place for user code */ + } /* USER CODE END PREPOSTSLEEP */ @@ -208,6 +229,13 @@ void MX_FREERTOS_Init(void) { /* add semaphores, ... */ /* USER CODE END RTOS_SEMAPHORES */ + /* Create the timer(s) */ + /* creation of PwrBtnTimer */ + PwrBtnTimerHandle = osTimerNew(PwrBtnTimerCallback, osTimerOnce, NULL, &PwrBtnTimer_attributes); + + /* creation of CustBtnTimer */ + CustBtnTimerHandle = osTimerNew(CustBtnTimerCallback, osTimerOnce, NULL, &CustBtnTimer_attributes); + /* USER CODE BEGIN RTOS_TIMERS */ /* start timers, add new ones, ... */ /* USER CODE END RTOS_TIMERS */ @@ -223,28 +251,24 @@ void MX_FREERTOS_Init(void) { /* creation of LedBlinkTask */ LedBlinkTaskHandle = osThreadNew(StartLedBlinkTask, NULL, &LedBlinkTask_attributes); - /* creation of IWDGRefreshTask */ -// IWDGRefreshTaskHandle = osThreadNew(StartIWDGRefreshTask, NULL, &IWDGRefreshTask_attributes); - - /* creation of EventDetect */ - EventDetectHandle = osThreadNew(StartEventDetect, NULL, &EventDetect_attributes); - /* creation of CoulombRead */ CoulombReadHandle = osThreadNew(StartCoulombRead, NULL, &CoulombRead_attributes); - /* creation of StateSwitchTask */ - StateSwitchTaskHandle = osThreadNew(StartStateSwitchTask, NULL, &StateSwitchTask_attributes); - /* creation of PowerMonitTask */ PowerMonitTaskHandle = osThreadNew(StartPowerMonitTask, NULL, &PowerMonitTask_attributes); + /* creation of EventDetect */ + EventDetectHandle = osThreadNew(StartEventDetect, NULL, &EventDetect_attributes); + /* USER CODE BEGIN RTOS_THREADS */ /* add threads, ... */ + StateSwitchTaskHandle = osThreadNew(StartStateSwitchTask, NULL, &defaultTask_attributes); /* USER CODE END RTOS_THREADS */ /* USER CODE BEGIN RTOS_EVENTS */ /* add events, ... */ + buttonEventHandle = osEventFlagsNew(&buttonEvent_attributes); /* USER CODE END RTOS_EVENTS */ } @@ -259,14 +283,38 @@ void MX_FREERTOS_Init(void) { void StartDefaultTask(void *argument) { /* init code for USB_DEVICE */ -// MX_USB_DEVICE_Init(); + MX_USB_DEVICE_Init(); /* USER CODE BEGIN StartDefaultTask */ - osVersion_t osVersion; - char id_buf[20]; - unsigned char id_size=20; + 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); - Beep(50); + HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN1); + if(isWakeUpReset()){//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){ + NaviKit.sys.next_sta = run; + Log(info,"EC Reset source :Power Button WakeUP"); + }else{ + NaviKit.sys.next_sta = idle; + Log(info,"EC Reset source :RTC WakeUP"); +// HAL_IWDG_Refresh(&hiwdg); +// HAL_PWR_EnterSTANDBYMode(); + } + }else{//judge reset source "power on" + Log(info,"EC Reset source :PowerON"); + if(HAL_GPIO_ReadPin(SYS_CUSTOM_BTN_GPIO_Port, SYS_CUSTOM_BTN_Pin)==GPIO_PIN_RESET){ + Log(debug,"EC next state isp"); + NaviKit.sys.next_sta = isp; + }else{ + NaviKit.sys.next_sta = idle; + } + } Log(info,"----------------------------------------------"); Log(info,"Copyright (c) Powered by www.autolabor.com.cn"); @@ -278,17 +326,42 @@ void StartDefaultTask(void *argument) Log(info,"Chip UID: %u%u%u ", HAL_GetUIDw0(),HAL_GetUIDw1(),HAL_GetUIDw2()); if(osOK == osKernelGetInfo(&osVersion,id_buf,id_size)){ - Log(info,"OS Kernel Version: %u ", osVersion.kernel ); - Log(info,"OS Kernel ID: %s ",id_buf ); + Log(info,"OS Kernel Version: %u ", osVersion.kernel); + Log(info,"OS Kernel ID: %s ",id_buf); } - Log(info,"Core initial successfully"); Log(info,"----------------------------------------------"); -// osEnterSleepState(); + + TaskBeep(50,1); /* Infinite loop */ for(;;) { - osDelay(1000); +// HAL_IWDG_Refresh(&hiwdg); + osDelay(100); + if(NaviKit.sys.sta != NaviKit.sys.next_sta){ + switch(NaviKit.sys.next_sta){ + case idle: { + enter_idle_state(100); + NaviKit.sys.sta = NaviKit.sys.next_sta; + } break; + case run: { + enter_run_state(100); + NaviKit.sys.sta = NaviKit.sys.next_sta; + }break; + case sleep:{ + enter_sleep_state(100); + NaviKit.sys.sta = NaviKit.sys.next_sta; + }break; + case dfu:{ + enter_dfu_state(100); + NaviKit.sys.sta = NaviKit.sys.next_sta; + }break; + case isp:{ + enter_isp_state(); + NaviKit.sys.sta = NaviKit.sys.next_sta; + }break; + } + } } /* USER CODE END StartDefaultTask */ @@ -307,81 +380,52 @@ void StartLedBlinkTask(void *argument) /* Infinite loop */ for(;;) { + osDelay(1000); switch(NaviKit.sys.sta){ - case running: - { - if(HAL_GPIO_ReadPin(SYS_POWER_LED_CTL_GPIO_Port,SYS_POWER_LED_CTL_Pin)) - HAL_GPIO_WritePin(SYS_POWER_LED_CTL_GPIO_Port,SYS_POWER_LED_CTL_Pin,GPIO_PIN_RESET);//turn on power led - - if(HAL_GPIO_ReadPin(SYS_RUN_LED_CTL_GPIO_Port,SYS_RUN_LED_CTL_Pin) == GPIO_PIN_SET)//sys run led is off - { - HAL_GPIO_WritePin(SYS_RUN_LED_CTL_GPIO_Port,SYS_RUN_LED_CTL_Pin,GPIO_PIN_RESET);//turn on sys run led - osDelay(50); - } - else - { - HAL_GPIO_WritePin(SYS_RUN_LED_CTL_GPIO_Port,SYS_RUN_LED_CTL_Pin,GPIO_PIN_SET);//turn off sys run led - osDelay(300); - } - }break; - case shutdown: - { - if(!HAL_GPIO_ReadPin(SYS_POWER_LED_CTL_GPIO_Port,SYS_POWER_LED_CTL_Pin)) - HAL_GPIO_WritePin(SYS_POWER_LED_CTL_GPIO_Port,SYS_POWER_LED_CTL_Pin,GPIO_PIN_SET);//turn off power led + case run: { + if(!PWR_Status(SYS_PWR_LED)){ + PWR_Enable(SYS_PWR_LED,true,100);//turn on power led + } - if(HAL_GPIO_ReadPin(SYS_RUN_LED_CTL_GPIO_Port,SYS_RUN_LED_CTL_Pin) == GPIO_PIN_SET)//sys run led is off - { - HAL_GPIO_WritePin(SYS_RUN_LED_CTL_GPIO_Port,SYS_RUN_LED_CTL_Pin,GPIO_PIN_RESET);//turn on sys run led - osDelay(20); - } - else - { - HAL_GPIO_WritePin(SYS_RUN_LED_CTL_GPIO_Port,SYS_RUN_LED_CTL_Pin,GPIO_PIN_SET);//turn off sys run led - osDelay(2000); - } - }break; - case sleep: - {//sleep mode - for(uint16_t i=0;i<25;i++) - {//20 light level - for(uint16_t j=0;j<3;j++) - {//the time length of every light level - HAL_GPIO_WritePin(SYS_POWER_LED_CTL_GPIO_Port,SYS_POWER_LED_CTL_Pin,GPIO_PIN_SET); - HAL_GPIO_WritePin(SYS_RUN_LED_CTL_GPIO_Port,SYS_RUN_LED_CTL_Pin,GPIO_PIN_SET); - osDelay(i); - HAL_GPIO_WritePin(SYS_POWER_LED_CTL_GPIO_Port,SYS_POWER_LED_CTL_Pin,GPIO_PIN_RESET); - HAL_GPIO_WritePin(SYS_RUN_LED_CTL_GPIO_Port,SYS_RUN_LED_CTL_Pin,GPIO_PIN_RESET); - osDelay(25-i); - } - } - for(uint16_t i=0;i<25;i++) - { - for(uint16_t j=0;j<3;j++) - { - HAL_GPIO_WritePin(SYS_POWER_LED_CTL_GPIO_Port,SYS_POWER_LED_CTL_Pin,GPIO_PIN_SET); - HAL_GPIO_WritePin(SYS_RUN_LED_CTL_GPIO_Port,SYS_RUN_LED_CTL_Pin,GPIO_PIN_SET); - osDelay(25-i); - HAL_GPIO_WritePin(SYS_POWER_LED_CTL_GPIO_Port,SYS_POWER_LED_CTL_Pin,GPIO_PIN_RESET); - HAL_GPIO_WritePin(SYS_RUN_LED_CTL_GPIO_Port,SYS_RUN_LED_CTL_Pin,GPIO_PIN_RESET); - osDelay(i); - } - } - - }break; - case dfu: - { - if(HAL_GPIO_ReadPin(SYS_POWER_LED_CTL_GPIO_Port,SYS_POWER_LED_CTL_Pin) == GPIO_PIN_SET)//power led is off - { - HAL_GPIO_WritePin(SYS_POWER_LED_CTL_GPIO_Port,SYS_POWER_LED_CTL_Pin,GPIO_PIN_RESET);//turn on power led - HAL_GPIO_WritePin(SYS_RUN_LED_CTL_GPIO_Port,SYS_RUN_LED_CTL_Pin,GPIO_PIN_RESET);//turn on run led - osDelay(300); + if(!PWR_Status(SYS_RUN_LED)){//sys run led is off + PWR_Enable(SYS_RUN_LED,true,50);//turn on sys run led + PWR_Enable(SYS_RUN_LED,false,0);//turn off sys run led } + }break; + case idle:{ + if(PWR_Status(SYS_PWR_LED)) + PWR_Enable(SYS_PWR_LED,false,0);//turn on power led//turn off power led + + if(PWR_Status(SYS_RUN_LED))//sys run led is off + PWR_Enable(SYS_RUN_LED,true,20);//turn on sys run led else - { - HAL_GPIO_WritePin(SYS_POWER_LED_CTL_GPIO_Port,SYS_POWER_LED_CTL_Pin,GPIO_PIN_SET);//turn off sys run led - HAL_GPIO_WritePin(SYS_RUN_LED_CTL_GPIO_Port,SYS_RUN_LED_CTL_Pin,GPIO_PIN_SET);//turn off run led - osDelay(300); + PWR_Enable(SYS_RUN_LED,false,3000);//turn off sys run led + }break; + case sleep:{//sleep mode + for(uint16_t i=0;i<25;i++){//20 light level + for(uint16_t j=0;j<3;j++){//the time length of every light level + PWR_Enable(SYS_PWR_LED,false,0); + PWR_Enable(SYS_RUN_LED,false,0); + osDelay(i); + PWR_Enable(SYS_PWR_LED,true,0); + PWR_Enable(SYS_RUN_LED,true,0); + osDelay(25-i); + } } + for(uint16_t i=0;i<25;i++){ + for(uint16_t j=0;j<3;j++){ + PWR_Enable(SYS_PWR_LED,false,0); + PWR_Enable(SYS_RUN_LED,false,0); + osDelay(25-i); + PWR_Enable(SYS_PWR_LED,true,0); + PWR_Enable(SYS_RUN_LED,true,0); + osDelay(i); + } + } + }break; + case dfu:{ + PWR_Enable(SYS_PWR_LED,!PWR_Status(SYS_PWR_LED),150); + PWR_Enable(SYS_RUN_LED,!PWR_Status(SYS_PWR_LED),150); }break; case isp: break; @@ -390,104 +434,6 @@ void StartLedBlinkTask(void *argument) /* USER CODE END StartLedBlinkTask */ } -/* USER CODE BEGIN Header_StartIWDGRefreshTask */ -/** -* @brief Function implementing the IWDGRefreshTask thread. -* @param argument: Not used -* @retval None -*/ -/* USER CODE END Header_StartIWDGRefreshTask */ -void StartIWDGRefreshTask(void *argument) -{ - /* USER CODE BEGIN StartIWDGRefreshTask */ - /* Infinite loop */ - for(;;) - { - HAL_IWDG_Refresh(&hiwdg); - osDelay(1000); - } - /* USER CODE END StartIWDGRefreshTask */ -} - -/* USER CODE BEGIN Header_StartEventDetect */ -/** -* @brief Function implementing the EventDetect thread. -* @param argument: Not used -* @retval None -*/ -/* USER CODE END Header_StartEventDetect */ -void StartEventDetect(void *argument) -{ - /* USER CODE BEGIN StartEventDetect */ - osDelay(100); - if(HAL_GPIO_ReadPin(SYS_CUSTOM_BTN_GPIO_Port,SYS_CUSTOM_BTN_Pin) == GPIO_PIN_RESET) - { - Log(debug,"Custom button has been pushed."); - NaviKit.sys.next_sta = isp; - } - /* Infinite loop */ - for(;;) - { - - if(NaviKit.sys.power_btn == true) - {//power btn has been pushed - uint8_t count =0; - while(NaviKit.sys.power_btn && count<=10){ - osDelay(100); - count ++; - } - if(count >10) - {//power btn has been pushed more than 1000 ms - // while(NaviKit.sys.power_btn == true);//wait to release button - switch(NaviKit.sys.sta){ - case running:{//system is running now, user request to shutdown - NaviKit.sys.next_sta = shutdown; - }break; - case shutdown:{//system is shutdown now , user request to power on - NaviKit.sys.next_sta = running; - }break; - case dfu:{ - NaviKit.sys.next_sta = shutdown; - }break; - case sleep:{ - NaviKit.sys.next_sta = running; - }break; - } - } - else - { - //sleep mode is unused - // if(NaviKit.sys.sta == runing) - // NaviKit.sys.sta = sleep; - // else if(NaviKit.sys.sta == sleep) - // NaviKit.sys.sta = runing; - } - } - if(NaviKit.sys.custom_btn == true ) - {//custom button has been pushed - uint8_t count =0; - while(NaviKit.sys.custom_btn && count<=20){ - osDelay(100); - count ++; - } - if(count > 20 ) - {//custom button has been pushed over 1000 ms - if((NaviKit.sys.power_btn == true) && (NaviKit.sys.sta == shutdown))//into dfu state - NaviKit.sys.next_sta = dfu; - else//TODO: force restart - NaviKit.sys.next_sta = shutdown; -// if(NaviKit.sys.sta == runing) -// NaviKit.sys.next_sta = shutdown; -// else if(NaviKit.sys.sta == shutdown) -// NaviKit.sys.next_sta = runing; - } - } - - osDelay(10); - } - /* USER CODE END StartEventDetect */ -} - /* USER CODE BEGIN Header_StartCoulombRead */ /** * @brief Function implementing the CoulombRead thread. @@ -512,7 +458,7 @@ void StartCoulombRead(void *argument) { coulomb_read_status_and_config(); coulomb_read_status_raw_to_actual(); - if(NaviKit.sys.sta == running) + if(NaviKit.sys.sta == run) osDelay(500); else osDelay(5000); @@ -520,56 +466,6 @@ void StartCoulombRead(void *argument) /* USER CODE END StartCoulombRead */ } -/* USER CODE BEGIN Header_StartStateSwitchTask */ -/** -* @brief Function implementing the StateSwitchTask thread. -* @param argument: Not used -* @retval None -*/ -/* USER CODE END Header_StartStateSwitchTask */ -void StartStateSwitchTask(void *argument) -{ - /* USER CODE BEGIN StartStateSwitchTask */ - /* Infinite loop */ - for(;;) - { - osDelay(10); - if(NaviKit.sys.next_sta != NaviKit.sys.sta) - { - switch(NaviKit.sys.next_sta) - { - case shutdown: - {//only from running state - if((NaviKit.sys.sta == running)){// && (NaviKit.som.shutdown_req == true) - enter_shutdown_state(); -// osEnterSleepState(); - } - }break; - case running: - {//from sleep and shutdown state - enter_runing_state(); - }break; - case sleep: - {//only form running state - if(NaviKit.sys.sta == running) - enter_sleep_state(); - - }break; - case dfu: - { - enter_dfu_state(); - }break; - case isp: - { - enter_isp_state(); - }break; - } - NaviKit.sys.sta = NaviKit.sys.next_sta; - } - } - /* USER CODE END StartStateSwitchTask */ -} - /* USER CODE BEGIN Header_StartPowerMonitTask */ /** * @brief Function: Monit som power status and PMB status @@ -581,71 +477,170 @@ void StartPowerMonitTask(void *argument) { /* USER CODE BEGIN StartPowerMonitTask */ HAL_ADCEx_Calibration_Start(&hadc1); - HAL_ADC_Start_DMA(&hadc1, (uint32_t*)&(NaviKit.pmb.rails.adc), ADC_CH_COUNT); + osDelay(2000); /* Infinite loop */ for(;;) { - osDelay(5000); - if(NaviKit.sys.sta == running){ - if(NaviKit.pmb.rails.out_24v > 26.4 || NaviKit.pmb.rails.out_24v < 21.6){ - Log(error,"24V(±10%) power rail exception:%dV",NaviKit.pmb.rails.out_24v); - } - if(NaviKit.pmb.rails.out_5v > 5.5 || NaviKit.pmb.rails.out_5v < 4.5){ - Log(error,"5V(±10%) power rail exception:%dV",NaviKit.pmb.rails.out_5v); - } - if(NaviKit.pmb.rails.out_12v > 13.2 || NaviKit.pmb.rails.out_12v < 10.8){ - Log(error ,"12V(±10%) power rail exception:%dV",NaviKit.pmb.rails.out_12v); - } - if(NaviKit.pmb.rails.bkp_bat < 9.5){ - Log(warning,"Backup power is not online"); - } + //stat1 stat2 lead-acid + //1 1 Not Charging + //1 0 Float Charge + //0 1 Absorption Charge + //0 0 Bulk Charge + NaviKit.pmb.sta.chrg_stat1 = (bool)HAL_GPIO_ReadPin(PMB_CHRG_STAT1_GPIO_Port, PMB_CHRG_STAT1_Pin); + NaviKit.pmb.sta.chrg_stat2 = (bool)HAL_GPIO_ReadPin(PMB_CHRG_STAT2_GPIO_Port, PMB_CHRG_STAT2_Pin); + + if(!NaviKit.pmb.sta.chrg_stat1 && !NaviKit.pmb.sta.chrg_stat2) + Log(trace,"Backup battery status:%dmV Bulk Charge(Vth=12.6V)",(uint32_t)(NaviKit.pmb.rails.bkp_bat*1000)); + if(!NaviKit.pmb.sta.chrg_stat1 && NaviKit.pmb.sta.chrg_stat2) + Log(trace,"Backup battery status:%dmV Absorption Charge(Vth=14.4V)",(uint32_t)(NaviKit.pmb.rails.bkp_bat*1000)); + if(NaviKit.pmb.sta.chrg_stat1 && !NaviKit.pmb.sta.chrg_stat2) + Log(trace,"Backup battery status:%dmV Float Charge(Vth=13.3V)",(uint32_t)(NaviKit.pmb.rails.bkp_bat*1000)); + if(NaviKit.pmb.sta.chrg_stat1 && NaviKit.pmb.sta.chrg_stat2) + Log(trace,"Backup battery status:%dmV Not Charge",(uint32_t)(NaviKit.pmb.rails.bkp_bat*1000)); + + + HAL_ADC_Start_DMA(&hadc1, (uint32_t*)&(NaviKit.pmb.rails.adc), ADC_CH_COUNT); + if(NaviKit.sys.sta == run || NaviKit.sys.next_sta == run ){ + osDelay(2000); + if(NaviKit.pmb.rails.out_24v > 26.4 || NaviKit.pmb.rails.out_24v < 21.6) + Log(fatal,"24V(±10%%) power rail exception:%dmV",(uint32_t)(NaviKit.pmb.rails.out_24v*1000)); + else + Log(trace,"24V(±10%%) power rail regular:%dmV",(uint32_t)(NaviKit.pmb.rails.out_24v*1000)); + + if(NaviKit.pmb.rails.out_5v > 5.5 || NaviKit.pmb.rails.out_5v < 4.5) + Log(fatal,"5V(±10%%) power rail exception:%dmV",(uint32_t)(NaviKit.pmb.rails.out_5v*1000)); + else + Log(trace,"5V(±10%%) power rail regular:%dmV",(uint32_t)(NaviKit.pmb.rails.out_5v*1000)); + + if(NaviKit.pmb.rails.out_12v > 13.2 || NaviKit.pmb.rails.out_12v < 10.8) + Log(fatal ,"12V(±10%%) power rail exception:%dmV",(uint32_t)(NaviKit.pmb.rails.out_12v*1000)); + else + Log(trace,"12V(±10%%) power rail regular:%dmV",(uint32_t)(NaviKit.pmb.rails.out_12v*1000)); + + if(NaviKit.pmb.rails.bkp_bat < 9.5) + Log(warning,"Backup battery exception:%dmV",(uint32_t)(NaviKit.pmb.rails.bkp_bat*1000)); + else + Log(trace,"Backup battery is regular:%dmV",(uint32_t)(NaviKit.pmb.rails.bkp_bat*1000)); + if(NaviKit.pmb.rails.main_pwr < 19){ - Beep(200); - Log(warning,"Main power has been lost, please shutdown computer as soon as possible"); - } + TaskBeep(200,1); + Log(warning,"Main power has been lost, please shutdown computer as soon as possible:%dmV",(uint32_t)(NaviKit.pmb.rails.main_pwr*1000)); + }else + Log(trace,"Main power rail regular:%dmV",(uint32_t)(NaviKit.pmb.rails.main_pwr*1000)); + + }else {//not run state + osDelay(5000); + if(NaviKit.sys.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 + Log(info,"EC enter STANDBY Mode to save power"); +// __HAL_PWR_CLEAR_FLAG + __HAL_RCC_RTC_DISABLE(); + HAL_PWR_EnterSTANDBYMode(); + } + } } - if(NaviKit.pmb.rails.bkp_bat > 15){ - Log(error,"Backup power is exception:%dV",NaviKit.pmb.rails.bkp_bat); - Beep(200); - } if(NaviKit.pmb.rails.main_pwr > 30){ - Beep(200); - Log(error,"Backup power is exception:%dV",NaviKit.pmb.rails.main_pwr); + TaskBeep(200,1); + Log(fatal,"Main power is exception:%dmV",(uint32_t)(NaviKit.pmb.rails.main_pwr*1000)); } + if(NaviKit.pmb.rails.bkp_bat > 15){ + Log(fatal,"Backup battery is exception:%dmV",(uint32_t)(NaviKit.pmb.rails.bkp_bat*1000)); + TaskBeep(200,1); + } + } /* USER CODE END StartPowerMonitTask */ } +/* USER CODE BEGIN Header_StartEventDetect */ +/** +* @brief Function implementing the EventDetect thread. +* @param argument: Not used +* @retval None +*/ +/* USER CODE END Header_StartEventDetect */ +void StartEventDetect(void *argument) +{ + /* USER CODE BEGIN StartEventDetect */ + /* Infinite loop */ + + for(;;) + { + osDelay(10); + + if(NaviKit.sys.power_btn && !osTimerIsRunning(PwrBtnTimerHandle)){ + osTimerStart(PwrBtnTimerHandle,2000); + } + if(NaviKit.sys.custom_btn && !osTimerIsRunning(CustBtnTimerHandle)){ + osTimerStart(PwrBtnTimerHandle,2000); + } + if(!NaviKit.sys.power_btn && osTimerIsRunning(PwrBtnTimerHandle)){ + osTimerStop(PwrBtnTimerHandle); + Log(trace,"power btn short pressed"); + } + if(!NaviKit.sys.custom_btn && osTimerIsRunning(CustBtnTimerHandle)){ + osTimerStop(CustBtnTimerHandle); + Log(trace,"custom btn short pressed"); + } + } + /* USER CODE END StartEventDetect */ +} + +/* PwrBtnTimerCallback function */ +void PwrBtnTimerCallback(void *argument) +{ + /* USER CODE BEGIN PwrBtnTimerCallback */ +// Log(debug,"power btn timer callback"); + Log(trace,"power btn long pressed"); + switch(NaviKit.sys.sta){ + case run: {NaviKit.sys.next_sta = idle;Log(trace,"change to idle"); }break;//system is run now, user request to idle + case idle: {NaviKit.sys.next_sta = run;Log(trace,"change to run"); }break;//system is idle now , user request to power on + case dfu: {NaviKit.sys.next_sta = idle;Log(trace,"change to idle"); }break; + case sleep:{NaviKit.sys.next_sta = run;Log(trace,"change to run"); }break; + case isp: {NaviKit.sys.next_sta = idle;Log(trace,"change to idle"); }break; + } + /* USER CODE END PwrBtnTimerCallback */ +} + +/* CustBtnTimerCallback function */ +void CustBtnTimerCallback(void *argument) +{ + /* USER CODE BEGIN CustBtnTimerCallback */ +// Log(debug,"custom btn timer callback"); + Log(trace,"custom btn long pressed"); + switch(NaviKit.sys.sta){ + case run:{//system is run now, user request to restart system + HAL_GPIO_WritePin(SOM_SYS_RESET_GPIO_Port,SOM_SYS_RESET_Pin,GPIO_PIN_RESET); + osDelay(10); + HAL_GPIO_WritePin(SOM_SYS_RESET_GPIO_Port,SOM_SYS_RESET_Pin,GPIO_PIN_SET); + }break; + case idle:{ + }break; + case dfu:{ + }break; + case sleep:{ + }break; + case isp:{ + }break; + } + /* USER CODE END CustBtnTimerCallback */ +} + /* Private application code --------------------------------------------------*/ /* USER CODE BEGIN Application */ -//os enter sleep state for save power -void osEnterSleepState(){ - Log(info,"EC Enter Sleep State"); - HAL_GPIO_WritePin(SYS_RUN_LED_CTL_GPIO_Port,SYS_RUN_LED_CTL_Pin,GPIO_PIN_SET);//turn off sys run led - vTaskSuspendAll(); -// osKernelLock(); - HAL_SuspendTick(); -// MX_USB_DEVICE_DeInit(); - HAL_I2C_MspDeInit(&hi2c1); - HAL_ADC_MspDeInit(&hadc1); - HAL_ADC_Stop_DMA(&hadc1); - HAL_PWR_EnableSleepOnExit();//for rtos application,enter sleep after interrupt end - HAL_PWR_EnterSLEEPMode(0,PWR_SLEEPENTRY_WFE); -} -void osEnterRuningState(){ - HAL_ResumeTick(); - // MX_USB_DEVICE_Init(); - HAL_I2C_MspInit(&hi2c1); - HAL_ADC_MspInit(&hadc1); - HAL_ADC_Start_DMA(&hadc1, (uint32_t*)&(NaviKit.pmb.rails.adc), ADC_CH_COUNT); - __set_CONTROL(0); - xTaskResumeAll(); -// osKernelUnlock(); - NaviKit.sys.next_sta = running; - Log(info,"EC Enter Runing State"); +void StartStateSwitchTask(void *argument) +{ + + for(;;) + { + + } } + + //printf redirect #ifdef __GNUC__ //#define PUTCHAR_PROTOTYPE int __io_putchar(int ch) diff --git a/Core/Src/main.c b/Core/Src/main.c index b9b9759..2096092 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -84,7 +84,6 @@ int main(void) /* USER CODE BEGIN Init */ - NaviKit_var_init(); /* USER CODE END Init */ /* Configure the system clock */ @@ -102,13 +101,13 @@ int main(void) MX_UART4_Init(); MX_ADC1_Init(); MX_USART1_UART_Init(); - MX_RTC_Init(); +// MX_RTC_Init(); /* Initialize interrupts */ MX_NVIC_Init(); /* USER CODE BEGIN 2 */ - HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN1); + NaviKit_var_init(); /* USER CODE END 2 */ /* Init scheduler */ @@ -226,7 +225,7 @@ void ISP_Judge() {//Judge that if need jump to ISP area RTC_HandleTypeDef hrtc; hrtc.Instance = RTC; - if(HAL_RTCEx_BKUPRead(&hrtc,RTC_BKP_DR1) == 0x32f2) + if(HAL_RTCEx_BKUPRead(&hrtc,ISP_BKP_DR) == BKP_DR_Jump_to_ISP) { __HAL_RCC_PWR_CLK_ENABLE(); __HAL_RCC_BKP_CLK_ENABLE(); @@ -234,7 +233,7 @@ void ISP_Judge() __HAL_RCC_RTC_ENABLE(); HAL_PWR_EnableBkUpAccess(); - HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR1,0x0000); + HAL_RTCEx_BKUPWrite(&hrtc,ISP_BKP_DR,0x0000); __HAL_RCC_RTC_DISABLE(); __HAL_RCC_PWR_CLK_DISABLE(); diff --git a/Core/Src/navikit.c b/Core/Src/navikit.c index 11a66c0..595b5dd 100644 --- a/Core/Src/navikit.c +++ b/Core/Src/navikit.c @@ -2,7 +2,7 @@ * @Description: * @Date: 2020-04-17 13:16:16 * @LastEditors: CK.Zh - * @LastEditTime: 2020-12-17 17:29:07 + * @LastEditTime: 2020-12-30 14:19:14 * @FilePath: \NaviKit_stm32\Core\Src\navikit.c */ /* @@ -15,18 +15,22 @@ #include "navikit.h" NaviKit_t NaviKit; + void NaviKit_var_init() { - NaviKit.sys.sta = shutdown; - NaviKit.sys.next_sta = shutdown; + NaviKit.sys.sta = idle; + NaviKit.sys.next_sta = idle; + } -void Beep(uint32_t time_ms) +void TaskBeep(uint32_t time_ms , uint8_t n) { - HAL_GPIO_WritePin(SYS_BUZZ_CTL_GPIO_Port,SYS_BUZZ_CTL_Pin, GPIO_PIN_RESET); - HAL_Delay(time_ms>>1); - HAL_GPIO_WritePin(SYS_BUZZ_CTL_GPIO_Port,SYS_BUZZ_CTL_Pin, GPIO_PIN_SET); - HAL_Delay(time_ms>>1); + for(uint8_t i=0;i>1); + HAL_GPIO_WritePin(SYS_BUZZ_CTL_GPIO_Port,SYS_BUZZ_CTL_Pin, GPIO_PIN_SET); + osDelay(time_ms>>1); + } } @@ -34,13 +38,15 @@ void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) { if(hadc->Instance == ADC1) { - float adc_17_voltage = 1.2 / NaviKit.pmb.rails.adc[1];//reference voltage + // ADC_CHANNEL_TEMPSENSOR ADC_CHANNEL_16 /* ADC internal channel (no connection on device pin) */ + // ADC_CHANNEL_VREFINT ADC_CHANNEL_17 /* ADC internal channel (no connection on device pin) */ + float adc_17_voltage = (float)1.2 / NaviKit.pmb.rails.adc[1];//reference voltage - NaviKit.pmb.rails.out_24v = (float)(adc_17_voltage * NaviKit.pmb.rails.adc[2] * 16); + NaviKit.pmb.rails.out_24v = (float)(adc_17_voltage * NaviKit.pmb.rails.adc[2] * 16); NaviKit.pmb.rails.out_5v = (float)(adc_17_voltage * NaviKit.pmb.rails.adc[3] * 8); NaviKit.pmb.rails.out_12v = (float)(adc_17_voltage * NaviKit.pmb.rails.adc[4] * 8); NaviKit.pmb.rails.bkp_bat = (float)(adc_17_voltage * NaviKit.pmb.rails.adc[5] * 8); - NaviKit.pmb.rails.main_pwr = (float)(adc_17_voltage * NaviKit.pmb.rails.adc[6] * 16); + NaviKit.pmb.rails.main_pwr = (float)(adc_17_voltage * NaviKit.pmb.rails.adc[6] * 16); } } @@ -48,58 +54,41 @@ void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef* hadc) { HAL_ADCEx_Calibration_Start(&hadc1); } + void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { - if(GPIO_Pin == SOM_SHUTDOWN_REQ_Pin) - { - if(HAL_GPIO_ReadPin(SOM_SHUTDOWN_REQ_GPIO_Port, SOM_SHUTDOWN_REQ_Pin)==GPIO_PIN_RESET) - {//falling edge trigger - if(NaviKit.sys.sta == running) - {//if jetson nano shutdown output low,the power_en should be set low less than 10us + if(GPIO_Pin == SOM_SHUTDOWN_REQ_Pin){ + if(HAL_GPIO_ReadPin(SOM_SHUTDOWN_REQ_GPIO_Port, SOM_SHUTDOWN_REQ_Pin)==GPIO_PIN_RESET){//falling edge trigger + if(NaviKit.sys.sta == run){//if jetson nano shutdown output low,the power_en should be set low less than 10us HAL_GPIO_WritePin(SOM_POWER_EN_GPIO_Port ,SOM_POWER_EN_Pin, GPIO_PIN_RESET); - NaviKit.sys.next_sta = shutdown; - Log(info,"som request to shutdown"); + NaviKit.sys.next_sta = idle; + Log(info,"SOM request to shutdown"); } - NaviKit.som.shutdown_req = true; } else{//Rising edge trigger - NaviKit.som.shutdown_req = false; } - } - else if(GPIO_Pin == SYS_POWER_BTN_Pin) - { - if(HAL_GPIO_ReadPin(SYS_POWER_BTN_GPIO_Port, SYS_POWER_BTN_Pin)==GPIO_PIN_SET) - {//Rising edge trigger + else if(GPIO_Pin == SYS_POWER_BTN_Pin){ + if(HAL_GPIO_ReadPin(SYS_POWER_BTN_GPIO_Port, SYS_POWER_BTN_Pin)==GPIO_PIN_SET){//Rising edge trigger NaviKit.sys.power_btn = true; - Log(debug,"power_btn pushed"); - if(NaviKit.sys.sta==shutdown){ -// osEnterRuningState(); - } + Log(debug,"power_btn status: pressed."); } - if(HAL_GPIO_ReadPin(SYS_POWER_BTN_GPIO_Port, SYS_POWER_BTN_Pin)==GPIO_PIN_RESET) - {//falling edge trigger + if(HAL_GPIO_ReadPin(SYS_POWER_BTN_GPIO_Port, SYS_POWER_BTN_Pin)==GPIO_PIN_RESET){//falling edge trigger NaviKit.sys.power_btn = false; - Log(debug,"power_btn released"); + Log(debug,"power_btn status: released."); } } - else if(GPIO_Pin == SYS_CUSTOM_BTN_Pin) - { - if(HAL_GPIO_ReadPin(SYS_CUSTOM_BTN_GPIO_Port, SYS_CUSTOM_BTN_Pin)==GPIO_PIN_SET) - {//Rising edge trigger - NaviKit.sys.custom_btn = false; - Log(debug,"custom_btn released"); - } - if(HAL_GPIO_ReadPin(SYS_CUSTOM_BTN_GPIO_Port, SYS_CUSTOM_BTN_Pin)==GPIO_PIN_RESET) - {//falling edge trigger + else if(GPIO_Pin == SYS_CUSTOM_BTN_Pin){ + if(HAL_GPIO_ReadPin(SYS_CUSTOM_BTN_GPIO_Port, SYS_CUSTOM_BTN_Pin)==GPIO_PIN_RESET){//falling edge trigger + Log(debug,"custom_btn status: pressed."); NaviKit.sys.custom_btn = true; - Log(debug,"custom_btn pushed"); + } + if(HAL_GPIO_ReadPin(SYS_CUSTOM_BTN_GPIO_Port, SYS_CUSTOM_BTN_Pin)==GPIO_PIN_SET){//Rising edge trigger + Log(debug,"custom_btn status: released."); + NaviKit.sys.custom_btn = false; } } - } -__weak void osEnterRuningState(){} -__weak void osEnterSleepState(){} //write "bios update flag" to bkp register, and reset system void enter_isp_state() @@ -114,16 +103,12 @@ void enter_isp_state() __HAL_RCC_RTC_ENABLE(); HAL_PWR_EnableBkUpAccess(); - HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR1,0x32f2); + HAL_RTCEx_BKUPWrite(&hrtc,ISP_BKP_DR,BKP_DR_Jump_to_ISP); - HAL_Delay(10); - if(HAL_RTCEx_BKUPRead(&hrtc,RTC_BKP_DR1) == 0x32f2) + osDelay(10); + if(HAL_RTCEx_BKUPRead(&hrtc,ISP_BKP_DR) == BKP_DR_Jump_to_ISP) {//write successful - Beep(500); - Beep(500); - Beep(500); - Beep(500); - Beep(500); + TaskBeep(500,5); Log(info,"Enter to EC update state."); Log(info,"System will reboot ,then run ISP automatic."); HAL_NVIC_SystemReset(); @@ -134,149 +119,120 @@ void enter_isp_state() } //timeout:the time of wait -void enter_shutdown_state() +void enter_idle_state(uint16_t delay) { - Log(info,"Enter to shutdown state"); - Beep(50); + Log(info,"Enter to idle state"); + TaskBeep(50,1); - HAL_GPIO_WritePin(SOM_POWER_EN_GPIO_Port ,SOM_POWER_EN_Pin, GPIO_PIN_RESET); HAL_Delay(100); + PWR_Enable(SOM_PWR_EN ,false, delay); - HAL_GPIO_WritePin(SOC_U3_HUB_PWR_CTL_GPIO_Port,SOC_U3_HUB_PWR_CTL_Pin, GPIO_PIN_RESET); HAL_Delay(100); - HAL_GPIO_WritePin(SOC_U2_HUB_PWR_CTL_GPIO_Port,SOC_U2_HUB_PWR_CTL_Pin, GPIO_PIN_RESET); HAL_Delay(100); - HAL_GPIO_WritePin(SOC_U3_HOST_PWR_CTL_GPIO_Port,SOC_U3_HOST_PWR_CTL_Pin, GPIO_PIN_RESET); HAL_Delay(100); - HAL_GPIO_WritePin(SOC_GE_SW_PWR_CTL_GPIO_Port,SOC_GE_SW_PWR_CTL_Pin, GPIO_PIN_RESET); HAL_Delay(100); - HAL_GPIO_WritePin(SOC_U3_GEC_PWR_CTL_GPIO_Port,SOC_U3_GEC_PWR_CTL_Pin, GPIO_PIN_RESET); HAL_Delay(100); + PWR_Enable(SOC_USB3_HUB ,false, delay); + PWR_Enable(SOC_USB2_HUB ,false, delay); + PWR_Enable(SOC_USB3_HOST,false, delay); + PWR_Enable(SOC_USB3_GEC ,false, delay); + PWR_Enable(SOC_GE_SW ,false, delay); - HAL_GPIO_WritePin(USB3_VBUS_CTL_4_GPIO_Port,USB3_VBUS_CTL_4_Pin, GPIO_PIN_RESET); HAL_Delay(100); - HAL_GPIO_WritePin(USB3_VBUS_CTL_3_GPIO_Port,USB3_VBUS_CTL_3_Pin, GPIO_PIN_RESET); HAL_Delay(100); - HAL_GPIO_WritePin(USB3_VBUS_CTL_2_GPIO_Port,USB3_VBUS_CTL_2_Pin, GPIO_PIN_RESET); HAL_Delay(100); - HAL_GPIO_WritePin(USB3_VBUS_CTL_1_GPIO_Port,USB3_VBUS_CTL_1_Pin, GPIO_PIN_RESET); HAL_Delay(100); - HAL_GPIO_WritePin(USB3_VBUS_CTL_6_GPIO_Port,USB3_VBUS_CTL_6_Pin, GPIO_PIN_RESET); HAL_Delay(100); - HAL_GPIO_WritePin(USB3_VBUS_CTL_5_GPIO_Port,USB3_VBUS_CTL_5_Pin, GPIO_PIN_RESET); HAL_Delay(100); + PWR_Enable(USB3_Port4,false, delay); + PWR_Enable(USB3_Port3,false, delay); + PWR_Enable(USB3_Port2,false, delay); + PWR_Enable(USB3_Port1,false, delay); + PWR_Enable(USB3_Port6,false, delay); + PWR_Enable(USB3_Port5,false, delay); - HAL_GPIO_WritePin(USB2_VBUS_CTL_6_GPIO_Port,USB2_VBUS_CTL_6_Pin, GPIO_PIN_RESET); HAL_Delay(100); - HAL_GPIO_WritePin(USB2_VBUS_CTL_5_GPIO_Port,USB2_VBUS_CTL_5_Pin, GPIO_PIN_RESET); HAL_Delay(100); - HAL_GPIO_WritePin(USB2_VBUS_CTL_4_GPIO_Port,USB2_VBUS_CTL_4_Pin, GPIO_PIN_RESET); HAL_Delay(100); - HAL_GPIO_WritePin(USB2_VBUS_CTL_3_GPIO_Port,USB2_VBUS_CTL_3_Pin, GPIO_PIN_RESET); HAL_Delay(100); - HAL_GPIO_WritePin(USB2_VBUS_CTL_2_GPIO_Port,USB2_VBUS_CTL_2_Pin, GPIO_PIN_RESET); HAL_Delay(100); - HAL_GPIO_WritePin(USB2_VBUS_CTL_1_GPIO_Port,USB2_VBUS_CTL_1_Pin, GPIO_PIN_RESET); HAL_Delay(100); + PWR_Enable(USB2_Port6,false, delay); + PWR_Enable(USB2_Port5,false, delay); + PWR_Enable(USB2_Port4,false, delay); + PWR_Enable(USB2_Port3,false, delay); + PWR_Enable(USB2_Port2,false, delay); + PWR_Enable(USB2_Port1,false, delay); - HAL_GPIO_WritePin(SYS_FAN_CTL_1_GPIO_Port,SYS_FAN_CTL_1_Pin,GPIO_PIN_RESET); HAL_Delay(100); - HAL_GPIO_WritePin(SYS_FAN_CTL_2_GPIO_Port,SYS_FAN_CTL_2_Pin,GPIO_PIN_RESET); HAL_Delay(100); - HAL_GPIO_WritePin(SYS_FAN_CTL_3_GPIO_Port,SYS_FAN_CTL_3_Pin,GPIO_PIN_RESET); HAL_Delay(100); + PWR_Enable(SYS_FAN1 ,false , delay); + PWR_Enable(SYS_FAN2 ,false , delay); + PWR_Enable(SYS_FAN3 ,false , delay); - HAL_GPIO_WritePin(PMB_PS_ON_GPIO_Port ,PMB_PS_ON_Pin, GPIO_PIN_RESET); HAL_Delay(100); + PWR_Enable(PMB_PS_ON ,false , delay); - MX_USB_DEVICE_DeInit(); } -void enter_sleep_state() + + +void enter_run_state(uint16_t delay) { - Log(info,"Enter to sleep state"); - Beep(50); + Log(info,"Enter to run state"); + TaskBeep(200,1); - // HAL_GPIO_WritePin(SOC_U3_HUB_PWR_CTL_GPIO_Port,SOC_U3_HUB_PWR_CTL_Pin, GPIO_PIN_RESET); HAL_Delay(100); - HAL_GPIO_WritePin(SOC_U2_HUB_PWR_CTL_GPIO_Port,SOC_U2_HUB_PWR_CTL_Pin, GPIO_PIN_RESET); HAL_Delay(100); -// HAL_GPIO_WritePin(SOC_U3_HOST_PWR_CTL_GPIO_Port,SOC_U3_HOST_PWR_CTL_Pin, GPIO_PIN_RESET); HAL_Delay(100); - HAL_GPIO_WritePin(SOC_GE_SW_PWR_CTL_GPIO_Port,SOC_GE_SW_PWR_CTL_Pin, GPIO_PIN_RESET); HAL_Delay(100); - // HAL_GPIO_WritePin(SOC_U3_GEC_PWR_CTL_GPIO_Port,SOC_U3_GEC_PWR_CTL_Pin, GPIO_PIN_RESET); HAL_Delay(100); + PWR_Enable(SOM_DFU ,false, delay); + PWR_Enable(PMB_PS_ON ,true, delay); - HAL_GPIO_WritePin(USB3_VBUS_CTL_4_GPIO_Port,USB3_VBUS_CTL_4_Pin, GPIO_PIN_RESET); HAL_Delay(100); - HAL_GPIO_WritePin(USB3_VBUS_CTL_3_GPIO_Port,USB3_VBUS_CTL_3_Pin, GPIO_PIN_RESET); HAL_Delay(100); - HAL_GPIO_WritePin(USB3_VBUS_CTL_2_GPIO_Port,USB3_VBUS_CTL_2_Pin, GPIO_PIN_RESET); HAL_Delay(100); - HAL_GPIO_WritePin(USB3_VBUS_CTL_1_GPIO_Port,USB3_VBUS_CTL_1_Pin, GPIO_PIN_RESET); HAL_Delay(100); - // HAL_GPIO_WritePin(USB3_VBUS_CTL_6_GPIO_Port,USB3_VBUS_CTL_6_Pin, GPIO_PIN_RESET); HAL_Delay(100); - // HAL_GPIO_WritePin(USB3_VBUS_CTL_5_GPIO_Port,USB3_VBUS_CTL_5_Pin, GPIO_PIN_RESET); HAL_Delay(100); + PWR_Enable(SYS_FAN1 ,true , delay); + PWR_Enable(SYS_FAN2 ,true , delay); + PWR_Enable(SYS_FAN3 ,true , delay); - HAL_GPIO_WritePin(USB2_VBUS_CTL_6_GPIO_Port,USB2_VBUS_CTL_6_Pin, GPIO_PIN_RESET); HAL_Delay(100); - HAL_GPIO_WritePin(USB2_VBUS_CTL_5_GPIO_Port,USB2_VBUS_CTL_5_Pin, GPIO_PIN_RESET); HAL_Delay(100); - HAL_GPIO_WritePin(USB2_VBUS_CTL_4_GPIO_Port,USB2_VBUS_CTL_4_Pin, GPIO_PIN_RESET); HAL_Delay(100); - HAL_GPIO_WritePin(USB2_VBUS_CTL_3_GPIO_Port,USB2_VBUS_CTL_3_Pin, GPIO_PIN_RESET); HAL_Delay(100); - HAL_GPIO_WritePin(USB2_VBUS_CTL_2_GPIO_Port,USB2_VBUS_CTL_2_Pin, GPIO_PIN_RESET); HAL_Delay(100); - HAL_GPIO_WritePin(USB2_VBUS_CTL_1_GPIO_Port,USB2_VBUS_CTL_1_Pin, GPIO_PIN_RESET); HAL_Delay(100); + PWR_Enable(SOC_USB3_HUB ,true , delay); + PWR_Enable(SOC_USB2_HUB ,true , delay); + PWR_Enable(SOC_USB3_HOST,true , delay); + PWR_Enable(SOC_USB3_GEC ,true , delay); + PWR_Enable(SOC_GE_SW ,true , delay); - HAL_GPIO_WritePin(SYS_FAN_CTL_1_GPIO_Port,SYS_FAN_CTL_1_Pin,GPIO_PIN_RESET); HAL_Delay(100); - HAL_GPIO_WritePin(SYS_FAN_CTL_2_GPIO_Port,SYS_FAN_CTL_2_Pin,GPIO_PIN_RESET); HAL_Delay(100); - HAL_GPIO_WritePin(SYS_FAN_CTL_3_GPIO_Port,SYS_FAN_CTL_3_Pin,GPIO_PIN_RESET); HAL_Delay(100); + PWR_Enable(USB2_Port1,true , delay); + PWR_Enable(USB2_Port2,true , delay); + PWR_Enable(USB2_Port3,true , delay); + PWR_Enable(USB2_Port4,true , delay); + PWR_Enable(USB2_Port5,true , delay); + PWR_Enable(USB2_Port6,true , delay); + + PWR_Enable(USB3_Port5,true , delay); + PWR_Enable(USB3_Port6,true , delay); + PWR_Enable(USB3_Port1,true , delay); + PWR_Enable(USB3_Port2,true , delay); + PWR_Enable(USB3_Port3,true , delay); + PWR_Enable(USB3_Port4,true , delay); + + PWR_Enable(SOM_PWR_EN , true , delay); } - -void enter_runing_state() +void enter_sleep_state(uint16_t delay) { - - Log(info,"Enter to runing state"); - Beep(200); - - HAL_GPIO_WritePin(SOM_FORCE_RECOVERY_GPIO_Port ,SOM_FORCE_RECOVERY_Pin, GPIO_PIN_SET); HAL_Delay(100); - HAL_GPIO_WritePin(PMB_PS_ON_GPIO_Port ,PMB_PS_ON_Pin, GPIO_PIN_SET); HAL_Delay(100); - - HAL_GPIO_WritePin(SYS_FAN_CTL_1_GPIO_Port,SYS_FAN_CTL_1_Pin,GPIO_PIN_SET); HAL_Delay(100); - HAL_GPIO_WritePin(SYS_FAN_CTL_2_GPIO_Port,SYS_FAN_CTL_2_Pin,GPIO_PIN_SET); HAL_Delay(100); - HAL_GPIO_WritePin(SYS_FAN_CTL_3_GPIO_Port,SYS_FAN_CTL_3_Pin,GPIO_PIN_SET); HAL_Delay(100); - - HAL_GPIO_WritePin(SOC_U3_HUB_PWR_CTL_GPIO_Port,SOC_U3_HUB_PWR_CTL_Pin, GPIO_PIN_SET); HAL_Delay(100); - HAL_GPIO_WritePin(SOC_U2_HUB_PWR_CTL_GPIO_Port,SOC_U2_HUB_PWR_CTL_Pin, GPIO_PIN_SET); HAL_Delay(100); - HAL_GPIO_WritePin(SOC_U3_HOST_PWR_CTL_GPIO_Port,SOC_U3_HOST_PWR_CTL_Pin, GPIO_PIN_SET); HAL_Delay(100); - HAL_GPIO_WritePin(SOC_GE_SW_PWR_CTL_GPIO_Port,SOC_GE_SW_PWR_CTL_Pin, GPIO_PIN_SET); HAL_Delay(100); - HAL_GPIO_WritePin(SOC_U3_GEC_PWR_CTL_GPIO_Port,SOC_U3_GEC_PWR_CTL_Pin, GPIO_PIN_SET); HAL_Delay(100); - - HAL_GPIO_WritePin(USB2_VBUS_CTL_1_GPIO_Port,USB2_VBUS_CTL_1_Pin, GPIO_PIN_SET); HAL_Delay(100); - HAL_GPIO_WritePin(USB2_VBUS_CTL_2_GPIO_Port,USB2_VBUS_CTL_2_Pin, GPIO_PIN_SET); HAL_Delay(100); - HAL_GPIO_WritePin(USB2_VBUS_CTL_3_GPIO_Port,USB2_VBUS_CTL_3_Pin, GPIO_PIN_SET); HAL_Delay(100); - HAL_GPIO_WritePin(USB2_VBUS_CTL_4_GPIO_Port,USB2_VBUS_CTL_4_Pin, GPIO_PIN_SET); HAL_Delay(100); - HAL_GPIO_WritePin(USB2_VBUS_CTL_5_GPIO_Port,USB2_VBUS_CTL_5_Pin, GPIO_PIN_SET); HAL_Delay(100); - HAL_GPIO_WritePin(USB2_VBUS_CTL_6_GPIO_Port,USB2_VBUS_CTL_6_Pin, GPIO_PIN_SET); HAL_Delay(100); - - HAL_GPIO_WritePin(USB3_VBUS_CTL_5_GPIO_Port,USB3_VBUS_CTL_5_Pin, GPIO_PIN_SET); HAL_Delay(100); - HAL_GPIO_WritePin(USB3_VBUS_CTL_6_GPIO_Port,USB3_VBUS_CTL_6_Pin, GPIO_PIN_SET); HAL_Delay(100); - HAL_GPIO_WritePin(USB3_VBUS_CTL_1_GPIO_Port,USB3_VBUS_CTL_1_Pin, GPIO_PIN_SET); HAL_Delay(100); - HAL_GPIO_WritePin(USB3_VBUS_CTL_2_GPIO_Port,USB3_VBUS_CTL_2_Pin, GPIO_PIN_SET); HAL_Delay(100); - HAL_GPIO_WritePin(USB3_VBUS_CTL_3_GPIO_Port,USB3_VBUS_CTL_3_Pin, GPIO_PIN_SET); HAL_Delay(100); - HAL_GPIO_WritePin(USB3_VBUS_CTL_4_GPIO_Port,USB3_VBUS_CTL_4_Pin, GPIO_PIN_SET); HAL_Delay(100); - - HAL_GPIO_WritePin(SOM_POWER_EN_GPIO_Port ,SOM_POWER_EN_Pin, GPIO_PIN_SET); HAL_Delay(100); - - MX_USB_DEVICE_Init(); + Log(info,"Enter to sleep state"); + TaskBeep(50,1); } - -void enter_dfu_state() +void enter_dfu_state(uint16_t delay) { - Log(info,"Enter to dfu state"); - Beep(500); - Beep(500); - Beep(500); + Log(info,"Enter to DFU state"); +TaskBeep(500,3); - HAL_GPIO_WritePin(SOM_FORCE_RECOVERY_GPIO_Port ,SOM_FORCE_RECOVERY_Pin, GPIO_PIN_RESET); HAL_Delay(100); + PWR_Enable(SOM_DFU , true , delay); - HAL_GPIO_WritePin(PMB_PS_ON_GPIO_Port ,PMB_PS_ON_Pin, GPIO_PIN_SET); HAL_Delay(100); + PWR_Enable(PMB_PS_ON , true , delay); - HAL_GPIO_WritePin(SOM_POWER_EN_GPIO_Port ,SOM_POWER_EN_Pin, GPIO_PIN_SET); HAL_Delay(1000); + PWR_Enable(SYS_FAN1 , true , delay); + PWR_Enable(SYS_FAN2 , true , delay); + PWR_Enable(SYS_FAN3 , true , delay); - HAL_GPIO_WritePin(SYS_FAN_CTL_1_GPIO_Port,SYS_FAN_CTL_1_Pin,GPIO_PIN_SET); HAL_Delay(100); - HAL_GPIO_WritePin(SYS_FAN_CTL_2_GPIO_Port,SYS_FAN_CTL_2_Pin,GPIO_PIN_SET); HAL_Delay(100); - HAL_GPIO_WritePin(SYS_FAN_CTL_3_GPIO_Port,SYS_FAN_CTL_3_Pin,GPIO_PIN_SET); HAL_Delay(100); + PWR_Enable(SOC_USB3_HUB , true , delay); + PWR_Enable(SOC_USB2_HUB , true , delay); + PWR_Enable(SOC_USB3_HOST , true , delay); + PWR_Enable(SOC_USB3_GEC , true , delay); + PWR_Enable(SOC_GE_SW , true , delay); - HAL_GPIO_WritePin(SOC_U3_HUB_PWR_CTL_GPIO_Port,SOC_U3_HUB_PWR_CTL_Pin, GPIO_PIN_SET); HAL_Delay(100); - HAL_GPIO_WritePin(SOC_U2_HUB_PWR_CTL_GPIO_Port,SOC_U2_HUB_PWR_CTL_Pin, GPIO_PIN_SET); HAL_Delay(100); - HAL_GPIO_WritePin(SOC_U3_HOST_PWR_CTL_GPIO_Port,SOC_U3_HOST_PWR_CTL_Pin, GPIO_PIN_SET); HAL_Delay(100); - HAL_GPIO_WritePin(SOC_GE_SW_PWR_CTL_GPIO_Port,SOC_GE_SW_PWR_CTL_Pin, GPIO_PIN_SET); HAL_Delay(100); - HAL_GPIO_WritePin(SOC_U3_GEC_PWR_CTL_GPIO_Port,SOC_U3_GEC_PWR_CTL_Pin, GPIO_PIN_SET); HAL_Delay(100); + PWR_Enable(USB2_Port1 , true , delay); + PWR_Enable(USB2_Port2 , true , delay); + PWR_Enable(USB2_Port3 , true , delay); + PWR_Enable(USB2_Port4 , true , delay); + PWR_Enable(USB2_Port5 , true , delay); + PWR_Enable(USB2_Port6 , true , delay); + PWR_Enable(USB2_Port1 , true , delay); + PWR_Enable(USB2_Port1 , true , delay); - HAL_GPIO_WritePin(USB2_VBUS_CTL_1_GPIO_Port,USB2_VBUS_CTL_1_Pin, GPIO_PIN_SET); HAL_Delay(100); - HAL_GPIO_WritePin(USB2_VBUS_CTL_2_GPIO_Port,USB2_VBUS_CTL_2_Pin, GPIO_PIN_SET); HAL_Delay(100); - HAL_GPIO_WritePin(USB2_VBUS_CTL_3_GPIO_Port,USB2_VBUS_CTL_3_Pin, GPIO_PIN_SET); HAL_Delay(100); - HAL_GPIO_WritePin(USB2_VBUS_CTL_4_GPIO_Port,USB2_VBUS_CTL_4_Pin, GPIO_PIN_SET); HAL_Delay(100); - HAL_GPIO_WritePin(USB2_VBUS_CTL_5_GPIO_Port,USB2_VBUS_CTL_5_Pin, GPIO_PIN_SET); HAL_Delay(100); - HAL_GPIO_WritePin(USB2_VBUS_CTL_6_GPIO_Port,USB2_VBUS_CTL_6_Pin, GPIO_PIN_SET); HAL_Delay(100); - - HAL_GPIO_WritePin(USB3_VBUS_CTL_5_GPIO_Port,USB3_VBUS_CTL_5_Pin, GPIO_PIN_SET); HAL_Delay(100); - HAL_GPIO_WritePin(USB3_VBUS_CTL_6_GPIO_Port,USB3_VBUS_CTL_6_Pin, GPIO_PIN_SET); HAL_Delay(100); - HAL_GPIO_WritePin(USB3_VBUS_CTL_1_GPIO_Port,USB3_VBUS_CTL_1_Pin, GPIO_PIN_SET); HAL_Delay(100); - HAL_GPIO_WritePin(USB3_VBUS_CTL_2_GPIO_Port,USB3_VBUS_CTL_2_Pin, GPIO_PIN_SET); HAL_Delay(100); - HAL_GPIO_WritePin(USB3_VBUS_CTL_3_GPIO_Port,USB3_VBUS_CTL_3_Pin, GPIO_PIN_SET); HAL_Delay(100); - HAL_GPIO_WritePin(USB3_VBUS_CTL_4_GPIO_Port,USB3_VBUS_CTL_4_Pin, GPIO_PIN_SET); HAL_Delay(100); + PWR_Enable(USB3_Port5,true , delay); + PWR_Enable(USB3_Port6,true , delay); + PWR_Enable(USB3_Port1,true , delay); + PWR_Enable(USB3_Port2,true , delay); + PWR_Enable(USB3_Port3,true , delay); + PWR_Enable(USB3_Port4,true , delay); + PWR_Enable(SOM_PWR_EN , true , delay); // // HAL_GPIO_WritePin(SOM_FORCE_RECOVERY_GPIO_Port ,SOM_FORCE_RECOVERY_Pin, GPIO_PIN_RESET); HAL_Delay(100); @@ -285,6 +241,74 @@ void enter_dfu_state() // HAL_Delay(5000); // HAL_GPIO_WritePin(SOM_SYS_RESET_GPIO_Port ,SOM_SYS_RESET_Pin, GPIO_PIN_SET); HAL_Delay(100); // HAL_GPIO_WritePin(SOM_FORCE_RECOVERY_GPIO_Port ,SOM_FORCE_RECOVERY_Pin, GPIO_PIN_SET); HAL_Delay(100); - - +} + + + +//device power enable or disable +//delay: After executing the operation delay (ms) +void PWR_Enable(enum Device_t device,bool en,uint16_t delay){ + switch (device){ + case USB2_Port1: {HAL_GPIO_WritePin(USB2_VBUS_CTL_1_GPIO_Port,USB2_VBUS_CTL_1_Pin, en); }break; + case USB2_Port2: {HAL_GPIO_WritePin(USB2_VBUS_CTL_2_GPIO_Port,USB2_VBUS_CTL_2_Pin, en); }break; + case USB2_Port3: {HAL_GPIO_WritePin(USB2_VBUS_CTL_3_GPIO_Port,USB2_VBUS_CTL_3_Pin, en); }break; + case USB2_Port4: {HAL_GPIO_WritePin(USB2_VBUS_CTL_4_GPIO_Port,USB2_VBUS_CTL_4_Pin, en); }break; + case USB2_Port5: {HAL_GPIO_WritePin(USB2_VBUS_CTL_5_GPIO_Port,USB2_VBUS_CTL_5_Pin, en); }break; + case USB2_Port6: {HAL_GPIO_WritePin(USB2_VBUS_CTL_6_GPIO_Port,USB2_VBUS_CTL_6_Pin, en); }break; + case USB3_Port1: {HAL_GPIO_WritePin(USB3_VBUS_CTL_1_GPIO_Port,USB3_VBUS_CTL_1_Pin, en); }break; + case USB3_Port2: {HAL_GPIO_WritePin(USB3_VBUS_CTL_2_GPIO_Port,USB3_VBUS_CTL_2_Pin, en); }break; + case USB3_Port3: {HAL_GPIO_WritePin(USB3_VBUS_CTL_3_GPIO_Port,USB3_VBUS_CTL_3_Pin, en); }break; + case USB3_Port4: {HAL_GPIO_WritePin(USB3_VBUS_CTL_4_GPIO_Port,USB3_VBUS_CTL_4_Pin, en); }break; + case USB3_Port5: {HAL_GPIO_WritePin(USB3_VBUS_CTL_5_GPIO_Port,USB3_VBUS_CTL_5_Pin, en); }break; + case USB3_Port6: {HAL_GPIO_WritePin(USB3_VBUS_CTL_6_GPIO_Port,USB3_VBUS_CTL_6_Pin, en); }break; + case SOC_USB2_HUB: {HAL_GPIO_WritePin(SOC_U2_HUB_PWR_CTL_GPIO_Port,SOC_U2_HUB_PWR_CTL_Pin, en); }break; + case SOC_USB3_HUB: {HAL_GPIO_WritePin(SOC_U3_HUB_PWR_CTL_GPIO_Port,SOC_U3_HUB_PWR_CTL_Pin, en); }break; + case SOC_USB3_HOST: {HAL_GPIO_WritePin(SOC_U3_HOST_PWR_CTL_GPIO_Port,SOC_U3_HOST_PWR_CTL_Pin, en); }break; + case SOC_USB3_GEC: {HAL_GPIO_WritePin(SOC_U3_GEC_PWR_CTL_GPIO_Port,SOC_U3_GEC_PWR_CTL_Pin, en); }break; + case SOC_GE_SW: {HAL_GPIO_WritePin(SOC_GE_SW_PWR_CTL_GPIO_Port,SOC_GE_SW_PWR_CTL_Pin, en); }break; + case SYS_FAN1: {HAL_GPIO_WritePin(SYS_FAN_CTL_1_GPIO_Port,SYS_FAN_CTL_1_Pin, en); }break; + case SYS_FAN2: {HAL_GPIO_WritePin(SYS_FAN_CTL_2_GPIO_Port,SYS_FAN_CTL_2_Pin, en); }break; + case SYS_FAN3: {HAL_GPIO_WritePin(SYS_FAN_CTL_3_GPIO_Port,SYS_FAN_CTL_3_Pin, en); }break; + case SYS_RUN_LED: {HAL_GPIO_WritePin(SYS_RUN_LED_CTL_GPIO_Port,SYS_RUN_LED_CTL_Pin, !en); }break; + case SYS_PWR_LED: {HAL_GPIO_WritePin(SYS_POWER_LED_CTL_GPIO_Port,SYS_POWER_LED_CTL_Pin, !en); }break; + case SOM_PWR_EN: {HAL_GPIO_WritePin(SOM_POWER_EN_GPIO_Port,SOM_POWER_EN_Pin, en); }break; + case SOM_DFU: {HAL_GPIO_WritePin(SOM_FORCE_RECOVERY_GPIO_Port,SOM_FORCE_RECOVERY_Pin, !en); }break; + case PMB_PS_ON: {HAL_GPIO_WritePin(PMB_PS_ON_GPIO_Port,PMB_PS_ON_Pin, en); }break; + default: {Log(error,"PWR_Enable device parameter is invalid."); }break; + } + osDelay(delay); +} + +//check device on board power status +bool PWR_Status(enum Device_t device){ + bool sta = false; + switch (device){ + case USB2_Port1: {sta = HAL_GPIO_ReadPin(USB2_VBUS_CTL_1_GPIO_Port,USB2_VBUS_CTL_1_Pin); }break; + case USB2_Port2: {sta = HAL_GPIO_ReadPin(USB2_VBUS_CTL_2_GPIO_Port,USB2_VBUS_CTL_2_Pin); }break; + case USB2_Port3: {sta = HAL_GPIO_ReadPin(USB2_VBUS_CTL_3_GPIO_Port,USB2_VBUS_CTL_3_Pin); }break; + case USB2_Port4: {sta = HAL_GPIO_ReadPin(USB2_VBUS_CTL_4_GPIO_Port,USB2_VBUS_CTL_4_Pin); }break; + case USB2_Port5: {sta = HAL_GPIO_ReadPin(USB2_VBUS_CTL_5_GPIO_Port,USB2_VBUS_CTL_5_Pin); }break; + case USB2_Port6: {sta = HAL_GPIO_ReadPin(USB2_VBUS_CTL_6_GPIO_Port,USB2_VBUS_CTL_6_Pin); }break; + case USB3_Port1: {sta = HAL_GPIO_ReadPin(USB3_VBUS_CTL_1_GPIO_Port,USB3_VBUS_CTL_1_Pin); }break; + case USB3_Port2: {sta = HAL_GPIO_ReadPin(USB3_VBUS_CTL_2_GPIO_Port,USB3_VBUS_CTL_2_Pin); }break; + case USB3_Port3: {sta = HAL_GPIO_ReadPin(USB3_VBUS_CTL_3_GPIO_Port,USB3_VBUS_CTL_3_Pin); }break; + case USB3_Port4: {sta = HAL_GPIO_ReadPin(USB3_VBUS_CTL_4_GPIO_Port,USB3_VBUS_CTL_4_Pin); }break; + case USB3_Port5: {sta = HAL_GPIO_ReadPin(USB3_VBUS_CTL_5_GPIO_Port,USB3_VBUS_CTL_5_Pin); }break; + case USB3_Port6: {sta = HAL_GPIO_ReadPin(USB3_VBUS_CTL_6_GPIO_Port,USB3_VBUS_CTL_6_Pin); }break; + case SOC_USB2_HUB: {sta = HAL_GPIO_ReadPin(SOC_U2_HUB_PWR_CTL_GPIO_Port,SOC_U2_HUB_PWR_CTL_Pin); }break; + case SOC_USB3_HUB: {sta = HAL_GPIO_ReadPin(SOC_U3_HUB_PWR_CTL_GPIO_Port,SOC_U3_HUB_PWR_CTL_Pin); }break; + case SOC_USB3_HOST: {sta = HAL_GPIO_ReadPin(SOC_U3_HOST_PWR_CTL_GPIO_Port,SOC_U3_HOST_PWR_CTL_Pin); }break; + case SOC_USB3_GEC: {sta = HAL_GPIO_ReadPin(SOC_U3_GEC_PWR_CTL_GPIO_Port,SOC_U3_GEC_PWR_CTL_Pin); }break; + case SOC_GE_SW: {sta = HAL_GPIO_ReadPin(SOC_GE_SW_PWR_CTL_GPIO_Port,SOC_GE_SW_PWR_CTL_Pin); }break; + case SYS_FAN1: {sta = HAL_GPIO_ReadPin(SYS_FAN_CTL_1_GPIO_Port,SYS_FAN_CTL_1_Pin); }break; + case SYS_FAN2: {sta = HAL_GPIO_ReadPin(SYS_FAN_CTL_2_GPIO_Port,SYS_FAN_CTL_2_Pin); }break; + case SYS_FAN3: {sta = HAL_GPIO_ReadPin(SYS_FAN_CTL_3_GPIO_Port,SYS_FAN_CTL_3_Pin); }break; + case SYS_RUN_LED: {sta = !HAL_GPIO_ReadPin(SYS_RUN_LED_CTL_GPIO_Port,SYS_RUN_LED_CTL_Pin); }break; + case SYS_PWR_LED: {sta = !HAL_GPIO_ReadPin(SYS_POWER_LED_CTL_GPIO_Port,SYS_POWER_LED_CTL_Pin); }break; + case SOM_PWR_EN: {sta = HAL_GPIO_ReadPin(SOM_POWER_EN_GPIO_Port,SOM_POWER_EN_Pin); }break; + case SOM_DFU: {sta = !HAL_GPIO_ReadPin(SOM_FORCE_RECOVERY_GPIO_Port,SOM_FORCE_RECOVERY_Pin); }break; + case PMB_PS_ON: {sta = HAL_GPIO_ReadPin(PMB_PS_ON_GPIO_Port,PMB_PS_ON_Pin); }break; + default: {Log(error,"PWR_Status device parameter is invalid."); }break; + } + return sta; } diff --git a/Core/Src/rtc.c b/Core/Src/rtc.c index ba9868d..fdf0825 100644 --- a/Core/Src/rtc.c +++ b/Core/Src/rtc.c @@ -59,6 +59,8 @@ void HAL_RTC_MspInit(RTC_HandleTypeDef* rtcHandle) /* RTC interrupt Init */ HAL_NVIC_SetPriority(RTC_IRQn, 5, 0); HAL_NVIC_EnableIRQ(RTC_IRQn); + HAL_NVIC_SetPriority(RTC_Alarm_IRQn, 5, 0); + HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn); /* USER CODE BEGIN RTC_MspInit 1 */ /* USER CODE END RTC_MspInit 1 */ @@ -78,6 +80,7 @@ void HAL_RTC_MspDeInit(RTC_HandleTypeDef* rtcHandle) /* RTC interrupt Deinit */ HAL_NVIC_DisableIRQ(RTC_IRQn); + HAL_NVIC_DisableIRQ(RTC_Alarm_IRQn); /* USER CODE BEGIN RTC_MspDeInit 1 */ /* USER CODE END RTC_MspDeInit 1 */ diff --git a/Core/Src/stm32f1xx_it.c b/Core/Src/stm32f1xx_it.c index c4414d8..b05afac 100644 --- a/Core/Src/stm32f1xx_it.c +++ b/Core/Src/stm32f1xx_it.c @@ -349,6 +349,20 @@ void USART1_IRQHandler(void) /* USER CODE END USART1_IRQn 1 */ } +/** + * @brief This function handles RTC alarm interrupt through EXTI line 17. + */ +void RTC_Alarm_IRQHandler(void) +{ + /* USER CODE BEGIN RTC_Alarm_IRQn 0 */ + + /* USER CODE END RTC_Alarm_IRQn 0 */ + HAL_RTC_AlarmIRQHandler(&hrtc); + /* USER CODE BEGIN RTC_Alarm_IRQn 1 */ + + /* USER CODE END RTC_Alarm_IRQn 1 */ +} + /** * @brief This function handles UART4 global interrupt. */ diff --git a/Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_pwr.h b/Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_pwr.h index 9696c00..b8fee49 100644 --- a/Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_pwr.h +++ b/Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_pwr.h @@ -1,3 +1,10 @@ +/* + * @Description: + * @Date: 2020-04-10 17:35:38 + * @LastEditors: CK.Zh + * @LastEditTime: 2021-01-04 17:51:16 + * @FilePath: \NaviKit_stm32\Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_pwr.h + */ /** ****************************************************************************** * @file stm32f1xx_hal_pwr.h diff --git a/Middlewares/Log/log.c b/Middlewares/Log/log.c index 00c8fa5..e1e3357 100644 --- a/Middlewares/Log/log.c +++ b/Middlewares/Log/log.c @@ -10,6 +10,9 @@ void Log(LogLevel_t loglevel,char *format,...) { switch(loglevel){ + case trace:{ + printf("[Trace ] ") ; + }break; case debug:{ printf("[Debug ] ") ; }break; @@ -22,6 +25,9 @@ void Log(LogLevel_t loglevel,char *format,...) case error:{ printf("[Error ] ") ; }break; + case fatal:{ + printf("[Fatal ] ") ; + }break; } printf("%u | ",osKernelGetTickCount()); va_list args; diff --git a/Middlewares/Log/log.h b/Middlewares/Log/log.h index a0582ad..0e24fc6 100644 --- a/Middlewares/Log/log.h +++ b/Middlewares/Log/log.h @@ -11,10 +11,12 @@ #include typedef enum { - debug = 0, - info = 1, - warning = 2, - error = 3 + trace = 0, + debug = 1, + info = 2, + warning = 3, + error = 4, + fatal =5 }LogLevel_t; void Log(LogLevel_t loglevel,char *format, ...); diff --git a/NaviKit_stm32.ioc b/NaviKit_stm32.ioc index 7dd286a..31c57a0 100644 --- a/NaviKit_stm32.ioc +++ b/NaviKit_stm32.ioc @@ -9,7 +9,7 @@ ADC1.Channel-3\#ChannelRegularConversion=ADC_CHANNEL_4 ADC1.Channel-4\#ChannelRegularConversion=ADC_CHANNEL_5 ADC1.Channel-5\#ChannelRegularConversion=ADC_CHANNEL_6 ADC1.Channel-6\#ChannelRegularConversion=ADC_CHANNEL_7 -ADC1.ContinuousConvMode=ENABLE +ADC1.ContinuousConvMode=DISABLE ADC1.EnableAnalogWatchDog=true ADC1.ExternalTrigConv=ADC_SOFTWARE_START ADC1.IPParameters=Rank-0\#ChannelRegularConversion,Channel-0\#ChannelRegularConversion,SamplingTime-0\#ChannelRegularConversion,NbrOfConversionFlag,ContinuousConvMode,Rank-1\#ChannelRegularConversion,Channel-1\#ChannelRegularConversion,SamplingTime-1\#ChannelRegularConversion,Rank-2\#ChannelRegularConversion,Channel-2\#ChannelRegularConversion,SamplingTime-2\#ChannelRegularConversion,Rank-3\#ChannelRegularConversion,Channel-3\#ChannelRegularConversion,SamplingTime-3\#ChannelRegularConversion,NbrOfConversion,Rank-4\#ChannelRegularConversion,Channel-4\#ChannelRegularConversion,SamplingTime-4\#ChannelRegularConversion,Rank-5\#ChannelRegularConversion,Channel-5\#ChannelRegularConversion,SamplingTime-5\#ChannelRegularConversion,ExternalTrigConv,master,EnableAnalogWatchDog,Rank-6\#ChannelRegularConversion,Channel-6\#ChannelRegularConversion,SamplingTime-6\#ChannelRegularConversion,WatchdogMode,AWD1ITMode,WatchdogChannel,AWD1HighThreshold,AWD1LowThreshold @@ -65,6 +65,7 @@ Dma.ADC1.0.Priority=DMA_PRIORITY_LOW Dma.ADC1.0.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority Dma.Request0=ADC1 Dma.RequestsNb=1 +FREERTOS.Events01= FREERTOS.FootprintOK=true FREERTOS.HEAP_NUMBER=4 FREERTOS.INCLUDE_pcTaskGetTaskName=1 @@ -74,8 +75,9 @@ FREERTOS.INCLUDE_xSemaphoreGetMutexHolder=1 FREERTOS.INCLUDE_xTaskAbortDelay=1 FREERTOS.INCLUDE_xTaskGetCurrentTaskHandle=1 FREERTOS.INCLUDE_xTaskGetHandle=1 -FREERTOS.IPParameters=Tasks01,configMAX_TASK_NAME_LEN,configUSE_TICKLESS_IDLE,INCLUDE_xTaskGetCurrentTaskHandle,INCLUDE_xTaskGetHandle,configUSE_APPLICATION_TASK_TAG,FootprintOK,configUSE_IDLE_HOOK,configUSE_TICK_HOOK,configUSE_MALLOC_FAILED_HOOK,configGENERATE_RUN_TIME_STATS,configUSE_STATS_FORMATTING_FUNCTIONS,configUSE_TRACE_FACILITY,HEAP_NUMBER,configTOTAL_HEAP_SIZE,configCHECK_FOR_STACK_OVERFLOW,configUSE_TASK_NOTIFICATIONS,INCLUDE_xTaskAbortDelay,INCLUDE_xEventGroupSetBitFromISR,INCLUDE_xSemaphoreGetMutexHolder,INCLUDE_pcTaskGetTaskName,INCLUDE_vTaskCleanUpResources -FREERTOS.Tasks01=defaultTask,24,128,StartDefaultTask,Default,NULL,Dynamic,NULL,NULL;LedBlinkTask,8,128,StartLedBlinkTask,Default,NULL,Dynamic,NULL,NULL;IWDGRefreshTask,40,128,StartIWDGRefreshTask,Default,NULL,Dynamic,NULL,NULL;EventDetect,8,128,StartEventDetect,Default,NULL,Dynamic,NULL,NULL;CoulombRead,8,128,StartCoulombRead,Default,NULL,Dynamic,NULL,NULL;StateSwitchTask,8,128,StartStateSwitchTask,Default,NULL,Dynamic,NULL,NULL;PowerMonitTask,8,128,StartPowerMonitTask,Default,NULL,Dynamic,NULL,NULL +FREERTOS.IPParameters=Tasks01,configMAX_TASK_NAME_LEN,configUSE_TICKLESS_IDLE,INCLUDE_xTaskGetCurrentTaskHandle,INCLUDE_xTaskGetHandle,configUSE_APPLICATION_TASK_TAG,FootprintOK,configUSE_IDLE_HOOK,configUSE_TICK_HOOK,configUSE_MALLOC_FAILED_HOOK,configGENERATE_RUN_TIME_STATS,configUSE_STATS_FORMATTING_FUNCTIONS,configUSE_TRACE_FACILITY,HEAP_NUMBER,configTOTAL_HEAP_SIZE,configCHECK_FOR_STACK_OVERFLOW,configUSE_TASK_NOTIFICATIONS,INCLUDE_xTaskAbortDelay,INCLUDE_xEventGroupSetBitFromISR,INCLUDE_xSemaphoreGetMutexHolder,INCLUDE_pcTaskGetTaskName,INCLUDE_vTaskCleanUpResources,Events01,Timers01 +FREERTOS.Tasks01=defaultTask,24,128,StartDefaultTask,Default,NULL,Dynamic,NULL,NULL;LedBlinkTask,8,128,StartLedBlinkTask,Default,NULL,Dynamic,NULL,NULL;CoulombRead,8,128,StartCoulombRead,Default,NULL,Dynamic,NULL,NULL;PowerMonitTask,8,128,StartPowerMonitTask,Default,NULL,Dynamic,NULL,NULL;EventDetect,8,128,StartEventDetect,Default,NULL,Dynamic,NULL,NULL +FREERTOS.Timers01=PwrBtnTimer,PwrBtnTimerCallback,osTimerOnce,Default,NULL,Dynamic,NULL;CustBtnTimer,CustBtnTimerCallback,osTimerOnce,Default,NULL,Dynamic,NULL FREERTOS.configCHECK_FOR_STACK_OVERFLOW=1 FREERTOS.configGENERATE_RUN_TIME_STATS=1 FREERTOS.configMAX_TASK_NAME_LEN=32 @@ -209,7 +211,8 @@ NVIC.PVD_IRQn=true\:5\:0\:true\:true\:true\:1\:false\:true\:true NVIC.PendSV_IRQn=true\:15\:0\:false\:false\:false\:true\:false\:false NVIC.PriorityGroup=NVIC_PRIORITYGROUP_4 NVIC.RCC_IRQn=true\:5\:0\:false\:true\:true\:3\:true\:true\:false -NVIC.RTC_IRQn=true\:5\:0\:true\:false\:true\:false\:true\:true +NVIC.RTC_Alarm_IRQn=true\:5\:0\:false\:false\:true\:true\:true\:true +NVIC.RTC_IRQn=true\:5\:0\:false\:false\:true\:true\:true\:true NVIC.SVCall_IRQn=true\:0\:0\:false\:false\:false\:false\:false\:false NVIC.SavedPendsvIrqHandlerGenerated=true NVIC.SavedSvcallIrqHandlerGenerated=true @@ -520,7 +523,7 @@ PE5.GPIO_PuPd=GPIO_PULLUP PE5.Locked=true PE5.Signal=GPIO_Input PE6.GPIOParameters=GPIO_PuPd,GPIO_Label -PE6.GPIO_Label=PMG_CHRG_STAT1 +PE6.GPIO_Label=PMB_CHRG_STAT1 PE6.GPIO_PuPd=GPIO_PULLUP PE6.Locked=true PE6.Signal=GPIO_Input