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 "drv_digital_out.h"
36 #include "drv_digital_in.h"
37 #include "drv_spi_master.h"
38 #include "spi_specifics.h"
39 
65 #define TDC2_REG_CFG0 0x00
66 #define TDC2_REG_CFG1 0x01
67 #define TDC2_REG_CFG2 0x02
68 #define TDC2_REG_CFG3 0x03
69 #define TDC2_REG_CFG4 0x04
70 #define TDC2_REG_CFG5 0x05
71 #define TDC2_REG_CFG6 0x06
72 #define TDC2_REG_CFG7 0x07
73 #define TDC2_REG_CFG8 0x08
74 #define TDC2_REG_CFG9 0x09
75 #define TDC2_REG_CFG10 0x0A
76 #define TDC2_REG_CFG11 0x0B
77 #define TDC2_REG_CFG12 0x0C
78 #define TDC2_REG_CFG13 0x0D
79 #define TDC2_REG_CFG14 0x0E
80 #define TDC2_REG_CFG15 0x0F
81 #define TDC2_REG_CFG16 0x10
82 
87 #define TDC2_REG_INDEX_CH1_BYTE3 0x08
88 #define TDC2_REG_INDEX_CH1_BYTE2 0x09
89 #define TDC2_REG_INDEX_CH1_BYTE1 0x0A
90 #define TDC2_REG_STOP_CH1_BYTE3 0x0B
91 #define TDC2_REG_STOP_CH1_BYTE2 0x0C
92 #define TDC2_REG_STOP_CH1_BYTE1 0x0D
93 #define TDC2_REG_INDEX_CH2_BYTE3 0x0E
94 #define TDC2_REG_INDEX_CH2_BYTE2 0x0F
95 #define TDC2_REG_INDEX_CH2_BYTE1 0x10
96 #define TDC2_REG_STOP_CH2_BYTE3 0x11
97 #define TDC2_REG_STOP_CH2_BYTE2 0x12
98 #define TDC2_REG_STOP_CH2_BYTE1 0x13
99 #define TDC2_REG_INDEX_CH3_BYTE3 0x14
100 #define TDC2_REG_INDEX_CH3_BYTE2 0x15
101 #define TDC2_REG_INDEX_CH3_BYTE1 0x16
102 #define TDC2_REG_STOP_CH3_BYTE3 0x17
103 #define TDC2_REG_STOP_CH3_BYTE2 0x18
104 #define TDC2_REG_STOP_CH3_BYTE1 0x19
105 #define TDC2_REG_INDEX_CH4_BYTE3 0x1A
106 #define TDC2_REG_INDEX_CH4_BYTE2 0x1B
107 #define TDC2_REG_INDEX_CH4_BYTE1 0x1C
108 #define TDC2_REG_STOP_CH4_BYTE3 0x1D
109 #define TDC2_REG_STOP_CH4_BYTE2 0x1E
110 #define TDC2_REG_STOP_CH4_BYTE1 0x1F
111  // tdc2_reg
113 
133 #define TDC2_SPIOPC_POWER 0x30
134 #define TDC2_SPIOPC_INIT 0x18
135 #define TDC2_SPIOPC_WRITE_CONFIG 0x80
136 #define TDC2_SPIOPC_READ_CONFIG 0x40
137 #define TDC2_SPIOPC_READ_RESULTS 0x60
138 #define TDC2_SPIOPC_READ_RESULTS 0x60
139 
144 #define TDC2_ENABLE_STOP1_PIN 0x01
145 #define TDC2_DISABLE_STOP1_PIN 0x00
146 #define TDC2_ENABLE_STOP2_PIN 0x02
147 #define TDC2_DISABLE_STOP2_PIN 0x00
148 #define TDC2_ENABLE_STOP3_PIN 0x04
149 #define TDC2_DISABLE_STOP3_PIN 0x00
150 #define TDC2_ENABLE_STOP4_PIN 0x08
151 #define TDC2_DISABLE_STOP4_PIN 0x00
152 #define TDC2_ENABLE_REFCLK_PIN 0x10
153 #define TDC2_DISABLE_REFCLK_PIN 0x00
154 #define TDC2_ENABLE_DISABLE_PIN 0x40
155 #define TDC2_DISABLE_DISABLE_PIN 0x00
156 #define TDC2_ENABLE_RSTINDX_PIN 0x80
157 #define TDC2_DISABLE_RSTINDX_PIN 0x00
158 
163 #define TDC2_HIT_ENABLE_STOP1 0x01
164 #define TDC2_HIT_DISABLE_STOP1 0x00
165 #define TDC2_HIT_ENABLE_STOP2 0x02
166 #define TDC2_HIT_DISABLE_STOP2 0x00
167 #define TDC2_HIT_ENABLE_STOP3 0x04
168 #define TDC2_HIT_DISABLE_STOP3 0x00
169 #define TDC2_HIT_ENABLE_STOP4 0x08
170 #define TDC2_HIT_DISABLE_STOP4 0x00
171 #define TDC2_CHANNEL_COMBINE_NORMAL 0x00
172 #define TDC2_CHANNEL_COMBINE_PULSE_DISTANCE 0x10
173 #define TDC2_CHANNEL_COMBINE_PULSE_WIDTH 0x20
174 #define TDC2_HIGH_RESOLUTION_OFF 0x00
175 #define TDC2_HIGH_RESOLUTION_X2 0x40
176 #define TDC2_HIGH_RESOLUTION_X4 0x80
177 
182 #define TDC2_FIFO_COMMON_READ_OFF 0x00
183 #define TDC2_FIFO_COMMON_READ_ON 0x40
184 #define TDC2_FIFO_BLOCKWISE_READ_OFF 0x00
185 #define TDC2_FIFO_BLOCKWISE_READ_ON 0x80
186 
191 #define TDC2_CFG6_FIXED_VALUE 0xC0
192 #define TDC2_CFG8_FIXED_VALUE 0xA1
193 #define TDC2_CFG9_FIXED_VALUE 0x13
194 #define TDC2_CFG10_FIXED_VALUE 0x00
195 #define TDC2_CFG11_FIXED_VALUE 0x0A
196 #define TDC2_CFG12_FIXED_VALUE 0xCC
197 #define TDC2_CFG13_FIXED_VALUE 0x05
198 #define TDC2_CFG14_FIXED_VALUE 0xF1
199 #define TDC2_CFG15_FIXED_VALUE 0x7D
200 #define TDC2_CFG16_FIXED_VALUE 0x04
201 
206 #define TDC2_CFG7_FIXED_VALUE 0x23
207 #define TDC2_REFERENCE_CLOCK_INTERNAL 0x80
208 #define TDC2_REFERENCE_CLOCK_EXTERNAL 0x00
209 
214 #define TDC2_1pS_RESOLUTION 125000
215 #define TDC2_5pS_RESOLUTION 62500
216 #define TDC2_10pS_RESOLUTION 12500
217 
218 #define TDC2_uS_TO_mS 1000000
219 
228 #define TDC2_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
229 #define TDC2_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
230  // tdc2_set
232 
247 #define TDC2_MAP_MIKROBUS( cfg, mikrobus ) \
248  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
249  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
250  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
251  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
252  cfg.rir = MIKROBUS( mikrobus, MIKROBUS_AN ); \
253  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
254  cfg.dis = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
255  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
256  // tdc2_map // tdc2
259 
264 typedef struct
265 {
266  // Output pins
267  digital_out_t rir;
268  digital_out_t rst;
269  digital_out_t dis;
271  // Input pins
272  digital_in_t int_pin;
274  // Modules
275  spi_master_t spi;
277  pin_name_t chip_select;
279 } tdc2_t;
280 
285 typedef struct
286 {
287  // Communication gpio pins
288  pin_name_t miso;
289  pin_name_t mosi;
290  pin_name_t sck;
291  pin_name_t cs;
293  // Additional gpio pins
294  pin_name_t rir;
295  pin_name_t rst;
296  pin_name_t dis;
297  pin_name_t int_pin;
299  // static variable
300  uint32_t spi_speed;
301  spi_master_mode_t spi_mode;
302  spi_master_chip_select_polarity_t cs_polarity;
304 } tdc2_cfg_t;
305 
310 typedef enum
311 {
312  TDC2_OK = 0,
313  TDC2_ERROR = -1
314 
316 
333 
347 err_t tdc2_init ( tdc2_t *ctx, tdc2_cfg_t *cfg );
348 
361 err_t tdc2_default_cfg ( tdc2_t *ctx );
362 
377 err_t tdc2_generic_write ( tdc2_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
378 
393 err_t tdc2_generic_read ( tdc2_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
394 
409 err_t tdc2_write_config ( tdc2_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
410 
425 err_t tdc2_read_config ( tdc2_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
426 
441 err_t tdc2_read_results( tdc2_t *ctx, uint8_t reg, uint32_t *reference_index, uint32_t *stop_result );
442 
455 err_t tdc2_send_command ( tdc2_t *ctx, uint8_t command );
456 
467 err_t tdc2_sw_reset ( tdc2_t *ctx );
468 
480 
492 err_t tdc2_set_resolution ( tdc2_t *ctx, uint32_t resolution );
493 
505 err_t tdc2_get_resolution ( tdc2_t *ctx, uint32_t *resolution );
506 
515 void tdc2_reset_index ( tdc2_t *ctx );
516 
526 
536 
546 uint8_t tdc2_get_int_state ( tdc2_t *ctx );
547 
562 err_t tdc2_get_time_between_stops ( tdc2_t *ctx, uint32_t stop_result1, uint32_t reference_index1,
563  uint32_t stop_result2, uint32_t reference_index2,
564  uint32_t *time_in_us );
565 
566 #ifdef __cplusplus
567 }
568 #endif
569 #endif // TDC2_H
570  // tdc2
572 
573 // ------------------------------------------------------------------------ END
tdc2_t
TDC 2 Click context object.
Definition: tdc2.h:265
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:275
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:272
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:269
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:313
tdc2_t::rir
digital_out_t rir
Definition: tdc2.h:267
tdc2_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: tdc2.h:301
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:311
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:296
tdc2_t::rst
digital_out_t rst
Definition: tdc2.h:268
tdc2_cfg_t::miso
pin_name_t miso
Definition: tdc2.h:288
tdc2_cfg_t::cs
pin_name_t cs
Definition: tdc2.h:291
tdc2_t::chip_select
pin_name_t chip_select
Definition: tdc2.h:277
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:294
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:295
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:289
tdc2_cfg_t::int_pin
pin_name_t int_pin
Definition: tdc2.h:297
tdc2_cfg_t::sck
pin_name_t sck
Definition: tdc2.h:290
TDC2_OK
@ TDC2_OK
Definition: tdc2.h:312
tdc2_cfg_t
TDC 2 Click configuration object.
Definition: tdc2.h:286
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:302
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:300
tdc2_sw_reset
err_t tdc2_sw_reset(tdc2_t *ctx)
TDC 2 software reset function.