adc7  2.0.0.0
adc7.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  */
32 // ----------------------------------------------------------------------------
33 
34 #ifndef ADC7_H
35 #define ADC7_H
36 
37 #include "mikrosdk_version.h"
38 
39 #ifdef __GNUC__
40 #if mikroSDK_GET_VERSION < 20800ul
41 #include "rcu_delays.h"
42 #else
43 #include "delays.h"
44 #endif
45 #endif
46 
47 #include "drv_digital_out.h"
48 #include "drv_digital_in.h"
49 #include "drv_spi_master.h"
50 
51 // -------------------------------------------------------------- PUBLIC MACROS
62 #define ADC7_MAP_MIKROBUS( cfg, mikrobus ) \
63  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
64  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
65  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
66  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
67  cfg.drl = MIKROBUS( mikrobus, MIKROBUS_AN ); \
68  cfg.pre = MIKROBUS( mikrobus, MIKROBUS_RST ); \
69  cfg.mck = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
70  cfg.bsy = MIKROBUS( mikrobus, MIKROBUS_INT )
71 
77 #define ADC7_RETVAL uint8_t
78 
79 #define ADC7_OK 0x00
80 #define ADC7_INIT_ERROR 0xFF
81 
87 #define ADC7_SINC1_FILT 0x01
88 #define ADC7_SINC2_FILT 0x02
89 #define ADC7_SINC3_FILT 0x03
90 #define ADC7_SINC4_FILT 0x04
91 #define ADC7_SSINC_FILT 0x05
92 #define ADC7_FLAT_PASSBAND_FILT 0x06
93 #define ADC7_AVERAGING_FILT 0x07
94 
100 #define ADC7_DOWNSAMPL_FACT_4 0x02
101 #define ADC7_DOWNSAMPL_FACT_8 0x03
102 #define ADC7_DOWNSAMPL_FACT_16 0x04
103 #define ADC7_DOWNSAMPL_FACT_32 0x05
104 #define ADC7_DOWNSAMPL_FACT_64 0x06
105 #define ADC7_DOWNSAMPL_FACT_128 0x07
106 #define ADC7_DOWNSAMPL_FACT_256 0x08
107 #define ADC7_DOWNSAMPL_FACT_512 0x09
108 #define ADC7_DOWNSAMPL_FACT_1024 0x0A
109 #define ADC7_DOWNSAMPL_FACT_2048 0x0B
110 #define ADC7_DOWNSAMPL_FACT_4096 0x0C
111 #define ADC7_DOWNSAMPL_FACT_8192 0x0D
112 #define ADC7_DOWNSAMPL_FACT_16384 0x0E
113 
119 #define ADC7_GAIN_EXPAN_EN 0x01
120 #define ADC7_GAIN_COMPR_EN 0x02
121 #define ADC7_GAIN_DISABLE 0x00
122 
128 #define ADC7_DATA_NOT_READY 0x01
129 #define ADC7_DATA_IS_READY 0x00
130 #define ADC7_DEVICE_IS_BUSY 0x01
131 #define ADC7_DEVICE_NOT_BUSY 0x00
132 #define ADC7_WRONG_GAIN_CONFIG 0x02
133 #define ADC7_WRONG_DOWNSAMPL_FACT 0x03
134 #define ADC7_WRONG_FILT_TYPE 0x04
135 
141 #define ADC7_HIGH_STATE 0x01
142 #define ADC7_LOW_STATE 0x00
143  // End group macro
146 // --------------------------------------------------------------- PUBLIC TYPES
155 typedef struct
156 {
157  digital_out_t cs;
158 
159  // Output pins
160 
161  digital_out_t pre;
162  digital_out_t mck;
163 
164  // Input pins
165 
166  digital_in_t drl;
167  digital_in_t bsy;
168 
169  // Modules
170 
171  spi_master_t spi;
172  pin_name_t chip_select;
173 
174  uint16_t num_sampl;
175  float volt_ref;
176  float value_lsb;
177 
178 
179 } adc7_t;
180 
184 typedef struct
185 {
186  // Communication gpio pins
187 
188  pin_name_t miso;
189  pin_name_t mosi;
190  pin_name_t sck;
191  pin_name_t cs;
192 
193  // Additional gpio pins
194 
195  pin_name_t drl;
196  pin_name_t pre;
197  pin_name_t mck;
198  pin_name_t bsy;
199 
200  // static variable
201 
202  uint32_t spi_speed;
203  spi_master_mode_t spi_mode;
204  spi_master_chip_select_polarity_t cs_polarity;
205 
206  uint16_t dev_num_sampl;
209 
210 } adc7_cfg_t;
211  // End types group
213 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
214 
219 #ifdef __cplusplus
220 extern "C"{
221 #endif
222 
232 
240 void adc7_default_cfg ( adc7_t *ctx );
241 
251 
265 uint8_t adc7_set_config ( adc7_t* ctx, uint8_t gain_config,
266  uint8_t down_samp_factor, uint8_t filter_type );
267 
277 uint8_t adc7_check_data_ready ( adc7_t* ctx );
278 
288 uint8_t adc7_check_busy ( adc7_t* ctx );
289 
299 void adc7_set_clock ( adc7_t* ctx, uint8_t state );
300 
310 void adc7_preset_mode ( adc7_t* ctx, uint8_t state );
311 
324 uint8_t adc7_read_bytes ( adc7_t* ctx, uint8_t n_bytes, uint8_t* data_out );
325 
337 
349 uint8_t adc7_read_results ( adc7_t* ctx, float* voltage );
350 
351 
352 #ifdef __cplusplus
353 }
354 #endif
355 #endif // _ADC7_H_
356  // End public_function group
359 
360 // ------------------------------------------------------------------------- END
adc7_cfg_t::pre
pin_name_t pre
Definition: adc7.h:196
adc7_cfg_setup
void adc7_cfg_setup(adc7_cfg_t *cfg)
Config Object Initialization function.
adc7_cfg_t::dev_value_lsb
float dev_value_lsb
Definition: adc7.h:208
adc7_cfg_t::sck
pin_name_t sck
Definition: adc7.h:190
adc7_cfg_t::mosi
pin_name_t mosi
Definition: adc7.h:189
adc7_read_bytes
uint8_t adc7_read_bytes(adc7_t *ctx, uint8_t n_bytes, uint8_t *data_out)
Read Bytes Function.
adc7_t::pre
digital_out_t pre
Definition: adc7.h:161
adc7_cfg_t::spi_speed
uint32_t spi_speed
Definition: adc7.h:202
adc7_set_clock
void adc7_set_clock(adc7_t *ctx, uint8_t state)
Set Clock.
adc7_cfg_t::dev_volt_ref
float dev_volt_ref
Definition: adc7.h:207
adc7_read_results
uint8_t adc7_read_results(adc7_t *ctx, float *voltage)
Start Conversion Function.
adc7_cfg_t::miso
pin_name_t miso
Definition: adc7.h:188
adc7_default_cfg
void adc7_default_cfg(adc7_t *ctx)
Default Configuration Initialization function.
adc7_check_busy
uint8_t adc7_check_busy(adc7_t *ctx)
Busy Check Function.
adc7_t
Click ctx object definition.
Definition: adc7.h:156
adc7_t::drl
digital_in_t drl
Definition: adc7.h:166
adc7_preset_mode
void adc7_preset_mode(adc7_t *ctx, uint8_t state)
Preset Mode.
adc7_cfg_t::bsy
pin_name_t bsy
Definition: adc7.h:198
ADC7_RETVAL
#define ADC7_RETVAL
Definition: adc7.h:77
adc7_start_conv_cycle
void adc7_start_conv_cycle(adc7_t *ctx)
Start Conversion Function.
adc7_cfg_t::mck
pin_name_t mck
Definition: adc7.h:197
adc7_cfg_t::drl
pin_name_t drl
Definition: adc7.h:195
adc7_t::volt_ref
float volt_ref
Definition: adc7.h:175
adc7_t::value_lsb
float value_lsb
Definition: adc7.h:176
adc7_t::chip_select
pin_name_t chip_select
Definition: adc7.h:172
adc7_t::spi
spi_master_t spi
Definition: adc7.h:171
adc7_cfg_t::dev_num_sampl
uint16_t dev_num_sampl
Definition: adc7.h:206
adc7_set_config
uint8_t adc7_set_config(adc7_t *ctx, uint8_t gain_config, uint8_t down_samp_factor, uint8_t filter_type)
Configuration set function.
adc7_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: adc7.h:203
adc7_t::cs
digital_out_t cs
Definition: adc7.h:157
adc7_t::mck
digital_out_t mck
Definition: adc7.h:162
adc7_cfg_t
Click configuration structure definition.
Definition: adc7.h:185
adc7_t::num_sampl
uint16_t num_sampl
Definition: adc7.h:174
adc7_init
ADC7_RETVAL adc7_init(adc7_t *ctx, adc7_cfg_t *cfg)
Initialization function.
adc7_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: adc7.h:204
adc7_t::bsy
digital_in_t bsy
Definition: adc7.h:167
adc7_cfg_t::cs
pin_name_t cs
Definition: adc7.h:191
adc7_check_data_ready
uint8_t adc7_check_data_ready(adc7_t *ctx)
Data Ready Check Function.