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 "drv_digital_out.h"
36 #include "drv_digital_in.h"
37 #include "drv_spi_master.h"
38 #include "spi_specifics.h"
39 
60 #define DAC12_REG_NOP 0x00
61 #define DAC12_REG_DEVICE_ID 0x01
62 #define DAC12_REG_SYNC 0x02
63 #define DAC12_REG_CONFIG 0x03
64 #define DAC12_REG_GAIN 0x04
65 #define DAC12_REG_TRIGGER 0x05
66 #define DAC12_REG_BROADCAST 0x06
67 #define DAC12_REG_STATUS 0x07
68 #define DAC12_REG_DAC0 0x08
69 #define DAC12_REG_DAC1 0x09
70 #define DAC12_REG_DAC2 0x0A
71 #define DAC12_REG_DAC3 0x0B
72 #define DAC12_REG_DAC4 0x0C
73 #define DAC12_REG_DAC5 0x0D
74 #define DAC12_REG_DAC6 0x0E
75 #define DAC12_REG_DAC7 0x0F
76  // dac12_reg
78 
93 #define DAC12_SELECT_CHANNEL_NONE 0x00
94 #define DAC12_SELECT_CHANNEL_0 0x01
95 #define DAC12_SELECT_CHANNEL_1 0x02
96 #define DAC12_SELECT_CHANNEL_2 0x04
97 #define DAC12_SELECT_CHANNEL_3 0x08
98 #define DAC12_SELECT_CHANNEL_4 0x10
99 #define DAC12_SELECT_CHANNEL_5 0x20
100 #define DAC12_SELECT_CHANNEL_6 0x40
101 #define DAC12_SELECT_CHANNEL_7 0x80
102 #define DAC12_SELECT_CHANNEL_ALL 0xFF
103 
108 #define DAC12_GAIN_REFDIV_EN 0x0100
109 
114 #define DAC12_MIN_DAC_VALUE 0x0000
115 #define DAC12_MAX_DAC_VALUE 0x0FFF
116 
121 #define DAC12_INTERNAL_VREF 2.5
122 
127 #define DAC12_SOFT_RESET 0x000A
128 
133 #define DAC12_DEVICE_ID 0x2894
134 #define DAC12_DEVICE_ID_MASK 0xFFFC
135 
140 #define DAC12_SPI_READ 0x80
141 
150 #define DAC12_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
151 #define DAC12_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
152  // dac12_set
154 
169 #define DAC12_MAP_MIKROBUS( cfg, mikrobus ) \
170  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
171  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
172  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
173  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS );
174  // dac12_map // dac12
177 
182 typedef struct
183 {
184  // Modules
185  spi_master_t spi;
187  pin_name_t chip_select;
189  pin_name_t channel_gain;
191 } dac12_t;
192 
197 typedef struct
198 {
199  // Communication gpio pins
200  pin_name_t miso;
201  pin_name_t mosi;
202  pin_name_t sck;
203  pin_name_t cs;
205  // static variable
206  uint32_t spi_speed;
207  spi_master_mode_t spi_mode;
208  spi_master_chip_select_polarity_t cs_polarity;
210 } dac12_cfg_t;
211 
216 typedef enum
217 {
218  DAC12_OK = 0,
219  DAC12_ERROR = -1
220 
222 
239 
254 err_t dac12_init ( dac12_t *ctx, dac12_cfg_t *cfg );
255 
269 err_t dac12_default_cfg ( dac12_t *ctx );
270 
284 err_t dac12_write_register ( dac12_t *ctx, uint8_t reg, uint16_t data_in );
285 
299 err_t dac12_read_register ( dac12_t *ctx, uint8_t reg, uint16_t *data_out );
300 
313 
325 err_t dac12_soft_reset ( dac12_t *ctx );
326 
340 err_t dac12_set_gain ( dac12_t *ctx, uint8_t channel );
341 
355 err_t dac12_set_channel_value ( dac12_t *ctx, uint8_t channel, uint16_t dac_value );
356 
370 err_t dac12_set_channel_voltage ( dac12_t *ctx, uint8_t channel, float voltage );
371 
372 #ifdef __cplusplus
373 }
374 #endif
375 #endif // DAC12_H
376  // dac12
378 
379 // ------------------------------------------------------------------------ END
dac12_t::spi
spi_master_t spi
Definition: dac12.h:185
dac12_cfg_t
DAC 12 Click configuration object.
Definition: dac12.h:198
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:208
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:189
dac12_cfg_t::mosi
pin_name_t mosi
Definition: dac12.h:201
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:206
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:202
dac12_default_cfg
err_t dac12_default_cfg(dac12_t *ctx)
DAC 12 default configuration function.
DAC12_OK
@ DAC12_OK
Definition: dac12.h:218
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:200
dac12_return_value_t
dac12_return_value_t
DAC 12 Click return value data.
Definition: dac12.h:217
dac12_t
DAC 12 Click context object.
Definition: dac12.h:183
DAC12_ERROR
@ DAC12_ERROR
Definition: dac12.h:219
dac12_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: dac12.h:207
dac12_t::chip_select
pin_name_t chip_select
Definition: dac12.h:187
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:203
dac12_soft_reset
err_t dac12_soft_reset(dac12_t *ctx)
DAC 12 soft reset function.