|
| 1 | +/* |
| 2 | +* Copyright (c) 2020 - 2025 Renesas Electronics Corporation and/or its affiliates |
| 3 | +* |
| 4 | +* SPDX-License-Identifier: BSD-3-Clause |
| 5 | +*/ |
| 6 | + |
| 7 | +#ifndef R_RTC_API_H |
| 8 | +#define R_RTC_API_H |
| 9 | + |
| 10 | +/*******************************************************************************************************************//** |
| 11 | + * @ingroup RENESAS_TIMERS_INTERFACES |
| 12 | + * @defgroup RTC_API RTC Interface |
| 13 | + * @brief Interface for accessing the Realtime Clock. |
| 14 | + * |
| 15 | + * |
| 16 | + * @section RTC_API_Summary Summary |
| 17 | + * The RTC Interface is for configuring Real Time Clock (RTC) functionality including alarm, periodic notification and |
| 18 | + * error adjustment. |
| 19 | + * |
| 20 | + * |
| 21 | + * @{ |
| 22 | + **********************************************************************************************************************/ |
| 23 | + |
| 24 | +/*********************************************************************************************************************** |
| 25 | + * Includes |
| 26 | + **********************************************************************************************************************/ |
| 27 | + |
| 28 | +/* Register definitions, common services and error codes. */ |
| 29 | +#include "bsp_api.h" |
| 30 | + |
| 31 | +/* Use of time structure, tm */ |
| 32 | +#include <time.h> |
| 33 | + |
| 34 | +/* Common macro for FSP header files. There is also a corresponding FSP_FOOTER macro at the end of this file. */ |
| 35 | +FSP_HEADER |
| 36 | + |
| 37 | +/********************************************************************************************************************** |
| 38 | + * Macro definitions |
| 39 | + **********************************************************************************************************************/ |
| 40 | + |
| 41 | +/********************************************************************************************************************** |
| 42 | + * Typedef definitions |
| 43 | + **********************************************************************************************************************/ |
| 44 | +#ifndef BSP_OVERRIDE_RTC_EVENT_T |
| 45 | + |
| 46 | +/** Events that can trigger a callback function */ |
| 47 | +typedef enum e_rtc_event |
| 48 | +{ |
| 49 | + RTC_EVENT_ALARM_IRQ, ///< Real Time Clock ALARM 0 IRQ |
| 50 | + RTC_EVENT_ALARM1_IRQ, ///< Real Time Clock ALARM 1 IRQ |
| 51 | + RTC_EVENT_PERIODIC_IRQ, ///< Real Time Clock PERIODIC IRQ |
| 52 | +} rtc_event_t; |
| 53 | +#endif |
| 54 | + |
| 55 | +/** RTC alarm channel */ |
| 56 | +typedef enum e_rtc_alarm_channel |
| 57 | +{ |
| 58 | + RTC_ALARM_CHANNEL_0, |
| 59 | + RTC_ALARM_CHANNEL_1, |
| 60 | +} rtc_alarm_channel_t; |
| 61 | + |
| 62 | +/** Callback function parameter data */ |
| 63 | +typedef struct st_rtc_callback_args |
| 64 | +{ |
| 65 | + rtc_event_t event; ///< The event can be used to identify what caused the callback (compare match or error). |
| 66 | + void * p_context; ///< Placeholder for user data. |
| 67 | +} rtc_callback_args_t; |
| 68 | + |
| 69 | +/** Clock source for the RTC block */ |
| 70 | +typedef enum e_rtc_count_source |
| 71 | +{ |
| 72 | + RTC_CLOCK_SOURCE_SUBCLK = 0, ///< Sub-clock oscillator |
| 73 | + RTC_CLOCK_SOURCE_LOCO = 1, ///< Low power On Chip Oscillator |
| 74 | + RTC_CLOCK_SOURCE_MAINCLK = 2 ///< Main clock oscillator |
| 75 | +} rtc_clock_source_t; |
| 76 | + |
| 77 | +/** RTC run state */ |
| 78 | +typedef enum e_rtc_status |
| 79 | +{ |
| 80 | + RTC_STATUS_STOPPED = 0, ///< RTC counter is stopped |
| 81 | + RTC_STATUS_RUNNING = 1 ///< RTC counter is running |
| 82 | +} rtc_status_t; |
| 83 | + |
| 84 | +/** Time error adjustment settings */ |
| 85 | +typedef enum e_rtc_error_adjustment |
| 86 | +{ |
| 87 | + RTC_ERROR_ADJUSTMENT_NONE = 0, ///< Adjustment is not performed |
| 88 | + RTC_ERROR_ADJUSTMENT_ADD_PRESCALER = 1, ///< Adjustment is performed by the addition to the prescaler |
| 89 | + RTC_ERROR_ADJUSTMENT_SUBTRACT_PRESCALER = 2, ///< Adjustment is performed by the subtraction from the prescaler |
| 90 | +} rtc_error_adjustment_t; |
| 91 | + |
| 92 | +/** Time error adjustment mode settings */ |
| 93 | +typedef enum e_rtc_error_adjustment_mode |
| 94 | +{ |
| 95 | + RTC_ERROR_ADJUSTMENT_MODE_MANUAL = 0, ///< Adjustment mode is set to manual |
| 96 | + RTC_ERROR_ADJUSTMENT_MODE_AUTOMATIC = 1, ///< Adjustment mode is set to automatic |
| 97 | +} rtc_error_adjustment_mode_t; |
| 98 | + |
| 99 | +/** Time error adjustment period settings */ |
| 100 | +typedef enum e_rtc_error_adjustment_period |
| 101 | +{ |
| 102 | + RTC_ERROR_ADJUSTMENT_PERIOD_1_MINUTE = 0, ///< Adjustment period is set to every one minute |
| 103 | + RTC_ERROR_ADJUSTMENT_PERIOD_10_SECOND = 1, ///< Adjustment period is set to every ten second |
| 104 | + RTC_ERROR_ADJUSTMENT_PERIOD_NONE = 2, ///< Adjustment period not supported in manual mode |
| 105 | + RTC_ERROR_ADJUSTMENT_PERIOD_20_SECOND = 3, ///< Adjustment period is set to every twenty seconds |
| 106 | +} rtc_error_adjustment_period_t; |
| 107 | + |
| 108 | +/** Time error adjustment value configuration */ |
| 109 | +typedef struct st_rtc_error_adjustment_cfg |
| 110 | +{ |
| 111 | + rtc_error_adjustment_mode_t adjustment_mode; ///< Automatic Adjustment Enable/Disable |
| 112 | + rtc_error_adjustment_period_t adjustment_period; ///< Error Adjustment period |
| 113 | + rtc_error_adjustment_t adjustment_type; ///< Time error adjustment setting |
| 114 | + uint32_t adjustment_value; ///< Value of the prescaler for error adjustment |
| 115 | +} rtc_error_adjustment_cfg_t; |
| 116 | + |
| 117 | +#ifndef BSP_OVERRIDE_RTC_PERIODIC_IRQ_SELECT_T |
| 118 | + |
| 119 | +/** Periodic Interrupt select */ |
| 120 | +typedef enum e_rtc_periodic_irq_select |
| 121 | +{ |
| 122 | + RTC_PERIODIC_IRQ_SELECT_1_DIV_BY_256_SECOND = 6, ///< A periodic irq is generated every 1/256 second |
| 123 | + RTC_PERIODIC_IRQ_SELECT_1_DIV_BY_128_SECOND = 7, ///< A periodic irq is generated every 1/128 second |
| 124 | + RTC_PERIODIC_IRQ_SELECT_1_DIV_BY_64_SECOND = 8, ///< A periodic irq is generated every 1/64 second |
| 125 | + RTC_PERIODIC_IRQ_SELECT_1_DIV_BY_32_SECOND = 9, ///< A periodic irq is generated every 1/32 second |
| 126 | + RTC_PERIODIC_IRQ_SELECT_1_DIV_BY_16_SECOND = 10, ///< A periodic irq is generated every 1/16 second |
| 127 | + RTC_PERIODIC_IRQ_SELECT_1_DIV_BY_8_SECOND = 11, ///< A periodic irq is generated every 1/8 second |
| 128 | + RTC_PERIODIC_IRQ_SELECT_1_DIV_BY_4_SECOND = 12, ///< A periodic irq is generated every 1/4 second |
| 129 | + RTC_PERIODIC_IRQ_SELECT_1_DIV_BY_2_SECOND = 13, ///< A periodic irq is generated every 1/2 second |
| 130 | + RTC_PERIODIC_IRQ_SELECT_1_SECOND = 14, ///< A periodic irq is generated every 1 second |
| 131 | + RTC_PERIODIC_IRQ_SELECT_2_SECOND = 15, ///< A periodic irq is generated every 2 seconds |
| 132 | + RTC_PERIODIC_IRQ_SELECT_1_MINUTE = 16, ///< A periodic irq is generated every 1 minute |
| 133 | + RTC_PERIODIC_IRQ_SELECT_1_HOUR = 17, ///< A periodic irq is generated every 1 hour |
| 134 | + RTC_PERIODIC_IRQ_SELECT_1_DAY = 18, ///< A periodic irq is generated every 1 day |
| 135 | + RTC_PERIODIC_IRQ_SELECT_1_MONTH = 19, ///< A periodic irq is generated every 1 month |
| 136 | +} rtc_periodic_irq_select_t; |
| 137 | +#endif |
| 138 | + |
| 139 | +#ifndef BSP_OVERRIDE_RTC_TIME_CAPTURE_SOURCE_T |
| 140 | + |
| 141 | +/** Time capture trigger source */ |
| 142 | +typedef enum e_rtc_time_capture_source |
| 143 | +{ |
| 144 | + RTC_TIME_CAPTURE_SOURCE_DISABLED = 0, ///< Disable trigger |
| 145 | + RTC_TIME_CAPTURE_SOURCE_PIN_RISING = 1, ///< Rising edge pin trigger |
| 146 | + RTC_TIME_CAPTURE_SOURCE_PIN_FALLING = 2, ///< Falling edge pin trigger |
| 147 | + RTC_TIME_CAPTURE_SOURCE_PIN_BOTH = 3, ///< Both edges pin trigger |
| 148 | + RTC_TIME_CAPTURE_SOURCE_SOFTWARE = 4, ///< Software trigger |
| 149 | + RTC_TIME_CAPTURE_SOURCE_ELC_EVENT = 5, ///< ELC event trigger |
| 150 | +} rtc_time_capture_source_t; |
| 151 | +#endif |
| 152 | + |
| 153 | +/** Time capture trigger mode */ |
| 154 | +typedef enum e_rtc_time_capture_mode |
| 155 | +{ |
| 156 | + RTC_TIME_CAPTURE_MODE_CONTINUOUS = 0, ///< Continuous capturing to all capturing channels |
| 157 | + RTC_TIME_CAPTURE_MODE_ONE_SHOT = 1, ///< Single capture to a particular channel |
| 158 | +} rtc_time_capture_mode_t; |
| 159 | + |
| 160 | +/** Time capture noise filter control */ |
| 161 | +typedef enum e_rtc_time_capture_noise_filter |
| 162 | +{ |
| 163 | + RTC_TIME_CAPTURE_NOISE_FILTER_OFF = 0, ///< Turn noise filter off |
| 164 | + RTC_TIME_CAPTURE_NOISE_FILTER_ON = 2, ///< Turn noise filter on (count source) |
| 165 | + RTC_TIME_CAPTURE_NOISE_FILTER_ON_DIVIDER_32 = 3, ///< Turn noise filter on (count source by divided by 32) |
| 166 | + RTC_TIME_CAPTURE_NOISE_FILTER_ON_DIVIDER_4096 = 4, ///< Turn noise filter on (count source by divided by 4096) |
| 167 | + RTC_TIME_CAPTURE_NOISE_FILTER_ON_DIVIDER_8192 = 5, ///< Turn noise filter on (count source by divided by 8192) |
| 168 | +} rtc_time_capture_noise_filter_t; |
| 169 | + |
| 170 | +/** Date and time structure defined in C standard library <time.h> */ |
| 171 | +typedef struct tm rtc_time_t; |
| 172 | + |
| 173 | +#ifndef BSP_OVERRIDE_RTC_ALARM_TIME_T |
| 174 | + |
| 175 | +/** Alarm time setting structure */ |
| 176 | +typedef struct st_rtc_alarm_time |
| 177 | +{ |
| 178 | + rtc_time_t time; ///< Time structure |
| 179 | + bool sec_match; ///< Enable the alarm based on a match of the seconds field |
| 180 | + bool min_match; ///< Enable the alarm based on a match of the minutes field |
| 181 | + bool hour_match; ///< Enable the alarm based on a match of the hours field |
| 182 | + bool mday_match; ///< Enable the alarm based on a match of the days field |
| 183 | + bool mon_match; ///< Enable the alarm based on a match of the months field |
| 184 | + bool year_match; ///< Enable the alarm based on a match of the years field |
| 185 | + bool dayofweek_match; ///< Enable the alarm based on a match of the dayofweek field |
| 186 | + bool sunday_match; ///< Enable the alarm on Sunday |
| 187 | + bool monday_match; ///< Enable the alarm on Monday |
| 188 | + bool tuesday_match; ///< Enable the alarm on Tuesday |
| 189 | + bool wednesday_match; ///< Enable the alarm on Wednesday |
| 190 | + bool thursday_match; ///< Enable the alarm on Thursday |
| 191 | + bool friday_match; ///< Enable the alarm on Friday |
| 192 | + bool saturday_match; ///< Enable the alarm on Saturday |
| 193 | + rtc_alarm_channel_t channel; ///< Select alarm 0 or alarm 1 |
| 194 | +} rtc_alarm_time_t; |
| 195 | +#endif |
| 196 | + |
| 197 | +/** Time capture configuration structure */ |
| 198 | +typedef struct st_rtc_time_capture |
| 199 | +{ |
| 200 | + rtc_time_t time; ///< Time structure |
| 201 | + uint8_t channel; ///< Capture channel |
| 202 | + rtc_time_capture_source_t source; ///< Trigger source |
| 203 | + rtc_time_capture_noise_filter_t noise_filter; ///< Noise filter |
| 204 | + rtc_time_capture_mode_t mode; ///< Capture mode |
| 205 | +} rtc_time_capture_t; |
| 206 | + |
| 207 | +/** RTC Information Structure for information returned by infoGet() */ |
| 208 | +typedef struct st_rtc_info |
| 209 | +{ |
| 210 | + rtc_clock_source_t clock_source; ///< Clock source for the RTC block |
| 211 | + rtc_status_t status; ///< RTC run status |
| 212 | +} rtc_info_t; |
| 213 | + |
| 214 | +/** User configuration structure, used in open function */ |
| 215 | +typedef struct st_rtc_cfg |
| 216 | +{ |
| 217 | + rtc_clock_source_t clock_source; ///< Clock source for the RTC block |
| 218 | + uint32_t freq_compare_value; ///< The frequency comparison value |
| 219 | + rtc_error_adjustment_cfg_t const * const p_err_cfg; ///< Pointer to Error Adjustment configuration |
| 220 | + uint8_t alarm_ipl; ///< Alarm interrupt priority |
| 221 | + IRQn_Type alarm_irq; ///< Alarm interrupt vector |
| 222 | + uint8_t periodic_ipl; ///< Periodic interrupt priority |
| 223 | + IRQn_Type periodic_irq; ///< Periodic interrupt vector |
| 224 | + uint8_t carry_ipl; ///< Carry interrupt priority |
| 225 | + IRQn_Type carry_irq; ///< Carry interrupt vector |
| 226 | + void (* p_callback)(rtc_callback_args_t * p_args); ///< Called from the ISR. |
| 227 | + void * p_context; ///< User defined context passed into callback function. |
| 228 | + void const * p_extend; ///< RTC hardware dependant configuration. |
| 229 | +} rtc_cfg_t; |
| 230 | + |
| 231 | +/** RTC control block. Allocate an instance specific control block to pass into the RTC API calls. |
| 232 | + */ |
| 233 | +typedef void rtc_ctrl_t; |
| 234 | + |
| 235 | +/** RTC driver structure. General RTC functions implemented at the HAL layer follow this API. */ |
| 236 | +typedef struct st_rtc_api |
| 237 | +{ |
| 238 | + /** Open the RTC driver. |
| 239 | + * |
| 240 | + * @param[in] p_ctrl Pointer to RTC device handle |
| 241 | + * @param[in] p_cfg Pointer to the configuration structure |
| 242 | + */ |
| 243 | + fsp_err_t (* open)(rtc_ctrl_t * const p_ctrl, rtc_cfg_t const * const p_cfg); |
| 244 | + |
| 245 | + /** Close the RTC driver. |
| 246 | + * |
| 247 | + * @param[in] p_ctrl Pointer to RTC device handle. |
| 248 | + */ |
| 249 | + fsp_err_t (* close)(rtc_ctrl_t * const p_ctrl); |
| 250 | + |
| 251 | + /** Sets the RTC clock source. |
| 252 | + * |
| 253 | + * @param[in] p_ctrl Pointer to RTC device handle |
| 254 | + */ |
| 255 | + fsp_err_t (* clockSourceSet)(rtc_ctrl_t * const p_ctrl); |
| 256 | + |
| 257 | + /** Set the calendar time and start the calendar counter |
| 258 | + * |
| 259 | + * @param[in] p_ctrl Pointer to RTC device handle |
| 260 | + * @param[in] p_time Pointer to a time structure that contains the time to set |
| 261 | + */ |
| 262 | + fsp_err_t (* calendarTimeSet)(rtc_ctrl_t * const p_ctrl, rtc_time_t * const p_time); |
| 263 | + |
| 264 | + /** Get the calendar time. |
| 265 | + * |
| 266 | + * @param[in] p_ctrl Pointer to RTC device handle |
| 267 | + * @param[out] p_time Pointer to a time structure that contains the time to get |
| 268 | + */ |
| 269 | + fsp_err_t (* calendarTimeGet)(rtc_ctrl_t * const p_ctrl, rtc_time_t * const p_time); |
| 270 | + |
| 271 | + /** Set the calendar alarm time and enable the alarm interrupt. |
| 272 | + * |
| 273 | + * @param[in] p_ctrl Pointer to RTC device handle |
| 274 | + * @param[in] p_alarm Pointer to an alarm structure that contains the alarm time to set |
| 275 | + */ |
| 276 | + fsp_err_t (* calendarAlarmSet)(rtc_ctrl_t * const p_ctrl, rtc_alarm_time_t * const p_alarm); |
| 277 | + |
| 278 | + /** Get the calendar alarm time. |
| 279 | + * |
| 280 | + * @param[in] p_ctrl Pointer to RTC device handle |
| 281 | + * @param[out] p_alarm Pointer to an alarm structure to fill up with the alarm time |
| 282 | + */ |
| 283 | + fsp_err_t (* calendarAlarmGet)(rtc_ctrl_t * const p_ctrl, rtc_alarm_time_t * const p_alarm); |
| 284 | + |
| 285 | + /** Set the periodic irq rate |
| 286 | + * |
| 287 | + * @param[in] p_ctrl Pointer to RTC device handle |
| 288 | + * @param[in] rate Rate of periodic interrupts |
| 289 | + */ |
| 290 | + fsp_err_t (* periodicIrqRateSet)(rtc_ctrl_t * const p_ctrl, rtc_periodic_irq_select_t const rate); |
| 291 | + |
| 292 | + /** Set time error adjustment. |
| 293 | + * |
| 294 | + * |
| 295 | + * @param[in] p_ctrl Pointer to control handle structure |
| 296 | + * @param[in] err_adj_cfg Pointer to the Error Adjustment Config |
| 297 | + */ |
| 298 | + fsp_err_t (* errorAdjustmentSet)(rtc_ctrl_t * const p_ctrl, rtc_error_adjustment_cfg_t const * const err_adj_cfg); |
| 299 | + |
| 300 | + /** |
| 301 | + * Specify callback function and optional context pointer and working memory pointer. |
| 302 | + * |
| 303 | + * @param[in] p_ctrl Pointer to the RTC control block. |
| 304 | + * @param[in] p_callback Callback function |
| 305 | + * @param[in] p_context Pointer to send to callback function |
| 306 | + * @param[in] p_working_memory Pointer to volatile memory where callback structure can be allocated |
| 307 | + */ |
| 308 | + fsp_err_t (* callbackSet)(rtc_ctrl_t * const p_ctrl, void (* p_callback)(rtc_callback_args_t *), |
| 309 | + void * const p_context, rtc_callback_args_t * const p_callback_memory); |
| 310 | + |
| 311 | + /** Return the currently configure clock source for the RTC |
| 312 | + * |
| 313 | + * |
| 314 | + * @param[in] p_ctrl Pointer to control handle structure |
| 315 | + * @param[out] p_rtc_info Pointer to RTC information structure |
| 316 | + */ |
| 317 | + fsp_err_t (* infoGet)(rtc_ctrl_t * const p_ctrl, rtc_info_t * const p_rtc_info); |
| 318 | + |
| 319 | + /** Config Time capture |
| 320 | + * |
| 321 | + * @param[in] p_ctrl Pointer to RTC device handle |
| 322 | + * @param[in] p_time_capture Pointer to a time capture structure that contains the configuration |
| 323 | + */ |
| 324 | + fsp_err_t (* timeCaptureSet)(rtc_ctrl_t * const p_ctrl, rtc_time_capture_t * const p_time_capture); |
| 325 | + |
| 326 | + /** Get the capture time and clear bit status. |
| 327 | + * |
| 328 | + * @param[in] p_ctrl Pointer to RTC device handle |
| 329 | + * @param[out] p_time_capture Pointer to a time capture structure to fill up with the time capture |
| 330 | + */ |
| 331 | + fsp_err_t (* timeCaptureGet)(rtc_ctrl_t * const p_ctrl, rtc_time_capture_t * const p_time_capture); |
| 332 | +} rtc_api_t; |
| 333 | + |
| 334 | +/** This structure encompasses everything that is needed to use an instance of this interface. */ |
| 335 | +typedef struct st_rtc_instance |
| 336 | +{ |
| 337 | + rtc_ctrl_t * p_ctrl; ///< Pointer to the control structure for this instance |
| 338 | + rtc_cfg_t const * p_cfg; ///< Pointer to the configuration structure for this instance |
| 339 | + rtc_api_t const * p_api; ///< Pointer to the API structure for this instance |
| 340 | +} rtc_instance_t; |
| 341 | + |
| 342 | +/*******************************************************************************************************************//** |
| 343 | + * @} (end defgroup RTC_API) |
| 344 | + **********************************************************************************************************************/ |
| 345 | + |
| 346 | +/* Common macro for FSP header files. There is also a corresponding FSP_HEADER macro at the top of this file. */ |
| 347 | +FSP_FOOTER |
| 348 | + |
| 349 | +#endif |
0 commit comments