Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/Pmods/PmodDA5/AD5781.c

raw symbols SCIP
1 /**************************************************************************//**
2 * @file AD5781.c
3 * @brief Source file for AD5781 driver.
4 * @author ATofan ([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 * SVN Revision: $WCREV$
42 ******************************************************************************/
43
44 /*****************************************************************************/
45 /***************************** Include Files *********************************/
46 /*****************************************************************************/
47 #include <stdio.h>
48 #include "xparameters.h"
49 #include "xil_io.h"
50 #include "system_config.h"
51 #include "AD5781.h"
52
53 /*****************************************************************************/
54 /***************************** Variables Definitions *************************/
55 /*****************************************************************************/
56 volatile int rxData = 0;
57 volatile int timeStepValue = 0xFD050F81;
58 volatile int dacStepValue = 0x1555;
59 volatile int dacStep = 0x0f;
60 volatile char dacTop = 0;
61 volatile char dacBot = 1;
62
63 extern u32 (*SPI_Init)(u32, char, char, char);
64 extern u32 (*SPI_TransferData)(u32, char, char*, char, char*, char);
65
66 /*****************************************************************************/
67 /***************************** Functions Definitions *************************/
68 /*****************************************************************************/
69
70 /******************************************************************************
71 * @brief Displays Main Menu.
72 *
73 * @param None.
74 *
75 * @return None.
76 ******************************************************************************/
77 void AD5781_DisplayMainMenu(void)
78 {
79 xil_printf("\n\rPmod-DA5 Demo Program\n\r");
80 xil_printf("Available modes: \n\r");
81 xil_printf(" [f] Fixed Value\n\r");
82 xil_printf(" [w] Waveform Generation\n\r");
83 xil_printf(" [p] Programmable Ramp Signal Generator\n\r");
84 xil_printf(" [m] Stop the program and display this menu\n\r");
85 xil_printf("Press key to select the desired mode:\n\r");
86
87 }
88
89 /******************************************************************************
90 * @brief Displays Menu.
91 *
92 * @param None.
93 *
94 * @return None.
95 ******************************************************************************/
96 void AD5781_DisplayMenu(void)
97 {
98 xil_printf("\n\rAvailable modes: \n\r");
99 xil_printf(" [f] Fixed Value\n\r");
100 xil_printf(" [w] Waveform Generation\n\r");
101 xil_printf(" [p] Programmable Ramp Signal Generator\n\r");
102 xil_printf(" [m] Stop the program and display this menu\n\r");
103 xil_printf("Press key to select the desired mode:\n\r");
104 rxData = 0;
105 }
106
107 /******************************************************************************
108 * @brief Displays Waveform Generation Mode Menu.
109 *
110 * @param None.
111 *
112 * @return None.
113 ******************************************************************************/
114 void AD5781_DisplayWaveMenu(void)
115 {
116 xil_printf("\n\rPlease select between the following waveforms:\n\r");
117 xil_printf(" 1. Square Waveform\n\r");
118 xil_printf(" 2. Triangle Waveform\n\r");
119 xil_printf(" 3. Sawtooth Waveform\n\r");
120 xil_printf(" 4. Sine Waveform\n\r");
121 xil_printf(" q. Return to Main Menu\n\r");
122 }
123
124 /******************************************************************************
125 * @brief Displays Fixed Value Mode Menu.
126 *
127 * @param None.
128 *
129 * @return None.
130 ******************************************************************************/
131 void AD5781_DisplayFixedMenu(void)
132 {
133 xil_printf("\n\r>Fixed Value Mode Selected\n\r");
134 xil_printf("Press [q] to return to Main Menu\n\r");
135 }
136
137 /******************************************************************************
138 * @brief Displays Programmable Ramp Mode Menu.
139 *
140 * @param None.
141 *
142 * @return None.
143 ******************************************************************************/
144 void AD5781_DisplayProgRampMenu(void)
145 {
146 xil_printf("\n\rPlease select between the following options:\n\r");
147 xil_printf(" [t] Enter time step\n\r");
148 xil_printf(" [i] Enter increment size\n\r");
149 xil_printf(" [r] Run Ramp Signal Generator\n\r");
150 xil_printf("Press [q] to return to Main Menu\n\r");
151 }
152
153 /******************************************************************************
154 * @brief Sends data to the AD5781
155 *
156 * @param txData - Data to be sent.
157 *
158 * @return None.
159 ******************************************************************************/
160 void AD5781_SendData(int txData)
161 {
162 char txBuffer[3] = {0, 0, 0};
163 char rxBuffer[3] = {0, 0, 0};
164 int cfgValue = txData;
165
166 txBuffer[0] = ((cfgValue & 0xFF0000) >> 16);
167 txBuffer[1] = ((cfgValue & 0xFF00) >> 8);
168 txBuffer[2] = (cfgValue & 0xFF);
169 SPI_TransferData(SPI_BASEADDR, 3, txBuffer, 3, rxBuffer, 1);
170
171 }
172
173 /******************************************************************************
174 * @brief Configure the AD5871 to put it in normal operating mode
175 *
176 * @param None.
177 *
178 * @return None.
179 ******************************************************************************/
180 void AD5781_InitDAC(void)
181 {
182 int configValue = 0;
183
184 configValue = ((configValue | DAC_CNT) << 20); /* Set up Control Register address to write */
185 configValue = (configValue | 0x12); /* Clear the DACTRI and OPGND bits, DAC register uses offset binary coding */
186 AD5781_SendData(configValue);
187 }
188
189 /******************************************************************************
190 * @brief Enters Fixed Generation Mode.
191 *
192 * @param None.
193 *
194 * @return None.
195 ******************************************************************************/
196 void AD5781_FixedValue(void)
197 {
198 char rx = 0;
199 char c[5] = "00000";
200 char *c_ptr;
201 int i = 0;
202 char cnt = 0;
203 char valid = 0;
204 int value = 0;
205
206 // Disable interrupts in order to use normal polling
207 DisableInterrupts(UART_INTERRUPT);
208
209 AD5781_DisplayFixedMenu();
210
211 // Maximum number of received characters will be 6 (e.g. F F F F 0x0D 0x0A)
212 while(rx != 'q')
213 {
214 cnt = 0;
215 i = 0;
216 valid = 0;
217 value = 0;
218 c_ptr = c;
219
220 xil_printf("\n\rPlease enter a value between 0x00000 and 0x3ffff: 0x");
221 while(i < 7)
222 {
223 // Check if data is available on the UART
224 rx = UartReadChar();
225 xil_printf("%c", rx);
226 // Check if pressed key is [Enter] : CR or NL
227 if((rx == 0x0D) || (rx == 0x0A))
228 {
229 i = 6;
230 }
231 else if(rx == 'q')
232 {
233 i = 6;
234 valid = 0;
235 }
236 else if(((rx > 0x00)&&(rx < 0x30))|| // Not 0 - 9
237 ((rx > 0x39)&&(rx < 0x41))|| // Not A - F
238 ((rx > 0x46)&&(rx < 0x61))|| // Not a - f
239 (rx > 0x66))
240 {
241 xil_printf("\n\rCharacters entered must be HEX values (0 to 9 and A B C D E F)");
242 i = 7;
243 valid = 0;
244 }
245 else
246 {
247 *c_ptr++ = rx;
248 cnt = cnt + 1;
249 valid = 1;
250 }
251
252 if(cnt == 5)
253 {
254 if(((c[0] > 0x33) || (c[0] < 0x30)) && (cnt == 5))
255 {
256 xil_printf("\n\rThe value entered must be between 0x00000 and 0x3ffff\n\r");
257 valid = 0;
258 i = 7;
259 }
260 i = 7;
261 }
262 i++;
263 }
264
265 // Translate from ASCII to hex
266 for(i = 0; i < cnt; i++)
267 {
268 if(c[i] > 0x60)
269 {
270 value = value * 16 + (c[i] - 0x57);
271 }
272 else if(c[i] > 0x39)
273 {
274 value = value * 16 + (c[i] - 0x37);
275 }
276 else
277 {
278 value = value * 16 + (c[i] - 0x30);
279 }
280 }
281
282 if(valid == 1)
283 {
284 AD5781_SendData(REGWR(value));
285 AD5781_SendData(UPDATE_DAC_REG); //The output is set according the DAC register value
286 xil_printf("\n\r>DAC Output modified\n\r");
287 }
288 }
289
290 rxData = 'm';
291 // Enable interrupts
292 EnableInterrupts(UART_INTERRUPT);
293 xil_printf("\n\r>Returning to Menu...\n\r");
294 }
295
296 /******************************************************************************
297 * @brief Enters Waveform Generation Mode.
298 *
299 * @param None.
300 *
301 * @return None.
302 ******************************************************************************/
303 void AD5781_WaveGen(void)
304 {
305 int prevRxData = 0;
306 int i = 0;
307 char valid = 1;
308 // Disable interrupts in order to use normal polling
309 DisableInterrupts(UART_INTERRUPT);
310
311 AD5781_DisplayWaveMenu();
312
313 // Check if data is available on the UART
314 rxData = UartReadChar();
315
316 // Enable interrupts
317 EnableInterrupts(UART_INTERRUPT);
318
319 while(valid)
320 {
321 switch(rxData)
322 {
323 case '1':
324 AD5781_SendData(REGWR(squaretable[i] << 8));
325 AD5781_SendData(UPDATE_DAC_REG);
326 i+=1;
327 break;
328 case '2':
329 AD5781_SendData(REGWR(triangletable[i] << 8));
330 AD5781_SendData(UPDATE_DAC_REG);
331 i+=1;
332 break;
333 case '3':
334 AD5781_SendData(REGWR(sawtable[i] << 8));
335 AD5781_SendData(UPDATE_DAC_REG);
336 i+=1;
337 break;
338 case '4':
339 AD5781_SendData(REGWR(sinetable[i] << 8));
340 AD5781_SendData(UPDATE_DAC_REG);
341 i+=1;
342 break;
343 case 'q':
344 valid = 0;
345 break;
346 default:
347 break;
348 }
349 if(rxData != prevRxData)
350 {
351 prevRxData = rxData;
352 switch(rxData)
353 {
354 case '1':
355 xil_printf("\n\r>Square waveform selected\n\r");
356 break;
357 case '2':
358 xil_printf("\n\r>Triangle waveform selected\n\r");
359 break;
360 case '3':
361 xil_printf("\n\r>Sawtooth waveform selected\n\r");
362 break;
363 case '4':
364 xil_printf("\n\r>Sine waveform selected\n\r");
365 break;
366 case 'q':
367 xil_printf("\n\r>Returning to Menu\n\r");
368 break;
369 default:
370 xil_printf("Press [1] to [4] in order to select the desired output, or [q] to exit\n\r");
371 break;
372 }
373 }
374 if(i > 255)
375 {
376 i = 0;
377 }
378 }
379 rxData = 'm';
380 }
381
382 /******************************************************************************
383 * @brief Enters Time Step Value Set Mode.
384 *
385 * @param None.
386 *
387 * @return None.
388 ******************************************************************************/
389 void AD5781_EnterTimeStep(void)
390 {
391 char rx = 0;
392 char c[5] = "00000";
393 char *c_ptr;
394 int i = 0;
395 char cnt = 0;
396 char valid = 0;
397 int value = 0;
398 int topValue = 0xFFFFFFFF;
399
400 // Disable interrupts in order to use normal polling
401 DisableInterrupts(UART_INTERRUPT);
402
403 while(valid == 0)
404 {
405 c_ptr = c;
406 i = 0;
407 cnt = 0;
408 value = 0;
409
410 xil_printf("\n\rPlease enter a value between 100 and 5000 [ms]: ");
411
412 // Maximum number of received characters will be 6 (e.g. 5 0 0 0 0x0D 0x0A)
413 while(i < 7)
414 {
415 // Check if data is available on UART
416 rx = UartReadChar();
417 xil_printf("%c", rx);
418 // Check if pressed key is [Enter]
419 if((rx == 0x0D) || (rx == 0x0A))
420 {
421 i = 6;
422 }
423 else if(((rx > 0x00) && (rx < 0x30)) || (rx > 0x39))
424 {
425 xil_printf("\n\rCharacters must be digits [0] to [9]\n\r");
426 i = 7;
427 valid = 0;
428 }
429 else
430 {
431 *c_ptr ++= rx;
432 cnt++;
433 valid = 1;
434 }
435
436 if(cnt == 4)
437 {
438 i = 6;
439 }
440 i++;
441 }
442 xil_printf("\n\r");
443 // Translate from ASCII to decimal
444 for(i = 0; i < cnt; i++)
445 {
446 value = value * 10 + (c[i] - 0x30);
447 }
448
449 if((value > 5000)||(value < 100))
450 {
451 xil_printf("\n\r> Incorrect value entered. Value must be between 100 and 5000.\n\r");
452 valid = 0;
453 }
454 }
455
456 if(valid == 1)
457 {
458 if(USE_PS7 == 1){
459 timeStepValue = ((float)6781 / 1000) * value + 1;
460 }
461 else{
462 timeStepValue = (topValue - (value * 100000)) + 2;
463 }
464 }
465
466 }
467
468 /******************************************************************************
469 * @brief Runs Programmable Ramp Generation Mode.
470 *
471 * @param None.
472 *
473 * @return None.
474 ******************************************************************************/
475 void AD5781_RunProgRamp(void)
476 {
477 xil_printf("\n\r>Running. Press [q] to return to the Main Menu\n\r");
478
479 EnableInterrupts(UART_INTERRUPT);
480
481 // Start Timer
482 StartTimer();
483
484 while(rxData != 'q')
485 {
486 }
487
488 // Stop Timer
489 StopTimer();
490
491 DisableInterrupts(UART_INTERRUPT);
492
493 rxData = 'm';
494 }
495
496 /******************************************************************************
497 * @brief Enters Increment Step Value Set Mode.
498 *
499 * @param None.
500 *
501 * @return None.
502 ******************************************************************************/
503 void AD5781_EnterIncrStep(void)
504 {
505 char rx = 0;
506 char c[4] = "0000";
507 char *c_ptr;
508 int i = 0;
509 char cnt = 0;
510 char valid = 0;
511 int value = 0;
512
513 // Disable interrupts in order to use normal polling
514 DisableInterrupts(UART_INTERRUPT);
515
516 xil_printf("\n\rPlease enter a value between 0x0000 and 0xFFFF: 0x");
517
518 c_ptr = c;
519
520 // Maximum number of received characters will be 6 (e.g. 5 0 0 F 0x0D 0x0A)
521 while(i < 6)
522 {
523 // Check if data is available on UART
524 rx = UartReadChar();
525 xil_printf("%c", rx);
526 // Check if pressed key is [Enter]
527 if(rx == 0x0D)
528 {
529 i = 5;
530 }
531 else if(rx == 0x0A)
532 {
533 i = 5;
534 }
535 else if(((rx > 0x00)&&(rx < 0x30))|| // Not 0 - 9
536 ((rx > 0x39)&&(rx < 0x41))|| // Not A - F
537 ((rx > 0x46)&&(rx < 0x61))|| // Not a - f
538 (rx > 0x66))
539 {
540 xil_printf("\n\rCharacters entered must be HEX values (0 to 9 and A B C D E F)");
541 i = 6;
542 valid = 0;
543 }
544 else
545 {
546 *c_ptr ++= rx;
547 cnt++;
548 valid = 1;
549 }
550
551 if(cnt == 4)
552 {
553 i = 6;
554 }
555 i++;
556 }
557 xil_printf("\n\r");
558 // Translate from ASCII to decimal
559 for(i = 0; i < cnt; i++)
560 {
561 if(c[i] > 0x60)
562 {
563 value = value * 16 + (c[i] - 0x57);
564 }
565 else if(c[i] > 0x39)
566 {
567 value = value * 16 + (c[i] - 0x37);
568 }
569 else
570 {
571 value = value * 16 + (c[i] - 0x30);
572 }
573 }
574
575 if(valid == 1)
576 {
577 dacStepValue = value;
578 xil_printf("Increment size value modified to 0x%x\n\r", dacStepValue);
579 }
580
581 }
582
583 /******************************************************************************
584 * @brief Runs Programmable Ramp Generation Mode Main Menu.
585 *
586 * @param None.
587 *
588 * @return None.
589 ******************************************************************************/
590 void AD5781_ProgRamp(void)
591 {
592 DisableInterrupts(UART_INTERRUPT);
593 AD5781_DisplayProgRampMenu();
594
595 // Check if data is available on the UART
596 rxData = UartReadChar();
597
598 switch(rxData)
599 {
600 case 't':
601 AD5781_EnterTimeStep();
602 break;
603 case 'i':
604 AD5781_EnterIncrStep();
605 break;
606 case 'r':
607 AD5781_RunProgRamp();
608 break;
609 case 'q':
610 rxData = 'm';
611 default:
612 xil_printf("\n\rWrong option! Please select one of the options below.");
613 break;
614 }
615
616 if(rxData != 'm')
617 {
618 rxData = 'p';
619 }
620 EnableInterrupts(UART_INTERRUPT);
621 }
622
623 /******************************************************************************
624 * @brief Timer interrupt function. Loads new data into DAC at desired time step
625 *
626 * @param None.
627 *
628 * @return None.
629 ******************************************************************************/
630 void ADITimerIntrFunction(void)
631 {
632 int prevDacStep = dacStep;
633 LoadTimer(timeStepValue);
634 // Increase output data value
635 if(dacBot == 1)
636 {
637 dacStep = dacStep + dacStepValue;
638 }
639 else if(dacTop == 1)
640 {
641 dacStep = dacStep - dacStepValue;
642 }
643 if((dacStep > 0xFFFF) && (dacTop == 0))
644 {
645 dacTop = 1;
646 dacBot = 0;
647 }
648 if((dacStep < 0x0000) && (dacBot == 0))
649 {
650 dacTop = 0;
651 dacBot = 1;
652 }
653 if(dacStep > 0xFFFF)
654 {
655 dacStep = 0xFFFF;
656 }
657 if(dacStep < 0x0000)
658 {
659 dacStep = 0x0000;
660 }
661
662 if(dacStep != prevDacStep)
663 {
664 AD5781_SendData(REGWR(dacStep));
665 AD5781_SendData(UPDATE_DAC_REG);
666 }
667
668 if(USE_PS7 == 0)
669 {
670 // Clear Interrupts
671 Xil_Out32(TIMER_BASEADDR + 0x00, 0x00000100);
672 // Start Timer
673 StartTimer();
674 }
675 }
676
677 /******************************************************************************
678 * @brief Demo application for AD5541A
679 *
680 * @param None.
681 *
682 * @return None.
683 ******************************************************************************/
684 void DemoProgram(void)
685 {
686 AD5781_DisplayMainMenu();
687
688 while(1)
689 {
690 switch(rxData)
691 {
692 case 'f':
693 AD5781_FixedValue();
694 break;
695 case 'w':
696 AD5781_WaveGen();
697 break;
698 case 'p':
699 AD5781_ProgRamp();
700 break;
701 case 'm':
702 AD5781_DisplayMenu();
703 break;
704 case 0:
705 break;
706 default:
707 xil_printf("\n\rWrong option! Please select one of the options below.");
708 AD5781_DisplayMenu();
709 break;
710 }
711 }
712 }