-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathad7091r.c
216 lines (191 loc) · 8.07 KB
/
ad7091r.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/***************************************************************************//**
* @file AD7091R.c
* @brief Implementation of AD7091R Driver.
* @author DNechita ([email protected])
********************************************************************************
* Copyright 2012(c) Analog Devices, Inc.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* - Neither the name of Analog Devices, Inc. nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* - The use of this software may or may not infringe the patent rights
* of one or more patent holders. This license does not release you
* from the requirement that you obtain separate licenses from these
* patent holders to use this software.
* - Use of the software either in source or binary form, must be run
* on or directly connected to an Analog Devices Inc. component.
*
* THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************/
/******************************************************************************/
/***************************** Include Files **********************************/
/******************************************************************************/
#include <stdlib.h>
#include "ad7091r.h"
#include "no_os_alloc.h"
#include "no_os_device.h"
/**
* @brief Device driver compatibility list.
*/
const struct no_os_device ad7091r_device_table[] = {
{.compatible = "adi,ad7091r"},
{}
};
/******************************************************************************/
/************************ Functions Definitions *******************************/
/******************************************************************************/
/***************************************************************************//**
* @brief Initializes the communication peripheral and the initial Values for
* AD7092R Board.
*
* @param device - The device structure.
* @param init_param - The structure that contains the device initial
* parameters.
*
* @return ret - The result of the initialization procedure.
* Example: -1 - SPI peripheral was not initialized or the
* device is not present.
* 0 - SPI peripheral was initialized and the
* device is present.
*******************************************************************************/
int8_t ad7091r_init(struct ad7091r_dev **device,
struct ad7091r_init_param init_param)
{
struct ad7091r_dev *dev;
uint8_t status = 0;
uint8_t tmp_val = 0xFF;
dev = (struct ad7091r_dev *)no_os_malloc(sizeof(*dev));
if (!dev)
return -1;
status = no_os_spi_init(&dev->spi_desc, &init_param.spi_init);
/* Ensures that last state of SDO is high. */
no_os_spi_write_and_read(dev->spi_desc, &tmp_val, 1);
ad7091r_software_reset(dev);
*device = dev;
return status;
}
/***************************************************************************//**
* @brief Free the resources allocated by ad7091r_init().
*
* @param dev - The device structure.
*
* @return ret - The result of the remove procedure.
*******************************************************************************/
int32_t ad7091r_remove(struct ad7091r_dev *dev)
{
int32_t ret;
ret = no_os_spi_remove(dev->spi_desc);
no_os_free(dev);
return ret;
}
/***************************************************************************//**
* @brief Initiate a software reset of the device. The AD7091R requires the user
* to initiate a software reset when power is first applied.
*
* @param dev - The device structure.
*
* @return None.
*******************************************************************************/
void ad7091r_software_reset(struct ad7091r_dev *dev)
{
uint8_t write_byte = 0xBF;
/* Initiate a conversion. */
no_os_spi_write_and_read(dev->spi_desc, &write_byte, 1);
/* Short cycle the read operation. */
write_byte = 0xFF;
no_os_spi_write_and_read(dev->spi_desc, &write_byte, 1);
/* Perform another conversion in order to reset the device. */
write_byte = 0xBF;
no_os_spi_write_and_read(dev->spi_desc, &write_byte, 1);
}
/***************************************************************************//**
* @brief Initiates one conversion and reads back the result. During this
* process the device runs in normal mode and operates without the busy
* indicator.
*
* @param dev - The device structure.
*
* @return conversionResult - 12bit conversion result.
*******************************************************************************/
uint16_t ad7091r_read_sample(struct ad7091r_dev *dev)
{
uint16_t conversion_result = 0;
uint8_t write_byte = 0xBF;
uint8_t buffer[2] = {0xFF, 0xFF};
/* Initiate a conversion. */
no_os_spi_write_and_read(dev->spi_desc, &write_byte, 1);
/* Read conversion data. */
no_os_spi_write_and_read(dev->spi_desc, buffer, 2);
conversion_result = (buffer[0] << 8) + buffer[1];
conversion_result >>= 4;
return conversion_result;
}
/***************************************************************************//**
* @brief Puts the device in power-down mode.
*
* @param dev - The device structure.
*
* @return None.
*******************************************************************************/
void ad7091r_power_down(struct ad7091r_dev *dev)
{
uint8_t buffer[2] = {0, 0};
uint8_t write_value = 0x00;
/* Initiate a conversion. */
no_os_spi_write_and_read(dev->spi_desc, &write_value, 1);
/* Perform a dummy read. */
no_os_spi_write_and_read(dev->spi_desc, buffer, 2);
}
/***************************************************************************//**
* @brief Powers up the device by pulling CONVST high. After calling this
* function, a time delay is required before initiating another
* conversion:
* - 50 ms delay, when internal reference is used.(with 2.2uF capacitor)
* - 100 us delay, when external reference is used.
*
* @param dev - The device structure.
*
* @return None.
*******************************************************************************/
void ad7091r_power_up(struct ad7091r_dev *dev)
{
uint8_t write_value = 0xFF;
/* Pull CONVST signal high. */
no_os_spi_write_and_read(dev->spi_desc, &write_value, 1);
}
/***************************************************************************//**
* @brief Converts a 12-bit raw sample to volts.
*
* @param raw_sample - 12-bit data sample.
* @param v_ref - The value of the voltage reference used by the device.
*
* @return voltage - The result of the conversion expressed as volts.
*******************************************************************************/
float ad7091r_convert_to_volts(int16_t raw_sample, float v_ref)
{
float voltage = 0;
if(v_ref == 0) {
v_ref = 2.5;
}
voltage = v_ref * (float)raw_sample / (1 << 12);
return voltage;
}