Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/Pmods/PmodACL2/ADXL362.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file ADXL362.c
3 * @brief Implementation of ADXL362 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 "ADXL362.h"
47
48 /*****************************************************************************/
49 /********************** Variable Definitions *********************************/
50 /*****************************************************************************/
51 volatile char rxData;
52 volatile char mode;
53
54 /******************************************************************************
55 * @brief Generates a delay in ms.
56 *
57 * @param ms_count - Number of ms desired.
58 *
59 * @return None.
60 ******************************************************************************/
61 void delay_ms(u32 ms_count)
62 {
63 u32 count;
64 for (count = 0; count < ((ms_count * 6670) + 1); count++)
65 {
66 asm("nop");
67 }
68 }
69
70 /******************************************************************************
71 * @brief Displays Main Menu.
72 *
73 * @param None.
74 *
75 * @return None.
76 ******************************************************************************/
77 void ADXL362_DisplayMainMenu(void)
78 {
79 xil_printf("\n\r\n\rPmod-ACL2 Demo Program\n\r");
80 xil_printf("\n\r");
81 xil_printf("Options: \n\r");
82 xil_printf(" [a] Display acceleration on All Axes\n\r");
83 xil_printf(" [x] Display acceleration on X Axis\n\r");
84 xil_printf(" [y] Display acceleration on Y Axis\n\r");
85 xil_printf(" [z] Display acceleration on Z Axis\n\r");
86 xil_printf(" [t] Display temperature\n\r");
87 xil_printf(" [r] Select measurement range\n\r");
88 xil_printf(" [s] Switch resolution\n\r");
89 xil_printf(" [i] Print device ID\n\r");
90 xil_printf(" [m] Display main menu\n\r");
91 xil_printf("Please select desired option\n\r");
92
93 rxData = 0;
94 }
95
96 /******************************************************************************
97 * @brief Displays Menu.
98 *
99 * @param None.
100 *
101 * @return None.
102 ******************************************************************************/
103 void ADXL362_DisplayMenu(void)
104 {
105 xil_printf("\n\rOptions: \n\r");
106 xil_printf(" [a] Display acceleration on All Axes\n\r");
107 xil_printf(" [x] Display acceleration on X Axis\n\r");
108 xil_printf(" [y] Display acceleration on Y Axis\n\r");
109 xil_printf(" [z] Display acceleration on Z Axis\n\r");
110 xil_printf(" [t] Display temperature\n\r");
111 xil_printf(" [r] Select measurement range\n\r");
112 xil_printf(" [s] Switch resolution\n\r");
113 xil_printf(" [i] Print device ID\n\r");
114 xil_printf(" [m] Display main menu\n\r");
115 xil_printf("Please select desired option\n\r");
116
117 rxData = 0;
118 }
119
120 /******************************************************************************
121 * @brief Displays a float value divided in whole and thousands.
122 *
123 * @param whole - whole value.
124 * @param thousands - thousands value.
125 * @param sign - 0 = +, 1 = -
126 * @param temp - 0 = acceleration, 1 = celsius
127 *
128 * @return None.
129 ******************************************************************************/
130 void ADXL362_Display(int whole, int thousands, int sign, char temp)
131 {
132 if(thousands > 99)
133 {
134 xil_printf("%c%d.%3d %c\n\r", (sign == 0)?'+':'-', whole, thousands, (temp == 0)?'g':'C');
135 }
136 else if(thousands > 9)
137 {
138 xil_printf("%c%d.0%2d %c\n\r", (sign == 0)?'+':'-', whole, thousands, (temp == 0)?'g':'C');
139 }
140 else
141 {
142 xil_printf("%c%d.00%1d %c\n\r", (sign == 0)?'+':'-', whole, thousands, (temp == 0)?'g':'C');
143 }
144 }
145
146 /******************************************************************************
147 * @brief Writes to an ADXL362 Register.
148 *
149 * @param addr - register address.
150 * @param data - data to be written
151 *
152 * @return None.
153 ******************************************************************************/
154 void ADXL362_WriteReg(char addr, char data)
155 {
156 char txBuffer[3] = {0x0A, addr, data};
157 char rxBuffer[3] = {0x00, 0x00, 0x00};
158
159 SPI_TransferData(SPI_BASEADDR, 3, txBuffer, 3, rxBuffer, 1);
160 }
161
162 /******************************************************************************
163 * @brief Reads from an ADXL362 Register.
164 *
165 * @param addr - register address.
166 *
167 * @return rx - data read from register.
168 ******************************************************************************/
169 char ADXL362_ReadReg(char addr)
170 {
171 char txBuffer[3] = {0x0B, addr, 0x00};
172 char rxBuffer[3] = {0x00, 0x00, 0x00};
173 char rx = 0x00;
174
175 SPI_TransferData(SPI_BASEADDR, 3, txBuffer, 3, rxBuffer, 1);
176
177 rx = rxBuffer[2];
178
179 return(rx);
180 }
181
182 /******************************************************************************
183 * @brief Prints ADXL362 internal ID Registers.
184 *
185 * @param None.
186 *
187 * @return None.
188 ******************************************************************************/
189 void ADXL362_PrintID(void)
190 {
191 xil_printf("\n\r");
192 xil_printf("Device ID: %x\n\r",ADXL362_ReadReg(ADXL362_DEVID_AD) & 0xFF);
193 xil_printf("Device ID: %x\n\r",ADXL362_ReadReg(ADXL362_DEVID_MST) & 0xFF);
194 xil_printf("Part ID: %x\n\r",ADXL362_ReadReg(ADXL362_PARTID) & 0xFF);
195 xil_printf("Silicon ID: %x\n\r",ADXL362_ReadReg(ADXL362_REVID) & 0xFF);
196
197 rxData = 0;
198
199 xil_printf("\n\r");
200 ADXL362_DisplayMenu();
201 }
202
203 /******************************************************************************
204 * @brief Reads temperature from ADXL362.
205 *
206 * @param None.
207 *
208 * @return Temperature value.
209 ******************************************************************************/
210 int ADXL362_ReadTemp(void)
211 {
212 char temp[2] = {0x00, 0x00};
213 int result = 0;
214
215 temp[0] = ADXL362_ReadReg(ADXL362_TEMP_H);
216 temp[1] = ADXL362_ReadReg(ADXL362_TEMP_L);
217
218 result = ((temp[0] & 0xFF) << 8) | ((temp[1]) & 0xFF);
219
220 return(result);
221 }
222
223 /******************************************************************************
224 * @brief Displays read temperature from ADXL362.
225 *
226 * @param None.
227 *
228 * @return None.
229 ******************************************************************************/
230 void ADXL362_PrintTemp(void)
231 {
232 float value = 0;
233 int rxTemp = 0;
234 char sign = 0;
235 int whole = 0;
236 int thousands = 0;
237
238 rxTemp = ADXL362_ReadTemp();
239 value = (float)((rxTemp & 0x7FF) * 0.065);
240 sign = (rxTemp & 0x800) >> 11;
241
242 whole = value;
243 thousands = (value - whole) * 1000;
244
245 xil_printf("\n\rADXL362 Temperature is: ");
246 ADXL362_Display(whole, thousands, sign, 1);
247 //xil_printf("\n\r");
248 rxData = 0;
249
250 xil_printf("\n\r");
251 ADXL362_DisplayMenu();
252 }
253
254 /******************************************************************************
255 * @brief Checks if ADXL362 Conversion is complete.
256 *
257 * @param None.
258 *
259 * @return 1 - Data is ready, 0 - Data is not ready.
260 ******************************************************************************/
261 char ADXL362_IsDataReady(void)
262 {
263 if((ADXL362_ReadReg(ADXL362_STATUS) & 0x01) == 0x01)
264 {
265 return(1);
266 }
267 else
268 {
269 return(0);
270 }
271 }
272
273 /******************************************************************************
274 * @brief Read X Axis 8 MSB.
275 *
276 * @param None.
277 *
278 * @return rx - read value.
279 ******************************************************************************/
280 char ADXL362_ReadXSmall(void)
281 {
282 char rx;
283
284 while(!ADXL362_IsDataReady());
285 rx = ADXL362_ReadReg(ADXL362_XDATA) & 0xFF;
286
287 return(rx);
288 }
289
290 /******************************************************************************
291 * @brief Read Y Axis 8 MSB.
292 *
293 * @param None.
294 *
295 * @return rx - read value.
296 ******************************************************************************/
297 char ADXL362_ReadYSmall(void)
298 {
299 char rx;
300
301 while(!ADXL362_IsDataReady());
302 rx = ADXL362_ReadReg(ADXL362_YDATA) & 0xFF;
303
304 return(rx);
305 }
306
307 /******************************************************************************
308 * @brief Read Z Axis 8 MSB.
309 *
310 * @param None.
311 *
312 * @return rx - read value.
313 ******************************************************************************/
314 char ADXL362_ReadZSmall(void)
315 {
316 char rx;
317
318 while(!ADXL362_IsDataReady());
319 rx = ADXL362_ReadReg(ADXL362_ZDATA) & 0xFF;
320
321 return(rx);
322 }
323
324 /******************************************************************************
325 * @brief Displays Axis read value 8 MSB.
326 *
327 * @param axis - which axis to print.
328 *
329 * @return None.
330 ******************************************************************************/
331 void ADXL362_PrintSmall(char axis)
332 {
333 int rxG = 0;
334 int sign = 0;
335 float value = 0;
336 int whole = 0;
337 int thousands = 0;
338
339 switch(axis)
340 {
341 case 'x':
342 rxG = ADXL362_ReadXSmall();
343 xil_printf("X = ");
344 break;
345 case 'y':
346 rxG = ADXL362_ReadYSmall();
347 xil_printf("Y = ");
348 break;
349 case 'z':
350 xil_printf("Z = ");
351 rxG = ADXL362_ReadZSmall();
352 break;
353 default:
354 break;
355 }
356
357 // Sign is MSB. If 1 -> Negative Number, else Positive Number
358 sign = (rxG & 0x80) >> 7;
359
360 if(sign == 1)
361 {
362 // If negative number, padding with FFFFF MSB's
363 rxG = (rxG << 4) | 0xFFFFF000;
364 }
365 else
366 {
367 rxG = (rxG << 4);
368 }
369
370 value = ((float)rxG / 1000);
371
372 if(rxG >= 0)
373 {
374 whole = value;
375 thousands = (value - whole) * 1000;
376 }
377 else
378 {
379 value = value * (-1);
380 whole = value;
381 thousands = (value - whole) * 1000;
382 }
383
384 ADXL362_Display(whole, thousands, sign, 0);
385 }
386
387 /******************************************************************************
388 * @brief Displays data read from all 3 axes, 8 MSB.
389 *
390 * @param None.
391 *
392 * @return None.
393 ******************************************************************************/
394 void ADXL362_DisplayAllSmall(void)
395 {
396 ADXL362_PrintSmall('x');
397 ADXL362_PrintSmall('y');
398 ADXL362_PrintSmall('z');
399 }
400
401 /******************************************************************************
402 * @brief Read X Axis 12 MSB.
403 *
404 * @param None.
405 *
406 * @return rx - read value.
407 ******************************************************************************/
408 int ADXL362_ReadX(void)
409 {
410 int rx;
411
412 while(!ADXL362_IsDataReady());
413 rx = ((ADXL362_ReadReg(ADXL362_XDATA_H) & 0xFF) << 8) | (ADXL362_ReadReg(ADXL362_XDATA_L) & 0xFF);
414
415
416 return(rx);
417 }
418
419 /******************************************************************************
420 * @brief Read Y Axis 12 MSB.
421 *
422 * @param None.
423 *
424 * @return rx - read value.
425 ******************************************************************************/
426 int ADXL362_ReadY(void)
427 {
428 int rx;
429
430 while(!ADXL362_IsDataReady());
431 rx = ((ADXL362_ReadReg(ADXL362_YDATA_H) & 0xFF) << 8) | (ADXL362_ReadReg(ADXL362_YDATA_L) & 0xFF);
432
433
434 return(rx);
435 }
436
437 /******************************************************************************
438 * @brief Read Z Axis 12 MSB.
439 *
440 * @param None.
441 *
442 * @return rx - read value.
443 ******************************************************************************/
444 int ADXL362_ReadZ(void)
445 {
446 int rx;
447
448 while(!ADXL362_IsDataReady());
449 rx = ((ADXL362_ReadReg(ADXL362_ZDATA_H) & 0xFF) << 8) | (ADXL362_ReadReg(ADXL362_ZDATA_L) & 0xFF);
450
451
452 return(rx);
453 }
454
455 /******************************************************************************
456 * @brief Displays read axis data 12 MSB.
457 *
458 * @param axis - which data to display.
459 *
460 * @return None.
461 ******************************************************************************/
462 void ADXL362_Print(char axis)
463 {
464 int rxG = 0;
465 char sign = 0;
466 float value = 0;
467 int whole = 0;
468 int thousands = 0;
469
470 switch(axis)
471 {
472 case 'x':
473 rxG = ADXL362_ReadX();
474 xil_printf("X = ");
475 break;
476 case 'y':
477 rxG = ADXL362_ReadY();
478 xil_printf("Y = ");
479 break;
480 case 'z':
481 rxG = ADXL362_ReadZ();
482 xil_printf("Z = ");
483 break;
484 default:
485 break;
486 }
487
488 // Sign is MSB. If 1 -> Negative Number, else Positive Number
489 sign = (rxG & 0x800) >> 11;
490
491 if (sign == 1)
492 {
493 // If negative number, padding with FFFFF MSB's
494 rxG = rxG | 0xFFFFF000;
495 }
496 else
497 {
498 rxG = rxG | 0x00000000;
499 }
500
501 value = ((float)rxG / 1000);
502
503 if(rxG >= 0)
504 {
505 whole = value;
506 thousands = (value - whole) * 1000;
507 }
508 else
509 {
510 value = value * (-1);
511 whole = value;
512 thousands = (value - whole) * 1000;
513 }
514
515 ADXL362_Display(whole, thousands, sign, 0);
516 }
517
518 /******************************************************************************
519 * @brief Displays data from all 3 axes, 12 MSB.
520 *
521 * @param None.
522 *
523 * @return None.
524 ******************************************************************************/
525 void ADXL362_DisplayAll(void)
526 {
527 ADXL362_Print('x');
528 ADXL362_Print('y');
529 ADXL362_Print('z');
530 }
531
532 /******************************************************************************
533 * @brief Switches between 8 bit and 12 bit resolution.
534 *
535 * @param None.
536 *
537 * @return None.
538 ******************************************************************************/
539 void ADXL362_SwitchRes(void)
540 {
541 mode = !mode;
542
543 if(mode == 0)
544 {
545 xil_printf("\n\r>Full 12-Bit resolution selected.\n\r");
546 }
547 else
548 {
549 xil_printf("\n\r>8-Bit resolution selected.\n\r");
550 }
551 rxData = 0;
552 xil_printf("\n\r");
553 ADXL362_DisplayMenu();
554 }
555
556 /******************************************************************************
557 * @brief Sets measurement range.
558 *
559 * @param None.
560 *
561 * @return None.
562 ******************************************************************************/
563 void ADXL362_SetRange(void)
564 {
565 char rx = 0;
566 char rxReg = 0;
567 xil_printf("\n\r>Select measurement range: \n\r");
568 xil_printf(" [1] +/- 2g\n\r");
569 xil_printf(" [2] +/- 4g\n\r");
570 xil_printf(" [3] +/- 8g\n\r");
571 xil_printf("Press [1] to [3] to select desired range\n\r");
572
573 DisableInterrupts(UART_INTERRUPT);
574
575 // Check if data is available on the UART
576 rx = UartReadChar();
577
578 rxReg = (ADXL362_ReadReg(ADXL362_FILTER_CTL) & 0x3F);
579
580 switch(rx)
581 {
582 case '1':
583 rxReg = 0x00 | rxReg;
584 xil_printf("> +/- 2g measurement range selected\n\r");
585 break;
586 case '2':
587 rxReg = 0x40 | rxReg;
588 xil_printf("> +/- 4g measurement range selected\n\r");
589 break;
590 case '3':
591 rxReg = 0x80 | rxReg;
592 xil_printf("> +/- 8g measurement range selected\n\r");
593 break;
594 default:
595 xil_printf("> wrong measurement range\n\r");
596 break;
597 }
598
599 ADXL362_WriteReg(ADXL362_FILTER_CTL, rxReg);
600
601 EnableInterrupts(UART_INTERRUPT);
602 rxData = 0;
603 xil_printf("\n\r");
604 ADXL362_DisplayMenu();
605
606 }
607
608 /******************************************************************************
609 * @brief Initialize ADXL362.
610 *
611 * @param None.
612 *
613 * @return None.
614 ******************************************************************************/
615 void ADXL362_Init(void)
616 {
617 // Software Reset
618 ADXL362_WriteReg(ADXL362_SOFT_RESET, ADXL362_RESET_CMD);
619 delay_ms(10);
620 ADXL362_WriteReg(ADXL362_SOFT_RESET, 0x00);
621
622 // Enable Measurement
623 ADXL362_WriteReg(ADXL362_POWER_CTL, (2 << ADXL362_MEASURE));
624 }
625
626 /******************************************************************************
627 * @brief Demo application for AD5541A
628 *
629 * @param None.
630 *
631 * @return None.
632 ******************************************************************************/
633 void DemoProgram(void)
634 {
635 ADXL362_DisplayMainMenu();
636
637 while(1)
638 {
639 switch(rxData)
640 {
641 case 'a':
642 (mode == 0) ? ADXL362_DisplayAll() : ADXL362_DisplayAllSmall();
643 delay_ms(100);
644 break;
645 case 'x':
646 (mode == 0) ? ADXL362_Print('x') : ADXL362_PrintSmall('x');
647 delay_ms(100);
648 break;
649 case 'y':
650 (mode == 0) ? ADXL362_Print('y') : ADXL362_PrintSmall('y');
651 delay_ms(100);
652 break;
653 case 'z':
654 (mode == 0) ? ADXL362_Print('z') : ADXL362_PrintSmall('z');
655 delay_ms(100);
656 break;
657 case 't':
658 ADXL362_PrintTemp();
659 break;
660 case 'r':
661 ADXL362_SetRange();
662 break;
663 case 's':
664 ADXL362_SwitchRes();
665 break;
666 case 'm':
667 ADXL362_DisplayMainMenu();
668 break;
669 case 'i':
670 ADXL362_PrintID();
671 break;
672 case 0:
673 break;
674 default:
675 xil_printf("\n\r> Wrong option! Please select one of the options below");
676 ADXL362_DisplayMenu();
677 break;
678 }
679 }
680 }
681