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
32 extern "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  // 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  // 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  // 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  // adc23_map // adc23
183 
188 typedef 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 
210 typedef 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 
230 } adc23_cfg_t;
231 
236 typedef enum
237 {
238  ADC23_OK = 0,
239  ADC23_ERROR = -1
240 
242 
259 
273 err_t adc23_init ( adc23_t *ctx, adc23_cfg_t *cfg );
274 
287 err_t adc23_default_cfg ( adc23_t *ctx );
288 
303 err_t adc23_generic_write ( adc23_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
304 
319 err_t adc23_generic_read ( adc23_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
320 
335 err_t adc23_generic_transfer ( adc23_t *ctx, uint8_t *data_in, uint8_t *data_out, uint8_t len );
336 
353 err_t adc23_read_reg_cmd ( adc23_t *ctx, uint8_t reg, uint16_t *data_out, uint8_t *reg_data_out );
354 
372 err_t adc23_read_reg_data ( adc23_t *ctx, uint8_t reg, uint32_t *data_out, uint8_t *reg_data_out );
373 
389 err_t adc23_write_reg_cmd ( adc23_t *ctx, uint8_t reg, uint8_t data_in, uint16_t *data_out );
390 
406 err_t adc23_write_reg_data ( adc23_t *ctx, uint8_t reg, uint8_t data_in, uint32_t *data_out );
407 
420 err_t adc23_sw_reset ( adc23_t *ctx );
421 
435 
449 
463 err_t adc23_read_conversion_data ( adc23_t *ctx, int32_t *adc_data );
464 
479 err_t adc23_get_voltage ( adc23_t *ctx, float *voltage );
480 
493 void adc23_set_start ( adc23_t *ctx );
494 
507 void adc23_set_stop ( adc23_t *ctx );
508 
521 void adc23_hw_reset ( adc23_t *ctx );
522 
534 uint8_t adc23_get_data_ready ( adc23_t *ctx );
535 
536 #ifdef __cplusplus
537 }
538 #endif
539 #endif // ADC23_H
540  // adc23
542 
543 // ------------------------------------------------------------------------ END
adc23_init
err_t adc23_init(adc23_t *ctx, adc23_cfg_t *cfg)
ADC 23 initialization function.
adc23_read_conversion_data
err_t adc23_read_conversion_data(adc23_t *ctx, int32_t *adc_data)
ADC 23 read conversion data function.
adc23_start_conversion
err_t adc23_start_conversion(adc23_t *ctx)
ADC 23 start conversion function.
adc23_return_value_t
adc23_return_value_t
ADC 23 Click return value data.
Definition: adc23.h:237
spi_specifics.h
This file contains SPI specific macros, functions, etc.
adc23_generic_write
err_t adc23_generic_write(adc23_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
ADC 23 data writing function.
adc23_read_reg_cmd
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.
adc23_get_data_ready
uint8_t adc23_get_data_ready(adc23_t *ctx)
ADC 23 get data ready function.
adc23_hw_reset
void adc23_hw_reset(adc23_t *ctx)
ADC 23 hardware reset function.
adc23_cfg_t::rdy
pin_name_t rdy
Definition: adc23.h:221
adc23_cfg_setup
void adc23_cfg_setup(adc23_cfg_t *cfg)
ADC 23 configuration object setup function.
adc23_cfg_t::vref
float vref
Definition: adc23.h:228
adc23_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: adc23.h:225
adc23_default_cfg
err_t adc23_default_cfg(adc23_t *ctx)
ADC 23 default configuration function.
adc23_stop_conversion
err_t adc23_stop_conversion(adc23_t *ctx)
ADC 23 stop conversion function.
adc23_t
ADC 23 Click context object.
Definition: adc23.h:189
adc23_t::rdy
digital_in_t rdy
Definition: adc23.h:195
adc23_cfg_t
ADC 23 Click configuration object.
Definition: adc23.h:211
adc23_cfg_t::cs
pin_name_t cs
Definition: adc23.h:216
adc23_cfg_t::mosi
pin_name_t mosi
Definition: adc23.h:214
adc23_t::chip_select
pin_name_t chip_select
Definition: adc23.h:200
adc23_t::vref
float vref
Definition: adc23.h:202
adc23_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: adc23.h:226
adc23_set_start
void adc23_set_start(adc23_t *ctx)
ADC 23 set start function.
adc23_cfg_t::str
pin_name_t str
Definition: adc23.h:220
ADC23_ERROR
@ ADC23_ERROR
Definition: adc23.h:239
adc23_sw_reset
err_t adc23_sw_reset(adc23_t *ctx)
ADC 23 software reset function.
adc23_generic_read
err_t adc23_generic_read(adc23_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
ADC 23 data reading function.
adc23_write_reg_cmd
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.
adc23_t::str
digital_out_t str
Definition: adc23.h:192
adc23_generic_transfer
err_t adc23_generic_transfer(adc23_t *ctx, uint8_t *data_in, uint8_t *data_out, uint8_t len)
ADC 23 data transfer function.
adc23_cfg_t::spi_speed
uint32_t spi_speed
Definition: adc23.h:224
adc23_write_reg_data
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.
adc23_cfg_t::rst
pin_name_t rst
Definition: adc23.h:219
adc23_t::spi
spi_master_t spi
Definition: adc23.h:198
ADC23_OK
@ ADC23_OK
Definition: adc23.h:238
adc23_read_reg_data
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.
adc23_cfg_t::sck
pin_name_t sck
Definition: adc23.h:215
adc23_get_voltage
err_t adc23_get_voltage(adc23_t *ctx, float *voltage)
ADC 23 read get voltage level function.
adc23_set_stop
void adc23_set_stop(adc23_t *ctx)
ADC 23 set stop function.
adc23_t::rst
digital_out_t rst
Definition: adc23.h:191
adc23_cfg_t::miso
pin_name_t miso
Definition: adc23.h:213