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 
35 #include "mikrosdk_version.h"
36 
37 #ifdef __GNUC__
38 #if mikroSDK_GET_VERSION < 20800ul
39 #include "rcu_delays.h"
40 #else
41 #include "delays.h"
42 #endif
43 #endif
44 
45 #include "drv_digital_out.h"
46 #include "drv_digital_in.h"
47 #include "drv_spi_master.h"
48 #include "spi_specifics.h"
49 
70 #define ADC_REG_DEV_ID 0x00
71 #define ADC_REG_REV_ID 0x01
72 #define ADC_REG_STATUS 0x02
73 #define ADC_REG_CONTROL 0x03
74 #define ADC_REG_MUX 0x04
75 #define ADC_REG_CONFIG1 0x05
76 #define ADC_REG_CONFIG2 0x06
77 #define ADC_REG_CONFIG3 0x07
78 #define ADC_REG_CONFIG4 0x08
79 #define ADC_REG_OFFSET2 0x09
80 #define ADC_REG_OFFSET1 0x0A
81 #define ADC_REG_OFFSET0 0x0B
82 #define ADC_REG_GAIN2 0x0C
83 #define ADC_REG_GAIN1 0x0D
84 #define ADC_REG_GAIN0 0x0E
85 #define ADC_REG_CRC 0x0F
86  // adc23_reg
88 
98 #define ADC_CMD_NO_OPERATION 0x00
99 #define ADC_CMD_READ_REG 0x40
100 #define ADC_CMD_WRITE_REG 0x80
101  // adc23_cmd
103 
118 #define ADC23_INT_VREF_2_5V 2.5f
119 
124 #define ADC23_SET_CTRL_STOP 0x01
125 #define ADC23_SET_CTRL_START 0x02
126 #define ADC23_SET_CTRL_SW_RESET 0x58
127 #define ADC23_MAX_REG_ADDR 0x0F
128 
133 #define ADC23_CONV_V_TO_MV 1000.0f
134 
139 #define ADC23_FULL_SCALE_OUT_DATA_RANGE 0x7FFFFFul
140 
149 #define ADC23_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
150 #define ADC23_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
151  // adc23_set
153 
168 #define ADC23_MAP_MIKROBUS( cfg, mikrobus ) \
169  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
170  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
171  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
172  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
173  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
174  cfg.str = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
175  cfg.rdy = MIKROBUS( mikrobus, MIKROBUS_INT )
176  // adc23_map // adc23
179 
184 typedef struct
185 {
186  // Output pins
187  digital_out_t rst;
188  digital_out_t str;
190  // Input pins
191  digital_in_t rdy;
193  // Modules
194  spi_master_t spi;
196  pin_name_t chip_select;
198  float vref;
199 
200 } adc23_t;
201 
206 typedef struct
207 {
208  // Communication gpio pins
209  pin_name_t miso;
210  pin_name_t mosi;
211  pin_name_t sck;
212  pin_name_t cs;
214  // Additional gpio pins
215  pin_name_t rst;
216  pin_name_t str;
217  pin_name_t rdy;
219  // static variable
220  uint32_t spi_speed;
221  spi_master_mode_t spi_mode;
222  spi_master_chip_select_polarity_t cs_polarity;
224  float vref;
225 
226 } adc23_cfg_t;
227 
232 typedef enum
233 {
234  ADC23_OK = 0,
235  ADC23_ERROR = -1
236 
238 
255 
269 err_t adc23_init ( adc23_t *ctx, adc23_cfg_t *cfg );
270 
283 err_t adc23_default_cfg ( adc23_t *ctx );
284 
299 err_t adc23_generic_write ( adc23_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
300 
315 err_t adc23_generic_read ( adc23_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
316 
331 err_t adc23_generic_transfer ( adc23_t *ctx, uint8_t *data_in, uint8_t *data_out, uint8_t len );
332 
349 err_t adc23_read_reg_cmd ( adc23_t *ctx, uint8_t reg, uint16_t *data_out, uint8_t *reg_data_out );
350 
368 err_t adc23_read_reg_data ( adc23_t *ctx, uint8_t reg, uint32_t *data_out, uint8_t *reg_data_out );
369 
385 err_t adc23_write_reg_cmd ( adc23_t *ctx, uint8_t reg, uint8_t data_in, uint16_t *data_out );
386 
402 err_t adc23_write_reg_data ( adc23_t *ctx, uint8_t reg, uint8_t data_in, uint32_t *data_out );
403 
416 err_t adc23_sw_reset ( adc23_t *ctx );
417 
431 
445 
459 err_t adc23_read_conversion_data ( adc23_t *ctx, int32_t *adc_data );
460 
475 err_t adc23_get_voltage ( adc23_t *ctx, float *voltage );
476 
489 void adc23_set_start ( adc23_t *ctx );
490 
503 void adc23_set_stop ( adc23_t *ctx );
504 
517 void adc23_hw_reset ( adc23_t *ctx );
518 
530 uint8_t adc23_get_data_ready ( adc23_t *ctx );
531 
532 #ifdef __cplusplus
533 }
534 #endif
535 #endif // ADC23_H
536  // adc23
538 
539 // ------------------------------------------------------------------------ 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:233
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:217
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:224
adc23_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: adc23.h:221
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:185
adc23_t::rdy
digital_in_t rdy
Definition: adc23.h:191
adc23_cfg_t
ADC 23 Click configuration object.
Definition: adc23.h:207
adc23_cfg_t::cs
pin_name_t cs
Definition: adc23.h:212
adc23_cfg_t::mosi
pin_name_t mosi
Definition: adc23.h:210
adc23_t::chip_select
pin_name_t chip_select
Definition: adc23.h:196
adc23_t::vref
float vref
Definition: adc23.h:198
adc23_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: adc23.h:222
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:216
ADC23_ERROR
@ ADC23_ERROR
Definition: adc23.h:235
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:188
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:220
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:215
adc23_t::spi
spi_master_t spi
Definition: adc23.h:194
ADC23_OK
@ ADC23_OK
Definition: adc23.h:234
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:211
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:187
adc23_cfg_t::miso
pin_name_t miso
Definition: adc23.h:209