Petaluma (MAXREFDES30#) Code Documentation  V01.00
16-Bit, 8-Ch, +/-10V input, Simultaneous-Sampling Analog Front End
 All Data Structures Files Functions Variables Macros Pages
maximDeviceSpecificUtilities.c
Go to the documentation of this file.
1 
31 /*
32  * Copyright (C) 2012 Maxim Integrated Products, Inc., All Rights Reserved.
33  *
34  * Permission is hereby granted, free of charge, to any person obtaining a
35  * copy of this software and associated documentation files (the "Software"),
36  * to deal in the Software without restriction, including without limitation
37  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
38  * and/or sell copies of the Software, and to permit persons to whom the
39  * Software is furnished to do so, subject to the following conditions:
40  *
41  * The above copyright notice and this permission notice shall be included
42  * in all copies or substantial portions of the Software.
43  *
44  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
45  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
46  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
47  * IN NO EVENT SHALL MAXIM INTEGRATED PRODUCTS BE LIABLE FOR ANY CLAIM, DAMAGES
48  * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
49  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
50  * OTHER DEALINGS IN THE SOFTWARE.
51  *
52  * Except as contained in this notice, the name of Maxim Integrated Products
53  * shall not be used except as stated in the Maxim Integrated Products
54  * Branding Policy.
55  *
56  * The mere transfer of this software does not imply any licenses
57  * of trade secrets, proprietary technology, copyrights, patents,
58  * trademarks, maskwork rights, or any other form of intellectual
59  * property whatsoever. Maxim Integrated Products retains all ownership rights.
60  *
61  ***************************************************************************/
62 
63 #include "xbasic_types.h"
64 #include "stdio.h"
65 #include "xscugic.h"
66 #include "axi_max11046.h" //AXI_MAX11046 custom ip core driver
67 
68 #include "utilities.h"
69 
70 #define INTC_DEVICE_INT_ID 91 //interrupt ID for the AXI_MAX11046 IP core defined in the XPS
71 
72 static void ReadADCHandler(void *CallBackRef);
73 int SetupInterruptSystem(XScuGic *IntcInstancePtr);
74 
75 u32 g_unCount=0;
76 u16 *g_auSamplesCh0; //a pointer that stores the sampled data
77 u16 *g_auSamplesCh1; //a pointer that stores the sampled data
78 u16 *g_auSamplesCh2; //a pointer that stores the sampled data
79 u16 *g_auSamplesCh3; //a pointer that stores the sampled data
80 u16 *g_auSamplesCh4; //a pointer that stores the sampled data
81 u16 *g_auSamplesCh5; //a pointer that stores the sampled data
82 u16 *g_auSamplesCh6; //a pointer that stores the sampled data
83 u16 *g_auSamplesCh7; //a pointer that stores the sampled data
84 u32 g_unSampleSize; //requested sample size
85 u8 g_uchReadADCHandlerStop=1; //a status bit that shows whether the sampling is done
86 
87 u32 start_sampling(u32 unSampleSize, int nSampleRate, u16 *auSamplesCh0, u16 *auSamplesCh1, u16 *auSamplesCh2, u16 *auSamplesCh3, u16 *auSamplesCh4, u16 *auSamplesCh5, u16 *auSamplesCh6, u16 *auSamplesCh7)
102 {
103  u32 unDelay;
104 
105  //sampling rate = 100000000/(unDelay+400)
106 
107  if(nSampleRate==5) //sample rate = 250ksps
108  unDelay=0;
109  else if(nSampleRate==4) //sample rate = 100ksps
110  unDelay=600;
111  else if(nSampleRate==3) //sample rate = 50ksps
112  unDelay=1600;
113  else if(nSampleRate==2) //sample rate = 20ksps
114  unDelay=4600;
115  else if(nSampleRate==1) //sample rate = 10ksps
116  unDelay=9600;
117  else if(nSampleRate==0) //sample rate = 1ksps
118  unDelay=99600;
119 
120  g_unCount=0;
121  g_auSamplesCh0=auSamplesCh0;
122  g_auSamplesCh1=auSamplesCh1;
123  g_auSamplesCh2=auSamplesCh2;
124  g_auSamplesCh3=auSamplesCh3;
125  g_auSamplesCh4=auSamplesCh4;
126  g_auSamplesCh5=auSamplesCh5;
127  g_auSamplesCh6=auSamplesCh6;
128  g_auSamplesCh7=auSamplesCh7;
129  g_unSampleSize=unSampleSize;
130 
131  XScuGic InterruptController;
132 
133  /*
134  * ADC configuration
135  * - use external reference
136  * - use offset binary format
137  * - use acquisition mode 1
138  */
139  AXI_MAX11046_Config_ADC(XPAR_AXI_MAX11046_0_BASEADDR, 0x01);
140 
141  /*
142  * Set up interrupt handler for the AXI_MAX11046 IP Core
143  */
144  SetupInterruptSystem(&InterruptController);
145 
146  /*
147  * Enable the interrupt for AXI_MAX11046 IP Core
148  */
149  XScuGic_Enable(&InterruptController, INTC_DEVICE_INT_ID);
150 
151  g_uchReadADCHandlerStop=0; //this signal goes high when the sampling is done
152 
153 
154  /*
155  * Enable AXI_MAX11046 IP core interrupt
156  */
157  AXI_MAX11046_Interrupt_Enable(XPAR_AXI_MAX11046_0_BASEADDR);
158 
159  /*
160  * Write to the delay register
161  * the sampling rate = 2*(SCLK rate)/(unDelay+48)
162  */
163  AXI_MAX11046_Write_Delay_Reg(XPAR_AXI_MAX11046_0_BASEADDR, unDelay);
164 
165  /*
166  * Start the ADC conversion at the rate defined above
167  * When a sample is available, AXI_MAX11046 raises an interrupt
168  * the interrupt handler (ReadADCHandler) reads/stores the sampled data
169  *
170  * The AXI_MAX11046 stops sampling when AXI_MAX11046_Stop_Conversion() is called
171  */
172  AXI_MAX11046_Start_Conversion(XPAR_AXI_MAX11046_0_BASEADDR);
173 
174  /*
175  * wait until the sampling is done
176  */
177  while(g_uchReadADCHandlerStop==0);
178 
179  /*
180  * return the number of samples collected
181  */
182  return g_unCount;
183 }
184 
185 void continuous_sampling(int Channel)
196 {
197  u16 uSample[8];
198  u8 uchInput;
199 
200  printf("Press the ESC key to stop sampling\n\n");
201 
202 
204 
205  if(Channel < 8)
206  printf("Channel %i\n",Channel);
207  else
208  printf("Ch 0, Ch 1, Ch 2, Ch 3, Ch 4, Ch 5, Ch 6, Ch 7\n");
209 
210  /*
211  * ADC configuration
212  * - use external reference
213  * - use offset binary format
214  * - use acquisition mode 1
215  */
216  AXI_MAX11046_Config_ADC(XPAR_AXI_MAX11046_0_BASEADDR, 0x01);
217 
218  while(uchInput!=0x1B)
219  {
220  if(Channel<8)
221  {
222  AXI_MAX11046_Single_Convert(XPAR_AXI_MAX11046_0_BASEADDR, Channel, uSample);
223 
224  printf("%i\n", uSample[0]);
225 
227 
228  if((Xil_In32(XPAR_PS7_UART_1_BASEADDR+0x2C) & 0x02) == 0)
229  uchInput = Xil_In32(XPAR_PS7_UART_1_BASEADDR+0x30); // read the FIFO (only the LSB is valid).
230  }
231  else
232  {
233  AXI_MAX11046_Single_Convert(XPAR_AXI_MAX11046_0_BASEADDR, 8, uSample);
234 
235  printf("%i, %i, %i, %i, %i, %i, %i, %i\n", uSample[0], uSample[1], uSample[2], uSample[3], uSample[4], uSample[5], uSample[6], uSample[7]);
236 
238 
239  if((Xil_In32(XPAR_PS7_UART_1_BASEADDR+0x2C) & 0x02) == 0)
240  uchInput = Xil_In32(XPAR_PS7_UART_1_BASEADDR+0x30); // read the FIFO (only the LSB is valid).
241  }
242  }
243 }
244 
245 int SetupInterruptSystem(XScuGic *IntcInstancePtr)
255 {
256  int nStatus;
257  XScuGic_Config *GicConfig;
258 
259  GicConfig = XScuGic_LookupConfig(XPAR_SCUGIC_SINGLE_DEVICE_ID);
260  if (NULL == GicConfig) {
261  return XST_FAILURE;
262  }
263 
264  nStatus = XScuGic_CfgInitialize(IntcInstancePtr, GicConfig,
265  GicConfig->CpuBaseAddress);
266  if (nStatus != XST_SUCCESS) {
267  return XST_FAILURE;
268  }
269 
270  /*
271  * Perform a self-test to ensure that the hardware was built
272  * correctly
273  */
274  nStatus = XScuGic_SelfTest(IntcInstancePtr);
275  if (nStatus != XST_SUCCESS) {
276  return XST_FAILURE;
277  }
278 
279  /*
280  * Connect the interrupt controller interrupt handler to the hardware
281  * interrupt handling logic in the ARM processor.
282  */
283  Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
284  (Xil_ExceptionHandler) XScuGic_InterruptHandler,
285  IntcInstancePtr);
286 
287  /*
288  * Enable interrupts in the ARM
289  */
290  Xil_ExceptionEnable();
291 
292  /*
293  * Connect a device driver handler that will be called when an
294  * interrupt for the device occurs, the device driver handler performs
295  * the specific interrupt processing for the device
296  */
297  nStatus = XScuGic_Connect(IntcInstancePtr, INTC_DEVICE_INT_ID,
298  (Xil_ExceptionHandler)ReadADCHandler,
299  (void *)IntcInstancePtr);
300 
301  return nStatus;
302 }
303 
304 void ReadADCHandler(void *CallBackRef)
320 {
321  u32 nStatus;
322 
323  /*
324  * Read the interrupt status register to clear the interrupt
325  */
326  nStatus=AXI_MAX11046_Read_Interrupt_Status(XPAR_AXI_MAX11046_0_BASEADDR);
327 
328  /*
329  * Read the sampled data and store it in the *g_suSamples array
330  */
331  //*(g_auSamples+g_unCount)=AXI_MAX11046_Read_Data(XPAR_AXI_MAX11046_0_BASEADDR);
332  g_auSamplesCh0[g_unCount]=AXI_MAX11046_Read_Data(XPAR_AXI_MAX11046_0_BASEADDR,0);
333  g_auSamplesCh1[g_unCount]=AXI_MAX11046_Read_Data(XPAR_AXI_MAX11046_0_BASEADDR,1);
334  g_auSamplesCh2[g_unCount]=AXI_MAX11046_Read_Data(XPAR_AXI_MAX11046_0_BASEADDR,2);
335  g_auSamplesCh3[g_unCount]=AXI_MAX11046_Read_Data(XPAR_AXI_MAX11046_0_BASEADDR,3);
336  g_auSamplesCh4[g_unCount]=AXI_MAX11046_Read_Data(XPAR_AXI_MAX11046_0_BASEADDR,4);
337  g_auSamplesCh5[g_unCount]=AXI_MAX11046_Read_Data(XPAR_AXI_MAX11046_0_BASEADDR,5);
338  g_auSamplesCh6[g_unCount]=AXI_MAX11046_Read_Data(XPAR_AXI_MAX11046_0_BASEADDR,6);
339  g_auSamplesCh7[g_unCount]=AXI_MAX11046_Read_Data(XPAR_AXI_MAX11046_0_BASEADDR,7);
340  if(g_unCount==5)
342  g_unCount++;
343 
344  /*
345  * Stop sampling when enough samples are collected
346  */
348  {
349  /*
350  * stops sampling
351  */
352  AXI_MAX11046_Stop_Conversion(XPAR_AXI_MAX11046_0_BASEADDR);
354  }
355 
356  /*
357  * End sampling if the ESC key is pressed
358  */
359  if((Xil_In32(XPAR_PS7_UART_1_BASEADDR+0x2C) & 0x02) == 0) //if UART Rx buffer is not empty
360  {
361  u8 uchInput = Xil_In32(XPAR_PS7_UART_1_BASEADDR+0x30); // read the FIFO (only the LSB is valid).
362  if(uchInput==0x1B)
363  {
364  AXI_MAX11046_Stop_Conversion(XPAR_AXI_MAX11046_0_BASEADDR);
366  }
367  }
368 }