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
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 C420MAT2_REG_XFER 0x01
61 #define C420MAT2_REG_NOP 0x02
62 #define C420MAT2_REG_WR_MODE 0x03
63 #define C420MAT2_REG_DACCODE 0x04
64 #define C420MAT2_REG_ERR_CONFIG 0x05
65 #define C420MAT2_REG_ERR_LOW 0x06
66 #define C420MAT2_REG_ERR_HIGH 0x07
67 #define C420MAT2_REG_RESET 0x08
68 #define C420MAT2_REG_STATUS 0x09
69  // c420mat2_reg
71 
86 #define C420MAT2_CMD_XFER 0x00FF
87 #define C420MAT2_CMD_NOP 0x0000
88 #define C420MAT2_CMD_WR_MODE_DISABLE 0x0000
89 #define C420MAT2_CMD_WR_MODE_ENABLE 0x0001
90 #define C420MAT2_CMD_RETRY_TIMEL_RETRY_TIME_50ms 0x0000
91 #define C420MAT2_CMD_RETRY_TIMEL_RETRY_TIME_100ms 0x0100
92 #define C420MAT2_CMD_RETRY_TIMEL_RETRY_TIME_150ms 0x0200
93 #define C420MAT2_CMD_RETRY_TIMEL_RETRY_TIME_200ms 0x0300
94 #define C420MAT2_CMD_RETRY_TIMEL_RETRY_TIME_250ms 0x0400
95 #define C420MAT2_CMD_RETRY_TIMEL_RETRY_TIME_300ms 0x0500
96 #define C420MAT2_CMD_RETRY_TIMEL_RETRY_TIME_350ms 0x0600
97 #define C420MAT2_CMD_RETRY_TIMEL_RETRY_TIME_400ms 0x0700
98 #define C420MAT2_CMD_DIS_RETRY_LOOP_DISABLE 0x0000
99 #define C420MAT2_CMD_DIS_RETRY_LOOP_ENABLE 0x0080
100 #define C420MAT2_CMD_MASK_LOOP_ERR_DISABLE 0x0000
101 #define C420MAT2_CMD_MASK_LOOP_ERR_ENABLE 0x0040
102 #define C420MAT2_CMD_DIS_LOOP_ERR_ERRB_DISABLE 0x0000
103 #define C420MAT2_CMD_DIS_LOOP_ERR_ERRB_ENABLE 0x0020
104 #define C420MAT2_CMD_MASK_SPI_ERR_DISABLE 0x0000
105 #define C420MAT2_CMD_MASK_SPI_ERR_ENABLE 0x0010
106 #define C420MAT2_CMD_SPI_TIMEOUT_50ms 0x0000
107 #define C420MAT2_CMD_SPI_TIMEOUT_100ms 0x0002
108 #define C420MAT2_CMD_SPI_TIMEOUT_150ms 0x0004
109 #define C420MAT2_CMD_SPI_TIMEOUT_200ms 0x0006
110 #define C420MAT2_CMD_SPI_TIMEOUT_250ms 0x0008
111 #define C420MAT2_CMD_SPI_TIMEOUT_300ms 0x000A
112 #define C420MAT2_CMD_SPI_TIMEOUT_350ms 0x000C
113 #define C420MAT2_CMD_SPI_TIMEOUT_400ms 0x000E
114 #define C420MAT2_CMD_MASK_SPI_TOUT_DISABLE 0x0000
115 #define C420MAT2_CMD_MASK_SPI_TOUT_ENABLE 0x0001
116 #define C420MAT2_CMD_RESET 0xC33C
117 
122 #define C420MAT2_STATUS_DAC_RES_BIT_MASK 0x00E0
123 #define C420MAT2_STATUS_ERRLVL_PIN_BIT_MASK 0x0010
124 #define C420MAT2_STATUS_FERR_STS_BIT_MASK 0x0008
125 #define C420MAT2_STATUSSPI_TIMEOUT_ERR_BIT_MASK 0x0004
126 #define C420MAT2_STATUS_LOOP_STS_BIT_MASK 0x0002
127 #define C420MAT2_STATUS_CURR_LOOP_STS_BIT_MASK 0x0001
128 
129 
138 #define C420MAT2_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
139 #define C420MAT2_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
140  // c420mat2_set
142 
157 #define C420MAT2_MAP_MIKROBUS( cfg, mikrobus ) \
158  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
159  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
160  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
161  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
162  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
163  cfg.err = MIKROBUS( mikrobus, MIKROBUS_INT )
164  // c420mat2_map // c420mat2
167 
172 typedef struct
173 {
174  // Output pins
175  digital_out_t rst;
177  // Input pins
178  digital_in_t err;
180  // Modules
181  spi_master_t spi;
183  pin_name_t chip_select;
185 } c420mat2_t;
186 
191 typedef struct
192 {
193  // Communication gpio pins
194  pin_name_t miso;
195  pin_name_t mosi;
196  pin_name_t sck;
197  pin_name_t cs;
199  // Additional gpio pins
200  pin_name_t rst;
201  pin_name_t err;
203  // static variable
204  uint32_t spi_speed;
205  spi_master_mode_t spi_mode;
206  spi_master_chip_select_polarity_t cs_polarity;
209 
214 typedef enum
215 {
217  C420MAT2_ERROR = -1
218 
220 
225 typedef struct
226 {
227  uint8_t errlvl_pin;
228  uint8_t ferr_sts;
230  uint8_t loop_sts;
231  uint8_t curr_loop_sts;
232 
234 
239 typedef enum
240 {
243 
245 
262 
277 
291 
305 err_t c420mat2_write_data ( c420mat2_t *ctx, uint8_t reg, uint16_t data_in );
306 
320 err_t c420mat2_read_data ( c420mat2_t *ctx, uint8_t cmd, uint16_t *data_out );
321 
335 err_t c420mat2_protected_write ( c420mat2_t *ctx, uint8_t reg, uint16_t data_in );
336 
350 
365 err_t c420mat2_set_dac_data ( c420mat2_t *ctx, uint16_t dac_output_code );
366 
380 err_t c420mat2_set_lower_limit ( c420mat2_t *ctx, float lower_limit_ma );
381 
395 err_t c420mat2_set_upper_limit ( c420mat2_t *ctx, float upper_limit_ma );
396 
412 err_t c420mat2_set_output_current ( c420mat2_t *ctx, float current_ma );
413 
429 
443 
444 #ifdef __cplusplus
445 }
446 #endif
447 #endif // C420MAT2_H
448  // c420mat2
450 
451 // ------------------------------------------------------------------------ END
C420MAT2_STATUS_OK
@ C420MAT2_STATUS_OK
Definition: c420mat2.h:241
C420MAT2_ERROR
@ C420MAT2_ERROR
Definition: c420mat2.h:217
c420mat2_cfg_t::err
pin_name_t err
Definition: c420mat2.h:201
c420mat2_get_status
err_t c420mat2_get_status(c420mat2_t *ctx, c420mat2_status_t *status)
4-20mA T 2 set status function.
c420mat2_cfg_t::rst
pin_name_t rst
Definition: c420mat2.h:200
C420MAT2_STATUS_ERROR
@ C420MAT2_STATUS_ERROR
Definition: c420mat2.h:242
spi_specifics.h
This file contains SPI specific macros, functions, etc.
c420mat2_set_upper_limit
err_t c420mat2_set_upper_limit(c420mat2_t *ctx, float upper_limit_ma)
4-20mA T 2 set upper limit function.
C420MAT2_OK
@ C420MAT2_OK
Definition: c420mat2.h:216
c420mat2_default_cfg
err_t c420mat2_default_cfg(c420mat2_t *ctx)
4-20mA T 2 default configuration function.
c420mat2_cfg_t::cs
pin_name_t cs
Definition: c420mat2.h:197
c420mat2_cfg_t::spi_speed
uint32_t spi_speed
Definition: c420mat2.h:204
c420mat2_protected_write
err_t c420mat2_protected_write(c420mat2_t *ctx, uint8_t reg, uint16_t data_in)
4-20mA T 2 protected data writing function.
c420mat2_status_t
4-20mA T 2 Click status object.
Definition: c420mat2.h:226
c420mat2_init
err_t c420mat2_init(c420mat2_t *ctx, c420mat2_cfg_t *cfg)
4-20mA T 2 initialization function.
c420mat2_status_t::loop_sts
uint8_t loop_sts
Definition: c420mat2.h:230
c420mat2_get_err_pin
uint8_t c420mat2_get_err_pin(c420mat2_t *ctx)
4-20mA T 2 get error flag pin state function.
c420mat2_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: c420mat2.h:206
c420mat2_cfg_t::mosi
pin_name_t mosi
Definition: c420mat2.h:195
c420mat2_t::spi
spi_master_t spi
Definition: c420mat2.h:181
c420mat2_status_t::curr_loop_sts
uint8_t curr_loop_sts
Definition: c420mat2.h:231
c420mat2_set_output_current
err_t c420mat2_set_output_current(c420mat2_t *ctx, float current_ma)
4-20mA T 2 set output current function.
c420mat2_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: c420mat2.h:205
c420mat2_t::rst
digital_out_t rst
Definition: c420mat2.h:175
c420mat2_status_t::spi_timeout_err
uint8_t spi_timeout_err
Definition: c420mat2.h:229
c420mat2_t::chip_select
pin_name_t chip_select
Definition: c420mat2.h:183
c420mat2_status_value_t
c420mat2_status_value_t
4-20mA T 2 Click error status value data.
Definition: c420mat2.h:240
c420mat2_status_t::ferr_sts
uint8_t ferr_sts
Definition: c420mat2.h:228
c420mat2_t::err
digital_in_t err
Definition: c420mat2.h:178
c420mat2_cfg_t::sck
pin_name_t sck
Definition: c420mat2.h:196
c420mat2_status_t::errlvl_pin
uint8_t errlvl_pin
Definition: c420mat2.h:227
c420mat2_write_data
err_t c420mat2_write_data(c420mat2_t *ctx, uint8_t reg, uint16_t data_in)
4-20mA T 2 data writing function.
c420mat2_cfg_t::miso
pin_name_t miso
Definition: c420mat2.h:194
c420mat2_cfg_setup
void c420mat2_cfg_setup(c420mat2_cfg_t *cfg)
4-20mA T 2 configuration object setup function.
c420mat2_read_data
err_t c420mat2_read_data(c420mat2_t *ctx, uint8_t cmd, uint16_t *data_out)
4-20mA T 2 data reading function.
c420mat2_set_lower_limit
err_t c420mat2_set_lower_limit(c420mat2_t *ctx, float lower_limit_ma)
4-20mA T 2 set lower limit function.
c420mat2_sw_reset
err_t c420mat2_sw_reset(c420mat2_t *ctx)
4-20mA T 2 soft reset function.
c420mat2_set_dac_data
err_t c420mat2_set_dac_data(c420mat2_t *ctx, uint16_t dac_output_code)
4-20mA T 2 set DAC data function.
c420mat2_t
4-20mA T 2 Click context object.
Definition: c420mat2.h:173
c420mat2_return_value_t
c420mat2_return_value_t
4-20mA T 2 Click return value data.
Definition: c420mat2.h:215
c420mat2_cfg_t
4-20mA T 2 Click configuration object.
Definition: c420mat2.h:192