adc  2.0.0.0
adc.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 _ADC_H_
36 #define _ADC_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_spi_master.h"
50 
51 // -------------------------------------------------------------- PUBLIC MACROS
61 #define ADC_MAP_MIKROBUS( cfg, mikrobus ) \
62  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
63  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
64  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
65  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS )
66  // End macros group
69 // --------------------------------------------------------------- PUBLIC TYPES
78 #define ADC_RETVAL uint8_t
79 
80 #define ADC_OK 0x00
81 #define ADC_INIT_ERROR 0xFF
82 
86 #define ADC_VREF_3300MV 3300
87 #define ADC_VREF_4096MV 4096
88 #define ADC_VREF_5000MV 5000
89 
93 #define ADC_CH0_OR_CH01 0x00
94 #define ADC_CH1_OR_CH10 0x01
95 #define ADC_CH2_OR_CH23 0x02
96 #define ADC_CH3_OR_CH32 0x03
97 
101 typedef struct
102 {
103  digital_out_t cs;
104 
105  // SPI Module.
106  spi_master_t spi;
107  pin_name_t chip_select;
108 
109  // Single-Ended Channels Data.
110  uint16_t ch0;
111  uint16_t ch1;
112  uint16_t ch2;
113  uint16_t ch3;
114 
115  // Differential Channels Data.
116  uint16_t ch01;
117  uint16_t ch10;
118  uint16_t ch23;
119  uint16_t ch32;
120 
121  // Reference Voltage.
122  uint16_t vref;
123 
124 } adc_t;
125 
129 typedef struct
130 {
131  // Communication GPIO Pins.
132  pin_name_t miso;
133  pin_name_t mosi;
134  pin_name_t sck;
135  pin_name_t cs;
136 
137  // SPI Configuration Variables.
138  uint32_t spi_speed;
139  spi_master_mode_t spi_mode;
140  spi_master_chip_select_polarity_t cs_polarity;
141 
142  // Reference Voltage Selection.
143  uint16_t vref;
144 
145 } adc_cfg_t;
146  // End types group
148 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
154 #ifdef __cplusplus
155 extern "C"{
156 #endif
157 
169 
183 
197 uint16_t adc_get_single_ended_ch( adc_t *ctx, uint8_t channel );
198 
212 uint16_t adc_get_differential_ch( adc_t *ctx, uint8_t channel );
213 
214 #ifdef __cplusplus
215 }
216 #endif
217 #endif // _ADC_H_
218  // End public_function group
221 
222 // ------------------------------------------------------------------------ END
adc_get_differential_ch
uint16_t adc_get_differential_ch(adc_t *ctx, uint8_t channel)
Get Pseudo-Differential Pair function.
adc_t::ch1
uint16_t ch1
Definition: adc.h:111
adc_get_single_ended_ch
uint16_t adc_get_single_ended_ch(adc_t *ctx, uint8_t channel)
Get Single-Ended Channel function.
adc_t::ch0
uint16_t ch0
Definition: adc.h:110
adc_t::ch23
uint16_t ch23
Definition: adc.h:118
adc_cfg_t::cs
pin_name_t cs
Definition: adc.h:135
adc_t
Click context object definition.
Definition: adc.h:102
adc_t::vref
uint16_t vref
Definition: adc.h:122
adc_cfg_t::sck
pin_name_t sck
Definition: adc.h:134
adc_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: adc.h:140
adc_cfg_t::mosi
pin_name_t mosi
Definition: adc.h:133
adc_t::ch3
uint16_t ch3
Definition: adc.h:113
adc_cfg_t::vref
uint16_t vref
Definition: adc.h:143
adc_cfg_t::miso
pin_name_t miso
Definition: adc.h:132
adc_t::cs
digital_out_t cs
Definition: adc.h:103
adc_cfg_t
Click configuration structure definition.
Definition: adc.h:130
adc_init
ADC_RETVAL adc_init(adc_t *ctx, adc_cfg_t *cfg)
Click Initialization function.
adc_t::ch2
uint16_t ch2
Definition: adc.h:112
adc_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: adc.h:139
adc_t::chip_select
pin_name_t chip_select
Definition: adc.h:107
adc_cfg_setup
void adc_cfg_setup(adc_cfg_t *cfg)
Configuration Object Setup function.
adc_t::spi
spi_master_t spi
Definition: adc.h:106
adc_t::ch01
uint16_t ch01
Definition: adc.h:116
ADC_RETVAL
#define ADC_RETVAL
Click error code definition.
Definition: adc.h:78
adc_t::ch32
uint16_t ch32
Definition: adc.h:119
adc_t::ch10
uint16_t ch10
Definition: adc.h:117
adc_cfg_t::spi_speed
uint32_t spi_speed
Definition: adc.h:138