Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/Pmods/PmodIOXP/ADP5589.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file ADP5589.c
3 * @brief ADP5589 Driver.
4 * @author ATofan ([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 <stdio.h>
47 #include "system_config.h"
48 #include "ADP5589.h"
49
50 volatile int rxData;
51
52 /******************************************************************************
53 * @brief Decodes the key pressed on the Pmod-KYPD.
54 *
55 * @param rxData - Value received from ADP5589
56 *
57 * @return c - Actual Key on the Pmod-KYPD.
58 ******************************************************************************/
59 char KeyDecode(int rxData)
60 {
61 char c;
62
63 switch(rxData)
64 {
65 case 0x25:
66 c = '1';
67 break;
68 case 0x24:
69 c = '4';
70 break;
71 case 0x23:
72 c = '7';
73 break;
74 case 0x1A:
75 c = '2';
76 break;
77 case 0x19:
78 c = '5';
79 break;
80 case 0x18:
81 c = '8';
82 break;
83 case 0x0F:
84 c = '3';
85 break;
86 case 0x0E:
87 c = '6';
88 break;
89 case 0x0D:
90 c = '9';
91 break;
92 case 0x04:
93 c = 'A';
94 break;
95 case 0x22:
96 c = '0';
97 break;
98 case 0x17:
99 c = 'F';
100 break;
101 case 0x0C:
102 c = 'E';
103 break;
104 case 0x01:
105 c = 'D';
106 break;
107 case 0x02:
108 c = 'C';
109 break;
110 case 0x03:
111 c = 'B';
112 break;
113 default:
114 break;
115 }
116 return(c);
117 }
118
119 /******************************************************************************
120 * @brief Flush the internal FIFO on the ADP5589.
121 *
122 * @param None.
123 *
124 * @return None.
125 ******************************************************************************/
126 void FlushFIFO(void)
127 {
128 int evCnt;
129 unsigned char rxBuffer[3] = {0x00, 0x00, 0x00};
130
131 I2C_Read_axi(I2C_BASEADDR, ADP5589_I2C_ADDR, StatusReg, 1, rxBuffer);
132
133 evCnt = (rxBuffer[0] & 0x1F);
134
135 while(evCnt != 0)
136 {
137 I2C_Read_axi(I2C_BASEADDR, ADP5589_I2C_ADDR, FIFO1Reg, 1, rxBuffer);
138 I2C_Read_axi(I2C_BASEADDR, ADP5589_I2C_ADDR, StatusReg, 1, rxBuffer);
139 evCnt = (rxBuffer[0] & 0x1F);
140 }
141
142 xil_printf(" Flushing Event FIFO...\n\r");
143 }
144
145 /******************************************************************************
146 * @brief Initialize the internal registers on the ADP5589.
147 *
148 * @param None.
149 *
150 * @return None.
151 ******************************************************************************/
152 void ADP5589_Init(void)
153 {
154 unsigned char rxBuffer[3] = {0x00, 0x00, 0x00};
155 unsigned char txBuffer[3] = {0x00, 0x00, 0x00};
156
157 // Display Manufacturer ID and Revision ID
158 I2C_Read_axi(I2C_BASEADDR, ADP5589_I2C_ADDR, IDReg, 1, rxBuffer);
159 xil_printf("PmodIOXP demonstration starting...\n\r");
160 xil_printf("\n\r");
161 xil_printf("------ ADP5589 ------\n\r");
162 xil_printf("Manufacturer ID: %x\n\r", ((rxBuffer[0] & 0xF0) >> 4));
163 xil_printf("Revision ID: %x\n\r", (rxBuffer[0] & 0x0F));
164
165 xil_printf("Setting internal registers...\n\r");
166 txBuffer[0] = 0x0F;
167 I2C_Write_axi(I2C_BASEADDR, ADP5589_I2C_ADDR, PinCfgAReg, 1, txBuffer);
168 I2C_Write_axi(I2C_BASEADDR, ADP5589_I2C_ADDR, PinCfgBReg, 1, txBuffer);
169
170 xil_printf("Keypad set to connector J1: R0-R3, C0-C3\n\r");
171 txBuffer[0] = 0x80;
172 I2C_Write_axi(I2C_BASEADDR, ADP5589_I2C_ADDR, GenCfgBReg, 1, txBuffer);
173
174 xil_printf("Internal oscillator enabled\n\r");
175 }
176
177 /******************************************************************************
178 * @brief Prints the Pressed/Released Key on the UART.
179 *
180 * @param None.
181 *
182 * @return None.
183 ******************************************************************************/
184 void ADP5589_KeyDecodeTest(void)
185 {
186 unsigned char rxBuffer[3] = {0x00, 0x00, 0x00};
187 unsigned char txBuffer[3] = {0x00, 0x00, 0x00};
188 int evCnt;
189
190 xil_printf("\n\r");
191 xil_printf("Entering Key Decoder Test Mode...\n\r");
192 xil_printf("Press / Release any key to test its response:\n\r");
193 xil_printf("--- Pressing [F] will exit Key Decoder Test Mode.\n\r");
194
195 while(KeyDecode((rxBuffer[0] & 0x7F)) != 'F')
196 {
197 do
198 {
199 I2C_Read_axi(I2C_BASEADDR, ADP5589_I2C_ADDR, INTStatusReg, 1, rxBuffer);
200 }
201 while((rxBuffer[0] & 0x01) == 0);
202
203 I2C_Read_axi(I2C_BASEADDR, ADP5589_I2C_ADDR, StatusReg, 1, rxBuffer);
204
205 evCnt = (rxBuffer[0] & 0x1F);
206
207 if(evCnt != 0)
208 {
209 I2C_Read_axi(I2C_BASEADDR, ADP5589_I2C_ADDR, FIFO1Reg, 1, rxBuffer);
210 if((rxBuffer[0] & 0x80) == 0x80)
211 {
212 xil_printf("Key pressed: %c\n\r", KeyDecode((rxBuffer[0] & 0x7F)));
213 }
214 else
215 {
216 xil_printf("Key released: %c\n\r", KeyDecode((rxBuffer[0] & 0x7F)));
217 }
218 }
219
220 txBuffer[0] = 0x01;
221 I2C_Write_axi(I2C_BASEADDR, ADP5589_I2C_ADDR, INTStatusReg, 1, txBuffer);
222 }
223
224 xil_printf("Exiting Key Decoder Test Mode...\n\r");
225 }
226
227 /******************************************************************************
228 * @brief Locks the ADP5589 and requests Password for unlock.
229 *
230 * @param None
231 *
232 * @return None.
233 ******************************************************************************/
234 void ADP5589_KeyLockTest(void)
235 {
236 unsigned char rxBuffer[3] = {0x00, 0x00, 0x00};
237 unsigned char txBuffer[3] = {0x00, 0x00, 0x00};
238
239 FlushFIFO();
240 xil_printf("\n\r");
241 xil_printf("Entering Key Lock Test Mode...\n\r");
242
243 xil_printf("--- The correct combination for unlock is [1][A].\n\r");
244 txBuffer[0] = 0x80 | 0x25;
245 I2C_Write_axi(I2C_BASEADDR, ADP5589_I2C_ADDR, Unlock1Reg, 1, txBuffer);
246 txBuffer[0] = 0x80 | 0x04;
247 I2C_Write_axi(I2C_BASEADDR, ADP5589_I2C_ADDR, Unlock2Reg, 1, txBuffer);
248 txBuffer[0] = 0x01;
249 I2C_Write_axi(I2C_BASEADDR, ADP5589_I2C_ADDR, LockCfgReg, 1, txBuffer);
250
251 do
252 {
253 I2C_Read_axi(I2C_BASEADDR, ADP5589_I2C_ADDR, StatusReg, 1, rxBuffer);
254 }
255 while((rxBuffer[0] & 0x20) != 0);
256
257 FlushFIFO();
258
259 xil_printf("Correct unlock combination entered. FIFO is now unlocked.\n\r");
260 }
261
262 /******************************************************************************
263 * @brief Demo program for the ADP5589.
264 *
265 * @param None
266 *
267 * @return None.
268 ******************************************************************************/
269 void DemoProgram(void)
270 {
271 ADP5589_KeyDecodeTest();
272
273 ADP5589_KeyLockTest();
274 }