adc23 2.1.0.0
adc23.h
Go to the documentation of this file.
1/****************************************************************************
2** Copyright (C) 2020 MikroElektronika d.o.o.
3** Contact: https://www.mikroe.com/contact
4**
5** Permission is hereby granted, free of charge, to any person obtaining a copy
6** of this software and associated documentation files (the "Software"), to deal
7** in the Software without restriction, including without limitation the rights
8** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9** copies of the Software, and to permit persons to whom the Software is
10** furnished to do so, subject to the following conditions:
11** The above copyright notice and this permission notice shall be
12** included in all copies or substantial portions of the Software.
13**
14** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
19** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20** USE OR OTHER DEALINGS IN THE SOFTWARE.
21****************************************************************************/
22
28#ifndef ADC23_H
29#define ADC23_H
30
31#ifdef __cplusplus
32extern "C"{
33#endif
34
39#ifdef PREINIT_SUPPORTED
40#include "preinit.h"
41#endif
42
43#ifdef MikroCCoreVersion
44 #if MikroCCoreVersion >= 1
45 #include "delays.h"
46 #endif
47#endif
48
49#include "drv_digital_out.h"
50#include "drv_digital_in.h"
51#include "drv_spi_master.h"
52#include "spi_specifics.h"
53
74#define ADC_REG_DEV_ID 0x00
75#define ADC_REG_REV_ID 0x01
76#define ADC_REG_STATUS 0x02
77#define ADC_REG_CONTROL 0x03
78#define ADC_REG_MUX 0x04
79#define ADC_REG_CONFIG1 0x05
80#define ADC_REG_CONFIG2 0x06
81#define ADC_REG_CONFIG3 0x07
82#define ADC_REG_CONFIG4 0x08
83#define ADC_REG_OFFSET2 0x09
84#define ADC_REG_OFFSET1 0x0A
85#define ADC_REG_OFFSET0 0x0B
86#define ADC_REG_GAIN2 0x0C
87#define ADC_REG_GAIN1 0x0D
88#define ADC_REG_GAIN0 0x0E
89#define ADC_REG_CRC 0x0F
90
91 // adc23_reg
92
102#define ADC_CMD_NO_OPERATION 0x00
103#define ADC_CMD_READ_REG 0x40
104#define ADC_CMD_WRITE_REG 0x80
105
106 // adc23_cmd
107
122#define ADC23_INT_VREF_2_5V 2.5f
123
128#define ADC23_SET_CTRL_STOP 0x01
129#define ADC23_SET_CTRL_START 0x02
130#define ADC23_SET_CTRL_SW_RESET 0x58
131#define ADC23_MAX_REG_ADDR 0x0F
132
137#define ADC23_CONV_V_TO_MV 1000.0f
138
143#define ADC23_FULL_SCALE_OUT_DATA_RANGE 0x7FFFFFul
144
153#define ADC23_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
154#define ADC23_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
155
156 // adc23_set
157
172#define ADC23_MAP_MIKROBUS( cfg, mikrobus ) \
173 cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
174 cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
175 cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
176 cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
177 cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
178 cfg.str = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
179 cfg.rdy = MIKROBUS( mikrobus, MIKROBUS_INT )
180
181 // adc23_map
182 // adc23
183
188typedef struct
189{
190 // Output pins
191 digital_out_t rst;
192 digital_out_t str;
194 // Input pins
195 digital_in_t rdy;
197 // Modules
198 spi_master_t spi;
200 pin_name_t chip_select;
202 float vref;
203
204} adc23_t;
205
210typedef struct
211{
212 // Communication gpio pins
213 pin_name_t miso;
214 pin_name_t mosi;
215 pin_name_t sck;
216 pin_name_t cs;
218 // Additional gpio pins
219 pin_name_t rst;
220 pin_name_t str;
221 pin_name_t rdy;
223 // static variable
224 uint32_t spi_speed;
225 spi_master_mode_t spi_mode;
226 spi_master_chip_select_polarity_t cs_polarity;
228 float vref;
229
231
236typedef enum
237{
239 ADC23_ERROR = -1
240
242
259
273err_t adc23_init ( adc23_t *ctx, adc23_cfg_t *cfg );
274
288
303err_t adc23_generic_write ( adc23_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
304
319err_t adc23_generic_read ( adc23_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
320
335err_t adc23_generic_transfer ( adc23_t *ctx, uint8_t *data_in, uint8_t *data_out, uint8_t len );
336
353err_t adc23_read_reg_cmd ( adc23_t *ctx, uint8_t reg, uint16_t *data_out, uint8_t *reg_data_out );
354
372err_t adc23_read_reg_data ( adc23_t *ctx, uint8_t reg, uint32_t *data_out, uint8_t *reg_data_out );
373
389err_t adc23_write_reg_cmd ( adc23_t *ctx, uint8_t reg, uint8_t data_in, uint16_t *data_out );
390
406err_t adc23_write_reg_data ( adc23_t *ctx, uint8_t reg, uint8_t data_in, uint32_t *data_out );
407
420err_t adc23_sw_reset ( adc23_t *ctx );
421
435
449
463err_t adc23_read_conversion_data ( adc23_t *ctx, int32_t *adc_data );
464
479err_t adc23_get_voltage ( adc23_t *ctx, float *voltage );
480
494
508
522
535
536#ifdef __cplusplus
537}
538#endif
539#endif // ADC23_H
540
541 // adc23
542
543// ------------------------------------------------------------------------ END
adc23_return_value_t
ADC 23 Click return value data.
Definition adc23.h:237
@ ADC23_ERROR
Definition adc23.h:239
@ ADC23_OK
Definition adc23.h:238
err_t adc23_read_reg_cmd(adc23_t *ctx, uint8_t reg, uint16_t *data_out, uint8_t *reg_data_out)
ADC 23 read register command function.
err_t adc23_stop_conversion(adc23_t *ctx)
ADC 23 stop conversion function.
err_t adc23_default_cfg(adc23_t *ctx)
ADC 23 default configuration function.
err_t adc23_write_reg_data(adc23_t *ctx, uint8_t reg, uint8_t data_in, uint32_t *data_out)
ADC 23 write register data function.
void adc23_set_stop(adc23_t *ctx)
ADC 23 set stop function.
uint8_t adc23_get_data_ready(adc23_t *ctx)
ADC 23 get data ready function.
void adc23_set_start(adc23_t *ctx)
ADC 23 set start function.
err_t adc23_generic_read(adc23_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
ADC 23 data reading function.
err_t adc23_get_voltage(adc23_t *ctx, float *voltage)
ADC 23 read get voltage level function.
err_t adc23_init(adc23_t *ctx, adc23_cfg_t *cfg)
ADC 23 initialization function.
err_t adc23_read_reg_data(adc23_t *ctx, uint8_t reg, uint32_t *data_out, uint8_t *reg_data_out)
ADC 23 read register data function.
void adc23_hw_reset(adc23_t *ctx)
ADC 23 hardware reset function.
err_t adc23_start_conversion(adc23_t *ctx)
ADC 23 start conversion function.
void adc23_cfg_setup(adc23_cfg_t *cfg)
ADC 23 configuration object setup function.
err_t adc23_read_conversion_data(adc23_t *ctx, int32_t *adc_data)
ADC 23 read conversion data function.
err_t adc23_generic_write(adc23_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
ADC 23 data writing function.
err_t adc23_write_reg_cmd(adc23_t *ctx, uint8_t reg, uint8_t data_in, uint16_t *data_out)
ADC 23 write register command function.
err_t adc23_generic_transfer(adc23_t *ctx, uint8_t *data_in, uint8_t *data_out, uint8_t len)
ADC 23 data transfer function.
err_t adc23_sw_reset(adc23_t *ctx)
ADC 23 software reset function.
This file contains SPI specific macros, functions, etc.
ADC 23 Click configuration object.
Definition adc23.h:211
float vref
Definition adc23.h:228
spi_master_chip_select_polarity_t cs_polarity
Definition adc23.h:226
pin_name_t sck
Definition adc23.h:215
spi_master_mode_t spi_mode
Definition adc23.h:225
pin_name_t mosi
Definition adc23.h:214
uint32_t spi_speed
Definition adc23.h:224
pin_name_t rdy
Definition adc23.h:221
pin_name_t miso
Definition adc23.h:213
pin_name_t str
Definition adc23.h:220
pin_name_t rst
Definition adc23.h:219
pin_name_t cs
Definition adc23.h:216
ADC 23 Click context object.
Definition adc23.h:189
spi_master_t spi
Definition adc23.h:198
float vref
Definition adc23.h:202
digital_out_t rst
Definition adc23.h:191
digital_out_t str
Definition adc23.h:192
pin_name_t chip_select
Definition adc23.h:200
digital_in_t rdy
Definition adc23.h:195