adc18  2.0.0.0
adc18.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 ADC18_H
29 #define ADC18_H
30 
31 #ifdef __cplusplus
32 extern "C"{
33 #endif
34 
35 #include "mikrosdk_version.h"
36 
37 #ifdef __GNUC__
38 #if mikroSDK_GET_VERSION < 20800ul
39 #include "rcu_delays.h"
40 #else
41 #include "delays.h"
42 #endif
43 #endif
44 
45 #include "drv_digital_out.h"
46 #include "drv_digital_in.h"
47 #include "drv_spi_master.h"
48 #include "spi_specifics.h"
49 
70 #define ADC18_REG_GEN_PROD 0x00
71 #define ADC18_REG_GEN_REV 0x01
72 #define ADC18_REG_GEN_CNFG 0x02
73 #define ADC18_REG_GEN_CHNL_CTRL 0x03
74 #define ADC18_REG_GEN_GPIO_CTRL 0x04
75 #define ADC18_REG_GEN_GPI_INT 0x05
76 #define ADC18_REG_GEN_GPI_DATA 0x06
77 #define ADC18_REG_GEN_INT 0x07
78 #define ADC18_REG_GEN_INTEN 0x08
79 #define ADC18_REG_GEN_PWR_CTRL 0x09
80 #define ADC18_REG_DCHNL_CMD 0x20
81 #define ADC18_REG_DCHNL_STA 0x21
82 #define ADC18_REG_DCHNL_CTRL1 0x22
83 #define ADC18_REG_DCHNL_CTRL2 0x23
84 #define ADC18_REG_DCHNL_DATA 0x24
85 #define ADC18_REG_DCHNL_N_SEL 0x25
86 #define ADC18_REG_DCHNL_N_SOC 0x26
87 #define ADC18_REG_DCHNL_N_SGC 0x27
88  // adc18_reg
90 
105 #define ADC18_PRODUCT_ID 0x18
106 
111 #define ADC18_GEN_CNFG_CRC_EN 0x800000ul
112 
117 #define ADC18_DATA_RESOLUTION 0x7FFFFFul
118 #define ADC18_FULL_SCALE_VOLTAGE 12.5
119 
124 #define ADC18_START_CONVERSION 0x30
125 #define ADC18_STOP_CONVERSION 0x10
126 #define ADC18_DATA_RATE_5_SPS 0x00
127 #define ADC18_DATA_RATE_10_SPS 0x01
128 #define ADC18_DATA_RATE_15_SPS 0x02
129 #define ADC18_DATA_RATE_30_SPS 0x03
130 #define ADC18_DATA_RATE_50_SPS 0x04
131 #define ADC18_DATA_RATE_60_SPS 0x05
132 #define ADC18_DATA_RATE_225_SPS 0x06
133 #define ADC18_DATA_RATE_450_SPS 0x07
134 #define ADC18_DATA_RATE_900_SPS 0x08
135 #define ADC18_DATA_RATE_1800_SPS 0x09
136 #define ADC18_DATA_RATE_3600_SPS 0x0A
137 #define ADC18_DATA_RATE_7200_SPS 0x0B
138 #define ADC18_DATA_RATE_14400_SPS 0x0C
139 #define ADC18_DATA_RATE_28800_SPS 0x0D
140 #define ADC18_DATA_RATE_57600_SPS 0x0E
141 #define ADC18_DATA_RATE_115200_SPS 0x0F
142 
147 #define ADC18_PD_STATE_STANDBY 0x00
148 #define ADC18_PD_STATE_RESET 0x10
149 #define ADC18_CONV_MODE_CONTINUOUS 0x00
150 #define ADC18_CONV_MODE_SINGLE_CYCLE 0x02
151 #define ADC18_CONV_MODE_CONT_SINGLE_CYCLE 0x03
152 
157 #define ADC18_CH_AI1_SINGLE_ENDED 0x00
158 #define ADC18_CH_AI2_SINGLE_ENDED 0x01
159 #define ADC18_CH_AI3_SINGLE_ENDED 0x02
160 #define ADC18_CH_AI4_SINGLE_ENDED 0x03
161 #define ADC18_CH_AI5_SINGLE_ENDED 0x04
162 #define ADC18_CH_AI6_SINGLE_ENDED 0x05
163 #define ADC18_CH_AI7_SINGLE_ENDED 0x06
164 #define ADC18_CH_AI8_SINGLE_ENDED 0x07
165 #define ADC18_CH_AI9_SINGLE_ENDED 0x08
166 #define ADC18_CH_AI10_SINGLE_ENDED 0x09
167 #define ADC18_CH_AI11_SINGLE_ENDED 0x0A
168 #define ADC18_CH_AI12_SINGLE_ENDED 0x0B
169 #define ADC18_CH_AI1_AI2_DIFFERENTIAL 0x0C
170 #define ADC18_CH_AI3_AI4_DIFFERENTIAL 0x0D
171 #define ADC18_CH_AI5_AI6_DIFFERENTIAL 0x0E
172 #define ADC18_CH_AI7_AI8_DIFFERENTIAL 0x0F
173 #define ADC18_CH_AI9_AI10_DIFFERENTIAL 0x10
174 #define ADC18_CH_AI11_AI12_DIFFERENTIAL 0x11
175 #define ADC18_CH_AI1_COM_AI2_MULTI_DIFF 0x12
176 #define ADC18_CH_AI1_COM_AI3_MULTI_DIFF 0x13
177 #define ADC18_CH_AI4_COM_AI5_MULTI_DIFF 0x14
178 #define ADC18_CH_AI4_COM_AI6_MULTI_DIFF 0x15
179 #define ADC18_CH_AI7_COM_AI8_MULTI_DIFF 0x16
180 #define ADC18_CH_AI7_COM_AI9_MULTI_DIFF 0x17
181 #define ADC18_CH_AI10_COM_AI11_MULTI_DIFF 0x18
182 #define ADC18_CH_AI10_COM_AI12_MULTI_DIFF 0x19
183 #define ADC18_AIP_TEST_DISABLED 0x00
184 #define ADC18_AIP_TEST_2MOHM_TO_AGND 0x40
185 #define ADC18_AIP_TEST_2MOHM_TO_HVDD 0x80
186 #define ADC18_AIP_TEST_2MOHM_TO_HVDD_AGND 0xC0
187 #define ADC18_AIN_TEST_DISABLED 0x00
188 #define ADC18_AIN_TEST_2MOHM_TO_AGND 0x10
189 #define ADC18_AIN_TEST_2MOHM_TO_HVDD 0x20
190 #define ADC18_AIN_TEST_2MOHM_TO_HVDD_AGND 0x30
191 
200 #define ADC18_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
201 #define ADC18_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
202  // adc18_set
204 
219 #define ADC18_MAP_MIKROBUS( cfg, mikrobus ) \
220  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
221  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
222  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
223  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
224  cfg.rdy = MIKROBUS( mikrobus, MIKROBUS_AN ); \
225  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
226  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
227  // adc18_map // adc18
230 
235 typedef struct
236 {
237  // Output pins
238  digital_out_t rst;
240  // Input pins
241  digital_in_t rdy;
242  digital_in_t int_pin;
244  // Modules
245  spi_master_t spi;
247  pin_name_t chip_select;
251 } adc18_t;
252 
257 typedef struct
258 {
259  // Communication gpio pins
260  pin_name_t miso;
261  pin_name_t mosi;
262  pin_name_t sck;
263  pin_name_t cs;
265  // Additional gpio pins
266  pin_name_t rst;
267  pin_name_t rdy;
268  pin_name_t int_pin;
270  // static variable
271  uint32_t spi_speed;
272  spi_master_mode_t spi_mode;
273  spi_master_chip_select_polarity_t cs_polarity;
275 } adc18_cfg_t;
276 
281 typedef enum
282 {
283  ADC18_OK = 0,
284  ADC18_ERROR = -1
285 
287 
304 
318 err_t adc18_init ( adc18_t *ctx, adc18_cfg_t *cfg );
319 
332 err_t adc18_default_cfg ( adc18_t *ctx );
333 
347 err_t adc18_write_register ( adc18_t *ctx, uint8_t reg, uint32_t data_in );
348 
362 err_t adc18_read_register ( adc18_t *ctx, uint8_t reg, uint32_t *data_out );
363 
374 
385 
396 
406 
415 uint8_t adc18_get_rdy_pin ( adc18_t *ctx );
416 
425 uint8_t adc18_get_int_pin ( adc18_t *ctx );
426 
437 err_t adc18_start_conversion ( adc18_t *ctx, uint8_t data_rate );
438 
449 
463 err_t adc18_set_conversion_mode ( adc18_t *ctx, uint8_t mode );
464 
475 err_t adc18_set_active_channel ( adc18_t *ctx, uint8_t channel );
476 
487 err_t adc18_read_raw_adc ( adc18_t *ctx, int32_t *raw_adc );
488 
499 err_t adc18_read_voltage ( adc18_t *ctx, float *voltage );
500 
501 #ifdef __cplusplus
502 }
503 #endif
504 #endif // ADC18_H
505  // adc18
507 
508 // ------------------------------------------------------------------------ END
adc18_get_int_pin
uint8_t adc18_get_int_pin(adc18_t *ctx)
ADC 18 get int pin function.
adc18_return_value_t
adc18_return_value_t
ADC 18 Click return value data.
Definition: adc18.h:282
adc18_t::rst
digital_out_t rst
Definition: adc18.h:238
adc18_write_register
err_t adc18_write_register(adc18_t *ctx, uint8_t reg, uint32_t data_in)
ADC 18 write register function.
adc18_cfg_t::mosi
pin_name_t mosi
Definition: adc18.h:261
spi_specifics.h
This file contains SPI specific macros, functions, etc.
adc18_read_register
err_t adc18_read_register(adc18_t *ctx, uint8_t reg, uint32_t *data_out)
ADC 18 read register function.
adc18_init
err_t adc18_init(adc18_t *ctx, adc18_cfg_t *cfg)
ADC 18 initialization function.
adc18_t::chip_select
pin_name_t chip_select
Definition: adc18.h:247
adc18_cfg_t::int_pin
pin_name_t int_pin
Definition: adc18.h:268
adc18_reset_device
void adc18_reset_device(adc18_t *ctx)
ADC 18 reset device function.
adc18_t::rdy
digital_in_t rdy
Definition: adc18.h:241
adc18_set_active_channel
err_t adc18_set_active_channel(adc18_t *ctx, uint8_t channel)
ADC 18 set active channel function.
adc18_cfg_t::sck
pin_name_t sck
Definition: adc18.h:262
adc18_cfg_setup
void adc18_cfg_setup(adc18_cfg_t *cfg)
ADC 18 configuration object setup function.
adc18_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: adc18.h:273
adc18_t::spi_crc_enable
bool spi_crc_enable
Definition: adc18.h:249
adc18_cfg_t
ADC 18 Click configuration object.
Definition: adc18.h:258
adc18_stop_conversion
err_t adc18_stop_conversion(adc18_t *ctx)
ADC 18 stop conversion function.
adc18_t::int_pin
digital_in_t int_pin
Definition: adc18.h:242
adc18_cfg_t::rdy
pin_name_t rdy
Definition: adc18.h:267
adc18_read_voltage
err_t adc18_read_voltage(adc18_t *ctx, float *voltage)
ADC 18 read voltage function.
adc18_cfg_t::rst
pin_name_t rst
Definition: adc18.h:266
adc18_cfg_t::cs
pin_name_t cs
Definition: adc18.h:263
adc18_disable_spi_crc
err_t adc18_disable_spi_crc(adc18_t *ctx)
ADC 18 disable spi crc function.
adc18_read_raw_adc
err_t adc18_read_raw_adc(adc18_t *ctx, int32_t *raw_adc)
ADC 18 read raw adc function.
adc18_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: adc18.h:272
adc18_check_communication
err_t adc18_check_communication(adc18_t *ctx)
ADC 18 check communication function.
adc18_t
ADC 18 Click context object.
Definition: adc18.h:236
adc18_start_conversion
err_t adc18_start_conversion(adc18_t *ctx, uint8_t data_rate)
ADC 18 start conversion function.
adc18_cfg_t::miso
pin_name_t miso
Definition: adc18.h:260
adc18_enable_spi_crc
err_t adc18_enable_spi_crc(adc18_t *ctx)
ADC 18 enable spi crc function.
ADC18_ERROR
@ ADC18_ERROR
Definition: adc18.h:284
adc18_set_conversion_mode
err_t adc18_set_conversion_mode(adc18_t *ctx, uint8_t mode)
ADC 18 set conversion mode function.
adc18_get_rdy_pin
uint8_t adc18_get_rdy_pin(adc18_t *ctx)
ADC 18 get rdy pin function.
adc18_t::spi
spi_master_t spi
Definition: adc18.h:245
adc18_cfg_t::spi_speed
uint32_t spi_speed
Definition: adc18.h:271
adc18_default_cfg
err_t adc18_default_cfg(adc18_t *ctx)
ADC 18 default configuration function.
ADC18_OK
@ ADC18_OK
Definition: adc18.h:283