Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/ad5766-sdz/ad5766_core.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file ad5766_core.c
3 * @brief Implementation of AD5766 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 <stdlib.h>
44 #include "platform_drivers.h"
45 #include "spi_engine.h"
46 #include "ad5766_core.h"
47
48 /******************************************************************************/
49 /********************** Macros and Constants Definitions **********************/
50 /******************************************************************************/
51 const uint16_t sine_lut[512] = {
52 0x8000, 0x8192, 0x8324, 0x84b6, 0x8647, 0x87d9, 0x896a, 0x8afb,
53 0x8c8b, 0x8e1b, 0x8fab, 0x9139, 0x92c7, 0x9455, 0x95e1, 0x976d,
54 0x98f8, 0x9a82, 0x9c0b, 0x9d93, 0x9f19, 0xa09f, 0xa223, 0xa3a6,
55 0xa527, 0xa6a7, 0xa826, 0xa9a3, 0xab1f, 0xac98, 0xae10, 0xaf87,
56 0xb0fb, 0xb26e, 0xb3de, 0xb54d, 0xb6b9, 0xb824, 0xb98c, 0xbaf2,
57 0xbc56, 0xbdb7, 0xbf17, 0xc073, 0xc1cd, 0xc325, 0xc47a, 0xc5cc,
58 0xc71c, 0xc869, 0xc9b3, 0xcafb, 0xcc3f, 0xcd81, 0xcebf, 0xcffb,
59 0xd133, 0xd268, 0xd39a, 0xd4c9, 0xd5f5, 0xd71d, 0xd842, 0xd964,
60 0xda82, 0xdb9c, 0xdcb3, 0xddc7, 0xded7, 0xdfe3, 0xe0eb, 0xe1f0,
61 0xe2f1, 0xe3ee, 0xe4e8, 0xe5dd, 0xe6cf, 0xe7bc, 0xe8a6, 0xe98b,
62 0xea6d, 0xeb4a, 0xec23, 0xecf8, 0xedc9, 0xee96, 0xef5e, 0xf022,
63 0xf0e2, 0xf19d, 0xf254, 0xf307, 0xf3b5, 0xf45f, 0xf504, 0xf5a5,
64 0xf641, 0xf6d8, 0xf76b, 0xf7fa, 0xf884, 0xf909, 0xf989, 0xfa05,
65 0xfa7c, 0xfaee, 0xfb5c, 0xfbc5, 0xfc29, 0xfc88, 0xfce3, 0xfd39,
66 0xfd89, 0xfdd5, 0xfe1d, 0xfe5f, 0xfe9c, 0xfed5, 0xff09, 0xff37,
67 0xff61, 0xff86, 0xffa6, 0xffc1, 0xffd8, 0xffe9, 0xfff5, 0xfffd,
68 0xffff, 0xfffd, 0xfff5, 0xffe9, 0xffd8, 0xffc1, 0xffa6, 0xff86,
69 0xff61, 0xff37, 0xff09, 0xfed5, 0xfe9c, 0xfe5f, 0xfe1d, 0xfdd5,
70 0xfd89, 0xfd39, 0xfce3, 0xfc88, 0xfc29, 0xfbc5, 0xfb5c, 0xfaee,
71 0xfa7c, 0xfa05, 0xf989, 0xf909, 0xf884, 0xf7fa, 0xf76b, 0xf6d8,
72 0xf641, 0xf5a5, 0xf504, 0xf45f, 0xf3b5, 0xf307, 0xf254, 0xf19d,
73 0xf0e2, 0xf022, 0xef5e, 0xee96, 0xedc9, 0xecf8, 0xec23, 0xeb4a,
74 0xea6d, 0xe98b, 0xe8a6, 0xe7bc, 0xe6cf, 0xe5dd, 0xe4e8, 0xe3ee,
75 0xe2f1, 0xe1f0, 0xe0eb, 0xdfe3, 0xded7, 0xddc7, 0xdcb3, 0xdb9c,
76 0xda82, 0xd964, 0xd842, 0xd71d, 0xd5f5, 0xd4c9, 0xd39a, 0xd268,
77 0xd133, 0xcffb, 0xcebf, 0xcd81, 0xcc3f, 0xcafb, 0xc9b3, 0xc869,
78 0xc71c, 0xc5cc, 0xc47a, 0xc325, 0xc1cd, 0xc073, 0xbf17, 0xbdb7,
79 0xbc56, 0xbaf2, 0xb98c, 0xb824, 0xb6b9, 0xb54d, 0xb3de, 0xb26e,
80 0xb0fb, 0xaf87, 0xae10, 0xac98, 0xab1f, 0xa9a3, 0xa826, 0xa6a7,
81 0xa527, 0xa3a6, 0xa223, 0xa09f, 0x9f19, 0x9d93, 0x9c0b, 0x9a82,
82 0x98f8, 0x976d, 0x95e1, 0x9455, 0x92c7, 0x9139, 0x8fab, 0x8e1b,
83 0x8c8b, 0x8afb, 0x896a, 0x87d9, 0x8647, 0x84b6, 0x8324, 0x8192,
84 0x8000, 0x7e6d, 0x7cdb, 0x7b49, 0x79b8, 0x7826, 0x7695, 0x7504,
85 0x7374, 0x71e4, 0x7054, 0x6ec6, 0x6d38, 0x6baa, 0x6a1e, 0x6892,
86 0x6707, 0x657d, 0x63f4, 0x626c, 0x60e6, 0x5f60, 0x5ddc, 0x5c59,
87 0x5ad8, 0x5958, 0x57d9, 0x565c, 0x54e0, 0x5367, 0x51ef, 0x5078,
88 0x4f04, 0x4d91, 0x4c21, 0x4ab2, 0x4946, 0x47db, 0x4673, 0x450d,
89 0x43a9, 0x4248, 0x40e8, 0x3f8c, 0x3e32, 0x3cda, 0x3b85, 0x3a33,
90 0x38e3, 0x3796, 0x364c, 0x3504, 0x33c0, 0x327e, 0x3140, 0x3004,
91 0x2ecc, 0x2d97, 0x2c65, 0x2b36, 0x2a0a, 0x28e2, 0x27bd, 0x269b,
92 0x257d, 0x2463, 0x234c, 0x2238, 0x2128, 0x201c, 0x1f14, 0x1e0f,
93 0x1d0e, 0x1c11, 0x1b17, 0x1a22, 0x1930, 0x1843, 0x1759, 0x1674,
94 0x1592, 0x14b5, 0x13dc, 0x1307, 0x1236, 0x1169, 0x10a1, 0xfdd,
95 0xf1d, 0xe62, 0xdab, 0xcf8, 0xc4a, 0xba0, 0xafb, 0xa5a,
96 0x9be, 0x927, 0x894, 0x805, 0x77b, 0x6f6, 0x676, 0x5fa,
97 0x583, 0x511, 0x4a3, 0x43a, 0x3d6, 0x377, 0x31c, 0x2c6,
98 0x276, 0x22a, 0x1e2, 0x1a0, 0x163, 0x12a, 0xf6, 0xc8,
99 0x9e, 0x79, 0x59, 0x3e, 0x27, 0x16, 0xa, 0x2,
100 0x0, 0x2, 0xa, 0x16, 0x27, 0x3e, 0x59, 0x79,
101 0x9e, 0xc8, 0xf6, 0x12a, 0x163, 0x1a0, 0x1e2, 0x22a,
102 0x276, 0x2c6, 0x31c, 0x377, 0x3d6, 0x43a, 0x4a3, 0x511,
103 0x583, 0x5fa, 0x676, 0x6f6, 0x77b, 0x805, 0x894, 0x927,
104 0x9be, 0xa5a, 0xafb, 0xba0, 0xc4a, 0xcf8, 0xdab, 0xe62,
105 0xf1d, 0xfdd, 0x10a1, 0x1169, 0x1236, 0x1307, 0x13dc, 0x14b5,
106 0x1592, 0x1674, 0x1759, 0x1843, 0x1930, 0x1a22, 0x1b17, 0x1c11,
107 0x1d0e, 0x1e0f, 0x1f14, 0x201c, 0x2128, 0x2238, 0x234c, 0x2463,
108 0x257d, 0x269b, 0x27bd, 0x28e2, 0x2a0a, 0x2b36, 0x2c65, 0x2d97,
109 0x2ecc, 0x3004, 0x3140, 0x327e, 0x33c0, 0x3504, 0x364c, 0x3796,
110 0x38e3, 0x3a33, 0x3b85, 0x3cda, 0x3e32, 0x3f8c, 0x40e8, 0x4248,
111 0x43a9, 0x450d, 0x4673, 0x47db, 0x4946, 0x4ab2, 0x4c21, 0x4d91,
112 0x4f04, 0x5078, 0x51ef, 0x5367, 0x54e0, 0x565c, 0x57d9, 0x5958,
113 0x5ad8, 0x5c59, 0x5ddc, 0x5f60, 0x60e6, 0x626c, 0x63f4, 0x657d,
114 0x6707, 0x6892, 0x6a1e, 0x6baa, 0x6d38, 0x6ec6, 0x7054, 0x71e4,
115 0x7374, 0x7504, 0x7695, 0x7826, 0x79b8, 0x7b49, 0x7cdb, 0x7e6d,
116 };
117
118 /***************************************************************************//**
119 * @brief ad5766_core_write
120 *******************************************************************************/
121 int32_t ad5766_core_write(ad5766_core *core,
122 uint32_t reg_addr,
123 uint32_t reg_data)
124 {
125 Xil_Out32((core->core_baseaddr + reg_addr), reg_data);
126
127 return 0;
128 }
129
130 /***************************************************************************//**
131 * @brief ad5766_core_read
132 *******************************************************************************/
133 void ad5766_core_read(ad5766_core *core,
134 uint32_t reg_addr,
135 uint32_t *reg_data)
136 {
137 *reg_data = Xil_In32(core->core_baseaddr + reg_addr);
138 }
139
140 /***************************************************************************//**
141 * @brief ad5766_core_write
142 *******************************************************************************/
143 int32_t ad5766_dma_write(ad5766_core *core,
144 uint32_t reg_addr,
145 uint32_t reg_data)
146 {
147 Xil_Out32((core->dma_baseaddr + reg_addr), reg_data);
148
149 return 0;
150 }
151
152 /***************************************************************************//**
153 * @brief ad5766_core_read
154 *******************************************************************************/
155 void ad5766_dma_read(ad5766_core *core,
156 uint32_t reg_addr,
157 uint32_t *reg_data)
158 {
159 *reg_data = Xil_In32(core->dma_baseaddr + reg_addr);
160 }
161
162 /***************************************************************************//**
163 * @brief ad5766_core_setup
164 *******************************************************************************/
165 int32_t ad5766_core_setup(ad5766_core **ad_core,
166 ad5766_core_init_param init_param)
167 {
168 ad5766_core *core;
169 uint32_t ref_clk_hz = 100000000;
170 uint32_t rate_reg;
171 uint32_t clk_div = 0;
172 uint8_t spi_config = SPI_ENGINE_CONFIG_CPHA;
173 uint8_t chip_select = 0;
174 uint32_t sync_id = 0;
175 uint8_t size = 3;
176
177 core = (ad5766_core *)malloc(sizeof(*core));
178 if (!core)
179 return FAILURE;
180
181 core->core_baseaddr = init_param.core_baseaddr;
182 core->dma_baseaddr = init_param.dma_baseaddr;
183 core->dma_source_addr = init_param.dma_source_addr;
184 core->rate_hz = init_param.rate_hz;
185 core->spi_clk_hz = init_param.spi_clk_hz;
186
187 ad5766_core_write(core, 0x00001100, 0x00020); // DAC0
188 ad5766_core_write(core, 0x00001104, 0x00021); // DAC1
189 ad5766_core_write(core, 0x00001108, 0x00022); // DAC2
190 ad5766_core_write(core, 0x0000110C, 0x00023); // DAC3
191 ad5766_core_write(core, 0x00001110, 0x00024); // DAC4
192 ad5766_core_write(core, 0x00001114, 0x00025); // DAC5
193 ad5766_core_write(core, 0x00001118, 0x00026); // DAC6
194 ad5766_core_write(core, 0x0000111C, 0x00027); // DAC7
195 ad5766_core_write(core, 0x00001120, 0x00028); // DAC8
196 ad5766_core_write(core, 0x00001124, 0x00029); // DAC9
197 ad5766_core_write(core, 0x00001128, 0x0002A); // DAC10
198 ad5766_core_write(core, 0x0000112C, 0x0002B); // DAC11
199 ad5766_core_write(core, 0x00001130, 0x0002C); // DAC12
200 ad5766_core_write(core, 0x00001134, 0x0002D); // DAC13
201 ad5766_core_write(core, 0x00001138, 0x0002E); // DAC14
202 ad5766_core_write(core, 0x0000113C, 0x1002F); // DAC15
203
204 rate_reg = ref_clk_hz / core->rate_hz;
205 if (rate_reg > 0xFFFF)
206 return FAILURE;
207
208 ad5766_core_write(core, 0x00000040, 0x0003);
209 ad5766_core_write(core, 0x0000004C, rate_reg);
210
211 clk_div = (ref_clk_hz / core->spi_clk_hz) - 1;
212
213 spi_engine_write(SPI_ENGINE_REG_OFFLOAD_RESET(0), 0x0001);
214
215 spi_engine_write(SPI_ENGINE_REG_OFFLOAD_CMD_MEM(0),
216 SPI_ENGINE_CMD_WRITE(SPI_ENGINE_CMD_REG_CONFIG, spi_config));
217
218 spi_engine_write(SPI_ENGINE_REG_OFFLOAD_CMD_MEM(0),
219 SPI_ENGINE_CMD_WRITE(SPI_ENGINE_CMD_REG_CLK_DIV, clk_div));
220
221 spi_engine_write(SPI_ENGINE_REG_OFFLOAD_CMD_MEM(0),
222 SPI_ENGINE_CMD_ASSERT(1, (0xff ^ BIT(chip_select))));
223
224 spi_engine_write(SPI_ENGINE_REG_OFFLOAD_CMD_MEM(0),
225 SPI_ENGINE_CMD_TRANSFER(1, 0, size -1));
226
227 spi_engine_write(SPI_ENGINE_REG_OFFLOAD_CMD_MEM(0),
228 SPI_ENGINE_CMD_SYNC(sync_id));
229
230 spi_engine_write(SPI_ENGINE_REG_OFFLOAD_CMD_MEM(0),
231 SPI_ENGINE_CMD_ASSERT(1, 0xff));
232
233 ad5766_dma_write(core, 0x400, 0x0);
234
235 u32 no_of_samples;
236 u32 index;
237 u32 index_mem = 0;
238 u32 phase_offset;
239
240 no_of_samples = sizeof(sine_lut) / sizeof(typeof(sine_lut[0]));
241 phase_offset = no_of_samples/16;
242
243 for (index = 0; index < no_of_samples; index ++) {
244 Xil_Out16(core->dma_source_addr + (index_mem + 0) * 2,
245 sine_lut[(index + ( 0 * phase_offset)) % no_of_samples]); // DAC0
246 Xil_Out16(core->dma_source_addr + (index_mem + 1) * 2,
247 sine_lut[(index + ( 1 * phase_offset)) % no_of_samples]); // DAC1
248 Xil_Out16(core->dma_source_addr + (index_mem + 2) * 2,
249 sine_lut[(index + ( 2 * phase_offset)) % no_of_samples]); // DAC2
250 Xil_Out16(core->dma_source_addr + (index_mem + 3) * 2,
251 sine_lut[(index + ( 3 * phase_offset)) % no_of_samples]); // DAC3
252 Xil_Out16(core->dma_source_addr + (index_mem + 4) * 2,
253 sine_lut[(index + ( 4 * phase_offset)) % no_of_samples]); // DAC4
254 Xil_Out16(core->dma_source_addr + (index_mem + 5) * 2,
255 sine_lut[(index + ( 5 * phase_offset)) % no_of_samples]); // DAC5
256 Xil_Out16(core->dma_source_addr + (index_mem + 6) * 2,
257 sine_lut[(index + ( 6 * phase_offset)) % no_of_samples]); // DAC6
258 Xil_Out16(core->dma_source_addr + (index_mem + 7) * 2,
259 sine_lut[(index + ( 7 * phase_offset)) % no_of_samples]); // DAC7
260 Xil_Out16(core->dma_source_addr + (index_mem + 8) * 2,
261 sine_lut[(index + ( 8 * phase_offset)) % no_of_samples]); // DAC8
262 Xil_Out16(core->dma_source_addr + (index_mem + 9) * 2,
263 sine_lut[(index + ( 9 * phase_offset)) % no_of_samples]); // DAC9
264 Xil_Out16(core->dma_source_addr + (index_mem + 10) * 2,
265 sine_lut[(index + (10 * phase_offset)) % no_of_samples]); // DAC10
266 Xil_Out16(core->dma_source_addr + (index_mem + 11) * 2,
267 sine_lut[(index + (11 * phase_offset)) % no_of_samples]); // DAC11
268 Xil_Out16(core->dma_source_addr + (index_mem + 12) * 2,
269 sine_lut[(index + (12 * phase_offset)) % no_of_samples]); // DAC12
270 Xil_Out16(core->dma_source_addr + (index_mem + 13) * 2,
271 sine_lut[(index + (13 * phase_offset)) % no_of_samples]); // DAC13
272 Xil_Out16(core->dma_source_addr + (index_mem + 14) * 2,
273 sine_lut[(index + (14 * phase_offset)) % no_of_samples]); // DAC14
274 Xil_Out16(core->dma_source_addr + (index_mem + 15) * 2,
275 sine_lut[(index + (15 * phase_offset)) % no_of_samples]); // DAC15
276 index_mem += 16;
277 }
278
279 ad5766_dma_write(core, 0x400, 0x0);
280 ad5766_dma_write(core, 0x400, 0x1);
281 ad5766_dma_write(core, 0x414, core->dma_source_addr);
282 ad5766_dma_write(core, 0x424, 0x0);
283 ad5766_dma_write(core, 0x418, (16 * sizeof(sine_lut)) - 1);
284 ad5766_dma_write(core, 0x41C, 0x0);
285 ad5766_dma_write(core, 0x40C, 0x1);
286 ad5766_dma_write(core, 0x408, 0x1);
287
288 spi_engine_offload_enable(1);
289
290 *ad_core = core;
291
292 return SUCCESS;
293 }