c420mat2 2.1.0.0
c420mat2.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 C420MAT2_H
29#define C420MAT2_H
30
31#ifdef __cplusplus
32extern "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_spi_master.h"
52#include "spi_specifics.h"
53
74#define C420MAT2_REG_XFER 0x01
75#define C420MAT2_REG_NOP 0x02
76#define C420MAT2_REG_WR_MODE 0x03
77#define C420MAT2_REG_DACCODE 0x04
78#define C420MAT2_REG_ERR_CONFIG 0x05
79#define C420MAT2_REG_ERR_LOW 0x06
80#define C420MAT2_REG_ERR_HIGH 0x07
81#define C420MAT2_REG_RESET 0x08
82#define C420MAT2_REG_STATUS 0x09
83
84 // c420mat2_reg
85
100#define C420MAT2_CMD_XFER 0x00FF
101#define C420MAT2_CMD_NOP 0x0000
102#define C420MAT2_CMD_WR_MODE_DISABLE 0x0000
103#define C420MAT2_CMD_WR_MODE_ENABLE 0x0001
104#define C420MAT2_CMD_RETRY_TIMEL_RETRY_TIME_50ms 0x0000
105#define C420MAT2_CMD_RETRY_TIMEL_RETRY_TIME_100ms 0x0100
106#define C420MAT2_CMD_RETRY_TIMEL_RETRY_TIME_150ms 0x0200
107#define C420MAT2_CMD_RETRY_TIMEL_RETRY_TIME_200ms 0x0300
108#define C420MAT2_CMD_RETRY_TIMEL_RETRY_TIME_250ms 0x0400
109#define C420MAT2_CMD_RETRY_TIMEL_RETRY_TIME_300ms 0x0500
110#define C420MAT2_CMD_RETRY_TIMEL_RETRY_TIME_350ms 0x0600
111#define C420MAT2_CMD_RETRY_TIMEL_RETRY_TIME_400ms 0x0700
112#define C420MAT2_CMD_DIS_RETRY_LOOP_DISABLE 0x0000
113#define C420MAT2_CMD_DIS_RETRY_LOOP_ENABLE 0x0080
114#define C420MAT2_CMD_MASK_LOOP_ERR_DISABLE 0x0000
115#define C420MAT2_CMD_MASK_LOOP_ERR_ENABLE 0x0040
116#define C420MAT2_CMD_DIS_LOOP_ERR_ERRB_DISABLE 0x0000
117#define C420MAT2_CMD_DIS_LOOP_ERR_ERRB_ENABLE 0x0020
118#define C420MAT2_CMD_MASK_SPI_ERR_DISABLE 0x0000
119#define C420MAT2_CMD_MASK_SPI_ERR_ENABLE 0x0010
120#define C420MAT2_CMD_SPI_TIMEOUT_50ms 0x0000
121#define C420MAT2_CMD_SPI_TIMEOUT_100ms 0x0002
122#define C420MAT2_CMD_SPI_TIMEOUT_150ms 0x0004
123#define C420MAT2_CMD_SPI_TIMEOUT_200ms 0x0006
124#define C420MAT2_CMD_SPI_TIMEOUT_250ms 0x0008
125#define C420MAT2_CMD_SPI_TIMEOUT_300ms 0x000A
126#define C420MAT2_CMD_SPI_TIMEOUT_350ms 0x000C
127#define C420MAT2_CMD_SPI_TIMEOUT_400ms 0x000E
128#define C420MAT2_CMD_MASK_SPI_TOUT_DISABLE 0x0000
129#define C420MAT2_CMD_MASK_SPI_TOUT_ENABLE 0x0001
130#define C420MAT2_CMD_RESET 0xC33C
131
136#define C420MAT2_STATUS_DAC_RES_BIT_MASK 0x00E0
137#define C420MAT2_STATUS_ERRLVL_PIN_BIT_MASK 0x0010
138#define C420MAT2_STATUS_FERR_STS_BIT_MASK 0x0008
139#define C420MAT2_STATUSSPI_TIMEOUT_ERR_BIT_MASK 0x0004
140#define C420MAT2_STATUS_LOOP_STS_BIT_MASK 0x0002
141#define C420MAT2_STATUS_CURR_LOOP_STS_BIT_MASK 0x0001
142
143
152#define C420MAT2_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
153#define C420MAT2_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
154
155 // c420mat2_set
156
171#define C420MAT2_MAP_MIKROBUS( cfg, mikrobus ) \
172 cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
173 cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
174 cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
175 cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
176 cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
177 cfg.err = MIKROBUS( mikrobus, MIKROBUS_INT )
178
179 // c420mat2_map
180 // c420mat2
181
186typedef struct
187{
188 // Output pins
189 digital_out_t rst;
191 // Input pins
192 digital_in_t err;
194 // Modules
195 spi_master_t spi;
197 pin_name_t chip_select;
199} c420mat2_t;
200
205typedef struct
206{
207 // Communication gpio pins
208 pin_name_t miso;
209 pin_name_t mosi;
210 pin_name_t sck;
211 pin_name_t cs;
213 // Additional gpio pins
214 pin_name_t rst;
215 pin_name_t err;
217 // static variable
218 uint32_t spi_speed;
219 spi_master_mode_t spi_mode;
220 spi_master_chip_select_polarity_t cs_polarity;
223
228typedef enum
229{
231 C420MAT2_ERROR = -1
232
234
239typedef struct
240{
241 uint8_t errlvl_pin;
242 uint8_t ferr_sts;
244 uint8_t loop_sts;
246
248
259
276
291
305
319err_t c420mat2_write_data ( c420mat2_t *ctx, uint8_t reg, uint16_t data_in );
320
334err_t c420mat2_read_data ( c420mat2_t *ctx, uint8_t cmd, uint16_t *data_out );
335
349err_t c420mat2_protected_write ( c420mat2_t *ctx, uint8_t reg, uint16_t data_in );
350
364
379err_t c420mat2_set_dac_data ( c420mat2_t *ctx, uint16_t dac_output_code );
380
394err_t c420mat2_set_lower_limit ( c420mat2_t *ctx, float lower_limit_ma );
395
409err_t c420mat2_set_upper_limit ( c420mat2_t *ctx, float upper_limit_ma );
410
426err_t c420mat2_set_output_current ( c420mat2_t *ctx, float current_ma );
427
443
457
458#ifdef __cplusplus
459}
460#endif
461#endif // C420MAT2_H
462
463 // c420mat2
464
465// ------------------------------------------------------------------------ END
c420mat2_return_value_t
4-20mA T 2 Click return value data.
Definition c420mat2.h:229
@ C420MAT2_OK
Definition c420mat2.h:230
@ C420MAT2_ERROR
Definition c420mat2.h:231
c420mat2_status_value_t
4-20mA T 2 Click error status value data.
Definition c420mat2.h:254
@ C420MAT2_STATUS_ERROR
Definition c420mat2.h:256
@ C420MAT2_STATUS_OK
Definition c420mat2.h:255
err_t c420mat2_protected_write(c420mat2_t *ctx, uint8_t reg, uint16_t data_in)
4-20mA T 2 protected data writing function.
err_t c420mat2_init(c420mat2_t *ctx, c420mat2_cfg_t *cfg)
4-20mA T 2 initialization function.
err_t c420mat2_set_upper_limit(c420mat2_t *ctx, float upper_limit_ma)
4-20mA T 2 set upper limit function.
err_t c420mat2_set_output_current(c420mat2_t *ctx, float current_ma)
4-20mA T 2 set output current function.
err_t c420mat2_get_status(c420mat2_t *ctx, c420mat2_status_t *status)
4-20mA T 2 set status function.
uint8_t c420mat2_get_err_pin(c420mat2_t *ctx)
4-20mA T 2 get error flag pin state function.
err_t c420mat2_default_cfg(c420mat2_t *ctx)
4-20mA T 2 default configuration function.
err_t c420mat2_sw_reset(c420mat2_t *ctx)
4-20mA T 2 soft reset function.
err_t c420mat2_write_data(c420mat2_t *ctx, uint8_t reg, uint16_t data_in)
4-20mA T 2 data writing function.
err_t c420mat2_read_data(c420mat2_t *ctx, uint8_t cmd, uint16_t *data_out)
4-20mA T 2 data reading function.
err_t c420mat2_set_lower_limit(c420mat2_t *ctx, float lower_limit_ma)
4-20mA T 2 set lower limit function.
void c420mat2_cfg_setup(c420mat2_cfg_t *cfg)
4-20mA T 2 configuration object setup function.
err_t c420mat2_set_dac_data(c420mat2_t *ctx, uint16_t dac_output_code)
4-20mA T 2 set DAC data function.
This file contains SPI specific macros, functions, etc.
4-20mA T 2 Click configuration object.
Definition c420mat2.h:206
spi_master_chip_select_polarity_t cs_polarity
Definition c420mat2.h:220
pin_name_t sck
Definition c420mat2.h:210
pin_name_t err
Definition c420mat2.h:215
spi_master_mode_t spi_mode
Definition c420mat2.h:219
pin_name_t mosi
Definition c420mat2.h:209
uint32_t spi_speed
Definition c420mat2.h:218
pin_name_t miso
Definition c420mat2.h:208
pin_name_t rst
Definition c420mat2.h:214
pin_name_t cs
Definition c420mat2.h:211
4-20mA T 2 Click status object.
Definition c420mat2.h:240
uint8_t loop_sts
Definition c420mat2.h:244
uint8_t spi_timeout_err
Definition c420mat2.h:243
uint8_t ferr_sts
Definition c420mat2.h:242
uint8_t curr_loop_sts
Definition c420mat2.h:245
uint8_t errlvl_pin
Definition c420mat2.h:241
4-20mA T 2 Click context object.
Definition c420mat2.h:187
spi_master_t spi
Definition c420mat2.h:195
digital_out_t rst
Definition c420mat2.h:189
digital_in_t err
Definition c420mat2.h:192
pin_name_t chip_select
Definition c420mat2.h:197