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 
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 ADC15_CMD_NULL 0x0000
75 #define ADC15_CMD_RESET 0x0011
76 #define ADC15_CMD_STANDBY 0x0022
77 #define ADC15_CMD_WAKEUP 0x0033
78 #define ADC15_CMD_LOCK 0x0555
79 #define ADC15_CMD_UNLOCK 0x0655
80 #define ADC15_CMD_RREG 0xA000
81 #define ADC15_CMD_WREG 0x6000
82 
87 #define ADC15_REG_ID 0x00
88 #define ADC15_REG_STATUS 0x01
89 #define ADC15_REG_MODE 0x02
90 #define ADC15_REG_CLOCK 0x03
91 #define ADC15_REG_GAIN 0x04
92 #define ADC15_REG_CFG 0x06
93 #define ADC15_REG_THRSHLD_MSB 0x07
94 #define ADC15_REG_THRSHLD_LSB 0x08
95 #define ADC15_REG_CH0_CFG 0x09
96 #define ADC15_REG_CH0_OCAL_MSB 0x0A
97 #define ADC15_REG_CH0_OCAL_LSB 0x0B
98 #define ADC15_REG_CH0_GCAL_MSB 0x0C
99 #define ADC15_REG_CH0_GCAL_LSB 0x0D
100 #define ADC15_REG_CH1_CFG 0x0E
101 #define ADC15_REG_CH1_OCAL_MSB 0x0F
102 #define ADC15_REG_CH1_OCAL_LSB 0x10
103 #define ADC15_REG_CH1_GCAL_MSB 0x11
104 #define ADC15_REG_CH1_GCAL_LSB 0x12
105 #define ADC15_REG_REGMAP_CRC 0x3E
106  // adc15_reg
108 
124 #define ADC15_FREQ_MODE_HIGH_RESOLUTION 8192000
125 #define ADC15_FREQ_MODE_LOW_POWER 4096000
126 #define ADC15_FREQ_MODE_VERY_LOW_POWER 2048000
127 
132 #define ADC15_LDC_CFG_POWER_ON 2
133 #define ADC15_LDC_CFG_POWER_DOWN 3
134 
143 #define ADC15_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
144 #define ADC15_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
145  // adc15_set
147 
162 #define ADC15_MAP_MIKROBUS( cfg, mikrobus ) \
163  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
164  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
165  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
166  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
167  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
168  cfg.cs2 = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
169  cfg.drdy = MIKROBUS( mikrobus, MIKROBUS_INT )
170  // adc15_map // adc15
173 
178 typedef struct
179 {
180  // Output pins
181  digital_out_t rst;
182  digital_out_t cs2;
184  // Input pins
185  digital_in_t drdy;
187  // Modules
188  spi_master_t spi;
190  pin_name_t chip_select;
192  uint8_t gain1;
193  uint8_t gain2;
194  uint8_t word_len;
195  uint32_t resolution;
197 } adc15_t;
198 
203 typedef struct
204 {
205  // Communication gpio pins
206  pin_name_t miso;
207  pin_name_t mosi;
208  pin_name_t sck;
209  pin_name_t cs;
211  // Additional gpio pins
212  pin_name_t rst;
213  pin_name_t cs2;
214  pin_name_t drdy;
216  // static variable
217  uint32_t spi_speed;
218  spi_master_mode_t spi_mode;
219  spi_master_chip_select_polarity_t cs_polarity;
221 } adc15_cfg_t;
222 
227 typedef enum
228 {
229  ADC15_OK = 0,
230  ADC15_ERROR = -1
231 
233 
238 typedef enum
239 {
241  ADC15_CHANNEL2 = 4
242 
244 
249 typedef enum
250 {
261 
266 typedef enum
267 {
271 
273 
290 
304 err_t adc15_init ( adc15_t *ctx, adc15_cfg_t *cfg );
305 
319 err_t adc15_default_cfg ( adc15_t *ctx );
320 
333 err_t adc15_adc_write ( adc15_t *ctx, uint8_t *data_in, uint8_t len );
334 
347 err_t adc15_reg_write ( adc15_t *ctx, uint8_t reg, uint16_t data_in );
348 
362 err_t adc15_multiple_reg_write ( adc15_t *ctx, uint8_t reg, uint16_t *data_in, uint8_t data_size );
363 
376 err_t adc15_adc_read ( adc15_t *ctx, uint8_t *data_out, uint8_t len );
377 
390 err_t adc15_reg_read ( adc15_t *ctx, uint8_t reg, uint16_t *data_out );
391 
405 err_t adc15_multiple_reg_read ( adc15_t *ctx, uint8_t reg, uint16_t *data_out, uint8_t data_size );
406 
420 err_t adc15_adc_transfer ( adc15_t *ctx, uint8_t *data_in, uint8_t *data_out, uint8_t len );
421 
435 err_t adc15_ltc_write ( adc15_t *ctx, uint8_t oct, uint16_t dac, uint8_t cfg );
436 
448 err_t adc15_set_frequency ( adc15_t *ctx, uint32_t frequency );
449 
463 err_t adc15_read_adc_value ( adc15_t *ctx, uint16_t *status, int32_t *ch1_out, int32_t *ch2_out );
464 
479 err_t adc15_read_voltage ( adc15_t *ctx, uint16_t *status, float *ch1_out, float *ch2_out );
480 
489 uint8_t adc15_data_ready ( adc15_t *ctx );
490 
504 err_t adc15_set_gain ( adc15_t *ctx, adc15_channel_t channel, adc15_gain_t gain );
505 
517 err_t adc15_set_word_len ( adc15_t *ctx, adc15_word_t word_len );
518 
519 #ifdef __cplusplus
520 }
521 #endif
522 #endif // ADC15_H
523  // adc15
525 
526 // ------------------------------------------------------------------------ END
ADC15_CHANNEL2
@ ADC15_CHANNEL2
Definition: adc15.h:241
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:182
adc15_cfg_t::miso
pin_name_t miso
Definition: adc15.h:206
ADC15_GAIN32
@ ADC15_GAIN32
Definition: adc15.h:256
ADC15_GAIN4
@ ADC15_GAIN4
Definition: adc15.h:253
ADC15_GAIN2
@ ADC15_GAIN2
Definition: adc15.h:252
spi_specifics.h
This file contains SPI specific macros, functions, etc.
adc15_t::drdy
digital_in_t drdy
Definition: adc15.h:185
ADC15_GAIN64
@ ADC15_GAIN64
Definition: adc15.h:257
adc15_cfg_t::spi_speed
uint32_t spi_speed
Definition: adc15.h:217
adc15_t::spi
spi_master_t spi
Definition: adc15.h:188
ADC15_ERROR
@ ADC15_ERROR
Definition: adc15.h:230
ADC15_OK
@ ADC15_OK
Definition: adc15.h:229
adc15_t::gain1
uint8_t gain1
Definition: adc15.h:192
ADC15_GAIN8
@ ADC15_GAIN8
Definition: adc15.h:254
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
@ ADC15_16bit
Definition: adc15.h:268
ADC15_CHANNEL1
@ ADC15_CHANNEL1
Definition: adc15.h:240
ADC15_24bit
@ ADC15_24bit
Definition: adc15.h:269
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:209
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:219
adc15_cfg_t::mosi
pin_name_t mosi
Definition: adc15.h:207
adc15_t::word_len
uint8_t word_len
Definition: adc15.h:194
ADC15_GAIN16
@ ADC15_GAIN16
Definition: adc15.h:255
adc15_set_word_len
err_t adc15_set_word_len(adc15_t *ctx, adc15_word_t word_len)
Set word len.
ADC15_GAIN1
@ ADC15_GAIN1
Definition: adc15.h:251
adc15_t::resolution
uint32_t resolution
Definition: adc15.h:195
adc15_cfg_t
ADC 15 Click configuration object.
Definition: adc15.h:204
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:213
ADC15_GAIN128
@ ADC15_GAIN128
Definition: adc15.h:258
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:239
adc15_word_t
adc15_word_t
ADC 15 Click communication word data length.
Definition: adc15.h:267
adc15_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: adc15.h:218
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:181
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:212
adc15_cfg_t::drdy
pin_name_t drdy
Definition: adc15.h:214
adc15_cfg_t::sck
pin_name_t sck
Definition: adc15.h:208
ADC15_32bit
@ ADC15_32bit
Definition: adc15.h:270
adc15_gain_t
adc15_gain_t
ADC 15 Click ADC gain configuration.
Definition: adc15.h:250
adc15_return_value_t
adc15_return_value_t
ADC 15 Click return value data.
Definition: adc15.h:228
adc15_t
ADC 15 Click context object.
Definition: adc15.h:179
adc15_t::chip_select
pin_name_t chip_select
Definition: adc15.h:190
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:193
adc15_set_frequency
err_t adc15_set_frequency(adc15_t *ctx, uint32_t frequency)
Set sampling frequency.