digipot  2.0.0.0
digipot.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 DIGIPOT_H
29 #define DIGIPOT_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_spi_master.h"
47 
68 #define DIGIPOT_MAP_MIKROBUS( cfg, mikrobus ) \
69  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
70  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
71  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
72  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS )
73  // digipot_map
75 
90 #define DIGIPOT_DATA_EEPROM_6 0x06
91 #define DIGIPOT_DATA_EEPROM_7 0x07
92 #define DIGIPOT_DATA_EEPROM_8 0x08
93 #define DIGIPOT_DATA_EEPROM_9 0x09
94 #define DIGIPOT_DATA_EEPROM_A 0x0A
95 #define DIGIPOT_DATA_EEPROM_B 0x0B
96 #define DIGIPOT_DATA_EEPROM_C 0x0C
97 #define DIGIPOT_DATA_EEPROM_D 0x0D
98 #define DIGIPOT_DATA_EEPROM_E 0x0E
99 #define DIGIPOT_DATA_EEPROM_F 0x0F
100 #define DIGIPOT_DATA_WRITE_CMD 0x20
101 
106 #define DIGIPOT_VOLATILE_WIPER_0 0x00
107 #define DIGIPOT_VOLATILE_WIPER_1 0x01
108 #define DIGIPOT_NON_VOLATILE_WIPER_0 0x02
109 #define DIGIPOT_NON_VOLATILE_WIPER_1 0x03
110 #define DIGIPOT_VOLATILE_TCON_REGISTER 0x04
111 #define DIGIPOT_STATUS_REGISTER 0x05
112  // digipot_macros // digipot
115 
120 typedef struct
121 {
122  // Output pins
123 
124  digital_out_t cs;
125 
126  // Modules
127  spi_master_t spi;
129  pin_name_t chip_select;
131 } digipot_t;
132 
137 typedef struct
138 {
139  // Communication gpio pins
140  pin_name_t miso;
141  pin_name_t mosi;
142  pin_name_t sck;
143  pin_name_t cs;
145  // static variable
146 
147  uint32_t spi_speed;
148  spi_master_mode_t spi_mode;
149  spi_master_chip_select_polarity_t cs_polarity;
151 } digipot_cfg_t;
152 
157 typedef enum
158 {
160  DIGIPOT_ERROR = -1
161 
163 
180 
195 err_t digipot_init ( digipot_t *ctx, digipot_cfg_t *cfg );
196 
209 void digipot_generic_transfer ( digipot_t *ctx, uint8_t *wr_buf, uint16_t wr_len, uint8_t *rd_buf, uint16_t rd_len );
210 
221 
231 float digipot_convert_output ( uint16_t adc_val, float v_ref );
232 
233 #ifdef __cplusplus
234 }
235 #endif
236 #endif // DIGIPOT_H
237  // digipot
239 
240 // ------------------------------------------------------------------------ END
digipot_set_wiper_positions
void digipot_set_wiper_positions(digipot_t *ctx, uint8_t wiper_pos)
Set wiper position.
digipot_t::spi
spi_master_t spi
Definition: digipot.h:127
digipot_cfg_t::cs
pin_name_t cs
Definition: digipot.h:143
digipot_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: digipot.h:149
digipot_t
DIGI POT Click context object.
Definition: digipot.h:121
digipot_generic_transfer
void digipot_generic_transfer(digipot_t *ctx, uint8_t *wr_buf, uint16_t wr_len, uint8_t *rd_buf, uint16_t rd_len)
Generic transfer function.
digipot_t::cs
digital_out_t cs
Definition: digipot.h:124
digipot_cfg_setup
void digipot_cfg_setup(digipot_cfg_t *cfg)
DIGI POT configuration object setup function.
wiper_pos
uint8_t wiper_pos
Definition: main.c:28
digipot_init
err_t digipot_init(digipot_t *ctx, digipot_cfg_t *cfg)
DIGI POT initialization function.
DIGIPOT_ERROR
@ DIGIPOT_ERROR
Definition: digipot.h:160
digipot_cfg_t::miso
pin_name_t miso
Definition: digipot.h:140
digipot_convert_output
float digipot_convert_output(uint16_t adc_val, float v_ref)
Convert ADC value to volatage.
digipot_cfg_t::mosi
pin_name_t mosi
Definition: digipot.h:141
DIGIPOT_OK
@ DIGIPOT_OK
Definition: digipot.h:159
digipot_return_value_t
digipot_return_value_t
DIGI POT Click return value data.
Definition: digipot.h:158
digipot_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: digipot.h:148
digipot_t::chip_select
pin_name_t chip_select
Definition: digipot.h:129
digipot_cfg_t::sck
pin_name_t sck
Definition: digipot.h:142
digipot_cfg_t
DIGI POT Click configuration object.
Definition: digipot.h:138
digipot_cfg_t::spi_speed
uint32_t spi_speed
Definition: digipot.h:147