1 /*
2  * Copyright (c) 2016-2018 Nordic Semiconductor ASA
3  * Copyright (c) 2016 Vinayak Kariappa Chettimada
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #define HAL_TICKER_CNTR_CLK_UNIT_FSEC 30517578125UL
9 #define HAL_TICKER_FSEC_PER_USEC      1000000000UL
10 #define HAL_TICKER_PSEC_PER_USEC      1000000UL
11 #define HAL_TICKER_FSEC_PER_PSEC      1000UL
12 
13 /* Macro defining the minimum counter compare offset */
14 #define HAL_TICKER_CNTR_CMP_OFFSET_MIN 2
15 
16 /* Macro defining the max. counter update latency in ticks */
17 #define HAL_TICKER_CNTR_SET_LATENCY 1
18 
19 /* Macro defines the h/w supported most significant bit */
20 #define HAL_TICKER_CNTR_MSBIT 31
21 
22 /* Macro defining the HW supported counter bits */
23 #define HAL_TICKER_CNTR_MASK 0xFFFFFFFF
24 
25 /*
26  * When the LPTMR is enabled, the first increment will take an additional
27  * one or two prescaler clock cycles due to synchronization logic.
28  */
29 
30 /* Macro to translate microseconds to tick units.
31  * NOTE: This returns the floor value.
32  */
33 #define HAL_TICKER_US_TO_TICKS(x) \
34 	( \
35 		((uint32_t)(((uint64_t) (x) * HAL_TICKER_FSEC_PER_USEC) / \
36 			    HAL_TICKER_CNTR_CLK_UNIT_FSEC)) & \
37 		HAL_TICKER_CNTR_MASK \
38 	)
39 
40 /* Macro to translate microseconds to tick units.
41  * NOTE: This returns the ceil value.
42  */
43 #define HAL_TICKER_US_TO_TICKS_CEIL(x) \
44 	( \
45 		DIV_ROUND_UP(((uint64_t) (x) * HAL_TICKER_FSEC_PER_USEC), \
46 			     HAL_TICKER_CNTR_CLK_UNIT_FSEC) & \
47 		HAL_TICKER_CNTR_MASK \
48 	)
49 
50 /* Macro to translate tick units to microseconds. */
51 #define HAL_TICKER_TICKS_TO_US(x) \
52 	( \
53 		((uint32_t)(((uint64_t)(x) * HAL_TICKER_CNTR_CLK_UNIT_FSEC) / \
54 		 HAL_TICKER_FSEC_PER_USEC)) \
55 	)
56 
57 /* Macro returning remainder in picoseconds (to fit in 32-bits) */
58 #define HAL_TICKER_REMAINDER(x) \
59 	( \
60 		( \
61 			((uint64_t) (x) * HAL_TICKER_FSEC_PER_USEC) \
62 			- ((uint64_t)HAL_TICKER_US_TO_TICKS(x) * \
63 			   HAL_TICKER_CNTR_CLK_UNIT_FSEC) \
64 		) \
65 		/ HAL_TICKER_FSEC_PER_PSEC \
66 	)
67 
68 /* Macro defining the remainder resolution/range
69  * ~ 1000000 * HAL_TICKER_TICKS_TO_US(1)
70  */
71 #define HAL_TICKER_REMAINDER_RANGE \
72 	HAL_TICKER_TICKS_TO_US(HAL_TICKER_PSEC_PER_USEC)
73 
74 /* Macro defining the margin for positioning re-scheduled nodes */
75 #define HAL_TICKER_RESCHEDULE_MARGIN \
76 	HAL_TICKER_US_TO_TICKS(150)
77