Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/device_drivers/AD9833/ad9833.c

raw symbols SCIP
1 /**************************************************************************//**
2 * @file ad9833.c
3 * @brief Implementation of ad9833 Driver for Microblaze processor.
4 * @author Lucian Sin ([email protected])
5 *
6 *******************************************************************************
7 * Copyright 2013(c) Analog Devices, Inc.
8 *
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without modification,
12 * are permitted provided that the following conditions are met:
13 * - Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 * - Neither the name of Analog Devices, Inc. nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 * - The use of this software may or may not infringe the patent rights
23 * of one or more patent holders. This license does not release you
24 * from the requirement that you obtain separate licenses from these
25 * patent holders to use this software.
26 * - Use of the software either in source or binary form, must be run
27 * on or directly connected to an Analog Devices Inc. component.
28 *
29 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED
30 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY
31 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
34 * INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
36 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 ******************************************************************************/
41
42 /******************************************************************************/
43 /******************************* Include Files ********************************/
44 /******************************************************************************/
45 #include "ad9833.h"
46 #include "TIME.h"
47
48 /******************************************************************************/
49 /************************** Constants Definitions *****************************/
50 /******************************************************************************/
51 float PHASE_CONST = 651.8986469f;
52
53 /******************************************************************************/
54 /************************** Variables Definitions *****************************/
55 /******************************************************************************/
56 unsigned char txBuffer[4] = {0, 0, 0, 0}; // Tx SPI Buffer.
57 unsigned char rxBuffer[4] = {0, 0, 0, 0}; // Rx SPI Buffer.
58 unsigned short ctrlRegValue = 0;
59 unsigned short testOpbiten = 0;
60
61 struct ad9833_chip_info {
62 unsigned long mclk;
63 float freq_const;
64 };
65
66 static const struct ad9833_chip_info ad9833_chip_info[] = {
67 [ID_AD9833] = {
68 .mclk = 25000000,
69 .freq_const = 10.7374182f,
70 },
71 [ID_AD9834] = {
72 .mclk = 75000000,
73 .freq_const = 3.5791394f,
74 },
75 [ID_AD9837] = {
76 .mclk = 16000000,
77 .freq_const = 16.777216f,
78 },
79 [ID_AD9838] = {
80 .mclk = 16000000,
81 .freq_const = 16.777216f,
82 }
83 };
84
85 AD9833_type act_device;
86
87 unsigned char prog_method = 0;
88
89 /******************************************************************************/
90 /************************** Functions Definitions *****************************/
91 /******************************************************************************/
92
93 /***************************************************************************//**
94 * @brief Initialize the SPI communication with the device.
95 *
96 * @param None.
97 *
98 * @return status - Result of the initialization procedure.
99 * Example: 0 - if initialization was successful;
100 * -1 - if initialization was unsuccessful.
101 *******************************************************************************/
102 char AD9833_Init(AD9833_type device)
103 {
104 unsigned short spiData = 0;
105 char status = -1;
106
107 AD9834_PSEL_OUT;
108 AD9834_PSEL_LOW;
109 AD9834_FSEL_OUT;
110 AD9834_FSEL_LOW;
111 AD9834_RESET_OUT;
112 AD9834_RESET_LOW;
113 AD9834_SLEEP_OUT;
114 AD9834_SLEEP_LOW;
115
116 act_device = device;
117 /* Setup SPI interface. */
118 status = SPI_Init(0, 1000000, 1, 1);
119 /* Initialize board. */
120 spiData |= AD9833_CTRLRESET;
121 AD9833_TxSpi(spiData);
122 TIME_DelayMs(10);
123 spiData &= ~AD9833_CTRLRESET;
124 AD9833_TxSpi(spiData);
125
126 AD9833_SetFreq(0, 0);
127 AD9833_SetFreq(1, 0);
128 AD9833_SetPhase(0, 0);
129 AD9833_SetPhase(1, 0);
130
131 return status;
132 }
133
134 /**************************************************************************//**
135 * @brief Transmits 16 bits on SPI.
136 *
137 * @param value - Data which will be transmitted.
138 *
139 * @return none.
140 ******************************************************************************/
141 void AD9833_TxSpi(short value)
142 {
143 unsigned short spiData = 0;
144
145 txBuffer[0] = (unsigned char) ((value & 0x00ff00) >> 8); // data to be sent
146 txBuffer[1] = (unsigned char) (value & 0x0000ff); // in 8 bit packets
147 if (SPI_Write(AD9833_SLAVE_ID, txBuffer, 2) != 2)
148 {
149 /* Setup the SPI interface. */
150 SPI_Init(0, 1000000, 1, 1);
151 /* Initialize board. */
152 spiData |= AD9833_CTRLRESET;
153 AD9833_TxSpi(spiData);
154 TIME_DelayMs(10);
155 spiData &=~ AD9833_CTRLRESET;
156 AD9833_TxSpi(spiData);
157 }
158 }
159
160 /**************************************************************************//**
161 * @brief Selects the type of output.
162 *
163 * @param OutMode - type of output
164 * Example AD9833&AD9837: 0 - Sinusoid.
165 * 1 - Triangle.
166 * 2 - DAC Data MSB/2.
167 * 3 - DAC Data MSB.
168 * Example AD9834&AD9838: 0 - Sinusoid.
169 * 1 - Triangle.
170 *
171 * @return status - output type could / couldn't be selected.
172 * Example: 0 - output type possible for the given configuration.
173 * -1 - output type not possible for the given configuration.
174 ******************************************************************************/
175 char AD9833_OutMode(unsigned char OutMode)
176 {
177 unsigned short spiData = 0;
178 char status = 0;
179
180
181 if ((act_device == ID_AD9833) || (act_device == ID_AD9837))
182 {
183 spiData = (ctrlRegValue & ~(AD9833_CTRLMODE |
184 AD9833_CTRLOPBITEN |
185 AD9833_CTRLDIV2));
186 switch(OutMode)
187 {
188 case 1: // Triangle
189 spiData += AD9833_CTRLMODE;
190 break;
191 case 2: // DAC Data MSB/2
192 spiData += AD9833_CTRLOPBITEN;
193 break;
194 case 3: // DAC Data MSB
195 spiData += AD9833_CTRLOPBITEN + AD9833_CTRLDIV2;
196 break;
197 default: // Sinusoid
198 break;
199 }
200 AD9833_TxSpi(spiData);
201 ctrlRegValue = spiData;
202 }
203 else
204 {
205 if ((act_device == ID_AD9834) || (act_device == ID_AD9838))
206 {
207 spiData = (ctrlRegValue & ~AD9833_CTRLMODE);
208 testOpbiten = ctrlRegValue & AD9833_CTRLOPBITEN;
209
210 switch(OutMode)
211 {
212 case 1: // Triangle
213 if (testOpbiten == 0)
214 {
215 spiData += AD9833_CTRLMODE;
216 }
217 else
218 {
219 status = -1;
220 }
221 break;
222 default: // Sinusoid
223 break;
224 }
225 AD9833_TxSpi(spiData);
226 ctrlRegValue = spiData;
227 }
228 }
229
230 return status;
231 }
232
233 /**************************************************************************//**
234 * @brief Enable / Disable the sleep function.
235 *
236 * @param sleepMode - type of sleep
237 * Example soft method(all devices):
238 * 0 - No power-down.
239 * 1 - DAC powered down.
240 * 2 - Internal clock disabled.
241 * 3 - DAC powered down and Internal
242 * clock disabled.
243 * Example hard method(AD9834 & AD9838):
244 * 0 - No power-down.
245 * 1 - DAC powered down.
246 *
247 * @return None.
248 ******************************************************************************/
249 void AD9833_SleepMode(unsigned char sleepMode)
250 {
251 unsigned short spiData = 0;
252
253 if (prog_method == 0)
254 {
255 spiData = (ctrlRegValue & ~(AD9833_CTRLSLEEP12 | AD9833_CTRLSLEEP1));
256 switch(sleepMode)
257 {
258 case 1: // DAC powered down
259 spiData += AD9833_CTRLSLEEP12;
260 break;
261 case 2: // Internal clock disabled
262 spiData += AD9833_CTRLSLEEP1;
263 break;
264 case 3: // DAC powered down and Internal clock disabled
265 spiData += AD9833_CTRLSLEEP1 + AD9833_CTRLSLEEP12;
266 break;
267 default: // No power-down
268 break;
269 }
270 AD9833_TxSpi(spiData);
271 ctrlRegValue = spiData;
272 }
273 else
274 {
275 if (prog_method == 1)
276 {
277 switch(sleepMode)
278 {
279 case 0: // No power-down
280 AD9834_SLEEP_LOW;
281 break;
282 case 1: // DAC powered down
283 AD9834_SLEEP_HIGH;
284 break;
285 default: // Do nothing
286 break;
287 }
288 }
289 }
290
291 }
292
293 /**************************************************************************//**
294 * @brief Loads a frequency value in a register.
295 *
296 * @param registerNumber - Number of the register (0 / 1).
297 * @param frequencyValue - Frequency value.
298 *
299 * @return None.
300 ******************************************************************************/
301 void AD9833_SetFreq(unsigned char registerNumber, unsigned long frequencyValue)
302 {
303 unsigned long ulFreqRegister;
304 unsigned short iFreqLsb, iFreqMsb;
305
306 ulFreqRegister = (unsigned long)(frequencyValue *
307 ad9833_chip_info[act_device].freq_const);
308 iFreqLsb = (ulFreqRegister & 0x0003FFF);
309 iFreqMsb = ((ulFreqRegister & 0xFFFC000) >> 14);
310 ctrlRegValue |= AD9833_CTRLB28;
311 AD9833_TxSpi(ctrlRegValue);
312 if(registerNumber == 0)
313 {
314 AD9833_TxSpi(BIT_F0ADDRESS + iFreqLsb);
315 AD9833_TxSpi(BIT_F0ADDRESS + iFreqMsb);
316 }
317 else
318 {
319 AD9833_TxSpi(BIT_F1ADDRESS + iFreqLsb);
320 AD9833_TxSpi(BIT_F1ADDRESS + iFreqMsb);
321 }
322 }
323
324 /**************************************************************************//**
325 * @brief Loads a phase value in a register.
326 *
327 * @param registerNumber - Number of the register (0 / 1).
328 * @param phaseValue - Phase value.
329 *
330 * @return none
331 ******************************************************************************/
332 void AD9833_SetPhase(unsigned char registerNumber, float phaseValue)
333 {
334 unsigned short phaseCalc;
335
336 phaseCalc = (unsigned short)(phaseValue * PHASE_CONST);
337 if(registerNumber == 0)
338 {
339 AD9833_TxSpi(BIT_P0ADDRESS + phaseCalc);
340 }
341 else
342 {
343 AD9833_TxSpi(BIT_P1ADDRESS + phaseCalc);
344 }
345 }
346
347 /**************************************************************************//**
348 * @brief Select the frequency register to be used.
349 *
350 * @param freqReg - Number of frequency register. (0 / 1)
351 *
352 * @return None.
353 ******************************************************************************/
354 void AD9833_SelectFreqReg(unsigned char freqReg)
355 {
356 unsigned short spiData = 0;
357
358 if (prog_method == 0)
359 {
360 spiData = (ctrlRegValue & ~AD9833_CTRLFSEL);
361 // Select soft the working frequency register according to parameter
362 if(freqReg == 1)
363 {
364 spiData += AD9833_CTRLFSEL;
365 }
366 AD9833_TxSpi(spiData);
367 ctrlRegValue = spiData;
368 }
369 else
370 {
371 if (prog_method == 1)
372 {
373 // Select hard the working frequency register according to parameter
374 if (freqReg == 1)
375 {
376 AD9834_FSEL_HIGH;
377 }
378 else
379 {
380 if (freqReg == 0)
381 {
382 AD9834_FSEL_LOW;
383 }
384 }
385 }
386 }
387 }
388
389 /**************************************************************************//**
390 * @brief Select the phase register to be used.
391 *
392 * @param phaseReg - Number of phase register. (0 / 1)
393 *
394 * @return None.
395 ******************************************************************************/
396 void AD9833_SelectPhaseReg(unsigned char phaseReg)
397 {
398 unsigned short spiData = 0;
399
400 if (prog_method == 0)
401 {
402 spiData = (ctrlRegValue & ~AD9833_CTRLPSEL);
403 // Select soft the working phase register according to parameter
404 if(phaseReg == 1)
405 {
406 spiData += AD9833_CTRLPSEL;
407 }
408 AD9833_TxSpi(spiData);
409 ctrlRegValue = spiData;
410 }
411 else
412 {
413 if (prog_method == 1)
414 {
415 // Select hard the working phase register according to parameter
416 if (phaseReg == 1)
417 {
418 AD9834_PSEL_HIGH;
419 }
420 else
421 {
422 if (phaseReg == 0)
423 {
424 AD9834_PSEL_LOW;
425 }
426 }
427 }
428 }
429 }
430
431 /**************************************************************************//**
432 * @brief Sets the programming method. (only for AD9834 & AD9838)
433 *
434 * @param value - soft or hard method. (0 / 1)
435 *
436 * @return None.
437 ******************************************************************************/
438 void AD9834_SelectProgMethod(unsigned char value)
439 {
440 unsigned short spiData = (ctrlRegValue & ~AD9834_CTRLPINSW);
441
442 prog_method = 0; // software method
443 if (value == 1)
444 {
445 spiData += AD9834_CTRLPINSW;
446 prog_method = 1; // hardware method
447 }
448 AD9833_TxSpi(spiData);
449 ctrlRegValue = spiData;
450 }
451
452 /**************************************************************************//**
453 * @brief Configures the control register for logic output.
454 * (only for AD9834 & AD9838)
455 *
456 * @param opbiten - Enables / disables the logic output.
457 * @param signpib - Connects comparator / MSB to the SIGN BIT OUT pin.
458 * @param div2 - MSB / MSB/2
459 *
460 * @return None.
461 ******************************************************************************/
462 void AD9834_LogicOutput(unsigned char opbiten,
463 unsigned char signpib,
464 unsigned char div2)
465 {
466 unsigned short spiData = 0;
467
468 spiData = (ctrlRegValue & ~(AD9833_CTRLOPBITEN |
469 AD9833_CTRLMODE |
470 AD9834_CTRLSIGNPIB |
471 AD9833_CTRLDIV2));
472
473
474 if(opbiten == 1)
475 {
476 spiData |= AD9833_CTRLOPBITEN;
477
478 if (signpib == 1)
479 {
480 spiData |= AD9834_CTRLSIGNPIB;
481 }
482 else
483 {
484 if (signpib == 0)
485 {
486 spiData &= ~AD9834_CTRLSIGNPIB;
487 }
488 }
489 if (div2 == 1)
490 {
491 spiData |= AD9833_CTRLDIV2;
492 }
493 else
494 {
495 if (div2 == 0)
496 {
497 spiData &= ~AD9833_CTRLDIV2;
498 }
499 }
500 }
501 else
502 {
503 if (opbiten == 0)
504 {
505 spiData &= ~AD9833_CTRLOPBITEN;
506 }
507 }
508 AD9833_TxSpi(spiData);
509 ctrlRegValue = spiData;
510 }
511
512