
#ifndef ETHERCAT_SUGAR_DESIGNATED_INIT_H
#define ETHERCAT_SUGAR_DESIGNATED_INIT_H

#include "../ethercat_types.h"
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct {
    uint32_t cycle_ns;
    uint32_t cpu;
    uint8_t  priority;
    uint8_t  use_dc;
    uint8_t  use_wdk;
    uint8_t  _pad;
} ec_cycle_preset_t;

#define EC_PRESET_CYCLE_4MS                                              \
    { .cycle_ns = 4000000, .cpu = 0, .priority = 10, .use_dc = 0,        \
      .use_wdk = 0, ._pad = 0 }

#define EC_PRESET_CYCLE_1MS                                              \
    { .cycle_ns = 1000000, .cpu = 3, .priority = 50, .use_dc = 1,        \
      .use_wdk = 1, ._pad = 0 }

#define EC_PRESET_CYCLE_500US                                            \
    { .cycle_ns = 500000,  .cpu = 5, .priority = 80, .use_dc = 1,        \
      .use_wdk = 1, ._pad = 0 }

#define EC_PRESET_CYCLE_250US                                            \
    { .cycle_ns = 250000,  .cpu = 7, .priority = 99, .use_dc = 1,        \
      .use_wdk = 1, ._pad = 0 }

#define EC_PRESET_CYCLE_4MS_AS_LITERAL                                   \
    ((ec_cycle_preset_t)EC_PRESET_CYCLE_4MS)

#define EC_PRESET_CYCLE_1MS_AS_LITERAL                                   \
    ((ec_cycle_preset_t)EC_PRESET_CYCLE_1MS)

#define EC_PRESET_CYCLE_500US_AS_LITERAL                                 \
    ((ec_cycle_preset_t)EC_PRESET_CYCLE_500US)

#define EC_PRESET_CYCLE_250US_AS_LITERAL                                 \
    ((ec_cycle_preset_t)EC_PRESET_CYCLE_250US)

#define EC_CYCLE(...)  ((ec_cycle_preset_t){ __VA_ARGS__ })

#define EC_SDO_DOWNLOAD_SHORT(idx, sub, val_u32, prio_)                  \
    {                                                                    \
        .index            = (idx),                                       \
        .sub_index        = (sub),                                       \
        .data             = { (uint8_t)((val_u32)         & 0xFF),       \
                              (uint8_t)(((val_u32) >> 8)  & 0xFF),       \
                              (uint8_t)(((val_u32) >> 16) & 0xFF),       \
                              (uint8_t)(((val_u32) >> 24) & 0xFF) },     \
        .data_len         = 4,                                           \
        .transition       = EC_TRANS_PS,                                 \
        .timing           = EC_TIMING_BEFORE,                            \
        .priority         = (prio_),                                     \
        .complete_access  = 0,                                           \
        .is_register_write= 0                                            \
    }

#ifdef __cplusplus
}
#endif

#endif
