adc10 2.0.0.0
adc10.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 ADC10_H
29#define ADC10_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_uart.h"
52
53
74#define ADC10_REG_CONFIG_0 0x00
75#define ADC10_REG_CONFIG_1 0x01
76#define ADC10_REG_CONFIG_2 0x02
77#define ADC10_REG_CONFIG_3 0x03
78#define ADC10_REG_CONFIG_4 0x04
79
80 // adc10_reg
81
97#define ADC10_SYNC_WORD 0x55
98#define ADC10_CMD_RESET 0x06
99#define ADC10_CMD_START 0x08
100#define ADC10_CMD_POWERDOWN 0x03
101#define ADC10_CMD_RDATA 0x10
102#define ADC10_CMD_RREG 0x20
103#define ADC10_CMD_WREG 0x40
104
110#define ADC10_REG0_MUX_P0_N1 0x00
111#define ADC10_REG0_MUX_P0_N2 0x01
112#define ADC10_REG0_MUX_P0_N3 0x02
113#define ADC10_REG0_MUX_P1_N0 0x03
114#define ADC10_REG0_MUX_P1_N2 0x04
115#define ADC10_REG0_MUX_P1_N3 0x05
116#define ADC10_REG0_MUX_P2_N3 0x06
117#define ADC10_REG0_MUX_P3_N2 0x07
118#define ADC10_REG0_MUX_P0_NS 0x08
119#define ADC10_REG0_MUX_P1_NS 0x09
120#define ADC10_REG0_MUX_P2_NS 0x0A
121#define ADC10_REG0_MUX_P3_NS 0x0B
122#define ADC10_REG0_MUX_VREF 0x0C
123#define ADC10_REG0_MUX_AVOLD_4 0x0D
124#define ADC10_REG0_MUX_AVOLAVER 0x0E
125#define ADC10_REG0_MUX_RESERVE 0x0F
126
132#define ADC10_REG0_GAIN_1 0x00
133#define ADC10_REG0_GAIN_2 0x01
134#define ADC10_REG0_GAIN_4 0x02
135#define ADC10_REG0_GAIN_8 0x03
136#define ADC10_REG0_GAIN_16 0x04
137#define ADC10_REG0_GAIN_32 0x05
138#define ADC10_REG0_GAIN_64 0x06
139#define ADC10_REG0_GAIN_128 0x07
140
146#define ADC10_REG0_PGA_ENABLED 0x00
147#define ADC10_REG0_PGA_DISABLED 0x01
148
154#define ADC10_REG1_DR_NORMAL_20 0x00
155#define ADC10_REG1_DR_NORMAL_45 0x01
156#define ADC10_REG1_DR_NORMAL_90 0x02
157#define ADC10_REG1_DR_NORMAL_175 0x03
158#define ADC10_REG1_DR_NORMAL_330 0x04
159#define ADC10_REG1_DR_NORMAL_600 0x05
160#define ADC10_REG1_DR_NORMAL_1000 0x06
161#define ADC10_REG1_DR_TURBO_40 0x00
162#define ADC10_REG1_DR_TURBO_90 0x01
163#define ADC10_REG1_DR_TURBO_180 0x02
164#define ADC10_REG1_DR_TURBO_350 0x03
165#define ADC10_REG1_DR_TURBO_660 0x04
166#define ADC10_REG1_DR_TURBO_1200 0x05
167#define ADC10_REG1_DR_TURBO_2000 0x06
168
174#define ADC10_REG1_MODE_NORMAL 0x00
175#define ADC10_REG1_MODE_TURBO 0x01
176
182#define ADC10_REG1_CM_SINGAL_SHOT 0x00
183#define ADC10_REG1_CM_CONTINUOUS 0x01
184
190#define ADC10_REG1_VREF_INTERNAL_2048 0x00
191#define ADC10_REG1_VREF_EXTERNAL_REF 0x01
192#define ADC10_REG1_VREF_ANALOG_1 0x02
193#define ADC10_REG1_VREF_ANALOG_2 0x03
194
200#define ADC10_REG1_TS_DISABLED 0x00
201#define ADC10_REG1_TS_ENABLED 0x01
202
208#define ADC10_REG2_DRDY_NOT_READY 0x00
209#define ADC10_REG2_DRDY_READY 0x01
210
216#define ADC10_REG2_DCNT_DISABLED 0x00
217#define ADC10_REG2_DCNT_ENABLED 0x01
218
224#define ADC10_REG2_CRC_DISABLED 0x00
225#define ADC10_REG2_CRC_INVERTED_ENABLED 0x01
226#define ADC10_REG2_CRC_CRC16_ENABLED 0x02
227#define ADC10_REG2_CRC_RESERVED 0x03
228
234#define ADC10_REG2_BCS_CURRENT_SOURCE_OFF 0x00
235#define ADC10_REG2_BCS_CURRENT_SOURCE_ON 0x01
236
242#define ADC10_REG2_IDAC_CURRENT_OFF 0x00
243#define ADC10_REG2_IDAC_CURRENT_10_uA 0x01
244#define ADC10_REG2_IDAC_CURRENT_50_uA 0x02
245#define ADC10_REG2_IDAC_CURRENT_100_uA 0x03
246#define ADC10_REG2_IDAC_CURRENT_250_uA 0x04
247#define ADC10_REG2_IDAC_CURRENT_500_uA 0x05
248#define ADC10_REG2_IDAC_CURRENT_1000_uA 0x06
249#define ADC10_REG2_IDAC_CURRENT_1500_uA 0x07
250
256#define ADC10_REG3_L1MUX_DISABLED 0x00
257#define ADC10_REG3_L1MUX_TO_AIN0 0x01
258#define ADC10_REG3_L1MUX_TO_AIN1 0x02
259#define ADC10_REG3_L1MUX_TO_AIN2 0x03
260#define ADC10_REG3_L1MUX_TO_AIN3 0x04
261#define ADC10_REG3_L1MUX_TO_REFP 0x05
262#define ADC10_REG3_L1MUX_TO_REFN 0x06
263#define ADC10_REG3_L1MUX_RESERVED 0x07
264
270#define ADC10_REG3_L2MUX_DISABLED 0x00
271#define ADC10_REG3_L2MUX_TO_AIN0 0x01
272#define ADC10_REG3_L2MUX_TO_AIN1 0x02
273#define ADC10_REG3_L2MUX_TO_AIN2 0x03
274#define ADC10_REG3_L2MUX_TO_AIN3 0x04
275#define ADC10_REG3_L2MUX_TO_REFP 0x05
276#define ADC10_REG3_L2MUX_TO_REFN 0x06
277
283#define ADC10_REG3_DATA_MODE_MANUAL 0x00
284#define ADC10_REG3_DATA_MODE_AUTO 0x01
285
291#define ADC10_REG4_GPIO2_DIR_INPUT 0x00
292#define ADC10_REG4_GPIO2_DIR_OUTPUT 0x01
293#define ADC10_REG4_GPIO1_DIR_INPUT 0x00
294#define ADC10_REG4_GPIO1_DIR_OUTPUT 0x01
295#define ADC10_REG4_GPIO0_DIR_INPUT 0x00
296#define ADC10_REG4_GPIO0_DIR_OUTPUT 0x01
297
303#define ADC10_REG4_GPIO2_SEL_DAT 0x00
304#define ADC10_REG4_GPIO2_SEL_DRDY 0x01
305
311#define ADC10_REG4_GPIO_LOW 0x00
312#define ADC10_REG4_GPIO_HIGH 0x01
313
318#define ADC10_ANALOG_INPUT_CH_0 0x00
319#define ADC10_ANALOG_INPUT_CH_1 0x01
320#define ADC10_ANALOG_INPUT_CH_2 0x02
321#define ADC10_ANALOG_INPUT_CH_3 0x03
322
327#define ADC10_NEW_DATA_IS_READY 0x00
328#define ADC10_NEW_DATA_NOT_READY 0x01
329
334#define ADC10_GAIN_1 1
335#define ADC10_GAIN_2 2
336#define ADC10_GAIN_4 4
337#define ADC10_GAIN_8 8
338#define ADC10_GAIN_16 16
339#define ADC10_GAIN_32 32
340#define ADC10_GAIN_64 64
341#define ADC10_GAIN_128 128
342
347#define ADC10_REG_BITS 0x0E
348#define ADC10_ADC_RESOLUTION 0x007FFFFF
349#define ADC10_VREF_INTERNAL 2048.0
350#define ADC10_VREF_EXTERNAL_3300_mA 3300.0
351#define ADC10_VREF_EXTERNAL_5000_mA 5000.0
352
353
359#define DRV_BUFFER_SIZE 200
360
361 // adc10_set
362
377#define ADC10_MAP_MIKROBUS( cfg, mikrobus ) \
378 cfg.tx_pin = MIKROBUS( mikrobus, MIKROBUS_TX ); \
379 cfg.rx_pin = MIKROBUS( mikrobus, MIKROBUS_RX ); \
380 cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
381 cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
382
383 // adc10_map
384 // adc10
385
390typedef struct
391{
392 // Output pins
393
394 digital_out_t rst;
396 // Input pins
397
398 digital_in_t int_pin;
400 // Modules
401
402 uart_t uart;
404 // Buffers
405
406 char uart_rx_buffer[ DRV_BUFFER_SIZE ];
407 char uart_tx_buffer[ DRV_BUFFER_SIZE ];
409} adc10_t;
410
415typedef struct
416{
417 // Communication gpio pins
418
419 pin_name_t rx_pin;
420 pin_name_t tx_pin;
422 // Additional gpio pins
423
424 pin_name_t rst;
425 pin_name_t int_pin;
427 // Static variable
428
429 uint32_t baud_rate;
431 uart_data_bits_t data_bit;
432 uart_parity_t parity_bit;
433 uart_stop_bits_t stop_bit;
436
437typedef struct
438{
439 uint8_t input_mux;
440 uint8_t gain;
441 uint8_t pga_bypass;
442 uint8_t data_rate;
443 uint8_t op_mode;
444 uint8_t conv_mode;
445 uint8_t vref;
446 uint8_t temp_sensor;
447 uint8_t conv_drdy;
448 uint8_t data_cnt;
455 uint8_t gpio2_ctrl;
456 uint8_t gpio1_ctrl;
457 uint8_t gpio0_ctrl;
462}
464
481
496err_t adc10_init ( adc10_t *ctx, adc10_cfg_t *cfg );
497
510
524err_t adc10_generic_write ( adc10_t *ctx, char *data_buf, uint16_t len );
525
539err_t adc10_generic_read ( adc10_t *ctx, char *data_buf, uint16_t max_len );
540
553
567uint8_t adc10_check_drdy ( adc10_t *ctx );
568
581void adc10_send_command ( adc10_t *ctx, uint8_t cmd );
582
594void adc10_reset ( adc10_t *ctx );
595
608
621
636void adc10_write_reg ( adc10_t *ctx, uint8_t reg, uint8_t tx_data );
637
651uint8_t adc10_read_reg ( adc10_t *ctx, uint8_t reg );
652
668int32_t adc10_get_ch_output ( adc10_t *ctx, uint8_t sel_ch );
669
684float adc10_calc_voltage ( adc10_t *ctx, int32_t adc_data, float vref, uint8_t gain );
685
699
713
714
715
716#ifdef __cplusplus
717}
718#endif
719#endif // ADC10_H
720
721 // adc10
722
723// ------------------------------------------------------------------------ END
#define DRV_BUFFER_SIZE
ADC 10 driver buffer size.
Definition adc10.h:359
uint8_t adc10_read_reg(adc10_t *ctx, uint8_t reg)
ADC 10 Read register function.
int32_t adc10_get_ch_output(adc10_t *ctx, uint8_t sel_ch)
ADC 10 Get the channel output function.
void adc10_write_reg(adc10_t *ctx, uint8_t reg, uint8_t tx_data)
ADC 10 Write register function.
float adc10_calc_voltage(adc10_t *ctx, int32_t adc_data, float vref, uint8_t gain)
ADC 10 Calculate the voltage function.
void adc10_get_config(adc10_t *ctx, adc10_settings_t *cfg_data)
ADC 10 Get configuration function.
void adc10_start_sync(adc10_t *ctx)
ADC 10 Start the synchronization function.
void adc10_set_config(adc10_t *ctx, adc10_settings_t cfg_data)
ADC 10 Set configuration function.
void adc10_send_command(adc10_t *ctx, uint8_t cmd)
ADC 10 Send command function.
err_t adc10_generic_read(adc10_t *ctx, char *data_buf, uint16_t max_len)
ADC 10 data reading function.
void adc10_reset(adc10_t *ctx)
ADC 10 Software reset function.
err_t adc10_init(adc10_t *ctx, adc10_cfg_t *cfg)
ADC 10 initialization function.
void adc10_cfg_setup(adc10_cfg_t *cfg)
ADC 10 configuration object setup function.
err_t adc10_generic_write(adc10_t *ctx, char *data_buf, uint16_t len)
ADC 10 data writing function.
void adc10_shutdown(adc10_t *ctx)
ADC 10 Shutdown function.
void adc10_hw_reset(adc10_t *ctx)
ADC 10 Hardware reset function.
uint8_t adc10_check_drdy(adc10_t *ctx)
ADC 10 Check DRDY function.
void adc10_default_cfg(adc10_t *ctx)
ADC 10 default configuration function.
ADC 10 Click configuration object.
Definition adc10.h:416
uint32_t baud_rate
Definition adc10.h:429
bool uart_blocking
Definition adc10.h:430
uart_data_bits_t data_bit
Definition adc10.h:431
pin_name_t tx_pin
Definition adc10.h:420
pin_name_t rx_pin
Definition adc10.h:419
uart_stop_bits_t stop_bit
Definition adc10.h:433
pin_name_t int_pin
Definition adc10.h:425
uart_parity_t parity_bit
Definition adc10.h:432
pin_name_t rst
Definition adc10.h:424
Definition adc10.h:438
uint8_t gpio2_io_lvl
Definition adc10.h:459
uint8_t idac2_routing
Definition adc10.h:453
uint8_t conv_drdy
Definition adc10.h:447
uint8_t b_current_src
Definition adc10.h:450
uint8_t gpio1_io_lvl
Definition adc10.h:460
uint8_t input_mux
Definition adc10.h:439
uint8_t pga_bypass
Definition adc10.h:441
uint8_t idac_current
Definition adc10.h:451
uint8_t gain
Definition adc10.h:440
uint8_t gpio1_ctrl
Definition adc10.h:456
uint8_t gpio2_drdy_ctrl
Definition adc10.h:458
uint8_t adc_data_out_mode
Definition adc10.h:454
uint8_t data_rate
Definition adc10.h:442
uint8_t gpio0_ctrl
Definition adc10.h:457
uint8_t data_integ_check
Definition adc10.h:449
uint8_t vref
Definition adc10.h:445
uint8_t idac1_routing
Definition adc10.h:452
uint8_t data_cnt
Definition adc10.h:448
uint8_t temp_sensor
Definition adc10.h:446
uint8_t gpio2_ctrl
Definition adc10.h:455
uint8_t gpio0_io_lvl
Definition adc10.h:461
uint8_t op_mode
Definition adc10.h:443
uint8_t conv_mode
Definition adc10.h:444
ADC 10 Click context object.
Definition adc10.h:391
uart_t uart
Definition adc10.h:402
digital_in_t int_pin
Definition adc10.h:398
digital_out_t rst
Definition adc10.h:394