Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/Pmods/PmodTMP2/ADT7420.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file ADT7420.c
3 * @brief ADT7420 Driver Files for MicroBlaze Processor.
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 /*****************************************************************************/
44 /***************************** Include Files *********************************/
45 /*****************************************************************************/
46 #include <stdio.h>
47 #include "xil_io.h"
48 #include "system_config.h"
49 #include "ADT7420.h"
50
51 /*****************************************************************************/
52 /********************** Variable Definitions *********************************/
53 /*****************************************************************************/
54 volatile int rxData;
55 char valid = 0;
56 int TUpper = 0x1FFF;
57
58 /******************************************************************************
59 * @brief Performs Software Reset for ADT7420 and sets Alert Mode as Comparator.
60 *
61 * @param None.
62 *
63 * @return None.
64 ******************************************************************************/
65 void ADT7420_Init(void)
66 {
67 unsigned char txBuffer[1] = { 0x00 };
68
69 I2C_Write(I2C_BASEADDR, ADT7420_IIC_ADDR, SOFT_RST_REG, 1, txBuffer);
70
71 SetAlertModeComparator();
72 }
73 /******************************************************************************
74 * @brief Sets Alert Mode as Comparator.
75 *
76 * @param None.
77 *
78 * @return None.
79 ******************************************************************************/
80 void SetAlertModeComparator(void)
81 {
82 unsigned char txBuffer[1] = {0x00};
83
84 txBuffer[0] = 1 << INT_CT;
85
86 I2C_Write(I2C_BASEADDR, ADT7420_IIC_ADDR, CONFIG_REG, 1, txBuffer);
87 }
88
89 /******************************************************************************
90 * @brief Returns value from Configuration Register.
91 *
92 * @param None.
93 *
94 * @return rxBuffer[0] & 0x7F - all bits in the Configuration Register, except RESOLUTION bit
95 ******************************************************************************/
96 char ADT7420_ReadConfigReg(void)
97 {
98 unsigned char rxBuffer[1] = {0x00};
99
100 I2C_Read(I2C_BASEADDR, ADT7420_IIC_ADDR, CONFIG_REG, 1, rxBuffer);
101
102 return(rxBuffer[0] & 0x7F);
103 }
104
105 /******************************************************************************
106 * @brief Displays Revision ID and Manufacturer ID.
107 *
108 * @param None.
109 *
110 * @return None.
111 ******************************************************************************/
112 void ADT7420_PrintID(void)
113 {
114 unsigned char rxBuffer[1] = {0x00};
115
116 I2C_Read(I2C_BASEADDR, ADT7420_IIC_ADDR, ID_REG, 1, rxBuffer);
117
118 xil_printf("Revision ID = %d\n\r", rxBuffer[0] & REVISION_ID);
119 xil_printf("Manufacture ID = %d\n\r", (rxBuffer[0] & MANUFACTURE_ID) >> 3);
120 xil_printf("-----------------------------------------\n\r");
121 }
122
123 /******************************************************************************
124 * @brief Displays Main Menu.
125 *
126 * @param None.
127 *
128 * @return None.
129 ******************************************************************************/
130 void ADT7420_DisplayMainMenu(void)
131 {
132 xil_printf("\n\rPmodTMP2 Demo Program \n\r");
133
134 ADT7420_PrintID();
135
136 xil_printf("Available options: \n\r");
137 xil_printf(" [t] Read Temperature\n\r");
138 xil_printf(" [r] Set Resolution\n\r");
139 xil_printf(" [h] Set THigh\n\r");
140 xil_printf(" [l] Set TLow\n\r");
141 xil_printf(" [c] Set TCrit\n\r");
142 xil_printf(" [y] Set THyst\n\r");
143 xil_printf(" [f] Set Fault Queue\n\r");
144 xil_printf(" [s] Display Settings \n\r");
145 xil_printf(" [m] Stop the program and display this menu\n\r");
146 xil_printf("Press key to select desired option\n\r");
147 xil_printf("Press [q] to exit the application\n\r");
148 }
149
150 /******************************************************************************
151 * @brief Displays Menu.
152 *
153 * @param None.
154 *
155 * @return None.
156 ******************************************************************************/
157 void ADT7420_DisplayMenu(void)
158 {
159 xil_printf("\n\rAvailable options: \n\r");
160 xil_printf(" [t] Read Temperature\n\r");
161 xil_printf(" [r] Set Resolution\n\r");
162 xil_printf(" [h] Set THigh\n\r");
163 xil_printf(" [l] Set TLow\n\r");
164 xil_printf(" [c] Set TCrit\n\r");
165 xil_printf(" [y] Set THyst\n\r");
166 xil_printf(" [f] Set Fault Queue\n\r");
167 xil_printf(" [s] Display Settings \n\r");
168 xil_printf(" [m] Stop the program and display this menu\n\r");
169
170 rxData = 0;
171 }
172
173 /******************************************************************************
174 * @brief Returns resolution of ADT7420 internal ADC.
175 *
176 * @param display - 0 -> resolution is displayed on UART
177 * - 1 -> resolution is not displayed on UART.
178 *
179 * @return (rxBuffer[0] & (1 << RESOLUTION)) - bit 7 of CONFIGURATION REGISTER
180 * - 0 -> resolution is 13 bits
181 * - 1 -> resolution is 16 bits.
182 ******************************************************************************/
183 unsigned char ADT7420_GetResolution(char display)
184 {
185 unsigned char rxBuffer[2] = {0x00, 0x00};
186
187 I2C_Read(I2C_BASEADDR, ADT7420_IIC_ADDR, CONFIG_REG, 1, rxBuffer);
188
189 if(display == 1)
190 {
191 if((rxBuffer[0] & (1 << RESOLUTION)) == 0)
192 {
193 xil_printf("Resolution is 13 bits (0.0625 C/LSB)\n\r");
194 }
195 else
196 {
197 xil_printf("Resolution is 16 bits (0.0078 C/LSB)\n\r");
198 }
199 }
200
201 return (rxBuffer[0] & (1 << RESOLUTION));
202 }
203
204 /******************************************************************************
205 * @brief Displays Set Resolution Menu.
206 *
207 * @param None.
208 *
209 * @return None.
210 ******************************************************************************/
211 void ADT7420_DisplayResolutionMenu(void)
212 {
213 xil_printf("\n\r>Set Resolution Menu\n\r");
214 xil_printf("-----------------------------------------\n\r");
215 xil_printf("Possible resolutions are:\n\r");
216 xil_printf(" 1. 13 bits (0.0625 C/LSB):\n\r");
217 xil_printf(" 2. 16 bits (0.0078 C/LSB):\n\r");
218 }
219
220 /******************************************************************************
221 * @brief Sets and displays resolution for ADC of ADT7420
222 *
223 * @param None
224 *
225 * @return None
226 ******************************************************************************/
227 void ADT7420_SetResolution(void)
228 {
229 unsigned char txBuffer[1] = { 0x00 };
230 char rx = 0;
231
232 // Disable interrupts in order to use normal polling
233 DisableInterrupts(UART_INTERRUPT);
234
235 ADT7420_DisplayResolutionMenu();
236
237 // Check if data is available on the UART
238 rx = UartReadChar();
239
240 switch (rx)
241 {
242 case '1' :
243 txBuffer[0] = (0 << RESOLUTION) | ADT7420_ReadConfigReg() ; // so as not to change other configuration parameters
244 xil_printf("Resolution is 13 bits (0.0625 C/LSB)\n\r");
245 TUpper = 0x1FFF;
246 rxData = 'm';
247 xil_printf("\n\r>Returning to Main Menu...\n\r");
248 break;
249 case '2' :
250 txBuffer[0] = (1 << RESOLUTION) | ADT7420_ReadConfigReg();
251 xil_printf("Resolution is 16 bits (0.0078 C/LSB)\n\r");
252 TUpper = 0xFFFF;
253 rxData = 'm';
254 xil_printf("\n\r>Returning to Main Menu...\n\r");
255 break;
256 default:
257 xil_printf("Wrong option!\n\r");
258 break;
259 }
260
261 I2C_Write(I2C_BASEADDR, ADT7420_IIC_ADDR, CONFIG_REG, 1, txBuffer);
262
263 // Enable interrupts
264 EnableInterrupts(UART_INTERRUPT);
265 }
266
267 /******************************************************************************
268 * @brief Reads data from the Temperature MSB and LSB registers of ADT7420.
269 *
270 * @param None.
271 *
272 * @return data - value read from the Temperature MSB and LSB registers.
273 ******************************************************************************/
274 int ADT7420_ReadTemp(void)
275 {
276 unsigned char rxBuffer[2] = {0x00,0x00};
277 int data = 0;
278
279 I2C_Read(I2C_BASEADDR, ADT7420_IIC_ADDR, TEMP_REG, 2, rxBuffer);
280
281 if(ADT7420_GetResolution(0) == 0)
282 {
283 data = (rxBuffer[0] << 5) | (rxBuffer[1] >> 3);
284 }
285 else
286 {
287 data = (rxBuffer[0] << 8) | (rxBuffer[1]);
288 }
289
290 return (data);
291 }
292
293 /******************************************************************************
294 * @brief Displays temperature data in degrees Celsius.
295 *
296 * @param data - value read from the Temperature MSB and LSB registers.
297 *
298 * @return None.
299 ******************************************************************************/
300 void Display_Temp(short int data)
301 {
302 float value = 0;
303 short int whole = 0;
304 short int thousands = 0;
305 float Vlsb_13 = 0.0625;
306 float Vlsb_16 = 0.0078;
307
308 // converting data for display
309 if(ADT7420_GetResolution(0) == 0)
310 {
311 if(data&0x1000)
312 {
313 data = data | 0xffffe000;
314 }
315 value = data * Vlsb_13;
316 }
317 else
318 {
319 value = data * Vlsb_16;
320 }
321
322 if(value >= 0)
323 {
324 whole = (int)value;
325 thousands = (value - whole) * 1000;
326
327 if(thousands < 10)
328 {
329 xil_printf("T = %d.00%1d C\n\r", whole,thousands);
330 }
331 else if(thousands < 100)
332 {
333 xil_printf("T = %d.0%2d C\n\r", whole,thousands);
334 }
335 else
336 {
337 xil_printf("T = %d.%3d C\n\r", whole,thousands);
338 }
339 }
340 else
341 {
342 value = value * (-1);
343 whole = (int)value;
344 thousands = (value - whole) * 1000;
345
346 if(thousands < 10)
347 {
348 xil_printf("T = -%d.00%1d C\n\r", whole,thousands);
349 }
350 else if(thousands < 100)
351 {
352 xil_printf("T = -%d.0%2d C\n\r", whole,thousands);
353 }
354 else
355 {
356 xil_printf("T = -%d.%3d C\n\r", whole,thousands);
357 }
358 }
359 }
360
361 /******************************************************************************
362 * @brief Reads data from the UART console.
363 *
364 * @param None.
365 *
366 * @return value -> data converted to hex value
367 * 0 -> characters read are not hex values.
368 ******************************************************************************/
369 int ADT7420_ConsoleRead(void)
370 {
371 char rx = 0;
372 char c[4] = "0000";
373 char *c_ptr;
374 int i = 0;
375 char cnt = 0;
376 int value = 0;
377
378 cnt = 0;
379 i = 0;
380 valid = 0;
381 value = 0;
382 c_ptr = c;
383
384 while(i < 6)
385 {
386 // Check if data is available on the UART
387 rx = UartReadChar();
388 xil_printf("%c", rx);
389 // Check if pressed key is [Enter]
390 if(rx == 0x0D)
391 {
392 i = 5;
393 }
394 else if(rx == 0x0A)
395 {
396 i = 5;
397 }
398 else if(((rx > 0x00)&&(rx < 0x30))|| // Not 0 - 9
399 ((rx > 0x39)&&(rx < 0x41))|| // Not A - F
400 ((rx > 0x46)&&(rx < 0x61))|| // Not a - f
401 (rx > 0x66))
402 {
403 xil_printf("\n\rCharacters entered must be HEX values (0 to 9 and A B C D E F)\n\r");
404 i = 6;
405 valid = 0;
406 }
407 else
408 {
409 *c_ptr++ = rx;
410 cnt = cnt + 1;
411 valid = 1;
412 }
413 if(cnt == 4)
414 {
415 i = 6;
416 }
417 i++;
418 }
419
420 // Translate from ASCII to hex
421 for(i = 0; i < cnt; i++)
422 {
423 if(c[i] > 0x60)
424 {
425 value = value * 16 + (c[i] - 0x57);
426 }
427 else if(c[i] > 0x39)
428 {
429 value = value * 16 + (c[i] - 0x37);
430 }
431 else
432 {
433 value = value * 16 + (c[i] - 0x30);
434 }
435 }
436
437 if(valid == 1)
438 {
439 return value;
440 }
441 else
442 {
443 return 0;
444 }
445 }
446
447 /******************************************************************************
448 * @brief Menu for setting data in THigh register.
449 *
450 * @param None.
451 *
452 * @return None.
453 ******************************************************************************/
454 void ADT7420_DisplaySetTHighMenu(void)
455 {
456 int THigh = 0;
457
458 // Disable interrupts in order to use normal polling
459 DisableInterrupts(UART_INTERRUPT);
460
461 xil_printf("\n\r>Set THigh Menu\n\r");
462 xil_printf("-----------------------------------------\n\r");
463
464 xil_printf("\n\rPlease enter a value between 0x0000 and 0x%4x: 0x", TUpper);
465
466 THigh = ADT7420_ConsoleRead();
467
468 while(!((THigh>=0x0000)&(THigh<=TUpper)))
469 {
470 xil_printf("\n\rValue for THigh must be in the range 0x0000 and 0x%4x\n\r", TUpper);
471 xil_printf("Please enter a valid value: 0x");
472 THigh = ADT7420_ConsoleRead();
473 }
474
475 if(valid == 1)
476 {
477 ADT7420_SetTHigh(THigh);
478 rxData = 'm';
479 xil_printf("\n\r\n\r>Returning to Main Menu...\n\r");
480 }
481
482 // Enable interrupts
483 EnableInterrupts(UART_INTERRUPT);
484 }
485
486 /******************************************************************************
487 * @brief Sets value of THigh register.
488 *
489 * @param THigh - value to be placed in the register.
490 *
491 * @return None.
492 ******************************************************************************/
493 void ADT7420_SetTHigh(int THigh)
494 {
495 unsigned char txBuffer[2] = {0x00, 0x00};
496
497 if(ADT7420_GetResolution(0) == 0)
498 {
499 txBuffer[0] = (THigh & 0x1FE0) >> 5;
500 txBuffer[1] = (THigh & 0x001F) << 3;
501 }
502 else
503 {
504 txBuffer[0] = (THigh & 0xFF00) >> 8;
505 txBuffer[1] = THigh & 0x00FF;
506 }
507
508 I2C_Write(I2C_BASEADDR, ADT7420_IIC_ADDR, TH_SETP_MSB, 2, txBuffer);
509 }
510
511 /******************************************************************************
512 * @brief Displays value of THigh setpoint.
513 *
514 * @param None.
515 *
516 * @return None.
517 ******************************************************************************/
518 void ADT7420_PrintTHigh(void)
519 {
520 unsigned char rxBuffer[2] = {0x00, 0x00};
521 int val = 0;
522
523 I2C_Read(I2C_BASEADDR, ADT7420_IIC_ADDR, TH_SETP_MSB, 2, rxBuffer);
524
525 if(ADT7420_GetResolution(0) == 0)
526 val = ( rxBuffer[0] << 5 ) | ( rxBuffer[1] >> 3);
527 else
528 val = (rxBuffer[0] << 8) | rxBuffer[1];
529
530 xil_printf("THigh Setpoint ");
531 Display_Temp(val);
532 }
533
534 /******************************************************************************
535 * @brief Menu for setting data in TLow register.
536 *
537 * @param None.
538 *
539 * @return None.
540 ******************************************************************************/
541 void ADT7420_DisplaySetTLowMenu(void)
542 {
543 int TLow = 0;
544
545 // Disable interrupts in order to use normal polling
546 DisableInterrupts(UART_INTERRUPT);
547
548 xil_printf("\n\r>Set TLow Menu\n\r");
549 xil_printf("-----------------------------------------\n\r");
550 xil_printf("\n\rPlease enter a value between 0x0000 and 0x%4x: 0x", TUpper);
551
552 TLow = ADT7420_ConsoleRead();
553
554 while(!((TLow>=0x0000)&(TLow<=TUpper)))
555 {
556 xil_printf("\n\rValue for TLow must be in the range 0x0000 and 0x%4x\n\r", TUpper);
557 xil_printf("Please enter a valid value: 0x");
558 TLow = ADT7420_ConsoleRead();
559 }
560
561 if(valid == 1)
562 {
563 ADT7420_SetTLow(TLow);
564 rxData = 'm';
565 xil_printf("\n\r\n\r>Returning to Main Menu...\n\r");
566 }
567
568 // Enable interrupts
569 EnableInterrupts(UART_INTERRUPT);
570 }
571
572 /******************************************************************************
573 * @brief Sets value of TLow register.
574 *
575 * @param TLow - value to be placed in the register.
576 *
577 * @return None.
578 ******************************************************************************/
579 void ADT7420_SetTLow(int TLow)
580 {
581 unsigned char txBuffer[2] = {0x00, 0x00};
582
583 if(ADT7420_GetResolution(0) == 0)
584 {
585 txBuffer[0] = (TLow & 0x1FE0) >> 5;
586 txBuffer[1] = (TLow & 0x001F) << 3;
587 }
588 else
589 {
590 txBuffer[0] = (TLow & 0xFF00) >> 8;
591 txBuffer[1] = TLow & 0x00FF;
592 }
593
594 I2C_Write(I2C_BASEADDR, ADT7420_IIC_ADDR, TL_SETP_MSB, 2, txBuffer);
595 }
596
597 /******************************************************************************
598 * @brief Displays value of TLow setpoint.
599 *
600 * @param None.
601 *
602 * @return None.
603 ******************************************************************************/
604 void ADT7420_PrintTLow(void)
605 {
606 unsigned char rxBuffer[2] = {0x00, 0x00};
607 int val = 0;
608
609 I2C_Read(I2C_BASEADDR, ADT7420_IIC_ADDR, TL_SETP_MSB, 2, rxBuffer);
610
611 if(ADT7420_GetResolution(0) == 0)
612 {
613 val = ( rxBuffer[0] << 5 ) | ( rxBuffer[1] >> 3);
614 }
615 else
616 {
617 val = (rxBuffer[0] << 8) | rxBuffer[1];
618 }
619
620 xil_printf("TLow Setpoint ");
621 Display_Temp(val);
622 }
623
624 /******************************************************************************
625 * @brief Sets value of TCrit register.
626 *
627 * @param TCrit - value to be placed in the register.
628 *
629 * @return None.
630 ******************************************************************************/
631 void ADT7420_SetTCrit(int TCrit)
632 {
633 unsigned char txBuffer[2] = {0x00, 0x00};
634
635 if(ADT7420_GetResolution(0) == 0)
636 {
637 txBuffer[0] = (TCrit & 0x1FE0) >> 5;
638 txBuffer[1] = (TCrit & 0x001F) << 3;
639 }
640 else
641 {
642 txBuffer[0] = (TCrit & 0xFF00) >> 8;
643 txBuffer[1] = TCrit & 0x00FF;
644 }
645
646 I2C_Write(I2C_BASEADDR, ADT7420_IIC_ADDR, TCRIT_SETP_MSB, 2, txBuffer);
647 }
648
649 /******************************************************************************
650 * @brief Menu for setting data in TCrit register.
651 *
652 * @param None.
653 *
654 * @return None.
655 ******************************************************************************/
656
657 void ADT7420_DisplaySetTCritMenu(void)
658 {
659 int TCrit = 0;
660
661 // Disable interrupts in order to use normal polling
662 DisableInterrupts(UART_INTERRUPT);
663
664 xil_printf("\n\r>Set TCrit Menu\n\r");
665 xil_printf("-----------------------------------------\n\r");
666 xil_printf("\n\rPlease enter a value between 0x0000 and 0x%4x: 0x", TUpper);
667
668 TCrit = ADT7420_ConsoleRead();
669
670 while(!((TCrit>=0x0000)&(TCrit<=TUpper)))
671 {
672 xil_printf("\n\rValue for TCrit must be in the range 0x0000 and 0x%4x\n\r", TUpper);
673 xil_printf("Please enter a valid value: 0x");
674 TCrit = ADT7420_ConsoleRead();
675 }
676
677 if(valid == 1)
678 {
679 ADT7420_SetTCrit(TCrit);
680 rxData = 'm';
681 xil_printf("\n\r\n\r>Returning to Main Menu...\n\r");
682 }
683
684 // Enable interrupts
685 EnableInterrupts(UART_INTERRUPT);
686 }
687
688 /******************************************************************************
689 * @brief Displays value of TCrit setpoint.
690 *
691 * @param None.
692 *
693 * @return None.
694 ******************************************************************************/
695 void ADT7420_PrintTCrit(void)
696 {
697 unsigned char rxBuffer[2] = {0x00, 0x00};
698 int val = 0;
699
700 I2C_Read(I2C_BASEADDR, ADT7420_IIC_ADDR, TCRIT_SETP_MSB, 2, rxBuffer);
701
702 if(ADT7420_GetResolution(0) == 0)
703 {
704 val = ( rxBuffer[0] << 5 ) | ( rxBuffer[1] >> 3);
705 }
706 else
707 {
708 val = (rxBuffer[0] << 8) | rxBuffer[1];
709 }
710
711 xil_printf("TCrit Setpoint ");
712 Display_Temp(val);
713 }
714
715 /******************************************************************************
716 * @brief Menu for setting data in THyst register.
717 *
718 * @param None.
719 *
720 * @return None.
721 ******************************************************************************/
722 void ADT7420_DisplaySetTHystMenu(void)
723 {
724 int THyst = 0;
725
726 // Disable interrupts in order to use normal polling
727 DisableInterrupts(UART_INTERRUPT);
728
729 xil_printf("\n\r>Set THyst Menu\n\r");
730 xil_printf("-----------------------------------------\n\r");
731 xil_printf("Enter a value from 0x0000 to 0x000F: 0x");
732
733 THyst = ADT7420_ConsoleRead();
734
735 while(!((THyst>=0)&(THyst<16)))
736 {
737 xil_printf("\n\rValue for THyst must be in the range 0 C to 15 C\n\r");
738 xil_printf("Please enter a valid value: 0x");
739 THyst = ADT7420_ConsoleRead();
740 }
741
742 if(valid == 1)
743 {
744 ADT7420_SetHysteresis(THyst);
745 rxData = 'm';
746 xil_printf("\n\r\n\r>Returning to Main Menu...\n\r");
747 }
748
749 // Enable interrupts
750 EnableInterrupts(UART_INTERRUPT);
751 }
752
753 /******************************************************************************
754 * @brief Sets value of THyst register.
755 *
756 * @param THyst - value from 0x0000 to 0x000F to be placed in the register.
757 *
758 * @return None.
759 ******************************************************************************/
760 void ADT7420_SetHysteresis(int THyst)
761 {
762 unsigned char txBuffer[1] = {0x00};
763
764 txBuffer[0] = THyst & 0x0F;
765 I2C_Write(I2C_BASEADDR, ADT7420_IIC_ADDR, T_HYST_SETP, 1, txBuffer);
766 }
767
768 /******************************************************************************
769 * @brief Displays value of THyst.
770 *
771 * @param None.
772 *
773 * @return None.
774 ******************************************************************************/
775 void ADT7420_PrintHysteresis(void)
776 {
777 unsigned char rxBuffer[2] = { 0x00 };
778
779 I2C_Read(I2C_BASEADDR, ADT7420_IIC_ADDR, T_HYST_SETP, 1, rxBuffer);
780 xil_printf("THyst Setpoint T = %d C\n\r", rxBuffer[0]);
781 }
782
783 /******************************************************************************
784 * @brief Menu for setting Fault Queue.
785 *
786 * @param None.
787 *
788 * @return None.
789 ******************************************************************************/
790 void ADT7420_DisplaySetFaultQueueMenu(void)
791 {
792 unsigned char txBuffer[1] = { 0x00 };
793 char rx = 0;
794
795 // Disable interrupts in order to use normal polling
796 DisableInterrupts(UART_INTERRUPT);
797
798 xil_printf("\n\r>Fault Queue Menu\n\r");
799 xil_printf("-----------------------------------------\n\r");
800 xil_printf("Number of fault queues:\n\r");
801 xil_printf(" 1. 1 fault\n\r");
802 xil_printf(" 2. 2 faults\n\r");
803 xil_printf(" 3. 3 faults\n\r");
804 xil_printf(" 4. 4 faults\n\r");
805
806 // Check if data is available on the UART
807 rx = UartReadChar();
808
809 switch (rx)
810 {
811 case '1' :
812 txBuffer[0] = 0x00 << FAULT_QUEUE;
813 xil_printf("1 fault queue\n\r");
814 rxData = 'm';
815 xil_printf("\n\r>Returning to Main Menu...\n\r");
816 break;
817 case '2' :
818 txBuffer[0] = 0x01 << FAULT_QUEUE;
819 xil_printf("2 fault queues\n\r");
820 rxData = 'm';
821 xil_printf("\n\r>Returning to Main Menu...\n\r");
822 break;
823 case '3' :
824 txBuffer[0] = 0x02 << FAULT_QUEUE;
825 xil_printf("3 fault queues\n\r");
826 rxData = 'm';
827 xil_printf("\n\r>Returning to Main Menu...\n\r");
828 break;
829 case '4' :
830 txBuffer[0] = 0x03 << FAULT_QUEUE;
831 xil_printf("4 fault queues\n\r");
832 rxData = 'm';
833 xil_printf("\n\r>Returning to Main Menu...\n\r");
834 break;
835 default:
836 xil_printf("Wrong option!\n\r");
837 break;
838 }
839 I2C_Write(I2C_BASEADDR, ADT7420_IIC_ADDR, CONFIG_REG, 1, txBuffer);
840
841 // Enable interrupts
842 EnableInterrupts(UART_INTERRUPT);
843 }
844
845 /******************************************************************************
846 * @brief Displays value of Fault Queues.
847 *
848 * @param None.
849 *
850 * @return None.
851 ******************************************************************************/
852 void ADT7420_PrintFaultQueue(void)
853 {
854 unsigned char rxBuffer[1] = { 0x00 };
855 char rx = 0;
856
857 I2C_Read(I2C_BASEADDR, ADT7420_IIC_ADDR, CONFIG_REG, 1, rxBuffer);
858
859 rx = rxBuffer[0] & (0x03 << FAULT_QUEUE);
860
861 switch (rx)
862 {
863 case 0x00 :
864 xil_printf("1 fault queue\n\r");
865 break;
866 case 0x01 :
867 xil_printf("2 fault queues\n\r");
868 break;
869 case 0x02 :
870 xil_printf("3 fault queues\n\r");
871 break;
872 case 0x03 :
873 xil_printf("4 fault queues\n\r");
874 break;
875 default:
876 break;
877 }
878 }
879
880 /******************************************************************************
881 * @brief Displays Alert Mode setting.
882 *
883 * @param None.
884 *
885 * @return None.
886 ******************************************************************************/
887 void ADT7420_PrintAlertMode(void)
888 {
889 unsigned char rxBuffer[2] = { 0x00 };
890
891 I2C_Read(I2C_BASEADDR, ADT7420_IIC_ADDR, CONFIG_REG, 1, rxBuffer);
892
893 if (rxBuffer[0] & (1 << INT_CT))
894 {
895 xil_printf("Alert Mode: Comparator\n\r");
896 }
897 else
898 {
899 xil_printf("Alert Mode: Interrupt\n\r");
900 }
901 }
902
903 /******************************************************************************
904 * @brief Displays output polarity setting for CT pin.
905 *
906 * @param None.
907 *
908 * @return None.
909 ******************************************************************************/
910 void ADT7420_PrintCTPolarity(void)
911 {
912 unsigned char rxBuffer[1] = { 0x00 };
913
914 I2C_Read(I2C_BASEADDR, ADT7420_IIC_ADDR, CONFIG_REG, 1, rxBuffer);
915
916 if (rxBuffer[0] & (1 << CT_POL))
917 {
918 xil_printf("CT pin is Active High\n\r");
919 }
920 else
921 {
922 xil_printf("CT pin is Active Low\n\r");
923 }
924 }
925
926
927 /******************************************************************************
928 * @brief Displays output polarity setting for INT pin.
929 *
930 * @param None.
931 *
932 * @return None.
933 ******************************************************************************/
934 void ADT7420_PrintINTPolarity(void)
935 {
936 unsigned char rxBuffer[1] = { 0x00 };
937
938 I2C_Read(I2C_BASEADDR, ADT7420_IIC_ADDR, CONFIG_REG, 1, rxBuffer);
939
940 if (rxBuffer[0] & (1 << INT_POL))
941 {
942 xil_printf("INT pin is Active High\n\r");
943 }
944 else
945 {
946 xil_printf("INT pin is Active Low\n\r");
947 }
948 }
949
950 /******************************************************************************
951 * @brief Displays current settings of the ADT7420.
952 *
953 * @param None.
954 *
955 * @return None.
956 ******************************************************************************/
957 void ADT7420_DisplaySettings(void)
958 {
959 // Disable interrupts in order to use normal polling
960 DisableInterrupts(UART_INTERRUPT);
961
962 xil_printf("\n\r ADT7420 Settings \n\r");
963 xil_printf("-----------------------------------------\n\r");
964 ADT7420_GetResolution(1);
965
966 ADT7420_PrintTHigh();
967 ADT7420_PrintTLow();
968 ADT7420_PrintTCrit();
969 ADT7420_PrintHysteresis();
970
971 ADT7420_PrintFaultQueue();
972
973 ADT7420_PrintAlertMode();
974 ADT7420_PrintCTPolarity();
975 ADT7420_PrintINTPolarity();
976
977 rxData = 'm';
978 // Enable interrupts
979 EnableInterrupts(UART_INTERRUPT);
980 xil_printf("\n\r>Returning to Main Menu...\n\r");
981 }
982
983 /******************************************************************************
984 * @brief Demo program for the ADT7420.
985 *
986 * @param None.
987 *
988 * @return None.
989 ******************************************************************************/
990 void DemoProgram(void)
991 {
992 // Display Main Menu on UART
993 ADT7420_DisplayMainMenu();
994
995 while(rxData != 'q')
996 {
997 switch(rxData)
998 {
999 case 't':
1000 Display_Temp(ADT7420_ReadTemp());
1001 break;
1002 case 'r':
1003 ADT7420_SetResolution();
1004 break;
1005 case 'h':
1006 ADT7420_DisplaySetTHighMenu();
1007 break;
1008 case 'l':
1009 ADT7420_DisplaySetTLowMenu();
1010 break;
1011 case 'c':
1012 ADT7420_DisplaySetTCritMenu();
1013 break;
1014 case 'y':
1015 ADT7420_DisplaySetTHystMenu();
1016 break;
1017 case 'f':
1018 ADT7420_DisplaySetFaultQueueMenu();
1019 break;
1020 case 's':
1021 ADT7420_DisplaySettings();
1022 break;
1023 case 'm':
1024 ADT7420_DisplayMenu();
1025 break;
1026 case 0:
1027 break;
1028 default:
1029 xil_printf("\n\rWrong option! Please select one of the options below.");
1030 ADT7420_DisplayMenu();
1031 break;
1032 }
1033 }
1034
1035 xil_printf("Exiting application\n\r");
1036
1037 }