Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/drivers/ADP5589/ADP5589.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file ADP5589.c
3 * @brief Implementation of ADP5589 Driver.
4 * @author Mihai Bancisor ([email protected])
5 ********************************************************************************
6 * Copyright 2012(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 * SVN Revision: $WCREV$
41 *******************************************************************************/
42
43 /******************************************************************************/
44 /***************************** Include Files **********************************/
45 /******************************************************************************/
46 #include "ADP5589.h" // ADP5589 definitions.
47 #include "Communication.h" // Communication definitions.
48
49 /******************************************************************************/
50 /************************ Functions Definitions *******************************/
51 /******************************************************************************/
52
53 /***************************************************************************//**
54 * @brief Writes data into a register.
55 *
56 * @param registerAddress - Address of the register.
57 * @param registerValue - Data value to write.
58 *
59 * @return None.
60 *******************************************************************************/
61 void ADP5589_SetRegisterValue(unsigned char registerAddress,
62 unsigned char registerValue)
63 {
64 static unsigned char writeData[2] = {0, 0};
65
66 writeData[0] = registerAddress;
67 writeData[1] = registerValue;
68 I2C_Write(ADP5589_ADDRESS, writeData, 2, 1);
69 }
70
71 /***************************************************************************//**
72 * @brief Reads the value of a register.
73 *
74 * @param registerAddress - Address of the register.
75 *
76 * @return registerValue - Value of the register.
77 *******************************************************************************/
78 unsigned char ADP5589_GetRegisterValue(unsigned char registerAddress)
79 {
80 static unsigned char readData[2] = {0, 0};
81 static unsigned char registerValue = 0;
82
83 readData[0] = registerAddress;
84 I2C_Write(ADP5589_ADDRESS, readData, 1, 0);
85 I2C_Read(ADP5589_ADDRESS, readData, 1, 1);
86 registerValue = readData[0];
87
88 return registerValue;
89 }
90
91 /***************************************************************************//**
92 * @brief Initializes the communication peripheral and checks if the ADP5589
93 * part is present.
94 *
95 * @return status - Result of the initialization procedure.
96 * Example: -1 - I2C peripheral was not initialized or
97 * ADP5589 part is not present.
98 * 0 - I2C peripheral is initialized and ADP5589
99 * part is present.
100 *******************************************************************************/
101 char ADP5589_Init(void)
102 {
103 static unsigned char status = 0;
104
105 status = I2C_Init(100000);
106 if((ADP5589_GetRegisterValue(ADP5589_ADR_ID) & ADP5589_ID_MAN_ID) !=
107 ADP5589_ID)
108 {
109 status = -1;
110 }
111 /* Enable internal oscillator and set clock frequency to 500 kHz. */
112 ADP5589_SetRegisterValue(ADP5589_ADR_GENERAL_CFG_B,
113 ADP5589_GENERAL_CFG_B_OSC_EN|
114 ADP5589_GENERAL_CFG_B_CORE_FREQ(3));
115
116 return status;
117 }
118
119 /***************************************************************************//**
120 * @brief Initializes the PWM generator in continuous mode.
121 *
122 * @return None.
123 *******************************************************************************/
124 void ADP5589_InitPwm(void)
125 {
126 ADP5589_SetRegisterValue(ADP5589_ADR_PWM_CFG,
127 ADP5589_PWM_CFG_PWM_EN);
128 ADP5589_SetRegisterValue(ADP5589_ADR_PIN_CONFIG_D,
129 ADP5589_PIN_CONFIG_D_R3_EXTEND(2));
130 }
131
132 /***************************************************************************//**
133 * @brief Initializes keyboard decoder.
134 * @param pmodPort - J1 or J2 connector of PmodIOXP
135 * Example: PMOD_IOXP_J1 - J1 connector.
136 * PMOD_IOXP_J2 - J2 connector.
137 * @return None.
138 *******************************************************************************/
139 void ADP5589_InitKey(unsigned char pmodPort)
140 {
141 if(!pmodPort)
142 {
143 /* Config row 0,1,2,3 */
144 ADP5589_SetRegisterValue(ADP5589_ADR_PIN_CONFIG_A,0x0F);
145 /* Config column 0,1,2,3 */
146 ADP5589_SetRegisterValue(ADP5589_ADR_PIN_CONFIG_B,0x0F);
147 }
148 else
149 {
150 /* Config row 4,5,6,7 */
151 ADP5589_SetRegisterValue(ADP5589_ADR_PIN_CONFIG_A,0xF0);
152 /* Config column 4,5,6,7 */
153 ADP5589_SetRegisterValue(ADP5589_ADR_PIN_CONFIG_B,0xF0);
154 }
155 }
156
157 /***************************************************************************//**
158 * @brief Sets the PWM On and Off times.
159 *
160 * @param pwmOffTime - The amount of time in uS for which the PWM pulse is LOW.
161 * @param pwmONTime - The amount of time in uS for which the PWM pulse is HIGH.
162 *
163 * @return None.
164 *******************************************************************************/
165 void ADP5589_SetPwm(unsigned short pwmOffTime, unsigned short pwmOnTime)
166 {
167 unsigned char data = 0;
168
169 data = (unsigned char)(pwmOffTime >> 8);
170 ADP5589_SetRegisterValue(ADP5589_ADR_PWM_OFFT_HIGH, data);
171 data = (unsigned char)(pwmOffTime & 0xFF);
172 ADP5589_SetRegisterValue(ADP5589_ADR_PWM_OFFT_LOW, data);
173 data = (unsigned char)(pwmOnTime & 0xFF);
174 ADP5589_SetRegisterValue(ADP5589_ADR_PWM_ONT_LOW, data);
175 data = (unsigned char)(pwmOnTime >> 8);
176 ADP5589_SetRegisterValue(ADP5589_ADR_PWM_ONT_HIGH, data);
177 }
178 /***************************************************************************//**
179 * @brief Sets the direction of the pins.
180 *
181 * @param reg - The address of the direction register to be written.
182 * @param val - The data to be written to the direction register.
183 *
184 * @return None.
185 *******************************************************************************/
186 void ADP5589_GpioDirection(unsigned char reg, unsigned char val)
187 {
188 ADP5589_SetRegisterValue(reg, val);
189 }
190 /***************************************************************************//**
191 * @brief Reads the state of the pins.
192 *
193 * @param reg - The address of the status register to be read.
194 *
195 * @return val - The value of the register.
196 *******************************************************************************/
197 unsigned char ADP5589_GetPinState(unsigned char reg)
198 {
199 unsigned char val = 0;
200
201 val = ADP5589_GetRegisterValue(reg);
202
203 return val;
204 }
205
206 /***************************************************************************//**
207 * @brief Sets the state of the pins.
208 *
209 * @param reg - The address of the status register to be read.
210 * @param state - The value to be written to the pins.
211 *
212 * @return none
213 *******************************************************************************/
214 void ADP5589_SetPinState(unsigned char reg, unsigned char state)
215 {
216 ADP5589_SetRegisterValue(reg,state);
217 }
218
219 /***************************************************************************//**
220 * @brief Decodes the key on the Pmod-KYPD.
221 *
222 * @param reg - The value of the FIFO register
223 * @param pmodPort - J1 or J2 connector of PmodIOXP
224 * Example: PMOD_IOXP_J1 - J1 connector.
225 * PMOD_IOXP_J2 - J2 connector.
226 * @param eventType - Pressed or released.
227 * Example: ADP5589_EVENT_KEY_RELEASED
228 * ADP5589_EVENT_KEY_PRESSED
229 *
230 * @return key - Actual Key on the Pmod-KYPD.
231 *******************************************************************************/
232 unsigned char ADP5589_KeyDecode(unsigned char reg,
233 unsigned char eventType,
234 unsigned char pmodPort)
235 {
236 unsigned char key = 0;
237
238 reg -= 0x30 * pmodPort;
239 if(eventType == ADP5589_EVENT_KEY_PRESSED)
240 {
241 reg -= 0x80;
242 }
243 switch(reg)
244 {
245 case 0x25:
246 key = '1';
247 break;
248 case 0x24:
249 key = '4';
250 break;
251 case 0x23:
252 key = '7';
253 break;
254 case 0x1A:
255 key = '2';
256 break;
257 case 0x19:
258 key = '5';
259 break;
260 case 0x18:
261 key = '8';
262 break;
263 case 0x0F:
264 key = '3';
265 break;
266 case 0x0E:
267 key = '6';
268 break;
269 case 0x0D:
270 key = '9';
271 break;
272 case 0x04:
273 key = 'A';
274 break;
275 case 0x22:
276 key = '0';
277 break;
278 case 0x17:
279 key = 'F';
280 break;
281 case 0x0C:
282 key = 'E';
283 break;
284 case 0x01:
285 key = 'D';
286 break;
287 case 0x02:
288 key = 'C';
289 break;
290 case 0x03:
291 key = 'B';
292 break;
293 default:
294 key = 'x';
295 break;
296 }
297
298 return key;
299 }
300
301 /***************************************************************************//**
302 * @brief Locks the ADP5589 and requests Password for unlock.
303 *
304 * @param pmodPort - J1 or J2 connector of PmodIOXP
305 * Example: PMOD_IOXP_J1 - J1 connector.
306 * PMOD_IOXP_J2 - J2 connector.
307 *
308 * @return key - Actual Key on the Pmod-KYPD.
309 *******************************************************************************/
310 void ADP5589_KeyLock(unsigned char firstEvent,
311 unsigned char secondEvent,
312 unsigned char pmodPort)
313 {
314 unsigned char data = 0;
315
316 ADP5589_SetRegisterValue(ADP5589_ADR_UNLOCK1,
317 ADP5589_UNLOCK1_UNLOCK1_STATE | (firstEvent + 0x30 * pmodPort));
318 ADP5589_SetRegisterValue(ADP5589_ADR_UNLOCK2,
319 ADP5589_UNLOCK2_UNLOCK2_STATE | (secondEvent + 0x30 * pmodPort));
320 ADP5589_SetRegisterValue(ADP5589_ADR_LOCK_CFG, ADP5589_LOCK_CFG_LOCK_EN);
321 do
322 {
323 data = ADP5589_GetRegisterValue(ADP5589_ADR_STATUS);
324 }
325 while((data & ADP5589_STATUS_LOCK_STAT) != 0);
326 }