dac18  2.1.0.0
dac18.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 DAC18_H
29 #define DAC18_H
30 
31 #ifdef __cplusplus
32 extern "C"{
33 #endif
34 
39 #ifdef PREINIT_SUPPORTED
40 #include "preinit.h"
41 #endif
42 
43 #ifdef MikroCCoreVersion
44  #if MikroCCoreVersion >= 1
45  #include "delays.h"
46  #endif
47 #endif
48 
49 #include "drv_digital_out.h"
50 #include "drv_digital_in.h"
51 #include "drv_i2c_master.h"
52 
73 #define DAC18_REG_NOP 0x00
74 #define DAC18_REG_DAC0_VOUT_CMP_CONFIG 0x15
75 #define DAC18_REG_DAC1_VOUT_CMP_CONFIG 0x03
76 #define DAC18_REG_COMMON_CONFIG 0x1F
77 #define DAC18_REG_COMMON_TRIGGER 0x20
78 #define DAC18_REG_FUNCTION_TRIGGER 0x21
79 #define DAC18_REG_GENERAL_STATUS 0x22
80 #define DAC18_REG_DEVICE_MODE_CONFIG 0x25
81 #define DAC18_REG_INTERFACE_CONFIG 0x26
82 #define DAC18_REG_STATE_MACHINE_CONFIG 0x27
83 #define DAC18_REG_SRAM_CONFIG 0x2B
84 #define DAC18_REG_SRAM_DATA 0x2C
85 
90 #define DAC18_SRAM_REG_FUNCTION_CONFIG 0x20
91 #define DAC18_SRAM_REG_FUNCTION_MAX 0x21
92 #define DAC18_SRAM_REG_FUNCTION_MIN 0x22
93 #define DAC18_SRAM_REG_GPI_DEBOUNCE 0x23
94 #define DAC18_SRAM_REG_VOUT_DATA_0 0x24
95 #define DAC18_SRAM_REG_VOUT_DATA_1 0x25
96 #define DAC18_SRAM_REG_VOUT_DATA_2 0x26
97 #define DAC18_SRAM_REG_VOUT_DATA_3 0x27
98 #define DAC18_SRAM_REG_VOUT_DATA_4 0x28
99 #define DAC18_SRAM_REG_VOUT_DATA_5 0x29
100 #define DAC18_SRAM_REG_VOUT_DATA_6 0x2A
101 #define DAC18_SRAM_REG_VOUT_DATA_7 0x2B
102 #define DAC18_SRAM_REG_PWM_FREQUENCY_ERROR 0x9E
103  // dac18_reg
105 
120 #define DAC18_MODE_CONFIGURATION 0
121 #define DAC18_MODE_STANDALONE 1
122 
127 #define DAC18_DACX_VOUT_CFG_GAIN_BITMASK 0x1C00u
128 #define DAC18_DACX_VOUT_CFG_GAIN_X1 0x0400u
129 #define DAC18_DACX_VOUT_CFG_GAIN_X1_5 0x0800u
130 #define DAC18_DACX_VOUT_CFG_GAIN_X2 0x0C00u
131 #define DAC18_DACX_VOUT_CFG_GAIN_X3 0x1000u
132 #define DAC18_DACX_VOUT_CFG_GAIN_X4 0x1400u
133 #define DAC18_DACX_VOUT_CFG_CMPX_OD_EN 0x0010u
134 #define DAC18_DACX_VOUT_CFG_CMPX_OUT_EN 0x0008u
135 #define DAC18_DACX_VOUT_CFG_CMPX_HIZ_IN_DIS 0x0004u
136 #define DAC18_DACX_VOUT_CFG_CMPX_INV_EN 0x0002u
137 #define DAC18_DACX_VOUT_CFG_CMPX_EN 0x0001u
138 
143 #define DAC18_COMMON_CFG_DEFAULT 0x03F9u
144 #define DAC18_COMMON_CFG_DEV_LOCK 0x4000u
145 #define DAC18_COMMON_CFG_EN_INT_REF 0x1000u
146 #define DAC18_COMMON_CFG_VOUT0_BITMASK 0x0C00u
147 #define DAC18_COMMON_CFG_VOUT0_PDN 0x0000u
148 #define DAC18_COMMON_CFG_VOUT0_10KOHM 0x0400u
149 #define DAC18_COMMON_CFG_VOUT0_100KOHM 0x0800u
150 #define DAC18_COMMON_CFG_VOUT0_HIZ 0x0C00u
151 #define DAC18_COMMON_CFG_VOUT1_BITMASK 0x0060u
152 #define DAC18_COMMON_CFG_VOUT1_PDN 0x0000u
153 #define DAC18_COMMON_CFG_VOUT1_10KOHM 0x0040u
154 #define DAC18_COMMON_CFG_VOUT1_100KOHM 0x0020u
155 #define DAC18_COMMON_CFG_VOUT1_HIZ 0x0060u
156 
161 #define DAC18_COMMON_TRG_DEV_LOCK 0x5000u
162 #define DAC18_COMMON_TRG_RESET 0x0A00u
163 #define DAC18_COMMON_TRG_NVM_PROG 0x0002u
164 #define DAC18_COMMON_TRG_NVM_RELOAD 0x0001u
165 
170 #define DAC18_FUNCTION_TRG_START 0x0001u
171 #define DAC18_FUNCTION_TRG_STOP 0x0000u
172 
177 #define DAC18_GEN_STATUS_NVM_CRC_FAIL_INT 0x8000u
178 #define DAC18_GEN_STATUS_NVM_CRC_FAIL_USER 0x4000u
179 #define DAC18_GEN_STATUS_DAC_0_BUSY 0x1000u
180 #define DAC18_GEN_STATUS_DAC_1_BUSY 0x0200u
181 #define DAC18_GEN_STATUS_NVM_BUSY 0x0100u
182 #define DAC18_GEN_STATUS_DEVICE_ID_BITMASK 0x00FCu
183 #define DAC18_GEN_STATUS_DEVICE_ID 0x001Au
184 #define DAC18_GEN_STATUS_VERSION_ID_BITMASK 0x0003u
185 #define DAC18_GEN_STATUS_VERSION_ID 0x0000u
186 
191 #define DAC18_DEV_MODE_CFG_DEFAULT 0x8000u
192 #define DAC18_DEV_MODE_CFG_EN 0x0000u
193 #define DAC18_DEV_MODE_CFG_DIS 0x2000u
194 #define DAC18_DEV_MODE_CFG_SM_IO_EN 0x0040u
195 
200 #define DAC18_STATE_M_CFG_SM_ABORT 0x0004u
201 #define DAC18_STATE_M_CFG_SM_START 0x0002u
202 #define DAC18_STATE_M_CFG_SM_EN 0x0001u
203 #define DAC18_STATE_M_CFG_SM_STOP 0x0000u
204 
209 #define DAC18_FUN_CFG_F_SEL_TRIANGULAR 0x00
210 #define DAC18_FUN_CFG_F_SEL_SAWTOOTH 0x01
211 #define DAC18_FUN_CFG_F_SEL_INV_SAWTOOTH 0x02
212 #define DAC18_FUN_CFG_F_SEL_DISABLE 0x07
213 #define DAC18_FUN_CFG_C_STEP_1LSB 0x00
214 #define DAC18_FUN_CFG_C_STEP_2LSB 0x01
215 #define DAC18_FUN_CFG_C_STEP_3LSB 0x02
216 #define DAC18_FUN_CFG_C_STEP_4LSB 0x03
217 #define DAC18_FUN_CFG_C_STEP_6LSB 0x04
218 #define DAC18_FUN_CFG_C_STEP_8LSB 0x05
219 #define DAC18_FUN_CFG_C_STEP_16LSB 0x06
220 #define DAC18_FUN_CFG_C_STEP_32LSB 0x07
221 #define DAC18_FUN_CFG_T_STEP_INVALID 0x00
222 #define DAC18_FUN_CFG_T_STEP_4_US 0x01
223 #define DAC18_FUN_CFG_T_STEP_8_US 0x02
224 #define DAC18_FUN_CFG_T_STEP_12_US 0x03
225 #define DAC18_FUN_CFG_T_STEP_18_US 0x04
226 #define DAC18_FUN_CFG_T_STEP_27_04_US 0x05
227 #define DAC18_FUN_CFG_T_STEP_40_48_US 0x06
228 #define DAC18_FUN_CFG_T_STEP_60_72_US 0x07
229 #define DAC18_FUN_CFG_T_STEP_91_12_US 0x08
230 #define DAC18_FUN_CFG_T_STEP_136_72_US 0x09
231 #define DAC18_FUN_CFG_T_STEP_239_2_US 0x0A
232 #define DAC18_FUN_CFG_T_STEP_418_64_US 0x0B
233 #define DAC18_FUN_CFG_T_STEP_732_56_US 0x0C
234 #define DAC18_FUN_CFG_T_STEP_1282_US 0x0D
235 #define DAC18_FUN_CFG_T_STEP_2563_92_US 0x0E
236 #define DAC18_FUN_CFG_T_STEP_5127_92_US 0x0F
237 
242 #define DAC18_DAC_CODE_MAX_DEFAULT 0x05AAu
243 #define DAC18_DAC_CODE_MIN_DEFAULT 0x00C8u
244 #define DAC18_DAC_CODE_12BIT_MAX 0x0FFFu
245 
250 #define DAC18_GPI_DEBOUNCE_50_MS 50.0f
251 #define DAC18_GPI_DEBOUNCE_MAX 10485.7f
252 #define DAC18_GPI_DEBOUNCE_SENS 0.16f
253 
258 #define DAC18_LUT_DEFAULT_VOUT_DATA_0 0xA1C0u
259 #define DAC18_LUT_DEFAULT_VOUT_DATA_1 0x8E40u
260 #define DAC18_LUT_DEFAULT_VOUT_DATA_2 0x7AC0u
261 #define DAC18_LUT_DEFAULT_VOUT_DATA_3 0x6740u
262 #define DAC18_LUT_DEFAULT_VOUT_DATA_4 0x5380u
263 #define DAC18_LUT_DEFAULT_VOUT_DATA_5 0x4000u
264 #define DAC18_LUT_DEFAULT_VOUT_DATA_6 0x2C80u
265 #define DAC18_LUT_DEFAULT_VOUT_DATA_7 0x18C0u
266 
272 #define DAC18_DEVICE_ADDRESS_GND 0x48
273 #define DAC18_DEVICE_ADDRESS_VCC 0x49
274  // dac18_set
276 
291 #define DAC18_MAP_MIKROBUS( cfg, mikrobus ) \
292  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
293  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
294  cfg.gp2 = MIKROBUS( mikrobus, MIKROBUS_AN ); \
295  cfg.mod = MIKROBUS( mikrobus, MIKROBUS_RST ); \
296  cfg.gp1 = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
297  cfg.gp0 = MIKROBUS( mikrobus, MIKROBUS_INT )
298  // dac18_map // dac18
301 
306 typedef struct
307 {
308  // Output pins
309  digital_out_t mod;
311  // Input pins
312  digital_in_t gp2;
313  digital_in_t gp1;
314  digital_in_t gp0;
316  // Modules
317  i2c_master_t i2c;
319  // I2C slave address
320  uint8_t slave_address;
322 } dac18_t;
323 
328 typedef struct
329 {
330  pin_name_t scl;
331  pin_name_t sda;
333  pin_name_t gp2;
334  pin_name_t mod;
335  pin_name_t gp1;
336  pin_name_t gp0;
338  uint32_t i2c_speed;
339  uint8_t i2c_address;
341 } dac18_cfg_t;
342 
347 typedef enum
348 {
349  DAC18_OK = 0,
350  DAC18_ERROR = -1
351 
353 
370 
384 err_t dac18_init ( dac18_t *ctx, dac18_cfg_t *cfg );
385 
398 err_t dac18_default_cfg ( dac18_t *ctx );
399 
414 err_t dac18_generic_write ( dac18_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
415 
430 err_t dac18_generic_read ( dac18_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
431 
445 err_t dac18_register_write ( dac18_t *ctx, uint8_t reg, uint16_t data_in );
446 
460 err_t dac18_register_read ( dac18_t *ctx, uint8_t reg, uint16_t *data_out );
461 
476 err_t dac18_cfg_pwm_out ( dac18_t *ctx, uint8_t f_sel, uint8_t c_step, uint8_t t_step );
477 
492 err_t dac18_set_dac_code ( dac18_t *ctx, uint16_t max_dc, uint16_t min_dc );
493 
506 err_t dac18_set_debounce_delay ( dac18_t *ctx, float delay_ms );
507 
522 void dac18_set_mode ( dac18_t *ctx, uint8_t mode );
523 
533 uint8_t dac18_get_gp0 ( dac18_t *ctx );
534 
544 uint8_t dac18_get_gp1 ( dac18_t *ctx );
545 
555 uint8_t dac18_get_gp2 ( dac18_t *ctx );
556 
567 void dac18_get_gpi_status ( dac18_t *ctx, uint8_t *gpi_status );
568 
569 #ifdef __cplusplus
570 }
571 #endif
572 #endif // DAC18_H
573  // dac18
575 
576 // ------------------------------------------------------------------------ END
dac18_cfg_t
DAC 18 Click configuration object.
Definition: dac18.h:329
DAC18_ERROR
@ DAC18_ERROR
Definition: dac18.h:350
dac18_set_dac_code
err_t dac18_set_dac_code(dac18_t *ctx, uint16_t max_dc, uint16_t min_dc)
DAC 18 set DAC code function.
dac18_cfg_t::i2c_address
uint8_t i2c_address
Definition: dac18.h:339
dac18_cfg_t::gp0
pin_name_t gp0
Definition: dac18.h:336
dac18_t::gp0
digital_in_t gp0
Definition: dac18.h:314
dac18_return_value_t
dac18_return_value_t
DAC 18 Click return value data.
Definition: dac18.h:348
dac18_cfg_t::gp2
pin_name_t gp2
Definition: dac18.h:333
dac18_t::mod
digital_out_t mod
Definition: dac18.h:309
dac18_t::i2c
i2c_master_t i2c
Definition: dac18.h:317
dac18_generic_write
err_t dac18_generic_write(dac18_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
DAC 18 I2C writing function.
dac18_get_gp0
uint8_t dac18_get_gp0(dac18_t *ctx)
DAC 18 get GP0 pin state function.
dac18_init
err_t dac18_init(dac18_t *ctx, dac18_cfg_t *cfg)
DAC 18 initialization function.
dac18_cfg_t::gp1
pin_name_t gp1
Definition: dac18.h:335
DAC18_OK
@ DAC18_OK
Definition: dac18.h:349
dac18_register_write
err_t dac18_register_write(dac18_t *ctx, uint8_t reg, uint16_t data_in)
DAC 18 register writing function.
dac18_get_gp2
uint8_t dac18_get_gp2(dac18_t *ctx)
DAC 18 get GP2 pin state function.
dac18_t::gp2
digital_in_t gp2
Definition: dac18.h:312
dac18_t::slave_address
uint8_t slave_address
Definition: dac18.h:320
dac18_generic_read
err_t dac18_generic_read(dac18_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
DAC 18 I2C reading function.
dac18_register_read
err_t dac18_register_read(dac18_t *ctx, uint8_t reg, uint16_t *data_out)
DAC 18 register reading function.
dac18_cfg_t::sda
pin_name_t sda
Definition: dac18.h:331
dac18_get_gp1
uint8_t dac18_get_gp1(dac18_t *ctx)
DAC 18 get GP1 pin state function.
dac18_set_mode
void dac18_set_mode(dac18_t *ctx, uint8_t mode)
DAC 18 set operation mode function.
dac18_cfg_setup
void dac18_cfg_setup(dac18_cfg_t *cfg)
DAC 18 configuration object setup function.
dac18_cfg_t::scl
pin_name_t scl
Definition: dac18.h:330
dac18_cfg_t::mod
pin_name_t mod
Definition: dac18.h:334
dac18_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: dac18.h:338
dac18_get_gpi_status
void dac18_get_gpi_status(dac18_t *ctx, uint8_t *gpi_status)
DAC 18 get GPI status function.
dac18_t::gp1
digital_in_t gp1
Definition: dac18.h:313
dac18_t
DAC 18 Click context object.
Definition: dac18.h:307
dac18_default_cfg
err_t dac18_default_cfg(dac18_t *ctx)
DAC 18 default configuration function.
dac18_set_debounce_delay
err_t dac18_set_debounce_delay(dac18_t *ctx, float delay_ms)
DAC 18 set debounce delay function.
dac18_cfg_pwm_out
err_t dac18_cfg_pwm_out(dac18_t *ctx, uint8_t f_sel, uint8_t c_step, uint8_t t_step)
DAC 18 PWM OUT0 configuration function.