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
32extern "C"{
33#endif
34
39#ifdef PREINIT_SUPPORTED
40#include "preinit.h"
41#endif
42
43#ifdef MikroCCoreVersion
44 #if MikroCCoreVersion >= 1
45 #include "delays.h"
46 #endif
47#endif
48
49#include "drv_digital_out.h"
50#include "drv_digital_in.h"
51#include "drv_spi_master.h"
52
73#define TDC_REG_ADR_CONFIG1 0x00
74#define TDC_REG_ADR_CONFIG2 0x01
75#define TDC_REG_ADR_INT_STATUS 0x02
76#define TDC_REG_ADR_INT_MASK 0x03
77#define TDC_REG_ADR_COARSE_CNTR_OVF_H 0x04
78#define TDC_REG_ADR_COARSE_CNTR_OVF_L 0x05
79#define TDC_REG_ADR_CLOCK_CNTR_OVF_H 0x06
80#define TDC_REG_ADR_CLOCK_CNTR_OVF_L 0x07
81#define TDC_REG_ADR_CLOCK_CNTR_STOP_MASK_H 0x08
82#define TDC_REG_ADR_CLOCK_CNTR_STOP_MASK_L 0x09
83#define TDC_REG_ADR_TIME1 0x10
84#define TDC_REG_ADR_CLOCK_COUNT1 0x11
85#define TDC_REG_ADR_TIME2 0x12
86#define TDC_REG_ADR_CLOCK_COUNT2 0x13
87#define TDC_REG_ADR_TIME3 0x14
88#define TDC_REG_ADR_CLOCK_COUNT3 0x15
89#define TDC_REG_ADR_TIME4 0x16
90#define TDC_REG_ADR_CLOCK_COUNT4 0x17
91#define TDC_REG_ADR_TIME5 0x18
92#define TDC_REG_ADR_CLOCK_COUNT5 0x19
93#define TDC_REG_ADR_CLOCK_COUNTX(num) (TDC_REG_ADR_CLOCK_COUNT1+2*((num)-1))
94#define TDC_REG_ADR_TIME6 0x1A
95#define TDC_REG_ADR_TIMEX(num) (TDC_REG_ADR_TIME1+2*((num)-1))
96#define TDC_REG_ADR_CALIBRATION1 0x1B
97#define TDC_REG_ADR_CALIBRATION2 0x1C
98#define TDC_REG_DEFAULTS_CONFIG2 0x40
99#define TDC_REG_DEFAULTS_INT_MASK 0x07
100
101#define TDC_REG_SHIFT_CONFIG1_FORCE_CAL 7
102#define TDC_REG_SHIFT_CONFIG1_PARITY_EN 6
103#define TDC_REG_SHIFT_CONFIG1_TRIGG_EDGE 5
104#define TDC_REG_SHIFT_CONFIG1_STOP_EDGE 4
105#define TDC_REG_SHIFT_CONFIG1_START_EDGE 3
106#define TDC_REG_SHIFT_CONFIG1_MEAS_MODE 1
107#define TDC_REG_SHIFT_CONFIG1_START_MEAS 0
108
109#define TDC_REG_VAL_CONFIG1_MEAS_MODE_MIN 1
110#define TDC_REG_VAL_CONFIG1_MEAS_MODE_MAX 2
111#define TDC_REG_VAL_CONFIG1_MEAS_MODE(num) ((num)-1)
112
113
114#define TDC_REG_SHIFT_CONFIG2_CALIBRATION2_PERIODS 6
115#define TDC_REG_SHIFT_CONFIG2_AVG_CYCLES 3
116#define TDC_REG_SHIFT_CONFIG2_NUM_STOP 0
117
118#define TDC_REG_VAL_CONFIG2_CALIBRATION2_PERIODS_2 0
119#define TDC_REG_VAL_CONFIG2_CALIBRATION2_PERIODS_10 1
120#define TDC_REG_VAL_CONFIG2_CALIBRATION2_PERIODS_20 2
121#define TDC_REG_VAL_CONFIG2_CALIBRATION2_PERIODS_40 3
122
123#define TDC_REG_VAL_CONFIG2_AVG_CYCLES_MIN_VAL 0
124#define TDC_REG_VAL_CONFIG2_AVG_CYCLES_MIN 1 << TDC_REG_VAL_CONFIG2_AVG_CYCLES_MIN_VAL
125#define TDC_REG_VAL_CONFIG2_AVG_CYCLES_MAX_VAL 7
126#define TDC_REG_VAL_CONFIG2_AVG_CYCLES_MAX 1 << TDC_REG_VAL_CONFIG2_AVG_CYCLES_MAX_VAL
127
128#define TDC_REG_VAL_CONFIG2_NUM_STOP(num) ((num)-1)
129#define TDC_REG_VAL_CONFIG2_NUM_STOP_MAX 5
130
131#define TDC_REG_SHIFT_INT_STATUS_MEAS_COMPLETE_FLAG 4
132#define TDC_REG_SHIFT_INT_STATUS_MEAS_STARTED_FLAG 3
133#define TDC_REG_SHIFT_INT_STATUS_CLOCK_CNTR_OVF_INT 2
134#define TDC_REG_SHIFT_INT_STATUS_COARSE_CNTR_OVF_INT 1
135#define TDC_REG_SHIFT_INT_STATUS_NEW_MEAS_INT 0
136
137#define TDC_REG_SHIFT_INT_MASK_CLOCK_CNTR_OVF_MASK 2
138#define TDC_REG_SHIFT_INT_MASK_COARSE_CNTR_OVF_MASK 1
139#define TDC_REG_SHIFT_INT_MASK_NEW_MEAS_MASK 0
140
141 // tdc_reg
142
157#define TDC_CLOCK_FREQ_HZ 8000000
158#define TDC_RING_OSC_FREQ_KHZ 8000
159#define TDC_RING_OSC_FREQ_MHZ TDC_RING_OSC_FREQ_KHZ/1000
160#define PS_PER_K_SEC 1000000000
161#define US_PER_K_SEC 1000000
162#define PS_PER_PIC32_SEC 878000
163#define PS_PER_PIC_SEC 160000
164#define TDC_CLOCK_FREQ_HZ_PS_PER_PIC_SEC 0.05
165#define TDC_CLOCK_FREQ_HZ_PS_PER_ARM_SEC 14.745
166
167#define TDC_MCU_CLOCK_MODE_168_MHZ 0
168#define TDC_MCU_CLOCK_MODE_80_MHZ 1
169#define TDC_MCU_CLOCK_MODE_64_MHZ 2
170
171 // tdc_set
172
187#define TDC_MAP_MIKROBUS( cfg, mikrobus ) \
188 cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
189 cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
190 cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
191 cfg.oen = MIKROBUS( mikrobus, MIKROBUS_AN ); \
192 cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
193 cfg.en = MIKROBUS( mikrobus, MIKROBUS_RST ); \
194 cfg.trg = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
195 cfg.stop = MIKROBUS( mikrobus, MIKROBUS_CS ); \
196 cfg.start = MIKROBUS( mikrobus, MIKROBUS_RST ); \
197 cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
198
199 // tdc_map
200 // tdc
201
206typedef struct
207{
208 // Output pins
209 digital_out_t oen;
210 digital_out_t en;
211 digital_out_t stop;
212 digital_out_t start;
214 // Input pins
215 digital_in_t trg;
216 digital_in_t int_pin;
218 // Modules
219 spi_master_t spi;
221 pin_name_t chip_select;
223} tdc_t;
224
229typedef struct
230{
231 // Communication gpio pins
232 pin_name_t miso;
233 pin_name_t mosi;
234 pin_name_t sck;
235 pin_name_t cs;
237 // Additional gpio pins
238 pin_name_t oen;
239 pin_name_t en;
240 pin_name_t trg;
241 pin_name_t stop;
242 pin_name_t start;
243 pin_name_t int_pin;
245 // static variable
246 uint32_t spi_speed;
247 spi_master_mode_t spi_mode;
248 spi_master_chip_select_polarity_t cs_polarity;
250} tdc_cfg_t;
251
256typedef enum
257{
259 TDC_ERROR = -1
260
262
279
294err_t tdc_init ( tdc_t *ctx, tdc_cfg_t *cfg );
295
309err_t tdc_default_cfg ( tdc_t *ctx );
310
326err_t tdc_generic_write ( tdc_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
327
343err_t tdc_generic_read ( tdc_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
344
359err_t tdc_generic_write_byte ( tdc_t *ctx, uint8_t reg, uint8_t data_in );
360
375err_t tdc_generic_read_byte ( tdc_t *ctx, uint8_t reg, uint8_t *data_out );
376
388void tdc_gen_pulse ( tdc_t *ctx, uint32_t u_sec, uint8_t n_stops );
389
402
415void tdc_setup_overflow ( tdc_t *ctx, uint32_t overflow_ps );
416
434err_t tdc_setup_measurement ( tdc_t *ctx, uint8_t cal_periods, uint8_t avg_cycles, uint8_t num_stops, uint8_t mode );
435
454err_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 );
455
467uint8_t tdc_get_interrupt ( tdc_t *ctx );
468
480uint8_t tdc_get_trg ( tdc_t *ctx );
481
482#ifdef __cplusplus
483}
484#endif
485#endif // TDC_H
486
487 // tdc
488
489// ------------------------------------------------------------------------ END
err_t tdc_generic_write(tdc_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
TDC data writing function.
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.
uint8_t tdc_get_trg(tdc_t *ctx)
TDC get trigger signal function.
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.
err_t tdc_generic_write_byte(tdc_t *ctx, uint8_t reg, uint8_t data_in)
TDC data write byte function.
err_t tdc_generic_read_byte(tdc_t *ctx, uint8_t reg, uint8_t *data_out)
TDC data read function.
err_t tdc_init(tdc_t *ctx, tdc_cfg_t *cfg)
TDC initialization function.
err_t tdc_generic_read(tdc_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
TDC data reading function.
void tdc_cfg_setup(tdc_cfg_t *cfg)
TDC configuration object setup function.
void tdc_setup_overflow(tdc_t *ctx, uint32_t overflow_ps)
TDC setup overflow function.
void tdc_start_measurement(tdc_t *ctx)
TDC start measurement function.
uint8_t tdc_get_interrupt(tdc_t *ctx)
TDC get interrupt function.
void tdc_gen_pulse(tdc_t *ctx, uint32_t u_sec, uint8_t n_stops)
TDC pulse generation function.
err_t tdc_default_cfg(tdc_t *ctx)
TDC default configuration function.
TDC Click configuration object.
Definition tdc.h:230
spi_master_chip_select_polarity_t cs_polarity
Definition tdc.h:248
pin_name_t sck
Definition tdc.h:234
spi_master_mode_t spi_mode
Definition tdc.h:247
pin_name_t mosi
Definition tdc.h:233
uint32_t spi_speed
Definition tdc.h:246
pin_name_t start
Definition tdc.h:242
pin_name_t en
Definition tdc.h:239
pin_name_t int_pin
Definition tdc.h:243
pin_name_t trg
Definition tdc.h:240
pin_name_t miso
Definition tdc.h:232
pin_name_t oen
Definition tdc.h:238
pin_name_t stop
Definition tdc.h:241
pin_name_t cs
Definition tdc.h:235
TDC Click context object.
Definition tdc.h:207
digital_in_t trg
Definition tdc.h:215
spi_master_t spi
Definition tdc.h:219
digital_out_t start
Definition tdc.h:212
digital_out_t stop
Definition tdc.h:211
digital_out_t oen
Definition tdc.h:209
digital_in_t int_pin
Definition tdc.h:216
digital_out_t en
Definition tdc.h:210
pin_name_t chip_select
Definition tdc.h:221
tdc_return_value_t
TDC Click return value data.
Definition tdc.h:257
@ TDC_OK
Definition tdc.h:258
@ TDC_ERROR
Definition tdc.h:259