/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* 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 "UsbDataHandler.h"
#include "CanDataTask.h"
#include "CLS.h"
#include "fatfs.h"
#include "ulog.h"
#include "stdio.h"
#include "tim.h"
#include "BSP_EE24.h"
#include "BSP_INA.h"
#include "BSP_POWER.h"
#include "LightTask.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 */

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

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

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

/* USER CODE BEGIN 1 */
/* Functions needed when configGENERATE_RUN_TIME_STATS is on */
__weak void configureTimerForRunTimeStats(void)
{
  HAL_TIM_Base_Start(&htim2);
}

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

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */
  BSP_POWER_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, ... */
  /* 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 */
  ULOG_INFO("Setup UsbDataHandler");
  UsbDataHandler_Start();
  ULOG_INFO("Setup CanDataTask");
  CanDataTask_start();
  ULOG_INFO("Setup CLS");
  CLS_Init();
  ULOG_INFO("Setup LightTask");
  LightTask_start();
  /* USER CODE END RTOS_THREADS */

  /* USER CODE BEGIN RTOS_EVENTS */
  /* add events, ... */
  /* USER CODE END RTOS_EVENTS */

}

/* USER CODE BEGIN Header_StartDefaultTask */
/**
  * @brief  Function implementing the defaultTask thread.
  * @param  argument: Not used
  * @retval None
  */

#include "CLS_BSP.h"
#include "CLSAddress.h"
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void *argument)
{
  /* init code for USB_DEVICE */
  MX_USB_DEVICE_Init();
  /* USER CODE BEGIN StartDefaultTask */
  BSP_EE24_Init();
  BSP_INA_Init();

  retSD = f_mount(&SDFatFS,SDPath,0);
  retSD =  f_open(&SDFile, "counter.bin", FA_READ);
  
  uint64_t counter = 0;
  unsigned int size=0;
  if(retSD == 0) {
    retSD = f_read(&SDFile, &counter, sizeof(counter),&size);
  }
  retSD = f_close(&SDFile);
  counter++;
  retSD = f_open(&SDFile, "counter.bin", FA_CREATE_ALWAYS | FA_WRITE);
  retSD = f_write(&SDFile,&counter,sizeof(counter),&size);
  retSD = f_close(&SDFile);

  {
    char output[64];
    snprintf(output,64,"Read SD Bootcount %lld" ,counter);
    ULOG_INFO(output);
  }

  char  INA_LOG[72];

  /* Infinite loop */
  for(;;)
  {
    osDelay(1000);

    uint16_t current = BSP_INA_Current()*10;
    uint16_t voltage = BSP_INA_Voltage();
    uint32_t power = (current * voltage)/1000;  // milli watts
    float power_W = (float)power/1000.0;
    float voltage_V =(float) voltage /1000.0;
    
    snprintf(INA_LOG, sizeof(INA_LOG),"Voltage[mV] %.2f Current[mA] %d P[W]: %.2f", voltage_V, current, power_W);
    ULOG_INFO(INA_LOG);

  }
  /* USER CODE END StartDefaultTask */
}

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

/* USER CODE END Application */

