-
Notifications
You must be signed in to change notification settings - Fork 1
/
mcp48x2.c
290 lines (237 loc) · 6.1 KB
/
mcp48x2.c
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include "mcp48x2.h"
#include <stdint.h>
#include <stddef.h>
#define REF_VOLTAGE_MV 2048
#if defined(DAC_TYPE_MCP4822)
#define MAX_OUT_VALUE (4095)
#define SHIFT_BITS_CNT (0)
#elif defined(DAC_TYPE_MCP4812)
#define MAX_OUT_VALUE (1023)
#define SHIFT_BITS_CNT (2)
#elif defined(DAC_TYPE_MCP4802)
#define MAX_OUT_VALUE (255)
#define SHIFT_BITS_CNT (4)
#else
#error "Please, define DAC type"
#endif
#define CHANNEL_BIT_POS 15
#define GAIN_BIT_POS 13
#define SHDN_BIT_POS 12
static mcp48x2_ret_t write_packet(mcp48x2_ll_t *ll, uint16_t data)
{
mcp48x2_ret_t ret = MCP48X2_FAIL;
ll->nss_reset();
ret = ll->spi_write_packet(data);
ll->nss_set();
if (ret != MCP48X2_OK)
{
return MCP48X2_FAIL;
}
return MCP48X2_OK;
}
static void toggle_ldac(mcp48x2_ll_t *ll)
{
ll->ldac_reset();
ll->delay_us(100);
ll->ldac_set();
}
/* Returns 0 in good case */
static int check_args_for_nulls(mcp48x2_device_t *dev, mcp48x2_ll_t *ll)
{
if (!dev || !ll || !ll->delay_us ||
!ll->ldac_reset || !ll->ldac_set ||
!ll->nss_set || !ll->nss_reset ||
!ll->spi_write_packet)
{
return 1;
}
return 0;
}
mcp48x2_ret_t mcp48x2_default_init(mcp48x2_device_t *dev, mcp48x2_ll_t *ll)
{
uint16_t data = 0;
if (check_args_for_nulls(dev, ll))
{
return MCP48X2_FAIL;
}
/* channel A */
data |= ((uint16_t)MCP48X2_CH_ACTIVE << SHDN_BIT_POS);
if (write_packet(ll, data) == MCP48X2_FAIL)
{
return MCP48X2_FAIL;
}
/* channel B */
data |= ((uint16_t)MCP48X2_DAC_CH_B << CHANNEL_BIT_POS) | \
((uint16_t)MCP48X2_CH_ACTIVE << SHDN_BIT_POS);
if (write_packet(ll, data) == MCP48X2_FAIL)
{
return MCP48X2_FAIL;
}
toggle_ldac(ll);
dev->ch_a_val = 0;
dev->ch_b_val = 0;
dev->ll = ll;
dev->mode_ch_a = MCP48X2_CH_ACTIVE;
dev->mode_ch_b = MCP48X2_CH_ACTIVE;
dev->gain_ch_a = MCP48X2_GAIN_2X;
dev->gain_ch_b = MCP48X2_GAIN_2X;
return MCP48X2_OK;
}
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)
{
uint16_t data = 0;
if (check_args_for_nulls(dev, ll))
{
return MCP48X2_FAIL;
}
data |= (((uint16_t)ch << CHANNEL_BIT_POS) | \
((uint16_t)gain << GAIN_BIT_POS) | \
((uint16_t)mode << SHDN_BIT_POS));
if (write_packet(ll, data) == MCP48X2_FAIL)
{
return MCP48X2_FAIL;
}
toggle_ldac(ll);
dev->ll = ll;
if (ch == MCP48X2_DAC_CH_A)
{
dev->ch_a_val = 0;
dev->mode_ch_a = mode;
dev->gain_ch_a = gain;
}
else if (ch == MCP48X2_DAC_CH_B)
{
dev->ch_b_val = 0;
dev->mode_ch_b = mode;
dev->gain_ch_b = gain;
}
return MCP48X2_OK;
}
mcp48x2_ret_t mcp48x2_set_channel_value(mcp48x2_device_t *dev,
mcp48x2_ch_t ch, uint16_t val)
{
uint16_t data = val;
mcp48x2_gain_t gain = 0;
mcp48x2_ch_mode_t mode = 0;
if (!dev || val > MAX_OUT_VALUE)
{
return MCP48X2_FAIL;
}
gain = (ch == MCP48X2_DAC_CH_A) ? (dev->gain_ch_a) : (dev->gain_ch_b);
mode = (ch == MCP48X2_DAC_CH_A) ? (dev->mode_ch_a) : (dev->mode_ch_b);
data <<= SHIFT_BITS_CNT;
data |= (((uint16_t)ch << CHANNEL_BIT_POS) | \
((uint16_t)gain << GAIN_BIT_POS) | \
((uint16_t)mode << SHDN_BIT_POS));
if (write_packet(dev->ll, data) == MCP48X2_FAIL)
{
return MCP48X2_FAIL;
}
toggle_ldac(dev->ll);
if (ch == MCP48X2_DAC_CH_A)
{
dev->ch_a_val = val;
}
else if (ch == MCP48X2_DAC_CH_B)
{
dev->ch_b_val = val;
}
return MCP48X2_OK;
}
mcp48x2_ret_t mcp48x2_set_channel_mode(mcp48x2_device_t *dev,
mcp48x2_ch_t ch,
mcp48x2_ch_mode_t mode)
{
if (!dev)
{
return MCP48X2_FAIL;
}
uint16_t data = (ch == MCP48X2_DAC_CH_A) ? (dev->ch_a_val) : (dev->ch_b_val);
mcp48x2_gain_t gain = (ch == MCP48X2_DAC_CH_A) ? (dev->gain_ch_a) : (dev->gain_ch_b);
data <<= SHIFT_BITS_CNT;
data |= (((uint16_t)ch << CHANNEL_BIT_POS) | \
((uint16_t)gain << GAIN_BIT_POS) | \
((uint16_t)mode << SHDN_BIT_POS));
if (write_packet(dev->ll, data) == MCP48X2_FAIL)
{
return MCP48X2_FAIL;
}
toggle_ldac(dev->ll);
if (ch == MCP48X2_DAC_CH_A)
{
dev->mode_ch_a = mode;
}
else if (ch == MCP48X2_DAC_CH_B)
{
dev->mode_ch_b = mode;
}
return MCP48X2_OK;
}
mcp48x2_ret_t mcp48x2_set_channel_gain(mcp48x2_device_t *dev,
mcp48x2_ch_t ch,
mcp48x2_gain_t gain)
{
if (!dev)
{
return MCP48X2_FAIL;
}
uint16_t data = (ch == MCP48X2_DAC_CH_A) ? (dev->ch_a_val) : (dev->ch_b_val);
mcp48x2_ch_mode_t mode = (ch == MCP48X2_DAC_CH_A) ? (dev->mode_ch_a) : (dev->mode_ch_b);
data <<= SHIFT_BITS_CNT;
data |= (((uint16_t)ch << CHANNEL_BIT_POS) | \
((uint16_t)gain << GAIN_BIT_POS) | \
((uint16_t)mode << SHDN_BIT_POS));
if (write_packet(dev->ll, data) == MCP48X2_FAIL)
{
return MCP48X2_FAIL;
}
toggle_ldac(dev->ll);
if (ch == MCP48X2_DAC_CH_A)
{
dev->gain_ch_a = gain;
}
else if (ch == MCP48X2_DAC_CH_B)
{
dev->gain_ch_b = gain;
}
return MCP48X2_OK;
}
mcp48x2_ret_t mcp48x2_set_channel_values_sync(mcp48x2_device_t *dev,
uint16_t val_ch_a,
uint16_t val_ch_b)
{
if (!dev || val_ch_a > MAX_OUT_VALUE || val_ch_b > MAX_OUT_VALUE)
{
return MCP48X2_FAIL;
}
uint16_t data_a = val_ch_a;
uint16_t data_b = val_ch_b;
mcp48x2_gain_t gain_a = dev->gain_ch_a;
mcp48x2_gain_t gain_b = dev->gain_ch_b;
mcp48x2_ch_mode_t mode_a = dev->mode_ch_a;
mcp48x2_ch_mode_t mode_b = dev->mode_ch_b;
/* channel A */
data_a <<= SHIFT_BITS_CNT;
data_a |= (((uint16_t)MCP48X2_DAC_CH_A << CHANNEL_BIT_POS) | \
((uint16_t)gain_a << GAIN_BIT_POS) | \
((uint16_t)mode_a << SHDN_BIT_POS));
if (write_packet(dev->ll, data_a) == MCP48X2_FAIL)
{
return MCP48X2_FAIL;
}
/* channel B */
data_b <<= SHIFT_BITS_CNT;
data_b |= (((uint16_t)MCP48X2_DAC_CH_B << CHANNEL_BIT_POS) | \
((uint16_t)gain_b << GAIN_BIT_POS) | \
((uint16_t)mode_b << SHDN_BIT_POS));
if (write_packet(dev->ll, data_b) == MCP48X2_FAIL)
{
return MCP48X2_FAIL;
}
toggle_ldac(dev->ll);
dev->ch_a_val = val_ch_a;
dev->ch_b_val = val_ch_b;
return MCP48X2_OK;
}