#include "omw_uart.h"
#include "t1001_it.h"
#include "omw_gpio.h"

#define GPADC_Handler   Interrupt7_Handler
#if defined(OMW_HAS_SCH) && (defined(OMW_BLE_V0) || defined(OMW_BLE_V0_2_0))
#include "omw_rtos.h"
#endif

extern void calc_hp_offset_at_curr_temp();
extern void RF_BT_IRQHandler(void);
#if defined(OMW_BLE_V0) || defined(OMW_BLE_V0_2_0)
extern void BT_TIMER_IRQHandler(void);
#endif
extern void VTIMER_IRQHandler(void);
#ifdef OMW_EN_DEEP_SLEEP
extern void Check_PwrOn_OnBbDmaIrq();
extern void  Check_PwrOn_OnBbDmaIrq_Before();

#ifndef OMW_OTP_ROM_USE_OTP_INIT
extern void otp_powup();
extern void otp_powdown();
#endif
#endif

#if defined(OMW_DEBUG_TIFS) || defined(OMW_DEBUG_RX_TIMING)
#include "t1001_bt.h"

extern volatile uint8_t  tifs_test_flag;
volatile uint32_t rxon_tick;
volatile uint32_t  rx_gap_ticks;
volatile uint16_t  min_rx_gap_ticks = 0xFFFF;
volatile uint16_t  max_rx_gap_ticks;
volatile uint32_t  avg_gap_ticks;

extern RfBtGlobalParameters_t rf_bt_mgr;
#endif

#if defined(CLK_CMP_TEST) && defined(OMW_CPU_E902)
uint32_t test_cnts;
extern volatile uint8_t  need_pwr_on;
#endif

#ifdef OMW_EN_SLEEP_TMR_CHK
void omw_app_sleep_process();
#endif

#ifndef OMW_ON_FPGA
#ifndef OMW_MCU_ONLY
void  dynamic_agc_info_get();
#endif
#endif
ATTRIBUTE_ISR __RAM_CODE_SECTION void RADIO_DMA_Handler(void)
{
    #if defined(OMW_DEBUG_TIFS) || defined(OMW_DEBUG_RX_TIMING)
    if (0 == tifs_test_flag)
    {
        uint8_t *p0 = rf_bt_mgr.rx_ptr;
        if (p0 != NULL)
        {
            tifs_test_flag = 3;
            REG_WRT(0x42000064, 1);
            return;
        }
    }
    else if (3 == tifs_test_flag)
    {
        tifs_test_flag = 0;
    }
    else if (1 == tifs_test_flag)
    {
        tifs_test_flag = 2;
        rxon_tick = REG_RD(0x42000104);
        REG_WRT(0x42000064, 1);

        return;
    }
    else
    {
        if (tifs_test_flag == 2)
        {
            uint16_t status = RADIO_BASEBAND_DMA->BB_STATUS;
            uint8_t *p0 = rf_bt_mgr.rx_ptr;

           tifs_test_flag = 0;
           if (p0 && (status & BTSTATUS_RXSYNC))
           {
              static uint8_t chk_tms = 0;
              rx_gap_ticks = REG_RD(0x4200003C) - rxon_tick;
              if (rx_gap_ticks < 100)
              {
                  avg_gap_ticks += rx_gap_ticks;
                  if (min_rx_gap_ticks > rx_gap_ticks) min_rx_gap_ticks = rx_gap_ticks;
                  if (max_rx_gap_ticks < rx_gap_ticks) max_rx_gap_ticks = rx_gap_ticks;

                  chk_tms++;

                  if (chk_tms > 63)
                  {
                      avg_gap_ticks = avg_gap_ticks / chk_tms;
                      while(1);
                  }
              }
           }
        }
    }
    #endif

    #ifdef DEBUG_TIMING_BUF_BASE
    REG_WRT(DEBUG_TIMING_BUF_BASE, REG_RD(0x42000104));
    REG_WRT((DEBUG_TIMING_BUF_BASE + 4), 0);
    #endif

    #ifndef OMW_OTP_ROM_USE_OTP_INIT
    #ifdef OMW_EN_DEEP_SLEEP
    #ifdef DEBUG_TIMING_BUF_BASE
    extern uint8_t otp_pwr_st;
    extern uint8_t on_sleep_st;

    uint8_t  from_sleep = ((!otp_pwr_st) && on_sleep_st);
    #endif

    otp_powup();
    #endif
    #endif

    #if defined(CLK_CMP_TEST) && defined(OMW_CPU_E902)
    if ((REG_RD(0x42000104) - test_cnts) >= 30 * 60 * 1000 * 1000)
    {
        need_pwr_on = 1;
    }
    #endif

    #ifndef OMW_ON_FPGA
    #ifndef OMW_MCU_ONLY
    dynamic_agc_info_get();
    #endif
    #endif

    #ifdef OMW_EN_DEEP_SLEEP
    Check_PwrOn_OnBbDmaIrq_Before();
    #endif

    omw_hw_set_in_irq_flag();

    RF_BT_IRQHandler();  //v0/t1101: rom api, v0_2_0/v2: source code

    #ifdef OMW_EN_DEEP_SLEEP
    Check_PwrOn_OnBbDmaIrq();
    #endif

    #if defined(OMW_HAS_SCH) && (defined(OMW_BLE_V0) || defined(OMW_BLE_V0_2_0))
    omw_rtos_low_pri_free_cpu(OMW_CTRL_ARG_FLAG);
    #endif

    omw_hw_clr_in_irq_flag();

    #ifdef DEBUG_TIMING_BUF_BASE
    REG_WRT((DEBUG_TIMING_BUF_BASE + 0x14) , REG_RD(0x42000104));

    #ifdef OMW_EN_DEEP_SLEEP
    // uint32_t val11 = REG_RD(DEBUG_TIMING_BUF_BASE);  (val11 & 0xFFFF0000) ==
    uint32_t val22 = REG_RD((DEBUG_TIMING_BUF_BASE + 4));
    while(from_sleep && (val22 != 0));
    #endif
    #endif

    #ifndef OMW_OTP_ROM_USE_OTP_INIT
    #ifdef OMW_EN_DEEP_SLEEP
    #ifdef OMW_EN_SLEEP_TMR_CHK
    omw_app_sleep_process();
    #endif

    otp_powdown();
    #endif
    #endif
}

#if defined(OMW_BLE_V0) || defined(OMW_BLE_V0_2_0)
ATTRIBUTE_ISR __RAM_CODE_SECTION void RADIO_TICK_Handler(void)
{
    #ifndef OMW_OTP_ROM_USE_OTP_INIT
    #ifdef OMW_EN_DEEP_SLEEP
    otp_powup();
    #endif
    #endif

    BT_TIMER_IRQHandler(); //rom api

    // uint16_t  bb_ctrl_val = 0xf700;
    // if (rf_bt_mgr.rx_ptr != NULL)
    // {
    //    bb_ctrl_val = 0xf708;
    // }
    // REG_WRT(0x42000008, bb_ctrl_val);

    #ifdef OMW_EN_DEEP_SLEEP
    Check_PwrOn_OnBbDmaIrq();
    #endif

    #if defined(OMW_HAS_SCH)
    omw_rtos_low_pri_free_cpu(OMW_CTRL_ARG_FLAG);
    #endif

    #ifndef OMW_OTP_ROM_USE_OTP_INIT
    #ifdef OMW_EN_DEEP_SLEEP
    otp_powdown();
    #endif
    #endif

    #ifdef OMW_BLE_V0
    calc_hp_offset_at_curr_temp();
    #endif
}
#endif

ATTRIBUTE_ISR void UART1_Handler(void)
{
    omw_uart_it_rx_by_base_addr(OMW_UART1);
}

#if defined(OMW_BLE_HOST_V0) || defined(OMW_BLE_HOST_V1) || defined(OMW_BLE_HOST_V2) || defined(OMW_BLE_HOST_OTHER) || defined(OMW_CLTR_SLEEP_TEST)
ATTRIBUTE_ISR void RTC_Handler(void)
{
    omw_hw_set_in_irq_flag();

    VTIMER_IRQHandler();

    omw_hw_clr_in_irq_flag();
}
#endif

#if 0
ATTRIBUTE_ISR void GPADC_Handler(void)
{

}

ATTRIBUTE_ISR void WDT_Handler(void)
{

}

ATTRIBUTE_ISR void GPIO_Handler(void)
{

}

ATTRIBUTE_ISR void TIMER0_Handler(void)
{

}

ATTRIBUTE_ISR void TIMER1_Handler(void)
{

}

ATTRIBUTE_ISR void TIMER2_Handler(void)
{

}

ATTRIBUTE_ISR void TIMER3_Handler(void)
{

}

ATTRIBUTE_ISR void TIMER4_Handler(void)
{
  omw_gpio_set_output_en(OMW_GPIO_PIN19,ENABLE);
  omw_gpio_set_output(OMW_GPIO_PIN19, HIGH);
}

ATTRIBUTE_ISR void TIMER5_Handler(void)
{
  omw_gpio_set_output_en(OMW_GPIO_PIN19,ENABLE);
  omw_gpio_set_output(OMW_GPIO_PIN19, HIGH);
}

ATTRIBUTE_ISR void TIMER6_Handler(void)
{

}

ATTRIBUTE_ISR void TIMER7_Handler(void)
{

}
#endif
