/************************************************************************
* NAME: fnet_cpu_isr_install
*
* DESCRIPTION: 
*************************************************************************/
int fnet_cpu_isr_install(unsigned int vector_number, unsigned int priority)
{
    int result;
    unsigned long *irq_vec;
    int div;
    int irq_number; /* The irq number NOT the vector number.*/

    irq_vec = (unsigned long *)(FNET_CFG_CPU_VECTOR_TABLE) + vector_number;

    if(*irq_vec != (unsigned long) FNET_ISR_HANDLER)
    { /* It's not installed yet.*/
        *irq_vec = (unsigned long) FNET_ISR_HANDLER;
    }
        
    if(*irq_vec == (unsigned long) FNET_ISR_HANDLER)
    {        
        /* Make sure that the IRQ is an allowable number. */
        irq_number = vector_number - 16;
        div = irq_number/32;
        if((div >= 0) && (div < 3))
        {
            /* Initialize the NVIC to enable the specified IRQ.*/
            FNET_MK_NVIC_ICPR(div) |= 1 << (irq_number%32); /* Clear-pending. */
            FNET_MK_NVIC_ISER(div) |= 1 << (irq_number%32); /* Set-enable.*/
            /* Set priority.*/
            FNET_MK_NVIC_IP(irq_number) = (0x7-priority&0x7)<<4;
        }
        result = FNET_OK;
    }
    else
        result = FNET_ERR;
   
   return result;            
}
/************************************************************************
* NAME: fnet_cpu_isr_install
*
* DESCRIPTION:
*************************************************************************/
fnet_return_t fnet_cpu_isr_install(fnet_uint32_t vector_number, fnet_uint32_t priority)
{
    fnet_return_t   result;
    fnet_uint32_t   *irq_vec;
    fnet_uint32_t   divider;
    fnet_uint32_t   irq_number; /* The irq number NOT the vector number.*/

    irq_vec = (fnet_uint32_t *)(FNET_CFG_CPU_VECTOR_TABLE) + vector_number;

    fnet_bool_t isrInstalled = *irq_vec & ~(0x1) != (fnet_uint32_t) FNET_ISR_HANDLER;
    if(!isrInstalled)
    {
        /* It's not installed yet.*/
        *irq_vec = (fnet_uint32_t) FNET_ISR_HANDLER | 0x1;
    }
    isrInstalled = *irq_vec & ~(0x1) != (fnet_uint32_t) FNET_ISR_HANDLER;

    if(priority > FNET_CFG_CPU_VECTOR_PRIORITY_MAX)
    {
        priority = FNET_CFG_CPU_VECTOR_PRIORITY_MAX;
    }

    if(isrInstalled)
    {
        /* Make sure that the IRQ is an allowable number. */
        irq_number = vector_number - 16u;
        divider = irq_number / 32u;
        if(divider < 3u)
        {
            /* Initialize the NVIC to enable the specified IRQ.*/
            FNET_MK_NVIC_ICPR(divider) |= (fnet_uint32_t)(1u << (irq_number % 32u)); /* Clear-pending. */
            FNET_MK_NVIC_ISER(divider) |= (fnet_uint32_t)(1u << (irq_number % 32u)); /* Set-enable.*/
            /* Set priority.*/
            FNET_MK_NVIC_IP(irq_number) = (fnet_uint8_t)(FNET_CFG_CPU_VECTOR_PRIORITY_MAX - priority) << 4;
        }
        result = FNET_OK;
    }
    else
    {
        result = FNET_ERR;
    }

    return result;
}