diff --git a/Core/Inc/gpio.h b/Core/Inc/gpio.h index 54467d2..95bcd54 100644 --- a/Core/Inc/gpio.h +++ b/Core/Inc/gpio.h @@ -39,7 +39,6 @@ void MX_GPIO_Init(void); /* USER CODE BEGIN Prototypes */ -void Beep(uint32_t time_ms); /* USER CODE END Prototypes */ diff --git a/Core/Inc/navikit.h b/Core/Inc/navikit.h index 1ca6a50..662c976 100644 --- a/Core/Inc/navikit.h +++ b/Core/Inc/navikit.h @@ -9,34 +9,36 @@ #define INC_NAVIKIT_H_ #include "stdbool.h" +#include "stm32f1xx_hal.h" +#include "adc.h" +#include "gpio.h" + +#define ADC_CH_COUNT 5 -typedef enum -{ - shutdown, //only mcu runing - runing, //all function runing - sleep, //SOCs and FANs are stop - dfu, //device firmware update for SOM - isp //in system program for BIOS -}sta_t; typedef struct { struct{ - sta_t sta; //current system state - sta_t next_sta; //current system state + enum { + shutdown, //only mcu runing + runing, //all function runing + sleep, //SOCs and FANs are stop + dfu, //device firmware update for SOM + isp //in system program for BIOS + }sta,next_sta; bool power_btn; //开关是否被按下 bool custom_btn; bool pwr_led; bool run_led; }sys; - struct{ + // struct{ - bool fan_1; - bool fan_2; - bool fan_3; - }status; + // bool fan_1; + // bool fan_2; + // bool fan_3; + // }status; struct{ @@ -49,17 +51,19 @@ typedef struct }som;//som power management struct{ + uint32_t adc[ADC_CH_COUNT]; + float out_24v_div16; float out_5v_div8; float out_12v_div8; float bkp_bat_div8; float main_pwr_div16; - bool pmb_temp_sen_en; - bool pmb_temp_sen_alt; - bool pmb_coulomb_alcc; - bool pmb_chrg_stat2; - bool pmg_chrg_stat1; + bool temp_sen_en; + bool temp_sen_alt; + bool coulomb_alcc; + bool chrg_stat2; + bool chrg_stat1; struct{ float voltage; @@ -109,5 +113,8 @@ typedef struct extern NaviKit_t NaviKit; void NaviKit_var_init(); +void Beep(uint32_t time_ms); + +void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc); #endif /* INC_NAVIKIT_H_ */ diff --git a/Core/Src/adc.c b/Core/Src/adc.c index 4298000..ab5b528 100644 --- a/Core/Src/adc.c +++ b/Core/Src/adc.c @@ -35,12 +35,12 @@ void MX_ADC1_Init(void) /** Common config */ hadc1.Instance = ADC1; - hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE; + hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE; hadc1.Init.ContinuousConvMode = ENABLE; hadc1.Init.DiscontinuousConvMode = DISABLE; hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; - hadc1.Init.NbrOfConversion = 1; + hadc1.Init.NbrOfConversion = 5; if (HAL_ADC_Init(&hadc1) != HAL_OK) { Error_Handler(); @@ -49,7 +49,39 @@ void MX_ADC1_Init(void) */ sConfig.Channel = ADC_CHANNEL_3; sConfig.Rank = ADC_REGULAR_RANK_1; - sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5; + sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5; + if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) + { + Error_Handler(); + } + /** Configure Regular Channel + */ + sConfig.Channel = ADC_CHANNEL_4; + sConfig.Rank = ADC_REGULAR_RANK_2; + if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) + { + Error_Handler(); + } + /** Configure Regular Channel + */ + sConfig.Channel = ADC_CHANNEL_5; + sConfig.Rank = ADC_REGULAR_RANK_3; + if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) + { + Error_Handler(); + } + /** Configure Regular Channel + */ + sConfig.Channel = ADC_CHANNEL_6; + sConfig.Rank = ADC_REGULAR_RANK_4; + if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) + { + Error_Handler(); + } + /** Configure Regular Channel + */ + sConfig.Channel = ADC_CHANNEL_7; + sConfig.Rank = ADC_REGULAR_RANK_5; if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { Error_Handler(); diff --git a/Core/Src/freertos.c b/Core/Src/freertos.c index 8d6e22c..22dff51 100644 --- a/Core/Src/freertos.c +++ b/Core/Src/freertos.c @@ -33,6 +33,7 @@ #include "navikit.h" #include "coulomb.h" #include "stdbool.h" +#include "adc.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -96,10 +97,10 @@ const osThreadAttr_t StateSwitchTask_attributes = { .priority = (osPriority_t) osPriorityLow, .stack_size = 128 * 4 }; -/* Definitions for SOMPowerManageTask */ -osThreadId_t SOMPowerManageTaskHandle; -const osThreadAttr_t SOMPowerManageTask_attributes = { - .name = "SOMPowerManageTask", +/* Definitions for PowerMonitTask */ +osThreadId_t PowerMonitTaskHandle; +const osThreadAttr_t PowerMonitTask_attributes = { + .name = "PowerMonitTask", .priority = (osPriority_t) osPriorityLow, .stack_size = 128 * 4 }; @@ -114,7 +115,7 @@ void StartIWDGRefreshTask(void *argument); void StartEventDetect(void *argument); void StartCoulombRead(void *argument); void StartStateSwitchTask(void *argument); -void StartSOMPowerManageTask(void *argument); +void StartPowerMonitTask(void *argument); extern void MX_USB_DEVICE_Init(void); void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */ @@ -164,8 +165,8 @@ void MX_FREERTOS_Init(void) { /* creation of StateSwitchTask */ StateSwitchTaskHandle = osThreadNew(StartStateSwitchTask, NULL, &StateSwitchTask_attributes); - /* creation of SOMPowerManageTask */ - SOMPowerManageTaskHandle = osThreadNew(StartSOMPowerManageTask, NULL, &SOMPowerManageTask_attributes); + /* creation of PowerMonitTask */ + PowerMonitTaskHandle = osThreadNew(StartPowerMonitTask, NULL, &PowerMonitTask_attributes); /* USER CODE BEGIN RTOS_THREADS */ /* add threads, ... */ @@ -192,7 +193,7 @@ void StartDefaultTask(void *argument) for(;;) { osDelay(1000); - printf("[%f]\n",(float)(osKernelGetTickCount()/1000.0)); + // printf("[%f]\n",(float)(osKernelGetTickCount()/1000.0)); } /* USER CODE END StartDefaultTask */ } @@ -462,39 +463,53 @@ void StartStateSwitchTask(void *argument) /* USER CODE END StartStateSwitchTask */ } -/* USER CODE BEGIN Header_StartSOMPowerManageTask */ +/* USER CODE BEGIN Header_StartPowerMonitTask */ /** -* @brief Function implementing the SOMPowerManageTask thread. +* @brief Function: Monit som power status and PMB status * @param argument: Not used * @retval None */ -/* USER CODE END Header_StartSOMPowerManageTask */ -void StartSOMPowerManageTask(void *argument) +/* USER CODE END Header_StartPowerMonitTask */ +void StartPowerMonitTask(void *argument) { - /* USER CODE BEGIN StartSOMPowerManageTask */ + /* USER CODE BEGIN StartPowerMonitTask */ + HAL_ADC_Start_DMA(&hadc1, &(NaviKit.pmb.adc), ADC_CH_COUNT); /* Infinite loop */ for(;;) { - osDelay(1); + osDelay(1000); + // printf("out_24v: %f \n",NaviKit.pmb.out_24v_div16); + // printf("out_5v: %f \n",NaviKit.pmb.out_5v_div8); + // printf("out_12v: %f \n",NaviKit.pmb.out_12v_div8); + // printf("bkp_bat: %f \n",NaviKit.pmb.bkp_bat_div8); + // printf("main_pwr: %f \n",NaviKit.pmb.main_pwr_div16); -// //module request to stop -// if(HAL_GPIO_ReadPin(SOM_SHUTDOWN_REQ_GPIO_Port,SOM_SHUTDOWN_REQ_Pin) == GPIO_PIN_SET) -// { -// //1.the last step of normal power off process -// //2.Thermal shutdown -// //3.vdd_in's wave more than 5% of 5.0V -// -// -// HAL_GPIO_WritePin(SOM_POWER_EN_GPIO_Port,SOM_POWER_EN_Pin,GPIO_PIN_RESET); -// } + printf("out_24v: %f \n",NaviKit.pmb.adc[0]); + printf("out_5v: %f \n",NaviKit.pmb.adc[1]); + printf("out_12v: %f \n",NaviKit.pmb.adc[2]); + printf("bkp_bat: %f \n",NaviKit.pmb.adc[3]); + printf("main_pwr: %f \n",NaviKit.pmb.adc[4]); + + + // //module request to stop + // if(HAL_GPIO_ReadPin(SOM_SHUTDOWN_REQ_GPIO_Port,SOM_SHUTDOWN_REQ_Pin) == GPIO_PIN_SET) + // { + // //1.the last step of normal power off process + // //2.Thermal shutdown + // //3.vdd_in's wave more than 5% of 5.0V + // + // + // HAL_GPIO_WritePin(SOM_POWER_EN_GPIO_Port,SOM_POWER_EN_Pin,GPIO_PIN_RESET); + // } } - /* USER CODE END StartSOMPowerManageTask */ + /* USER CODE END StartPowerMonitTask */ } /* Private application code --------------------------------------------------*/ /* USER CODE BEGIN Application */ + //write "bios update flag" to bkp register, and reset system void enter_isp_state() { diff --git a/Core/Src/gpio.c b/Core/Src/gpio.c index 3bfa397..744e751 100644 --- a/Core/Src/gpio.c +++ b/Core/Src/gpio.c @@ -190,60 +190,7 @@ void MX_GPIO_Init(void) /* USER CODE BEGIN 2 */ -void Beep(uint32_t time_ms) -{ - HAL_GPIO_WritePin(SYS_BUZZ_CTL_GPIO_Port,SYS_BUZZ_CTL_Pin, GPIO_PIN_RESET); - osDelay(time_ms); - HAL_GPIO_WritePin(SYS_BUZZ_CTL_GPIO_Port,SYS_BUZZ_CTL_Pin, GPIO_PIN_SET); - osDelay(time_ms); -} -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_SET) - {//Rising edge trigger - NaviKit.som.shutdown_req = true; - if(NaviKit.sys.sta == runing) - { - NaviKit.sys.next_sta = shutdown; - printf("enter shutdown mode... \n"); - } - } - if(HAL_GPIO_ReadPin(SOM_SHUTDOWN_REQ_GPIO_Port, SOM_SHUTDOWN_REQ_Pin)==GPIO_PIN_RESET) - {//falling 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 - NaviKit.sys.power_btn = true; - printf("power_btn pushed \n"); - } - if(HAL_GPIO_ReadPin(SYS_POWER_BTN_GPIO_Port, SYS_POWER_BTN_Pin)==GPIO_PIN_RESET) - {//falling edge trigger - NaviKit.sys.power_btn = false; - printf("power_btn released \n"); - } - } - 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; - printf("custom_btn released \n"); - } - if(HAL_GPIO_ReadPin(SYS_CUSTOM_BTN_GPIO_Port, SYS_CUSTOM_BTN_Pin)==GPIO_PIN_RESET) - {//falling edge trigger - NaviKit.sys.custom_btn = true; - printf("custom_btn pushed \n"); - } - } - -} /* USER CODE END 2 */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/Core/Src/navikit.c b/Core/Src/navikit.c index cca76f6..c7b4868 100644 --- a/Core/Src/navikit.c +++ b/Core/Src/navikit.c @@ -13,3 +13,72 @@ void NaviKit_var_init() NaviKit.sys.sta = shutdown; NaviKit.sys.next_sta = shutdown; } + +void Beep(uint32_t time_ms) +{ + 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); +} + + +void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) +{ + if(hadc->Instance == ADC1) + { + NaviKit.pmb.out_24v_div16 = (float)((NaviKit.pmb.adc[0]<<4) / 52.8); + NaviKit.pmb.out_5v_div8 = (float)((NaviKit.pmb.adc[1]<<3) / 52.8); + NaviKit.pmb.out_12v_div8 = (float)((NaviKit.pmb.adc[2]<<3) / 52.8); + NaviKit.pmb.bkp_bat_div8 = (float)((NaviKit.pmb.adc[3]<<3) / 52.8); + NaviKit.pmb.main_pwr_div16 = (float)((NaviKit.pmb.adc[4]<<4) / 52.8); + } +} + +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_SET) + {//Rising edge trigger + NaviKit.som.shutdown_req = true; + if(NaviKit.sys.sta == runing) + { + NaviKit.sys.next_sta = shutdown; + printf("enter shutdown mode... \n"); + } + } + if(HAL_GPIO_ReadPin(SOM_SHUTDOWN_REQ_GPIO_Port, SOM_SHUTDOWN_REQ_Pin)==GPIO_PIN_RESET) + {//falling 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 + NaviKit.sys.power_btn = true; + printf("power_btn pushed \n"); + } + if(HAL_GPIO_ReadPin(SYS_POWER_BTN_GPIO_Port, SYS_POWER_BTN_Pin)==GPIO_PIN_RESET) + {//falling edge trigger + NaviKit.sys.power_btn = false; + printf("power_btn released \n"); + } + } + 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; + printf("custom_btn released \n"); + } + if(HAL_GPIO_ReadPin(SYS_CUSTOM_BTN_GPIO_Port, SYS_CUSTOM_BTN_Pin)==GPIO_PIN_RESET) + {//falling edge trigger + NaviKit.sys.custom_btn = true; + printf("custom_btn pushed \n"); + } + } + +} diff --git a/NaviKit_stm32.ioc b/NaviKit_stm32.ioc index be000b6..4a305b9 100644 --- a/NaviKit_stm32.ioc +++ b/NaviKit_stm32.ioc @@ -1,10 +1,23 @@ #MicroXplorer Configuration settings - do not modify ADC1.Channel-0\#ChannelRegularConversion=ADC_CHANNEL_3 +ADC1.Channel-1\#ChannelRegularConversion=ADC_CHANNEL_4 +ADC1.Channel-2\#ChannelRegularConversion=ADC_CHANNEL_5 +ADC1.Channel-3\#ChannelRegularConversion=ADC_CHANNEL_6 +ADC1.Channel-4\#ChannelRegularConversion=ADC_CHANNEL_7 ADC1.ContinuousConvMode=ENABLE -ADC1.IPParameters=Rank-0\#ChannelRegularConversion,Channel-0\#ChannelRegularConversion,SamplingTime-0\#ChannelRegularConversion,NbrOfConversionFlag,master,ContinuousConvMode +ADC1.IPParameters=Rank-0\#ChannelRegularConversion,Channel-0\#ChannelRegularConversion,SamplingTime-0\#ChannelRegularConversion,NbrOfConversionFlag,master,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 +ADC1.NbrOfConversion=5 ADC1.NbrOfConversionFlag=1 ADC1.Rank-0\#ChannelRegularConversion=1 -ADC1.SamplingTime-0\#ChannelRegularConversion=ADC_SAMPLETIME_1CYCLE_5 +ADC1.Rank-1\#ChannelRegularConversion=2 +ADC1.Rank-2\#ChannelRegularConversion=3 +ADC1.Rank-3\#ChannelRegularConversion=4 +ADC1.Rank-4\#ChannelRegularConversion=5 +ADC1.SamplingTime-0\#ChannelRegularConversion=ADC_SAMPLETIME_239CYCLES_5 +ADC1.SamplingTime-1\#ChannelRegularConversion=ADC_SAMPLETIME_239CYCLES_5 +ADC1.SamplingTime-2\#ChannelRegularConversion=ADC_SAMPLETIME_239CYCLES_5 +ADC1.SamplingTime-3\#ChannelRegularConversion=ADC_SAMPLETIME_239CYCLES_5 +ADC1.SamplingTime-4\#ChannelRegularConversion=ADC_SAMPLETIME_239CYCLES_5 ADC1.master=1 ARM.CMSIS.5.6.0.CMSISJjCORE=true ARM.CMSIS.5.6.0.CMSISJjDSP=Library @@ -45,7 +58,7 @@ FREERTOS.HEAP_NUMBER=4 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 -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;SOMPowerManageTask,8,128,StartSOMPowerManageTask,Default,NULL,Dynamic,NULL,NULL +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.configGENERATE_RUN_TIME_STATS=0 FREERTOS.configMAX_TASK_NAME_LEN=32 FREERTOS.configTOTAL_HEAP_SIZE=8192