Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/Pmods/PmodDPOT/AD5160.c

raw symbols SCIP
1 /**************************************************************************//**
2 * @file ad5160.c
3 * @brief Implementation of AD5160 Driver for Microblaze processor.
4 * @author ATofan ([email protected])
5 *
6 *******************************************************************************
7 * Copyright 2012(c) Analog Devices, Inc.
8 *
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without modification,
12 * are permitted provided that the following conditions are met:
13 * - Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 * - Neither the name of Analog Devices, Inc. nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 * - The use of this software may or may not infringe the patent rights
23 * of one or more patent holders. This license does not release you
24 * from the requirement that you obtain separate licenses from these
25 * patent holders to use this software.
26 * - Use of the software either in source or binary form, must be run
27 * on or directly connected to an Analog Devices Inc. component.
28 *
29 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED
30 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY
31 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
34 * INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
36 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 *******************************************************************************
41 * SVN Revision: 468
42 ******************************************************************************/
43
44 /*****************************************************************************/
45 /***************************** Include Files *********************************/
46 /*****************************************************************************/
47 #include "xparameters.h"
48 #include "system_config.h"
49 #include "xil_io.h"
50 #include "ad5160.h"
51
52 volatile int rxData;
53
54 #if(USE_PS7 == 1)
55 extern XScuGic IntcInstance;
56 #endif
57
58 extern u32 (*SPI_Init)(u32, char, char, char);
59 extern u32 (*SPI_TransferData)(u32, char, char*, char, char*, char);
60
61 static int rab = 10000; // Resistance between A and B on the PmodDPOT => Change with measured value
62 static int rwp = 60; // Wiper resistance (measured between B and W when D = 0) => Change with measured value
63
64 /******************************************************************************
65 * @brief Displays Main Menu.
66 *
67 * @param None.
68 *
69 * @return None.
70 ******************************************************************************/
71 void AD5160_DisplayMainMenu(void)
72 {
73 xil_printf("\n\rPmod-DPOT Demo Program\n\r");
74 xil_printf("The software is calibrated for the following values: \n\r");
75 xil_printf(" Rab = %d ohms\n\r", rab);
76 xil_printf(" Rwp = %d ohms\n\r", rwp);
77 xil_printf("These value may differ for your PmodDPOT\n\r");
78 xil_printf("\n\r");
79 xil_printf("Available options: \n\r");
80 xil_printf(" [d] Set PmodDPOT division factor\n\r");
81 xil_printf(" [c] Calibrate PmodDPOT\n\r");
82 xil_printf(" [m] Display this menu\n\r");
83 xil_printf("Press key to select the desired option\n\r");
84 }
85
86 /******************************************************************************
87 * @brief Displays Menu.
88 *
89 * @param None.
90 *
91 * @return None.
92 ******************************************************************************/
93 void AD5160_DisplayMenu(void)
94 {
95 xil_printf("\n\rAvailable options: \n\r");
96 xil_printf(" [d] Set PmodDPOT division factor\n\r");
97 xil_printf(" [c] Calibrate PmodDPOT\n\r");
98 xil_printf(" [m] Display this menu\n\r");
99 rxData = 0;
100 }
101
102 /******************************************************************************
103 * @brief Sets AD5160 resistors value.
104 *
105 * @param None.
106 *
107 * @return retValue - Value of programmed resistor value.
108 ******************************************************************************/
109 int AD5160_Set(void)
110 {
111 char rx; // Character received from UART
112 char c[] = "000"; // Variable in which we store received data
113 char *c_ptr; // Pointer to c[]
114 int i = 0; // Used to keep track of how many characters we have received from UART
115 int cnt = 0; // Number of received characters from UART
116 char valid = 0; // Validates correct input
117 int value = 0; // Value between 0 and 255 to be sent to the Pmod-DPOT
118 float rwb; // Resistance between W and B on the Pmod-DPOT
119 float rwa; // Resistance between W and A on the Pmod-DPOT
120 int whole; // Used to send float Values on the UART
121 int retValue = 0; // Value returned by function
122 char txBuffer[1] = {0x00};
123 char rxBuffer[1] = {0x00};
124
125 c_ptr = c;
126
127 // Disable interrupts in order to use normal polling
128 DisableInterrupts(UART_INTERRUPT);
129
130 // Maximum number of received characters will be 4 (e.g. F F 0x0D 0x0A)
131 while(rx != 'q')
132 {
133 cnt = 0;
134 i = 0;
135 valid = 0;
136 value = 0;
137 c_ptr = c;
138
139 if(i == 0)
140 {
141 xil_printf("\n\rPlease enter a value between 0x00 and 0xFF or [q] to return to menu: 0x");
142 }
143 while(i < 4)
144 {
145 rx = UartReadChar();
146 xil_printf("%c",rx);
147
148 // Check if key pressed is [Enter]
149 if(rx == 0x0D)
150 {
151 i = 3;
152 }
153 else if(rx == 0x0A)
154 {
155 i = 3;
156 }
157 else if(rx == 'q')
158 {
159 i = 4;
160 valid = 0;
161 }
162 else if(((rx > 0x00)&&(rx < 0x30))|| // Not 0 - 9
163 ((rx > 0x39)&&(rx < 0x41))|| // Not A - F
164 ((rx > 0x46)&&(rx < 0x61))|| // Not a - f
165 (rx > 0x66))
166 {
167 xil_printf("\n\rCharacters entered must be HEX values (0 to 9 and A B C D E F)\n\r");
168 i = 4;
169 valid = 0;
170 }
171 else
172 {
173 *c_ptr++ = rx;
174 cnt = cnt + 1;
175 valid = 1;
176 }
177 if(cnt == 2)
178 {
179 i = 4;
180 }
181 i++;
182 }
183
184 // Translate from ASCII to hex
185 for(i = 0; i < cnt; i++)
186 {
187 if(c[i] > 0x60)
188 {
189 value = value * 16 + (c[i] - 0x57);
190 }
191 else if(c[i] > 0x39)
192 {
193 value = value * 16 + (c[i] - 0x37);
194 }
195 else
196 {
197 value = value * 16 + (c[i] - 0x30);
198 }
199 }
200 if(valid == 1)
201 {
202 // Calculated according to Datasheet. rab and rwp measured on the Pmod using multimeter
203 // Please replace rab and rwp with your own measured values (see beginning of AD5160.c)
204 rwb = (float)((value * rab) / 256) + rwp;
205 whole = (int)rwb;
206 xil_printf("\n\r >>Rwb = %d ohms\n\r",whole);
207
208 rwa = (float)(((256 - value) * rab)/256) + rwp;
209 whole = (int)rwa;
210 xil_printf(" >>Rwa = %d ohms\n\r",whole);
211
212 retValue = value;
213
214 txBuffer[0] = value;
215 SPI_TransferData(SPI_BASEADDR, 1, txBuffer, 1, rxBuffer, 1);
216 }
217 }
218 rxData = 'm';
219 // Enable interrupts
220 EnableInterrupts(UART_INTERRUPT);
221 return(retValue);
222 }
223
224 /******************************************************************************
225 * @brief Calibrate value for Rab.
226 *
227 * @param None.
228 *
229 * @return None.
230 ******************************************************************************/
231 void AD5160_CalibrateRab(void)
232 {
233 char rx = 0; // Character received from UART
234 char c[] = "00000"; // Variable in which we store received data
235 char *c_ptr; // Pointer to c[]
236 int i = 0; // Used to keep track of how many characters we have received from UART
237 char cnt = 0; // Number of received characters from UART
238 char valid = 0; // Validates correct input
239 int value = 0; // Value between 0 and 255 to be sent to the Pmod-DPOT
240
241 // Disable interrupts in order to use normal polling
242 DisableInterrupts(UART_INTERRUPT);
243
244 while(!valid)
245 {
246 xil_printf("\n\rPlease measure the resistance between A and B. Enter the value in ohms: ");
247 cnt = 0;
248 i = 0;
249 value = 0;
250 c_ptr = c;
251 // Maximum number of received characters will be 7 (e.g. 1 0 2 7 0 0x0A 0x0D)
252 while(i < 7)
253 {
254 // Check if data is available on the UART
255 rx = UartReadChar();
256 xil_printf("%c",rx);
257 // Check if pressed key is [Enter]
258 if(rx == 0x0D)
259 {
260 i = 6;
261 }
262 else if(rx == 0x0A)
263 {
264 i = 6;
265 }
266 else if(((rx > 0x00)&&(rx < 0x30)) || ((rx > 0x39))) // Not 0 - 9
267 {
268 xil_printf("\n\rCharacters entered must be valid numbers (0 to 9)");
269 i = 7;
270 valid = 0;
271 }
272 else
273 {
274 *c_ptr++ = rx;
275 cnt = cnt + 1;
276 valid = 1;
277 }
278 if(cnt == 5)
279 {
280 i = 7;
281 }
282 i++;
283 }
284
285 // Translate from ASCII to decimal
286 for(i = 0; i < cnt; i++)
287 {
288 value = value * 10 + (c[i] - 0x30);
289 }
290
291 if(valid == 1)
292 {
293 rab = value;
294 xil_printf("\n\rNew value stored for Rab: %d\n\r", rab);
295 }
296 else
297 {
298 xil_printf("\n\rValue entered is not valid.\n\r");
299 }
300 }
301 // Enable interrupts
302 EnableInterrupts(UART_INTERRUPT);
303 }
304
305 /******************************************************************************
306 * @brief Calibrate value for Rwiper.
307 *
308 * @param None.
309 *
310 * @return None.
311 ******************************************************************************/
312 void AD5160_CalibrateRwp(void)
313 {
314 char rx = 0; // Character received from UART
315 char c[] = "000"; // Variable in which we store received data
316 char *c_ptr; // Pointer to c[]
317 int i = 0; // Used to keep track of how many characters we have received from UART
318 char cnt = 0; // Number of received characters from UART
319 char valid = 0; // Validates correct input
320 int value = 0; // Value between 0 and 255 to be sent to the Pmod-DPOT
321 char txBuffer[1] = {0x00};
322 char rxBuffer[1] = {0x00};
323
324 // Disable interrupts in order to use normal polling
325 DisableInterrupts(UART_INTERRUPT);
326
327 // Send 0x00 to PmodDPOT in order to measure Rwp between W and B
328 SPI_TransferData(SPI_BASEADDR, 1, txBuffer, 1, rxBuffer, 1);
329
330 while(!valid)
331 {
332 xil_printf("\n\rPlease measure the resistance between W and B. Enter the value in ohms: ");
333 c_ptr = c;
334 cnt = 0;
335 i = 0;
336 value = 0;
337
338 // Maximum number of received characters will be 5 (e.g. 1 1 7 0x0A 0x0D)
339 while(i < 5)
340 {
341 // Check if data is available on the UART
342 rx = UartReadChar();
343 xil_printf("%c",rx);
344 // Check if pressed key is [Enter]
345 if(rx == 0x0D)
346 {
347 i = 4;
348 }
349 else if(rx == 0x0A)
350 {
351 i = 4;
352 }
353 else if(((rx > 0x00)&&(rx < 0x30)) || ((rx > 0x39))) // Not 0 - 9
354 {
355 xil_printf("\n\rCharacters entered must be valid numbers (0 to 9)");
356 i = 5;
357 valid = 0;
358 }
359 else
360 {
361 *c_ptr++ = rx;
362 cnt = cnt + 1;
363 valid = 1;
364 }
365 if(cnt == 3)
366 {
367 i = 5;
368 }
369 i++;
370 }
371
372 // Translate from ASCII to decimal
373 for(i = 0; i < cnt; i++)
374 {
375 value = value * 10 + (c[i] - 0x30);
376 }
377
378 if(valid == 1)
379 {
380 rwp = value;
381 xil_printf("\n\rNew value stored for Rwp: %d\n\r", rwp);
382 }
383 else
384 {
385 xil_printf("\n\rValue entered is not valid.\n\r");
386 }
387 }
388 rxData = 'm';
389 // Enable interrupts
390 EnableInterrupts(UART_INTERRUPT);
391 xil_printf("\n\r>Returning to Menu...\n\r");
392 }
393
394 /******************************************************************************
395 * @brief Launch demo application for AD5160
396 *
397 * @param None.
398 *
399 * @return None.
400 ******************************************************************************/
401 void DemoProgram(void)
402 {
403 AD5160_DisplayMainMenu();
404
405 while(1)
406 {
407 switch(rxData)
408 {
409 case 'd':
410 AD5160_Set();
411 break;
412 case 'c':
413 AD5160_CalibrateRab();
414 AD5160_CalibrateRwp();
415 break;
416 case 'm':
417 AD5160_DisplayMenu();
418 break;
419 case 0:
420 break;
421 default:
422 xil_printf("\n\rWrong option! Please select one of the options below.");
423 AD5160_DisplayMenu();
424 break;
425 }
426 }
427 }