LibDriver AD9833
Loading...
Searching...
No Matches
driver_ad9833.c
Go to the documentation of this file.
1
36
37#include "driver_ad9833.h"
38
42#define CHIP_NAME "Analog Devices AD9833"
43#define MANUFACTURER_NAME "Analog Devices"
44#define SUPPLY_VOLTAGE_MIN 2.3f
45#define SUPPLY_VOLTAGE_MAX 5.5f
46#define MAX_CURRENT 5.5f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 85.0f
49#define DRIVER_VERSION 1000
50
60static uint8_t a_ad9833_spi_write(ad9833_handle_t *handle, uint16_t reg)
61{
62 uint8_t buf[2];
63
64 buf[0] = (reg >> 8) & 0xFF; /* set msb */
65 buf[1] = (reg >> 0) & 0xFF; /* set lsb */
66 if (handle->spi_write_cmd(buf, 2) != 0) /* write command */
67 {
68 return 1; /* return error */
69 }
70
71 return 0; /* success return 0 */
72}
73
86{
87 uint8_t res;
88 uint16_t conf;
89
90 if (handle == NULL) /* check handle */
91 {
92 return 2; /* return error */
93 }
94 if (handle->debug_print == NULL) /* check debug_print */
95 {
96 return 3; /* return error */
97 }
98 if (handle->spi_init == NULL) /* check spi_init */
99 {
100 handle->debug_print("ad9833: spi_init is null.\n"); /* spi_init is null */
101
102 return 3; /* return error */
103 }
104 if (handle->spi_deinit == NULL) /* check spi_init */
105 {
106 handle->debug_print("ad9833: spi_deinit is null.\n"); /* spi_deinit is null */
107
108 return 3; /* return error */
109 }
110 if (handle->spi_write_cmd == NULL) /* check spi_write_cmd */
111 {
112 handle->debug_print("ad9833: spi_write_cmd is null.\n"); /* spi_write_cmd is null */
113
114 return 3; /* return error */
115 }
116 if (handle->delay_ms == NULL) /* check delay_ms */
117 {
118 handle->debug_print("ad9833: delay_ms is null.\n"); /* delay_ms is null */
119
120 return 3; /* return error */
121 }
122
123 if (handle->spi_init() != 0) /* spi init */
124 {
125 handle->debug_print("ad9833: spi init failed.\n"); /* spi init failed */
126
127 return 3; /* return error */
128 }
129 handle->conf = 0; /* init 0 */
130 conf = handle->conf; /* get config */
131 conf &= ~(1 << 8); /* clear reset */
132 conf |= 1 << 8; /* set reset */
133 res = a_ad9833_spi_write(handle, conf); /* write config */
134 if (res != 0) /* check result */
135 {
136 handle->debug_print("ad9833: write config failed.\n"); /* write config failed */
137
138 return 4; /* return error */
139 }
140 handle->delay_ms(10); /* delay 10ms */
141 conf &= ~(1 << 8); /* clear reset */
142 res = a_ad9833_spi_write(handle, conf); /* write config */
143 if (res != 0) /* check result */
144 {
145 handle->debug_print("ad9833: write config failed.\n"); /* write config failed */
146
147 return 4; /* return error */
148 }
149 handle->inited = 1; /* flag initialization */
150
151 return 0; /* success return 0 */
152}
153
166{
167 uint8_t res;
168 uint16_t conf;
169
170 if (handle == NULL) /* check handle */
171 {
172 return 2; /* return error */
173 }
174 if (handle->inited != 1) /* check handle initialization */
175 {
176 return 3; /* return error */
177 }
178
179 handle->conf &= ~(1 << 7); /* clear settings */
180 handle->conf |= 1 << 7; /* set enable */
181 handle->conf &= ~(1 << 6); /* clear settings */
182 handle->conf |= 1 << 6; /* set enable */
183 conf = handle->conf; /* get config */
184 conf &= ~(3 << 14); /* clear bit14 and bit15 */
185 res = a_ad9833_spi_write(handle, conf); /* write config */
186 if (res != 0) /* check result */
187 {
188 handle->debug_print("ad9833: write config failed.\n"); /* write config failed */
189
190 return 4; /* return error */
191 }
192 if (handle->spi_deinit() != 0) /* spi deinit */
193 {
194 handle->debug_print("ad9833: spi deinit failed.\n"); /* spi deinit failed */
195
196 return 1; /* return error */
197 }
198 handle->inited = 0; /* flag close */
199
200 return 0; /* success return 0 */
201}
202
215{
216 uint8_t res;
217 uint16_t conf;
218
219 if (handle == NULL) /* check handle */
220 {
221 return 2; /* return error */
222 }
223 if (handle->inited != 1) /* check handle initialization */
224 {
225 return 3; /* return error */
226 }
227
228 handle->conf &= ~(1 << 11); /* clear settings */
229 handle->conf |= (number << 11); /* set number */
230 conf = handle->conf; /* get config */
231 conf &= ~(3 << 14); /* clear bit14 and bit15 */
232 res = a_ad9833_spi_write(handle, conf); /* write config */
233 if (res != 0) /* check result */
234 {
235 handle->debug_print("ad9833: write config failed.\n"); /* write config failed */
236
237 return 1; /* return error */
238 }
239
240 return 0; /* success return 0 */
241}
242
255{
256 uint8_t res;
257 uint16_t conf;
258
259 if (handle == NULL) /* check handle */
260 {
261 return 2; /* return error */
262 }
263 if (handle->inited != 1) /* check handle initialization */
264 {
265 return 3; /* return error */
266 }
267
268 handle->conf &= ~(1 << 10); /* clear settings */
269 handle->conf |= (number << 10); /* set number */
270 conf = handle->conf; /* get config */
271 conf &= ~(3 << 14); /* clear bit14 and bit15 */
272 res = a_ad9833_spi_write(handle, conf); /* write config */
273 if (res != 0) /* check result */
274 {
275 handle->debug_print("ad9833: write config failed.\n"); /* write config failed */
276
277 return 1; /* return error */
278 }
279
280 return 0; /* success return 0 */
281}
282
296uint8_t ad9833_set_frequency(ad9833_handle_t *handle, ad9833_number_t number, uint32_t freq)
297{
298 uint8_t res;
299 uint16_t conf;
300
301 if (handle == NULL) /* check handle */
302 {
303 return 2; /* return error */
304 }
305 if (handle->inited != 1) /* check handle initialization */
306 {
307 return 3; /* return error */
308 }
309 if (freq > 0x0FFFFFFF) /* check freq */
310 {
311 handle->debug_print("ad9833: freq > 0x0FFFFFFF.\n"); /* freq > 0x0FFFFFFF */
312
313 return 4; /* return error */
314 }
315
316 conf = handle->conf; /* get config */
317 conf |= 1 << 13; /* set b28 */
318 conf &= ~(3 << 14); /* clear bit14 and bit15 */
319 res = a_ad9833_spi_write(handle, conf); /* write config */
320 if (res != 0) /* check result */
321 {
322 handle->debug_print("ad9833: write config failed.\n"); /* write config failed */
323
324 return 1; /* return error */
325 }
326 if (number == AD9833_NUMBER_0) /* number 0 */
327 {
328 conf = 1 << 14; /* set freq number0 */
329 conf |= freq & 0x3FFF; /* set lsb */
330 res = a_ad9833_spi_write(handle, conf); /* write config */
331 if (res != 0) /* check result */
332 {
333 handle->debug_print("ad9833: write config failed.\n"); /* write config failed */
334
335 return 1; /* return error */
336 }
337 conf = 1 << 14; /* set freq number0 */
338 conf |= (freq >> 14) & 0x3FFF; /* set msb */
339 res = a_ad9833_spi_write(handle, conf); /* write config */
340 if (res != 0) /* check result */
341 {
342 handle->debug_print("ad9833: write config failed.\n"); /* write config failed */
343
344 return 1; /* return error */
345 }
346 }
347 else /* number 1 */
348 {
349 conf = 1 << 15; /* set freq number1 */
350 conf |= freq & 0x3FFF; /* set lsb */
351 res = a_ad9833_spi_write(handle, conf); /* write config */
352 if (res != 0) /* check result */
353 {
354 handle->debug_print("ad9833: write config failed.\n"); /* write config failed */
355
356 return 1; /* return error */
357 }
358 conf = 1 << 15; /* set freq number1 */
359 conf |= (freq >> 14) & 0x3FFF; /* set msb */
360 res = a_ad9833_spi_write(handle, conf); /* write config */
361 if (res != 0) /* check result */
362 {
363 handle->debug_print("ad9833: write config failed.\n"); /* write config failed */
364
365 return 1; /* return error */
366 }
367 }
368
369 return 0; /* success return 0 */
370}
371
385uint8_t ad9833_set_frequency_lsb(ad9833_handle_t *handle, ad9833_number_t number, uint16_t freq)
386{
387 uint8_t res;
388 uint16_t conf;
389
390 if (handle == NULL) /* check handle */
391 {
392 return 2; /* return error */
393 }
394 if (handle->inited != 1) /* check handle initialization */
395 {
396 return 3; /* return error */
397 }
398 if (freq > 0x3FFF) /* check freq */
399 {
400 handle->debug_print("ad9833: freq > 0x3FFF.\n"); /* freq > 0x3FFF */
401
402 return 4; /* return error */
403 }
404
405 conf = handle->conf; /* get config */
406 conf &= ~(1 << 13); /* clear b28 */
407 conf &= ~(1 << 12); /* set lsb */
408 conf &= ~(3 << 14); /* clear bit14 and bit15 */
409 res = a_ad9833_spi_write(handle, conf); /* write config */
410 if (res != 0) /* check result */
411 {
412 handle->debug_print("ad9833: write config failed.\n"); /* write config failed */
413
414 return 1; /* return error */
415 }
416 if (number == AD9833_NUMBER_0) /* number 0 */
417 {
418 conf = 1 << 14; /* set freq number0 */
419 conf |= freq; /* set lsb */
420 res = a_ad9833_spi_write(handle, conf); /* write config */
421 if (res != 0) /* check result */
422 {
423 handle->debug_print("ad9833: write config failed.\n"); /* write config failed */
424
425 return 1; /* return error */
426 }
427 }
428 else /* number 1 */
429 {
430 conf = 1 << 15; /* set freq number1 */
431 conf |= freq; /* set lsb */
432 res = a_ad9833_spi_write(handle, conf); /* write config */
433 if (res != 0) /* check result */
434 {
435 handle->debug_print("ad9833: write config failed.\n"); /* write config failed */
436
437 return 1; /* return error */
438 }
439 }
440
441 return 0; /* success return 0 */
442}
443
457uint8_t ad9833_set_frequency_msb(ad9833_handle_t *handle, ad9833_number_t number, uint16_t freq)
458{
459 uint8_t res;
460 uint16_t conf;
461
462 if (handle == NULL) /* check handle */
463 {
464 return 2; /* return error */
465 }
466 if (handle->inited != 1) /* check handle initialization */
467 {
468 return 3; /* return error */
469 }
470 if (freq > 0x3FFF) /* check freq */
471 {
472 handle->debug_print("ad9833: freq > 0x3FFF.\n"); /* freq > 0x3FFF */
473
474 return 4; /* return error */
475 }
476
477 conf = handle->conf; /* get config */
478 conf &= ~(1 << 13); /* clear b28 */
479 conf |= 1 << 12; /* set msb */
480 conf &= ~(3 << 14); /* clear bit14 and bit15 */
481 res = a_ad9833_spi_write(handle, conf); /* write config */
482 if (res != 0) /* check result */
483 {
484 handle->debug_print("ad9833: write config failed.\n"); /* write config failed */
485
486 return 1; /* return error */
487 }
488 if (number == AD9833_NUMBER_0) /* number 0 */
489 {
490 conf = 1 << 14; /* set freq number0 */
491 conf |= freq; /* set lsb */
492 res = a_ad9833_spi_write(handle, conf); /* write config */
493 if (res != 0) /* check result */
494 {
495 handle->debug_print("ad9833: write config failed.\n"); /* write config failed */
496
497 return 1; /* return error */
498 }
499 }
500 else /* number 1 */
501 {
502 conf = 1 << 15; /* set freq number1 */
503 conf |= freq; /* set lsb */
504 res = a_ad9833_spi_write(handle, conf); /* write config */
505 if (res != 0) /* check result */
506 {
507 handle->debug_print("ad9833: write config failed.\n"); /* write config failed */
508
509 return 1; /* return error */
510 }
511 }
512
513 return 0; /* success return 0 */
514}
515
529uint8_t ad9833_set_phase(ad9833_handle_t *handle, ad9833_number_t number, uint16_t phase)
530{
531 uint8_t res;
532 uint16_t conf;
533
534 if (handle == NULL) /* check handle */
535 {
536 return 2; /* return error */
537 }
538 if (handle->inited != 1) /* check handle initialization */
539 {
540 return 3; /* return error */
541 }
542 if (phase > 0x0FFF) /* check phase */
543 {
544 handle->debug_print("ad9833: phase > 0x0FFF.\n"); /* phase > 0x0FFF */
545
546 return 4; /* return error */
547 }
548
549 if (number == AD9833_NUMBER_0) /* number 0 */
550 {
551 conf = 3 << 14; /* set phase */
552 conf &= ~(1 << 13); /* set number 0 */
553 conf |= phase & 0x0FFF; /* set phase */
554 res = a_ad9833_spi_write(handle, conf); /* write config */
555 if (res != 0) /* check result */
556 {
557 handle->debug_print("ad9833: write config failed.\n"); /* write config failed */
558
559 return 1; /* return error */
560 }
561 }
562 else /* number 1 */
563 {
564 conf = 3 << 14; /* set phase */
565 conf |= 1 << 13; /* set number 1 */
566 conf |= phase & 0x0FFF; /* set phase */
567 res = a_ad9833_spi_write(handle, conf); /* write config */
568 if (res != 0) /* check result */
569 {
570 handle->debug_print("ad9833: write config failed.\n"); /* write config failed */
571
572 return 1; /* return error */
573 }
574 }
575
576 return 0; /* success return 0 */
577}
578
590{
591 uint8_t res;
592 uint16_t conf;
593
594 if (handle == NULL) /* check handle */
595 {
596 return 2; /* return error */
597 }
598 if (handle->inited != 1) /* check handle initialization */
599 {
600 return 3; /* return error */
601 }
602
603 conf = handle->conf; /* get config */
604 conf &= ~(1 << 8); /* clear reset */
605 conf |= 1 << 8; /* set reset */
606 res = a_ad9833_spi_write(handle, conf); /* write config */
607 if (res != 0) /* check result */
608 {
609 handle->debug_print("ad9833: write config failed.\n"); /* write config failed */
610
611 return 1; /* return error */
612 }
613 handle->delay_ms(10); /* delay 10ms */
614 conf &= ~(1 << 8); /* clear reset */
615 res = a_ad9833_spi_write(handle, conf); /* write config */
616 if (res != 0) /* check result */
617 {
618 handle->debug_print("ad9833: write config failed.\n"); /* write config failed */
619
620 return 1; /* return error */
621 }
622
623 return 0; /* success return 0 */
624}
625
638{
639 uint8_t res;
640 uint16_t conf;
641
642 if (handle == NULL) /* check handle */
643 {
644 return 2; /* return error */
645 }
646 if (handle->inited != 1) /* check handle initialization */
647 {
648 return 3; /* return error */
649 }
650
651 handle->conf &= ~(1 << 6); /* clear settings */
652 handle->conf |= enable << 6; /* set enable */
653 conf = handle->conf; /* get config */
654 conf &= ~(3 << 14); /* clear bit14 and bit15 */
655 res = a_ad9833_spi_write(handle, conf); /* write config */
656 if (res != 0) /* check result */
657 {
658 handle->debug_print("ad9833: write config failed.\n"); /* write config failed */
659
660 return 1; /* return error */
661 }
662
663 return 0; /* success return 0 */
664}
665
678{
679 uint8_t res;
680 uint16_t conf;
681
682 if (handle == NULL) /* check handle */
683 {
684 return 2; /* return error */
685 }
686 if (handle->inited != 1) /* check handle initialization */
687 {
688 return 3; /* return error */
689 }
690
691 handle->conf &= ~(1 << 7); /* clear settings */
692 handle->conf |= enable << 7; /* set enable */
693 conf = handle->conf; /* get config */
694 conf &= ~(3 << 14); /* clear bit14 and bit15 */
695 res = a_ad9833_spi_write(handle, conf); /* write config */
696 if (res != 0) /* check result */
697 {
698 handle->debug_print("ad9833: write config failed.\n"); /* write config failed */
699
700 return 1; /* return error */
701 }
702
703 return 0; /* success return 0 */
704}
705
718{
719 uint8_t res;
720 uint8_t c;
721 uint16_t conf;
722
723 if (handle == NULL) /* check handle */
724 {
725 return 2; /* return error */
726 }
727 if (handle->inited != 1) /* check handle initialization */
728 {
729 return 3; /* return error */
730 }
731
732 handle->conf &= ~(1 << 5); /* clear opbiten */
733 handle->conf &= ~(1 << 3); /* clear div2 */
734 handle->conf &= ~(1 << 1); /* clear mode */
735 c = mode; /* copy mode */
736 handle->conf |= (((c >> 2) & 0x1) << 5); /* set opbiten */
737 handle->conf |= (((c >> 0) & 0x1) << 3); /* set div2 */
738 handle->conf |= (((c >> 1) & 0x1) << 1); /* set mode */
739 conf = handle->conf; /* get config */
740 conf &= ~(3 << 14); /* clear bit14 and bit15 */
741 res = a_ad9833_spi_write(handle, conf); /* write config */
742 if (res != 0) /* check result */
743 {
744 handle->debug_print("ad9833: write config failed.\n"); /* write config failed */
745
746 return 1; /* return error */
747 }
748
749 return 0; /* success return 0 */
750}
751
763uint8_t ad9833_frequency_convert_to_register(ad9833_handle_t *handle, double hz, uint32_t *reg)
764{
765 if (handle == NULL) /* check handle */
766 {
767 return 2; /* return error */
768 }
769 if (handle->inited != 1) /* check handle initialization */
770 {
771 return 3; /* return error */
772 }
773
774 *reg = (uint32_t)(hz / (25000000.0 / pow(2, 28))); /* convert real data to register data */
775
776 return 0; /* success return 0 */
777}
778
790uint8_t ad9833_frequency_convert_to_data(ad9833_handle_t *handle, uint32_t reg, double *hz)
791{
792 if (handle == NULL) /* check handle */
793 {
794 return 2; /* return error */
795 }
796 if (handle->inited != 1) /* check handle initialization */
797 {
798 return 3; /* return error */
799 }
800
801 *hz = (double)reg * (25000000.0 / pow(2, 28)); /* convert real data to register data */
802
803 return 0; /* success return 0 */
804}
805
817uint8_t ad9833_phase_convert_to_register(ad9833_handle_t *handle, double rad, uint16_t *reg)
818{
819 if (handle == NULL) /* check handle */
820 {
821 return 2; /* return error */
822 }
823 if (handle->inited != 1) /* check handle initialization */
824 {
825 return 3; /* return error */
826 }
827
828 *reg = (uint32_t)(rad / ((2.0 * 3.14159265357) / 4096.0)); /* convert real data to register data */
829
830 return 0; /* success return 0 */
831}
832
844uint8_t ad9833_phase_convert_to_data(ad9833_handle_t *handle, uint16_t reg, double *rad)
845{
846 if (handle == NULL) /* check handle */
847 {
848 return 2; /* return error */
849 }
850 if (handle->inited != 1) /* check handle initialization */
851 {
852 return 3; /* return error */
853 }
854
855 *rad = (double)reg * ((2.0 * 3.14159265357) / 4096.0); /* convert real data to register data */
856
857 return 0; /* success return 0 */
858}
859
871uint8_t ad9833_set_reg(ad9833_handle_t *handle, uint16_t value)
872{
873 uint8_t res;
874
875 if (handle == NULL) /* check handle */
876 {
877 return 2; /* return error */
878 }
879 if (handle->inited != 1) /* check handle initialization */
880 {
881 return 3; /* return error */
882 }
883
884 res = a_ad9833_spi_write(handle, value); /* write config */
885 if (res != 0) /* check result */
886 {
887 handle->debug_print("ad9833: write config failed.\n"); /* write config failed */
888
889 return 1; /* return error */
890 }
891
892 return 0; /* success return 0 */
893}
894
904{
905 if (info == NULL) /* check handle */
906 {
907 return 2; /* return error */
908 }
909
910 memset(info, 0, sizeof(ad9833_info_t)); /* initialize ad9833 info structure */
911 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
912 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
913 strncpy(info->interface, "SPI", 8); /* copy interface name */
914 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
915 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
916 info->max_current_ma = MAX_CURRENT; /* set maximum current */
917 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
918 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
919 info->driver_version = DRIVER_VERSION; /* set driver version */
920
921 return 0; /* success return 0 */
922}
#define MAX_CURRENT
#define SUPPLY_VOLTAGE_MAX
#define TEMPERATURE_MAX
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
driver ad9833 header file
uint8_t ad9833_set_internal_clock_disable(ad9833_handle_t *handle, ad9833_bool_t enable)
enable or disable internal clock disable
ad9833_bool_t
ad9833 bool enumeration definition
uint8_t ad9833_set_frequency_lsb(ad9833_handle_t *handle, ad9833_number_t number, uint16_t freq)
set the frequency lsb
uint8_t ad9833_frequency_convert_to_register(ad9833_handle_t *handle, double hz, uint32_t *reg)
convert the frequency to the register raw data
ad9833_number_t
ad9833 number enumeration definition
uint8_t ad9833_set_frequency_msb(ad9833_handle_t *handle, ad9833_number_t number, uint16_t freq)
set the frequency msb
uint8_t ad9833_set_dac_power_down(ad9833_handle_t *handle, ad9833_bool_t enable)
enable or disable dac power down
struct ad9833_handle_s ad9833_handle_t
ad9833 handle structure definition
uint8_t ad9833_set_used_frequency_register(ad9833_handle_t *handle, ad9833_number_t number)
set the used frequency register
uint8_t ad9833_phase_convert_to_register(ad9833_handle_t *handle, double rad, uint16_t *reg)
convert the phase to the register raw data
struct ad9833_info_s ad9833_info_t
ad9833 information structure definition
uint8_t ad9833_phase_convert_to_data(ad9833_handle_t *handle, uint16_t reg, double *rad)
convert the register raw data to phase
uint8_t ad9833_set_output_mode(ad9833_handle_t *handle, ad9833_output_mode_t mode)
set output mode
uint8_t ad9833_init(ad9833_handle_t *handle)
initialize the chip
ad9833_output_mode_t
ad9833 output mode enumeration definition
uint8_t ad9833_set_frequency(ad9833_handle_t *handle, ad9833_number_t number, uint32_t freq)
set the frequency
uint8_t ad9833_reset(ad9833_handle_t *handle)
reset the chip
uint8_t ad9833_deinit(ad9833_handle_t *handle)
close the chip
uint8_t ad9833_set_used_phase_register(ad9833_handle_t *handle, ad9833_number_t number)
set the used phase register
uint8_t ad9833_set_phase(ad9833_handle_t *handle, ad9833_number_t number, uint16_t phase)
set the phase
uint8_t ad9833_info(ad9833_info_t *info)
get chip's information
uint8_t ad9833_frequency_convert_to_data(ad9833_handle_t *handle, uint32_t reg, double *hz)
convert the register raw data to frequency
@ AD9833_NUMBER_0
uint8_t ad9833_set_reg(ad9833_handle_t *handle, uint16_t value)
set the chip register
uint8_t(* spi_init)(void)
void(* delay_ms)(uint32_t ms)
void(* debug_print)(const char *const fmt,...)
uint8_t(* spi_deinit)(void)
uint8_t(* spi_write_cmd)(uint8_t *buf, uint16_t len)
float supply_voltage_max_v
uint32_t driver_version
char manufacturer_name[32]
float supply_voltage_min_v
char chip_name[32]