no-OS
ad7799.h
Go to the documentation of this file.
1 /***************************************************************************/
33 #ifndef AD7799_H_
34 #define AD7799_H_
35 
36 /******************************************************************************/
37 /***************************** Include Files **********************************/
38 /******************************************************************************/
39 #include <stdint.h>
40 #include <stdbool.h>
41 #include "no_os_spi.h"
42 
43 /******************************************************************************/
44 /********************** Macros and Types Declarations *************************/
45 /******************************************************************************/
46 
47 /*AD7799 Registers*/
48 #define AD7799_REG_COMM 0x0 /* Communications Register(WO, 8-bit) */
49 #define AD7799_REG_STAT 0x0 /* Status Register (RO, 8-bit) */
50 #define AD7799_REG_MODE 0x1 /* Mode Register (RW, 16-bit) */
51 #define AD7799_REG_CONF 0x2 /* Configuration Register (RW, 16-bit)*/
52 #define AD7799_REG_DATA 0x3 /* Data Register (RO, 16-/24-bit) */
53 #define AD7799_REG_ID 0x4 /* ID Register (RO, 8-bit) */
54 #define AD7799_REG_IO 0x5 /* IO Register (RO, 8-bit) */
55 #define AD7799_REG_OFFSET 0x6 /* Offset Register (RW, 24-bit) */
56 #define AD7799_REG_FULLSCALE 0x7 /* Full-Scale Register (RW, 24-bit) */
57 
58 /* AD7799 Polarity */
59 #define AD7799_BIPOLAR 0x0 /* Bipolar bit */
60 #define AD7799_UNIPOLAR 0x1 /* Unipolar bit */
61 
62 /* Communications Register Bit Designations (AD7799_REG_COMM) */
63 #define AD7799_COMM_WEN 0x80 /* Write Enable */
64 #define AD7799_COMM_WRITE 0x00 /* Write Operation */
65 #define AD7799_COMM_READ 0x40 /* Read Operation */
66 #define AD7799_COMM_ADDR(x) (((x) & 0x7) << 3) /* Register Address */
67 #define AD7799_COMM_CREAD 0x04 /* Continuous Read */
68 
69 /* Status Register Bit Designations (AD7799_REG_STAT) */
70 #define AD7799_STAT_RDY 0x80 /* Ready */
71 #define AD7799_STAT_ERR 0x40 /* Error (Overrange, Underrange) */
72 #define AD7799_STAT_CH3 0x04 /* Channel 3 */
73 #define AD7799_STAT_CH2 (1 << 1) /* Channel 2 */
74 #define AD7799_STAT_CH1 (1 << 0) /* Channel 1 */
75 
76 /* Mode Register Bit Designations (AD7799_REG_MODE) */
77 #define AD7799_MODE_SEL(x) (((x) & 0x7) << 13) /* Operation Mode Select */
78 #define AD7799_MODE_PSW(x) 0x1000 /* Power Switch Control Bit */
79 #define AD7799_MODE_RATE(x) ((x) & 0xF) /* Filter Update Rate */
80 
81 /* AD7799_MODE_SEL(x) options */
82 #define AD7799_MODE_CONT 0x0 /* Continuous Conversion Mode */
83 #define AD7799_MODE_SINGLE 0x1 /* Single Conversion Mode */
84 #define AD7799_MODE_IDLE 0x2 /* Idle Mode */
85 #define AD7799_MODE_PWRDN 0x3 /* Power-Down Mode */
86 #define AD7799_MODE_CAL_INT_ZERO 0x4 /* Internal Zero-Scale Calibration */
87 #define AD7799_MODE_CAL_INT_FULL 0x5 /* Internal Full-Scale Calibration */
88 #define AD7799_MODE_CAL_SYS_ZERO 0x6 /* System Zero-Scale Calibration */
89 #define AD7799_MODE_CAL_SYS_FULL 0x7 /* System Full-Scale Calibration */
90 
91 /* Configuration Register Bit Designations (AD7799_REG_CONF) */
92 #define AD7799_CONF_BO_EN 0x2000 /* Burnout Current */
93 #define AD7799_CONF_POLARITY(x) (((x) & 0x1) << 12) /* Unipolar/Bipolar */
94 #define AD7799_CONF_GAIN(x) (((x) & 0x7) << 8) /* Gain Select */
95 #define AD7799_CONF_REFDET(x) (((x) & 0x1) << 5) /* Reference detect */
96 #define AD7799_CONF_BUF 0x10 /* Buffered Mode Enable */
97 #define AD7799_CONF_CHAN(x) ((x) & 0x7) /* Channel select */
98 
99 /* AD7799_CONF_GAIN(x) options */
100 #define AD7799_GAIN_1 0x0
101 #define AD7799_GAIN_2 0x1
102 #define AD7799_GAIN_4 0x2
103 #define AD7799_GAIN_8 0x3
104 #define AD7799_GAIN_16 0x4
105 #define AD7799_GAIN_32 0x5
106 #define AD7799_GAIN_64 0x6
107 #define AD7799_GAIN_128 0x7
108 
109 /* AD7799 Register size */
110 #define AD7799_REG_SIZE_1B 0x1
111 #define AD7799_REG_SIZE_2B 0x2
112 #define AD7799_REG_SIZE_3B 0x3
113 
114 /* AD7799_CONF_REFDET(x) options */
115 #define AD7799_REFDET_ENA 0x1
116 #define AD7799_REFDET_DIS 0x0
117 
118 /* AD7799_CONF_CHAN(x) options */
119 #define AD7799_CH_AIN1P_AIN1M 0x0 /* AIN1(+) - AIN1(-) */
120 #define AD7799_CH_AIN2P_AIN2M 0x1 /* AIN2(+) - AIN2(-) */
121 #define AD7799_CH_AIN3P_AIN3M 0x2 /* AIN3(+) - AIN3(-) */
122 #define AD7799_CH_AIN1M_AIN1M 0x3 /* AIN1(-) - AIN1(-) */
123 #define AD7799_CH_AVDD_MONITOR 0x7 /* AVDD Monitor */
124 
125 /* ID Register Bit Designations (AD7799_REG_ID) */
126 #define AD7799_ID_MASK 0xF
127 
128 /* AD7799 Configuration Mask */
129 #define AD7799_REG_MASK 0xF
130 
131 /* IO (Excitation Current Sources) Register Bit Designations (AD7799_REG_IO) */
132 #define AD7799_IOEN 0x40
133 #define AD7799_IO1(x) (((x) & 0x1) << 4)
134 #define AD7799_IO2(x) (((x) & 0x1) << 5)
135 
136 /* AD7799 Timeout */
137 #define AD7799_TIMEOUT 0xFFFF
138 
139 /* AD7799 Reset Sequence */
140 #define AD7799_RESET_DATA 0xFF
141 
148  ID_AD7798 = 0x8,
150  ID_AD7799 = 0x9
151 };
152 
158 };
159 
164 struct ad7799_dev {
168  uint8_t chip_type;
170  const uint8_t *reg_size;
172  uint8_t gain;
174  bool polarity;
176  uint32_t vref_mv;
179 };
180 
191  uint8_t gain;
193  bool polarity;
195  uint32_t vref_mv;
198 };
199 
200 /******************************************************************************/
201 /************************ Functions Declarations ******************************/
202 /******************************************************************************/
203 
204 /* Read device register. */
205 int32_t ad7799_read(struct ad7799_dev *device, uint8_t reg_addr,
206  uint32_t *reg_data);
207 
208 /* Write device register */
209 int32_t ad7799_write(struct ad7799_dev *device, uint8_t reg_addr,
210  uint32_t reg_data);
211 
212 /* Software reset of the device. */
213 int32_t ad7799_reset(struct ad7799_dev *device);
214 
215 /* Set the device mode. */
216 int32_t ad7799_set_mode(struct ad7799_dev *device, uint8_t mode);
217 
218 /* Select the ADC channel. */
219 int32_t ad7799_set_channel(struct ad7799_dev *device, uint8_t ch);
220 
221 /* Read specific ADC channel. */
222 int32_t ad7799_get_channel(struct ad7799_dev *device, uint8_t ch,
223  uint32_t *reg_data);
224 
225 /* Read specific ADC channel data with the specified precision. */
226 int32_t ad7799_read_channel(struct ad7799_dev *device, uint8_t ch,
227  int32_t *data_scaled);
228 
229 /* Set the ADC gain. */
230 int32_t ad7799_set_gain(struct ad7799_dev *device, uint8_t gain);
231 
232 /* Get the ADC gain. */
233 int32_t ad7799_get_gain(struct ad7799_dev *device, uint8_t *gain);
234 
235 /* Enable or disable the reference detect function. */
236 int32_t ad7799_set_refdet(struct ad7799_dev *device, uint8_t ref_en);
237 
238 /* Set ADC polarity. */
239 int32_t ad7799_set_polarity(struct ad7799_dev *device, uint8_t polarity);
240 
241 /* Check the status of the device. */
242 int32_t ad7799_dev_ready(struct ad7799_dev *device);
243 
244 /* Initialize the device. */
245 int32_t ad7799_init(struct ad7799_dev **device,
246  const struct ad7799_init_param *init_param);
247 
248 /* Remove the device and release resources. */
249 int32_t ad7799_remove(struct ad7799_dev *device);
250 
251 #endif /* AD7799_H_ */
ad7799_set_gain
int32_t ad7799_set_gain(struct ad7799_dev *device, uint8_t gain)
Set the ADC gain.
Definition: ad7799.c:277
ad7799_precision
ad7799_precision
Definition: ad7799.h:153
AD7799_COMM_ADDR
#define AD7799_COMM_ADDR(x)
Definition: ad7799.h:66
timeout
uint32_t timeout
Definition: ad413x.c:49
no_os_alloc.h
ad7799_init
int32_t ad7799_init(struct ad7799_dev **device, const struct ad7799_init_param *init_param)
Initialize the device.
Definition: ad7799.c:391
ad7799_dev::chip_type
uint8_t chip_type
Definition: ad7799.h:168
ad7799_set_mode
int32_t ad7799_set_mode(struct ad7799_dev *device, uint8_t mode)
Set the device mode.
Definition: ad7799.c:157
ad7799_dev
AD7798/AD7799 Device description.
Definition: ad7799.h:164
no_os_spi_write_and_read
int32_t no_os_spi_write_and_read(struct no_os_spi_desc *desc, uint8_t *data, uint16_t bytes_number)
Write and read data to/from SPI.
Definition: no_os_spi.c:159
AD7799_REG_DATA
#define AD7799_REG_DATA
Definition: ad7799.h:52
no_os_spi.h
Header file of SPI Interface.
ad7799_read_channel
int32_t ad7799_read_channel(struct ad7799_dev *device, uint8_t ch, int32_t *data_scaled)
Read data from specific ADC channel with specified precision.
Definition: ad7799.c:239
AD7799_RESET_DATA
#define AD7799_RESET_DATA
Definition: ad7799.h:140
AD7799_REG_FULLSCALE
#define AD7799_REG_FULLSCALE
Definition: ad7799.h:56
ad7799_get_channel
int32_t ad7799_get_channel(struct ad7799_dev *device, uint8_t ch, uint32_t *reg_data)
Read specific ADC channel.
Definition: ad7799.c:208
ad7799_dev::spi_desc
struct no_os_spi_desc * spi_desc
Definition: ad7799.h:166
no_os_spi_init_param::mode
enum no_os_spi_mode mode
Definition: no_os_spi.h:148
device
Definition: ad9361_util.h:69
ID_AD7798
@ ID_AD7798
Definition: ad7799.h:148
ad7799_dev::reg_size
const uint8_t * reg_size
Definition: ad7799.h:170
ad7799_get_gain
int32_t ad7799_get_gain(struct ad7799_dev *device, uint8_t *gain)
Get the ADC gain.
Definition: ad7799.c:298
no_os_calloc
void * no_os_calloc(size_t nitems, size_t size)
Allocate memory and return a pointer to it, set memory to 0.
Definition: chibios_alloc.c:54
ad7799_remove
int32_t ad7799_remove(struct ad7799_dev *device)
Remove the device and release resources.
Definition: ad7799.c:474
AD7799_REG_SIZE_1B
#define AD7799_REG_SIZE_1B
Definition: ad7799.h:110
AD7799_CONF_GAIN
#define AD7799_CONF_GAIN(x)
Definition: ad7799.h:94
ad7799_set_polarity
int32_t ad7799_set_polarity(struct ad7799_dev *device, uint8_t polarity)
Set ADC polarity.
Definition: ad7799.c:343
ad7799_init_param::precision
enum ad7799_precision precision
Definition: ad7799.h:197
ad7799_init_param::vref_mv
uint32_t vref_mv
Definition: ad7799.h:195
ad7799_set_mode
int32_t ad7799_set_mode(struct ad7799_dev *device, uint8_t mode)
Set the device mode.
Definition: ad7799.c:157
no_os_error.h
Error codes definition.
ad7799_dev::vref_mv
uint32_t vref_mv
Definition: ad7799.h:176
ad7799_init_param::gain
uint8_t gain
Definition: ad7799.h:191
AD7799_REG_COMM
#define AD7799_REG_COMM
Definition: ad7799.h:48
ad7799_set_polarity
int32_t ad7799_set_polarity(struct ad7799_dev *device, uint8_t polarity)
Set ADC polarity.
Definition: ad7799.c:343
ad7799_read
int32_t ad7799_read(struct ad7799_dev *device, uint8_t reg_addr, uint32_t *reg_data)
Read device register.
Definition: ad7799.c:80
AD7799_REG_OFFSET
#define AD7799_REG_OFFSET
Definition: ad7799.h:55
ad7799_set_channel
int32_t ad7799_set_channel(struct ad7799_dev *device, uint8_t ch)
Select the ADC channel.
Definition: ad7799.c:186
AD7799_STAT_RDY
#define AD7799_STAT_RDY
Definition: ad7799.h:70
ad7799_write
int32_t ad7799_write(struct ad7799_dev *device, uint8_t reg_addr, uint32_t reg_data)
Write device register.
Definition: ad7799.c:113
AD7799_COMM_READ
#define AD7799_COMM_READ
Definition: ad7799.h:65
ad7799.h
Header file of AD7798/AD7799 Driver.
ad7799_write
int32_t ad7799_write(struct ad7799_dev *device, uint8_t reg_addr, uint32_t reg_data)
Write device register.
Definition: ad7799.c:113
no_os_spi_desc
Structure holding SPI descriptor.
Definition: no_os_spi.h:192
ad7799_dev_ready
int32_t ad7799_dev_ready(struct ad7799_dev *device)
Read the /RDY bit of status register and check the status of the device.
Definition: ad7799.c:364
ad7799_init
int32_t ad7799_init(struct ad7799_dev **device, const struct ad7799_init_param *init_param)
Initialize the device.
Definition: ad7799.c:391
AD7799_PRECISION_UV
@ AD7799_PRECISION_UV
Definition: ad7799.h:157
AD7799_MODE_SINGLE
#define AD7799_MODE_SINGLE
Definition: ad7799.h:83
ad7799_init_param::polarity
bool polarity
Definition: ad7799.h:193
AD7799_ID_MASK
#define AD7799_ID_MASK
Definition: ad7799.h:126
AD7799_COMM_WRITE
#define AD7799_COMM_WRITE
Definition: ad7799.h:64
ad7799_set_refdet
int32_t ad7799_set_refdet(struct ad7799_dev *device, uint8_t ref_en)
Enable or disable the reference detect function.
Definition: ad7799.c:320
AD7799_REG_IO
#define AD7799_REG_IO
Definition: ad7799.h:54
ad7799_read_channel
int32_t ad7799_read_channel(struct ad7799_dev *device, uint8_t ch, int32_t *data_scaled)
Read data from specific ADC channel with specified precision.
Definition: ad7799.c:239
AD7799_CONF_POLARITY
#define AD7799_CONF_POLARITY(x)
Definition: ad7799.h:93
AD7799_REG_CONF
#define AD7799_REG_CONF
Definition: ad7799.h:51
AD7799_REG_SIZE_2B
#define AD7799_REG_SIZE_2B
Definition: ad7799.h:111
ad7799_dev::precision
enum ad7799_precision precision
Definition: ad7799.h:178
ad7799_get_channel
int32_t ad7799_get_channel(struct ad7799_dev *device, uint8_t ch, uint32_t *reg_data)
Read specific ADC channel.
Definition: ad7799.c:208
no_os_free
void no_os_free(void *ptr)
Deallocate memory previously allocated by a call to no_os_calloc or no_os_malloc.
Definition: chibios_alloc.c:69
ad7799_init_param::spi_init
struct no_os_spi_init_param spi_init
Definition: ad7799.h:187
AD7799_REG_MASK
#define AD7799_REG_MASK
Definition: ad7799.h:129
ad7799_dev::gain
uint8_t gain
Definition: ad7799.h:172
ad7799_get_gain
int32_t ad7799_get_gain(struct ad7799_dev *device, uint8_t *gain)
Get the ADC gain.
Definition: ad7799.c:298
init_param
struct ad7616_init_param init_param
Definition: ad7616_sdz.c:113
ad7799_set_refdet
int32_t ad7799_set_refdet(struct ad7799_dev *device, uint8_t ref_en)
Enable or disable the reference detect function.
Definition: ad7799.c:320
ad7799_dev::polarity
bool polarity
Definition: ad7799.h:174
ad7799_reset
int32_t ad7799_reset(struct ad7799_dev *device)
Software reset of the device.
Definition: ad7799.c:139
ad7799_remove
int32_t ad7799_remove(struct ad7799_dev *device)
Remove the device and release resources.
Definition: ad7799.c:474
AD7799_REG_SIZE_3B
#define AD7799_REG_SIZE_3B
Definition: ad7799.h:112
AD7799_TIMEOUT
#define AD7799_TIMEOUT
Definition: ad7799.h:137
AD7799_MODE_SEL
#define AD7799_MODE_SEL(x)
Definition: ad7799.h:77
no_os_spi_remove
int32_t no_os_spi_remove(struct no_os_spi_desc *desc)
Free the resources allocated by no_os_spi_init().
Definition: no_os_spi.c:116
ad7799_type
ad7799_type
Device type (AD7798/AD7799)
Definition: ad7799.h:146
ad7799_init_param::chip_type
enum ad7799_type chip_type
Definition: ad7799.h:189
AD7799_REG_ID
#define AD7799_REG_ID
Definition: ad7799.h:53
AD7799_CONF_REFDET
#define AD7799_CONF_REFDET(x)
Definition: ad7799.h:95
no_os_spi_init
int32_t no_os_spi_init(struct no_os_spi_desc **desc, const struct no_os_spi_init_param *param)
Initialize the SPI communication peripheral.
Definition: no_os_spi.c:52
ad7799_dev_ready
int32_t ad7799_dev_ready(struct ad7799_dev *device)
Read the /RDY bit of status register and check the status of the device.
Definition: ad7799.c:364
AD7799_REG_STAT
#define AD7799_REG_STAT
Definition: ad7799.h:49
AD7799_CONF_CHAN
#define AD7799_CONF_CHAN(x)
Definition: ad7799.h:97
ad7799_init_param
AD7798/AD7799 Device initialization parameters.
Definition: ad7799.h:185
ad7799_reset
int32_t ad7799_reset(struct ad7799_dev *device)
Software reset of the device.
Definition: ad7799.c:139
AD7799_PRECISION_MV
@ AD7799_PRECISION_MV
Definition: ad7799.h:155
AD7799_REG_MODE
#define AD7799_REG_MODE
Definition: ad7799.h:50
ad7799_set_gain
int32_t ad7799_set_gain(struct ad7799_dev *device, uint8_t gain)
Set the ADC gain.
Definition: ad7799.c:277
ad7799_read
int32_t ad7799_read(struct ad7799_dev *device, uint8_t reg_addr, uint32_t *reg_data)
Read device register.
Definition: ad7799.c:80
ad7799_set_channel
int32_t ad7799_set_channel(struct ad7799_dev *device, uint8_t ch)
Select the ADC channel.
Definition: ad7799.c:186
chip_id
chip_id
Definition: ad9172.h:51
no_os_spi_init_param
Structure holding the parameters for SPI initialization.
Definition: no_os_spi.h:140
ID_AD7799
@ ID_AD7799
Definition: ad7799.h:150