#pragma once

#include "spi_cs_if.h"

#define AD_DEFAULT_FREQ 1000U  ///< Default initialisation frequency (Hz)
#define AD_DEFAULT_PHASE 0     ///< Default initialisation phase angle (degrees)
#define AD_MCLK 25000000U      ///< Clock speed of the AD9833 reference clock in Hz
#define AD_MCLK_DIV2 12500000U ///< Clock speed of the AD9833 reference clock in Hz
/**
 * Channel enumerated type.
 *
 * This enumerated type is used with the to specify which channel
 * is being invoked on operations that could be channel related.
 */
typedef enum
{
    CHAN_0 = 0, ///< Channel 0 definition
    CHAN_1 = 1, ///< Channel 1 definition
} AD_channel_t;

/**
 * Output mode request enumerated type.
 *
 * This enumerated type is used with the \ref setMode() methods to identify
 * the mode request.
 */
typedef enum
{
    MODE_OFF,      ///< Set output all off
    MODE_SINE,     ///< Set output to a sine wave at selected frequency
    MODE_SQUARE1,  ///< Set output to a square wave at selected frequency
    MODE_SQUARE2,  ///< Set output to a square wave at half selected frequency
    MODE_TRIANGLE, ///< Set output to a triangle wave at selected frequency
} AD_mode_t;

typedef struct
{
    uint16_t _regCtl;      // control register image
    uint32_t _regFreq[2];  // frequency registers
    uint32_t _regPhase[2]; // phase registers

    AD_mode_t _mode; // last set mode

    SPI_HandleTypeDef *hspi;
    cs_handle_t *hcs;
} ad9833_handle_t;

void ad9833_init(ad9833_handle_t *hfg, SPI_HandleTypeDef *hspi, cs_handle_t *hcs);
void ad9833_spi_activate(ad9833_handle_t *hfg);
void ad9833_spi_deactivate(ad9833_handle_t *hfg);
void ad9833_reset(ad9833_handle_t *hfg, uint8_t hold);
void ad9833_setActiveChannelFreq(ad9833_handle_t *hfg, AD_channel_t chan);
AD_channel_t ad9833_getActiveChannelFreq(ad9833_handle_t *hfg);
void ad9833_setActiveChannelPhase(ad9833_handle_t *hfg, AD_channel_t chan);
AD_channel_t ad9833_getActiveChannelPhase(ad9833_handle_t *hfg);
void ad9833_setMode(ad9833_handle_t *hfg, AD_mode_t mode);
void ad9833_setFrequency(ad9833_handle_t *hfg, AD_channel_t chan, uint32_t freq);
void ad9833_setPhase(ad9833_handle_t *hfg, AD_channel_t chan, uint16_t phase);