
/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx_hal.h"
#include "main.h"
#include "stm32f4xx_it.h"
#include "ethernetif.h"
#include "process2.h"
#include "lab.h"
#include "math.h"
 
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/******************************************************************************/
/*            Cortex-M4 Processor Exceptions Handlers                         */
/******************************************************************************/

void TIM1_BRK_TIM9_IRQHandler(void)
{
  Timer_flag = 1;
  static uint32_t ADC_5_acc 					= 0;
  static uint32_t ADC_Press_acc 			= 0;
	static uint32_t Press_Atm 					= 0;
	static uint32_t ADC_Conc_spirt_acc 	= 0;
  
  
  HAL_TIM_IRQHandler(&TimHandle50ms); 
  
  ADC_5_acc = ADC_5_acc + HAL_ADCEx_InjectedGetValue(&AdcHandle_ADC, 1) - ADC_5;
  ADC_5 		= ADC_5_acc/32;
  
  ADC_Press_acc = ADC_Press_acc + (HAL_ADCEx_InjectedGetValue(&AdcHandle_ADC, 2)) - Press_Atm;
  Press_Atm 		= ADC_Press_acc/500;
  
  ADC_Press = (Press_Atm*10)/74;

  ADC_Conc_spirt_acc 	= ADC_Conc_spirt_acc + HAL_ADCEx_InjectedGetValue(&AdcHandle_ADC, 3) - ADC_Conc_spirt;
  ADC_Conc_spirt 			= ADC_Conc_spirt_acc/1000;
	
  AdcHandle_ADC.Instance->CR2 |= ADC_CR2_JSWSTART;
	
}


void TIM7_IRQHandler(void)
{
  RC5_Sample_Data();
}

void EXTI0_IRQHandler(void)
{		
  RC5_MeasureFirstLowDuration();	
  HAL_GPIO_EXTI_IRQHandler(IR);	
}

void EXTI3_IRQHandler(void)
{	
  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_3);	
  static int32_t integ;
  
  //-------------------------------------
  volatile uint32_t tmp_loc;
  volatile uint32_t tmp_loc2;
  
  volatile uint64_t ADC_t;
  uint32_t ADC_5_loc;
  
  static uint8_t sec_tmp = 0;
  static uint32_t period_tmp = 0;
  
  tmp_loc = 0;
  ADC_t 	 = 0;
  uint16_t i = 0;
  static uint64_t tmp_loc3;
  static int16_t i_sum;
  
  if(n_RC5_TRIAC > 40){
    HAL_GPIO_WritePin(TRIAC_PORT, TRIAC, GPIO_PIN_RESET);
  }
  
  uint32_t ADC_0 =  ADC_5/2;
  
  HAL_ADC_Stop_DMA(&AdcHandle);
  
  for (i=0; i<1024; i++){	
    
    tmp_loc2 = 0;
    tmp_loc = ADCxConvertedValue[i];
		//ADCxConvertedValue[i] = 0xFFFF;	!!!!!
    
    if(tmp_loc > 5000){
      break;
    }
    i_sum += 1;
    
    if (tmp_loc < ADC_0){
      tmp_loc2 = ADC_0 - tmp_loc;
    }
    else if(tmp_loc > ADC_0){
      tmp_loc2 = tmp_loc - ADC_0;
    }		
    tmp_loc3 += tmp_loc2*tmp_loc2;
  }		
 
  
  for (i=0; i<1024; i++){	
    ADCxConvertedValue[i] = 0xFFFF;	
  }	
  
  if (period_tmp < 1){
    period_tmp += 1;
  }
  else{
    ADC_t = tmp_loc3/i_sum;
    i_sum = 1;
    tmp_loc2 = sqrt(ADC_t); 
    Triac_Power_Real = tmp_loc2;
    sum_ADC = sum_ADC + Triac_Power_Real;
		
		if (Triac_Power_Real < 40) {
			Err_Power += 1;
		}
    
    number_ADC +=1;
    
    tmp_loc3 = 0;
    i_sum = 1;	
    period_tmp  = 0;
    
    //-------PI------
    int32_t error = Triac_Power_Real - Triac_Power_Val;
    if(error < -50){
      integ = 1100 - Triac_Power_Val;
      n_RC5_TRIAC -=1;
    }
    else{
      integ = integ+error;
      if(integ > 16384){
				integ = 16384;
			}
      else if(integ < -16384){
				integ = -16384;
      }
			
      n_RC5_TRIAC=((error*3)>>7) + ((integ*5)>>3);	
      
      if(n_RC5_TRIAC < 0){
				n_RC5_TRIAC=0;
			}
      else if(n_RC5_TRIAC > 1100){
				n_RC5_TRIAC = 1100;
			}
    }
    
  }
	
  if(!stop_start){
    HAL_GPIO_WritePin(TRIAC_PORT, TRIAC, GPIO_PIN_RESET);
		HAL_TIM_Base_Stop_IT(&TimHandleTRIAC) ;	
  }
  else{
    if(n_RC5_TRIAC < 50){
      HAL_GPIO_WritePin(TRIAC_PORT, TRIAC, GPIO_PIN_SET);
    }
    else{
      HAL_TIM_Base_Start_IT(&TimHandleTRIAC) ;	
    }
  }
  
  
  HAL_ADC_Start_DMA(&AdcHandle, (uint32_t*)&ADCxConvertedValue, 1024);	
  //-------------------------------------
  

  
}

void TIM8_BRK_TIM12_IRQHandler(void)
{ 
  static uint16_t Counter_Triac2;
  
  HAL_TIM_IRQHandler(&TimHandleTRIAC);
  
  Counter_Triac2 += 1;
	
	//n_RC5_TRIAC = 1060;  MIN
  
  if((Counter_Triac2 < n_RC5_TRIAC)|(Counter_Triac2 >(n_RC5_TRIAC+40))){
    HAL_GPIO_WritePin(TRIAC_PORT, TRIAC, GPIO_PIN_RESET);
  }
  else{
    HAL_GPIO_WritePin(TRIAC_PORT, TRIAC, GPIO_PIN_SET);
    HAL_TIM_Base_Stop_IT(&TimHandleTRIAC) ;	
    Counter_Triac2 = 0;
  }
  
}



void EXTI9_5_IRQHandler(void)
{	
  
  if(__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_6) != RESET){		
    ethernetif_set_link(&gnetif);
    HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_6);		
  }	
	 else if(__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_7) != RESET){	
		 HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_7);		
	 }
}


/**
* @brief   This function handles NMI exception.
* @param  None
* @retval None
*/
void NMI_Handler(void)
{
}

/**
* @brief  This function handles Hard Fault exception.
* @param  None
* @retval None
*/
void HardFault_Handler(void)
{
  /* Go to infinite loop when Hard Fault exception occurs */
while (1)
  {
  }
}

/**
* @brief  This function handles Memory Manage exception.
* @param  None
* @retval None
*/
void MemManage_Handler(void)
{
  /* Go to infinite loop when Memory Manage exception occurs */
  while (1)
  {
  }
}

/**
* @brief  This function handles Bus Fault exception.
* @param  None
* @retval None
*/
void BusFault_Handler(void)
{
  /* Go to infinite loop when Bus Fault exception occurs */
  while (1)
  {
  }
}

/**
* @brief  This function handles Usage Fault exception.
* @param  None
* @retval None
*/
void UsageFault_Handler(void)
{
  /* Go to infinite loop when Usage Fault exception occurs */
  while (1)
  {
  }
}

/**
* @brief  This function handles Debug Monitor exception.
* @param  None
* @retval None
*/
void DebugMon_Handler(void)
{
}

/**
* @brief  This function handles SysTick Handler.
* @param  None
* @retval None
*/
void SysTick_Handler(void)
{
  /* Update the LocalTime by adding 1 ms each SysTick interrupt */
  HAL_IncTick();
	
}

/**
* @brief  This function handles External line 10 interrupt request.
* @param  None
* @retval None
*/

/******************************************************************************/
/*                 STM32F4xx Peripherals Interrupt Handlers                   */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file (startup_stm32f4xx.s).                                               */
/******************************************************************************/

/**
* @brief  This function handles PPP interrupt request.
* @param  None
* @retval None
*/
/*void PPP_IRQHandler(void)
{
}*/

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