-
Notifications
You must be signed in to change notification settings - Fork 1
/
mcp48x2.h
145 lines (127 loc) · 3.59 KB
/
mcp48x2.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#ifndef MCP_48X2_H
#define MCP_48X2_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
/* constants for channel */
typedef enum
{
MCP48X2_DAC_CH_A = 0,
MCP48X2_DAC_CH_B = 1,
} mcp48x2_ch_t;
/* constants for channel mode */
typedef enum
{
MCP48X2_CH_SHDN = 0,
MCP48X2_CH_ACTIVE = 1,
} mcp48x2_ch_mode_t;
/* constants for gain factor for output voltage */
typedef enum
{
MCP48X2_GAIN_2X = 0,
MCP48X2_GAIN_1X = 1,
} mcp48x2_gain_t;
/* Possible return values */
typedef enum
{
MCP48X2_OK = 0,
MCP48X2_FAIL = 1,
} mcp48x2_ret_t;
/* Low level functions to ensure SPI, NSS and LDAC managing */
typedef struct
{
/* Manage NSS pin */
void (*nss_set)(void);
void (*nss_reset)(void);
/* Manage LDAC pin */
void (*ldac_set)(void);
void (*ldac_reset)(void);
void (*delay_us)(uint32_t us);
/* Transmit 16 bits with SPI */
mcp48x2_ret_t (*spi_write_packet)(uint16_t data);
} mcp48x2_ll_t;
/* One instance of DAC */
typedef struct
{
mcp48x2_ch_mode_t mode_ch_a;
mcp48x2_ch_mode_t mode_ch_b;
mcp48x2_gain_t gain_ch_a;
mcp48x2_gain_t gain_ch_b;
uint16_t ch_a_val;
uint16_t ch_b_val;
mcp48x2_ll_t *ll;
} mcp48x2_device_t;
/**
* Default setup:
* both channels are in active mode, gain factor equals 2
* and initial output voltages are set to zero.
*
* Also the function fill in mcp48x2_device_t structure with
* corresponding values.
*
* Function returns 'MCP48X2_OK' in case of success.
*/
mcp48x2_ret_t mcp48x2_default_init(mcp48x2_device_t *dev, mcp48x2_ll_t *ll);
/**
* Init particular channel. Call this function for each channel.
* If you use this function for channels, don't use the 'mcp48x2_default_init()'.
*
* Function returns 'MCP48X2_OK' in case of success.
*/
mcp48x2_ret_t mcp48x2_init_channel(mcp48x2_device_t *dev, mcp48x2_ll_t *ll,
mcp48x2_ch_t ch, mcp48x2_ch_mode_t mode,
mcp48x2_gain_t gain);
/**
* Set channel mode of work to active or shutdown.
*
* Function returns 'MCP48X2_OK' in case of success.
*/
mcp48x2_ret_t mcp48x2_set_channel_mode(mcp48x2_device_t *dev,
mcp48x2_ch_t ch,
mcp48x2_ch_mode_t mode);
/**
* Set channel gain factor to 1x or 2x.
*
* Function returns 'MCP48X2_OK' in case of success.
*/
mcp48x2_ret_t mcp48x2_set_channel_gain(mcp48x2_device_t *dev,
mcp48x2_ch_t ch,
mcp48x2_gain_t gain);
/**
* Write value to output register for one channel and toggle the LDAC pin.
* Other channel is stayed unchangeable
*
* Formula: Vout = (2048 * val * G) / 2^(resolution)
*
* 2048 - internal reference voltage
* G - gain factor
* val = [0 - 4095] for MCP4922
* val = [0 - 1023] for MCP4912
* val = [0 - 255] for MCP4902
*
* Function returns 'MCP48X2_OK' in case of success.
*/
mcp48x2_ret_t mcp48x2_set_channel_value(mcp48x2_device_t *dev,
mcp48x2_ch_t ch, uint16_t val);
/**
* Write values to output registers for both channels
* and toggle the LDAC pin to synchronize setting the values.
*
* Formula: Vout = (2048 * val * G) / 2^(resolution)
*
* 2048 - internal reference voltage
* G - gain factor
* val = [0 - 4095] for MCP4922
* val = [0 - 1023] for MCP4912
* val = [0 - 255] for MCP4902
*
* Function returns 'MCP48X2_OK' in case of success.
*/
mcp48x2_ret_t mcp48x2_set_channel_values_sync(mcp48x2_device_t *dev,
uint16_t val_ch_a,
uint16_t val_ch_b);
#ifdef __cplusplus
}
#endif
#endif // MCP_48X2_H