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 
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 ADC22_CMD_NOP 0x00
61 #define ADC22_CMD_CLEAR_HWORD 0xC0
62 #define ADC22_CMD_READ_HWORD 0xC8
63 #define ADC22_CMD_READ 0x48
64 #define ADC22_CMD_WRITE 0xD0
65 #define ADC22_CMD_WRITE_MS 0xD2
66 #define ADC22_CMD_WRITE_LS 0xD4
67 #define ADC22_CMD_SET_HWORD 0xD8
68  // adc22_cmd
70 
85 #define ADC22_REG_DEVICE_ID 0x02
86 #define ADC22_REG_RST_PWRCTL_LSB 0x04
87 #define ADC22_REG_RST_PWRCTL_MSB 0x05
88 #define ADC22_REG_SDI_CTL 0x08
89 #define ADC22_REG_SDO_CTL_LSB 0x0C
90 #define ADC22_REG_SDO_CTL_MSB 0x0D
91 #define ADC22_REG_DATAOUT_CTL_LSB 0x10
92 #define ADC22_REG_DATAOUT_CTL_MSB 0x11
93 #define ADC22_REG_RANGE_SEL 0x14
94 #define ADC22_REG_ALARM_LSB 0x20
95 #define ADC22_REG_ALARM_MSB 0x21
96 #define ADC22_REG_ALARM_H_TH_LSB 0x24
97 #define ADC22_REG_ALARM_H_TH_MSB 0x25
98 #define ADC22_REG_ALARM_L_TH_LSB 0x28
99 #define ADC22_REG_ALARM_L_TH_MSB 0x29
100  // adc22_reg
102 
117 #define ADC22_DEVICE_ADDR_MAX 16
118 
123 #define ADC22_SDO_MODE_SAME_SDI 1
124 #define ADC22_SDO_MODE_INV 2
125 #define ADC22_SDO_MODE_ADC_M_CLK 3
126 
131 #define ADC22_DATAOUT_CTL_VAL_CONV 0x00
132 #define ADC22_DATAOUT_CTL_VAL_0_S 0x04
133 #define ADC22_DATAOUT_CTL_VAL_1_S 0x05
134 #define ADC22_DATAOUT_CTL_VAL_A_01_S 0x06
135 #define ADC22_DATAOUT_CTL_VAL_A_0011_S 0x07
136 #define ADC22_DATAOUT_CTL_PAR_DIS 0x00
137 #define ADC22_DATAOUT_CTL_PAR_EN 0x08
138 
143 #define ADC22_ADC_ZERO_SCALE 2045u
144 #define ADC22_ADC_MIDDLE_SCALE 2048.0f
145 #define ADC22_ADC_FULL_SCALE 4096.0f
146 
151 #define ADC22_ADC_RANGE_12V28 12.28f
152 #define ADC22_ADC_RANGE_10V24 10.24f
153 #define ADC22_ADC_RANGE_6V14 6.14f
154 #define ADC22_ADC_RANGE_5V12 5.12f
155 #define ADC22_ADC_RANGE_2V56 2.56f
156 #define ADC22_SET_RANGE_BIT_MASK 0x0F
157 
162 #define ADC22_PIN_STATE_LOW 0x00
163 #define ADC22_PIN_STATE_HIGH 0x01
164 
173 #define ADC22_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
174 #define ADC22_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
175  // adc22_set
177 
192 #define ADC22_MAP_MIKROBUS( cfg, mikrobus ) \
193  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
194  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
195  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
196  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
197  cfg.rvs = MIKROBUS( mikrobus, MIKROBUS_AN ); \
198  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
199  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
200  // adc22_map // adc22
203 
208 typedef struct
209 {
210  // Output pins
211  digital_out_t rst;
213  // Input pins
214  digital_in_t rvs;
215  digital_in_t int_pin;
217  // Modules
218  spi_master_t spi;
220  pin_name_t chip_select;
222 } adc22_t;
223 
228 typedef struct
229 {
230  // Communication gpio pins
231  pin_name_t miso;
232  pin_name_t mosi;
233  pin_name_t sck;
234  pin_name_t cs;
236  // Additional gpio pins
237  pin_name_t rvs;
238  pin_name_t rst;
239  pin_name_t int_pin;
241  // static variable
242  uint32_t spi_speed;
243  spi_master_mode_t spi_mode;
244  spi_master_chip_select_polarity_t cs_polarity;
246 } adc22_cfg_t;
247 
252 typedef enum
253 {
254  ADC22_OK = 0,
255  ADC22_ERROR = -1
256 
258 
263 typedef enum
264 {
273  ADC22_RANGE_5V12 = 0x0B
276 
293 
307 err_t adc22_init ( adc22_t *ctx, adc22_cfg_t *cfg );
308 
321 err_t adc22_default_cfg ( adc22_t *ctx );
322 
334 void adc22_hw_reset ( adc22_t *ctx );
335 
348 uint8_t adc22_get_interrupt ( adc22_t *ctx );
349 
362 uint8_t adc22_get_rvs ( adc22_t *ctx );
363 
364 
382 err_t adc22_write_reg ( adc22_t *ctx, uint8_t w_cmd, uint8_t reg, uint8_t data_in );
383 
401 err_t adc22_read_reg ( adc22_t *ctx, uint8_t r_cmd, uint8_t reg, uint8_t *data_out );
402 
417 err_t adc22_get_adc_raw_data ( adc22_t *ctx, uint16_t *raw_adc );
418 
435 err_t adc22_get_voltage ( adc22_t *ctx, float *voltage );
436 
451 err_t adc22_set_device_addr ( adc22_t *ctx, uint8_t device_addr );
452 
467 err_t adc22_get_device_id ( adc22_t *ctx, uint8_t *device_id );
468 
483 err_t adc22_set_sdo_mode ( adc22_t *ctx, uint8_t sdo_mode );
484 
499 
516 
532 err_t adc22_get_range ( adc22_t *ctx, uint8_t *range );
533 
534 #ifdef __cplusplus
535 }
536 #endif
537 #endif // ADC22_H
538  // adc22
540 
541 // ------------------------------------------------------------------------ END
adc22_t::chip_select
pin_name_t chip_select
Definition: adc22.h:220
ADC22_RANGE_BI_5V12
@ ADC22_RANGE_BI_5V12
Definition: adc22.h:268
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:239
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:209
spi_specifics.h
This file contains SPI specific macros, functions, etc.
adc22_t::spi
spi_master_t spi
Definition: adc22.h:218
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:238
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:265
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:264
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:229
adc22_cfg_t::miso
pin_name_t miso
Definition: adc22.h:231
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:232
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:215
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:237
ADC22_RANGE_BI_6V14
@ ADC22_RANGE_BI_6V14
Definition: adc22.h:267
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:272
ADC22_RANGE_5V12
@ ADC22_RANGE_5V12
Definition: adc22.h:273
adc22_t::rst
digital_out_t rst
Definition: adc22.h:211
adc22_cfg_t::spi_speed
uint32_t spi_speed
Definition: adc22.h:242
adc22_return_value_t
adc22_return_value_t
ADC 22 Click return value data.
Definition: adc22.h:253
adc22_cfg_t::sck
pin_name_t sck
Definition: adc22.h:233
ADC22_RANGE_10V24
@ ADC22_RANGE_10V24
Definition: adc22.h:271
ADC22_RANGE_BI_2V56
@ ADC22_RANGE_BI_2V56
Definition: adc22.h:269
ADC22_RANGE_BI_10V24
@ ADC22_RANGE_BI_10V24
Definition: adc22.h:266
ADC22_ERROR
@ ADC22_ERROR
Definition: adc22.h:255
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:270
ADC22_OK
@ ADC22_OK
Definition: adc22.h:254
adc22_t::rvs
digital_in_t rvs
Definition: adc22.h:214
adc22_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: adc22.h:243
adc22_cfg_t::cs
pin_name_t cs
Definition: adc22.h:234
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:244
adc22_en_out_data_parity
err_t adc22_en_out_data_parity(adc22_t *ctx)
ADC 22 enable output data parity function.