Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/Pmods/PmodDA3/AD5541A.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file AD5541A.c
3 * @brief Driver File for AD5541A Driver.
4 * @author ATofan ([email protected])
5 ********************************************************************************
6 * Copyright 2012(c) Analog Devices, Inc.
7 *
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 * - Neither the name of Analog Devices, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 * - The use of this software may or may not infringe the patent rights
22 * of one or more patent holders. This license does not release you
23 * from the requirement that you obtain separate licenses from these
24 * patent holders to use this software.
25 * - Use of the software either in source or binary form, must be run
26 * on or directly connected to an Analog Devices Inc. component.
27 *
28 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
30 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 *
39 ********************************************************************************
40 * SVN Revision: $WCREV$
41 *******************************************************************************/
42
43 #include <stdio.h>
44 #include "xparameters.h"
45 #include "xil_io.h"
46 #include "system_config.h"
47 #include "ad5541a.h"
48
49 volatile int rxData = 0;
50 volatile int timeStepValue = 0xFD050F81;
51 volatile int dacStepValue = 0x1555;
52 volatile int dacStep = 0x0f;
53 volatile char dacTop = 0;
54 volatile char dacBot = 1;
55
56 extern u32 (*SPI_Init)(u32, char, char, char);
57 extern u32 (*SPI_TransferData)(u32, char, char*, char, char*, char);
58
59 /******************************************************************************
60 * @brief Displays Main Menu.
61 *
62 * @param None.
63 *
64 * @return None.
65 ******************************************************************************/
66 void AD5541_DisplayMainMenu(void)
67 {
68 xil_printf("\n\rPmod-DA3 Demo Program\n\r");
69 xil_printf("Available modes: \n\r");
70 xil_printf(" [f] Fixed Value\n\r");
71 xil_printf(" [w] Waveform Generation\n\r");
72 xil_printf(" [p] Programmable Ramp Signal Generator\n\r");
73 xil_printf(" [m] Display this menu\n\r");
74 xil_printf("Press key to select the desired mode:\n\r");
75
76 }
77
78 /******************************************************************************
79 * @brief Displays Menu.
80 *
81 * @param None.
82 *
83 * @return None.
84 ******************************************************************************/
85 void AD5541_DisplayMenu(void)
86 {
87 xil_printf("\n\rAvailable modes: \n\r");
88 xil_printf(" [f] Fixed Value\n\r");
89 xil_printf(" [w] Waveform Generation\n\r");
90 xil_printf(" [p] Programmable Ramp Signal Generator\n\r");
91 xil_printf(" [m] Display this menu\n\r");
92 xil_printf("Press key to select the desired mode:\n\r");
93 rxData = 0;
94 }
95
96 /******************************************************************************
97 * @brief Displays Waveform Generation Mode Menu.
98 *
99 * @param None.
100 *
101 * @return None.
102 ******************************************************************************/
103 void AD5541_DisplayWaveMenu(void)
104 {
105 xil_printf("\n\rPlease select between the following waveforms:\n\r");
106 xil_printf(" 1. Square Waveform\n\r");
107 xil_printf(" 2. Triangle Waveform\n\r");
108 xil_printf(" 3. Sawtooth Waveform\n\r");
109 xil_printf(" 4. Sine Waveform\n\r");
110 xil_printf(" q. Return to Main Menu\n\r");
111 }
112
113 /******************************************************************************
114 * @brief Displays Fixed Valeu Mode Menu.
115 *
116 * @param None.
117 *
118 * @return None.
119 ******************************************************************************/
120 void AD5541_DisplayFixedMenu(void)
121 {
122 xil_printf("\n\r>Fixed Value Mode Selected\n\r");
123 xil_printf("Press [q] to return to Main Menu\n\r");
124 }
125
126 /******************************************************************************
127 * @brief Displays Programmable Ramp Mode Menu.
128 *
129 * @param None.
130 *
131 * @return None.
132 ******************************************************************************/
133 void AD5541_DisplayProgRampMenu(void)
134 {
135 xil_printf("\n\rPlease select between the following options:\n\r");
136 xil_printf(" [t] Enter time step\n\r");
137 xil_printf(" [i] Enter increment size\n\r");
138 xil_printf(" [r] Run Ramp Signal Generator\n\r");
139 xil_printf(" [q] Return to Main Menu\n\r");
140 xil_printf("Press key for desired option.\n\r");
141 }
142
143 void AD5541_SendData(int txData)
144 {
145 char txBuffer[2] = {0, 0};
146 char rxBuffer[2] = {0, 0};
147 int cfgValue = txData;
148
149 txBuffer[0] = ((cfgValue & 0xFF00) >> 8);
150 txBuffer[1] = (cfgValue & 0xFF);
151
152 SPI_TransferData(SPI_BASEADDR, 2, txBuffer, 2, rxBuffer, 1);
153 }
154
155 /******************************************************************************
156 * @brief Enters Fixed Generation Mode.
157 *
158 * @param None.
159 *
160 * @return None.
161 ******************************************************************************/
162 void AD5541_FixedValue(void)
163 {
164 char rx = 0;
165 char c[4] = "0000";
166 char *c_ptr;
167 int i = 0;
168 char cnt = 0;
169 char valid = 0;
170 int value = 0;
171
172 // Disable interrupts in order to use normal polling
173 DisableInterrupts(UART_INTERRUPT);
174
175 AD5541_DisplayFixedMenu();
176
177 // Maximum number of received characters will be 6 (e.g. F F F F 0x0D 0x0A)
178 while(rx != 'q')
179 {
180 cnt = 0;
181 i = 0;
182 valid = 0;
183 value = 0;
184 c_ptr = c;
185
186 xil_printf("\n\rPlease enter a value between 0x0000 and 0xffff: 0x");
187 while(i < 6)
188 {
189 // Check if data is available on the UART
190 rx = UartReadChar();
191 xil_printf("%c", rx);
192 // Check if pressed key is [Enter]
193 if(rx == 0x0D)
194 {
195 i = 5;
196 }
197 else if(rx == 0x0A)
198 {
199 i = 5;
200 }
201 else if(rx == 'q')
202 {
203 i = 6;
204 valid = 0;
205 }
206 else if(((rx > 0x00)&&(rx < 0x30))|| // Not 0 - 9
207 ((rx > 0x39)&&(rx < 0x41))|| // Not A - F
208 ((rx > 0x46)&&(rx < 0x61))|| // Not a - f
209 (rx > 0x66))
210 {
211 xil_printf("\n\rCharacters entered must be HEX values (0 to 9 and A B C D E F)");
212 i = 6;
213 valid = 0;
214 }
215 else
216 {
217 *c_ptr++ = rx;
218 cnt = cnt + 1;
219 valid = 1;
220 }
221
222 if(cnt == 4)
223 {
224 i = 6;
225 }
226 i++;
227 }
228
229 // Translate from ASCII to hex
230 for(i = 0; i < cnt; i++)
231 {
232 if(c[i] > 0x60)
233 {
234 value = value * 16 + (c[i] - 0x57);
235 }
236 else if(c[i] > 0x39)
237 {
238 value = value * 16 + (c[i] - 0x37);
239 }
240 else
241 {
242 value = value * 16 + (c[i] - 0x30);
243 }
244 }
245
246 if(valid == 1)
247 {
248 AD5541_SendData(value);
249 xil_printf("\n\r>DAC Output modified");
250 }
251 }
252
253 rxData = 'm';
254 // Enable interrupts
255 EnableInterrupts(UART_INTERRUPT);
256 xil_printf("\n\r>Returning to Menu\n\r");
257 }
258
259 /******************************************************************************
260 * @brief Enters Waveform Generation Mode.
261 *
262 * @param None.
263 *
264 * @return None.
265 ******************************************************************************/
266 void AD5541_WaveGen(void)
267 {
268 int prevRxData = 0;
269 int i = 0;
270 char valid = 1;
271 // Disable interrupts in order to use normal polling
272 DisableInterrupts(UART_INTERRUPT);
273
274 AD5541_DisplayWaveMenu();
275
276 // Check if data is available on the UART
277 rxData = UartReadChar();
278
279 // Enable interrupts
280 EnableInterrupts(UART_INTERRUPT);
281 while(valid)
282 {
283 switch(rxData)
284 {
285 case '1':
286 AD5541_SendData(squaretable[i] << 8);
287 i+=1;
288 break;
289 case '2':
290 AD5541_SendData(triangletable[i] << 8);
291 i+=1;
292 break;
293 case '3':
294 AD5541_SendData(sawtable[i] << 8);
295 i+=1;
296 break;
297 case '4':
298 AD5541_SendData(sinetable[i] << 8);
299 i+=1;
300 break;
301 case 'q':
302 valid = 0;
303 break;
304 default:
305 break;
306 }
307 if(rxData != prevRxData)
308 {
309 prevRxData = rxData;
310 switch(rxData)
311 {
312 case '1':
313 xil_printf("\n\r>Square waveform selected\n\r");
314 break;
315 case '2':
316 xil_printf("\n\r>Triangle waveform selected\n\r");
317 break;
318 case '3':
319 xil_printf("\n\r>Sawtooth waveform selected\n\r");
320 break;
321 case '4':
322 xil_printf("\n\r>Sine waveform selected\n\r");
323 break;
324 case 'q':
325 xil_printf("\n\r>Returning to Menu\n\r");
326 break;
327 default:
328 xil_printf("\n\rPress [1] to [4] in order to select the desired output, or [q] to exit\n\r");
329 break;
330 }
331 }
332 if(i>255)
333 {
334 i = 0;
335 }
336 }
337 rxData = 'm';
338 }
339
340 /******************************************************************************
341 * @brief Enters Time Step Value Set Mode.
342 *
343 * @param None.
344 *
345 * @return None.
346 ******************************************************************************/
347 void AD5541_EnterTimeStep(void)
348 {
349 char rx = 0;
350 char c[4] = "0000";
351 char *c_ptr;
352 int i = 0;
353 char cnt = 0;
354 char valid = 0;
355 int value = 0;
356 int topValue = 0xFFFFFFFF;
357
358 // Disable interrupts in order to use normal polling
359 DisableInterrupts(UART_INTERRUPT);
360
361 while(valid == 0)
362 {
363 c_ptr = c;
364 i = 0;
365 cnt = 0;
366 value = 0;
367
368 xil_printf("\n\rPlease enter a value between 100 and 5000 [ms]: ");
369
370 // Maximum number of received characters will be 6 (e.g. 5 0 0 0 0x0D 0x0A)
371 while(i < 6)
372 {
373 // Check if data is available on UART
374 rx = UartReadChar();
375 xil_printf("%c", rx);
376 // Check if pressed key is [Enter]
377 if(rx == 0x0D)
378 {
379 i = 5;
380 }
381 else if(rx == 0x0A)
382 {
383 i = 5;
384 }
385 else if(((rx > 0x00)&&(rx < 0x30))||(rx > 0x39))
386 {
387 xil_printf("\n\rCharacters must be digits [0] to [9]\n\r");
388 i = 6;
389 valid = 0;
390 }
391 else
392 {
393 *c_ptr ++= rx;
394 cnt++;
395 valid = 1;
396 }
397
398 if(cnt == 4)
399 {
400 i = 6;
401 }
402 i++;
403 }
404 xil_printf("\n\r");
405 // Translate from ASCII to dec
406 for(i = 0; i < cnt; i++)
407 {
408 value = value * 10 + (c[i] - 0x30);
409 }
410
411 if((value > 5000)||(value < 100))
412 {
413 xil_printf("\n\r> Incorrect value entered. Value must be between 100 and 5000.\n\r");
414 valid = 0;
415 }
416 }
417
418 if(valid == 1)
419 {
420 if(USE_PS7 == 1){
421 timeStepValue = ((float)6781 / 1000) * value + 1;
422 }
423 else{
424 timeStepValue = (topValue - (value * 100000)) + 2;
425 }
426 }
427 }
428
429 /******************************************************************************
430 * @brief Runs Programmable Ramp Generation Mode.
431 *
432 * @param None.
433 *
434 * @return None.
435 ******************************************************************************/
436 void AD5541_RunProgRamp(void)
437 {
438 xil_printf("\n\r>Running. Press [q] to return to the Main Menu\n\r");
439
440 EnableInterrupts(UART_INTERRUPT);
441
442 StartTimer();
443
444 while(rxData != 'q')
445 {
446 }
447
448 StopTimer();
449
450 rxData ='m';
451
452 DisableInterrupts(UART_INTERRUPT);
453 }
454
455 /******************************************************************************
456 * @brief Enters Increment Step Value Set Mode.
457 *
458 * @param None.
459 *
460 * @return None.
461 ******************************************************************************/
462 void AD5541_EnterIncrStep(void)
463 {
464 char rx = 0;
465 char c[4] = "0000";
466 char *c_ptr;
467 int i = 0;
468 char cnt = 0;
469 char valid = 0;
470 int value = 0;
471
472 // Disable interrupts in order to use normal polling
473 DisableInterrupts(UART_INTERRUPT);
474
475 xil_printf("\n\rPlease enter a value between 0x0000 and 0xFFFF: 0x");
476
477 c_ptr = c;
478
479 // Maximum number of received characters will be 6 (e.g. 5 0 0 F 0x0D 0x0A)
480 while(i < 6)
481 {
482 // Check if data is available on UART
483 rx = UartReadChar();
484 xil_printf("%c", rx);
485 // Check if pressed key is [Enter]
486 if(rx == 0x0D)
487 {
488 i = 5;
489 }
490 else if(rx == 0x0A)
491 {
492 i = 5;
493 }
494 else if(((rx > 0x00)&&(rx < 0x30))|| // Not 0 - 9
495 ((rx > 0x39)&&(rx < 0x41))|| // Not A - F
496 ((rx > 0x46)&&(rx < 0x61))|| // Not a - f
497 (rx > 0x66))
498 {
499 xil_printf("\n\rCharacters entered must be HEX values (0 to 9 and A B C D E F)");
500 i = 6;
501 valid = 0;
502 }
503 else
504 {
505 *c_ptr ++= rx;
506 cnt++;
507 valid = 1;
508 }
509
510 if(cnt == 4)
511 {
512 i = 6;
513 }
514 i++;
515 }
516 xil_printf("\n\r");
517 // Translate from ASCII to dec
518 for(i = 0; i < cnt; i++)
519 {
520 if(c[i] > 0x60)
521 {
522 value = value * 16 + (c[i] - 0x57);
523 }
524 else if(c[i] > 0x39)
525 {
526 value = value * 16 + (c[i] - 0x37);
527 }
528 else
529 {
530 value = value * 16 + (c[i] - 0x30);
531 }
532 }
533
534 if(valid == 1)
535 {
536 dacStepValue = value;
537 }
538 }
539
540 /******************************************************************************
541 * @brief Runs Programmable Ramp Generation Mode Main Menu.
542 *
543 * @param None.
544 *
545 * @return None.
546 ******************************************************************************/
547 void AD5541_ProgRamp(void)
548 {
549 DisableInterrupts(UART_INTERRUPT);
550 AD5541_DisplayProgRampMenu();
551
552 // Check if data is available on the UART
553 rxData = UartReadChar();
554
555 switch(rxData)
556 {
557 case 't':
558 AD5541_EnterTimeStep();
559 break;
560 case 'i':
561 AD5541_EnterIncrStep();
562 break;
563 case 'r':
564 AD5541_RunProgRamp();
565 break;
566 case 'q':
567 rxData = 'm';
568 break;
569 default:
570 xil_printf("\n\rWrong option!");
571 break;
572 }
573
574 if(rxData != 'm')
575 {
576 rxData = 'p';
577 }
578 EnableInterrupts(UART_INTERRUPT);
579 }
580
581 /******************************************************************************
582 * @brief Timer interrupt function. Loads new data into DAC at desired time step
583 *
584 * @param None.
585 *
586 * @return None.
587 ******************************************************************************/
588 void ADITimerIntrFunction(void)
589 {
590 int prevDacStep = dacStep;
591 LoadTimer(timeStepValue);
592 // Increase output data value
593 if(dacBot == 1)
594 {
595 dacStep = dacStep + dacStepValue;
596 }
597 else if(dacTop == 1)
598 {
599 dacStep = dacStep - dacStepValue;
600 }
601 if((dacStep > 0xFFFF) && (dacTop == 0))
602 {
603 dacTop = 1;
604 dacBot = 0;
605 }
606 if((dacStep < 0x0000) && (dacBot == 0))
607 {
608 dacTop = 0;
609 dacBot = 1;
610 }
611 if(dacStep > 0xFFFF)
612 {
613 dacStep = 0xFFFF;
614 }
615 if(dacStep < 0x0000)
616 {
617 dacStep = 0x0000;
618 }
619
620 if(dacStep != prevDacStep)
621 {
622 AD5541_SendData(dacStep);
623 }
624
625 if(USE_PS7 == 0)
626 {
627 // Clear Interrupts
628 Xil_Out32(TIMER_BASEADDR + 0x00, 0x00000100);
629 // Start Timer
630 StartTimer();
631 }
632 }
633
634 /******************************************************************************
635 * @brief Demo application for AD5541A
636 *
637 * @param None.
638 *
639 * @return None.
640 ******************************************************************************/
641 void DemoProgram(void)
642 {
643 AD5541_DisplayMainMenu();
644
645 while(1)
646 {
647 switch(rxData)
648 {
649 case 'f':
650 AD5541_FixedValue();
651 break;
652 case 'w':
653 AD5541_WaveGen();
654 break;
655 case 'p':
656 AD5541_ProgRamp();
657 break;
658 case 'm':
659 AD5541_DisplayMenu();
660 break;
661 case 0:
662 break;
663 default:
664 xil_printf("\n\rWrong option! Please select one of the options below.");
665 AD5541_DisplayMenu();
666 break;
667 }
668 }
669 }
670
671