// ******************** dsp_interrupt.h ***************************************
//                 Risc-Dsp runtime library
//
// This file implements the risc interface to dsp interrupt
// by setting mask IMASKR and set handler vector.
// For using this module you must manually add "dsp_interrupt_hnlr.c"
// and "dsp_interrupt_handler.s" to your dsp unit
// (this files locate at ElcoreSDK/ERLib/src/elcore). Also you must
// extern in your risc module common handler function "_inter_hnlr"
// and handlers vector "func_hnlr_dsp", e.g.
// extern int dspu__inter_hnlr;
// extern void* dspu__func_hnlr_dsp[];
//
// (c) multicore.ru
//
// *******************************************************************
#include "erlcommon.h"
#include "libdsp.h"
#include "risc_dsp_caller.h"

#ifndef _DSP_INTERRUPT_
#define _DSP_INTERRUPT_

#ifdef	__cplusplus
extern "C" {
#endif

typedef void( *DSP_INTERRUPT )(void*);

#define UNKNOWN_DSP ((DSP_INTERRUPT)(-1))

#define IMASKR_MCH0_MASK     (0x00000001)
#define IMASKR_MCH1_MASK     (0x00000002)
#define IMASKR_MCH2_MASK     (0x00000004)
#define IMASKR_MCH3_MASK     (0x00000008)
#define IMASKR_DRQ_MASK      (0x0000000f)
#define IMASKR_IRQ0_MASK     (0x01000000)
#define IMASKR_IRQ1_MASK     (0x02000000)
#define IMASKR_TMR_MASK      (0x10000000)
#define IMASKR_FPE_MASK      (0x20000000)
#define IMASKR_QT0_MASK      (0x40000000)
#define IMASKR_QT1_MASK      (0x80000000)

//Enumination: DSP_INTERRUPT_TYPE
//
//List of dsp interruptions types
typedef enum
{
        // Constant: DSP_INT_DRQ
        // Interrupt from dma channel
        DSP_INT_DRQ = 0,
        // Constant: DSP_INT_IRQ0
        // Program interrupt from dsp0
        DSP_INT_IRQ0,
        // Constant: DSP_INT_IRQ1
        // Program interrupt from dsp1
        DSP_INT_IRQ1,
        // Constant: DSP_INT_TMR
        // Interrupt from timer
        DSP_INT_TMR,
        // Constant: DSP_INT_FPE
        // Float point exception
        DSP_INT_FPE,
        // Constant: DSP_INT_QT0
        // Interrupt from cpu, qstr0
        DSP_INT_QT0,
        // Constant: DSP_INT_QT1
        // Interrupt from cpu, qstr1-qstr2
        DSP_INT_QT1,
} DSP_INTERRUPT_TYPE;

// Function: dsp_enable_interrupt
//
// Enable dsp interrupt by setting mask IMASKR
//
// Parameters:
//      type - type of interrupt (from enum DSP_INTERRUPT_TYPE)
//		hnlr_addr - address of "_inter_hnlr" from dsp unit
//      pTask - task (*dsp_task_info)
//
// Examples:
//  > dsp_enable_interrupt(DSP_INT_TMR, (unsigned int)&dspu__inter_hnlr, *pTask);
//
// See also:
//
//      dsp_disable_interrupt
//
void dsp_enable_interrupt(DSP_INTERRUPT_TYPE type, unsigned int hnlr_addr, struct dsp_task_info* pTask);

// Function: dsp_disable_interrupt
//
// Disable dsp interrupt by setting mask IMASKR
//
// Parameters:
//      type - type of interrupt (from enum DSP_INTERRUPT_TYPE)
//      pTask - task (*dsp_task_info)
//
// Examples:
//  > dsp_disable_interrupt(DSP_INT_TMR, *pTask);
//
// See also:
//
//      dsp_enable_interrupt
//      dsp_disable_interrupts
//
void dsp_disable_interrupt(DSP_INTERRUPT_TYPE type, struct dsp_task_info* pTask);

// Function: dsp_disable_interrupts
//
// Disable all dsp interrupts by setting mask IMASKR=0
//
// Parameters:
//      pTask - task (*dsp_task_info)
//
// Examples:
//  > dsp_disable_interrupts(*pTask);
//
// See also:
//
//      dsp_enable_interrupt
//      dsp_disable_interrupt
//
void dsp_disable_interrupts(struct dsp_task_info* pTask);

// Function: risc_dsp_register_interrupt_task
//
// Register dsp interrupt handler function in handlers vector
//
// Parameters:
//		pTask - task pointer
//		hnlr_vector - vector of interrupt handlers from dsp unit (func_hnlr_dsp[])
//      fn - your handler function
//      type - type of interrupt, for which you set handler
//
// Returns:
//      ERL_ERROR - status of error (from enum ERL_ERROR)
//
// Examples:
//  > risc_dsp_register_interrupt_task(&pTask, dspu__func_hnlr_dsp, &dsp_tmr_hnlr, DSP_INT_TMR);
//
// See also:
//
//      dsp_enable_interrupt
//
enum ERL_ERROR risc_dsp_register_interrupt_task(struct dsp_task_info* pTask, void* hnlr_vector[], unsigned int fnAddress, DSP_INTERRUPT_TYPE type);

// Function: dsp_send_pi
//
// Set program interrupt
//
// Parameters:
//      dsp_num - number of dsp core
//
// Examples:
//  > dsp_send_pi(0);
//
// See also:
//
//      dsp_enable_interrupt
//
void dsp_send_pi(int dsp_num);


#ifdef	__cplusplus
}
#endif

#endif //_DSP_INTERRUPT_