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

#define LOG_TAG	"TH-Default"
/* 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 "navikit.h"
#include <th_cdc.h>
#include <th_demo.h>
#include <th_elog.h>
#include <th_led.h>
#include <th_coulomb.h>
#include <th_power.h>
#include <th_exti.h>
#include <th_iwdg.h>
#include <th_info.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 */

osTimerId_t IdleStateHoldTimerHandle;
const osTimerAttr_t IdleStateHoldTimer_attributes = {
  .name = "IdleStateHoldTimer"
};

/* USER CODE END Variables */
/* Definitions for defaultTask */
osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes = {
  .name = "defaultTask",
  .stack_size = 256 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

void IdleStateHoldTimerCallback(void *argument);

bool isWakeUpFromReset();

//judge reset source flag
uint8_t ResetSourceJudge();

/* USER CODE END FunctionPrototypes */

void StartDefaultTask(void *argument);

extern void MX_USB_DEVICE_Init(void);
void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/* Hook prototypes */
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. */

	while(1);
}
/* 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 */

//	ElogUartBinarySemHandle = osSemaphoreNew(1, 1, &ElogUartBinarySem_attributes);
	ElogOutputBinarySemHandle = osSemaphoreNew(1, 1, &ElogOutputBinarySem_attributes);
	my_elog_init();

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

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  PwrBtnLongPressTimerHandle = osTimerNew(PwrBtnLongPressTimerCallback, osTimerOnce, NULL, &PwrBtnLongPressTimer_attributes);
  CustBtnLongPressTimerHandle = osTimerNew(CustBtnLongPressTimerCallback, osTimerOnce, NULL, &CustBtnLongPressTimer_attributes);
  PwrBtnShortPressTimerHandle = osTimerNew(PwrBtnShortPressTimerCallback, osTimerOnce, NULL, &PwrBtnShortPressTimer_attributes);
  CustBtnShortPressTimerHandle = osTimerNew(CustBtnShortPressTimerCallback, osTimerOnce, NULL, &CustBtnShortPressTimer_attributes);
  IdleStateHoldTimerHandle = osTimerNew(IdleStateHoldTimerCallback, osTimerOnce, NULL, &IdleStateHoldTimer_attributes);

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

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  //IWDG--------------------------------------------
#ifdef IWDG_ENABLE
  IWDGTaskHandle = osThreadNew(IWDGTask, NULL, &IWDGTask_attributes);
#endif

	//Power--------------------------------------------
  PowerMonitTaskHandle = osThreadNew(PowerMonitTask, NULL, &PowerMonitTask_attributes);


  //Button--------------------------------------------
  ExtiServiceTaskHandle = osThreadNew(ExtiServiceTask, NULL, &ExtiServiceTask_attributes);


  //LED--------------------------------------------
  LedBlinkTaskHandle = osThreadNew(LedBlinkTask, NULL, &LedBlinkTask_attributes);


  //CDC--------------------------------------------
  cdcMonitorTaskHandle = osThreadNew(CdcMonitorTask, NULL, &cdcMonitorTask_attributes);


  //Coulomb--------------------------------------------
// CoulombTaskHandle = osThreadNew(CoulombTask, NULL, &CoulombTask_attributes);


  //Elog--------------------------------------------
//   ElogInitTaskHandle = osThreadNew(ElogInitTask, NULL, &ElogInitTask_attributes);
#ifdef ELOG_BUF_OUTPUT_ENABLE
	ElogFlushTaskHandle = osThreadNew(ElogFlushTask, 1, &ElogFlushTask_attributes);
#endif

  //Demo--------------------------------------------
//	DemoTask1Handle = osThreadNew(DemoTask1, NULL, &DemoTask1_attributes);
//	DemoTask2Handle = osThreadNew(DemoTask2, NULL, &DemoTask2_attributes);


	//InfoOutput--------------------------------------------
	InfoOutputTaskHandle = osThreadNew(InfoOutputTask, NULL, &InfoOutputTask_attributes);

  /* USER CODE END RTOS_THREADS */

  /* USER CODE BEGIN RTOS_EVENTS */
  /* add events, ... */
  // ExtiEventFlags = osEventFlagsNew(&ExtiEventFlags_attributes);
  // PowerMonitorEventFlags = osEventFlagsNew(&PowerMonitorEventFlags_attributes);
  /* USER CODE END RTOS_EVENTS */

}

/* USER CODE BEGIN Header_DefaultTask */
/**
  * @brief  Function implementing the defaultTask thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_DefaultTask */
void StartDefaultTask(void *argument)
{
  /* init code for USB_DEVICE */
  MX_USB_DEVICE_Init();
  /* USER CODE BEGIN StartDefaultTask */
//	 ResetSourceJudge();
	HAL_GPIO_WritePin(USB2_FS_ENUM_CTL_GPIO_Port,USB2_FS_ENUM_CTL_Pin, GPIO_PIN_SET);
	if(isWakeUpFromReset()){//judge reset source form "wakeup event"

		__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
		__HAL_PWR_CLEAR_FLAG(PWR_FLAG_SB);

		if(HAL_GPIO_ReadPin(SYS_POWER_BTN_GPIO_Port, SYS_POWER_BTN_Pin)==GPIO_PIN_SET){
			if(HAL_GPIO_ReadPin(SYS_CUSTOM_BTN_GPIO_Port, SYS_CUSTOM_BTN_Pin)==GPIO_PIN_RESET)
				NaviKit.sys.next_sta = dfu;
			log_i("EC Reset source: [Power Button WakeUP]");
		}else{
			NaviKit.sys.next_sta = idle;
			log_i("EC Reset source: [RTC WakeUP]");
			enter_standby_state(0);
		}
	}else{//judge reset source "power on"
		log_i("EC Reset source: [PowerON]");
		if(HAL_GPIO_ReadPin(SYS_CUSTOM_BTN_GPIO_Port, SYS_CUSTOM_BTN_Pin)==GPIO_PIN_RESET){
			log_d("EC next state isp");
			NaviKit.sys.next_sta = isp;
		}else{
			NaviKit.sys.next_sta = idle;
		}
		TaskBeep(50,1);
	}

 	log_i("Core initial successfully");

  /* Infinite loop */
  for(;;)
  {
	osDelay(50);
    if(NaviKit.sys.sta != NaviKit.sys.next_sta){
    	switch(NaviKit.sys.next_sta){
			case standby:  {
				enter_standby_state(100);
			}break;
			case idle:  {
				enter_idle_state(100);
		    	NaviKit.sys.sta = NaviKit.sys.next_sta;
			} break;
			case run: {
				if(NaviKit.pmb.main_pwr_good){
					log_d("Main power good.");
					enter_run_state(100);
					NaviKit.sys.sta = NaviKit.sys.next_sta;
				}else{
					NaviKit.sys.next_sta = NaviKit.sys.sta;
					TaskBeep(500,2);
					log_e("Retry after main power checked please.");
				}
			}break;
			case sleep:{
				enter_sleep_state(100);
		    	NaviKit.sys.sta = NaviKit.sys.next_sta;
			}break;
			case dfu:{
				if(NaviKit.pmb.main_pwr_good){
					log_d("Main power good.");
					enter_dfu_state(100);
					NaviKit.sys.sta = NaviKit.sys.next_sta;
				}else{
					NaviKit.sys.next_sta = NaviKit.sys.sta;
					TaskBeep(500,2);
					log_e("Retry after main power checked please.");
				}
			}break;
			case isp:{
				enter_isp_state();
		    	NaviKit.sys.sta = NaviKit.sys.next_sta;
			}break;
    	}
    }
	  if(NaviKit.sys.sta == idle && NaviKit.sys.next_sta == idle){//idle state
//			  if((NaviKit.pmb.sta.chrg_stat1 && NaviKit.pmb.sta.chrg_stat2)
//			  || (NaviKit.pmb.sta.chrg_stat1 && !NaviKit.pmb.sta.chrg_stat2)){//Not Charge or float charge
//				  NaviKit.sys.next_sta = standby;
//			  }
		  if(!osTimerIsRunning(IdleStateHoldTimerHandle)){
		  	osTimerStart(IdleStateHoldTimerHandle,5000);
            log_i("Idle state hold timer started.");
		  }
	  }
  }
  /* USER CODE END StartDefaultTask */
}

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


void IdleStateHoldTimerCallback(void *argument){
	if(NaviKit.sys.sta == idle && NaviKit.sys.next_sta == idle){
	  log_i("Idle state duration more than 5000ms.");
	  NaviKit.sys.next_sta = standby;
	}
}
bool isWakeUpFromReset(){
	return  __HAL_PWR_GET_FLAG(PWR_FLAG_WU);
}

//judge reset source flag
uint8_t ResetSourceJudge(){
	uint8_t rst_src=0;
	rst_src = __HAL_RCC_GET_FLAG(RCC_FLAG_PINRST) ? RCC_FLAG_PINRST : 0;
	rst_src = __HAL_RCC_GET_FLAG(RCC_FLAG_PORRST) ? RCC_FLAG_PORRST : 0;
	rst_src = __HAL_RCC_GET_FLAG(RCC_FLAG_SFTRST) ? RCC_FLAG_SFTRST : 0;
	rst_src = __HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST) ? RCC_FLAG_IWDGRST : 0;
	rst_src = __HAL_RCC_GET_FLAG(RCC_FLAG_WWDGRST) ? RCC_FLAG_WWDGRST : 0;
	rst_src = __HAL_RCC_GET_FLAG(RCC_FLAG_LPWRRST) ? RCC_FLAG_LPWRRST : 0;
	switch (rst_src){
		case  RCC_FLAG_PINRST :{
			log_i("Reset source RCC_FLAG_PINRST");
		}break;
		case  RCC_FLAG_PORRST :{
			log_i("Reset source RCC_FLAG_PORRST");
		}break;
		case  RCC_FLAG_SFTRST :{
			log_i("Reset source RCC_FLAG_SFTRST");
		}break;
		case  RCC_FLAG_IWDGRST :{
			log_i("Reset source RCC_FLAG_IWDGRST");
		}break;
		case  RCC_FLAG_WWDGRST :{
			log_i("Reset source RCC_FLAG_WWDGRST");
		}break;
		case  RCC_FLAG_LPWRRST :{
			log_i("Reset source RCC_FLAG_LPWRRST");
		}break;
	}
	return rst_src;
}

/* USER CODE END Application */

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