adc24  2.1.0.0
adc24.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 ADC24_H
29 #define ADC24_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 ADC24_CTRL_SET_WRITE 0x0800u
71 #define ADC24_CTRL_SET_SEQ 0x0400u
72 #define ADC24_CTRL_SET_CH_0 0x0000u
73 #define ADC24_CTRL_SET_CH_1 0x0040u
74 #define ADC24_CTRL_SET_CH_2 0x0080u
75 #define ADC24_CTRL_SET_CH_3 0x00C0u
76 #define ADC24_CTRL_SET_CH_4 0x0100u
77 #define ADC24_CTRL_SET_CH_5 0x0140u
78 #define ADC24_CTRL_SET_CH_6 0x0180u
79 #define ADC24_CTRL_SET_CH_7 0x01C0u
80 #define ADC24_CTRL_SET_CH_8 0x0200u
81 #define ADC24_CTRL_SET_CH_9 0x0240u
82 #define ADC24_CTRL_SET_CH_10 0x0280u
83 #define ADC24_CTRL_SET_CH_11 0x02C0u
84 #define ADC24_CTRL_SET_CH_12 0x0300u
85 #define ADC24_CTRL_SET_CH_13 0x0340u
86 #define ADC24_CTRL_SET_CH_14 0x0380u
87 #define ADC24_CTRL_SET_CH_15 0x03C0u
88 #define ADC24_CTRL_SET_PM_NORMAL 0x0030u
89 #define ADC24_CTRL_SET_PM_FULL_SHDN 0x0020u
90 #define ADC24_CTRL_SET_PM_AUTO_SHDN 0x0010u
91 #define ADC24_CTRL_SET_PM_STANDBY 0x0000u
92 #define ADC24_CTRL_SET_SHADOW 0x0008u
93 #define ADC24_CTRL_SET_WEAK 0x0004u
94 #define ADC24_CTRL_SET_RANGE_5V 0x0000u
95 #define ADC24_CTRL_SET_RANGE_2_5V 0x0002u
96 #define ADC24_CTRL_SET_CODING_2C 0x0000u
97 #define ADC24_CTRL_SET_CODING_BIN 0x0001u
98 
103 #define ADC24_SEQ_SHADOW_AN_INPUT 0
104 #define ADC24_SEQ_SHADOW_PROGRAMMING 1
105 #define ADC24_SEQ_SHADOW_ALTERED 2
106 #define ADC24_SEQ_SHADOW_ADDR_CONJ 3
107 
112 #define ADC24_CH_SEL_IN_0 0
113 #define ADC24_CH_SEL_IN_1 1
114 #define ADC24_CH_SEL_IN_2 2
115 #define ADC24_CH_SEL_IN_3 3
116 #define ADC24_CH_SEL_IN_4 4
117 #define ADC24_CH_SEL_IN_5 5
118 #define ADC24_CH_SEL_IN_6 6
119 #define ADC24_CH_SEL_IN_7 7
120 #define ADC24_CH_SEL_IN_8 8
121 #define ADC24_CH_SEL_IN_9 9
122 #define ADC24_CH_SEL_IN_10 10
123 #define ADC24_CH_SEL_IN_11 11
124 #define ADC24_CH_SEL_IN_12 12
125 #define ADC24_CH_SEL_IN_13 13
126 #define ADC24_CH_SEL_IN_14 14
127 #define ADC24_CH_SEL_IN_15 15
128 
133 #define ADC24_PM_STANDBY 0
134 #define ADC24_PM_AUTO_SHDN 1
135 #define ADC24_PM_FULL_SHDN 2
136 #define ADC24_PM_NORMAL 3
137 
142 #define ADC24_WEAK_DOUT_THREE_STATE 0
143 #define ADC24_WEAK_DOUT_WEAKLY_DRIVEN 1
144 
149 #define ADC24_RANGE_VREF_5V 0
150 #define ADC24_RANGE_VREF_2V5 1
151 
156 #define ADC24_CODING_2C 0
157 #define ADC24_CODING_BIN 1
158 
163 #define ADC24_ADC_RESOLUTION 0x0FFF
164 #define ADC24_VREF_2V5 2.5f
165 #define ADC24_VREF_5V 5.0f
166 
175 #define ADC24_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
176 #define ADC24_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
177  // adc24_set
179 
194 #define ADC24_MAP_MIKROBUS( cfg, mikrobus ) \
195  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
196  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
197  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
198  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS )
199  // adc24_map // adc24
202 
207 typedef struct
208 {
209  // Modules
210  spi_master_t spi;
212  pin_name_t chip_select;
214 } adc24_t;
215 
220 typedef struct
221 {
222  // Communication gpio pins
223  pin_name_t miso;
224  pin_name_t mosi;
225  pin_name_t sck;
226  pin_name_t cs;
228  // static variable
229  uint32_t spi_speed;
230  spi_master_mode_t spi_mode;
231  spi_master_chip_select_polarity_t cs_polarity;
233 } adc24_cfg_t;
234 
239 typedef struct
240 {
241  uint8_t ch_sel;
242  uint8_t pm;
243  uint8_t seq_shadow;
244  uint8_t weak;
245  uint8_t range;
246  uint8_t coding;
248 } adc24_ctrl_t;
249 
254 typedef enum
255 {
256  ADC24_OK = 0,
257  ADC24_ERROR = -1
258 
260 
277 
291 err_t adc24_init ( adc24_t *ctx, adc24_cfg_t *cfg );
292 
304 err_t adc24_generic_write ( adc24_t *ctx, uint16_t control );
305 
317 err_t adc24_generic_read ( adc24_t *ctx, uint16_t *data_out );
318 
333 err_t adc24_get_adc_data ( adc24_t *ctx, adc24_ctrl_t ctrl, uint8_t *in_pos, uint16_t *adc_data );
334 
350 err_t adc24_get_voltage ( adc24_t *ctx, adc24_ctrl_t ctrl, uint8_t *in_pos, float *voltage );
351 
352 #ifdef __cplusplus
353 }
354 #endif
355 #endif // ADC24_H
356  // adc24
358 
359 // ------------------------------------------------------------------------ END
adc24_ctrl_t::ch_sel
uint8_t ch_sel
Definition: adc24.h:241
adc24_get_adc_data
err_t adc24_get_adc_data(adc24_t *ctx, adc24_ctrl_t ctrl, uint8_t *in_pos, uint16_t *adc_data)
ADC 24 get ADC data function.
adc24_generic_read
err_t adc24_generic_read(adc24_t *ctx, uint16_t *data_out)
ADC 24 data reading function.
adc24_init
err_t adc24_init(adc24_t *ctx, adc24_cfg_t *cfg)
ADC 24 initialization function.
adc24_cfg_t::spi_speed
uint32_t spi_speed
Definition: adc24.h:229
ADC24_ERROR
@ ADC24_ERROR
Definition: adc24.h:257
adc24_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: adc24.h:231
spi_specifics.h
This file contains SPI specific macros, functions, etc.
ADC24_OK
@ ADC24_OK
Definition: adc24.h:256
adc24_t
ADC 24 Click context object.
Definition: adc24.h:208
adc24_ctrl_t::seq_shadow
uint8_t seq_shadow
Definition: adc24.h:243
adc24_generic_write
err_t adc24_generic_write(adc24_t *ctx, uint16_t control)
ADC 24 data writing function.
adc24_ctrl_t::range
uint8_t range
Definition: adc24.h:245
adc24_cfg_t
ADC 24 Click configuration object.
Definition: adc24.h:221
adc24_t::chip_select
pin_name_t chip_select
Definition: adc24.h:212
adc24_cfg_setup
void adc24_cfg_setup(adc24_cfg_t *cfg)
ADC 24 configuration object setup function.
adc24_get_voltage
err_t adc24_get_voltage(adc24_t *ctx, adc24_ctrl_t ctrl, uint8_t *in_pos, float *voltage)
ADC 24 get voltage level function.
adc24_ctrl_t::pm
uint8_t pm
Definition: adc24.h:242
adc24_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: adc24.h:230
adc24_cfg_t::sck
pin_name_t sck
Definition: adc24.h:225
adc24_cfg_t::mosi
pin_name_t mosi
Definition: adc24.h:224
adc24_ctrl_t::weak
uint8_t weak
Definition: adc24.h:244
adc24_cfg_t::miso
pin_name_t miso
Definition: adc24.h:223
adc24_return_value_t
adc24_return_value_t
ADC 24 Click return value data.
Definition: adc24.h:255
adc24_t::spi
spi_master_t spi
Definition: adc24.h:210
adc24_cfg_t::cs
pin_name_t cs
Definition: adc24.h:226
adc24_ctrl_t::coding
uint8_t coding
Definition: adc24.h:246
adc24_ctrl_t
ADC 24 Click control object.
Definition: adc24.h:240