/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    stm32f4xx_it.c
  * @brief   Interrupt Service Routines.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 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 "main.h"
#include "stm32f4xx_it.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN TD */

/* USER CODE END TD */

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

/* USER CODE END PV */

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

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/* External variables --------------------------------------------------------*/
extern PCD_HandleTypeDef hpcd_USB_OTG_FS;
extern DMA_HandleTypeDef hdma_adc1;
/* USER CODE BEGIN EV */
/* Externs are provided via main.h; no extra declarations needed here */
/* USER CODE END EV */

/******************************************************************************/
/*           Cortex-M4 Processor Interruption and Exception Handlers          */
/******************************************************************************/
/**
  * @brief This function handles Non maskable interrupt.
  */
void NMI_Handler(void)
{
  /* USER CODE BEGIN NonMaskableInt_IRQn 0 */

  /* USER CODE END NonMaskableInt_IRQn 0 */
  /* USER CODE BEGIN NonMaskableInt_IRQn 1 */
   while (1)
  {
  }
  /* USER CODE END NonMaskableInt_IRQn 1 */
}

/**
  * @brief This function handles Hard fault interrupt.
  */
void HardFault_Handler(void)
{
  /* USER CODE BEGIN HardFault_IRQn 0 */

  /* USER CODE END HardFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_HardFault_IRQn 0 */
    /* USER CODE END W1_HardFault_IRQn 0 */
  }
}

/**
  * @brief This function handles Memory management fault.
  */
void MemManage_Handler(void)
{
  /* USER CODE BEGIN MemoryManagement_IRQn 0 */

  /* USER CODE END MemoryManagement_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */
    /* USER CODE END W1_MemoryManagement_IRQn 0 */
  }
}

/**
  * @brief This function handles Pre-fetch fault, memory access fault.
  */
void BusFault_Handler(void)
{
  /* USER CODE BEGIN BusFault_IRQn 0 */

  /* USER CODE END BusFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_BusFault_IRQn 0 */
    /* USER CODE END W1_BusFault_IRQn 0 */
  }
}

/**
  * @brief This function handles Undefined instruction or illegal state.
  */
void UsageFault_Handler(void)
{
  /* USER CODE BEGIN UsageFault_IRQn 0 */

  /* USER CODE END UsageFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_UsageFault_IRQn 0 */
    /* USER CODE END W1_UsageFault_IRQn 0 */
  }
}

/**
  * @brief This function handles System service call via SWI instruction.
  */
void SVC_Handler(void)
{
  /* USER CODE BEGIN SVCall_IRQn 0 */

  /* USER CODE END SVCall_IRQn 0 */
  /* USER CODE BEGIN SVCall_IRQn 1 */

  /* USER CODE END SVCall_IRQn 1 */
}

/**
  * @brief This function handles Debug monitor.
  */
void DebugMon_Handler(void)
{
  /* USER CODE BEGIN DebugMonitor_IRQn 0 */

  /* USER CODE END DebugMonitor_IRQn 0 */
  /* USER CODE BEGIN DebugMonitor_IRQn 1 */

  /* USER CODE END DebugMonitor_IRQn 1 */
}

/**
  * @brief This function handles Pendable request for system service.
  */
void PendSV_Handler(void)
{
  /* USER CODE BEGIN PendSV_IRQn 0 */

  /* USER CODE END PendSV_IRQn 0 */
  /* USER CODE BEGIN PendSV_IRQn 1 */

  /* USER CODE END PendSV_IRQn 1 */
}

/**
  * @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();
  /* USER CODE BEGIN SysTick_IRQn 1 */

  /* USER CODE END SysTick_IRQn 1 */
}

/******************************************************************************/
/* STM32F4xx 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_stm32f4xx.s).                    */
/******************************************************************************/

/**
  * @brief This function handles EXTI line0 interrupt.
  */
void EXTI0_IRQHandler(void)
{
  /* USER CODE BEGIN EXTI0_IRQn 0 */
  Sweep_state.curr_step_start_DMA_N = ADC_BUFF_SIZE - hdma_adc1.Instance->NDTR;
  if (Sweep_state.curr_step_start_DMA_N < ADC_BUFF_SIZE/2) {
    Sweep_state.curr_step_started_flag =1; // first half DMA buffer
  } else{
    Sweep_state.curr_step_started_flag =2; // second half DMA buffer
  }
  Sweep_state.curr_step_N++;
  /* USER CODE END EXTI0_IRQn 0 */
  HAL_GPIO_EXTI_IRQHandler(CURR_STEP_START_TRG_Pin);
  /* USER CODE BEGIN EXTI0_IRQn 1 */

  /* USER CODE END EXTI0_IRQn 1 */
}

/**
  * @brief This function handles EXTI line3 interrupt.
  */
void EXTI3_IRQHandler(void)
{
  /* USER CODE BEGIN EXTI3_IRQn 0 */
  Sweep_state.sweep_cycle_started_flag = 1; //sweep cycle started
  Sweep_state.curr_step_N = 0;
  /* USER CODE END EXTI3_IRQn 0 */
  HAL_GPIO_EXTI_IRQHandler(SWEEP_CYCLE_START_TRG_Pin);
  /* USER CODE BEGIN EXTI3_IRQn 1 */

  /* USER CODE END EXTI3_IRQn 1 */
}

/**
  * @brief This function handles DMA2 stream0 global interrupt.
  */
void DMA2_Stream0_IRQHandler(void)
{
  /* USER CODE BEGIN DMA2_Stream0_IRQn 0 */

  /* USER CODE END DMA2_Stream0_IRQn 0 */
  HAL_DMA_IRQHandler(&hdma_adc1);
  /* USER CODE BEGIN DMA2_Stream0_IRQn 1 */

  /* USER CODE END DMA2_Stream0_IRQn 1 */
}

/**
  * @brief This function handles USB On The Go FS global interrupt.
  */
void OTG_FS_IRQHandler(void)
{
  /* USER CODE BEGIN OTG_FS_IRQn 0 */

  /* USER CODE END OTG_FS_IRQn 0 */
  HAL_PCD_IRQHandler(&hpcd_USB_OTG_FS);
  /* USER CODE BEGIN OTG_FS_IRQn 1 */

  /* USER CODE END OTG_FS_IRQn 1 */
}

/* USER CODE BEGIN 1 */

#ifdef SYNC_DET_ON




void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
  HAL_GPIO_WritePin(LED_BLUE_GPIO_Port, LED_BLUE_Pin, GPIO_PIN_SET);

  if (Sweep_state.curr_step_started_flag == 2) {
    Sweep_state.curr_step_started_flag = 0; // reset flag after processing second half
    for (uint32_t i = ADC_BUFF_SIZE/2; i < Sweep_state.curr_step_start_DMA_N; i++) {
      if ((i & 1) != 0){
        ADC_proc.sum_ON += ADC1_buff_circular[i];
      }else{
        ADC_proc.sum_OFF += ADC1_buff_circular[i];
      }
    }

    ADC_proc.N += (Sweep_state.curr_step_start_DMA_N - ADC_BUFF_SIZE/2)/2;

    ADC_proc_shadow.sum_ON = ADC_proc.sum_ON;
    ADC_proc_shadow.sum_OFF = ADC_proc.sum_OFF;
    ADC_proc_shadow.avg_ON = ADC_proc.avg_ON;
    ADC_proc_shadow.avg_OFF = ADC_proc.avg_OFF;
    ADC_proc_shadow.N = ADC_proc.N;
    ADC_proc_shadow.status = 2; // buffer filled


    ADC_proc.sum = 0;
    ADC_proc.sum_ON = 0;
    ADC_proc.sum_OFF = 0;
    ADC_proc.N = 0;
    ADC_proc.avg_ON = 0;
    ADC_proc.avg_OFF = 0;
    ADC_proc.status = 1; // collecting data

    for (uint32_t i = Sweep_state.curr_step_start_DMA_N; i < ADC_BUFF_SIZE; i++) {
      if ((i & 1) != 0){
        ADC_proc.sum_ON += ADC1_buff_circular[i];
      }else{
        ADC_proc.sum_OFF += ADC1_buff_circular[i];
      }
    }
    ADC_proc.N = (ADC_BUFF_SIZE - Sweep_state.curr_step_start_DMA_N)/2;


  }else{
    for (uint32_t i = ADC_BUFF_SIZE/2; i < ADC_BUFF_SIZE; i++) {
      if ((i & 1) != 0){
        ADC_proc.sum_ON += ADC1_buff_circular[i];
      }else{
        ADC_proc.sum_OFF += ADC1_buff_circular[i];
      }
    }
    ADC_proc.N += (ADC_BUFF_SIZE - ADC_BUFF_SIZE/2)/2;
  }

  //if (0){
  if (ADC_proc.N >= ADC_BUFF_SIZE*100){
    ADC_proc_shadow.sum_OFF = ADC_proc.sum_OFF;
    ADC_proc_shadow.sum_ON = ADC_proc.sum_ON;
    ADC_proc_shadow.avg_ON = ADC_proc.avg_ON;
    ADC_proc_shadow.avg_OFF = ADC_proc.avg_OFF;
    ADC_proc_shadow.N = ADC_proc.N;
    ADC_proc_shadow.status = 2; // buffer filled


    ADC_proc.sum_OFF = 0;
    ADC_proc.sum_ON = 0;
    
    ADC_proc.N = 0;
    ADC_proc.avg_ON = 0;
    ADC_proc.avg_OFF = 0;
    ADC_proc.status = 1; // collecting data
  }
    // This function is called when the first half of the ADC buffer is filled
    // You can process the first half of ADC1_buff_circular here
}

void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc)
{
  //HAL_GPIO_WritePin(LED_BLUE_GPIO_Port, LED_BLUE_Pin, GPIO_PIN_RESET);

  HAL_GPIO_TogglePin(LED_BLUE_GPIO_Port, LED_BLUE_Pin);
  if (Sweep_state.curr_step_started_flag == 1) {
    Sweep_state.curr_step_started_flag = 0;
    for (uint32_t i = 0; i < Sweep_state.curr_step_start_DMA_N; i++) {
      if ((i & 1) != 0){
        ADC_proc.sum_ON += ADC1_buff_circular[i];
      }else{
        ADC_proc.sum_OFF += ADC1_buff_circular[i];
      }
    }

    ADC_proc.N += (Sweep_state.curr_step_start_DMA_N)/2;

    ADC_proc_shadow.sum_ON = ADC_proc.sum_ON;
    ADC_proc_shadow.avg_ON = ADC_proc.avg_ON;
    ADC_proc_shadow.sum_OFF = ADC_proc.sum_OFF;
    ADC_proc_shadow.avg_OFF = ADC_proc.avg_OFF;
    ADC_proc_shadow.N = ADC_proc.N;
    ADC_proc_shadow.status = 2; // buffer filled


    ADC_proc.sum_ON = 0;
    ADC_proc.sum_OFF = 0;
    ADC_proc.sum = 0;
    ADC_proc.N = 0;
    ADC_proc.avg_OFF = 0;
    ADC_proc.avg_ON = 0;
    ADC_proc.status = 1; // collecting data

    for (uint32_t i = Sweep_state.curr_step_start_DMA_N; i < ADC_BUFF_SIZE/2; i++) {
      if ((i & 1) != 0){

        ADC_proc.sum_ON += ADC1_buff_circular[i];
      }else{
        ADC_proc.sum_OFF += ADC1_buff_circular[i];
      }
    }
    ADC_proc.N = (Sweep_state.curr_step_start_DMA_N)/2;

  }else{
    for (uint32_t i = 0; i < ADC_BUFF_SIZE/2; i++) {
      if ((i & 1) != 0){
        ADC_proc.sum_ON += ADC1_buff_circular[i];
      }else{
        ADC_proc.sum_OFF += ADC1_buff_circular[i];
      }
    }
    ADC_proc.N += (ADC_BUFF_SIZE/2)/2;
  }
    // This function is called when the first half of the ADC buffer is filled
    // You can process the first half of ADC1_buff_circular here
}


#else



void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
  HAL_GPIO_WritePin(LED_BLUE_GPIO_Port, LED_BLUE_Pin, GPIO_PIN_SET);

  if (Sweep_state.curr_step_started_flag == 2) {
    Sweep_state.curr_step_started_flag = 0; // reset flag after processing second half
    for (uint32_t i = ADC_BUFF_SIZE/2; i < Sweep_state.curr_step_start_DMA_N; i++) {
      ADC_proc.sum += ADC1_buff_circular[i];
    }

    ADC_proc.N += Sweep_state.curr_step_start_DMA_N - ADC_BUFF_SIZE/2;


    ADC_proc_shadow.sum = ADC_proc.sum;
    ADC_proc_shadow.avg = ADC_proc.avg;
    ADC_proc_shadow.N = ADC_proc.N;
    ADC_proc_shadow.status = 2; // buffer filled


    ADC_proc.sum = 0;
    ADC_proc.N = 0;
    ADC_proc.avg = 0;
    ADC_proc.status = 1; // collecting data

    for (uint32_t i = Sweep_state.curr_step_start_DMA_N; i < ADC_BUFF_SIZE; i++) {
      ADC_proc.sum += ADC1_buff_circular[i];
    }
    ADC_proc.N = ADC_BUFF_SIZE - Sweep_state.curr_step_start_DMA_N;


  }else{
    for (uint32_t i = ADC_BUFF_SIZE/2; i < ADC_BUFF_SIZE; i++) {
      ADC_proc.sum += ADC1_buff_circular[i];
    }
    ADC_proc.N += ADC_BUFF_SIZE - ADC_BUFF_SIZE/2;
  }

  //if (0){
  if (ADC_proc.N >= ADC_BUFF_SIZE*100){
    ADC_proc_shadow.sum = ADC_proc.sum;
    ADC_proc_shadow.avg = ADC_proc.avg;
    ADC_proc_shadow.N = ADC_proc.N;
    ADC_proc_shadow.status = 2; // buffer filled


    ADC_proc.sum = 0;
    ADC_proc.N = 0;
    ADC_proc.avg = 0;
    ADC_proc.status = 1; // collecting data
  }
    // This function is called when the first half of the ADC buffer is filled
    // You can process the first half of ADC1_buff_circular here
}

void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc)
{
  //HAL_GPIO_WritePin(LED_BLUE_GPIO_Port, LED_BLUE_Pin, GPIO_PIN_RESET);

  HAL_GPIO_TogglePin(LED_BLUE_GPIO_Port, LED_BLUE_Pin);
  if (Sweep_state.curr_step_started_flag == 1) {
    Sweep_state.curr_step_started_flag = 0;
    for (uint32_t i = 0; i < Sweep_state.curr_step_start_DMA_N; i++) {
      ADC_proc.sum += ADC1_buff_circular[i];
    }

    ADC_proc.N += Sweep_state.curr_step_start_DMA_N;


    ADC_proc_shadow.sum = ADC_proc.sum;
    ADC_proc_shadow.avg = ADC_proc.avg;
    ADC_proc_shadow.N = ADC_proc.N;
    ADC_proc_shadow.status = 2; // buffer filled


    ADC_proc.sum = 0;
    ADC_proc.N = 0;
    ADC_proc.avg = 0;
    ADC_proc.status = 1; // collecting data

    for (uint32_t i = Sweep_state.curr_step_start_DMA_N; i < ADC_BUFF_SIZE/2; i++) {
      ADC_proc.sum += ADC1_buff_circular[i];
    }
    ADC_proc.N = Sweep_state.curr_step_start_DMA_N;

  }else{
    for (uint32_t i = 0; i < ADC_BUFF_SIZE/2; i++) {
      ADC_proc.sum += ADC1_buff_circular[i];
    }
    ADC_proc.N += ADC_BUFF_SIZE/2;
  }
    // This function is called when the first half of the ADC buffer is filled
    // You can process the first half of ADC1_buff_circular here
}
#endif

/*
void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc)
{
  //HAL_GPIO_WritePin(LED_BLUE_GPIO_Port, LED_BLUE_Pin, GPIO_PIN_RESET);

  HAL_GPIO_TogglePin(LED_BLUE_GPIO_Port, LED_BLUE_Pin);
  for (uint32_t i = 0; i < ADC_BUFF_SIZE/2; i++) {
    adc_process.sum += ADC1_buff_circular[i]; 
  }
  adc_process.N += ADC_BUFF_SIZE/2;
  
    // This function is called when the first half of the ADC buffer is filled
    // You can process the first half of ADC1_buff_circular here
}

*/
/* USER CODE END 1 */
