adc4  2.0.0.0
adc4.h
Go to the documentation of this file.
1 /*
2  * MikroSDK - MikroE Software Development Kit
3  * Copyright© 2020 MikroElektronika d.o.o.
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without restriction,
8  * including without limitation the rights to use, copy, modify, merge,
9  * publish, distribute, sublicense, and/or sell copies of the Software,
10  * and to permit persons to whom the Software is furnished to do so,
11  * subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22  * OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
33 // ----------------------------------------------------------------------------
34 
35 #ifndef ADC4_H
36 #define ADC4_H
37 
38 #include "mikrosdk_version.h"
39 
40 #ifdef __GNUC__
41 #if mikroSDK_GET_VERSION < 20800ul
42 #include "rcu_delays.h"
43 #else
44 #include "delays.h"
45 #endif
46 #endif
47 
48 #include "drv_digital_out.h"
49 #include "drv_digital_in.h"
50 #include "drv_spi_master.h"
51 
52 // -------------------------------------------------------------- PUBLIC MACROS
63 #define ADC4_MAP_MIKROBUS( cfg, mikrobus ) \
64  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
65  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
66  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
67  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
68  cfg.err = MIKROBUS( mikrobus, MIKROBUS_INT )
69 
75 #define ADC4_RETVAL uint8_t
76 
77 #define ADC4_OK 0x00
78 #define ADC4_INIT_ERROR 0xFF
79 
85 #define ADC4_RESOLUTION 8388607
86 
92 #define ADC4_VREF_4000MV 4.096
93 #define ADC4_VREF_2500MV 2.5
94 
100 #define ADC4_STATUS_REG 0x00
101 #define ADC4_MODE_REG 0x01
102 #define ADC4_IFACE_MODE_REG 0x02
103 #define ADC4_CHECK_REG 0x03
104 #define ADC4_DATA_REG 0x04
105 #define ADC4_GPIO_CONFIG_REG 0x06
106 #define ADC4_ID_REG 0x07
107 #define ADC4_SETUP_CONFIGURATION_REG 0x20
108 
114 #define ADC4_CH_REG_BASE 0x10
115 #define ADC4_CON_REG_BASE 0x20
116 #define ADC4_FILCON_REG_BASE 0x28
117 #define ADC4_OFFSET_REG_BASE 0x30
118 #define ADC4_GAIN_REG_BASE 0x38
119 
125 #define ADC4_STATUS_RDY 0x80
126 #define ADC4_STATUS_ERR 0x40
127 #define ADC4_STATUS_CRC_ERR 0x20
128 #define ADC4_STATUS_REG_ERR 0x10
129 #define ADC4_STATUS_CH_ACTIVE 0x0F
130 
136 #define ADC4_MODE_INT_REF_AND_SING_CYC_EN 0xA000
137 #define ADC4_MODE_INT_REF_EN 0x8000
138 #define ADC4_MODE_HIDE_RELAY_DIS 0x4000
139 #define ADC4_MODE_SING_CYC_EN 0x2000
140 #define ADC4_MODE_DELAY_0 0x0000
141 #define ADC4_MODE_DELAY_4 0x0100
142 #define ADC4_MODE_DELAY_16 0x0200
143 #define ADC4_MODE_DELAY_40 0x0300
144 #define ADC4_MODE_DELAY_100 0x0400
145 #define ADC4_MODE_DELAY_200 0x0500
146 #define ADC4_MODE_DELAY_500 0x0600
147 #define ADC4_MODE_DELAY_1000 0x0700
148 #define ADC4_MODE_CONTINUOUS 0x0000
149 #define ADC4_MODE_SINGLE 0x0010
150 #define ADC4_MODE_STBY 0x0020
151 #define ADC4_MODE_POWER_DOWN 0x0030
152 #define ADC4_MODE_INTERNAL_OFFSET 0x0040
153 #define ADC4_MODE_SYSTEM_OFFSET 0x0060
154 #define ADC4_MODE_GAIN_OFFSET 0x0070
155 #define ADC4_MODE_CLK_INTOSC 0x0000
156 #define ADC4_MODE_CLK_INTOSC_XT2 0x0002
157 #define ADC4_MODE_CLK_EXTOSC_XT2 0x0004
158 #define ADC4_MODE_CLK_EXTOSC 0x0006
159 
165 #define ADC4_IFACE_MODE_ALTSYNC_EN 0x1000
166 #define ADC4_IFACE_MODE_IOSTREN_EN 0x0800
167 #define ADC4_IFACE_MODE_DOUTRES_EN 0x0100
168 #define ADC4_IFACE_MODE_CONTRD_EN 0x0080
169 #define ADC4_IFACE_MODE_DATASTA_EN 0x0040
170 #define ADC4_IFACE_MODE_REGCHK_EN 0x0020
171 #define ADC4_IFACE_MODE_CRC_DIS 0x0000
172 #define ADC4_IFACE_MODE_CRC_RD_EN 0x0002
173 #define ADC4_IFACE_MODE_CRC_RW_EN 0x0004
174 
180 #define ADC4_CFG_PDSW 0x4000
181 #define ADC4_CFG_OP_EN2_3 0x2000
182 #define ADC4_CFG_MUX_IO 0x1000
183 #define ADC4_CFG_SYNC_EN 0x0800
184 #define ADC4_CFG_ERR_DISABLE 0x0000
185 #define ADC4_CFG_ERR_INPUT 0x0400
186 #define ADC4_CFG_ERR_OPENDRAIN 0x0200
187 #define ADC4_CFG_ERR_OUTPUT 0x0600
188 #define ADC4_CFG_ERR_DAT 0x0010
189 
195 #define ADC4_CH_EN 0x8000
196 #define ADC4_CH_SETUP_0 0x0001 << 12
197 #define ADC4_CH_SETUP_1 0x0002 << 12
198 #define ADC4_CH_SETUP_2 0x0003 << 12
199 #define ADC4_CH_SETUP_3 0x0004 << 12
200 #define ADC4_CH_SETUP_4 0x0005 << 12
201 #define ADC4_CH_SETUP_5 0x0006 << 12
202 #define ADC4_CH_SETUP_6 0x0007 << 12
203 #define ADC4_CH_SETUP_7 0x0008 << 12
204 #define ADC4_CH_AINPOS_0 0x0000 << 5
205 #define ADC4_CH_AINPOS_1 0x0001 << 5
206 #define ADC4_CH_AINPOS_2 0x0002 << 5
207 #define ADC4_CH_AINPOS_3 0x0003 << 5
208 #define ADC4_CH_AINPOS_4 0x0004 << 5
209 #define ADC4_CH_AINPOS_5 0x0005 << 5
210 #define ADC4_CH_AINPOS_6 0x0006 << 5
211 #define ADC4_CH_AINPOS_7 0x0007 << 5
212 #define ADC4_CH_AINPOS_8 0x0008 << 5
213 #define ADC4_CH_AINPOS_9 0x0009 << 5
214 #define ADC4_CH_AINPOS_10 0x000A << 5
215 #define ADC4_CH_AINPOS_11 0x000B << 5
216 #define ADC4_CH_AINPOS_12 0x000C << 5
217 #define ADC4_CH_AINPOS_13 0x000D << 5
218 #define ADC4_CH_AINPOS_14 0x000E << 5
219 #define ADC4_CH_AINPOS_15 0x000F << 5
220 #define ADC4_CH_AINPOS_16 0x0010 << 5
221 #define ADC4_CH_AINPOS_TEMP_P 0x0011 << 5
222 #define ADC4_CH_AINPOS_TEMP_N 0x0012 << 5
223 #define ADC4_CH_AINPOS_AV_P 0x0013 << 5
224 #define ADC4_CH_AINPOS_AV_N 0x0014 << 5
225 #define ADC4_CH_AINPOS_REF_P 0x0015 << 5
226 #define ADC4_CH_AINPOS_REF_N 0x0016 << 5
227 #define ADC4_CH_AINNEG_0 0x0000
228 #define ADC4_CH_AINNEG_1 0x0001
229 #define ADC4_CH_AINNEG_2 0x0002
230 #define ADC4_CH_AINNEG_3 0x0003
231 #define ADC4_CH_AINNEG_4 0x0004
232 #define ADC4_CH_AINNEG_5 0x0005
233 #define ADC4_CH_AINNEG_6 0x0006
234 #define ADC4_CH_AINNEG_7 0x0007
235 #define ADC4_CH_AINNEG_8 0x0008
236 #define ADC4_CH_AINNEG_9 0x0009
237 #define ADC4_CH_AINNEG_10 0x000A
238 #define ADC4_CH_AINNEG_11 0x000B
239 #define ADC4_CH_AINNEG_12 0x000C
240 #define ADC4_CH_AINNEG_13 0x000D
241 #define ADC4_CH_AINNEG_14 0x000E
242 #define ADC4_CH_AINNEG_15 0x000F
243 #define ADC4_CH_AINNEG_16 0x0010
244 #define ADC4_CH_AINNEG_TEMP_P 0x0011
245 #define ADC4_CH_AINNEG_TEMP_N 0x0012
246 #define ADC4_CH_AINNEG_AV_P 0x0013
247 #define ADC4_CH_AINNEG_AV_N 0x0014
248 #define ADC4_CH_AINNEG_REF_P 0x0015
249 #define ADC4_CH_AINNEG_REF_N 0x0016
250 
256 #define ADC4_CON_UNIPOL 0x0000
257 #define ADC4_CON_BIPOL 0x0001 << 12
258 #define ADC4_CON_REFBUF_P_EN 0x0001 << 11
259 #define ADC4_CON_REFBUF_N_EN 0x0001 << 10
260 #define ADC4_CON_AINBUF_P_EN 0x0001 << 9
261 #define ADC4_CON_AINBUF_N_EN 0x0001 << 8
262 #define ADC4_CON_BURNOUT_EN 0x0001 << 7
263 #define ADC4_CON_EXTREF 0x0000
264 #define ADC4_CON_EXTREF_SUPP 0x0001 << 4
265 #define ADC4_CON_EXTREF_INT 0x0002 << 4
266 #define ADC4_CON_EXTREF_AV 0x0003 << 4
267 
273 #define ADC4_FILCON_SINC_MAP0 0x8000
274 #define ADC4_FILCON_ENHFILEN 0x0001 << 11
275 #define ADC4_FILCON_ENHFIL_SET1 0x0002 << 8
276 #define ADC4_FILCON_EHFFIL_SET2 0x0003 << 8
277 #define ADC4_FILCON_ENHFIL_SET3 0x0005 << 8
278 #define ADC4_FILCON_ENHFIL_SET4 0x0006 << 8
279 #define ADC4_FILCON_ORD_SINC5 0x0000
280 #define ADC4_FILCON_ORD_SINC3 0x0003 << 5
281 #define ADC4_FILCON_ODR_250000 0x0000
282 #define ADC4_FILCON_ODR_125000 0x0001
283 #define ADC4_FILCON_ODR_62500 0x0002
284 #define ADC4_FILCON_ODR_50000 0x0003
285 #define ADC4_FILCON_ODR_31250 0x0004
286 #define ADC4_FILCON_ODR_25000 0x0005
287 #define ADC4_FILCON_ODR_15625 0x0006
288 #define ADC4_FILCON_ODR_10000 0x0007
289 #define ADC4_FILCON_ODR_5000 0x0008
290 #define ADC4_FILCON_ODR_2500 0x0009
291 #define ADC4_FILCON_ODR_1000 0x000A
292 #define ADC4_FILCON_ODR_500 0x000B
293 #define ADC4_FILCON_ODR_397_5 0x000C
294 #define ADC4_FILCON_ODR_200 0x000D
295 #define ADC4_FILCON_ODR_100 0x000E
296 #define ADC4_FILCON_ODR_59_92 0x000F
297 #define ADC4_FILCON_ODR_49_96 0x0010
298 #define ADC4_FILCON_ODR_20 0x0011
299 #define ADC4_FILCON_ODR_16_66 0x0012
300 #define ADC4_FILCON_ODR_10 0x0013
301 #define ADC4_FILCON_ODR_5 0x0014
302  // End group macro
305 // --------------------------------------------------------------- PUBLIC TYPES
314 typedef struct
315 {
316  digital_out_t cs;
317 
318  // Input pins
319 
320  digital_in_t err;
321 
322  // Modules
323 
324  spi_master_t spi;
325  pin_name_t chip_select;
326 
327  uint16_t w_dog;
328  uint8_t sing_bit;
329 
330 } adc4_t;
331 
335 typedef struct
336 {
337  // Communication gpio pins
338 
339  pin_name_t miso;
340  pin_name_t mosi;
341  pin_name_t sck;
342  pin_name_t cs;
343 
344  // Additional gpio pins
345 
346  pin_name_t err;
347 
348  // static variable
349 
350  uint32_t spi_speed;
351  spi_master_mode_t spi_mode;
352  spi_master_chip_select_polarity_t cs_polarity;
353 
354 } adc4_cfg_t;
355  // End types group
357 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
358 
363 #ifdef __cplusplus
364 extern "C"{
365 #endif
366 
376 
386 
394 void adc4_default_cfg ( adc4_t *ctx );
395 
420 void adc4_generic_transfer ( adc4_t *ctx, uint8_t *wr_buf, uint16_t wr_len, uint8_t *rd_buf, uint16_t rd_len );
421 
433 uint8_t adc4_write_reg ( adc4_t *ctx, const uint8_t reg_address, uint8_t *value );
434 
446 uint8_t adc4_read_reg ( adc4_t *ctx, const uint8_t reg_address, uint8_t *value );
447 
455 void adc4_reset ( adc4_t *ctx );
456 
466 uint8_t adc4_get_err_pin ( adc4_t *ctx );
467 
477 void adc4_get_config ( adc4_t *ctx, uint8_t reg_address, uint16_t *value );
478 
488 void adc4_set_config ( adc4_t *ctx, const uint8_t reg_address, uint16_t value );
489 
499 uint32_t adc4_get_data ( adc4_t *ctx );
500 
511 uint16_t adc4_get_voltage ( adc4_t *ctx, const float ref_voltage );
512 
513 #ifdef __cplusplus
514 }
515 #endif
516 #endif // _ADC4_H_
517  // End public_function group
520 
521 // ------------------------------------------------------------------------- END
adc4_default_cfg
void adc4_default_cfg(adc4_t *ctx)
Click Default Configuration function.
ADC4_RETVAL
#define ADC4_RETVAL
Definition: adc4.h:75
adc4_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: adc4.h:352
adc4_cfg_t::err
pin_name_t err
Definition: adc4.h:346
adc4_t::sing_bit
uint8_t sing_bit
Definition: adc4.h:328
adc4_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: adc4.h:351
adc4_cfg_setup
void adc4_cfg_setup(adc4_cfg_t *cfg)
Config Object Initialization function.
adc4_cfg_t::sck
pin_name_t sck
Definition: adc4.h:341
adc4_generic_transfer
void adc4_generic_transfer(adc4_t *ctx, uint8_t *wr_buf, uint16_t wr_len, uint8_t *rd_buf, uint16_t rd_len)
Generic transfer function.
adc4_reset
void adc4_reset(adc4_t *ctx)
Reset function.
adc4_write_reg
uint8_t adc4_write_reg(adc4_t *ctx, const uint8_t reg_address, uint8_t *value)
Generic Write Function.
adc4_get_voltage
uint16_t adc4_get_voltage(adc4_t *ctx, const float ref_voltage)
Get voltage function.
adc4_get_config
void adc4_get_config(adc4_t *ctx, uint8_t reg_address, uint16_t *value)
Get configuration function.
adc4_t
Click ctx object definition.
Definition: adc4.h:315
adc4_get_data
uint32_t adc4_get_data(adc4_t *ctx)
Get data function.
adc4_t::chip_select
pin_name_t chip_select
Definition: adc4.h:325
adc4_get_err_pin
uint8_t adc4_get_err_pin(adc4_t *ctx)
Error check function.
adc4_cfg_t::miso
pin_name_t miso
Definition: adc4.h:339
adc4_cfg_t::cs
pin_name_t cs
Definition: adc4.h:342
adc4_set_config
void adc4_set_config(adc4_t *ctx, const uint8_t reg_address, uint16_t value)
Set configuration function.
adc4_t::spi
spi_master_t spi
Definition: adc4.h:324
adc4_cfg_t
Click configuration structure definition.
Definition: adc4.h:336
adc4_cfg_t::mosi
pin_name_t mosi
Definition: adc4.h:340
adc4_read_reg
uint8_t adc4_read_reg(adc4_t *ctx, const uint8_t reg_address, uint8_t *value)
Generic Write Function.
adc4_t::cs
digital_out_t cs
Definition: adc4.h:316
adc4_init
ADC4_RETVAL adc4_init(adc4_t *ctx, adc4_cfg_t *cfg)
Initialization function.
adc4_cfg_t::spi_speed
uint32_t spi_speed
Definition: adc4.h:350
adc4_t::err
digital_in_t err
Definition: adc4.h:320
adc4_t::w_dog
uint16_t w_dog
Definition: adc4.h:327