#include "ucos_ii.h"
#include "type_define.h"
#include "dw_apb_timers.h"
#include "ucp_param.h"
#include "inter_vector.h"
#include "timers.h"

volatile int timer_s;
volatile int timer_end;

typedef struct timer_struct{

    int timer_irq_num;
    volatile UINT32 *load_count_reg;
    volatile UINT32 * control_reg;
    volatile UINT32 * eoi_reg;
    irq_handler func;

}timer_desc;

void timer_isr();



timer_desc timer_val[8] = {
    {
        APC_TIMER10_INTR,
        ((volatile UINT32 *)(TIMER1_TIMER1BASE + TIMERLOADCOUNTOFF)),
        ((volatile UINT32 *)(TIMER1_TIMER1BASE + TIMERCONTROLREGOFF)),
        ((volatile UINT32 *)(TIMER1_TIMER1BASE + TIMEREOIOFF)),
        timer_isr,

    },
    {
        APC_TIMER11_INTR,
        ((volatile UINT32 *)(TIMER1_TIMER2BASE + TIMERLOADCOUNTOFF)),
        ((volatile UINT32 *)(TIMER1_TIMER2BASE + TIMERCONTROLREGOFF)),
        ((volatile UINT32 *)(TIMER1_TIMER2BASE + TIMEREOIOFF)),
        timer_isr,

    },
    {
        APC_TIMER12_INTR,			
        ((volatile UINT32 *)(TIMER1_TIMER3BASE + TIMERLOADCOUNTOFF)),
        ((volatile UINT32 *)(TIMER1_TIMER3BASE + TIMERCONTROLREGOFF)),
        ((volatile UINT32 *)(TIMER1_TIMER3BASE + TIMEREOIOFF)),
        timer_isr,
    },
    {
        APC_TIMER13_INTR,			
        ((volatile UINT32 *)(TIMER1_TIMER4BASE + TIMERLOADCOUNTOFF)),
        ((volatile UINT32 *)(TIMER1_TIMER4BASE + TIMERCONTROLREGOFF)),
        ((volatile UINT32 *)(TIMER1_TIMER4BASE + TIMEREOIOFF)),
        timer_isr,
    },
    {
        APC_TIMER14_INTR,			
        ((volatile UINT32 *)(TIMER1_TIMER5BASE + TIMERLOADCOUNTOFF)),
        ((volatile UINT32 *)(TIMER1_TIMER5BASE + TIMERCONTROLREGOFF)),
        ((volatile UINT32 *)(TIMER1_TIMER5BASE + TIMEREOIOFF)),
        timer_isr,
    },
    {
        APC_TIMER15_INTR,			
        ((volatile UINT32 *)(TIMER1_TIMER6BASE + TIMERLOADCOUNTOFF)),
        ((volatile UINT32 *)(TIMER1_TIMER6BASE + TIMERCONTROLREGOFF)),
        ((volatile UINT32 *)(TIMER1_TIMER6BASE + TIMEREOIOFF)),
        timer_isr,
    },
    {
        APC_TIMER16_INTR,			
        ((volatile UINT32 *)(TIMER1_TIMER7BASE + TIMERLOADCOUNTOFF)),
        ((volatile UINT32 *)(TIMER1_TIMER7BASE + TIMERCONTROLREGOFF)),
        ((volatile UINT32 *)(TIMER1_TIMER7BASE + TIMEREOIOFF)),
        timer_isr,
    },
    {
        APC_TIMER17_INTR,			
        ((volatile UINT32 *)(TIMER1_TIMER8BASE + TIMERLOADCOUNTOFF)),
        ((volatile UINT32 *)(TIMER1_TIMER8BASE + TIMERCONTROLREGOFF)),
        ((volatile UINT32 *)(TIMER1_TIMER8BASE + TIMEREOIOFF)),
        timer_isr,
    },

};

void timer_isr()
{
    int temp;
    temp = *(timer_val[cur_apeid].eoi_reg);
    OSTimeTick();
}

unsigned int get_cur_count_val()
{
    return TIMER1_TIMER1CURRENTVAL;
}

unsigned int ape_get_timer(unsigned int count)
{
    unsigned int now, base;
    unsigned int value;

    if (!count)
    {
        value = TIMER1_TIMER1CURRENTVAL;
        return value;
    }

    base = count;
    now = TIMER1_TIMER1CURRENTVAL;
    if (base > now)
    {
        return (base - now);
    }
    else
    {
        base = 0xffffffff - now + base;
        return base;
    }
}


void timer_init(int ape_id)
{
    smart_irq_request(timer_val[ape_id].timer_irq_num,timer_val[ape_id].func);
    *(timer_val[ape_id].load_count_reg) = (5000000)*1; //100ms

    *(timer_val[ape_id].control_reg) = 3;

}

