/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.h
  * @brief          : Header for main.c file.
  *                   This file contains the common defines of the application.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2023 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 */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __MAIN_H
#define __MAIN_H

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "stm32f7xx_hal.h"

#include "stm32f7xx_ll_dma.h"
#include "stm32f7xx_ll_spi.h"
#include "stm32f7xx_ll_tim.h"
#include "stm32f7xx_ll_usart.h"
#include "stm32f7xx_ll_rcc.h"
#include "stm32f7xx_ll_bus.h"
#include "stm32f7xx_ll_cortex.h"
#include "stm32f7xx_ll_system.h"
#include "stm32f7xx_ll_utils.h"
#include "stm32f7xx_ll_pwr.h"
#include "stm32f7xx_ll_gpio.h"

#include "stm32f7xx_ll_exti.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Exported types ------------------------------------------------------------*/
/* USER CODE BEGIN ET */

/* USER CODE END ET */

/* Exported constants --------------------------------------------------------*/
/* USER CODE BEGIN EC */

/* USER CODE END EC */

/* Exported macro ------------------------------------------------------------*/
/* USER CODE BEGIN EM */

/* USER CODE END EM */

void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);

/* Exported functions prototypes ---------------------------------------------*/
void Error_Handler(void);

/* USER CODE BEGIN EFP */
void Set_LTEC(uint8_t, uint16_t);
/* USER CODE END EFP */

/* Private defines -----------------------------------------------------------*/
#define INP_0_Pin GPIO_PIN_3
#define INP_0_GPIO_Port GPIOF
#define INP_1_Pin GPIO_PIN_4
#define INP_1_GPIO_Port GPIOF
#define ADC_MPD2_CS_Pin GPIO_PIN_6
#define ADC_MPD2_CS_GPIO_Port GPIOF
#define SPI5_CNV_Pin GPIO_PIN_9
#define SPI5_CNV_GPIO_Port GPIOF
#define ADC_ThrLD2_CS_Pin GPIO_PIN_10
#define ADC_ThrLD2_CS_GPIO_Port GPIOF
#define EN_5V2_Pin GPIO_PIN_2
#define EN_5V2_GPIO_Port GPIOC
#define EN_5V1_Pin GPIO_PIN_3
#define EN_5V1_GPIO_Port GPIOC
#define TECEN1_Pin GPIO_PIN_0
#define TECEN1_GPIO_Port GPIOA
#define TECEN2_Pin GPIO_PIN_1
#define TECEN2_GPIO_Port GPIOA
#define REF2_ON_Pin GPIO_PIN_3
#define REF2_ON_GPIO_Port GPIOA
#define DAC_TEC2_CS_Pin GPIO_PIN_4
#define DAC_TEC2_CS_GPIO_Port GPIOA
#define DAC_LD2_CS_Pin GPIO_PIN_6
#define DAC_LD2_CS_GPIO_Port GPIOA
#define LD2_EN_Pin GPIO_PIN_4
#define LD2_EN_GPIO_Port GPIOC
#define TEC2_PD_Pin GPIO_PIN_5
#define TEC2_PD_GPIO_Port GPIOC
#define TEC2_FLAG1_Pin GPIO_PIN_11
#define TEC2_FLAG1_GPIO_Port GPIOF
#define TEC2_FLAG2_Pin GPIO_PIN_12
#define TEC2_FLAG2_GPIO_Port GPIOF
#define TEC1_FLAG1_Pin GPIO_PIN_13
#define TEC1_FLAG1_GPIO_Port GPIOF
#define TEC1_FLAG2_Pin GPIO_PIN_14
#define TEC1_FLAG2_GPIO_Port GPIOF
#define ADC_MPD1_CS_Pin GPIO_PIN_10
#define ADC_MPD1_CS_GPIO_Port GPIOE
#define ADC_ThrLD1_CS_Pin GPIO_PIN_11
#define ADC_ThrLD1_CS_GPIO_Port GPIOE
#define SPI4_CNV_Pin GPIO_PIN_14
#define SPI4_CNV_GPIO_Port GPIOE
#define REF0_EN_Pin GPIO_PIN_10
#define REF0_EN_GPIO_Port GPIOB
#define TEC1_PD_Pin GPIO_PIN_11
#define TEC1_PD_GPIO_Port GPIOB
#define DAC_TEC1_CS_Pin GPIO_PIN_12
#define DAC_TEC1_CS_GPIO_Port GPIOB
#define DAC_LD1_CS_Pin GPIO_PIN_14
#define DAC_LD1_CS_GPIO_Port GPIOB
#define LD1_EN_Pin GPIO_PIN_8
#define LD1_EN_GPIO_Port GPIOD
#define USB_FLAG_Pin GPIO_PIN_8
#define USB_FLAG_GPIO_Port GPIOA
#define SDMMC1_EN_Pin GPIO_PIN_0
#define SDMMC1_EN_GPIO_Port GPIOD
#define TEST_01_Pin GPIO_PIN_1
#define TEST_01_GPIO_Port GPIOD
#define OUT_0_Pin GPIO_PIN_10
#define OUT_0_GPIO_Port GPIOG
#define OUT_1_Pin GPIO_PIN_11
#define OUT_1_GPIO_Port GPIOG
#define OUT_2_Pin GPIO_PIN_12
#define OUT_2_GPIO_Port GPIOG
#define OUT_3_Pin GPIO_PIN_13
#define OUT_3_GPIO_Port GPIOG
#define OUT_4_Pin GPIO_PIN_14
#define OUT_4_GPIO_Port GPIOG
#define OUT_5_Pin GPIO_PIN_15
#define OUT_5_GPIO_Port GPIOG
#define OUT_6_Pin GPIO_PIN_4
#define OUT_6_GPIO_Port GPIOB
#define OUT_7_Pin GPIO_PIN_5
#define OUT_7_GPIO_Port GPIOB
#define OUT_8_Pin GPIO_PIN_6
#define OUT_8_GPIO_Port GPIOB
#define OUT_9_Pin GPIO_PIN_7
#define OUT_9_GPIO_Port GPIOB

/* USER CODE BEGIN Private defines */
	#define CL_16 15
	#define DL_16 15
	#define CL_8  30
	#define DL_8  30
	#define TSK_8 32
	#define TSK_16 16

	
//	#define SD_Length 100
	
	#define HALT 0
	#define DECODE_ENABLE 1
	#define DEFAULT_ENABLE 2
	#define TRANS_S_ENABLE 3
	#define TRANS_ENABLE 4
	#define REMOVE_FILE 5
	#define STATE 6
	#define WORK_ENABLE 7
  #define DECODE_TASK 8
  #define RUN_TASK 9
	
	#define SD_ERR 0x01	
	#define UART_ERR 0x02
	#define UART_DECODE_ERR 0x04
	#define TEC1_ERR 0x08
	#define TEC2_ERR 0x10
	#define DEFAUL_ERR 0x20
	#define REMOVE_ERR 0x40
	
	#define NO_MESS 0
	#define MESS_01 1
	#define MESS_02 2
	#define MESS_03 3
	
	typedef struct{
	
		uint8_t WORK_EN;
		uint8_t U5V1_EN;
		uint8_t U5V2_EN;
		uint8_t LD1_EN;
		uint8_t LD2_EN;
		uint8_t REF1_EN;
		uint8_t REF2_EN;
		uint8_t TEC1_EN;
		uint8_t TEC2_EN;
		uint8_t TS1_EN;
		uint8_t TS2_EN;
		uint8_t SD_EN;
		uint8_t PI1_RD;
		uint8_t PI2_RD;
		
		uint16_t AVERAGES;
		uint16_t MES_ID;
		
	}Work_SetupTypeDef;
	
	typedef struct{
		
		uint16_t LD_TEMP;
    float P_coef_temp;
    float I_coef_temp;
		uint16_t CURRENT;
		
	}LDx_SetupTypeDef;
	
	typedef struct{
		
		uint16_t LD_CURR_TEMP;
		float e_integral;
		uint16_t POWER;
		
	}LDx_ParamTypeDef;
	
	typedef enum _task_type_t
  {
    TT_CHANGE_CURR_1 = 0x1,
    TT_CHANGE_CURR_2 = 0x2,
    TT_CHANGE_TEMP_1 = 0x3,
    TT_CHANGE_TEMP_2 = 0x4,
  } task_type_t;
  
  typedef struct _task_t
  {
    task_type_t task_type;
    float    min_param;
    float    max_param;
    float    delta_param; // change step between min_param and max_param
    float    current_param; // current_param is iterating by summary with delta_param every interrupt
    uint8_t  dt;          // microseconds
    uint16_t tau;         // milliseconds or microseconds?
    float    sec_param;
    float    curr;
    float    temp;
    float    i_coef_1;
    float    p_coef_1;
    float    i_coef_2;
    float    p_coef_2;
  } task_t;


  typedef struct{
	  task_type_t task_type;
	  uint16_t signal_pin;
	  GPIO_TypeDef * signal_port;
	  uint16_t param;
	  uint8_t state;  //0 -- disabled (do nothing);
	  	  	  	  	  //1 -- update LD current;
					  //2 -- blinking, set LD ON now;
					  //3 -- blinking, set LD OFF now
  } LD_Blinker_StateTypeDef;
/* USER CODE END Private defines */

#ifdef __cplusplus
}
#endif

#endif /* __MAIN_H */
