/*                                                                          */
/* File Name   : mbdr_dsp_cyclic_buf_double_header.h                        */
/*                                                                          */
/* Written By  : 79556                                                      */
/*                                                                          */
/* versions    :                                                            */
/*+--------+-----------+---------------------------------------------------+*/
/*| Version| Date      | Change content                                    |*/
/*+--------+-----------+---------------------------------------------------+*/
/*|        |29/Aug/2006| Creation                                          |*/
/*+--------+-----------+---------------------------------------------------+*/
/*|        |06/Mrx/2007| Supports user input memcpy to far CPU             |*/
/*+--------+-----------+---------------------------------------------------+*/
/*| 0.9.08 | 4/Dec/2007|- support pentium c++ compiler for interface       |*/
/*|        |           |- avoid use size_t by                              |*/
/*|        |           |  mbdr_dsp_common_cyclic_buffer_header.h           |*/
/*+--------+-----------+---------------------------------------------------+*/
/*| 0.01.97| 7/Jul/2009|- mbdr_dsp_cyclic_buf_double_header_write_n_items  |*/
/*|        |           |- mbdr_dsp_cyclic_buf_double_header_read_n_items   |*/
/*+--------+-----------+---------------------------------------------------+*/
/*| 0.02.37|19/Feb/2010|- MBDR_DSP_CYCLIC_BUFFER_DOUBLE_HEADER_            |*/
/*|        |           |             ADD_FAR_CPU_BASE_AND_BYTE_OFFSET_M    |*/
/*+--------+-----------+---------------------------------------------------+*/
/*| 0.02.43|22/Mrs/2010|- MBDR_DSP_CYCLIC_BUFFER_COPY_INDEX_COUNTERS_M     |*/
/*+--------+-----------+---------------------------------------------------+*/
/*| 0.02.44|25/Mrs/2010|- _cyclic_buf_double_header_increment_read_index   |*/
/*+--------+-----------+---------------------------------------------------+*/
/*| 0.02.45|13/Apr/2010|-_cyclic_buf_double_header_increment_write_index   |*/
/*+--------+-----------+---------------------------------------------------+*/
/*| 0.02.94|20/Dec/2011|- replace programmer name by code                  |*/
/*+--------+-----------+---------------------------------------------------+*/
/*                                                                          */
/* Function    : CPU->CPU cyclic buffer manage                              */
/*               each CPU performs only write access to neibour CPU         */
/* Background  : reading private memory of one CPU by other CPU cause       */
/*               to big overload over BUS                                   */
/*                                                                          */
/*                                                                          */
/*  Available routines:                                                     */
/*    1) void mbdr_dsp_cyclic_buf_double_header_config(                     */
/*                SHORT          pi_entries_number,                         */
/*                SHORT          pi_entry_byte_size,                        */
/*                void          *pi_buffer_data_area_p,                     */
/*                LONG           pi_buffer_data_area_physical_address,      */
/*        mbdr_dsp_cyclic_buf_hdr_t  *po_close_buffer_header_area_p,        */
/*        mbdr_dsp_cyclic_buf_hdr_t  *po_far_buffer_header_area_p);         */
/*                                                                          */
/*    2)void mbdr_dsp_cyclic_buf_write(                                     */
/*                   void                          *pi_data_p,              */
/*        mbdr_dsp_cyclic_buf_hdr_t  *pio_close_buffer_header_area_p,       */
/*        mbdr_dsp_cyclic_buf_hdr_t  *po_far_buffer_header_area_p);         */
/*                                                                          */
/*    3)void mbdr_dsp_cyclic_buf_read(                                      */
/*                   void  *po_read_data_p,                                 */
/*        mbdr_dsp_cyclic_buf_hdr_t  *pio_close_buffer_header_area_p,       */
/*        mbdr_dsp_cyclic_buf_hdr_t  *po_far_buffer_header_area_p);         */
/*                                                                          */
/*                                                                          */
/*    Buffer Configuration Process:                                         */
/*     1) call mbdr_dsp_cyclic_buf_double_header_config                     */
/*        from reader CPU, base pointer value is being written to writer    */
/*        CPU                                                               */
/*     2) If necessary the reader changes its base pointer value.           */
/*        through mbdr_dsp_cyclic_buf_double_header_change_base_pointer     */
/*     3) call mbdr_dsp_cyclic_buffer_config_write_data_proc_p              */
/*        from the writer CPU                                               */
/*     4) Adjust base pointer by writer value up to system memory mapping   */
/*        requirement, it can be done by                                    */
/*        4.1 mbdr_dsp_cyclic_buf_double_header_change_base_pointer         */
/*        4.2 mbdr_dsp_cyclic_buf_double_header_change_base_pointer         */
/*                                                                          */
/*                                                                          */
/*                                                                          */


#include  "mbdr_dsp_cyclic_buffer.h"
#include  "windef.h"

#ifndef  _MBDR_DSP_CYCLIC_BUF_DOUBLE_HEADER_H
#define  _MBDR_DSP_CYCLIC_BUF_DOUBLE_HEADER_H


/* configure & initialize cyclic buffer */
extern void mbdr_dsp_cyclic_buf_double_header_config(
          SHORT                          pi_entries_number,
          SHORT                          pi_entry_byte_size,
          void                          *pi_buffer_data_area_p,
		  void* (*pi_far_cpu_memcpy_proc_p)(void *, const void *, USHORT, USHORT),
          USHORT pi_write_header_memcpy_code,
          mbdr_dsp_cyclic_buf_hdr_t  *po_close_buffer_header_area_p,
          mbdr_dsp_cyclic_buf_hdr_t  *po_far_buffer_header_area_p);


/* write item to cyclic buffer */
extern void mbdr_dsp_cyclic_buf_double_header_write(
             void                          *pi_data_p,
             mbdr_dsp_cyclic_buf_hdr_t  *pio_close_buffer_header_area_p,
             mbdr_dsp_cyclic_buf_hdr_t  *po_far_buffer_header_area_p);


/* get item from cyclic buffer */
extern void mbdr_dsp_cyclic_buf_double_header_read(
              void                       *po_read_data_p,
              mbdr_dsp_cyclic_buf_hdr_t  *pio_close_buffer_header_area_p,
              mbdr_dsp_cyclic_buf_hdr_t  *po_far_buffer_header_area_p);

/* writes N items into cyclic buffer */
extern void mbdr_dsp_cyclic_buf_double_header_write_n_items(
              void                       *pi_write_data_buf_p,
			  SHORT                       pi_items_number,
              mbdr_dsp_cyclic_buf_hdr_t  *pio_close_buffer_header_area_p,
              mbdr_dsp_cyclic_buf_hdr_t  *po_far_buffer_header_area_p);

/* reads N items from cyclic buffer */
extern void mbdr_dsp_cyclic_buf_double_header_read_n_items(
              void                       *po_read_data_buf_p,
			  SHORT                       pi_items_number,
              mbdr_dsp_cyclic_buf_hdr_t  *pio_close_buffer_header_area_p,
              mbdr_dsp_cyclic_buf_hdr_t  *po_far_buffer_header_area_p);

/* returns buffer base pointer  */
extern UCHAR* mbdr_dsp_cyclic_buf_double_header_get_base_pointer(
              mbdr_dsp_cyclic_buf_hdr_t  *pi_close_buffer_header_area_p);

/* change cyclic buffer base pointer  */
extern void  mbdr_dsp_cyclic_buf_double_header_change_base_pointer(
              UCHAR                       *pi_new_buffer_base_p,
              mbdr_dsp_cyclic_buf_hdr_t  *po_close_buffer_header_area_p);

/* configs write data memcpy routine pointer                     */
/* if routine is not called SW uses standard C memcpy as default */
extern void  mbdr_dsp_cyclic_buffer_config_write_data_proc_p
     (void* (*pi_write_data_memcpy_proc_p)(void *, const void *, USHORT, USHORT),
     mbdr_dsp_cyclic_buf_hdr_t  *po_cyclic_buffer_hdr_p);

/* default values are 0                                   */
/* routine is necessary for between computers application */
extern void  mbdr_dsp_cyclic_buffer_config_write_memcpy_user_application(
         USHORT pi_write_data_memcpy_code,
         USHORT pi_write_header_memcpy_code,
         mbdr_dsp_cyclic_buf_hdr_t  *po_close_buffer_header_area_p);

/* dispache data from internal single header source cylic queue */
/* to double header cylic queue                                 */
extern void
   mbdr_dsp_cyclic_buffer_dispatch_from_single_header_to_double_header(
     mbdr_dsp_cyclic_buf_hdr_t
                   *pio_source_single_header_cyclic_buffer_hdr_p,
     SHORT          pi_items_number,

     mbdr_dsp_cyclic_buf_hdr_t  *pio_destination_close_double_header_hdr_p,
     mbdr_dsp_cyclic_buf_hdr_t  *pio_destination_far_double_header_hdr_p);


/* increment read_index by 1 of double header cyclic buffer */
extern void mbdr_dsp_cyclic_buf_double_header_increment_read_index(
                   mbdr_dsp_cyclic_buf_hdr_t  *pio_close_buffer_header_area_p,
                   mbdr_dsp_cyclic_buf_hdr_t  *po_far_buffer_header_area_p);

/* increment write_index by 1 of double header cyclic buffer */
extern void mbdr_dsp_cyclic_buf_double_header_increment_write_index(
                   mbdr_dsp_cyclic_buf_hdr_t  *pio_close_buffer_header_area_p,
                   mbdr_dsp_cyclic_buf_hdr_t  *po_far_buffer_header_area_p);

#define MBDR_DSP_CYCLIC_BUF_DOUBLE_HEADER_CHANGE_BASE_POINTER_ADD_M(\
              PI_ADD_VALUE,\
              PIO_CLOSE_BUFFER_HEADER_AREA_P)\
        mbdr_dsp_cyclic_buf_double_header_change_base_pointer(\
              mbdr_dsp_cyclic_buf_double_header_get_base_pointer(\
                PIO_CLOSE_BUFFER_HEADER_AREA_P)+PI_ADD_VALUE,\
                PIO_CLOSE_BUFFER_HEADER_AREA_P)

/* MACRO performs aithmetic addition of input parameters and re *mbdr_dsp_cyclic_buf_hdr_p */
#define MBDR_DSP_CYCLIC_BUFFER_DOUBLE_HEADER_ADD_FAR_CPU_BASE_AND_BYTE_OFFSET_M(\
		        PI_FAR_CPU_BASE,\
                PI_FAR_CPU_DOUBLE_HEADER_BYTE_OFFSET)\
       ((mbdr_dsp_cyclic_buf_hdr_t *)\
           ( ((ULONG)PI_FAR_CPU_BASE)\
            +((ULONG)PI_FAR_CPU_DOUBLE_HEADER_BYTE_OFFSET)))

/* MACRO copies read index from close header to far header it enables */
/* user to read a few doses of data and afterwards update read index  */
/* in FAR CPU cyclic_buffer_header_t                                  */
#define  MBDR_DSP_CYCLIC_BUF_DOUBLE_HEADER_COPY_READ_INDEX_M(\
                      PI_CLOSE_BUFFER_HEADER_AREA_P,\
					  PO_FAR_BUFFER_HEADER_AREA_P)\
  (PI_CLOSE_BUFFER_HEADER_AREA_P)->write_memcpy_proc_p\
             ((void *)&(PO_FAR_BUFFER_HEADER_AREA_P)->read_index,\
             (const void *)&(PI_CLOSE_BUFFER_HEADER_AREA_P)->read_index,\
              sizeof(LONG),\
              (PI_CLOSE_BUFFER_HEADER_AREA_P)->header_memcpy_code)

/* MACRO copies write index from close header to far header it enables */
/* user to read a few doses of data and afterwards update read index  */
/* in FAR CPU cyclic_buffer_header_t                                  */
#define  MBDR_DSP_CYCLIC_BUF_DOUBLE_HEADER_COPY_WRITE_INDEX_M(\
                      PI_CLOSE_BUFFER_HEADER_AREA_P,\
					  PO_FAR_BUFFER_HEADER_AREA_P)\
  (PI_CLOSE_BUFFER_HEADER_AREA_P)->write_memcpy_proc_p\
             ((void *)&(PO_FAR_BUFFER_HEADER_AREA_P)->write_index,\
             (const void *)&(PI_CLOSE_BUFFER_HEADER_AREA_P)->write_index,\
              sizeof(LONG),\
              (PI_CLOSE_BUFFER_HEADER_AREA_P)->header_memcpy_code)

#endif
