DAC5578.H
 1  #ifndef __DAC5578_H
 2  #define __DAC5578_H
 3  
 4  #ifdef __cplusplus
 5  extern "C" {
 6  #endif
 7  
 8  #include "stm32f7xx_hal.h"
 9  #include <stdbool.h>
10  
11  
12  
13  extern I2C_HandleTypeDef hi2c1;
14  
15  /* Command definitions - DAC5578 specific */
16  #define DAC5578_CMD_WRITE              (0x0 << 4)  // Write to input register
17  #define DAC5578_CMD_UPDATE             (0x1 << 4)  // Update DAC register
18  #define DAC5578_CMD_WRITE_UPDATE       (0x2 << 4)  // Write and update
19  #define DAC5578_CMD_WRITE_ALL          (0x3 << 4)  // Write to all channels
20  #define DAC5578_CMD_POWERDOWN          (0x4 << 4)  // Power down
21  #define DAC5578_CMD_POWERDOWN_ALL      (0x5 << 4)  // Power down all channels
22  #define DAC5578_CMD_INT_REF_ENABLE     (0x6 << 4)  // Enable internal reference
23  #define DAC5578_CMD_INT_REF_DISABLE    (0x7 << 4)  // Disable internal reference
24  #define DAC5578_CMD_RESET              (0x8 << 4)  // Software reset
25  #define DAC5578_CMD_LDAC_SETUP         (0x9 << 4)  // LDAC setup register
26  #define DAC5578_CMD_SOFTWARE_LDAC      (0xA << 4)  // Software LDAC trigger
27  
28  /* Broadcast channel for simultaneous updates */
29  #define DAC5578_CHANNEL_BROADCAST      0x8
30  
31  /* Power down modes */
32  typedef enum {
33      DAC5578_PWD_NORMAL = 0,     // Normal operation
34      DAC5578_PWD_1K = 1,         // 1kΩ to GND
35      DAC5578_PWD_100K = 2,       // 100kΩ to GND
36      DAC5578_PWD_HIZ = 3         // High impedance
37  } DAC5578_PowerDownMode_t;
38  
39  /* Clear code options - determines what happens when CLR pin is activated */
40  typedef enum {
41      DAC5578_CLR_CODE_ZERO = 0,      // Clear to zero scale (0x00)
42      DAC5578_CLR_CODE_MID = 1,       // Clear to midscale (0x80)
43      DAC5578_CLR_CODE_FULL = 2,      // Clear to full scale (0xFF)
44      DAC5578_CLR_CODE_NOP = 3        // No operation (retain current value)
45  } DAC5578_ClearCode_t;
46  
47  /* DAC handle structure */
48  typedef struct {
49      I2C_HandleTypeDef *hi2c;
50      uint8_t i2c_addr;
51      uint8_t resolution_bits;
52      GPIO_TypeDef *ldac_port;
53      uint16_t ldac_pin;
54      GPIO_TypeDef *clr_port;
55      uint16_t clr_pin;
56      DAC5578_ClearCode_t clear_code; // Current clear code setting
57  } DAC5578_HandleTypeDef;
58  
59  /* Function prototypes */
60  bool DAC5578_Init(DAC5578_HandleTypeDef *hdac, I2C_HandleTypeDef *hi2c, uint8_t i2c_addr, 
61                    uint8_t resolution, GPIO_TypeDef *ldac_port, uint16_t ldac_pin,
62                    GPIO_TypeDef *clr_port, uint16_t clr_pin);
63  bool DAC5578_Reset(DAC5578_HandleTypeDef *hdac);
64  bool DAC5578_WriteChannelValue(DAC5578_HandleTypeDef *hdac, uint8_t channel, uint16_t value);
65  bool DAC5578_UpdateChannel(DAC5578_HandleTypeDef *hdac, uint8_t channel);
66  bool DAC5578_WriteAndUpdateChannelValue(DAC5578_HandleTypeDef *hdac, uint8_t channel, uint16_t value);
67  bool DAC5578_WriteAllChannels(DAC5578_HandleTypeDef *hdac, uint16_t value);
68  bool DAC5578_ReadInputChannelValue(DAC5578_HandleTypeDef *hdac, uint8_t channel, uint16_t *value);
69  bool DAC5578_ReadDACChannelValue(DAC5578_HandleTypeDef *hdac, uint8_t channel, uint16_t *value);
70  bool DAC5578_SetPowerDownMode(DAC5578_HandleTypeDef *hdac, uint8_t channel, DAC5578_PowerDownMode_t mode);
71  bool DAC5578_SetPowerDownAll(DAC5578_HandleTypeDef *hdac, DAC5578_PowerDownMode_t mode);
72  bool DAC5578_SetInternalReference(DAC5578_HandleTypeDef *hdac, bool enable);
73  bool DAC5578_SetupLDAC(DAC5578_HandleTypeDef *hdac, uint8_t ldac_mask);
74  bool DAC5578_SoftwareLDAC(DAC5578_HandleTypeDef *hdac);
75  
76  /* CLR Pin Functions */
77  bool DAC5578_SetClearCode(DAC5578_HandleTypeDef *hdac, DAC5578_ClearCode_t clear_code);
78  DAC5578_ClearCode_t DAC5578_GetClearCode(DAC5578_HandleTypeDef *hdac);
79  void DAC5578_ActivateClearPin(DAC5578_HandleTypeDef *hdac);
80  void DAC5578_DeactivateClearPin(DAC5578_HandleTypeDef *hdac);
81  void DAC5578_ClearOutputs(DAC5578_HandleTypeDef *hdac); // Pulse CLR pin to clear outputs
82  
83  /* Private functions */
84  bool DAC5578_CommandWrite(DAC5578_HandleTypeDef *hdac, uint8_t command, uint16_t value);
85  bool DAC5578_CommandRead(DAC5578_HandleTypeDef *hdac, uint8_t command, uint16_t *value);
86  
87  #ifdef __cplusplus
88  }
89  #endif
90  
91  #endif /* __DAC5578_H */