Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/ad7616-sdz/ad7616_core.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file ad7616_core.c
3 * @brief Implementation of AD7616 Core Driver.
4 * @author DBogdan ([email protected])
5 ********************************************************************************
6 * Copyright 2016(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 <xparameters.h>
44 #include <stdint.h>
45 #include <stdio.h>
46 #include <xil_io.h>
47 #include "ad7616_core.h"
48 #include "platform_drivers.h"
49 #include "spi_engine.h"
50
51 /***************************************************************************//**
52 * @brief ad7616_core_read
53 *******************************************************************************/
54 int32_t ad7616_core_read(adc_core core,
55 uint32_t reg_addr,
56 uint32_t *reg_data)
57 {
58 *reg_data = Xil_In32((core.adc_baseaddr + reg_addr));
59
60 return 0;
61 }
62
63 /***************************************************************************//**
64 * @brief ad7616_core_write
65 *******************************************************************************/
66 int32_t ad7616_core_write(adc_core core,
67 uint32_t reg_addr,
68 uint32_t reg_data)
69 {
70 Xil_Out32((core.adc_baseaddr + reg_addr), reg_data);
71
72 return 0;
73 }
74
75 /***************************************************************************//**
76 * @brief ad7616_core_read
77 *******************************************************************************/
78 int32_t ad7616_dma_read(adc_core core,
79 uint32_t reg_addr,
80 uint32_t *reg_data)
81 {
82 *reg_data = Xil_In32((core.dmac_baseaddr + reg_addr));
83
84 return 0;
85 }
86
87 /***************************************************************************//**
88 * @brief ad7616_core_write
89 *******************************************************************************/
90 int32_t ad7616_dma_write(adc_core core,
91 uint32_t reg_addr,
92 uint32_t reg_data)
93 {
94 Xil_Out32((core.dmac_baseaddr + reg_addr), reg_data);
95
96 return 0;
97 }
98
99 /***************************************************************************//**
100 * @brief ad7616_core_setup
101 *******************************************************************************/
102 int32_t ad7616_core_setup(adc_core core)
103 {
104 uint32_t type;
105
106 ad7616_core_write(core, AD7616_REG_UP_CTRL, 0x00);
107 mdelay(10);
108 ad7616_core_write(core, AD7616_REG_UP_CTRL, AD7616_CTRL_RESETN);
109 ad7616_core_write(core, AD7616_REG_UP_CONV_RATE, 100);
110 ad7616_core_write(core, AD7616_REG_UP_CTRL,
111 AD7616_CTRL_RESETN | AD7616_CTRL_CNVST_EN);
112 mdelay(10);
113 ad7616_core_write(core, AD7616_REG_UP_CTRL, AD7616_CTRL_RESETN);
114 mdelay(10);
115
116 ad7616_core_read(core, AD7616_REG_UP_IF_TYPE, &type);
117 printf("AD7616 IP Core (%s interface) successfully initialized\n",
118 type ? "parallel" : "serial");
119
120 return 0;
121 }
122
123 /***************************************************************************//**
124 * @brief ad7616_capture_serial
125 *******************************************************************************/
126 int32_t ad7616_capture_serial(adc_core core,
127 uint32_t no_of_samples,
128 uint32_t start_address)
129 {
130 uint32_t reg_val;
131 uint32_t transfer_id;
132 uint32_t length;
133
134 mdelay(10);
135 spi_engine_write(SPI_ENGINE_REG_OFFLOAD_CTRL(0), 0x0);
136 mdelay(10);
137 spi_engine_write(SPI_ENGINE_REG_OFFLOAD_RESET(0), 0x1);
138 spi_engine_write(SPI_ENGINE_REG_OFFLOAD_SDO_MEM(0), 0x00);
139 spi_engine_write(SPI_ENGINE_REG_OFFLOAD_CMD_MEM(0), 0x2103);
140 spi_engine_write(SPI_ENGINE_REG_OFFLOAD_CMD_MEM(0), 0x2000);
141 spi_engine_write(SPI_ENGINE_REG_OFFLOAD_CMD_MEM(0), 0x11fe);
142 spi_engine_write(SPI_ENGINE_REG_OFFLOAD_CMD_MEM(0), 0x0201);
143 spi_engine_write(SPI_ENGINE_REG_OFFLOAD_CMD_MEM(0), 0x3000);
144 spi_engine_write(SPI_ENGINE_REG_OFFLOAD_CMD_MEM(0), 0x11ff);
145 spi_engine_write(SPI_ENGINE_REG_OFFLOAD_CTRL(0), 0x1);
146
147 ad7616_core_write(core, AD7616_REG_UP_CTRL,
148 AD7616_CTRL_RESETN | AD7616_CTRL_CNVST_EN);
149
150 length = no_of_samples * core.no_of_channels * ((core.resolution + 7) / 8);
151
152 ad7616_dma_write(core, ADC_DMAC_REG_CTRL, 0x0);
153 ad7616_dma_write(core, ADC_DMAC_REG_CTRL, ADC_DMAC_CTRL_ENABLE);
154
155 ad7616_dma_write(core, ADC_DMAC_REG_IRQ_MASK, 0x0);
156
157 ad7616_dma_read(core, ADC_DMAC_REG_TRANSFER_ID, &transfer_id);
158 ad7616_dma_read(core, ADC_DMAC_REG_IRQ_PENDING, &reg_val);
159 ad7616_dma_write(core, ADC_DMAC_REG_IRQ_PENDING, reg_val);
160
161 ad7616_dma_write(core, ADC_DMAC_REG_DEST_ADDRESS, start_address);
162 ad7616_dma_write(core, ADC_DMAC_REG_DEST_STRIDE, 0x0);
163 ad7616_dma_write(core, ADC_DMAC_REG_X_LENGTH, length - 1);
164 ad7616_dma_write(core, ADC_DMAC_REG_Y_LENGTH, 0x0);
165
166 ad7616_dma_write(core, ADC_DMAC_REG_START_TRANSFER, 0x1);
167 /* Wait until the new transfer is queued. */
168 do {
169 ad7616_dma_read(core, ADC_DMAC_REG_START_TRANSFER, &reg_val);
170 } while (reg_val == 1);
171
172 /* Wait until the current transfer is completed. */
173 do {
174 ad7616_dma_read(core, ADC_DMAC_REG_IRQ_PENDING, &reg_val);
175 } while ((reg_val != (ADC_DMAC_IRQ_SOT | ADC_DMAC_IRQ_EOT)));
176 ad7616_dma_write(core, ADC_DMAC_REG_IRQ_PENDING, reg_val);
177
178 /* Wait until the transfer with the ID transfer_id is completed. */
179 do {
180 ad7616_dma_read(core, ADC_DMAC_REG_TRANSFER_DONE, &reg_val);
181 } while ((reg_val & (1 << transfer_id)) != (1 << transfer_id));
182
183 ad7616_core_write(core, AD7616_REG_UP_CTRL, AD7616_CTRL_RESETN);
184
185 return 0;
186 }
187
188 /***************************************************************************//**
189 * @brief ad7616_capture_parallel
190 *******************************************************************************/
191 int32_t ad7616_capture_parallel(adc_core core,
192 uint32_t no_of_samples,
193 uint32_t start_address)
194 {
195 uint32_t reg_val;
196 uint32_t transfer_id;
197 uint32_t length;
198
199 ad7616_core_write(core, AD7616_REG_UP_CTRL,
200 AD7616_CTRL_RESETN | AD7616_CTRL_CNVST_EN);
201
202 length = no_of_samples * core.no_of_channels * ((core.resolution + 7) / 8);
203
204 ad7616_dma_write(core, ADC_DMAC_REG_CTRL, 0x0);
205 ad7616_dma_write(core, ADC_DMAC_REG_CTRL, ADC_DMAC_CTRL_ENABLE);
206
207 ad7616_dma_write(core, ADC_DMAC_REG_IRQ_MASK, 0x0);
208
209 ad7616_dma_read(core, ADC_DMAC_REG_TRANSFER_ID, &transfer_id);
210 ad7616_dma_read(core, ADC_DMAC_REG_IRQ_PENDING, &reg_val);
211 ad7616_dma_write(core, ADC_DMAC_REG_IRQ_PENDING, reg_val);
212
213 ad7616_dma_write(core, ADC_DMAC_REG_DEST_ADDRESS, start_address);
214 ad7616_dma_write(core, ADC_DMAC_REG_DEST_STRIDE, 0x0);
215 ad7616_dma_write(core, ADC_DMAC_REG_X_LENGTH, length - 1);
216 ad7616_dma_write(core, ADC_DMAC_REG_Y_LENGTH, 0x0);
217
218 ad7616_dma_write(core, ADC_DMAC_REG_START_TRANSFER, 0x1);
219 /* Wait until the new transfer is queued. */
220 do {
221 ad7616_dma_read(core, ADC_DMAC_REG_START_TRANSFER, &reg_val);
222 } while (reg_val == 1);
223
224 /* Wait until the current transfer is completed. */
225 do {
226 ad7616_dma_read(core, ADC_DMAC_REG_IRQ_PENDING, &reg_val);
227 } while ((reg_val != (ADC_DMAC_IRQ_SOT | ADC_DMAC_IRQ_EOT)));
228 ad7616_dma_write(core, ADC_DMAC_REG_IRQ_PENDING, reg_val);
229
230 /* Wait until the transfer with the ID transfer_id is completed. */
231 do {
232 ad7616_dma_read(core, ADC_DMAC_REG_TRANSFER_DONE, &reg_val);
233 } while ((reg_val & (1 << transfer_id)) != (1 << transfer_id));
234
235 ad7616_core_write(core, AD7616_REG_UP_CTRL, AD7616_CTRL_RESETN);
236
237 return 0;
238 }