adc15  2.0.0.0
adc15.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 ADC15_H
29 #define ADC15_H
30 
31 #ifdef __cplusplus
32 extern "C"{
33 #endif
34 
35 #include "drv_digital_out.h"
36 #include "drv_digital_in.h"
37 #include "drv_spi_master.h"
38 #include "spi_specifics.h"
39 
60 #define ADC15_CMD_NULL 0x0000
61 #define ADC15_CMD_RESET 0x0011
62 #define ADC15_CMD_STANDBY 0x0022
63 #define ADC15_CMD_WAKEUP 0x0033
64 #define ADC15_CMD_LOCK 0x0555
65 #define ADC15_CMD_UNLOCK 0x0655
66 #define ADC15_CMD_RREG 0xA000
67 #define ADC15_CMD_WREG 0x6000
68 
73 #define ADC15_REG_ID 0x00
74 #define ADC15_REG_STATUS 0x01
75 #define ADC15_REG_MODE 0x02
76 #define ADC15_REG_CLOCK 0x03
77 #define ADC15_REG_GAIN 0x04
78 #define ADC15_REG_CFG 0x06
79 #define ADC15_REG_THRSHLD_MSB 0x07
80 #define ADC15_REG_THRSHLD_LSB 0x08
81 #define ADC15_REG_CH0_CFG 0x09
82 #define ADC15_REG_CH0_OCAL_MSB 0x0A
83 #define ADC15_REG_CH0_OCAL_LSB 0x0B
84 #define ADC15_REG_CH0_GCAL_MSB 0x0C
85 #define ADC15_REG_CH0_GCAL_LSB 0x0D
86 #define ADC15_REG_CH1_CFG 0x0E
87 #define ADC15_REG_CH1_OCAL_MSB 0x0F
88 #define ADC15_REG_CH1_OCAL_LSB 0x10
89 #define ADC15_REG_CH1_GCAL_MSB 0x11
90 #define ADC15_REG_CH1_GCAL_LSB 0x12
91 #define ADC15_REG_REGMAP_CRC 0x3E
92  // adc15_reg
94 
110 #define ADC15_FREQ_MODE_HIGH_RESOLUTION 8192000
111 #define ADC15_FREQ_MODE_LOW_POWER 4096000
112 #define ADC15_FREQ_MODE_VERY_LOW_POWER 2048000
113 
118 #define ADC15_LDC_CFG_POWER_ON 2
119 #define ADC15_LDC_CFG_POWER_DOWN 3
120 
129 #define ADC15_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
130 #define ADC15_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
131  // adc15_set
133 
148 #define ADC15_MAP_MIKROBUS( cfg, mikrobus ) \
149  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
150  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
151  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
152  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
153  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
154  cfg.cs2 = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
155  cfg.drdy = MIKROBUS( mikrobus, MIKROBUS_INT )
156  // adc15_map // adc15
159 
164 typedef struct
165 {
166  // Output pins
167  digital_out_t rst;
168  digital_out_t cs2;
170  // Input pins
171  digital_in_t drdy;
173  // Modules
174  spi_master_t spi;
176  pin_name_t chip_select;
178  uint8_t gain1;
179  uint8_t gain2;
180  uint8_t word_len;
181  uint32_t resolution;
183 } adc15_t;
184 
189 typedef struct
190 {
191  // Communication gpio pins
192  pin_name_t miso;
193  pin_name_t mosi;
194  pin_name_t sck;
195  pin_name_t cs;
197  // Additional gpio pins
198  pin_name_t rst;
199  pin_name_t cs2;
200  pin_name_t drdy;
202  // static variable
203  uint32_t spi_speed;
204  spi_master_mode_t spi_mode;
205  spi_master_chip_select_polarity_t cs_polarity;
207 } adc15_cfg_t;
208 
213 typedef enum
214 {
215  ADC15_OK = 0,
217 
219 
224 typedef enum
225 {
228 
230 
235 typedef enum
236 {
246 } adc15_gain_t;
247 
252 typedef enum
253 {
257 
258 } adc15_word_t;
259 
275 void adc15_cfg_setup ( adc15_cfg_t *cfg );
276 
290 err_t adc15_init ( adc15_t *ctx, adc15_cfg_t *cfg );
291 
305 err_t adc15_default_cfg ( adc15_t *ctx );
306 
319 err_t adc15_adc_write ( adc15_t *ctx, uint8_t *data_in, uint8_t len );
320 
333 err_t adc15_reg_write ( adc15_t *ctx, uint8_t reg, uint16_t data_in );
334 
348 err_t adc15_multiple_reg_write ( adc15_t *ctx, uint8_t reg, uint16_t *data_in, uint8_t data_size );
349 
362 err_t adc15_adc_read ( adc15_t *ctx, uint8_t *data_out, uint8_t len );
363 
376 err_t adc15_reg_read ( adc15_t *ctx, uint8_t reg, uint16_t *data_out );
377 
391 err_t adc15_multiple_reg_read ( adc15_t *ctx, uint8_t reg, uint16_t *data_out, uint8_t data_size );
392 
406 err_t adc15_adc_transfer ( adc15_t *ctx, uint8_t *data_in, uint8_t *data_out, uint8_t len );
407 
421 err_t adc15_ltc_write ( adc15_t *ctx, uint8_t oct, uint16_t dac, uint8_t cfg );
422 
434 err_t adc15_set_frequency ( adc15_t *ctx, uint32_t frequency );
435 
449 err_t adc15_read_adc_value ( adc15_t *ctx, uint16_t *status, int32_t *ch1_out, int32_t *ch2_out );
450 
465 err_t adc15_read_voltage ( adc15_t *ctx, uint16_t *status, float *ch1_out, float *ch2_out );
466 
475 uint8_t adc15_data_ready ( adc15_t *ctx );
476 
490 err_t adc15_set_gain ( adc15_t *ctx, adc15_channel_t channel, adc15_gain_t gain );
491 
503 err_t adc15_set_word_len ( adc15_t *ctx, adc15_word_t word_len );
504 
505 #ifdef __cplusplus
506 }
507 #endif
508 #endif // ADC15_H
509  // adc15
511 
512 // ------------------------------------------------------------------------ END
ADC15_CHANNEL2
Definition: adc15.h:227
adc15_read_voltage
err_t adc15_read_voltage(adc15_t *ctx, uint16_t *status, float *ch1_out, float *ch2_out)
Get voltage value.
adc15_t::cs2
digital_out_t cs2
Definition: adc15.h:168
adc15_cfg_t::miso
pin_name_t miso
Definition: adc15.h:192
ADC15_GAIN32
Definition: adc15.h:242
ADC15_GAIN4
Definition: adc15.h:239
ADC15_GAIN2
Definition: adc15.h:238
spi_specifics.h
This file contains SPI specific macros, functions, etc.
adc15_t::drdy
digital_in_t drdy
Definition: adc15.h:171
ADC15_GAIN64
Definition: adc15.h:243
adc15_cfg_t::spi_speed
uint32_t spi_speed
Definition: adc15.h:203
adc15_t::spi
spi_master_t spi
Definition: adc15.h:174
ADC15_ERROR
Definition: adc15.h:216
ADC15_OK
Definition: adc15.h:215
adc15_t::gain1
uint8_t gain1
Definition: adc15.h:178
ADC15_GAIN8
Definition: adc15.h:240
adc15_set_gain
err_t adc15_set_gain(adc15_t *ctx, adc15_channel_t channel, adc15_gain_t gain)
Set gain for channel.
adc15_ltc_write
err_t adc15_ltc_write(adc15_t *ctx, uint8_t oct, uint16_t dac, uint8_t cfg)
LTC writing function.
adc15_adc_transfer
err_t adc15_adc_transfer(adc15_t *ctx, uint8_t *data_in, uint8_t *data_out, uint8_t len)
Data transfer function.
adc15_multiple_reg_write
err_t adc15_multiple_reg_write(adc15_t *ctx, uint8_t reg, uint16_t *data_in, uint8_t data_size)
Multiple register writing function.
adc15_default_cfg
err_t adc15_default_cfg(adc15_t *ctx)
ADC 15 default configuration function.
ADC15_16bit
Definition: adc15.h:254
ADC15_CHANNEL1
Definition: adc15.h:226
ADC15_24bit
Definition: adc15.h:255
adc15_read_adc_value
err_t adc15_read_adc_value(adc15_t *ctx, uint16_t *status, int32_t *ch1_out, int32_t *ch2_out)
Get raw adc value.
adc15_cfg_t::cs
pin_name_t cs
Definition: adc15.h:195
adc15_adc_write
err_t adc15_adc_write(adc15_t *ctx, uint8_t *data_in, uint8_t len)
Data writing function.
adc15_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: adc15.h:205
adc15_cfg_t::mosi
pin_name_t mosi
Definition: adc15.h:193
adc15_t::word_len
uint8_t word_len
Definition: adc15.h:180
ADC15_GAIN16
Definition: adc15.h:241
adc15_set_word_len
err_t adc15_set_word_len(adc15_t *ctx, adc15_word_t word_len)
Set word len.
ADC15_GAIN1
Definition: adc15.h:237
adc15_t::resolution
uint32_t resolution
Definition: adc15.h:181
adc15_cfg_t
ADC 15 Click configuration object.
Definition: adc15.h:189
adc15_multiple_reg_read
err_t adc15_multiple_reg_read(adc15_t *ctx, uint8_t reg, uint16_t *data_out, uint8_t data_size)
Multiple registern reading function.
adc15_init
err_t adc15_init(adc15_t *ctx, adc15_cfg_t *cfg)
ADC 15 initialization function.
adc15_cfg_t::cs2
pin_name_t cs2
Definition: adc15.h:199
ADC15_GAIN128
Definition: adc15.h:244
adc15_data_ready
uint8_t adc15_data_ready(adc15_t *ctx)
Get data ready state.
adc15_channel_t
adc15_channel_t
ADC 15 Click channel selection.
Definition: adc15.h:224
adc15_word_t
adc15_word_t
ADC 15 Click communication word data length.
Definition: adc15.h:252
adc15_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: adc15.h:204
adc15_reg_write
err_t adc15_reg_write(adc15_t *ctx, uint8_t reg, uint16_t data_in)
Register writing function.
adc15_reg_read
err_t adc15_reg_read(adc15_t *ctx, uint8_t reg, uint16_t *data_out)
Register reading function.
adc15_t::rst
digital_out_t rst
Definition: adc15.h:167
adc15_adc_read
err_t adc15_adc_read(adc15_t *ctx, uint8_t *data_out, uint8_t len)
Data reading function.
adc15_cfg_t::rst
pin_name_t rst
Definition: adc15.h:198
adc15_cfg_t::drdy
pin_name_t drdy
Definition: adc15.h:200
adc15_cfg_t::sck
pin_name_t sck
Definition: adc15.h:194
ADC15_32bit
Definition: adc15.h:256
adc15_gain_t
adc15_gain_t
ADC 15 Click ADC gain configuration.
Definition: adc15.h:235
adc15_return_value_t
adc15_return_value_t
ADC 15 Click return value data.
Definition: adc15.h:213
adc15_t
ADC 15 Click context object.
Definition: adc15.h:164
adc15_t::chip_select
pin_name_t chip_select
Definition: adc15.h:176
adc15_cfg_setup
void adc15_cfg_setup(adc15_cfg_t *cfg)
ADC 15 configuration object setup function.
adc15_t::gain2
uint8_t gain2
Definition: adc15.h:179
adc15_set_frequency
err_t adc15_set_frequency(adc15_t *ctx, uint32_t frequency)
Set sampling frequency.