/*
 * c37_94.h
 *
 *  Created on: 17-07-2019
 *      Author: Krzysztof Jakubczyk
 */

#ifndef C37_94_H_
#define C37_94_H_

#include "tdefs.h"
#include "helper.h"
#include "mkstream.h"
#include "eth.h"

#define C37_MAX_STREAMS			3
#define C37_STREAM_BUF_SIZE		16//8
#define C37_NUM_OF_CHANS		6
#define C37_FRAME_SIZE			sizeof(struct stream_frame)

#define C37_NO_DATA_SHIFT_INTERVAL	100

#define C37_SERIAL_MASK			0x3c

extern volatile u8 c37_str_bits[C37_MAX_STREAMS][C37_STREAM_BUF_SIZE];
//args->params.bits definitions

#define EXT_CLK			0x00000001
#define NO_BUF			0x00000002
#define MUX2_ACTIVE		0x00000004
#define MUX2_EXT_CLK	0x00000008

struct c37_94_logic
{
	struct binary_io stream_en;
	struct binary_io no_sync;
	struct binary_io clr;
	struct binary_io ping;
	struct stream_frame *stream_in;
	struct stream_frame stream_out[C37_MAX_STREAMS];
	struct binary_io stream_act[C37_MAX_STREAMS];
	struct binary_io link[C37_MAX_STREAMS];
	float *stream_errs[C37_MAX_STREAMS];
	float *stream_synt_errs[C37_MAX_STREAMS];
	struct binary_io err;
	struct binary_io y_bit;
	struct binary_io err2;
	struct binary_io y_bit2;
	float *cur_shift;
	float *cur_shift2;
	float *ping_time;
	u16 s_delay;
	u16 wr_bits;
	u16 wr_bits2;
	u16 prev_addr;
	u16 prev_addr2;
	u16 wr_addr;
	u16 wr_addr2;
	u8 shift_cnt;
	u8 shift_cnt2;
	u8 out_shift_cnt;
	u16 no_data_cnt;
	u16 no_data_cnt2;
	u32 prev_pos[C37_MAX_STREAMS];
	u32 link_timeout_cnt[C37_MAX_STREAMS];
	u32 link_up_cnt[C37_MAX_STREAMS];
	u16 prev_timestamp;
	u16 ping_cnt;
	u16 ping_sent;
	u8 my_mask;
};

struct c37_94_io
{
	u32 stream_en_in;
	u32 stream_ptr_in;
	u32 no_sync_in;
	u32 clr_in;
	u32 ping_in;

	u32 stream0_ptr_out;
	u32 stream0_act_out;
	u32 link0_out;
	u32 stream0_errs_float_out;

	u32 stream1_ptr_out;
	u32 stream1_act_out;
	u32 link1_out;
	u32 stream1_errs_float_out;

	u32 stream2_ptr_out;
	u32 stream2_act_out;
	u32 link2_out;
	u32 stream2_errs_float_out;
	u32 err_out;
	u32 y_bit_out;
	u32 cur_shift_float_out;
	u32 err2_out;
	u32 y2_bit_out;
	u32 cur_shift2_float_out;
	u32 stream0_synt_errs_float_out;
	u32 stream1_synt_errs_float_out;
	u32 stream2_synt_errs_float_out;
	u32 ping_float_out;

}__attribute__((__packed__));

struct c37_94_params
{
	u32 bits;
	u32 mux_chans;
	u32 sample_delay;
	u32 serial_num;
}__attribute__((__packed__));

struct c37_94_args
{
	struct c37_94_io io;
	struct c37_94_params params;
//	u16 crc;
}__attribute__((__packed__));

extern void c37_94(void *args, void *logic);
extern int c37_94_initlog(void *arguments, void *logic);

#endif
