/*
 * @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 */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under Ultimate Liberty license
  * SLA0044, the "License"; You may not use this file except in compliance with
  * the License. You may obtain a copy of the License at:
  *                             www.st.com/SLA0044
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "timers.h"
#include "iwdg.h"
#include "gpio.h"
#include "usart.h"
#include "navikit.h"
#include "coulomb.h"
#include "stdbool.h"
#include "adc.h"
#include "i2c.h"
#include "log.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* 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;
const osThreadAttr_t defaultTask_attributes = {
  .name = "defaultTask",
  .priority = (osPriority_t) osPriorityNormal,
  .stack_size = 128 * 4
};
/* Definitions for LedBlinkTask */
osThreadId_t LedBlinkTaskHandle;
const osThreadAttr_t LedBlinkTask_attributes = {
  .name = "LedBlinkTask",
  .priority = (osPriority_t) osPriorityLow,
  .stack_size = 128 * 4
};
/* Definitions for CoulombRead */
osThreadId_t CoulombReadHandle;
const osThreadAttr_t CoulombRead_attributes = {
  .name = "CoulombRead",
  .priority = (osPriority_t) osPriorityLow,
  .stack_size = 128 * 4
};
/* Definitions for PowerMonitTask */
osThreadId_t PowerMonitTaskHandle;
const osThreadAttr_t PowerMonitTask_attributes = {
  .name = "PowerMonitTask",
  .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 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 StartCoulombRead(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) */

/* Hook prototypes */
void configureTimerForRunTimeStats(void);
unsigned long getRunTimeCounterValue(void);
void vApplicationIdleHook(void);
void vApplicationTickHook(void);
void vApplicationStackOverflowHook(xTaskHandle xTask, signed char *pcTaskName);

/* USER CODE BEGIN 1 */
/* Functions needed when configGENERATE_RUN_TIME_STATS is on */
__weak void configureTimerForRunTimeStats(void)
{

}

__weak unsigned long getRunTimeCounterValue(void)
{
	return osKernelGetTickCount();
}
/* USER CODE END 1 */

/* USER CODE BEGIN 2 */
void vApplicationIdleHook( void )
{
   /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
   to 1 in FreeRTOSConfig.h. It will be called on each iteration of the idle
   task. It is essential that code added to this hook function never attempts
   to block in any way (for example, call xQueueReceive() with a block time
   specified, or call vTaskDelay()). If the application makes use of the
   vTaskDelete() API function (as this demo application does) then it is also
   important that vApplicationIdleHook() is permitted to return to its calling
   function, because it is the responsibility of the idle task to clean up
   memory allocated by the kernel to any task that has since been deleted. */
}
/* USER CODE END 2 */

/* USER CODE BEGIN 3 */
void vApplicationTickHook( void )
{
   /* This function will be called by each tick interrupt if
   configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h. User code can be
   added here, but the tick hook is called from an interrupt context, so
   code must not attempt to block, and only the interrupt safe FreeRTOS API
   functions can be used (those that end in FromISR()). */
}
/* USER CODE END 3 */

/* USER CODE BEGIN 4 */
void vApplicationStackOverflowHook(xTaskHandle xTask, signed char *pcTaskName)
{
   /* Run time stack overflow checking is performed if
   configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook function is
   called if a stack overflow is detected. */
}
/* USER CODE END 4 */

/* USER CODE BEGIN PREPOSTSLEEP */
__weak void PreSleepProcessing(uint32_t *ulExpectedIdleTime)
{
/* place for user code */

}

__weak void PostSleepProcessing(uint32_t *ulExpectedIdleTime)
{
/* place for user code */

}
/* USER CODE END PREPOSTSLEEP */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */
       
  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* 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 */

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* creation of defaultTask */
  defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);

  /* 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);

  /* 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 */

}

/* 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);
	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");

	Log(info,"EC Firmware: %s[%s], build: %s, %s, by STD:%u",APP_VERSION,DEPLOY_MODE,__DATE__ ,__TIME__,__STDC_VERSION__  );
	Log(info,"HAL Version: %u ", HAL_GetHalVersion());
	Log(info,"Revision ID: %u ", HAL_GetREVID());
	Log(info,"Device ID: %u ", HAL_GetDEVID());
	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,"Core initial successfully");
	Log(info,"----------------------------------------------");

	TaskBeep(50,1);
  /* Infinite loop */
  for(;;)
  {
//	 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 */
}

/* USER CODE BEGIN Header_StartLedBlinkTask */
/**
* @brief Function implementing the LedBlinkTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartLedBlinkTask */
void StartLedBlinkTask(void *argument)
{
  /* USER CODE BEGIN StartLedBlinkTask */
  /* Infinite loop */
	for(;;)
	{
	  osDelay(1000);
		switch(NaviKit.sys.sta){
		case run: {
			  if(!PWR_Status(SYS_PWR_LED)){
				PWR_Enable(SYS_PWR_LED,true,100);//turn on power led
			  }

			  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
				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;
		}
	}
  /* USER CODE END StartLedBlinkTask */
}

/* USER CODE BEGIN Header_StartCoulombRead */
/**
* @brief Function implementing the CoulombRead thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartCoulombRead */
void StartCoulombRead(void *argument)
{
  /* USER CODE BEGIN StartCoulombRead */
	//写寄存器方法
  coulomb_write_config_load();
  coulomb_write_config_actual_to_raw();
  coulomb_write_config();
  //读寄存器方法
  coulomb_read_status_and_config();
  coulomb_read_status_raw_to_actual();
  coulomb_read_config_raw_to_actual();

  /* Infinite loop */
  for(;;)
  {
    coulomb_read_status_and_config();
    coulomb_read_status_raw_to_actual();
	  if(NaviKit.sys.sta == run)
		  osDelay(500);
	  else
		  osDelay(5000);
  }
  /* USER CODE END StartCoulombRead */
}

/* USER CODE BEGIN Header_StartPowerMonitTask */
/**
* @brief Function: Monit som power status and PMB status
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartPowerMonitTask */
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);
  /* Infinite loop */
  for(;;)
  {
		//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));


		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){
				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.main_pwr > 30){
				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(CustBtnTimerHandle,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 
    	  PWR_Enable(SOM_RESET,true,10);
    	  PWR_Enable(SOM_RESET,false,0);
    	  Log(info,"SOM Reset.");
      }break;
      case idle:{
      }break;
      case dfu:{
      }break;
      case sleep:{
      }break;
      case isp:{
      }break;
    }
  /* USER CODE END CustBtnTimerCallback */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */




//printf redirect
#ifdef __GNUC__
//#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#define PUTCHAR_PROTOTYPE int putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif

PUTCHAR_PROTOTYPE
{
	while(HAL_UART_GetState(&huart1) == HAL_UART_STATE_BUSY_TX){}
	HAL_UART_Transmit(&huart1,(uint8_t *)&ch,1,1);
	return ch;
}

/* USER CODE END Application */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
