/*
 * udp1553_types.h
 *
 *  Created on: 05/ott/2021
 *      Author: chessaa
 */

#ifndef UDP1553_TYPES_H_
#define UDP1553_TYPES_H_

#include <stdint.h>

struct udp_1553_header_t
{
	enum {
                k_marker_1553 = 0x1553U,
                k_marker_end_1553 = 0x5315U,
                k_otype_bc= 0x4342U, //="BC"
                k_otype_rt= 0x5452U,	//="RT"
                k_ctrl_marker_begin=0x3C3C,
                k_ctrl_marker_end=0x3E3E,
        };

	enum {
		k_err_unsupported_version=1,
		k_err_invalid_source,
		k_err_invalid_len,
		k_err_missed_msg_marker
	};

    uint16_t marker1553;	//must be =k_marker_1553
    uint8_t vmajor;
    uint8_t vminor;

    uint16_t otype;			//must be k_otype_*
    uint8_t ta;			//1553 terminal address, BC=0, RT=1..30
    uint8_t flags;

    uint32_t fcounter;	//frame counter
    uint32_t mcounter;	//master counter
    uint32_t scounter;	//slave counter
    uint32_t mmiss;
    uint32_t smiss;

    uint32_t ltt;		//Originator local timetag, usec

    uint32_t errors;

    uint32_t bc_reserved[2];
    uint32_t rt_reserved[2];

    uint32_t spare[3];
};

struct udp_1553_msg_t
{
	enum errcode_t
	{
		e_ok=0,
		e_msg_err,
		e_incomplete
	};
    uint16_t marker;
    uint16_t cw;
    uint16_t sw;
    uint16_t errcode;
};

struct udp_1553_cw_structure_t
{
  unsigned int wc: 5;
  unsigned int sa: 5;
  unsigned int tr: 1;
  unsigned int rt: 5;
};

typedef uint16_t udp_1553_word_t;

union udp_1553_cw_t
{
  udp_1553_word_t raw;
  udp_1553_cw_structure_t str;
};

struct udp_1553_sw_structure_t
{
    unsigned int tf: 1;
    unsigned int bca: 1;
    unsigned int sf: 1;
    unsigned int busy: 1;
    unsigned int brcs: 1;
    unsigned int reserved: 3;
    unsigned int sr: 1;
    unsigned int instr: 1;
    unsigned int me: 1;
    unsigned int rt: 5;
};

union udp_1553_sw_t
{
  udp_1553_word_t raw;
  udp_1553_sw_structure_t str;
};




#endif /* UDP1553_TYPES_H_ */
