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 "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 C420MAT2_REG_XFER 0x01
71 #define C420MAT2_REG_NOP 0x02
72 #define C420MAT2_REG_WR_MODE 0x03
73 #define C420MAT2_REG_DACCODE 0x04
74 #define C420MAT2_REG_ERR_CONFIG 0x05
75 #define C420MAT2_REG_ERR_LOW 0x06
76 #define C420MAT2_REG_ERR_HIGH 0x07
77 #define C420MAT2_REG_RESET 0x08
78 #define C420MAT2_REG_STATUS 0x09
79  // c420mat2_reg
81 
96 #define C420MAT2_CMD_XFER 0x00FF
97 #define C420MAT2_CMD_NOP 0x0000
98 #define C420MAT2_CMD_WR_MODE_DISABLE 0x0000
99 #define C420MAT2_CMD_WR_MODE_ENABLE 0x0001
100 #define C420MAT2_CMD_RETRY_TIMEL_RETRY_TIME_50ms 0x0000
101 #define C420MAT2_CMD_RETRY_TIMEL_RETRY_TIME_100ms 0x0100
102 #define C420MAT2_CMD_RETRY_TIMEL_RETRY_TIME_150ms 0x0200
103 #define C420MAT2_CMD_RETRY_TIMEL_RETRY_TIME_200ms 0x0300
104 #define C420MAT2_CMD_RETRY_TIMEL_RETRY_TIME_250ms 0x0400
105 #define C420MAT2_CMD_RETRY_TIMEL_RETRY_TIME_300ms 0x0500
106 #define C420MAT2_CMD_RETRY_TIMEL_RETRY_TIME_350ms 0x0600
107 #define C420MAT2_CMD_RETRY_TIMEL_RETRY_TIME_400ms 0x0700
108 #define C420MAT2_CMD_DIS_RETRY_LOOP_DISABLE 0x0000
109 #define C420MAT2_CMD_DIS_RETRY_LOOP_ENABLE 0x0080
110 #define C420MAT2_CMD_MASK_LOOP_ERR_DISABLE 0x0000
111 #define C420MAT2_CMD_MASK_LOOP_ERR_ENABLE 0x0040
112 #define C420MAT2_CMD_DIS_LOOP_ERR_ERRB_DISABLE 0x0000
113 #define C420MAT2_CMD_DIS_LOOP_ERR_ERRB_ENABLE 0x0020
114 #define C420MAT2_CMD_MASK_SPI_ERR_DISABLE 0x0000
115 #define C420MAT2_CMD_MASK_SPI_ERR_ENABLE 0x0010
116 #define C420MAT2_CMD_SPI_TIMEOUT_50ms 0x0000
117 #define C420MAT2_CMD_SPI_TIMEOUT_100ms 0x0002
118 #define C420MAT2_CMD_SPI_TIMEOUT_150ms 0x0004
119 #define C420MAT2_CMD_SPI_TIMEOUT_200ms 0x0006
120 #define C420MAT2_CMD_SPI_TIMEOUT_250ms 0x0008
121 #define C420MAT2_CMD_SPI_TIMEOUT_300ms 0x000A
122 #define C420MAT2_CMD_SPI_TIMEOUT_350ms 0x000C
123 #define C420MAT2_CMD_SPI_TIMEOUT_400ms 0x000E
124 #define C420MAT2_CMD_MASK_SPI_TOUT_DISABLE 0x0000
125 #define C420MAT2_CMD_MASK_SPI_TOUT_ENABLE 0x0001
126 #define C420MAT2_CMD_RESET 0xC33C
127 
132 #define C420MAT2_STATUS_DAC_RES_BIT_MASK 0x00E0
133 #define C420MAT2_STATUS_ERRLVL_PIN_BIT_MASK 0x0010
134 #define C420MAT2_STATUS_FERR_STS_BIT_MASK 0x0008
135 #define C420MAT2_STATUSSPI_TIMEOUT_ERR_BIT_MASK 0x0004
136 #define C420MAT2_STATUS_LOOP_STS_BIT_MASK 0x0002
137 #define C420MAT2_STATUS_CURR_LOOP_STS_BIT_MASK 0x0001
138 
139 
148 #define C420MAT2_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
149 #define C420MAT2_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
150  // c420mat2_set
152 
167 #define C420MAT2_MAP_MIKROBUS( cfg, mikrobus ) \
168  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
169  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
170  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
171  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
172  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
173  cfg.err = MIKROBUS( mikrobus, MIKROBUS_INT )
174  // c420mat2_map // c420mat2
177 
182 typedef struct
183 {
184  // Output pins
185  digital_out_t rst;
187  // Input pins
188  digital_in_t err;
190  // Modules
191  spi_master_t spi;
193  pin_name_t chip_select;
195 } c420mat2_t;
196 
201 typedef struct
202 {
203  // Communication gpio pins
204  pin_name_t miso;
205  pin_name_t mosi;
206  pin_name_t sck;
207  pin_name_t cs;
209  // Additional gpio pins
210  pin_name_t rst;
211  pin_name_t err;
213  // static variable
214  uint32_t spi_speed;
215  spi_master_mode_t spi_mode;
216  spi_master_chip_select_polarity_t cs_polarity;
219 
224 typedef enum
225 {
227  C420MAT2_ERROR = -1
228 
230 
235 typedef struct
236 {
237  uint8_t errlvl_pin;
238  uint8_t ferr_sts;
240  uint8_t loop_sts;
241  uint8_t curr_loop_sts;
242 
244 
249 typedef enum
250 {
253 
255 
272 
287 
301 
315 err_t c420mat2_write_data ( c420mat2_t *ctx, uint8_t reg, uint16_t data_in );
316 
330 err_t c420mat2_read_data ( c420mat2_t *ctx, uint8_t cmd, uint16_t *data_out );
331 
345 err_t c420mat2_protected_write ( c420mat2_t *ctx, uint8_t reg, uint16_t data_in );
346 
360 
375 err_t c420mat2_set_dac_data ( c420mat2_t *ctx, uint16_t dac_output_code );
376 
390 err_t c420mat2_set_lower_limit ( c420mat2_t *ctx, float lower_limit_ma );
391 
405 err_t c420mat2_set_upper_limit ( c420mat2_t *ctx, float upper_limit_ma );
406 
422 err_t c420mat2_set_output_current ( c420mat2_t *ctx, float current_ma );
423 
439 
453 
454 #ifdef __cplusplus
455 }
456 #endif
457 #endif // C420MAT2_H
458  // c420mat2
460 
461 // ------------------------------------------------------------------------ END
C420MAT2_STATUS_OK
@ C420MAT2_STATUS_OK
Definition: c420mat2.h:251
C420MAT2_ERROR
@ C420MAT2_ERROR
Definition: c420mat2.h:227
c420mat2_cfg_t::err
pin_name_t err
Definition: c420mat2.h:211
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:210
C420MAT2_STATUS_ERROR
@ C420MAT2_STATUS_ERROR
Definition: c420mat2.h:252
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:226
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:207
c420mat2_cfg_t::spi_speed
uint32_t spi_speed
Definition: c420mat2.h:214
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:236
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:240
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:216
c420mat2_cfg_t::mosi
pin_name_t mosi
Definition: c420mat2.h:205
c420mat2_t::spi
spi_master_t spi
Definition: c420mat2.h:191
c420mat2_status_t::curr_loop_sts
uint8_t curr_loop_sts
Definition: c420mat2.h:241
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:215
c420mat2_t::rst
digital_out_t rst
Definition: c420mat2.h:185
c420mat2_status_t::spi_timeout_err
uint8_t spi_timeout_err
Definition: c420mat2.h:239
c420mat2_t::chip_select
pin_name_t chip_select
Definition: c420mat2.h:193
c420mat2_status_value_t
c420mat2_status_value_t
4-20mA T 2 Click error status value data.
Definition: c420mat2.h:250
c420mat2_status_t::ferr_sts
uint8_t ferr_sts
Definition: c420mat2.h:238
c420mat2_t::err
digital_in_t err
Definition: c420mat2.h:188
c420mat2_cfg_t::sck
pin_name_t sck
Definition: c420mat2.h:206
c420mat2_status_t::errlvl_pin
uint8_t errlvl_pin
Definition: c420mat2.h:237
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:204
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:183
c420mat2_return_value_t
c420mat2_return_value_t
4-20mA T 2 Click return value data.
Definition: c420mat2.h:225
c420mat2_cfg_t
4-20mA T 2 Click configuration object.
Definition: c420mat2.h:202