Linduino  1.3.0
Linear Technology Arduino-Compatible Demonstration Board
EVAL-AD7124-8.ino
Go to the documentation of this file.
1 /***************************************************************************//**
2  * @file EVAL-AD7124-8.ino
3  * @brief Exerciser program for ad2174 no-OS driver
4  * @author Mthoren
5 ********************************************************************************
6  * Copyright 2017(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 #include <Arduino.h>
41 #include <stdint.h>
42 #include "Linduino.h"
43 #include "UserInterface.h"
44 #include "QuikEval_EEPROM.h"
45 #include <platform_drivers.h>
46 extern "C" {
47 #include <ad7124.h>
48 //#include <ad7124_regs.h> // un-comment to use default values from ad7124_regs.c
49 };
50 
51 /******************************************************************************/
52 /*********************** AD7124 initial configuration *************************/
53 /******************************************************************************/
54 
55 struct ad7124_st_reg my_ad7124_regs[AD7124_REG_NO] =
56 {
57  {0x00, 0x00, 1, 2}, /* AD7124_Status */
58  {0x01, 0x0000, 2, 1}, /* AD7124_ADC_Control */
59  {0x02, 0x0000, 3, 2}, /* AD7124_Data */
60  {0x03, 0x0000, 3, 1}, /* AD7124_IOCon1 */
61  {0x04, 0x0000, 2, 1}, /* AD7124_IOCon2 */
62  {0x05, 0x02, 1, 2}, /* AD7124_ID */
63  {0x06, 0x0000, 3, 2}, /* AD7124_Error */
64  {0x07, 0x0044, 3, 1}, /* AD7124_Error_En */
65  {0x08, 0x00, 1, 2}, /* AD7124_Mclk_Count */
66  {0x09, 0x8013, 2, 1}, /* AD7124_Channel_0 */
67  {0x0A, 0x0001, 2, 1}, /* AD7124_Channel_1 */
68  {0x0B, 0x0001, 2, 1}, /* AD7124_Channel_2 */
69  {0x0C, 0x0001, 2, 1}, /* AD7124_Channel_3 */
70  {0x0D, 0x0001, 2, 1}, /* AD7124_Channel_4 */
71  {0x0E, 0x0001, 2, 1}, /* AD7124_Channel_5 */
72  {0x0F, 0x0001, 2, 1}, /* AD7124_Channel_6 */
73  {0x10, 0x0001, 2, 1}, /* AD7124_Channel_7 */
74  {0x11, 0x0001, 2, 1}, /* AD7124_Channel_8 */
75  {0x12, 0x0001, 2, 1}, /* AD7124_Channel_9 */
76  {0x13, 0x0001, 2, 1}, /* AD7124_Channel_10 */
77  {0x14, 0x0001, 2, 1}, /* AD7124_Channel_11 */
78  {0x15, 0x0001, 2, 1}, /* AD7124_Channel_12 */
79  {0x16, 0x0001, 2, 1}, /* AD7124_Channel_13 */
80  {0x17, 0x0001, 2, 1}, /* AD7124_Channel_14 */
81  {0x18, 0x0001, 2, 1}, /* AD7124_Channel_15 */
82  {0x19, 0x0860, 2, 1}, /* AD7124_Config_0 */
83  {0x1A, 0x0860, 2, 1}, /* AD7124_Config_1 */
84  {0x1B, 0x0860, 2, 1}, /* AD7124_Config_2 */
85  {0x1C, 0x0860, 2, 1}, /* AD7124_Config_3 */
86  {0x1D, 0x0860, 2, 1}, /* AD7124_Config_4 */
87  {0x1E, 0x0860, 2, 1}, /* AD7124_Config_5 */
88  {0x1F, 0x0860, 2, 1}, /* AD7124_Config_6 */
89  {0x20, 0x0860, 2, 1}, /* AD7124_Config_7 */
90  {0x21, 0x060180, 3, 1}, /* AD7124_Filter_0 */
91  {0x22, 0x060180, 3, 1}, /* AD7124_Filter_1 */
92  {0x23, 0x060180, 3, 1}, /* AD7124_Filter_2 */
93  {0x24, 0x060180, 3, 1}, /* AD7124_Filter_3 */
94  {0x25, 0x060180, 3, 1}, /* AD7124_Filter_4 */
95  {0x26, 0x060180, 3, 1}, /* AD7124_Filter_5 */
96  {0x27, 0x060180, 3, 1}, /* AD7124_Filter_6 */
97  {0x28, 0x060180, 3, 1}, /* AD7124_Filter_7 */
98  {0x29, 0x800000, 3, 1}, /* AD7124_Offset_0 */
99  {0x2A, 0x800000, 3, 1}, /* AD7124_Offset_1 */
100  {0x2B, 0x800000, 3, 1}, /* AD7124_Offset_2 */
101  {0x2C, 0x800000, 3, 1}, /* AD7124_Offset_3 */
102  {0x2D, 0x800000, 3, 1}, /* AD7124_Offset_4 */
103  {0x2E, 0x800000, 3, 1}, /* AD7124_Offset_5 */
104  {0x2F, 0x800000, 3, 1}, /* AD7124_Offset_6 */
105  {0x30, 0x800000, 3, 1}, /* AD7124_Offset_7 */
106  {0x31, 0x500000, 3, 1}, /* AD7124_Gain_0 */
107  {0x32, 0x500000, 3, 1}, /* AD7124_Gain_1 */
108  {0x33, 0x500000, 3, 1}, /* AD7124_Gain_2 */
109  {0x34, 0x500000, 3, 1}, /* AD7124_Gain_3 */
110  {0x35, 0x500000, 3, 1}, /* AD7124_Gain_4 */
111  {0x36, 0x500000, 3, 1}, /* AD7124_Gain_5 */
112  {0x37, 0x500000, 3, 1}, /* AD7124_Gain_6 */
113  {0x38, 0x500000, 3, 1}, /* AD7124_Gain_7 */
114 };
115 
116 
117 
118 /******************************************************************************/
119 /************************** Variables Declarations ****************************/
120 /******************************************************************************/
121 
122 
124 {
125  GENERIC_SPI, // Type
126  QUIKEVAL_CS, // ID
127  1000000, // Max speed, Hz
128  3, // Mode
129  QUIKEVAL_CS, // CS ID
130 };
131 
132 ad7124_init_param init_params =
133 {
134  spi_params, // SPI parameters
135  my_ad7124_regs, // declared in ad7124_regs.c
136  64000, // spi_rdy_poll_cnt: # tries before timeout
137 };
138 
139 
140 struct ad7124_dev my_ad7124; /* A new driver instance */
141 struct ad7124_dev *ad7124_handler = &my_ad7124; /* A driver handle to pass around */
142 
143 int regNr; /* Variable to iterate through registers */
144 long timeout = 100; /* Number of tries before a function times out */
145 long ret = 0; /* Return value */
146 long sample; /* Stores raw value read from the ADC */
147 
148 
149 void setup()
150 {
151  char demo_name[] = "AD7124-8";
152 
153  int32_t x;
154 
155  Serial.begin(115200);
156 
157  // Give the serial port a chance to initialize
158  // Without this we get some garbage on the console
159  delay(100);
160 
161  if (false) // Set to true to dump initial values to screen
162  {
163  Serial.println(F("Print inital values..."));
164  for (regNr = AD7124_Status; (regNr < AD7124_REG_NO) && !(ret < 0); regNr++)
165  {
166  //ret = ad7124_read_register(ad7124_handler, &ad7124_regs[regNr]);
167  Serial.print("Register #: ");
168  Serial.print(regNr);
169  Serial.print(", Value: ");
170  x = my_ad7124_regs[regNr].value;
171  Serial.println(x);
172  }
173  }
174 
175  Serial.println(F("Attempting to initialize..."));
176  delay(50);
177 
178  /* Initialize AD7124 device. */
179  ret = ad7124_setup(&ad7124_handler, init_params);
180  if (ret < 0)
181  {
182  /* AD7124 initialization failed, check the value of ret! */
183  Serial.println(F("AD7124-8 not found! :("));
184  Serial.print(F("Return value: "));
185  Serial.println(ret);
186  }
187  else
188  {
189  /* AD7124 initialization OK */
190  Serial.println(F("AD7124-8 Initialized!! :)"));
191  }
192 
193  /* Read all registers */
194  for (regNr = AD7124_Status; (regNr < AD7124_REG_NO) && !(ret < 0); regNr++)
195  {
196  ret = ad7124_read_register(ad7124_handler, &my_ad7124_regs[regNr]);
197  Serial.print("Register #: ");
198  Serial.print(regNr);
199  Serial.print(", Value: ");
200  x = my_ad7124_regs[regNr].value;
201  Serial.println(x);
202  }
203 
204  /* Read data from the ADC */
205  ret = ad7124_wait_for_conv_ready(ad7124_handler, timeout);
206  if (ret < 0)
207  {
208  /* Something went wrong, check the value of ret! */
209  Serial.print("error waiting for conv ready: ");
210  Serial.println(ret);
211  }
212  ret = ad7124_read_data(ad7124_handler, &sample);
213  if (ret < 0)
214  {
215  /* Something went wrong, check the value of ret! */
216  Serial.print("error reading ADC data: ");
217  Serial.println(ret);
218  }
219  else
220  {
221  Serial.print("ADC data: ");
222  Serial.println(sample);
223  }
224 }
225 
226 void loop()
227 {
228  while (1)
229  {
230  delay(1000);
231  Serial.println("Looping...");
232  ret = ad7124_read_data(ad7124_handler, &sample);
233  Serial.print("ADC data: ");
234  Serial.println(sample);
235  }
236 }
long ret
struct ad7124_dev my_ad7124
spi_init_param spi_params
Header File for Linduino Libraries and Demo Code.
static void setup()
static void loop()
struct ad7124_st_reg my_ad7124_regs[AD7124_REG_NO]
static int regNr
struct ad7124_dev * ad7124_handler
Header file of Generic Platform Drivers.
QuikEval EEPROM Library.
long timeout
char demo_name[]
Demo Board Name stored in QuikEval EEPROM.
Definition: DC1880A.ino:97
long sample
ad7124_init_param init_params
#define QUIKEVAL_CS
QuikEval CS pin (SPI chip select on QuikEval connector pin 6) connects to Arduino SS pin...
Definition: Linduino.h:57