dac12  2.0.0.0
dac12.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 DAC12_H
29 #define DAC12_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_digital_in.h"
47 #include "drv_spi_master.h"
48 #include "spi_specifics.h"
49 
70 #define DAC12_REG_NOP 0x00
71 #define DAC12_REG_DEVICE_ID 0x01
72 #define DAC12_REG_SYNC 0x02
73 #define DAC12_REG_CONFIG 0x03
74 #define DAC12_REG_GAIN 0x04
75 #define DAC12_REG_TRIGGER 0x05
76 #define DAC12_REG_BROADCAST 0x06
77 #define DAC12_REG_STATUS 0x07
78 #define DAC12_REG_DAC0 0x08
79 #define DAC12_REG_DAC1 0x09
80 #define DAC12_REG_DAC2 0x0A
81 #define DAC12_REG_DAC3 0x0B
82 #define DAC12_REG_DAC4 0x0C
83 #define DAC12_REG_DAC5 0x0D
84 #define DAC12_REG_DAC6 0x0E
85 #define DAC12_REG_DAC7 0x0F
86  // dac12_reg
88 
103 #define DAC12_SELECT_CHANNEL_NONE 0x00
104 #define DAC12_SELECT_CHANNEL_0 0x01
105 #define DAC12_SELECT_CHANNEL_1 0x02
106 #define DAC12_SELECT_CHANNEL_2 0x04
107 #define DAC12_SELECT_CHANNEL_3 0x08
108 #define DAC12_SELECT_CHANNEL_4 0x10
109 #define DAC12_SELECT_CHANNEL_5 0x20
110 #define DAC12_SELECT_CHANNEL_6 0x40
111 #define DAC12_SELECT_CHANNEL_7 0x80
112 #define DAC12_SELECT_CHANNEL_ALL 0xFF
113 
118 #define DAC12_GAIN_REFDIV_EN 0x0100
119 
124 #define DAC12_MIN_DAC_VALUE 0x0000
125 #define DAC12_MAX_DAC_VALUE 0x0FFF
126 
131 #define DAC12_INTERNAL_VREF 2.5
132 
137 #define DAC12_SOFT_RESET 0x000A
138 
143 #define DAC12_DEVICE_ID 0x2894
144 #define DAC12_DEVICE_ID_MASK 0xFFFC
145 
150 #define DAC12_SPI_READ 0x80
151 
160 #define DAC12_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
161 #define DAC12_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
162  // dac12_set
164 
179 #define DAC12_MAP_MIKROBUS( cfg, mikrobus ) \
180  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
181  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
182  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
183  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS );
184  // dac12_map // dac12
187 
192 typedef struct
193 {
194  // Modules
195  spi_master_t spi;
197  pin_name_t chip_select;
199  pin_name_t channel_gain;
201 } dac12_t;
202 
207 typedef struct
208 {
209  // Communication gpio pins
210  pin_name_t miso;
211  pin_name_t mosi;
212  pin_name_t sck;
213  pin_name_t cs;
215  // static variable
216  uint32_t spi_speed;
217  spi_master_mode_t spi_mode;
218  spi_master_chip_select_polarity_t cs_polarity;
220 } dac12_cfg_t;
221 
226 typedef enum
227 {
228  DAC12_OK = 0,
229  DAC12_ERROR = -1
230 
232 
249 
264 err_t dac12_init ( dac12_t *ctx, dac12_cfg_t *cfg );
265 
279 err_t dac12_default_cfg ( dac12_t *ctx );
280 
294 err_t dac12_write_register ( dac12_t *ctx, uint8_t reg, uint16_t data_in );
295 
309 err_t dac12_read_register ( dac12_t *ctx, uint8_t reg, uint16_t *data_out );
310 
323 
335 err_t dac12_soft_reset ( dac12_t *ctx );
336 
350 err_t dac12_set_gain ( dac12_t *ctx, uint8_t channel );
351 
365 err_t dac12_set_channel_value ( dac12_t *ctx, uint8_t channel, uint16_t dac_value );
366 
380 err_t dac12_set_channel_voltage ( dac12_t *ctx, uint8_t channel, float voltage );
381 
382 #ifdef __cplusplus
383 }
384 #endif
385 #endif // DAC12_H
386  // dac12
388 
389 // ------------------------------------------------------------------------ END
dac12_t::spi
spi_master_t spi
Definition: dac12.h:195
dac12_cfg_t
DAC 12 Click configuration object.
Definition: dac12.h:208
dac12_set_channel_value
err_t dac12_set_channel_value(dac12_t *ctx, uint8_t channel, uint16_t dac_value)
DAC 12 set channel value function.
dac12_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: dac12.h:218
dac12_set_gain
err_t dac12_set_gain(dac12_t *ctx, uint8_t channel)
DAC 12 set gain function.
dac12_write_register
err_t dac12_write_register(dac12_t *ctx, uint8_t reg, uint16_t data_in)
DAC 12 write register function.
spi_specifics.h
This file contains SPI specific macros, functions, etc.
dac12_t::channel_gain
pin_name_t channel_gain
Definition: dac12.h:199
dac12_cfg_t::mosi
pin_name_t mosi
Definition: dac12.h:211
dac12_read_register
err_t dac12_read_register(dac12_t *ctx, uint8_t reg, uint16_t *data_out)
DAC 12 read register function.
dac12_cfg_t::spi_speed
uint32_t spi_speed
Definition: dac12.h:216
dac12_set_channel_voltage
err_t dac12_set_channel_voltage(dac12_t *ctx, uint8_t channel, float voltage)
DAC 12 set channel voltage function.
dac12_cfg_t::sck
pin_name_t sck
Definition: dac12.h:212
dac12_default_cfg
err_t dac12_default_cfg(dac12_t *ctx)
DAC 12 default configuration function.
DAC12_OK
@ DAC12_OK
Definition: dac12.h:228
dac12_check_communication
err_t dac12_check_communication(dac12_t *ctx)
DAC 12 check communication function.
dac12_cfg_t::miso
pin_name_t miso
Definition: dac12.h:210
dac12_return_value_t
dac12_return_value_t
DAC 12 Click return value data.
Definition: dac12.h:227
dac12_t
DAC 12 Click context object.
Definition: dac12.h:193
DAC12_ERROR
@ DAC12_ERROR
Definition: dac12.h:229
dac12_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: dac12.h:217
dac12_t::chip_select
pin_name_t chip_select
Definition: dac12.h:197
dac12_init
err_t dac12_init(dac12_t *ctx, dac12_cfg_t *cfg)
DAC 12 initialization function.
dac12_cfg_setup
void dac12_cfg_setup(dac12_cfg_t *cfg)
DAC 12 configuration object setup function.
dac12_cfg_t::cs
pin_name_t cs
Definition: dac12.h:213
dac12_soft_reset
err_t dac12_soft_reset(dac12_t *ctx)
DAC 12 soft reset function.