Debian Codegraph @

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

raw symbols SCIP
1 /***************************************************************************//**
2 * @file Dyno.c
3 * @brief Implementation of Dyno Driver.
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" // ST7565R definitions
44 #include "Communication.h" // Communication definitions
45 #include "Time.h" // Time definitions
46 /******************************************************************************/
47 /************************* Constants Definitions ******************************/
48 /******************************************************************************/
49
50 /******************************************************************************/
51 /**************************** Global Variables ********************************/
52 /******************************************************************************/
53
54 volatile unsigned char btMin = 0;
55 volatile unsigned char btEnt = 0;
56 volatile unsigned char btPlu = 0;
57 volatile unsigned char prevBtEnt = 0;
58 volatile unsigned char debounceCt = 0;
59
60 /******************************************************************************/
61 /******************************* Functions ************************************/
62 /******************************************************************************/
63
64 /***************************************************************************//**
65 * @brief ADC initialization rountine.
66 *
67 * @param None.
68 *
69 * @return None.
70 *******************************************************************************/
71 void ADC_Init(void)
72 {
73 ADCCON = 0x20; //Power up ADC
74 TIME_DelayMs(2);
75 REFCON = 0x01; // Internal 2.5V reference.
76 ADCCON = (1 << 0) | // Enable Timer1 as a conversion input => 40KSPS.
77 (1 << 5) | // Power up ADC
78 (1 << 7) | // Start conversion
79 (1 << 9) | // acq. time = 8 clocks
80 (1 << 10) ; // fADC/2.
81 ADCCP = 0x00;
82 }
83
84 /***************************************************************************//**
85 * @brief Board initialization
86 *
87 * @param None.
88 *
89 * @return None.
90 *******************************************************************************/
91 void Board_Init(void)
92 {
93 /* PLLCON Write Sequence */
94 POWKEY1 = 0xAA;
95 PLLCON = 0x01; // Internal 32 kHz oscillator; Clocking mode: PLL.
96 POWKEY2 = 0x55;
97
98 /* POWCON0 Write Sequence */
99 POWKEY1 = 0x01;
100 POWCON0 = 0x00; // Configure UCLK CPU Clock for 41.78MHz, CD=0.
101 POWKEY2 = 0xF4;
102
103 // Initilize Timer 1
104 T1LD = 0x414; // 40kHz --> 25uS,
105 T1CON = 0x4C0; // Periodic, 41.78MHz UCLK, div/1
106
107 // Initialize interrupts
108 IRQEN = (1 << 3) // Enable Timer1 interrupt sources
109 |(1 << 2) // Enable Timer0 interrupt sources
110 | (1 << 6); // Enable ADC interrupt sources
111
112 GP1PAR |= ((1 << 30));
113 GP2PAR |= (1 << 2);
114
115 /* POWCON1 Write Sequence */
116 POWKEY3 = 0x76;
117 POWCON1 = 0x900; // Power up the SPI @ 41.78 MHz.
118 POWKEY4 = 0xB1;
119
120 /* Configure the PWM pins. */
121 GP1CON &= ~0x33000000;
122 GP1CON |= 0x22000000; // P1.7 - PWM3; P1.6 - PWM2.
123
124 GP2CON &= ~0x00000003;
125 GP2CON |= 0x00000002; // P2.0 - PWM4.
126
127 PWMCON1 = (1 << 6) | (1 << 7) // UCLK/16
128 | (1 << 0); // Enable all PWM outputs.
129
130 PWM1COM1 = 0x7F; // PWM2 output pin goes low when the PWM timer reaches the count value stored in this register.
131 PWM1COM2 = 0x7F; // PWM3 output pin goes low when the PWM timer reaches the count value stored in this register.
132 PWM1COM0 = 0xFF; // PWM2 output pin goes high when the PWM timer reaches the count value stored in this register.
133 PWM1LEN = 0xFF; // PWM3 output pin goes high when the PWM timer reaches the value stored in this register.
134
135
136 PWM2COM1 = 0x7F; // PWM2 output pin goes low when the PWM timer reaches the count value stored in this register.
137 PWM2LEN = 0xFF; // PWM3 output pin goes high when the PWM timer reaches the value stored in this register.WM3 output pin goes high when the PWM timer reaches the value stored in this register.
138
139 ADC_Init(); // Initialize ADC
140
141 //GP2DAT |= 0x04000000; // P2.2 output HALL C
142 GP1DAT |= 0x20000000; // P1.5 output Hall B
143 }
144
145 /***************************************************************************//**
146 * @brief Reads button presses.
147 *
148 * @param None.
149 *
150 * @return None.
151 *******************************************************************************/
152 void buttons()
153 {
154 if((debounceCt == 0) && (GP2DAT & (1 << 4)))
155 prevBtEnt = 0;
156 if(!(GP1DAT & (1 << 3)) && debounceCt == 0)
157 {
158 debounceCt = 25;
159 btMin = 1;
160 }
161 if(!(GP2DAT & (1 << 4)) && (debounceCt == 0) && !prevBtEnt)
162 {
163 prevBtEnt = 1;
164 debounceCt = 25;
165 btEnt = 1;
166 }
167 if(!(GP1DAT & (1 << 2)) && debounceCt == 0)
168 {
169 debounceCt = 25;
170 btPlu = 1;
171 }
172 if(debounceCt != 0)
173 debounceCt--;
174 }