/*
 * grifo_exp_if.h
 *
 *  Created on: 29/gen/2020
 *      Author: chessaa
 */

#ifndef GRIFO_EXP_IF_H_
#define GRIFO_EXP_IF_H_

#include <stdint.h>

namespace grifo_fw
{

struct expander_raw_if_t
{
    template<unsigned int S_, unsigned int E_> struct spare_t {uint32_t xxx[(E_-S_)/4];};

    enum
    {
        FRQ_BASE_MHZ = 1792,
        FRQ_LEN_MHZ = 224,

        fifo_len=8
    };

    enum mof_t {
        mof_off=0,
        mof_pulse=1,
        mof_cw=2,
        mof_ramp=3,
        mof_chirp=4
    };
/*
    enum reset_t {
        batch_start=256,
        wf_start=512,
        wf_end=1024,
        cw_no_stop=1792
    };
*/
    enum reset_t {
    	no_reset=0,
        batch_start=1,
        wf_start=2,
        wf_end=4,
        cw_no_stop=7 
    };



    uint32_t ctrl;
    spare_t<0x23, 0x40> spare_mof__;

    uint32_t mof_fifo[fifo_len];

    struct wf_info_t
    {
        uint32_t address[fifo_len];
        spare_t<0x9F, 0xC0> spare0__;
        uint32_t frq_start[fifo_len];
        spare_t<0xDF, 0x100> spare1__;
        uint32_t len[fifo_len];
        spare_t<0x11F, 0x140> spare2__;
        int32_t incr[fifo_len];
        spare_t<0x15F, 0x180> spare3__;
        uint32_t amp_peak[fifo_len];
    };

    spare_t<0x5F, 0x80> spare_wf1__;

    wf_info_t wf1;
    spare_t<0x19F, 0x1C0> spare_wf2__;
    wf_info_t wf2;
};


class expander_blob_t;

extern expander_raw_if_t* expander_blob_payload(const expander_blob_t* b=0);

extern void expander_initialize_raw_default_blob(void* raw_memory);

extern expander_blob_t* expander_get_default_blob();
extern expander_raw_if_t* expander_get_default_if(void);

extern void expander_mode_set(unsigned int different_prt_num);

extern void expander_test_cw_setup(unsigned int exp_reset, float feq_mhz, float len_us, float delta_hz=0);

extern void expander_test_cw_raw_setup(unsigned int feq, unsigned int len);

extern void expander_initialize_raw_hr_blob(void* raw_memory);
extern expander_blob_t* expander_get_hr_blob();
extern expander_raw_if_t* expander_get_hr_if(void);


struct expander_prf_settings_t
{
    expander_raw_if_t::mof_t mof;
    expander_raw_if_t::reset_t reset;
    uint32_t wf_address;
    uint32_t frq_start;
    uint32_t frq_len;
    int32_t frq_inc;
    uint32_t amp_peak;
};


extern void expander_prt_set(unsigned int prt_n, const expander_prf_settings_t& settings);

extern void expander_prt_set1(unsigned int prt_n, const expander_prf_settings_t& settings);
extern void expander_prt_set2(unsigned int prt_n, const expander_prf_settings_t& settings, const expander_prf_settings_t& settings2);

extern unsigned int expander_frequency_convert_mhz(float mhz);
extern unsigned int expander_frequency_convert_hz(float hz);

extern unsigned int expander_len_convert_usec(float us);

extern int expander_delta_convert_hz_224(float hz);
extern int expander_delta_convert_mhz_224(float mhz);

extern int expander_delta_convert_mhz(float mhz, float len_us);

} //namespace

#endif /* GRIFO_EXP_IF_H_ */
