• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 #pragma once
15 
16 #define _PW_SYSTEM_CLOCK_DURATION(num_ticks) \
17   ((pw_chrono_SystemClock_Duration){.ticks = (num_ticks)})
18 
19 // clang-format off
20 
21 // ticks_ceil = ((count * clock_period_den + time_unit_num - 1) * time_unit_den) /
22 //              (clock_period_num * time_unit_num)
23 #define _PW_SYSTEM_CLOCK_TIME_TO_DURATION_CEIL(                                                                                                    \
24     count, time_unit_seconds_numerator, time_unit_seconds_denominator)                                                                             \
25   _PW_SYSTEM_CLOCK_DURATION(                                                                                                                       \
26       (((int64_t)(count) * PW_CHRONO_SYSTEM_CLOCK_PERIOD_SECONDS_DENOMINATOR + time_unit_seconds_numerator - 1) * time_unit_seconds_denominator) / \
27        (PW_CHRONO_SYSTEM_CLOCK_PERIOD_SECONDS_NUMERATOR * time_unit_seconds_numerator))
28 
29 // ticks_floor = (count * clock_period_den * time_unit_den) /
30 //               (clock_period_num * time_unit_num)
31 #define _PW_SYSTEM_CLOCK_TIME_TO_DURATION_FLOOR(                                                               \
32     count, time_unit_seconds_numerator, time_unit_seconds_denominator)                                         \
33   _PW_SYSTEM_CLOCK_DURATION(                                                                                   \
34       ((int64_t)(count) * PW_CHRONO_SYSTEM_CLOCK_PERIOD_SECONDS_DENOMINATOR * time_unit_seconds_denominator) / \
35       (PW_CHRONO_SYSTEM_CLOCK_PERIOD_SECONDS_NUMERATOR * time_unit_seconds_numerator))
36 
37 
38 #define PW_SYSTEM_CLOCK_MS_CEIL(milliseconds) \
39   _PW_SYSTEM_CLOCK_TIME_TO_DURATION_CEIL(milliseconds, 1000, 1)
40 #define PW_SYSTEM_CLOCK_S_CEIL(seconds) \
41   _PW_SYSTEM_CLOCK_TIME_TO_DURATION_CEIL(seconds,         1, 1)
42 #define PW_SYSTEM_CLOCK_MIN_CEIL(minutes) \
43   _PW_SYSTEM_CLOCK_TIME_TO_DURATION_CEIL(minutes,         1, 60)
44 #define PW_SYSTEM_CLOCK_H_CEIL(hours) \
45   _PW_SYSTEM_CLOCK_TIME_TO_DURATION_CEIL(hours,           1, 60 * 60)
46 
47 #define PW_SYSTEM_CLOCK_MS_FLOOR(milliseconds) \
48   _PW_SYSTEM_CLOCK_TIME_TO_DURATION_FLOOR(milliseconds, 1000, 1)
49 #define PW_SYSTEM_CLOCK_S_FLOOR(seconds) \
50   _PW_SYSTEM_CLOCK_TIME_TO_DURATION_FLOOR(seconds,         1, 1)
51 #define PW_SYSTEM_CLOCK_MIN_FLOOR(minutes) \
52   _PW_SYSTEM_CLOCK_TIME_TO_DURATION_FLOOR(minutes,         1, 60)
53 #define PW_SYSTEM_CLOCK_H_FLOOR(hours) \
54   _PW_SYSTEM_CLOCK_TIME_TO_DURATION_FLOOR(hours,           1, 60 * 60)
55 
56 // clang-format on
57 
58 #define PW_SYSTEM_CLOCK_MS(milliseconds) PW_SYSTEM_CLOCK_MS_CEIL(milliseconds)
59 #define PW_SYSTEM_CLOCK_S(seconds) PW_SYSTEM_CLOCK_S_CEIL(seconds)
60 #define PW_SYSTEM_CLOCK_MIN(minutes) PW_SYSTEM_CLOCK_MIN_CEIL(minutes)
61 #define PW_SYSTEM_CLOCK_H(hours) PW_SYSTEM_CLOCK_H_CEIL(hours)
62