adc22  2.1.0.0
adc22.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 ADC22_H
29 #define ADC22_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 ADC22_CMD_NOP 0x00
75 #define ADC22_CMD_CLEAR_HWORD 0xC0
76 #define ADC22_CMD_READ_HWORD 0xC8
77 #define ADC22_CMD_READ 0x48
78 #define ADC22_CMD_WRITE 0xD0
79 #define ADC22_CMD_WRITE_MS 0xD2
80 #define ADC22_CMD_WRITE_LS 0xD4
81 #define ADC22_CMD_SET_HWORD 0xD8
82  // adc22_cmd
84 
99 #define ADC22_REG_DEVICE_ID 0x02
100 #define ADC22_REG_RST_PWRCTL_LSB 0x04
101 #define ADC22_REG_RST_PWRCTL_MSB 0x05
102 #define ADC22_REG_SDI_CTL 0x08
103 #define ADC22_REG_SDO_CTL_LSB 0x0C
104 #define ADC22_REG_SDO_CTL_MSB 0x0D
105 #define ADC22_REG_DATAOUT_CTL_LSB 0x10
106 #define ADC22_REG_DATAOUT_CTL_MSB 0x11
107 #define ADC22_REG_RANGE_SEL 0x14
108 #define ADC22_REG_ALARM_LSB 0x20
109 #define ADC22_REG_ALARM_MSB 0x21
110 #define ADC22_REG_ALARM_H_TH_LSB 0x24
111 #define ADC22_REG_ALARM_H_TH_MSB 0x25
112 #define ADC22_REG_ALARM_L_TH_LSB 0x28
113 #define ADC22_REG_ALARM_L_TH_MSB 0x29
114  // adc22_reg
116 
131 #define ADC22_DEVICE_ADDR_MAX 16
132 
137 #define ADC22_SDO_MODE_SAME_SDI 1
138 #define ADC22_SDO_MODE_INV 2
139 #define ADC22_SDO_MODE_ADC_M_CLK 3
140 
145 #define ADC22_DATAOUT_CTL_VAL_CONV 0x00
146 #define ADC22_DATAOUT_CTL_VAL_0_S 0x04
147 #define ADC22_DATAOUT_CTL_VAL_1_S 0x05
148 #define ADC22_DATAOUT_CTL_VAL_A_01_S 0x06
149 #define ADC22_DATAOUT_CTL_VAL_A_0011_S 0x07
150 #define ADC22_DATAOUT_CTL_PAR_DIS 0x00
151 #define ADC22_DATAOUT_CTL_PAR_EN 0x08
152 
157 #define ADC22_ADC_ZERO_SCALE 2045u
158 #define ADC22_ADC_MIDDLE_SCALE 2048.0f
159 #define ADC22_ADC_FULL_SCALE 4096.0f
160 
165 #define ADC22_ADC_RANGE_12V28 12.28f
166 #define ADC22_ADC_RANGE_10V24 10.24f
167 #define ADC22_ADC_RANGE_6V14 6.14f
168 #define ADC22_ADC_RANGE_5V12 5.12f
169 #define ADC22_ADC_RANGE_2V56 2.56f
170 #define ADC22_SET_RANGE_BIT_MASK 0x0F
171 
176 #define ADC22_PIN_STATE_LOW 0x00
177 #define ADC22_PIN_STATE_HIGH 0x01
178 
187 #define ADC22_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
188 #define ADC22_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
189  // adc22_set
191 
206 #define ADC22_MAP_MIKROBUS( cfg, mikrobus ) \
207  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
208  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
209  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
210  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
211  cfg.rvs = MIKROBUS( mikrobus, MIKROBUS_AN ); \
212  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
213  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
214  // adc22_map // adc22
217 
222 typedef struct
223 {
224  // Output pins
225  digital_out_t rst;
227  // Input pins
228  digital_in_t rvs;
229  digital_in_t int_pin;
231  // Modules
232  spi_master_t spi;
234  pin_name_t chip_select;
236 } adc22_t;
237 
242 typedef struct
243 {
244  // Communication gpio pins
245  pin_name_t miso;
246  pin_name_t mosi;
247  pin_name_t sck;
248  pin_name_t cs;
250  // Additional gpio pins
251  pin_name_t rvs;
252  pin_name_t rst;
253  pin_name_t int_pin;
255  // static variable
256  uint32_t spi_speed;
257  spi_master_mode_t spi_mode;
258  spi_master_chip_select_polarity_t cs_polarity;
260 } adc22_cfg_t;
261 
266 typedef enum
267 {
268  ADC22_OK = 0,
269  ADC22_ERROR = -1
270 
272 
277 typedef enum
278 {
287  ADC22_RANGE_5V12 = 0x0B
290 
307 
321 err_t adc22_init ( adc22_t *ctx, adc22_cfg_t *cfg );
322 
335 err_t adc22_default_cfg ( adc22_t *ctx );
336 
348 void adc22_hw_reset ( adc22_t *ctx );
349 
362 uint8_t adc22_get_interrupt ( adc22_t *ctx );
363 
376 uint8_t adc22_get_rvs ( adc22_t *ctx );
377 
378 
396 err_t adc22_write_reg ( adc22_t *ctx, uint8_t w_cmd, uint8_t reg, uint8_t data_in );
397 
415 err_t adc22_read_reg ( adc22_t *ctx, uint8_t r_cmd, uint8_t reg, uint8_t *data_out );
416 
431 err_t adc22_get_adc_raw_data ( adc22_t *ctx, uint16_t *raw_adc );
432 
449 err_t adc22_get_voltage ( adc22_t *ctx, float *voltage );
450 
465 err_t adc22_set_device_addr ( adc22_t *ctx, uint8_t device_addr );
466 
481 err_t adc22_get_device_id ( adc22_t *ctx, uint8_t *device_id );
482 
497 err_t adc22_set_sdo_mode ( adc22_t *ctx, uint8_t sdo_mode );
498 
513 
530 
546 err_t adc22_get_range ( adc22_t *ctx, uint8_t *range );
547 
548 #ifdef __cplusplus
549 }
550 #endif
551 #endif // ADC22_H
552  // adc22
554 
555 // ------------------------------------------------------------------------ END
adc22_t::chip_select
pin_name_t chip_select
Definition: adc22.h:234
ADC22_RANGE_BI_5V12
@ ADC22_RANGE_BI_5V12
Definition: adc22.h:282
adc22_set_device_addr
err_t adc22_set_device_addr(adc22_t *ctx, uint8_t device_addr)
ADC 22 set device address function.
adc22_cfg_t::int_pin
pin_name_t int_pin
Definition: adc22.h:253
adc22_hw_reset
void adc22_hw_reset(adc22_t *ctx)
ADC 22 HW reset function.
adc22_get_device_id
err_t adc22_get_device_id(adc22_t *ctx, uint8_t *device_id)
ADC 22 get device ID function.
adc22_t
ADC 22 Click context object.
Definition: adc22.h:223
spi_specifics.h
This file contains SPI specific macros, functions, etc.
adc22_t::spi
spi_master_t spi
Definition: adc22.h:232
adc22_write_reg
err_t adc22_write_reg(adc22_t *ctx, uint8_t w_cmd, uint8_t reg, uint8_t data_in)
ADC 22 data writing function.
adc22_set_sdo_mode
err_t adc22_set_sdo_mode(adc22_t *ctx, uint8_t sdo_mode)
ADC 22 set SDO mode function.
adc22_cfg_t::rst
pin_name_t rst
Definition: adc22.h:252
adc22_cfg_setup
void adc22_cfg_setup(adc22_cfg_t *cfg)
ADC 22 configuration object setup function.
ADC22_RANGE_BI_12V28
@ ADC22_RANGE_BI_12V28
Definition: adc22.h:279
adc22_get_adc_raw_data
err_t adc22_get_adc_raw_data(adc22_t *ctx, uint16_t *raw_adc)
ADC 22 get ADC raw data function.
adc22_set_range
err_t adc22_set_range(adc22_t *ctx, adc22_range_sel_t range)
ADC 22 set range function.
adc22_range_sel_t
adc22_range_sel_t
ADC 22 Click ADC ranges selections.
Definition: adc22.h:278
adc22_read_reg
err_t adc22_read_reg(adc22_t *ctx, uint8_t r_cmd, uint8_t reg, uint8_t *data_out)
ADC 22 data reading function.
adc22_cfg_t
ADC 22 Click configuration object.
Definition: adc22.h:243
adc22_cfg_t::miso
pin_name_t miso
Definition: adc22.h:245
adc22_get_rvs
uint8_t adc22_get_rvs(adc22_t *ctx)
ADC 22 get RVS function.
adc22_cfg_t::mosi
pin_name_t mosi
Definition: adc22.h:246
adc22_default_cfg
err_t adc22_default_cfg(adc22_t *ctx)
ADC 22 default configuration function.
adc22_t::int_pin
digital_in_t int_pin
Definition: adc22.h:229
adc22_get_voltage
err_t adc22_get_voltage(adc22_t *ctx, float *voltage)
ADC 22 get voltage level function.
adc22_cfg_t::rvs
pin_name_t rvs
Definition: adc22.h:251
ADC22_RANGE_BI_6V14
@ ADC22_RANGE_BI_6V14
Definition: adc22.h:281
adc22_get_range
err_t adc22_get_range(adc22_t *ctx, uint8_t *range)
ADC 22 get range function.
ADC22_RANGE_6V14
@ ADC22_RANGE_6V14
Definition: adc22.h:286
ADC22_RANGE_5V12
@ ADC22_RANGE_5V12
Definition: adc22.h:287
adc22_t::rst
digital_out_t rst
Definition: adc22.h:225
adc22_cfg_t::spi_speed
uint32_t spi_speed
Definition: adc22.h:256
adc22_return_value_t
adc22_return_value_t
ADC 22 Click return value data.
Definition: adc22.h:267
adc22_cfg_t::sck
pin_name_t sck
Definition: adc22.h:247
ADC22_RANGE_10V24
@ ADC22_RANGE_10V24
Definition: adc22.h:285
ADC22_RANGE_BI_2V56
@ ADC22_RANGE_BI_2V56
Definition: adc22.h:283
ADC22_RANGE_BI_10V24
@ ADC22_RANGE_BI_10V24
Definition: adc22.h:280
ADC22_ERROR
@ ADC22_ERROR
Definition: adc22.h:269
adc22_init
err_t adc22_init(adc22_t *ctx, adc22_cfg_t *cfg)
ADC 22 initialization function.
ADC22_RANGE_12V28
@ ADC22_RANGE_12V28
Definition: adc22.h:284
ADC22_OK
@ ADC22_OK
Definition: adc22.h:268
adc22_t::rvs
digital_in_t rvs
Definition: adc22.h:228
adc22_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: adc22.h:257
adc22_cfg_t::cs
pin_name_t cs
Definition: adc22.h:248
adc22_get_interrupt
uint8_t adc22_get_interrupt(adc22_t *ctx)
ADC 22 get interrupt function.
adc22_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: adc22.h:258
adc22_en_out_data_parity
err_t adc22_en_out_data_parity(adc22_t *ctx)
ADC 22 enable output data parity function.