|
30 | 30 |
|
31 | 31 | #include "gd32.h" |
32 | 32 |
|
33 | | -namespace gd32 { |
34 | | -namespace ptp { |
| 33 | +namespace gd32 |
| 34 | +{ |
| 35 | +namespace ptp |
| 36 | +{ |
35 | 37 | #if !defined(MCU_CLOCK_FREQ) |
36 | | -# error MCU_CLOCK_FREQ is not defined |
| 38 | +#error MCU_CLOCK_FREQ is not defined |
37 | 39 | #endif |
38 | 40 | #if !defined(PTP_ACCARACY_NS) |
39 | | - static constexpr uint8_t PTP_TICK = 20; |
| 41 | +static constexpr uint8_t PTP_TICK = 20; |
40 | 42 | #else |
41 | | - static constexpr uint8_t PTP_TICK = PTP_ACCARACY_NS; |
| 43 | +static constexpr uint8_t PTP_TICK = PTP_ACCARACY_NS; |
42 | 44 | #endif |
43 | | -static constexpr uint8_t ADJ_FREQ_BASE_INCREMENT = static_cast<uint8_t>((PTP_TICK * static_cast<uint64_t>(1ULL << 31) / 1E9) + 0.5f); |
44 | | -static constexpr uint32_t ADJ_FREQ_BASE_ADDEND = (static_cast<uint64_t>(1ULL << 63) / AHB_CLOCK_FREQ) / ADJ_FREQ_BASE_INCREMENT; |
45 | | -static constexpr int32_t ADJ_FREQ_MAX = 5120000; |
46 | | - |
47 | | -struct time_t { |
48 | | - int32_t tv_sec; |
49 | | - int32_t tv_nsec; |
| 45 | +inline constexpr uint8_t ADJ_FREQ_BASE_INCREMENT = static_cast<uint8_t>((PTP_TICK * static_cast<uint64_t>(1ULL << 31) / 1E9) + 0.5f); |
| 46 | +inline constexpr uint32_t ADJ_FREQ_BASE_ADDEND = (static_cast<uint64_t>(1ULL << 63) / AHB_CLOCK_FREQ) / ADJ_FREQ_BASE_INCREMENT; |
| 47 | +inline constexpr int32_t ADJ_FREQ_MAX = 5120000; |
| 48 | + |
| 49 | +struct time_t |
| 50 | +{ |
| 51 | + int32_t tv_sec; |
| 52 | + int32_t tv_nsec; |
50 | 53 | }; |
51 | 54 |
|
52 | | -struct ptptime { |
53 | | - uint32_t tv_sec; |
54 | | - uint32_t tv_nsec; |
| 55 | +struct ptptime |
| 56 | +{ |
| 57 | + uint32_t tv_sec; |
| 58 | + uint32_t tv_nsec; |
55 | 59 | }; |
56 | | -} // namespace ptp |
| 60 | +} // namespace ptp |
57 | 61 |
|
58 | | -inline uint32_t ptp_nanosecond_2_subsecond(const uint32_t nanosecond) { |
59 | | - uint64_t val = nanosecond * 0x80000000Ull; |
60 | | - val /= 1000000000U; |
61 | | - return static_cast<uint32_t>(val); |
| 62 | +inline uint32_t ptp_nanosecond_2_subsecond(const uint32_t nanosecond) |
| 63 | +{ |
| 64 | + uint64_t val = nanosecond * 0x80000000Ull; |
| 65 | + val /= 1000000000U; |
| 66 | + return static_cast<uint32_t>(val); |
62 | 67 | } |
63 | 68 |
|
64 | | -inline uint32_t ptp_subsecond_2_nanosecond(const uint32_t subsecond) { |
65 | | - uint64_t val = subsecond * 1000000000Ull; |
66 | | - val >>= 31U; |
67 | | - return (uint32_t) val; |
| 69 | +inline uint32_t ptp_subsecond_2_nanosecond(const uint32_t subsecond) |
| 70 | +{ |
| 71 | + uint64_t val = subsecond * 1000000000Ull; |
| 72 | + val >>= 31U; |
| 73 | + return (uint32_t)val; |
68 | 74 | } |
69 | 75 |
|
70 | | -inline void normalize_time(ptp::time_t *r) { |
71 | | - r->tv_sec += r->tv_nsec / 1000000000; |
72 | | - r->tv_nsec -= r->tv_nsec / 1000000000 * 1000000000; |
73 | | - |
74 | | - if (r->tv_sec > 0 && r->tv_nsec < 0) { |
75 | | - r->tv_sec -= 1; |
76 | | - r->tv_nsec += 1000000000; |
77 | | - } else if (r->tv_sec < 0 && r->tv_nsec > 0) { |
78 | | - r->tv_sec += 1; |
79 | | - r->tv_nsec -= 1000000000; |
80 | | - } |
| 76 | +inline void normalize_time(ptp::time_t* r) |
| 77 | +{ |
| 78 | + r->tv_sec += r->tv_nsec / 1000000000; |
| 79 | + r->tv_nsec -= r->tv_nsec / 1000000000 * 1000000000; |
| 80 | + |
| 81 | + if (r->tv_sec > 0 && r->tv_nsec < 0) |
| 82 | + { |
| 83 | + r->tv_sec -= 1; |
| 84 | + r->tv_nsec += 1000000000; |
| 85 | + } |
| 86 | + else if (r->tv_sec < 0 && r->tv_nsec > 0) |
| 87 | + { |
| 88 | + r->tv_sec += 1; |
| 89 | + r->tv_nsec -= 1000000000; |
| 90 | + } |
81 | 91 | } |
82 | 92 |
|
83 | | -inline void sub_time(struct ptp::time_t *r, const struct ptp::time_t *x, const struct ptp::time_t *y) { |
| 93 | +inline void sub_time(struct ptp::time_t* r, const struct ptp::time_t* x, const struct ptp::time_t* y) |
| 94 | +{ |
84 | 95 | r->tv_sec = x->tv_sec - y->tv_sec; |
85 | 96 | r->tv_nsec = x->tv_nsec - y->tv_nsec; |
86 | 97 |
|
87 | 98 | normalize_time(r); |
88 | 99 | } |
89 | 100 |
|
90 | | -} // namespace gd32 |
| 101 | +} // namespace gd32 |
91 | 102 |
|
92 | 103 | void gd32_ptp_start(); |
93 | | -void gd32_ptp_get_time(gd32::ptp::ptptime *ptp_time); |
94 | | -void gd32_ptp_set_time(const gd32::ptp::ptptime *ptp_time); |
95 | | -void gd32_ptp_update_time(const gd32::ptp::time_t *ptp_time); |
| 104 | +void gd32_ptp_get_time(gd32::ptp::ptptime* ptp_time); |
| 105 | +void gd32_ptp_set_time(const gd32::ptp::ptptime* ptp_time); |
| 106 | +void gd32_ptp_update_time(const gd32::ptp::time_t* ptp_time); |
96 | 107 | bool gd32_adj_frequency(const int32_t adjust_pbb); |
97 | 108 |
|
98 | 109 | #endif /* GD32_PTP_H_ */ |
0 commit comments