/**
  ******************************************************************************
  * @file    stm32l1xx_it.c
  * @brief   Interrupt Service Routines.
  ******************************************************************************
  *
  * COPYRIGHT(c) 2019 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 ------------------------------------------------------------------*/
#define STM32L1XX_IT_C
#include "core/config.h"
#include "core/main.h"
#include "stm32l1xx_it.h"
#include "drivers/i2c/i2c.h"
#include "drivers/adc/adc.h"
#if CONFIG_AUTOMAT_MODE
#include "app/automat/automat.h"
#if CONFIG_AUTOMAT_MODE_USB_POWERBANK
#include "app/usbapp/usb_application_enumspy.h"
#endif
#endif

#include "app/led/led.h"

#ifdef STM32L1XX_IT_C
extern PCD_HandleTypeDef hpcd_USB_FS;
#endif


/******************************************************************************/
/*           Cortex-M3 Processor Interruption and Exception Handlers          */ 
/******************************************************************************/
/**
  * @brief This function handles Non maskable interrupt.
  */
void NMI_Handler(void)
{
  
}

/**
  * @brief This function handles Hard fault interrupt.
  */
void HardFault_Handler(void)
{   
  while (1)
  { 
     HAL_NVIC_SystemReset();
  }
}

/**
  * @brief This function handles Memory management fault.
  */
void MemManage_Handler(void)
{   
  while (1)
  {  
     HAL_NVIC_SystemReset();
  }
}

/**
  * @brief This function handles Pre-fetch fault, memory access fault.
  */
void BusFault_Handler(void)
{   
  while (1)
  {   
     HAL_NVIC_SystemReset();
  }
}

/**
  * @brief This function handles Undefined instruction or illegal state.
  */
void UsageFault_Handler(void)
{   
  while (1)
  {   
     HAL_NVIC_SystemReset();
  }
}

/**
  * @brief This function handles System service call via SWI instruction.
  */
void SVC_Handler(void)
{  
}

/**
  * @brief This function handles Debug monitor.
  */
void DebugMon_Handler(void)
{  
}

/**
  * @brief This function handles Pendable request for system service.
  */
void PendSV_Handler(void)
{  
}

/**
  * @brief This function handles System tick timer.
  */
void SysTick_Handler(void)
{ 
  #if CONFIG_SYSTICK_DEBUG  
  CONFIG_SYSTICK_DEBUG_SETPIN
  #endif

  #if CONFIG_AUTOMAT_MODE
  ACMAutomatHandle.Tick();  // moved from the bottom
  #endif 

  #if CONFIG_SYSTICK_BOOST_MULTIPLIER && (CONFIG_SYSTICK == CONFIG_SYSTICK_NORMAL)
  static int prescaller = 0;
  if( ++prescaller >= CONFIG_SYSTICK_BOOST_MULTIPLIER )
  {
      #if CONFIG_HALTICK_DEBUG  
      CONFIG_HALTICK_DEBUG_SETPIN
      #endif

      HAL_IncTick();

      #if CONFIG_AUTOMAT_MODE && CONFIG_AUTOMAT_MODE_USB_POWERBANK
      UsbApplicationEnumSpyHandle.timerTickEventIrq1ms();
      #endif

      #if CONFIG_LEDS
      LEDHandle.Tick();
      #endif

      prescaller = 0;

      #if CONFIG_HALTICK_DEBUG  
      CONFIG_HALTICK_DEBUG_CLRPIN
      #endif
  }
  #else
  HAL_IncTick();

  #if CONFIG_AUTOMAT_MODE && CONFIG_AUTOMAT_MODE_USB_POWERBANK
  UsbApplicationEnumSpyHandle.timerTickEventIrq1ms();
  #endif

  #if CONFIG_LEDS
  LEDHandle.Tick();
  #endif

  #endif
       
  #if CONFIG_SYSTICK_DEBUG  
  CONFIG_SYSTICK_DEBUG_CLRPIN
  #endif
}

/******************************************************************************/
/* STM32L1xx 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_stm32l1xx.s).                    */
/******************************************************************************/


// USB Interrupt Event Routing
void USB_LP_IRQHandler(void)
{ 
  #if CONFIG_USB_DEBUGPIN
  CONFIG_USB_DEBUGPIN_SETPIN
  #endif
  HAL_PCD_IRQHandler(&hpcd_USB_FS);
  #if CONFIG_USB_DEBUGPIN
  CONFIG_USB_DEBUGPIN_CLRPIN
  #endif
} 

#if CONFIG_PERIF_TSENSI2C == CONFIG_PERIF_I2C1
// I2C Interrupt Event Routing
void I2C1_EV_IRQHandler(void)
{
   HAL_I2C_EV_IRQHandler( &i2c_handle );
}   
#elif CONFIG_PERIF_TSENSI2C == CONFIG_PERIF_I2C2
// I2C Interrupt Event Routing
void I2C2_EV_IRQHandler(void)
{
   HAL_I2C_EV_IRQHandler( &i2c_handle );
}
#else
#error Invalid I2C specified in 'config_pins.h': see 'CONFIG_PERIF_TSENSI2C' 
#endif

// ADC Interrupt Event Routing
#if CONFIG_VBATMON
void ADC1_IRQHandler()
{
   HAL_ADC_IRQHandler( &Adc1Handle );
}
#endif

// EXTI 10..15 Interrupt Event Routing
void EXTI15_10_IRQHandler()
{
    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);
    HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_14);
    HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_15);
}

// EXTI 5..9 Interrupt Event Routing
void EXTI9_5_IRQHandler()
{
    HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_5);
    HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_6);
    HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_7);
    HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_8);
    HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_9);
    HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_10);
}

// EXTI 4 Interrupt Event Routing
void EXTI4_IRQHandler()
{
   HAL_GPIO_EXTI_IRQHandler( GPIO_PIN_4 );
}

// EXTI 3 Interrupt Event Routing
void EXTI3_IRQHandler()
{
   HAL_GPIO_EXTI_IRQHandler( GPIO_PIN_3 );
}

// EXTI 2 Interrupt Event Routing
void EXTI2_IRQHandler()
{
   HAL_GPIO_EXTI_IRQHandler( GPIO_PIN_2 );
}

// EXTI 1 Interrupt Event Routing
void EXTI1_IRQHandler()
{
   HAL_GPIO_EXTI_IRQHandler( GPIO_PIN_1 );
}

// EXTI 0 Interrupt Event Routing
void EXTI0_IRQHandler()
{
   HAL_GPIO_EXTI_IRQHandler( GPIO_PIN_0 );
}

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