Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/ad-dyno1-ebz/src/Main.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file Main.c
3 * @brief Main.c implementation.
4 * @author Mihai Bancisor ([email protected])
5 ********************************************************************************
6 * Copyright 2014(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 /******************************************************************************/
41 /***************************** Include Files **********************************/
42 /******************************************************************************/
43 #include "ST7565R.h"
44 #include "Time.h"
45 #include "menu.h"
46 #include "Dyno.h"
47 #include "math.h"
48
49 /******************************************************************************/
50 /**************************** Global Variables ********************************/
51 /******************************************************************************/
52
53
54 // DC filter config
55 #define FILTERSHIFT 7 // for low pass filters to determine ADC offsets
56 #define FILTERROUNDING (1<<4)
57 volatile unsigned long filteredOffset = 2048 << FILTERSHIFT;
58 volatile unsigned long lastFilteredOffset = 2048 << FILTERSHIFT;
59
60 #define FILTERSHIFT1 1 // for low pass filters to determine ADC offsets
61 #define FILTERROUNDING1 (1<<4)
62 volatile unsigned short filteredOffset1 = 2048 << FILTERSHIFT1;
63 volatile unsigned short lastFilteredOffset1 = 2048 << FILTERSHIFT1;
64
65 // buttons
66 volatile unsigned int ct10Ms = 0;
67 extern volatile unsigned char btMin;
68 extern volatile unsigned char btEnt;
69 extern volatile unsigned char btPlu;
70
71 volatile unsigned long delay25Us = 0;
72
73 volatile unsigned short lcdUpdateCt = 0;
74
75 volatile unsigned long long c1SquareSum = 0;
76 volatile unsigned long long c1SquareSumRMS = 0;
77 extern volatile unsigned char sampleADC;
78
79 volatile unsigned short currentCt = 0;
80
81 volatile unsigned char dutyCycle = 0;
82 volatile unsigned char maxDutyCycle = 80;
83 volatile unsigned char minDutyCycle = 20;
84 volatile unsigned char dutyCycleStep = 5;
85
86 unsigned char rampTime = 2;
87 volatile unsigned short rampCt = 0;
88
89 volatile int adc[1000];
90
91 volatile int adc1 = 0;
92
93 volatile unsigned char hallValue = 0;
94 volatile unsigned char prevHallValue = 0;
95 volatile unsigned short hallCounter = 0;
96 volatile unsigned short hallStoppedCounter = 0;
97 volatile unsigned short hallTime[10];
98 volatile unsigned char hallTimeCt = 0;
99
100 menuType measurementsMenu =
101 {
102 "Measurements",
103 pfnMeasurements
104 };
105
106 menuType waveformsMenu =
107 {
108 "Waveforms",
109 };
110 menuType stepResponeMenu =
111 {
112 "Step Load",
113 };
114 menuType dutyCycleToggleMenu =
115 {
116 "Step",
117 pfnStep
118 };
119 menuType maxMenu =
120 {
121 "Set max val",
122 pfnSetMax
123 };
124 menuType minMenu =
125 {
126 "Set min val",
127 pfnSetMin
128 };
129 menuType rampMenu =
130 {
131 "Ramp",
132 pfnRamp
133 };
134
135 menuType settingsMenu =
136 {
137 "Settings",
138 };
139 menuType dutyCycleStepMenu =
140 {
141 "Duty cycle step",
142 pfnDutyCycleStep
143 };
144 menuType aboutMenu =
145 {
146 "About",
147 pfnAbout
148 };
149 menuType mainMenu = {""};
150 menuType* pCurrentMenu;
151 /******************************************************************************/
152 /******************************* Functions ************************************/
153 /******************************************************************************/
154
155 /***************************************************************************//**
156 * @brief Draws the ADI logo and displays the name of ADI component.
157 *
158 * @param componentName - The name of the ADI component.
159 *
160 * @return None.
161 *******************************************************************************/
162 void DisplayMenu()
163 {
164 unsigned char i;
165
166 for(i = 0; i < pCurrentMenu->submenuCount; i++)
167 {
168 if(pCurrentMenu->activeSubmenu == i)
169 {
170 ST7565R_String(i, 0, ">>", 0);
171 if(i == 0 && pCurrentMenu->parent)
172 ST7565R_String(i, 20, "...", 0);
173 else
174 ST7565R_String(i, 20, pCurrentMenu->submenu[i]->name, 0);
175 ST7565R_String(i, 110, "<<", 0);
176 }
177 else
178 {
179 ST7565R_String(i, 0, " ", 0);
180 if(i == 0 && pCurrentMenu->parent)
181 ST7565R_String(i, 20, "...", 0);
182 else
183 ST7565R_String(i, 20, pCurrentMenu->submenu[i]->name, 0);
184 ST7565R_String(i, 110, " ", 0);
185 }
186 }
187 }
188
189 /***************************************************************************//**
190 * @brief Draws the ADI logo and displays the name of ADI component.
191 *
192 * @param componentName - The name of the ADI component.
193 *
194 * @return None.
195 *******************************************************************************/
196 void ADI_Component(char* string)
197 {
198 const unsigned char adiComponent[440] =
199 {
200 0x00, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0x3E, 0x7E, 0x7E, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE,
201 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0x00,
202 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xF8, 0x3E, 0x0E, 0x3E, 0xF8, 0xC0, 0x00, 0x00,
203 0x00, 0x00, 0xFE, 0xFE, 0x38, 0x70, 0xE0, 0xC0, 0x80, 0x00, 0xFE, 0xFE, 0x00, 0x00, 0x00, 0x00,
204 0xC0, 0xF8, 0x3E, 0x0E, 0x3E, 0xF8, 0xC0, 0x00, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00, 0x00, 0x00,
205 0x00, 0x00, 0x00, 0xE0, 0xF8, 0x1C, 0x0E, 0x06, 0x06, 0x06, 0x06, 0x0E, 0x1C, 0xF8, 0xE0, 0x00,
206 0xC0, 0xF8, 0xFC, 0x0E, 0x06, 0x06, 0x06, 0x86, 0x86, 0x8E, 0x9C, 0x98, 0x00, 0x00, 0x00, 0xFF,
207 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x03, 0x07, 0x07, 0x0F,
208 0x0F, 0x1F, 0x3F, 0x3F, 0x7F, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00,
209 0x00, 0x00, 0x00, 0x38, 0x3F, 0x0F, 0x06, 0x06, 0x06, 0x06, 0x06, 0x0F, 0x3F, 0x38, 0x00, 0x00,
210 0x3F, 0x3F, 0x00, 0x00, 0x00, 0x01, 0x03, 0x07, 0x3F, 0x3F, 0x00, 0x00, 0x38, 0x3F, 0x0F, 0x06,
211 0x06, 0x06, 0x06, 0x06, 0x0F, 0x3F, 0x38, 0x00, 0x3F, 0x3F, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
212 0x30, 0x03, 0x0F, 0x1E, 0x38, 0x30, 0x30, 0x30, 0x30, 0x38, 0x1E, 0x0F, 0x03, 0x00, 0x01, 0x0F,
213 0x1F, 0x38, 0x30, 0x30, 0x30, 0x31, 0x31, 0x19, 0x3F, 0x3F, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF,
214 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xC0, 0xE0, 0xE0, 0xF0,
215 0xF8, 0xF8, 0xFC, 0xFC, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00,
216 0xFE, 0xFE, 0x06, 0x06, 0x06, 0x06, 0x0E, 0x1C, 0x38, 0xF0, 0xC0, 0x00, 0x00, 0xFE, 0xFE, 0x86,
217 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x00, 0x00, 0x0E, 0x7E, 0xF0, 0x80, 0x00, 0x00, 0x80, 0xF0,
218 0x7E, 0x0E, 0x00, 0xFE, 0xFE, 0x00, 0x00, 0xC0, 0xF0, 0x38, 0x1C, 0x0E, 0x06, 0x06, 0x06, 0x0E,
219 0x1C, 0x38, 0x30, 0x00, 0x00, 0xFE, 0xFE, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x06, 0x00, 0x78,
220 0xFC, 0xCE, 0x86, 0x86, 0x86, 0x86, 0x8E, 0x1C, 0x18, 0x00, 0x00, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F,
221 0x7F, 0x78, 0x7C, 0x7C, 0x7E, 0x7E, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F,
222 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0x7F,
223 0x60, 0x60, 0x60, 0x60, 0x70, 0x30, 0x3C, 0x1F, 0x07, 0x00, 0x00, 0x7F, 0x7F, 0x61, 0x61, 0x61,
224 0x61, 0x61, 0x61, 0x61, 0x60, 0x00, 0x00, 0x00, 0x07, 0x3F, 0x78, 0x78, 0x3F, 0x07, 0x00, 0x00,
225 0x00, 0x7F, 0x7F, 0x00, 0x00, 0x03, 0x0F, 0x1C, 0x38, 0x70, 0x60, 0x60, 0x60, 0x70, 0x38, 0x3C,
226 0x0C, 0x00, 0x00, 0x7F, 0x7F, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x60, 0x00, 0x18, 0x38, 0x31,
227 0x61, 0x61, 0x61, 0x61, 0x33, 0x3F, 0x0E, 0x00,
228 };
229
230 ST7565R_Image(0, 9, adiComponent, 110, 32);
231 //ST7565R_String(0, 9 + 5, string, 0);
232 }
233
234 /***************************************************************************//**
235 * @brief Interrupt Service Rountine.
236 *
237 * @param None.
238 *
239 * @return None.
240 *******************************************************************************/
241 void IRQ_Handler(void) __irq
242 {
243 unsigned long IRQSTATUS = 0;
244
245 IRQSTATUS = IRQSTA; // Read off IRQSTA register
246 if(IRQSTA & 0x04) // Timer0 IRQ?
247 {
248 T0CLRI = 0; //clear Timer0 interrupt
249 GP1DAT ^= 0x00200000;
250 // T0CON = 0x00; //disable Timer0 interrupt
251 // T0CON = 0xC8; //enable Timer0 interrupt
252 }
253 IRQSTATUS = IRQSTA; // Read off IRQSTA register
254 if ((IRQSTATUS & 0x08) == 0x08) //Timer 1 interrupt source -- every 25us
255 {
256 ct10Ms++; // counter for reading the button states
257 rampCt++;
258 lcdUpdateCt++;
259 if(delay25Us > 0)
260 delay25Us--;
261
262 if(pCurrentMenu->pfn == pfnMeasurements)
263 {
264 hallValue = GP2DAT & (1 << 2);
265 if(hallValue) // on rising edge start counting
266 {
267 hallCounter++;
268 }
269 else
270 if((!hallValue) && (prevHallValue)) // on falling edge stop counting
271 {
272 // 10 value circular array
273 hallTime[hallTimeCt % 10] = hallCounter;
274 hallTimeCt++;
275 hallCounter = 0;
276 hallStoppedCounter = 0;
277 }
278 else
279 {
280 hallStoppedCounter++;
281 }
282 prevHallValue = hallValue;
283 if(hallCounter > 10000)
284 hallCounter = 5001;
285 if(hallStoppedCounter > 10000)
286 hallStoppedCounter = 5001;
287 }
288 if((pCurrentMenu->pfn == pfnRamp) && (rampCt >= rampTime*400))
289 {
290 rampCt = 0;
291 if(dutyCycle < 100)
292 dutyCycle++;
293 else
294 dutyCycle = 0;
295 }
296 T1CLRI = 0x55; // Clear the currently active Timer0 Irq
297 }
298
299 if ((IRQSTATUS & (1 << 6)) == (1 << 6)) // ADC interrupt source
300 {
301
302 if(currentCt == 3999) // IF 1000 samples have been captured pass them to RMS calc code
303 {
304 currentCt = 0;
305 c1SquareSumRMS = c1SquareSum;
306 c1SquareSum = 0;
307 }
308 if(sampleADC)
309 {
310 adc1 = (unsigned short)(ADCDAT >> 16); // Read ADC result
311 // Compute DC offset by LPF of all adc1 values
312
313 adc1 <<= 4;
314 filteredOffset1 = (lastFilteredOffset1 - ( lastFilteredOffset1 >> FILTERSHIFT1 )) + (adc1 >> FILTERSHIFT1); // update the filter
315 lastFilteredOffset1 = filteredOffset1;
316
317 adc1 = lastFilteredOffset1;
318
319 // adc1 <<= 4;
320 filteredOffset = (lastFilteredOffset - ( lastFilteredOffset >> FILTERSHIFT )) + (adc1 >> FILTERSHIFT); // update the filter
321 lastFilteredOffset = filteredOffset;
322
323 adc1 -= filteredOffset; // Remove DC bias
324 adc1 >>= 4;
325
326 c1SquareSum += adc1*adc1; // Compute square sum
327 currentCt++; // increment the current samples counter
328 }
329 else
330 {
331 adc1 = (unsigned short)(ADCDAT >> 16); // Read ADC result
332 currentCt = 0;
333 c1SquareSum = 0;
334 }
335 }
336 }
337
338 /***************************************************************************//**
339 * @brief main function.
340 *
341 * @param None.
342 *
343 * @return None.
344 *******************************************************************************/
345 int main(void)
346 {
347 int duty = 0;
348
349 Board_Init(); // Initialize peripherals
350 ST7565R_Init(); // Initialize display
351
352 ADI_Component("Analog Devices");
353 TIME_DelayMs(5000);
354 ClearScreen();
355
356 /* Initialize menus*/
357 AddSubmenu(&mainMenu, &measurementsMenu);
358 AddSubmenu(&mainMenu, &waveformsMenu);
359 AddSubmenu(&waveformsMenu, &stepResponeMenu);
360 AddSubmenu(&stepResponeMenu, &dutyCycleToggleMenu);
361 AddSubmenu(&stepResponeMenu, &maxMenu);
362 AddSubmenu(&stepResponeMenu, &minMenu);
363 AddSubmenu(&waveformsMenu, &rampMenu);
364 AddSubmenu(&mainMenu, &settingsMenu);
365 AddSubmenu(&settingsMenu, &dutyCycleStepMenu);
366 AddSubmenu(&settingsMenu, &aboutMenu);
367 pCurrentMenu = &mainMenu;
368
369 while(1)
370 {
371
372 if(ct10Ms >= 400)
373 {
374 buttons();
375 ct10Ms = 0;
376 }
377
378 if((pCurrentMenu == &mainMenu) || (pCurrentMenu == &waveformsMenu))
379 {
380 dutyCycle = 0;
381 }
382
383 duty = (51*dutyCycle)/20;
384 //duty = (51*80)/20;
385 PWM1COM1 = duty;
386 PWM1COM2 = duty;
387 PWM2COM1 = duty;
388
389 if(btMin)
390 {
391 StepMenuDown(pCurrentMenu);
392 if(pCurrentMenu->pfn)
393 pCurrentMenu->pfn(0, 1);
394 btMin = 0;
395 }
396
397 if(btPlu)
398 {
399 StepMenuUp(pCurrentMenu);
400 if(pCurrentMenu->pfn)
401 pCurrentMenu->pfn(1, 0);
402 btPlu = 0;
403 }
404
405 if(btEnt)
406 {
407 ClearScreen();
408 if(pCurrentMenu->submenuCount)
409 pCurrentMenu = pCurrentMenu->submenu[pCurrentMenu->activeSubmenu];
410 else
411 pCurrentMenu = pCurrentMenu->parent;
412 btEnt = 0;
413 }
414 if(pCurrentMenu->pfn)
415 pCurrentMenu->pfn(0, 0);
416 DisplayMenu();
417 // Discovery Control
418 if(!(GP2DAT & (1 << 3)))
419 {
420 ClearScreen();
421 //Disable PWM pins
422 GP1CON &= ~0x33000000;
423 GP2CON &= ~0x00000003;
424
425 ST7565R_String(2, 12, "Analog Discovery", 0);
426 while(!(GP2DAT & (1 << 3)))
427 {}
428 //Enable PWM pins
429 GP1CON &= ~0x33000000;
430 GP1CON |= 0x22000000; // P1.7 - PWM3; P1.6 - PWM2.
431 GP2CON &= ~0x00000003;
432 GP2CON |= 0x00000002; // P2.0 - PWM4.
433 ClearScreen();
434 }
435 }
436 }