tdc2  2.1.0.0
tdc2.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 TDC2_H
29 #define TDC2_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 
75 #define TDC2_REG_CFG0 0x00
76 #define TDC2_REG_CFG1 0x01
77 #define TDC2_REG_CFG2 0x02
78 #define TDC2_REG_CFG3 0x03
79 #define TDC2_REG_CFG4 0x04
80 #define TDC2_REG_CFG5 0x05
81 #define TDC2_REG_CFG6 0x06
82 #define TDC2_REG_CFG7 0x07
83 #define TDC2_REG_CFG8 0x08
84 #define TDC2_REG_CFG9 0x09
85 #define TDC2_REG_CFG10 0x0A
86 #define TDC2_REG_CFG11 0x0B
87 #define TDC2_REG_CFG12 0x0C
88 #define TDC2_REG_CFG13 0x0D
89 #define TDC2_REG_CFG14 0x0E
90 #define TDC2_REG_CFG15 0x0F
91 #define TDC2_REG_CFG16 0x10
92 
97 #define TDC2_REG_INDEX_CH1_BYTE3 0x08
98 #define TDC2_REG_INDEX_CH1_BYTE2 0x09
99 #define TDC2_REG_INDEX_CH1_BYTE1 0x0A
100 #define TDC2_REG_STOP_CH1_BYTE3 0x0B
101 #define TDC2_REG_STOP_CH1_BYTE2 0x0C
102 #define TDC2_REG_STOP_CH1_BYTE1 0x0D
103 #define TDC2_REG_INDEX_CH2_BYTE3 0x0E
104 #define TDC2_REG_INDEX_CH2_BYTE2 0x0F
105 #define TDC2_REG_INDEX_CH2_BYTE1 0x10
106 #define TDC2_REG_STOP_CH2_BYTE3 0x11
107 #define TDC2_REG_STOP_CH2_BYTE2 0x12
108 #define TDC2_REG_STOP_CH2_BYTE1 0x13
109 #define TDC2_REG_INDEX_CH3_BYTE3 0x14
110 #define TDC2_REG_INDEX_CH3_BYTE2 0x15
111 #define TDC2_REG_INDEX_CH3_BYTE1 0x16
112 #define TDC2_REG_STOP_CH3_BYTE3 0x17
113 #define TDC2_REG_STOP_CH3_BYTE2 0x18
114 #define TDC2_REG_STOP_CH3_BYTE1 0x19
115 #define TDC2_REG_INDEX_CH4_BYTE3 0x1A
116 #define TDC2_REG_INDEX_CH4_BYTE2 0x1B
117 #define TDC2_REG_INDEX_CH4_BYTE1 0x1C
118 #define TDC2_REG_STOP_CH4_BYTE3 0x1D
119 #define TDC2_REG_STOP_CH4_BYTE2 0x1E
120 #define TDC2_REG_STOP_CH4_BYTE1 0x1F
121  // tdc2_reg
123 
143 #define TDC2_SPIOPC_POWER 0x30
144 #define TDC2_SPIOPC_INIT 0x18
145 #define TDC2_SPIOPC_WRITE_CONFIG 0x80
146 #define TDC2_SPIOPC_READ_CONFIG 0x40
147 #define TDC2_SPIOPC_READ_RESULTS 0x60
148 #define TDC2_SPIOPC_READ_RESULTS 0x60
149 
154 #define TDC2_ENABLE_STOP1_PIN 0x01
155 #define TDC2_DISABLE_STOP1_PIN 0x00
156 #define TDC2_ENABLE_STOP2_PIN 0x02
157 #define TDC2_DISABLE_STOP2_PIN 0x00
158 #define TDC2_ENABLE_STOP3_PIN 0x04
159 #define TDC2_DISABLE_STOP3_PIN 0x00
160 #define TDC2_ENABLE_STOP4_PIN 0x08
161 #define TDC2_DISABLE_STOP4_PIN 0x00
162 #define TDC2_ENABLE_REFCLK_PIN 0x10
163 #define TDC2_DISABLE_REFCLK_PIN 0x00
164 #define TDC2_ENABLE_DISABLE_PIN 0x40
165 #define TDC2_DISABLE_DISABLE_PIN 0x00
166 #define TDC2_ENABLE_RSTINDX_PIN 0x80
167 #define TDC2_DISABLE_RSTINDX_PIN 0x00
168 
173 #define TDC2_HIT_ENABLE_STOP1 0x01
174 #define TDC2_HIT_DISABLE_STOP1 0x00
175 #define TDC2_HIT_ENABLE_STOP2 0x02
176 #define TDC2_HIT_DISABLE_STOP2 0x00
177 #define TDC2_HIT_ENABLE_STOP3 0x04
178 #define TDC2_HIT_DISABLE_STOP3 0x00
179 #define TDC2_HIT_ENABLE_STOP4 0x08
180 #define TDC2_HIT_DISABLE_STOP4 0x00
181 #define TDC2_CHANNEL_COMBINE_NORMAL 0x00
182 #define TDC2_CHANNEL_COMBINE_PULSE_DISTANCE 0x10
183 #define TDC2_CHANNEL_COMBINE_PULSE_WIDTH 0x20
184 #define TDC2_HIGH_RESOLUTION_OFF 0x00
185 #define TDC2_HIGH_RESOLUTION_X2 0x40
186 #define TDC2_HIGH_RESOLUTION_X4 0x80
187 
192 #define TDC2_FIFO_COMMON_READ_OFF 0x00
193 #define TDC2_FIFO_COMMON_READ_ON 0x40
194 #define TDC2_FIFO_BLOCKWISE_READ_OFF 0x00
195 #define TDC2_FIFO_BLOCKWISE_READ_ON 0x80
196 
201 #define TDC2_CFG6_FIXED_VALUE 0xC0
202 #define TDC2_CFG8_FIXED_VALUE 0xA1
203 #define TDC2_CFG9_FIXED_VALUE 0x13
204 #define TDC2_CFG10_FIXED_VALUE 0x00
205 #define TDC2_CFG11_FIXED_VALUE 0x0A
206 #define TDC2_CFG12_FIXED_VALUE 0xCC
207 #define TDC2_CFG13_FIXED_VALUE 0x05
208 #define TDC2_CFG14_FIXED_VALUE 0xF1
209 #define TDC2_CFG15_FIXED_VALUE 0x7D
210 #define TDC2_CFG16_FIXED_VALUE 0x04
211 
216 #define TDC2_CFG7_FIXED_VALUE 0x23
217 #define TDC2_REFERENCE_CLOCK_INTERNAL 0x80
218 #define TDC2_REFERENCE_CLOCK_EXTERNAL 0x00
219 
224 #define TDC2_1pS_RESOLUTION 125000
225 #define TDC2_5pS_RESOLUTION 62500
226 #define TDC2_10pS_RESOLUTION 12500
227 
228 #define TDC2_uS_TO_mS 1000000
229 
238 #define TDC2_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
239 #define TDC2_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
240  // tdc2_set
242 
257 #define TDC2_MAP_MIKROBUS( cfg, mikrobus ) \
258  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
259  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
260  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
261  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
262  cfg.rir = MIKROBUS( mikrobus, MIKROBUS_AN ); \
263  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
264  cfg.dis = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
265  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
266  // tdc2_map // tdc2
269 
274 typedef struct
275 {
276  // Output pins
277  digital_out_t rir;
278  digital_out_t rst;
279  digital_out_t dis;
281  // Input pins
282  digital_in_t int_pin;
284  // Modules
285  spi_master_t spi;
287  pin_name_t chip_select;
289 } tdc2_t;
290 
295 typedef struct
296 {
297  // Communication gpio pins
298  pin_name_t miso;
299  pin_name_t mosi;
300  pin_name_t sck;
301  pin_name_t cs;
303  // Additional gpio pins
304  pin_name_t rir;
305  pin_name_t rst;
306  pin_name_t dis;
307  pin_name_t int_pin;
309  // static variable
310  uint32_t spi_speed;
311  spi_master_mode_t spi_mode;
312  spi_master_chip_select_polarity_t cs_polarity;
314 } tdc2_cfg_t;
315 
320 typedef enum
321 {
322  TDC2_OK = 0,
323  TDC2_ERROR = -1
324 
326 
343 
357 err_t tdc2_init ( tdc2_t *ctx, tdc2_cfg_t *cfg );
358 
371 err_t tdc2_default_cfg ( tdc2_t *ctx );
372 
387 err_t tdc2_generic_write ( tdc2_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
388 
403 err_t tdc2_generic_read ( tdc2_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
404 
419 err_t tdc2_write_config ( tdc2_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
420 
435 err_t tdc2_read_config ( tdc2_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
436 
451 err_t tdc2_read_results( tdc2_t *ctx, uint8_t reg, uint32_t *reference_index, uint32_t *stop_result );
452 
465 err_t tdc2_send_command ( tdc2_t *ctx, uint8_t command );
466 
477 err_t tdc2_sw_reset ( tdc2_t *ctx );
478 
490 
502 err_t tdc2_set_resolution ( tdc2_t *ctx, uint32_t resolution );
503 
515 err_t tdc2_get_resolution ( tdc2_t *ctx, uint32_t *resolution );
516 
525 void tdc2_reset_index ( tdc2_t *ctx );
526 
536 
546 
556 uint8_t tdc2_get_int_state ( tdc2_t *ctx );
557 
572 err_t tdc2_get_time_between_stops ( tdc2_t *ctx, uint32_t stop_result1, uint32_t reference_index1,
573  uint32_t stop_result2, uint32_t reference_index2,
574  uint32_t *time_in_us );
575 
576 #ifdef __cplusplus
577 }
578 #endif
579 #endif // TDC2_H
580  // tdc2
582 
583 // ------------------------------------------------------------------------ END
tdc2_t
TDC 2 Click context object.
Definition: tdc2.h:275
tdc2_start_measuring
err_t tdc2_start_measuring(tdc2_t *ctx)
TDC 2 start measuring function.
tdc2_generic_read
err_t tdc2_generic_read(tdc2_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
TDC 2 data reading function.
tdc2_t::spi
spi_master_t spi
Definition: tdc2.h:285
tdc2_disable_stop_channels
void tdc2_disable_stop_channels(tdc2_t *ctx)
TDC 2 disable stop channels function.
tdc2_t::int_pin
digital_in_t int_pin
Definition: tdc2.h:282
spi_specifics.h
This file contains SPI specific macros, functions, etc.
tdc2_init
err_t tdc2_init(tdc2_t *ctx, tdc2_cfg_t *cfg)
TDC 2 initialization function.
tdc2_generic_write
err_t tdc2_generic_write(tdc2_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
TDC 2 data writing function.
tdc2_reset_index
void tdc2_reset_index(tdc2_t *ctx)
TDC 2 reset reference index function.
tdc2_t::dis
digital_out_t dis
Definition: tdc2.h:279
tdc2_get_int_state
uint8_t tdc2_get_int_state(tdc2_t *ctx)
TDC 2 get int state function.
TDC2_ERROR
@ TDC2_ERROR
Definition: tdc2.h:323
tdc2_t::rir
digital_out_t rir
Definition: tdc2.h:277
tdc2_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: tdc2.h:311
tdc2_cfg_setup
void tdc2_cfg_setup(tdc2_cfg_t *cfg)
TDC 2 configuration object setup function.
tdc2_read_results
err_t tdc2_read_results(tdc2_t *ctx, uint8_t reg, uint32_t *reference_index, uint32_t *stop_result)
TDC 2 results data reading function.
tdc2_read_config
err_t tdc2_read_config(tdc2_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
TDC 2 config data reading function.
tdc2_default_cfg
err_t tdc2_default_cfg(tdc2_t *ctx)
TDC 2 default configuration function.
tdc2_return_value_t
tdc2_return_value_t
TDC 2 Click return value data.
Definition: tdc2.h:321
tdc2_enable_stop_channels
void tdc2_enable_stop_channels(tdc2_t *ctx)
TDC 2 enable stop channels function.
tdc2_cfg_t::dis
pin_name_t dis
Definition: tdc2.h:306
tdc2_t::rst
digital_out_t rst
Definition: tdc2.h:278
tdc2_cfg_t::miso
pin_name_t miso
Definition: tdc2.h:298
tdc2_cfg_t::cs
pin_name_t cs
Definition: tdc2.h:301
tdc2_t::chip_select
pin_name_t chip_select
Definition: tdc2.h:287
tdc2_set_resolution
err_t tdc2_set_resolution(tdc2_t *ctx, uint32_t resolution)
TDC 2 set resolution function.
tdc2_cfg_t::rir
pin_name_t rir
Definition: tdc2.h:304
tdc2_get_resolution
err_t tdc2_get_resolution(tdc2_t *ctx, uint32_t *resolution)
TDC 2 set resolution function.
tdc2_cfg_t::rst
pin_name_t rst
Definition: tdc2.h:305
tdc2_write_config
err_t tdc2_write_config(tdc2_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
TDC 2 config data writing function.
tdc2_cfg_t::mosi
pin_name_t mosi
Definition: tdc2.h:299
tdc2_cfg_t::int_pin
pin_name_t int_pin
Definition: tdc2.h:307
tdc2_cfg_t::sck
pin_name_t sck
Definition: tdc2.h:300
TDC2_OK
@ TDC2_OK
Definition: tdc2.h:322
tdc2_cfg_t
TDC 2 Click configuration object.
Definition: tdc2.h:296
tdc2_send_command
err_t tdc2_send_command(tdc2_t *ctx, uint8_t command)
TDC 2 send command function.
tdc2_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: tdc2.h:312
tdc2_get_time_between_stops
err_t tdc2_get_time_between_stops(tdc2_t *ctx, uint32_t stop_result1, uint32_t reference_index1, uint32_t stop_result2, uint32_t reference_index2, uint32_t *time_in_us)
TDC 2 get time between stops function.
tdc2_cfg_t::spi_speed
uint32_t spi_speed
Definition: tdc2.h:310
tdc2_sw_reset
err_t tdc2_sw_reset(tdc2_t *ctx)
TDC 2 software reset function.