tdc  2.0.0.0
tdc.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 TDC_H
29 #define TDC_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 
69 #define TDC_REG_ADR_CONFIG1 0x00
70 #define TDC_REG_ADR_CONFIG2 0x01
71 #define TDC_REG_ADR_INT_STATUS 0x02
72 #define TDC_REG_ADR_INT_MASK 0x03
73 #define TDC_REG_ADR_COARSE_CNTR_OVF_H 0x04
74 #define TDC_REG_ADR_COARSE_CNTR_OVF_L 0x05
75 #define TDC_REG_ADR_CLOCK_CNTR_OVF_H 0x06
76 #define TDC_REG_ADR_CLOCK_CNTR_OVF_L 0x07
77 #define TDC_REG_ADR_CLOCK_CNTR_STOP_MASK_H 0x08
78 #define TDC_REG_ADR_CLOCK_CNTR_STOP_MASK_L 0x09
79 #define TDC_REG_ADR_TIME1 0x10
80 #define TDC_REG_ADR_CLOCK_COUNT1 0x11
81 #define TDC_REG_ADR_TIME2 0x12
82 #define TDC_REG_ADR_CLOCK_COUNT2 0x13
83 #define TDC_REG_ADR_TIME3 0x14
84 #define TDC_REG_ADR_CLOCK_COUNT3 0x15
85 #define TDC_REG_ADR_TIME4 0x16
86 #define TDC_REG_ADR_CLOCK_COUNT4 0x17
87 #define TDC_REG_ADR_TIME5 0x18
88 #define TDC_REG_ADR_CLOCK_COUNT5 0x19
89 #define TDC_REG_ADR_CLOCK_COUNTX(num) (TDC_REG_ADR_CLOCK_COUNT1+2*((num)-1))
90 #define TDC_REG_ADR_TIME6 0x1A
91 #define TDC_REG_ADR_TIMEX(num) (TDC_REG_ADR_TIME1+2*((num)-1))
92 #define TDC_REG_ADR_CALIBRATION1 0x1B
93 #define TDC_REG_ADR_CALIBRATION2 0x1C
94 #define TDC_REG_DEFAULTS_CONFIG2 0x40
95 #define TDC_REG_DEFAULTS_INT_MASK 0x07
96 
97 #define TDC_REG_SHIFT_CONFIG1_FORCE_CAL 7
98 #define TDC_REG_SHIFT_CONFIG1_PARITY_EN 6
99 #define TDC_REG_SHIFT_CONFIG1_TRIGG_EDGE 5
100 #define TDC_REG_SHIFT_CONFIG1_STOP_EDGE 4
101 #define TDC_REG_SHIFT_CONFIG1_START_EDGE 3
102 #define TDC_REG_SHIFT_CONFIG1_MEAS_MODE 1
103 #define TDC_REG_SHIFT_CONFIG1_START_MEAS 0
104 
105 #define TDC_REG_VAL_CONFIG1_MEAS_MODE_MIN 1
106 #define TDC_REG_VAL_CONFIG1_MEAS_MODE_MAX 2
107 #define TDC_REG_VAL_CONFIG1_MEAS_MODE(num) ((num)-1)
108 
109 
110 #define TDC_REG_SHIFT_CONFIG2_CALIBRATION2_PERIODS 6
111 #define TDC_REG_SHIFT_CONFIG2_AVG_CYCLES 3
112 #define TDC_REG_SHIFT_CONFIG2_NUM_STOP 0
113 
114 #define TDC_REG_VAL_CONFIG2_CALIBRATION2_PERIODS_2 0
115 #define TDC_REG_VAL_CONFIG2_CALIBRATION2_PERIODS_10 1
116 #define TDC_REG_VAL_CONFIG2_CALIBRATION2_PERIODS_20 2
117 #define TDC_REG_VAL_CONFIG2_CALIBRATION2_PERIODS_40 3
118 
119 #define TDC_REG_VAL_CONFIG2_AVG_CYCLES_MIN_VAL 0
120 #define TDC_REG_VAL_CONFIG2_AVG_CYCLES_MIN 1 << TDC_REG_VAL_CONFIG2_AVG_CYCLES_MIN_VAL
121 #define TDC_REG_VAL_CONFIG2_AVG_CYCLES_MAX_VAL 7
122 #define TDC_REG_VAL_CONFIG2_AVG_CYCLES_MAX 1 << TDC_REG_VAL_CONFIG2_AVG_CYCLES_MAX_VAL
123 
124 #define TDC_REG_VAL_CONFIG2_NUM_STOP(num) ((num)-1)
125 #define TDC_REG_VAL_CONFIG2_NUM_STOP_MAX 5
126 
127 #define TDC_REG_SHIFT_INT_STATUS_MEAS_COMPLETE_FLAG 4
128 #define TDC_REG_SHIFT_INT_STATUS_MEAS_STARTED_FLAG 3
129 #define TDC_REG_SHIFT_INT_STATUS_CLOCK_CNTR_OVF_INT 2
130 #define TDC_REG_SHIFT_INT_STATUS_COARSE_CNTR_OVF_INT 1
131 #define TDC_REG_SHIFT_INT_STATUS_NEW_MEAS_INT 0
132 
133 #define TDC_REG_SHIFT_INT_MASK_CLOCK_CNTR_OVF_MASK 2
134 #define TDC_REG_SHIFT_INT_MASK_COARSE_CNTR_OVF_MASK 1
135 #define TDC_REG_SHIFT_INT_MASK_NEW_MEAS_MASK 0
136  // tdc_reg
138 
153 #define TDC_CLOCK_FREQ_HZ 8000000
154 #define TDC_RING_OSC_FREQ_KHZ 8000
155 #define TDC_RING_OSC_FREQ_MHZ TDC_RING_OSC_FREQ_KHZ/1000
156 #define PS_PER_K_SEC 1000000000
157 #define US_PER_K_SEC 1000000
158 #define PS_PER_PIC32_SEC 878000
159 #define PS_PER_PIC_SEC 160000
160 #define TDC_CLOCK_FREQ_HZ_PS_PER_PIC_SEC 0.05
161 #define TDC_CLOCK_FREQ_HZ_PS_PER_ARM_SEC 14.745
162 
163 #define TDC_MCU_CLOCK_MODE_168_MHZ 0
164 #define TDC_MCU_CLOCK_MODE_80_MHZ 1
165 #define TDC_MCU_CLOCK_MODE_64_MHZ 2
166  // tdc_set
168 
183 #define TDC_MAP_MIKROBUS( cfg, mikrobus ) \
184  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
185  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
186  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
187  cfg.oen = MIKROBUS( mikrobus, MIKROBUS_AN ); \
188  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
189  cfg.en = MIKROBUS( mikrobus, MIKROBUS_RST ); \
190  cfg.trg = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
191  cfg.stop = MIKROBUS( mikrobus, MIKROBUS_CS ); \
192  cfg.start = MIKROBUS( mikrobus, MIKROBUS_RST ); \
193  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
194  // tdc_map // tdc
197 
202 typedef struct
203 {
204  // Output pins
205  digital_out_t oen;
206  digital_out_t en;
207  digital_out_t stop;
208  digital_out_t start;
210  // Input pins
211  digital_in_t trg;
212  digital_in_t int_pin;
214  // Modules
215  spi_master_t spi;
217  pin_name_t chip_select;
219 } tdc_t;
220 
225 typedef struct
226 {
227  // Communication gpio pins
228  pin_name_t miso;
229  pin_name_t mosi;
230  pin_name_t sck;
231  pin_name_t cs;
233  // Additional gpio pins
234  pin_name_t oen;
235  pin_name_t en;
236  pin_name_t trg;
237  pin_name_t stop;
238  pin_name_t start;
239  pin_name_t int_pin;
241  // static variable
242  uint32_t spi_speed;
243  spi_master_mode_t spi_mode;
244  spi_master_chip_select_polarity_t cs_polarity;
246 } tdc_cfg_t;
247 
252 typedef enum
253 {
254  TDC_OK = 0,
255  TDC_ERROR = -1
256 
258 
274 void tdc_cfg_setup ( tdc_cfg_t *cfg );
275 
290 err_t tdc_init ( tdc_t *ctx, tdc_cfg_t *cfg );
291 
305 err_t tdc_default_cfg ( tdc_t *ctx );
306 
322 err_t tdc_generic_write ( tdc_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
323 
339 err_t tdc_generic_read ( tdc_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
340 
355 err_t tdc_generic_write_byte ( tdc_t *ctx, uint8_t reg, uint8_t data_in );
356 
371 err_t tdc_generic_read_byte ( tdc_t *ctx, uint8_t reg, uint8_t *data_out );
372 
384 void tdc_gen_pulse ( tdc_t *ctx, uint32_t u_sec, uint8_t n_stops );
385 
398 
411 void tdc_setup_overflow ( tdc_t *ctx, uint32_t overflow_ps );
412 
430 err_t tdc_setup_measurement ( tdc_t *ctx, uint8_t cal_periods, uint8_t avg_cycles, uint8_t num_stops, uint8_t mode );
431 
450 err_t tdc_get_measurement ( tdc_t *ctx, uint8_t clock_mode, uint8_t measure_num_stop, uint32_t *time, uint32_t *clock_countn, uint32_t *tof );
451 
463 uint8_t tdc_get_interrupt ( tdc_t *ctx );
464 
476 uint8_t tdc_get_trg ( tdc_t *ctx );
477 
478 #ifdef __cplusplus
479 }
480 #endif
481 #endif // TDC_H
482  // tdc
484 
485 // ------------------------------------------------------------------------ END
tdc_get_interrupt
uint8_t tdc_get_interrupt(tdc_t *ctx)
TDC get interrupt function.
tdc_return_value_t
tdc_return_value_t
TDC Click return value data.
Definition: tdc.h:253
tdc_cfg_t::cs
pin_name_t cs
Definition: tdc.h:231
TDC_ERROR
@ TDC_ERROR
Definition: tdc.h:255
tdc_cfg_t::en
pin_name_t en
Definition: tdc.h:235
tdc_cfg_t::sck
pin_name_t sck
Definition: tdc.h:230
tdc_generic_write_byte
err_t tdc_generic_write_byte(tdc_t *ctx, uint8_t reg, uint8_t data_in)
TDC data write byte function.
tdc_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: tdc.h:244
tdc_generic_write
err_t tdc_generic_write(tdc_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
TDC data writing function.
tdc_t::start
digital_out_t start
Definition: tdc.h:208
tdc_cfg_t::int_pin
pin_name_t int_pin
Definition: tdc.h:239
tdc_t::en
digital_out_t en
Definition: tdc.h:206
tdc_cfg_t::mosi
pin_name_t mosi
Definition: tdc.h:229
tdc_t::int_pin
digital_in_t int_pin
Definition: tdc.h:212
tdc_cfg_t::stop
pin_name_t stop
Definition: tdc.h:237
tdc_t::chip_select
pin_name_t chip_select
Definition: tdc.h:217
tdc_generic_read_byte
err_t tdc_generic_read_byte(tdc_t *ctx, uint8_t reg, uint8_t *data_out)
TDC data read function.
tdc_t::oen
digital_out_t oen
Definition: tdc.h:205
tdc_generic_read
err_t tdc_generic_read(tdc_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
TDC data reading function.
tdc_cfg_t::miso
pin_name_t miso
Definition: tdc.h:228
tdc_cfg_t::trg
pin_name_t trg
Definition: tdc.h:236
tdc_setup_measurement
err_t tdc_setup_measurement(tdc_t *ctx, uint8_t cal_periods, uint8_t avg_cycles, uint8_t num_stops, uint8_t mode)
TDC setup measurement function.
tdc_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: tdc.h:243
tdc_get_trg
uint8_t tdc_get_trg(tdc_t *ctx)
TDC get trigger signal function.
tdc_t::spi
spi_master_t spi
Definition: tdc.h:215
tdc_t::trg
digital_in_t trg
Definition: tdc.h:211
tdc_cfg_t
TDC Click configuration object.
Definition: tdc.h:226
tdc_t::stop
digital_out_t stop
Definition: tdc.h:207
tdc_cfg_setup
void tdc_cfg_setup(tdc_cfg_t *cfg)
TDC configuration object setup function.
tdc_t
TDC Click context object.
Definition: tdc.h:203
TDC_OK
@ TDC_OK
Definition: tdc.h:254
tdc_init
err_t tdc_init(tdc_t *ctx, tdc_cfg_t *cfg)
TDC initialization function.
tdc_get_measurement
err_t tdc_get_measurement(tdc_t *ctx, uint8_t clock_mode, uint8_t measure_num_stop, uint32_t *time, uint32_t *clock_countn, uint32_t *tof)
TDC get measurement function.
tdc_cfg_t::spi_speed
uint32_t spi_speed
Definition: tdc.h:242
tdc_cfg_t::oen
pin_name_t oen
Definition: tdc.h:234
tdc_default_cfg
err_t tdc_default_cfg(tdc_t *ctx)
TDC default configuration function.
tdc_setup_overflow
void tdc_setup_overflow(tdc_t *ctx, uint32_t overflow_ps)
TDC setup overflow function.
tdc_start_measurement
void tdc_start_measurement(tdc_t *ctx)
TDC start measurement function.
tdc_cfg_t::start
pin_name_t start
Definition: tdc.h:238
tdc_gen_pulse
void tdc_gen_pulse(tdc_t *ctx, uint32_t u_sec, uint8_t n_stops)
TDC pulse generation function.