1 // Copyright (C) 2022 Beken Corporation 2 // 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 #pragma once 16 17 #include <driver/aon_rtc_types.h> 18 19 #ifdef __cplusplus 20 extern "C" { 21 #endif 22 23 /** 24 * @brief Init the AON_RTC driver 25 * 26 * This API init the resoure common: 27 * - Init AON_RTC driver control memory 28 * - Register ISR to NVIC/INTC/PLIC... 29 * 30 * @attention 1. This API should be called before any other AON_RTC APIs. 31 * 32 * @return 33 * - BK_OK: succeed 34 * - others: other errors. 35 */ 36 bk_err_t bk_aon_rtc_driver_init(void); 37 38 /** 39 * @brief Denit the AON_RTC driver 40 * 41 * This API deinit the resoure common: 42 * - Deinit AON_RTC driver control memory 43 * - Deinit AON RTC HW:Disable AON RTC, unregister isr handler 44 * 45 * @attention 1. This API should be called after any other AON_RTC APIs. 46 * 47 * @return 48 * - BK_OK: succeed 49 * - others: other errors. 50 */ 51 bk_err_t bk_aon_rtc_driver_deinit(void); 52 53 //remove it, only one HW can't be used for many APPs. 54 #if (CONFIG_AON_RTC && (!CONFIG_AON_RTC_MANY_USERS)) 55 /** 56 * @brief Create a request to use the selected AON RTC timer. 57 * For period timer, every tick will come an isr, 58 * the caller should call bk_aon_rtc_destroy if it won't use it. 59 * For once timer, only come one isr, after the isr finish, 60 * the timer will be destoried by driver layer, the caller no needs to destroy it. 61 * 62 * This API init the resoure common: 63 * - Init AON_RTC driver control memory 64 * - Enable the AON RTC HW, and set the AON RTC counter to run. 65 * 66 * @param id which AON RTC will be selected. 67 * @param tick The tick value for the AON RTC timer, it will be set to AON RTC. 68 * @param isr The counter runs to setted value(tick), will call this isr 69 * @param p_isr_param Interaction with APP and Driver layer 70 * @param period Once timer or period timer 71 * 72 * @attention 1. This API should be called after bk_aon_rtc_driver_init. 73 * Driver layer will call bk_aon_rtc_driver_init, 74 * but can't guarantee APP call this function after driver inited. 75 * 76 * @return 77 * - BK_OK: succeed 78 * - others: other errors. 79 */ 80 bk_err_t bk_aon_rtc_create(aon_rtc_id_t id, uint32_t tick, bool period); 81 82 /** 83 * @brief Dstory the requested AON RTC period timer which is created by the caller. 84 * 85 * This API destory the resoure common: 86 * - Clear AON_RTC driver control memory. 87 * - Disable the AON RTC HW, and set the AON RTC HW to default status. 88 * 89 * @attention 1. This API should be called after bk_aon_rtc_driver_init and bk_aon_rtc_create. 90 * 91 * @return 92 * - BK_OK: succeed 93 * - others: other errors. 94 */ 95 bk_err_t bk_aon_rtc_destroy(aon_rtc_id_t id); 96 97 /** 98 * @brief Register the tick interrupt service routine for AON_RTC id 99 * 100 * @param id aon_rtc id 101 * @param isr AON_RTC tick callback 102 * @param param AON_RTC tick callback parameter 103 * 104 * @return 105 * - BK_OK: succeed 106 * - others: other errors. 107 */ 108 bk_err_t bk_aon_rtc_register_tick_isr(aon_rtc_id_t id, aon_rtc_isr_t isr, void *param); 109 110 /** 111 * @brief Register the upper interrupt service routine for AON_RTC id 112 * 113 * @param id aon_rtc id 114 * @param isr AON_RTC upper callback 115 * @param param AON_RTC upper callback parameter 116 * 117 * @return 118 * - BK_OK: succeed 119 * - others: other errors. 120 */ 121 bk_err_t bk_aon_rtc_register_upper_isr(aon_rtc_id_t id, aon_rtc_isr_t isr, void *param); 122 #endif 123 124 /** 125 * @brief Register an alarm to AON_RTC id timer 126 * !!! NOTES: the callback function is in ISR, so can't block too much time. F.E:can't call sleep/suspend; !!! 127 * !!! NOTES: freertos forbid call free/malloc in ISR which include suspend and resume. !!! 128 * @id: register to which aon_rtc id 129 * @param alarm_info_p includes below info: 130 * name_p: the name of the registered alarm 131 * period_time: what's the alarm time, milliseconds * AON_RTC_MS_TICK_CNT 132 * period_cnt: 0xFFFFFFF means always period until user unregister it, or means period how many times 133 * callback: if the alarm comes, will call this callback 134 * param_p: the param will be used for callback 135 * 136 * @return 137 * - BK_OK: succeed 138 * - others: other errors. 139 */ 140 bk_err_t bk_alarm_register(aon_rtc_id_t id, alarm_info_t *alarm_info_p); 141 142 /** 143 * @brief Unregister an alarm from AON_RTC id timer 144 * 145 * @attention 1. Please don't unregister self in the alarm's callback. 146 * 147 * @param id: unregister the aon_rtc id alarm 148 * @param name_p: the name of the registered alarm which will be unregistered. 149 * 150 * @return 151 * - BK_OK: succeed 152 * - others: other errors. 153 */ 154 bk_err_t bk_alarm_unregister(aon_rtc_id_t id, uint8_t *name_p); 155 156 #if (CONFIG_AON_RTC && (!CONFIG_AON_RTC_MANY_USERS)) 157 /** 158 * @brief Register the rtc tick init 159 * 160 * @param param none 161 * 162 * @return 163 * - BK_OK: succeed 164 * - others: other errors. 165 */ 166 bk_err_t bk_aon_rtc_tick_init(); 167 168 /** 169 * @brief Register rtc wakeup 170 * @param period: wake up timer 171 * 172 * @return 173 * - BK_OK: succeed 174 * - others: other errors. 175 */ 176 bk_err_t bk_aon_rtc_open_rtc_wakeup(uint32_t period); 177 #endif 178 179 /** 180 * @brief Get AON RTC current tick with 64 bits 181 * AON RTC uses 32 Bits counter with 32K clock, the max time is about 36.4 hours. 182 * Set upper interrupt as the 0xFFFFFFFF ticks as one round. 183 * @id: register to which aon_rtc id 184 * @return 185 * - BK_OK: succeed 186 * - others: other errors. 187 */ 188 uint64_t bk_aon_rtc_get_current_tick(aon_rtc_id_t id); 189 190 #if AON_RTC_DEBUG 191 /** 192 * @brief test rtc get/set tick(round *cycles count) consume time. 193 * 194 * @id: register to which aon_rtc id 195 * @round: 196 * @cycles: 197 * 198 * @return 199 * - BK_OK: succeed 200 * - others: other errors. 201 */ 202 void bk_aon_rtc_timing_test(aon_rtc_id_t id, uint32_t round, uint32_t cycles, uint32_t set_tick); 203 #endif 204 205 /** 206 * @brief dump aon rtc debug info 207 * 208 * @id: which aon_rtc id 209 * 210 * @return 211 * - BK_OK: succeed 212 * - others: other errors. 213 */ 214 void bk_aon_rtc_dump(aon_rtc_id_t id); 215 216 217 /** 218 * @} 219 */ 220 221 #ifdef __cplusplus 222 } 223 #endif 224 225 226 227