/*
 * ch32v003 interrupts
 */

#include "ch32v00x_it.h"

#include <ch32v00x.h>

#include <stdint.h>

#include "device/btn.h"
#include "device/intr_out.h"
#include "device/irda.h"
#include "i2c_slave.h"




void NMI_Handler(void); //__attribute__((interrupt("WCH-Interrupt-fast")));
void HardFault_Handler(void); //__attribute__((interrupt("WCH-Interrupt-fast")));



void NMI_Handler(void)
{
    while (1);
}

void HardFault_Handler(void)
{
    while (1);
}

void SysTick_Handler(void)
{
    // ---- PROCESS UI AND COMMS

    // button handler
    btn_poll();

    // irda timeout and retry
    irda_process();

    // clear comparison flag
    SysTick->SR = 0;


    // ---- DISABLE INTERRUPT

    // see if any buttons are currently pushed
    if ((BTN_PORT->INDR & BTN_PIN_MASK) != BTN_PIN_MASK) {
        // yup, something is pushed, so keep the interrupt on
        return;
    }

    // see if irda is active

    // nothing is active, so shut down this interrupt
    NVIC_DisableIRQ(SysTicK_IRQn);
}

__attribute__((interrupt("WCH-Interrupt-fast")))
void EXTI7_0_IRQHandler(void)
{
    if (EXTI_GetITStatus(EXTI_Line3)) {
        // accelerometer has triggered.
        // let the master know right away.
        i2cs_append_flag(INT_ACCEL);
        EXTI_ClearITPendingBit(EXTI_Line3);
    }

    if (EXTI_GetITStatus(EXTI_Line0 | EXTI_Line4 | EXTI_Line5 | EXTI_Line6 | EXTI_Line7)) {
        // button is pushed.
        // enable main tick handler on button push.
        // tick handler will sense when it isn't needed and shut itself off.
        SysTick->SR = 0;
        NVIC_EnableIRQ(SysTicK_IRQn);
        EXTI_ClearITPendingBit(EXTI_Line0 | EXTI_Line4 | EXTI_Line5 | EXTI_Line6 | EXTI_Line7);
    }
}
