• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://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,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15  * Description: Provides rtc port \n
16  *
17  * History: \n
18  * 2023-03-02, Create file. \n
19  */
20 #ifndef RTC_PORTING_H
21 #define RTC_PORTING_H
22 
23 #include "tcxo_porting.h"
24 
25 #ifdef __cplusplus
26 #if __cplusplus
27 extern "C" {
28 #endif /* __cplusplus */
29 #endif /* __cplusplus */
30 
31 /**
32  * @defgroup drivers_port_rtc RTC
33  * @ingroup  drivers_port
34  * @{
35  */
36 
37 #define RTC_CLOCK_MAX_NUM    4
38 #define MS_PER_S                    1000
39 #define US_PER_MS                   1000
40 #define MAX_UINT32                  0xFFFFFFFF
41 #define MAX_UINT64                  0xFFFFFFFFFFFFFFFFULL
42 #define RTC_MAX_MS (MAX_UINT32 / CONFIG_RTC_CLOCK_VALUE * MS_PER_S - 1)
43 #define RTC_HW_MAX_MS (MAX_UINT32 / CONFIG_RTC_CLOCK_VALUE * MS_PER_S - 1)
44 #define RTC_1_CYCLE                 1
45 #define RTC_2_CYCLE                 2
46 #define RTC_COMPENSAT_1_CYCLE_BY_TCXO       (HAL_TCXO_TICKS_PER_SECOND / CONFIG_RTC_CLOCK_VALUE)
47 #define RTC_COMPENSAT_2_CYCLE_BY_TCXO       (2 * HAL_TCXO_TICKS_PER_SECOND / CONFIG_RTC_CLOCK_VALUE)
48 #define RTC_COMPENSAT_3_CYCLE_BY_TCXO       (3 * HAL_TCXO_TICKS_PER_SECOND / CONFIG_RTC_CLOCK_VALUE)
49 /**
50  * @if Eng
51  * @brief  Definiton of the index of rtcs.
52  * @else
53  * @brief  rtc索引的定义.
54  * @endif
55  */
56 typedef enum {
57     RTC_0,                            /*!< RTC0 index. */
58     RTC_1,                            /*!< RTC1 index. */
59     RTC_2,                            /*!< RTC2 index. */
60     RTC_3,                            /*!< RTC3 index. */
61     RTC_MAX_NUM = RTC_CLOCK_MAX_NUM
62 } rtc_index_t;
63 
64 /**
65  * @if Eng
66  * @brief  Get the address of the rtc common regs.
67  * @return The address of the rtc common regs.
68  * @else
69  * @brief  获取硬件定时器公共寄存器的基地址。
70  * @return 硬件定时器公共寄存器的基地址。
71  * @endif
72  */
73 uintptr_t rtc_porting_comm_addr_get(void);
74 
75 /**
76  * @if Eng
77  * @brief  Get the base address of a specified rtc.
78  * @param  [in]  index Index of the hardware rtc. For detail, see @ref rtc_index_t.
79  * @return The base address of specified rtc.
80  * @else
81  * @brief  获取指定rtc的基地址。
82  * @param  [in]  index 硬件定时器索引值,参考 @ref rtc_index_t 。
83  * @return 指定rtc的基地址。
84  * @endif
85  */
86 uintptr_t rtc_porting_base_addr_get(rtc_index_t index);
87 
88 /**
89  * @if Eng
90  * @brief  Register irq for rtc.
91  * @param  [in]  index Index of the hardware rtc. For detail, see @ref rtc_index_t.
92  * @param  [in]  id rtc interrupt id.
93  * @param  [in]  priority rtc interrupt priority.
94  * @else
95  * @brief  rtc注册中断。
96  * @param  [in]  index 硬件定时器索引值,参考 @ref rtc_index_t 。
97  * @param  [in]  id 定时器中断ID。
98  * @param  [in]  priority 定时器中断优先级。
99  * @endif
100  */
101 void rtc_port_register_irq(rtc_index_t index, uint32_t id, uint16_t priority);
102 
103 /**
104  * @if Eng
105  * @brief  Unregister irq for rtc.
106  * @param  [in]  index Index of the hardware rtc. For detail, see @ref rtc_index_t.
107  * @param  [in]  id rtc interrupt id.
108  * @else
109  * @brief  rtc去注册中断。
110  * @param  [in]  index 硬件定时器索引值,参考 @ref rtc_index_t 。
111  * @param  [in]  id 定时器中断ID。
112  * @endif
113  */
114 void rtc_port_unregister_irq(rtc_index_t index, uint32_t id);
115 
116 /**
117  * @if Eng
118  * @brief update the RTC's clock freq value for caculating, not really change the hardware clock freq.
119  * @param  [in]  clock clock freq value
120  * @retval void
121  * @else
122  * @brief  更新RTC的时钟频率值,此值应和真实输入时钟频率保持一致
123  * @param  [in]  clock 时钟频率
124  * @retval void
125  * @endif
126  */
127 void rtc_porting_clock_value_update(uint32_t clock);
128 
129 /**
130  * @if Eng
131  * @brief  get the RTC clock value.
132  * @retval uint32_t  the value set by @ref rtc_porting_clock_value_update
133  * @else
134  * @brief  获取RTC时钟频率
135  * @retval uint32_t  设置的时钟频率 @ref rtc_porting_clock_value_update
136  * @endif
137  */
138 uint32_t rtc_porting_clock_value_get(void);
139 
140 uint64_t rtc_porting_ms_2_cycle(uint32_t ms);
141 
142 uint64_t rtc_hw_porting_ms_2_cycle(uint64_t ms);
143 
144 uint32_t rtc_porting_cycle_2_us(uint64_t cycle);
145 
146 uint64_t rtc_porting_compensat_by_tcxo(uint64_t diff);
147 /**
148  * @}
149  */
150 
151 #ifdef __cplusplus
152 #if __cplusplus
153 }
154 #endif /* __cplusplus */
155 #endif /* __cplusplus */
156 
157 #endif