TimestampP.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022-2023, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
42 #ifndef ti_dpl_TimestampP__include
43 #define ti_dpl_TimestampP__include
44 
45 #include <stdint.h>
46 
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50 
51 #define TimestampP_Exponent_Seconds 0
52 #define TimestampP_Exponent_Miliseconds 3
53 #define TimestampP_Exponent_Microseconds 6
54 #define TimestampP_Exponent_Nanoseconds 9
55 
56 /* @brief Specification for parsing native device timestamp
57  *
58  * When provided with a native timestamp, however many bits, a peer device
59  * may perform the calculation:
60  *
61  * @code
62  * double fractional = (ts & ((1 << fracBytes*8) - 1)) / 2^fracBytes*8
63  * double integral = (ts >> fracBytes*8) & ((1 << intBytes*8) - 1)
64  * // if multiplier > 0:
65  * double time = abs(multiplier) * (integral + fractional) * 10^-exponent
66  * // if multiplier < 0:
67  * double time = 1/abs(multiplier) * (integral + fractional) * 10^-exponent
68  * @endcode
69  *
70  * For example, if the native format is a 32-bit wide fixed point fractional
71  * value with 16 bits for seconds and subseconds, the setting would be:
72  * fracBytes = 2, intBytes = 2, multiplier = 1, exponent = 0
73  *
74  * On the other hand if the native format is a 32-bit wide integral value
75  * where each tick is worth 8 microseconds, you get this:
76  * fracBytes = 0, intBytes = 4, multiplier = 8, exponent = 6
77  *
78  * If the native format is a 48-bit wide 32786 Hz counter where each
79  * tick is worth 30.5175 etc us, the setting would be:
80  * fracBytes = 0, intBytes = 6, multiplier = -32768, exponent = 0
81  *
82  * It is expected that if there is a fractional part, it is in the LSB end.
83  */
84 typedef union TimestampP_Format
85 {
86  struct
87  {
88  uint32_t fracBytes:4; //<! Octets (LSB) used for fractional part (if any)
89  uint32_t intBytes:4; //<! Octets (MSB) used for integer part
90  uint32_t exponent:8; //<! How much to scale native time to get seconds.
91  int32_t multiplier:16; //<! Signed 16-bit multiplier, eg 8 if one tick is 8 time units
92  } format;
93  uint32_t value;
95 
96 extern uint32_t TimestampP_getNative32(void);
97 extern uint64_t TimestampP_getNative64(void);
100 
101 #ifdef __cplusplus
102 }
103 #endif
104 
105 #endif /* ti_dpl_TimestampP__include */
union TimestampP_Format TimestampP_Format
uint32_t intBytes
Definition: TimestampP.h:89
const TimestampP_Format TimestampP_nativeFormat32
const TimestampP_Format TimestampP_nativeFormat64
Definition: TimestampP.h:84
int32_t multiplier
Definition: TimestampP.h:91
uint32_t TimestampP_getNative32(void)
struct TimestampP_Format::@1 format
uint32_t exponent
Definition: TimestampP.h:90
uint64_t TimestampP_getNative64(void)
uint32_t value
Definition: TimestampP.h:93
uint32_t fracBytes
Definition: TimestampP.h:88
© Copyright 1995-2024, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale