Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/ad-fmcjesdadc1-ebz/ad9517.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file ad9517.c
3 * @brief Implementation of AD9517 Driver.
4 * @author DBogdan ([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 <stdint.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include "platform_drivers.h"
47 #include "ad9517.h"
48
49 /******************************************************************************/
50 /************************ Variables Definitions *******************************/
51 /******************************************************************************/
52 uint8_t ad9517_slave_select;
53
54 /***************************************************************************//**
55 * @brief ad9517_spi_read
56 *******************************************************************************/
57 int32_t ad9517_spi_read(ad9517_dev *dev,
58 uint16_t reg_addr,
59 uint8_t *reg_data)
60 {
61 uint8_t buf[4];
62 int32_t ret;
63
64 buf[0] = 0x84;
65 buf[1] = 0x80 | (reg_addr >> 8);
66 buf[2] = reg_addr & 0xFF;
67 buf[3] = 0x00;
68
69 ret = spi_write_and_read(&dev->spi_dev, buf, 4);
70 *reg_data = buf[3];
71
72 return ret;
73 }
74
75 /***************************************************************************//**
76 * @brief ad9517_spi_write
77 *******************************************************************************/
78 int32_t ad9517_spi_write(ad9517_dev *dev,
79 uint16_t reg_addr,
80 uint8_t reg_data)
81 {
82 uint8_t buf[4];
83 int32_t ret;
84
85 buf[0] = 0x84;
86 buf[1] = reg_addr >> 8;
87 buf[2] = reg_addr & 0xFF;
88 buf[3] = reg_data;
89
90 ret = spi_write_and_read(&dev->spi_dev, buf, 4);
91
92 return ret;
93 }
94
95 /***************************************************************************//**
96 * @brief ad9517_setup
97 *******************************************************************************/
98 int32_t ad9517_setup(ad9517_dev **device,
99 ad9517_init_param init_param)
100 {
101 ad9517_dev *dev;
102 uint8_t stat;
103 int32_t ret;
104
105 dev = (ad9517_dev *)malloc(sizeof(*dev));
106 if (!dev) {
107 return -1;
108 }
109
110 dev->spi_dev.chip_select = init_param.spi_chip_select;
111 dev->spi_dev.mode = init_param.spi_mode;
112 dev->spi_dev.device_id = init_param.spi_device_id;
113 dev->spi_dev.type = init_param.spi_type;
114 ret = spi_init(&dev->spi_dev);
115
116 ad9517_spi_write(dev, 0x0010, 0x7c);
117 ad9517_spi_write(dev, 0x0014, 0x05);
118 ad9517_spi_write(dev, 0x0016, 0x05);
119 ad9517_spi_write(dev, 0x001c, 0x02);
120 ad9517_spi_write(dev, 0x00f1, 0x08);
121 ad9517_spi_write(dev, 0x00f5, 0x08);
122 ad9517_spi_write(dev, 0x0140, 0x42);
123 ad9517_spi_write(dev, 0x0141, 0x42);
124 ad9517_spi_write(dev, 0x0142, 0x42);
125 ad9517_spi_write(dev, 0x0143, 0x43);
126 ad9517_spi_write(dev, 0x0190, 0x21);
127 ad9517_spi_write(dev, 0x0191, 0x00);
128 ad9517_spi_write(dev, 0x0197, 0x80);
129 ad9517_spi_write(dev, 0x0198, 0x02);
130 ad9517_spi_write(dev, 0x0199, 0x21);
131 ad9517_spi_write(dev, 0x019c, 0x20);
132 ad9517_spi_write(dev, 0x019e, 0x21);
133 ad9517_spi_write(dev, 0x01a0, 0x11);
134 ad9517_spi_write(dev, 0x01a1, 0x20);
135 ad9517_spi_write(dev, 0x01e0, 0x00);
136 ad9517_spi_write(dev, 0x01e1, 0x02);
137 ad9517_spi_write(dev, 0x0232, 0x01);
138 ad9517_spi_write(dev, 0x0018, 0x06);
139 ad9517_spi_write(dev, 0x0232, 0x01);
140 mdelay(50);
141
142 ad9517_spi_write(dev, 0x0018, 0x07);
143 ad9517_spi_write(dev, 0x0232, 0x01);
144 mdelay(50);
145
146 ad9517_spi_read(dev, 0x001f, &stat);
147 printf("AD9517 PLL %s.\n", stat & 0x01 ? "ok" : "errors");
148
149 *device = dev;
150
151 return ret;
152 }