/*                                                                          */
/* File Name   : mbdr_dsp_timer.h                                           */
/*                                                                          */
/* Written By  : 79556                                                      */
/*                                                                          */
/* versions    :                                                            */
/*               +-----------+----------------------------------------+     */
/*               | Date      | Change content                         |     */
/*               +-----------+----------------------------------------+     */
/*               |02/May/2007| Creation                               |     */
/*               +-----------+----------------------------------------+     */
/*                                                                          */
/* Function    : define timer utilities                                     */
/*                                                                          */
/* Macros      1)                                                           */
/*                                                                          */
/*                                                                          */

#ifndef _MBDR_DSP_TIMER_H
#define _MBDR_DSP_TIMER_H

#include  <csl.h>
#include  "windef.h"
#include  <string.h>
#include  "mbdr_dsp_utils.h"
#include  "mbdr_dsp_timer_family.h"


/* returns timer handle: INV - fault allocation */
/*                        ?? - ok               */
extern TIMER_Handle mbdr_dsp_init_clk_timer(void);

/* manages required timer allocation and configuration */
extern TIMER_Handle mbdr_dsp_init_timer(LONG timer_dev_code);

/* configs already  allocated timer to cyclic mode */
extern void mbdr_dsp_config_timer_to_cyclic_mode(TIMER_Handle pi_timer_handle);

extern TIMER_Handle mbdr_dsp_clk_timer_handle;

#define MBDR_DSP_READ_TIMER_CNT_REG_M(PI_TIMER_HANDLE)\
             TIMER_RGETH(PI_TIMER_HANDLE,CNT)
#define MBDR_DSP_READ_CLK_TIMER_CNT_M  TIMER_RGETH(mbdr_dsp_clk_timer_handle,CNT)


#define MBDR_DSP_TIMER_WRITE_CNT_REG_M(PI_TIMER_HANDLE, PI_VALUE)\
                  TIMER_RSETH(PI_TIMER_HANDLE, CNT, PI_VALUE) /* start  */

typedef struct
{
 SHORT  event_code;    /* timer CNT value upon event           */
 SHORT  user_info_0;   /* filled by user                       */
 LONG   dsp_clocks_differenc;/* difference compare to previous clock */
   /* SW takes into consideration that time input is DSP clock /8 */
 ULONG  clk;           /* timer CNT value upon event           */
 LONG   user_info_1;   /* filled by user                       */
}mbdr_dsp_timer_record_event_t;

#define MBDR_DSP_TIMER_RECORD_BUFFER_SIZE 32
extern mbdr_dsp_timer_record_event_t
          mbdr_dsp_timer_record_event_buf[MBDR_DSP_TIMER_RECORD_BUFFER_SIZE+1];

typedef struct
{
 LONG  index;
 ULONG previous_cnt_reg;
 ULONG current_cnt_reg;
 LONG  last_difference;
}mbdr_dsp_timer_record_control_t;
extern mbdr_dsp_timer_record_control_t mbdr_dsp_timer_record_control;

/* internal timer divides clock by 8 */
#define MBDR_DSP_CLOCK_2_INTERNAL_TIMER_RATIO 8

#define MBDR_DSP_TIMER_CNT_SAMPLES_TO_DELTA_DSP_CLOCKS_M(PI_CURRENT_TIMER_CNT_REG,\
                                                         PI_PREVIOUS_TIMER_CNT_REG)\
           ((((ULONG)PI_CURRENT_TIMER_CNT_REG)-((ULONG)PI_PREVIOUS_TIMER_CNT_REG))\
              *MBDR_DSP_CLOCK_2_INTERNAL_TIMER_RATIO)

/* timer record enable bits, */
extern LONG mbdr_dsp_timer_record_enable_bits;

/* eeeeeeee, enables user easily detect end of record */
/* following memory smashing won't happen since buffer has extra unused entry */

#define MBDR_DSP_TIMER_RECORD_EVENT_M(pi_event_code, pi_user_info_0, pi_user_info_1)\
   {\
    static ULONG current_cnt_reg, last_difference;\
	if(MBDR_DSP_UTILS_BIT_TEST_M(mbdr_dsp_timer_record_enable_bits, (pi_event_code)))\
	{\
      current_cnt_reg = MBDR_DSP_READ_CLK_TIMER_CNT_M;\
      mbdr_dsp_timer_record_event_buf[mbdr_dsp_timer_record_control.index].event_code = pi_event_code;\
      mbdr_dsp_timer_record_event_buf[mbdr_dsp_timer_record_control.index].user_info_0 = pi_user_info_0;\
      mbdr_dsp_timer_record_event_buf[mbdr_dsp_timer_record_control.index].user_info_1 = pi_user_info_1;\
      mbdr_dsp_timer_record_event_buf[mbdr_dsp_timer_record_control.index].clk   = current_cnt_reg;\
      last_difference = MBDR_DSP_TIMER_CNT_SAMPLES_TO_DELTA_DSP_CLOCKS_M(\
                                 current_cnt_reg,mbdr_dsp_timer_record_control.previous_cnt_reg);\
      mbdr_dsp_timer_record_event_buf[mbdr_dsp_timer_record_control.index].dsp_clocks_differenc =\
          last_difference;\
      mbdr_dsp_timer_record_control.last_difference = last_difference;\
      mbdr_dsp_timer_record_event_buf[mbdr_dsp_timer_record_control.index+1].clk = 0xeeeeeeee;\
	  mbdr_dsp_timer_record_control.index =\
	   (mbdr_dsp_timer_record_control.index + 1)&(MBDR_DSP_TIMER_RECORD_BUFFER_SIZE-1);\
      mbdr_dsp_timer_record_control.previous_cnt_reg = current_cnt_reg;\
	}\
   }


#endif
