#include <basic.h>
#include <tk/tkernel.h>

#define  TEST_USE_INTERRUPT1  40
#define  TEST_USE_INTERRUPT2  45


UINT  test_int_number_1 ;
UINT  test_int_number_2 ;


/*************************************************/
/*
 * Interrupt Handler 1
 */
void  test_inthandler1( UINT dintno )
{
    test_int_number_1 = dintno ;
}


/*************************************************/
/*
 * Interrupt Handler 2
 */
void  test_inthandler2( UINT dintno )
{
    test_int_number_2 = dintno ;
}


/*************************************************/
void  set_inthandler1( void )
{
    T_DINT  pk_dint ;

    /** tk_def_int() **/
    pk_dint.intatr = TA_HLNG ;
    pk_dint.inthdr = (FP)test_inthandler1 ;
    (void)tk_def_int( TEST_USE_INTERRUPT1, &pk_dint ) ;
}


/*************************************************/
void  set_inthandler2( void )
{
    T_DINT  pk_dint ;

    /** tk_def_int() **/
    pk_dint.intatr = TA_HLNG ;
    pk_dint.inthdr = (FP)test_inthandler2 ;
    (void)tk_def_int( TEST_USE_INTERRUPT2, &pk_dint ) ;
}


#define NVIC_BASE       0xe000e000
#define NVIC_SETPEND        0x0200

/*************************************************/
void  interrupt_occuer( UINT no )
{
    UW  *reg ;
    UINT  irq ;

    irq = no - 16 ;

    EnableInt( irq ) ;

    reg = ((UW *)(NVIC_BASE+NVIC_SETPEND) + (irq>>5));
    out_w( (UW)reg, (0x01U << (0x0000001f & irq)));
}


/*************************************************/
void  test_int_main( void )
{
    test_int_number_1 = 0 ;
    test_int_number_2 = 0 ;

    set_inthandler1() ;
    set_inthandler2() ;

    interrupt_occuer( TEST_USE_INTERRUPT1 ) ;
    interrupt_occuer( TEST_USE_INTERRUPT2 ) ;

    if( (test_int_number_1 != TEST_USE_INTERRUPT1)
     || (test_int_number_2 != TEST_USE_INTERRUPT2) ) {

        while(1) ;  /* ERROR happen */

    }
}

