/**
  ******************************************************************************
  * @file    stm32f3xx_it.c
  * @brief   Interrupt Service Routines.
  ******************************************************************************
  *
  * COPYRIGHT(c) 2018 STMicroelectronics
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */
/* Includes ------------------------------------------------------------------*/
#include "stm32f3xx_hal.h"
#include "stm32f3xx.h"
#include "stm32f3xx_it.h"

/* USER CODE BEGIN 0 */
#include "HAL_layer_io.h"
#include "HAL_layer_LED.h"
#include "HAL_adc.h"


extern uint16_t t1000ms_done;
extern uint16_t faults;
extern uint16_t faults_recorded;
extern uint16_t faults_free;
extern uint16_t Voltage_result;

uint16_t faults_80ms = 0;
uint8_t previous_state_MPPT = 0;


extern uint8_t MPPT_method;
extern float PI_I_current_act_error;
extern float PI_I_voltage_act_error;


/* USER CODE END 0 */

/* External variables --------------------------------------------------------*/
extern DMA_HandleTypeDef hdma_adc2;
extern ADC_HandleTypeDef hadc1;
extern ADC_HandleTypeDef hadc2;
extern CAN_HandleTypeDef hcan;
extern DAC_HandleTypeDef hdac1;
extern DMA_HandleTypeDef hdma_hrtim1_c;
extern DMA_HandleTypeDef hdma_hrtim1_a;
extern HRTIM_HandleTypeDef hhrtim1;
extern I2C_HandleTypeDef hi2c1;
extern RTC_HandleTypeDef hrtc;
extern TIM_HandleTypeDef htim2;
extern TIM_HandleTypeDef htim3;
extern TIM_HandleTypeDef htim6;
extern WWDG_HandleTypeDef hwwdg;

/******************************************************************************/
/*            Cortex-M4 Processor Interruption and Exception Handlers         */ 
/******************************************************************************/

/**
* @brief This function handles System tick timer.
*/
void SysTick_Handler(void)
{
  /* USER CODE BEGIN SysTick_IRQn 0 */

  /* USER CODE END SysTick_IRQn 0 */
  HAL_IncTick();
  HAL_SYSTICK_IRQHandler();
  /* USER CODE BEGIN SysTick_IRQn 1 */

  /* USER CODE END SysTick_IRQn 1 */
}

/******************************************************************************/
/* STM32F3xx Peripheral Interrupt Handlers                                    */
/* Add here the Interrupt Handlers for the used peripherals.                  */
/* For the available peripheral interrupt handler names,                      */
/* please refer to the startup file (startup_stm32f3xx.s).                    */
/******************************************************************************/

/**
* @brief This function handles Window watchdog interrupt.
*/
void WWDG_IRQHandler(void)
{
  /* USER CODE BEGIN WWDG_IRQn 0 */

  /* USER CODE END WWDG_IRQn 0 */
  HAL_WWDG_IRQHandler(&hwwdg);
  /* USER CODE BEGIN WWDG_IRQn 1 */

  /* USER CODE END WWDG_IRQn 1 */
}

/**
* @brief This function handles EXTI line 0 interrupt.
*/
void EXTI0_IRQHandler(void)
{
  /* USER CODE BEGIN EXTI0_IRQn 0 */
	// Fault_over_cur
	HAL_LED_fault_on();
	HAL_MCU_fault(1);
  /* USER CODE END EXTI0_IRQn 0 */
  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0);
  /* USER CODE BEGIN EXTI0_IRQn 1 */
  /* USER CODE END EXTI0_IRQn 1 */
}

/**
* @brief This function handles EXTI line 1 interrupt.
*/
void EXTI1_IRQHandler(void)
{
  /* USER CODE BEGIN EXTI1_IRQn 0 */
	// Fault_under_volt
	HAL_LED_fault_on();
	HAL_MCU_fault(1);
  /* USER CODE END EXTI1_IRQn 0 */
  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_1);
  /* USER CODE BEGIN EXTI1_IRQn 1 */

  /* USER CODE END EXTI1_IRQn 1 */
}

/**
* @brief This function handles EXTI line 2 and touch sense controller.
*/
void EXTI2_TSC_IRQHandler(void)
{
  /* USER CODE BEGIN EXTI2_TSC_IRQn 0 */
	// Fault_over_volt
	HAL_LED_fault_on();
	HAL_MCU_fault(1);
  /* USER CODE END EXTI2_TSC_IRQn 0 */
  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_2);
  /* USER CODE BEGIN EXTI2_TSC_IRQn 1 */

  /* USER CODE END EXTI2_TSC_IRQn 1 */
}

/**
* @brief This function handles EXTI line 3 interrupt.
*/
void EXTI3_IRQHandler(void)
{
  /* USER CODE BEGIN EXTI3_IRQn 0 */
	// Fault_over_temp
	HAL_LED_fault_on();
	HAL_MCU_fault(1);
  /* USER CODE END EXTI3_IRQn 0 */
  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_3);
  /* USER CODE BEGIN EXTI3_IRQn 1 */

  /* USER CODE END EXTI3_IRQn 1 */
}

/**
* @brief This function handles DMA1 channel3 global interrupt.
*/
void DMA1_Channel3_IRQHandler(void)
{
  /* USER CODE BEGIN DMA1_Channel3_IRQn 0 */

  /* USER CODE END DMA1_Channel3_IRQn 0 */
  HAL_DMA_IRQHandler(&hdma_hrtim1_a);
  /* USER CODE BEGIN DMA1_Channel3_IRQn 1 */

  /* USER CODE END DMA1_Channel3_IRQn 1 */
}

/**
* @brief This function handles DMA1 channel4 global interrupt.
*/
void DMA1_Channel4_IRQHandler(void)
{
  /* USER CODE BEGIN DMA1_Channel4_IRQn 0 */

  /* USER CODE END DMA1_Channel4_IRQn 0 */
  HAL_DMA_IRQHandler(&hdma_adc2);
  /* USER CODE BEGIN DMA1_Channel4_IRQn 1 */

  /* USER CODE END DMA1_Channel4_IRQn 1 */
}

/**
* @brief This function handles DMA1 channel5 global interrupt.
*/
void DMA1_Channel5_IRQHandler(void)
{
  /* USER CODE BEGIN DMA1_Channel5_IRQn 0 */

  /* USER CODE END DMA1_Channel5_IRQn 0 */
  HAL_DMA_IRQHandler(&hdma_hrtim1_c);
  /* USER CODE BEGIN DMA1_Channel5_IRQn 1 */

  /* USER CODE END DMA1_Channel5_IRQn 1 */
}

/**
* @brief This function handles ADC1 and ADC2 interrupts.
*/
void ADC1_2_IRQHandler(void)
{
  /* USER CODE BEGIN ADC1_2_IRQn 0 */
	if (__HAL_ADC_GET_FLAG(&hadc1, ADC_FLAG_AWD1) != RESET) // analog watchdog 12 V
	{
		HAL_LED_fault_on();
		HAL_MCU_fault(1);
	}
  /* USER CODE END ADC1_2_IRQn 0 */
  HAL_ADC_IRQHandler(&hadc1);
  HAL_ADC_IRQHandler(&hadc2);
  /* USER CODE BEGIN ADC1_2_IRQn 1 */

  /* USER CODE END ADC1_2_IRQn 1 */
}

/**
* @brief This function handles CAN TX interrupt.
*/
void CAN_TX_IRQHandler(void)
{
  /* USER CODE BEGIN CAN_TX_IRQn 0 */

  /* USER CODE END CAN_TX_IRQn 0 */
  HAL_CAN_IRQHandler(&hcan);
  /* USER CODE BEGIN CAN_TX_IRQn 1 */

  /* USER CODE END CAN_TX_IRQn 1 */
}

/**
* @brief This function handles CAN RX0 interrupt.
*/
void CAN_RX0_IRQHandler(void)
{
  /* USER CODE BEGIN CAN_RX0_IRQn 0 */

  /* USER CODE END CAN_RX0_IRQn 0 */
  HAL_CAN_IRQHandler(&hcan);
  /* USER CODE BEGIN CAN_RX0_IRQn 1 */

  /* USER CODE END CAN_RX0_IRQn 1 */
}

/**
* @brief This function handles CAN RX1 interrupt.
*/
void CAN_RX1_IRQHandler(void)
{
  /* USER CODE BEGIN CAN_RX1_IRQn 0 */

  /* USER CODE END CAN_RX1_IRQn 0 */
  HAL_CAN_IRQHandler(&hcan);
  /* USER CODE BEGIN CAN_RX1_IRQn 1 */

  /* USER CODE END CAN_RX1_IRQn 1 */
}

/**
* @brief This function handles CAN SCE interrupt.
*/
void CAN_SCE_IRQHandler(void)
{
  /* USER CODE BEGIN CAN_SCE_IRQn 0 */

  /* USER CODE END CAN_SCE_IRQn 0 */
  HAL_CAN_IRQHandler(&hcan);
  /* USER CODE BEGIN CAN_SCE_IRQn 1 */

  /* USER CODE END CAN_SCE_IRQn 1 */
}

/**
* @brief This function handles EXTI line[9:5] interrupts.
*/
void EXTI9_5_IRQHandler(void)
{
  /* USER CODE BEGIN EXTI9_5_IRQn 0 */

  if(__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_5) != RESET) // PB5 - button
  {
	  if (HAL_fault_io_check() == 0)
	  {
		  HAL_LED4(0);
	  }
  }
  if(__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_9) != RESET) // Temperature sensor TMP75C 1
  {
	  HAL_LED_fault_on();
	  HAL_MCU_fault(1);
  }

  /* USER CODE END EXTI9_5_IRQn 0 */
  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_5);
  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_9);
  /* USER CODE BEGIN EXTI9_5_IRQn 1 */

  /* USER CODE END EXTI9_5_IRQn 1 */
}

/**
* @brief This function handles TIM2 global interrupt.
*/
void TIM2_IRQHandler(void)
{
  /* USER CODE BEGIN TIM2_IRQn 0 */
  if (__HAL_TIM_GET_FLAG(&htim2,TIM_FLAG_UPDATE) != RESET) // Timer2 overflow flag - period 100 us, used for start new ADC samples
  {
		//HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_2);

  }
  /* USER CODE END TIM2_IRQn 0 */
  HAL_TIM_IRQHandler(&htim2);
  /* USER CODE BEGIN TIM2_IRQn 1 */

  /* USER CODE END TIM2_IRQn 1 */
}

/**
* @brief This function handles TIM3 global interrupt.
*/
void TIM3_IRQHandler(void)
{
  /* USER CODE BEGIN TIM3_IRQn 0 */
  if (__HAL_TIM_GET_FLAG(&htim3,TIM_FLAG_UPDATE) != RESET) // Timer3 overflow flag - period 1 ms, used for faults control, switches check and feed watch dog
  {
	  HAL_WWDG_Refresh(&hwwdg);
	  faults = HAL_fault_io_check();
	  if (faults != 0)
	  {
		  faults_recorded = 1;
		  faults_free = 0;
		  HAL_LED_fault(1);
		  HAL_LED4(1);
	  }
	  if ((faults_recorded == 1) && (faults == 0))
	  {
		  ++faults_80ms;
		  if (faults_80ms > 80)
		  {
			  faults_80ms = 0;
			  faults_free = 1;
		  }
	  }
	  // select MPPT method
	  // 0 - without MPPT method
	  // 1 - Perturb and Observe
	  // 2 - Incremental Conductance
	  // 3 - reserved
	  if ((HAL_Switch_1() == 0) && (HAL_Switch_0() == 0)) // without MPPT
		  MPPT_method = 0;
	  else if ((HAL_Switch_1() == 0) && (HAL_Switch_0() == 1)) // MPPT method Perturb and Observe
		  MPPT_method = 1;
	  else if ((HAL_Switch_1() == 1) && (HAL_Switch_0() == 0)) // MPPT method Incremental Conductance
	 	  MPPT_method = 2;
	  else if ((HAL_Switch_1() == 1) && (HAL_Switch_0() == 1)) // reserved
	 	  MPPT_method = 3;

	  if (previous_state_MPPT != MPPT_method)
	  {
		  Voltage_result = 0;
		  PI_I_current_act_error = 0;
		  PI_I_voltage_act_error = 0;
	  }

	  previous_state_MPPT = MPPT_method;
  }

  /* USER CODE END TIM3_IRQn 0 */
  HAL_TIM_IRQHandler(&htim3);
  /* USER CODE BEGIN TIM3_IRQn 1 */

  /* USER CODE END TIM3_IRQn 1 */
}

/**
* @brief This function handles I2C1 error interrupt.
*/
void I2C1_ER_IRQHandler(void)
{
  /* USER CODE BEGIN I2C1_ER_IRQn 0 */

  /* USER CODE END I2C1_ER_IRQn 0 */
  HAL_I2C_ER_IRQHandler(&hi2c1);
  /* USER CODE BEGIN I2C1_ER_IRQn 1 */

  /* USER CODE END I2C1_ER_IRQn 1 */
}

/**
* @brief This function handles EXTI line[15:10] interrupts.
*/
void EXTI15_10_IRQHandler(void)
{
  /* USER CODE BEGIN EXTI15_10_IRQn 0 */
  if(__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_10) != RESET) // Temperature sensor TMP75C 2
  {
	  HAL_LED_fault_on();
	  HAL_MCU_fault(1);
  }
  if(__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_11) != RESET) // Temperature sensor TMP75C 3
  {
	  HAL_LED_fault_on();
	  HAL_MCU_fault(1);
  }
  if(__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_12) != RESET) // LT3992_CMPO1
  {
  	  HAL_LED_fault_on();
  	  HAL_MCU_fault(1);
  }
  if(__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_13) != RESET) // LT3992_CMPO2 - set up when the input voltage decrease 12 V
  {
  	  HAL_LED_fault_on();
  	  HAL_MCU_fault(1);
  }
  /* USER CODE END EXTI15_10_IRQn 0 */
  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_10);
  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_11);
  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_12);
  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_13);
  /* USER CODE BEGIN EXTI15_10_IRQn 1 */

  /* USER CODE END EXTI15_10_IRQn 1 */
}

/**
* @brief This function handles RTC alarms A and B interrupt through EXTI line 17.
*/
void RTC_Alarm_IRQHandler(void)
{
  /* USER CODE BEGIN RTC_Alarm_IRQn 0 */

  /* USER CODE END RTC_Alarm_IRQn 0 */
  HAL_RTC_AlarmIRQHandler(&hrtc);
  /* USER CODE BEGIN RTC_Alarm_IRQn 1 */

  /* USER CODE END RTC_Alarm_IRQn 1 */
}

/**
* @brief This function handles TIM6 global and DAC1 underrun error interrupts.
*/
void TIM6_DAC1_IRQHandler(void)
{
  /* USER CODE BEGIN TIM6_DAC1_IRQn 0 */
  if (__HAL_TIM_GET_FLAG(&htim6,TIM_FLAG_UPDATE) != RESET) // Timer6 overflow flag - period 250 ms, blinking of LED
  {
	 //HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_1);
  }
  /* USER CODE END TIM6_DAC1_IRQn 0 */
  HAL_TIM_IRQHandler(&htim6);
  HAL_DAC_IRQHandler(&hdac1);
  /* USER CODE BEGIN TIM6_DAC1_IRQn 1 */

  /* USER CODE END TIM6_DAC1_IRQn 1 */
}

/**
* @brief This function handles HRTIM timer C global interrupt.
*/
void HRTIM1_TIMC_IRQHandler(void)
{
  /* USER CODE BEGIN HRTIM1_TIMC_IRQn 0 */

  /* USER CODE END HRTIM1_TIMC_IRQn 0 */
  HAL_HRTIM_IRQHandler(&hhrtim1,HRTIM_TIMERINDEX_TIMER_C);
  /* USER CODE BEGIN HRTIM1_TIMC_IRQn 1 */

  /* USER CODE END HRTIM1_TIMC_IRQn 1 */
}

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
