Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/Pmods/PmodACL/ADXL345.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file ADXL345.c
3 * @brief Implementation of ADXL345 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 /******************************************************************************/
44 /* Include Files */
45 /******************************************************************************/
46 #include <stdio.h>
47 #include "adxl345.h"
48 #include "system_config.h"
49
50 volatile char rxData;
51 volatile char isRunning = 0;
52 volatile int timeStepValue = 0;
53
54 #ifndef XPS_FPGA11_INT_ID
55 #define XPS_FPGA11_INT_ID 0
56 #endif
57
58 /******************************************************************************
59 * @brief Displays Main Menu.
60 *
61 * @param None.
62 *
63 * @return None.
64 ******************************************************************************/
65 void ADXL345_DisplayMainMenu(void)
66 {
67 xil_printf("\n\r\n\rPmodACL Demo Program\n\r");
68 xil_printf("\n\r");
69 xil_printf("Options:\n\r");
70 xil_printf(" [e] Enable Measurement\n\r");
71 xil_printf(" [d] Disable Measurement\n\r");
72 xil_printf(" [a] Display Acceleration\n\r");
73 xil_printf(" [s] Select Measurement Range\n\r");
74 xil_printf(" [r] Change acquisition rate\n\r");
75 xil_printf(" [t] Select Tap Interrupts\n\r");
76 xil_printf(" [q] Stop any on going action\n\r");
77 xil_printf("Please select desired option\n\r");
78 rxData = 0;
79 }
80
81 /******************************************************************************
82 * @brief Displays Menu.
83 *
84 * @param None.
85 *
86 * @return None.
87 ******************************************************************************/
88 void ADXL345_DisplayMenu(void)
89 {
90 xil_printf("\n\rOptions:\n\r");
91 xil_printf(" [e] Enable Measurement\n\r");
92 xil_printf(" [d] Disable Measurement\n\r");
93 xil_printf(" [a] Display Acceleration\n\r");
94 xil_printf(" [s] Select Measurement Range\n\r");
95 xil_printf(" [r] Change acquisition rate\n\r");
96 xil_printf(" [t] Select Tap Interrupts\n\r");
97 xil_printf(" [q] Stop any on going action\n\r");
98 xil_printf("Please select desired option\n\r");
99 rxData = 0;
100 }
101
102 /******************************************************************************
103 * @brief Displays Range Menu.
104 *
105 * @param None.
106 *
107 * @return None.
108 ******************************************************************************/
109 void ADXL345_DisplayRangeMenu(void)
110 {
111 xil_printf("\n\rMeasurement Range\n\r");
112 xil_printf("Options:\n\r");
113 xil_printf(" [1] +-2g\n\r");
114 xil_printf(" [2] +-4g\n\r");
115 xil_printf(" [3] +-8g\n\r");
116 xil_printf(" [4] +-16g\n\r");
117 xil_printf("Press [1] to [4] to select desired range\n\r");
118 }
119
120 /******************************************************************************
121 * @brief Displays Rate Menu.
122 *
123 * @param None.
124 *
125 * @return None.
126 ******************************************************************************/
127 void ADXL345_DisplayRateMenu(void)
128 {
129 xil_printf("\n\rAcquisition Rate\n\r");
130 xil_printf("Options:\n\r");
131 xil_printf(" [1] 6.25 Hz\n\r");
132 xil_printf(" [2] 12.5 Hz\n\r");
133 xil_printf(" [3] 25 Hz\n\r");
134 xil_printf(" [4] 50 Hz\n\r");
135 xil_printf(" [5] 100 Hz\n\r");
136 xil_printf(" [6] 200 Hz\n\r");
137 xil_printf(" [7] 400 Hz\n\r");
138 xil_printf(" [8] 800 Hz\n\r");
139 xil_printf(" [9] 1600 Hz\n\r");
140 xil_printf("Press [1] to [9] to select desired range\n\r");
141 }
142
143 /******************************************************************************
144 * @brief Displays Tap Menu.
145 *
146 * @param None.
147 *
148 * @return None.
149 ******************************************************************************/
150 void ADXL345_DisplayTapMenu(void)
151 {
152 xil_printf("\n\rTap Menu\n\r");
153 xil_printf("Options:\n\r");
154 xil_printf(" [1] Single Tap\n\r");
155 xil_printf(" [2] Double Tap\n\r");
156 xil_printf(" [3] Single and Double Tap\n\r");
157 xil_printf(" [4] Tap Off\n\r");
158 xil_printf("Press [1] to [4] to select desired range\n\r");
159 }
160
161 /******************************************************************************
162 * @brief Writes to a ADXL345 Internal Register.
163 *
164 * @param addr - Register address.
165 * @param txData - Data to be sent.
166 *
167 * @return None.
168 ******************************************************************************/
169 void ADXL345_WriteReg(unsigned char addr, unsigned char txData)
170 {
171 char txBuffer[2] = {0x00, 0x00};
172 char rxBuffer[2] = {0x00, 0x00};
173
174 txBuffer[0] = addr & 0x3F;
175 txBuffer[1] = txData;
176
177 SPI_TransferData(SPI_BASEADDR, 2, txBuffer, 2, rxBuffer, 1);
178 }
179
180 /******************************************************************************
181 * @brief Reads from a ADXL345 Internal Register.
182 *
183 * @param addr - Register address.
184 *
185 * @return rxData - Data read from ADXL345.
186 ******************************************************************************/
187 int ADXL345_ReadReg(unsigned char addr)
188 {
189 char txBuffer[2] = {0x00, 0x00};
190 char rxBuffer[2] = {0x00, 0x00};
191 char rxData = 0;
192
193 txBuffer[0] = (1 << ADXL345_RnW) | (addr & 0x3F);
194 txBuffer[1] = 0x00;
195
196 SPI_TransferData(SPI_BASEADDR, 2, txBuffer, 2, rxBuffer, 1);
197
198 rxData = (rxBuffer[1] & 0xFF);
199
200 return(rxData);
201 }
202
203 /******************************************************************************
204 * @brief Burst reads from a ADXL345 Internal Register.
205 *
206 * @param addr - First Register address.
207 *
208 * @return rxData - Data read from ADXL345.
209 ******************************************************************************/
210 int ADXL345_BurstReadReg(unsigned char addr)
211 {
212 char txBuffer[3] = {0x00, 0x00, 0x00};
213 char rxBuffer[3] = {0x00, 0x00, 0x00};
214 int rxData = 0;
215
216 txBuffer[0] = (1 << ADXL345_RnW) | (1 << ADXL345_MB) | (addr & 0x3F);
217 txBuffer[1] = 0x00;
218
219 SPI_TransferData(SPI_BASEADDR, 3, txBuffer, 3, rxBuffer, 1);
220
221 rxData = ((rxBuffer[2] & 0xFF) << 8) | (rxBuffer[1] & 0xFF);
222
223 return(rxData);
224 }
225
226 /******************************************************************************
227 * @brief Initialize ADXL345.
228 *
229 * @param None.
230 *
231 * @return None.
232 ******************************************************************************/
233 void ADXL345_Init(void)
234 {
235 ADXL345_WriteReg(ADXL345_BW_RATE, (0x07 << ADXL345_RATE)); // Data update rate = 100 Hz
236 ADXL345_WriteReg(ADXL345_DATA_FORMAT, ((1 << ADXL345_FULL_RES) | // Data format = +- 16g, Full Resolution
237 (3 << ADXL345_Range)));
238 ADXL345_WriteReg(ADXL345_INT_ENABLE, ((1 << ADXL345_SINGLE_TAP) | // Enable Single, Double Tap
239 (1 << ADXL345_DOUBLE_TAP) |
240 (0 << ADXL345_Activity) |
241 (0 << ADXL345_FREE_FALL) |
242 (0 << ADXL345_DATA_READY)));
243 ADXL345_WriteReg(ADXL345_THRESH_TAP, 0x20); // Tap threshold
244 ADXL345_WriteReg(ADXL345_DUR, 0x0D); // Tap duration
245 ADXL345_WriteReg(ADXL345_Latent, 0x50); // Tap latency
246 ADXL345_WriteReg(ADXL345_Window, 0xF0); // Tap Window
247 ADXL345_WriteReg(ADXL345_TAP_AXES, ((1 << ADXL345_TAP_X_en) | // Enable tap on all axes
248 (1 << ADXL345_TAP_Y_en) |
249 (1 << ADXL345_TAP_Z_en)));
250 ADXL345_WriteReg(ADXL345_THRESH_ACT, 0x08); // Activity threshold
251 ADXL345_WriteReg(ADXL345_ACT_INACT_CTL, ((1 << ADXL345_ACT_X_en)| // Activity axes
252 (1 << ADXL345_ACT_Y_en) |
253 (1 << ADXL345_ACT_Z_en)));
254 ADXL345_WriteReg(ADXL345_THRESH_FF, 0x08); // Free fall threshold
255 ADXL345_WriteReg(ADXL345_TIME_FF, 0x0A); // Free fall window
256 ADXL345_WriteReg(ADXL345_FIFO_CTL, 0x01);
257
258 ADXL345_ReadAllAxes();
259
260 if(USE_PS7 == 1)
261 {
262 timeStepValue = 0x1A7D;
263 Xil_Out32(LED_BASEADDR + 0x288, (1 << 19) | (1 << 20));
264 Xil_Out32(LED_BASEADDR + 0x284, (1 << 19) | (1 << 20));
265 }
266 else
267 {
268 timeStepValue = 0xFA0A1F01;
269 }
270 }
271
272 /******************************************************************************
273 * @brief Display g force for desired axis.
274 *
275 * @param axis - Which axis to read.
276 *
277 * @return None.
278 ******************************************************************************/
279 void ADXL345_Display_G_Force(char axis)
280 {
281 int gForce = 0;
282 int gForceCalc = 0;
283 float value = 0;
284 int whole = 0;
285 int thousands = 0;
286
287 // Select which axis to read
288 switch(axis)
289 {
290 case 'x':
291 gForce = ADXL345_BurstReadReg(ADXL345_DATA_X0);
292 xil_printf("[X = ");
293 break;
294 case 'y':
295 gForce = ADXL345_BurstReadReg(ADXL345_DATA_Y0);
296 xil_printf("[Y = ");
297 break;
298 case 'z':
299 gForce = ADXL345_BurstReadReg(ADXL345_DATA_Z0);
300 xil_printf("[Z = ");
301 break;
302 default:
303 break;
304 }
305
306 // If read result is negative, apply padding with 0xFFFFF000
307 if((gForce & 0x1000) == 0x1000)
308 {
309 gForceCalc = 0xFFFFF000 | gForce;
310 }
311 // If read result is positive, do nothing to modify it
312 else
313 {
314 gForceCalc = gForce & 0x0FFF;
315 }
316
317 // Apply HEX to Accel data processing
318 value = ((float)gForceCalc * 0.004);
319
320 // Prepare and display data
321 // If positive, leave as is
322 if(gForceCalc >= 0)
323 {
324 whole = value;
325 thousands = (value - whole) * 1000;
326
327 if(thousands > 99)
328 {
329 xil_printf("+%d.%3d] ", whole, thousands);
330 }
331 else if(thousands > 9)
332 {
333 xil_printf("+%d.0%2d] ", whole, thousands);
334 }
335 else
336 {
337 xil_printf("+%d.00%1d] ", whole, thousands);
338 }
339 }
340 // If negative, multiply by (-1) for proper display
341 else
342 {
343 value = value * (-1);
344 whole = value;
345 thousands = (value - whole) * 1000;
346
347 if(thousands > 99)
348 {
349 xil_printf("-%d.%3d] ", whole, thousands);
350 }
351 else if(thousands > 9)
352 {
353 xil_printf("-%d.0%2d] ", whole, thousands);
354 }
355 else
356 {
357 xil_printf("-%d.00%1d] ", whole, thousands);
358 }
359 }
360 }
361
362 /******************************************************************************
363 * @brief Display all axes.
364 *
365 * @param None.
366 *
367 * @return None.
368 ******************************************************************************/
369 void ADXL345_DisplayAllAxes(void)
370 {
371 ADXL345_Display_G_Force('x');
372 ADXL345_Display_G_Force('y');
373 ADXL345_Display_G_Force('z');
374 xil_printf("\n\r");
375 }
376
377 /******************************************************************************
378 * @brief Read all axes.
379 *
380 * @param None.
381 *
382 * @return None.
383 ******************************************************************************/
384 void ADXL345_ReadAllAxes(void)
385 {
386 ADXL345_BurstReadReg(ADXL345_DATA_X0);
387 ADXL345_BurstReadReg(ADXL345_DATA_Y0);
388 ADXL345_BurstReadReg(ADXL345_DATA_Z0);
389 }
390
391 /******************************************************************************
392 * @brief Run measurements.
393 *
394 * @param None.
395 *
396 * @return None.
397 ******************************************************************************/
398 void ADXL345_Run(void)
399 {
400 if(isRunning == 0)
401 {
402 ADXL345_WriteReg(ADXL345_POWER_CTL, (1 << ADXL345_Measure));
403 isRunning = 1;
404 }
405 xil_printf("\n\r>ADXL345 Measurement Enabled!\n\r\n\r");
406 ADXL345_DisplayMenu();
407 }
408
409 /******************************************************************************
410 * @brief Stop measurements.
411 *
412 * @param None.
413 *
414 * @return None.
415 ******************************************************************************/
416 void ADXL345_Stop(void)
417 {
418 if(isRunning == 1)
419 {
420 ADXL345_WriteReg(ADXL345_POWER_CTL, (0 << ADXL345_Measure));
421 isRunning = 0;
422 }
423 xil_printf("\n\r>ADXL345 Measurement Disabled!\n\r\n\r");
424 ADXL345_DisplayMenu();
425 }
426
427 /******************************************************************************
428 * @brief Displays acceleration on all 3 axes.
429 *
430 * @param None.
431 *
432 * @return None.
433 ******************************************************************************/
434 void ADXL345_DisplayAccel(void)
435 {
436 char rxValue = 0x00;
437 if(isRunning == 1)
438 {
439 DisableInterrupts(XPS_FPGA11_INT_ID);
440
441 rxValue = ADXL345_ReadReg(ADXL345_INT_SOURCE);
442 if(rxValue & 0x80)
443 {
444 ADXL345_DisplayAllAxes();
445 }
446 if(rxValue & 0x40)
447 {
448 if(USE_PS7 == 1)
449 {
450 // Turn ON LED 1
451 GpioOut(LED_BASEADDR, (1 << 19));
452 }
453 else
454 {
455 // Turn ON LED 1
456 GpioOut(LED_BASEADDR, 0x01);
457 }
458 // Load Timer with 1 second value
459 LoadTimer(timeStepValue);
460 // Start Timer
461 StartTimer();
462 }
463 else if(rxValue & 0x20)
464 {
465 if(USE_PS7 == 1)
466 {
467 // Turn ON LED 1 and LED2
468 GpioOut(LED_BASEADDR, (3 << 19));
469 }
470 else
471 {
472 // Turn ON LED 1 and LED2
473 GpioOut(LED_BASEADDR, 0x03);
474 }
475 // Load Timer with 1 second value
476 LoadTimer(timeStepValue);
477 // Start Timer
478 StartTimer();
479 }
480 EnableInterrupts(XPS_FPGA11_INT_ID);
481 }
482 else if(isRunning == 0)
483 {
484 xil_printf("\n\r!!! Measurement not enabled. Please enable measurement before performing current action !!!\n\r");
485 ADXL345_DisplayMenu();
486 }
487
488 }
489
490 /******************************************************************************
491 * @brief Select measurement range.
492 *
493 * @param None.
494 *
495 * @return None.
496 ******************************************************************************/
497 void ADXL345_SelectRange(void)
498 {
499 char rx = 0;
500 char rxReg = 0;
501
502 ADXL345_DisplayRangeMenu();
503
504 // Disable Interrupts in order to use polling mode
505 DisableInterrupts(UART_INTERRUPT);
506
507 // Check if data is available on the UART
508 rx = UartReadChar();
509
510 // Convert ASCII format to desired one
511 rx = rx - 0x31;
512
513 if ((rx >= 0x00) && (rx <= 0x03))
514 {
515 rxReg = (ADXL345_ReadReg(ADXL345_DATA_FORMAT) & 0xFC) | rx;
516
517 // Change DATA_FORMAT Register value
518 ADXL345_WriteReg(ADXL345_DATA_FORMAT, rxReg);
519 }
520
521 // Display new settings
522 switch(rx)
523 {
524 case 0:
525 xil_printf("> +-2g range selected\n\r");
526 break;
527 case 1:
528 xil_printf("> +-4g range selected\n\r");
529 break;
530 case 2:
531 xil_printf("> +-8g range selected\n\r");
532 break;
533 case 3:
534 xil_printf("> +-16g range selected\n\r");
535 break;
536 default:
537 xil_printf("> wrong measurement range\n\r");
538 break;
539 }
540
541 // Await new command after exiting function
542 rxData = 'q';
543
544 // Enable Interrupts
545 EnableInterrupts(UART_INTERRUPT);
546
547 ADXL345_DisplayMenu();
548 }
549
550 /******************************************************************************
551 * @brief Select Acquisition Rate.
552 *
553 * @param None.
554 *
555 * @return None.
556 ******************************************************************************/
557 void ADXL345_AcquisitionRate(void)
558 {
559 char rx = 0;
560 char rxReg = 0;
561
562 ADXL345_DisplayRateMenu();
563
564 // Disable Interrupts in order to use polling mode
565 DisableInterrupts(UART_INTERRUPT);
566
567 // Check if data is available on the UART
568 rx = UartReadChar();
569
570 // Convert ASCII format to desired one
571 rx = rx - 0x30 + 0x05;
572
573 if ((rx >= 0x06) && (rx <= 0x0E))
574 {
575 rxReg = (ADXL345_ReadReg(ADXL345_BW_RATE) & 0xF0) | rx;
576
577 // Change BW_RATE Register Value
578 ADXL345_WriteReg(ADXL345_BW_RATE, rxReg);
579 }
580 // Display new settings
581 switch(rx)
582 {
583 case 0x06:
584 xil_printf("> 6.25 Hz selected\n\r");
585 break;
586 case 0x07:
587 xil_printf("> 12.5 Hz selected\n\r");
588 break;
589 case 0x08:
590 xil_printf("> 25 Hz selected\n\r");
591 break;
592 case 0x09:
593 xil_printf("> 50 Hz selected\n\r");
594 break;
595 case 0x0A:
596 xil_printf("> 100 Hz selected\n\r");
597 break;
598 case 0x0B:
599 xil_printf("> 200 Hz selected\n\r");
600 break;
601 case 0x0C:
602 xil_printf("> 400 Hz selected\n\r");
603 break;
604 case 0x0D:
605 xil_printf("> 800 Hz selected\n\r");
606 break;
607 case 0x0E:
608 xil_printf("> 1600 Hz selected\n\r");
609 break;
610 default:
611 xil_printf("> wrong acquisition rate\n\r");
612 break;
613 }
614
615 // Await new command after exiting function
616 rxData = 'q';
617
618 // Enable Interrupts
619 EnableInterrupts(UART_INTERRUPT);
620
621 ADXL345_DisplayMenu();
622 }
623
624 /******************************************************************************
625 * @brief Select tap type.
626 *
627 * @param None.
628 *
629 * @return None.
630 ******************************************************************************/
631 void ADXL345_SelectTap(void)
632 {
633 char rx = 0;
634 char rxReg = 0;
635
636 ADXL345_DisplayTapMenu();
637
638 // Disable Interrupts in order to use polling mode
639 DisableInterrupts(UART_INTERRUPT);
640
641 // Check if data is available on the UART
642 rx = UartReadChar();
643
644 // Enable and display desired options
645 switch(rx)
646 {
647 case '1':
648 rxReg = (ADXL345_ReadReg(ADXL345_INT_ENABLE) & 0x9F) | (1 << ADXL345_SINGLE_TAP);
649 ADXL345_WriteReg(ADXL345_INT_ENABLE, rxReg);
650 xil_printf("> Single Tap selected\n\r");
651 break;
652 case '2':
653 rxReg = (ADXL345_ReadReg(ADXL345_INT_ENABLE) & 0x9F) | (1 << ADXL345_DOUBLE_TAP);
654 ADXL345_WriteReg(ADXL345_INT_ENABLE, rxReg);
655 xil_printf("> Double Tap selected\n\r");
656 break;
657 case '3':
658 rxReg = (ADXL345_ReadReg(ADXL345_INT_ENABLE) & 0x9F) | (1 << ADXL345_SINGLE_TAP) | (1 << ADXL345_DOUBLE_TAP);
659 ADXL345_WriteReg(ADXL345_INT_ENABLE, rxReg);
660 xil_printf("> Single and Double Tap selected\n\r");
661 break;
662 case '4':
663 rxReg = ADXL345_ReadReg(ADXL345_INT_ENABLE) & 0x9F;
664 ADXL345_WriteReg(ADXL345_INT_ENABLE, rxReg);
665 xil_printf("> Tap Off\n\r");
666 break;
667 default:
668 xil_printf("> wrong tap option\n\r");
669 break;
670 }
671
672 // Await new command after exiting function
673 rxData = 'q';
674
675 // Enable Interrupts
676 EnableInterrupts(UART_INTERRUPT);
677
678 ADXL345_DisplayMenu();
679 }
680
681 /******************************************************************************
682 * @brief Timer interrupt function.
683 *
684 * @param None.
685 *
686 * @return None.
687 ******************************************************************************/
688 void ADITimerIntrFunction(void)
689 {
690 if(USE_PS7 == 1)
691 {
692 // Turn OFF LEDs
693 GpioOut(LED_BASEADDR, 0);
694 // Stop Timer
695 StopTimer();
696 }
697 else
698 {
699 // Turn OFF LEDs
700 GpioOut(LED_BASEADDR, 0);
701 // Stop Timer
702 StopTimer();
703 // Clear Timer Interrupts
704 Xil_Out32(TIMER_BASEADDR + 0x00, 0x00000100);
705 }
706 }
707
708 /******************************************************************************
709 * @brief External interrupt function.
710 *
711 * @param None.
712 *
713 * @return None.
714 ******************************************************************************/
715 void ADIExtIntrFunction(void)
716 {
717 unsigned char intSource = 0;
718 unsigned char intEnabled = 0;
719
720 // Read ADXL345 Interrupt Source
721 intSource = ADXL345_ReadReg(ADXL345_INT_SOURCE);
722 // Read ADXL345 Enabled Interrupts
723 intEnabled = ADXL345_ReadReg(ADXL345_INT_ENABLE);
724
725 // If Interrupt Enabled and Triggered is Single Tap
726 if((intSource & 0x40) && (intEnabled & 0x40))
727 {
728 if(USE_PS7 == 1)
729 {
730 // Turn ON LED 1
731 GpioOut(LED_BASEADDR, (1 << 19));
732 }
733 else
734 {
735 // Turn ON LED 1
736 GpioOut(LED_BASEADDR, 0x01);
737 }
738 // Load Timer with 1 second value
739 LoadTimer(timeStepValue);
740 // Start Timer
741 StartTimer();
742 }
743 // If Interrupt Enabled and Triggered is Double Tap
744 if((intSource & 0x20) && (intEnabled & 0x20))
745 {
746 if(USE_PS7 == 1)
747 {
748 // Turn ON LED 1 and LED2
749 GpioOut(LED_BASEADDR, (3 << 19));
750 }
751 else
752 {
753 // Turn ON LED 1 and LED2
754 GpioOut(LED_BASEADDR, 0x03);
755 }
756 // Load Timer with 1 second value
757 LoadTimer(timeStepValue);
758 // Start Timer
759 StartTimer();
760 }
761
762 // Clear interrupts by reading all axes registers
763 ADXL345_ReadAllAxes();
764 if(USE_PS7 == 0)
765 {
766 // Clear External Interrupts
767 Xil_Out32(EXT_BASEADDR + 0x120, 0x01);
768 }
769 }
770
771 /******************************************************************************
772 * @brief Demo Program for ADXL345.
773 *
774 * @param None.
775 *
776 * @return None.
777 ******************************************************************************/
778 void DemoProgram(void)
779 {
780 // Display Main Menu
781 ADXL345_DisplayMainMenu();
782
783 while(1)
784 {
785 switch(rxData)
786 {
787 case 'e':
788 ADXL345_Run();
789 break;
790 case 'd':
791 ADXL345_Stop();
792 break;
793 case 'a':
794 ADXL345_DisplayAccel();
795 break;
796 case 's':
797 ADXL345_SelectRange();
798 break;
799 case 'r':
800 ADXL345_AcquisitionRate();
801 break;
802 case 't':
803 ADXL345_SelectTap();
804 break;
805 case 'q':
806 ADXL345_DisplayMainMenu();
807 break;
808 case 0:
809 break;
810 default:
811 xil_printf("\n> Wrong option! Please select one of the options below\r");
812 ADXL345_DisplayMenu();
813 break;
814 }
815 }
816 }