digipot  2.0.0.0
digipot.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 DIGIPOT_H
36 #define DIGIPOT_H
37 
38 #include "drv_digital_out.h"
39 #include "drv_spi_master.h"
40 
41 // -------------------------------------------------------------- PUBLIC MACROS
52 #define DIGIPOT_MAP_MIKROBUS( cfg, mikrobus ) \
53  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
54  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
55  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
56  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS )
57 
63 #define DIGIPOT_RETVAL uint8_t
64 
65 #define DIGIPOT_OK 0x00
66 #define DIGIPOT_INIT_ERROR 0xFF
67 
73 #define DIGIPOT_VOLATILE_WIPER_0 0x00
74 #define DIGIPOT_VOLATILE_WIPER_1 0x01
75 #define DIGIPOT_NON_VOLATILE_WIPER_0 0x02
76 #define DIGIPOT_NON_VOLATILE_WIPER_1 0x03
77 #define DIGIPOT_VOLATILE_TCON_REGISTER 0x04
78 #define DIGIPOT_STATUS_REGISTER 0x05
79 
85 #define DIGIPOT_DATA_EEPROM_6 0x06
86 #define DIGIPOT_DATA_EEPROM_7 0x07
87 #define DIGIPOT_DATA_EEPROM_8 0x08
88 #define DIGIPOT_DATA_EEPROM_9 0x09
89 #define DIGIPOT_DATA_EEPROM_A 0x0A
90 #define DIGIPOT_DATA_EEPROM_B 0x0B
91 #define DIGIPOT_DATA_EEPROM_C 0x0C
92 #define DIGIPOT_DATA_EEPROM_D 0x0D
93 #define DIGIPOT_DATA_EEPROM_E 0x0E
94 #define DIGIPOT_DATA_EEPROM_F 0x0F
95 #define DIGIPOT_DATA_WRITE_CMD 0x20
96  // End group macro
99 // --------------------------------------------------------------- PUBLIC TYPES
108 typedef struct
109 {
110  // Output pins
111 
112  digital_out_t cs;
113 
114  // Modules
115 
116  spi_master_t spi;
117  pin_name_t chip_select;
118 
119 } digipot_t;
120 
124 typedef struct
125 {
126  // Communication gpio pins
127 
128  pin_name_t miso;
129  pin_name_t mosi;
130  pin_name_t sck;
131  pin_name_t cs;
132 
133  // static variable
134 
135  uint32_t spi_speed;
136  spi_master_mode_t spi_mode;
137  spi_master_chip_select_polarity_t cs_polarity;
138 
139 } digipot_cfg_t;
140  // End types group
142 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
143 
148 #ifdef __cplusplus
149 extern "C"{
150 #endif
151 
160 void digipot_cfg_setup ( digipot_cfg_t *cfg );
161 
171 
183 void digipot_generic_transfer ( digipot_t *ctx, uint8_t *wr_buf,
184  uint16_t wr_len, uint8_t *rd_buf, uint16_t rd_len );
185 
194 void digipot_set_wiper_positions ( digipot_t *ctx, uint8_t wiper_pos );
195 
207 float digipot_convert_output ( uint16_t adc_val, float v_ref );
208 
209 #ifdef __cplusplus
210 }
211 #endif
212 #endif // _DIGIPOT_H_
213  // End public_function group
216 
217 // ------------------------------------------------------------------------- END
digipot_t::spi
spi_master_t spi
Definition: digipot.h:116
digipot_cfg_t::cs
pin_name_t cs
Definition: digipot.h:131
digipot_set_wiper_positions
void digipot_set_wiper_positions(digipot_t *ctx, uint8_t wiper_pos)
Set wiper position.
digipot_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: digipot.h:137
digipot_t
Click ctx object definition.
Definition: digipot.h:108
digipot_init
DIGIPOT_RETVAL digipot_init(digipot_t *ctx, digipot_cfg_t *cfg)
Initialization function.
digipot_t::cs
digital_out_t cs
Definition: digipot.h:112
wiper_pos
uint8_t wiper_pos
Definition: main.c:31
digipot_convert_output
float digipot_convert_output(uint16_t adc_val, float v_ref)
Convert ADC value to volatage.
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_cfg_setup
void digipot_cfg_setup(digipot_cfg_t *cfg)
Config Object Initialization function.
digipot_cfg_t::miso
pin_name_t miso
Definition: digipot.h:128
digipot_cfg_t::mosi
pin_name_t mosi
Definition: digipot.h:129
DIGIPOT_RETVAL
#define DIGIPOT_RETVAL
Definition: digipot.h:63
digipot_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: digipot.h:136
digipot_t::chip_select
pin_name_t chip_select
Definition: digipot.h:117
digipot_cfg_t::sck
pin_name_t sck
Definition: digipot.h:130
digipot_cfg_t
Click configuration structure definition.
Definition: digipot.h:124
digipot_cfg_t::spi_speed
uint32_t spi_speed
Definition: digipot.h:135