/*
 *  Created on: 27. 1. 2020
 *      Author: Ondrej Vitoslavsky
*/

#ifndef DAC8563_DRIVER_H_
#define DAC8563_DRIVER_H_


 //DA SPI Interface pins
 #define DA_SPI                           SPI2

 #define DA_SPI_SCK_GPIO_PORT             GPIOB
 #define DA_SPI_SCK_PIN                   GPIO_PIN_13
 #define DA_SPI_SCK_AF					  GPIO_AF5_SPI2

 #define DA_SPI_MOSI_GPIO_PORT            GPIOB
 #define DA_SPI_MOSI_PIN                  GPIO_PIN_15
 #define DA_SPI_MOSI_AF					  GPIO_AF5_SPI2

 #define DA_SYNC_0_GPIO_PORT              GPIOB
 #define DA_SYNC_0_PIN                    GPIO_PIN_8

 #define DA_SYNC_1_GPIO_PORT              GPIOB
 #define DA_SYNC_1_PIN                    GPIO_PIN_14

 #define DA_LDAC_GPIO_Port                GPIOD
 #define DA_LDAC_Pin                      GPIO_PIN_9



typedef enum
{
	DA_WRITE = 0x00,
	DA_UPDATE = 0x01,
	DA_WRITE_AND_UPDATE_ALL = 0x02,
	DA_WRITE_AND_UPDATE = 0x03,
	DA_GAIN = 0x00,
	DA_POWER = 0x04,
	DA_RESET = 0x05,
	DA_LDAC = 0x06,
	DA_REFERENCE = 0x07,
} DacCommand;

typedef enum
{
	DA_A = 0x00,
	DA_B = 0x01,
	DA_A_B = 0x07,
	GAIN = 0x02,
} DacAddress;

 //DA Data
 #define GAIN_B2_A2               0b00
 #define GAIN_B2_A1               0b01
 #define GAIN_B1_A2               0b10
 #define GAIN_B1_A1               0b11

 #define POWER_UP_A               0b000001
 #define POWER_UP_B               0b000010
 #define POWER_UP_A_B 	          0b000011

 #define POWER_DOWN_A_1K          0b001001
 #define POWER_DOWN_B_1K          0b001010
 #define POWER_DOWN_A_B_1K        0b001011

 #define POWER_DOWN_A_100K        0b010001
 #define POWER_DOWN_B_100K        0b010010
 #define POWER_DOWN_A_B_100K      0b010011

 #define POWER_DOWN_A_HiZ         0b011001
 #define POWER_DOWN_B_HiZ         0b011010
 #define POWER_DOWN_A_B_HiZ       0b011011

 #define RESET1                   0b0
 #define RESET2                   0b1

 #define LDAC_ACTIV_A_B           0b00
 #define LDAC_ACTIV_B             0b01
 #define LDAC_ACTIV_A             0b01
 #define LDAC_INACTIV             0b11

 #define INTERNAL_REF_ON          0b01
 #define INTERNAL_REF_OFF         0b00

 #define DA_N_20mA 				(float)0x17C0
 #define DA_0mA 				(float)0x8006
 #define DA_P_4mA 				(float)0x94DD
 #define DA_P_20mA 				(float)0xe840

/**
  * @brief  Initialization of DAC.
  * @note   Initializes GPIO and SPI used for communication with DAC.
  * @param  None.
  * @retval None.
  */
void DA_Init();

/**
  * @brief  Low Level Initialization.
  * @note   Sets SPI Low level configuration (clock, pins and alternate function to AF 5).
  * 		 Is automatically called in DA_Init().
  * @param  None.
  * @retval None.
  */
void DA_LowLevel_Init(void);

/**
  * @brief  SPI Initialization.
  * @note   Sets SPI configuration and enables SPI.
  *    	    Is automatically called in DA_Init().
  * @param  None.
  * @retval None.
  */
void DA_SPI_Init(void);

/**
  * @brief  Writes data.
  * @note   Writes data to DAC register.
  * @param  Command DAC register command.
  * @param  Address address of DAC output register .
  * @param  Data Data to be written to output register.
  * @retval none.
  */
void DA_Write(DacCommand Command, DacAddress Address, uint16_t Data);

/**
  * @brief 	Writes data to Output.
  * @note   Writes 16 bit data to Output and updates both outputs
  * @param  Data 16 bit data to be written.
  * @param  Address address of DAC output register.
  * @retval Data written to output register calculated from hex code to -24÷24 mA.
  */
float DA_Data_Output(uint16_t Data, DacAddress Address);

/**
  * @brief 	Starts communication.
  * @note   Starts communication with ADC by pulling Chip Select down.
  * @param  None.
  * @retval None.
  */
void DA_Com_Start(void);

/**
  * @brief 	Stops communication.
  * @note   Stops communication with ADC by pulling Chip Select up.
  * @param  None.
  * @retval None.
  */
void DA_Com_Stop(void);

/**
  * @brief  Writes data.
  * @note   Writes data to DAC by using SPI.
  * @param  Data data to be send.
  * @retval none.
  */
void DA_Send_24bit(uint32_t Data);

/**
  * @brief 	Calculate output data.
  * @note   Calculate output data depending on Analog Output type:
  * 		-Deviation_Bipolar_20mA.
  * 		-Deviation_Unipolar_0_20mA.
  * 		-Deviation_Unipolar_4_20mA.
  * 		-Nominal_Unipolar_0_20mA.
  * 		-Nominal_Unipolar_4_20mA.
  * @param  Data Calculated thickness.
  * @param  Range Range of thickness from nominal thickness.
  * @param  Nominal nominal thickness.
  * @param  Type type of analog output.
  * @retval hex code to be written to Output register of ADC.
  */
uint16_t DA_calculate(float Data, float Range, float Nominal, AO_Type Type);




#endif /* DAC8563_DRIVER_H_ */
