1 /** 2 * @file rtc.h 3 * @brief Real Time Clock (RTC) functions and prototypes. 4 */ 5 6 /****************************************************************************** 7 * 8 * Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by 9 * Analog Devices, Inc.), 10 * Copyright (C) 2023-2024 Analog Devices, Inc. 11 * 12 * Licensed under the Apache License, Version 2.0 (the "License"); 13 * you may not use this file except in compliance with the License. 14 * You may obtain a copy of the License at 15 * 16 * http://www.apache.org/licenses/LICENSE-2.0 17 * 18 * Unless required by applicable law or agreed to in writing, software 19 * distributed under the License is distributed on an "AS IS" BASIS, 20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 21 * See the License for the specific language governing permissions and 22 * limitations under the License. 23 * 24 ******************************************************************************/ 25 26 /* Define to prevent redundant inclusion */ 27 #ifndef LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32672_RTC_H_ 28 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32672_RTC_H_ 29 30 /* **** Includes **** */ 31 #include <stdint.h> 32 33 #include "mxc_device.h" 34 #include "mxc_sys.h" 35 #include "rtc_regs.h" 36 #include "tmr_regs.h" 37 38 #ifdef __cplusplus 39 extern "C" { 40 #endif 41 42 /** 43 * @defgroup rtc Real Time Clock (RTC) 44 * @ingroup periphlibs 45 * @{ 46 */ 47 48 #define MXC_RTC_MAX_SSEC (MXC_F_RTC_SSEC_SSEC + 1) 49 #define MXC_RTC_TRIM_TMR_IRQ MXC_F_TMR_INTFL_IRQ_A 50 51 /* **** Definitions **** */ 52 /** 53 * @brief Bitmasks for each of the RTC's Frequency. 54 */ 55 typedef enum { 56 MXC_RTC_F_1HZ = MXC_S_RTC_CTRL_SQW_SEL_FREQ1HZ, ///< 1Hz (Compensated) 57 MXC_RTC_F_512HZ = MXC_S_RTC_CTRL_SQW_SEL_FREQ512HZ, ///< 512Hz (Compensated) 58 MXC_RTC_F_4KHZ = MXC_S_RTC_CTRL_SQW_SEL_FREQ4KHZ, ///< 4Khz 59 MXC_RTC_F_32KHZ = 32, ///< 32Khz 60 } mxc_rtc_freq_sel_t; 61 62 /** 63 * @brief Bitmasks for each of the RTC's interrupt enables. 64 */ 65 typedef enum { 66 MXC_RTC_INT_EN_LONG = MXC_F_RTC_CTRL_TOD_ALARM_IE, ///< Long-interval alarm interrupt enable 67 MXC_RTC_INT_EN_SHORT = MXC_F_RTC_CTRL_SSEC_ALARM_IE, ///< Short-interval alarm interrupt enable 68 MXC_RTC_INT_EN_READY = MXC_F_RTC_CTRL_RDY_IE, ///< Timer ready interrupt enable 69 } mxc_rtc_int_en_t; 70 71 /** 72 * @brief Bitmasks for each of the RTC's interrupt flags. 73 */ 74 typedef enum { 75 MXC_RTC_INT_FL_LONG = MXC_F_RTC_CTRL_TOD_ALARM, ///< Long-interval alarm interrupt flag 76 MXC_RTC_INT_FL_SHORT = MXC_F_RTC_CTRL_SSEC_ALARM, ///< Short-interval alarm interrupt flag 77 MXC_RTC_INT_FL_READY = MXC_F_RTC_CTRL_RDY, ///< Timer ready interrupt flag 78 } mxc_rtc_int_fl_t; 79 80 /** 81 * @brief Set Time-of-Day alarm value and enable Interrupt 82 * @param ras 20-bit value 0-0xFFFFF 83 * @retval returns Success or Fail, see \ref MXC_Error_Codes 84 */ 85 int MXC_RTC_SetTimeofdayAlarm(uint32_t ras); 86 87 /** 88 * @brief Set Sub-Second alarm value and enable interrupt, 89 * @brief this is to be called after the init_rtc() function 90 * @param rssa 32-bit value 0-0xFFFFFFFF 91 * @retval returns Success or Fail, see \ref MXC_Error_Codes 92 */ 93 int MXC_RTC_SetSubsecondAlarm(uint32_t rssa); 94 95 /** 96 * @brief Start the Real Time Clock 97 * @retval returns Success or Fail, see \ref MXC_Error_Codes 98 */ 99 int MXC_RTC_Start(void); 100 /** 101 * @brief Stop the Real Time Clock (Blocking function) 102 * @retval returns Success or Fail, see \ref MXC_Error_Codes 103 */ 104 int MXC_RTC_Stop(void); 105 106 /** 107 * @brief Initialize the sec and ssec registers and enable RTC (Blocking function) 108 * @param sec set the RTC Sec counter (32-bit) 109 * @param ssec set the RTC Sub-second counter (12-bit) 110 * @retval returns Success or Fail, see \ref MXC_Error_Codes 111 */ 112 int MXC_RTC_Init(uint32_t sec, uint16_t ssec); 113 114 /** 115 * @brief Allow generation of Square Wave on the SQW pin (Blocking function) 116 * @param fq Frequency output selection 117 * @retval returns Success or Fail, see \ref MXC_Error_Codes 118 */ 119 int MXC_RTC_SquareWaveStart(mxc_rtc_freq_sel_t fq); 120 121 /** 122 * @brief Stop the generation of square wave (Blocking function) 123 * @retval returns Success or Fail, see \ref MXC_Error_Codes 124 */ 125 int MXC_RTC_SquareWaveStop(void); 126 127 /** 128 * @brief Set Trim register value (Blocking function) 129 * @param trm set the RTC Trim (8-bit, +/- 127) 130 * @retval returns Success or Fail, see \ref MXC_Error_Codes 131 */ 132 int MXC_RTC_Trim(int8_t trm); 133 134 /** 135 * @brief Enable Interurpts (Blocking function) 136 * @param mask The bitwise OR of interrupts to enable. 137 * See #mxc_rtc_int_en_t for available choices. 138 * @retval returns Success or Fail, see \ref MXC_Error_Codes 139 */ 140 int MXC_RTC_EnableInt(uint32_t mask); 141 142 /** 143 * @brief Disable Interurpts (Blocking function) 144 * @param mask The mask of interrupts to disable. 145 * See #mxc_rtc_int_en_t for available choices. 146 * @retval returns Success or Fail, see \ref MXC_Error_Codes 147 */ 148 int MXC_RTC_DisableInt(uint32_t mask); 149 150 /** 151 * @brief Gets interrupt flags. 152 * @retval The bitwise OR of any interrupts flags that are 153 * currently set. See \ref mxc_rtc_int_fl_t for the list 154 * of possible flags. 155 */ 156 int MXC_RTC_GetFlags(void); 157 158 /** 159 * @brief Clear interrupt flags. 160 * @param flags The bitwise OR of the interrupts flags to cleear. 161 * See #mxc_rtc_int_fl_t for the list of possible flags. 162 * @retval returns Success or Fail, see \ref MXC_Error_Codes 163 */ 164 int MXC_RTC_ClearFlags(int flags); 165 166 /** 167 * @brief Get SubSecond or E_BUSY, see /ref MXC_ERROR_CODES 168 * @retval Returns subsecond value 169 */ 170 #ifdef __GNUC__ 171 __attribute__((deprecated("Use MXC_RTC_GetSubSeconds() instead."))) 172 #endif 173 int MXC_RTC_GetSubSecond(void); 174 175 /** 176 * @brief This function stores the current value of the sub-seconds counter into a 177 * pointer if the RTC is not busy. If the RTC is busy, an error is returned. 178 * @param ssec Pointer to the variable to store the current sub-seconds value. 179 * @retval E_NO_ERROR if successful, otherwise an error code (see /ref MXC_ERROR_CODES). 180 */ 181 int MXC_RTC_GetSubSeconds(uint32_t *ssec); 182 183 /** 184 * @brief Get Second or E_BUSY, see /ref MXC_ERROR_CODES 185 * @retval returns second value 186 */ 187 #ifdef __GNUC__ 188 __attribute__((deprecated("Use MXC_RTC_GetSeconds() instead."))) 189 #endif 190 int MXC_RTC_GetSecond(void); 191 192 /** 193 * @brief This function stores the current value of the seconds counter into a 194 * pointer if the RTC is not busy. If the RTC is busy, an error is returned. 195 * @param sec Pointer to the variable to store the current seconds value. 196 * @retval E_NO_ERROR if successful, otherwise an error code (see /ref MXC_ERROR_CODES). 197 */ 198 int MXC_RTC_GetSeconds(uint32_t *sec); 199 200 /** 201 * @brief Get the current second and sub-second counts. 202 * @param sec pointer to store seconds value 203 * @param subsec pointer to store subseconds value 204 * @retval returns Success or Fail, see \ref MXC_Error_Codes 205 */ 206 int MXC_RTC_GetTime(uint32_t *sec, uint32_t *subsec); 207 208 /** 209 * @brief Get RTC busy flag. 210 * @retval returns Success or E_BUSY, see /ref MXC_ERROR_CODES 211 */ 212 int MXC_RTC_GetBusyFlag(void); 213 214 /** 215 * @brief Calculate and set the appropriate RTC trim value based on an accurate reference clock 216 * 217 * @param tmr Timer available to be used to measure known time periods over which the RTC ticks are counted 218 * 219 * @retval returns Success or Fail, see \ref MXC_Error_Codes 220 * 221 * @note If RTC running before calling this function and interrupts enabled, accuracy of trimming could be affected 222 * @note External 32MHz must be installed and calibrated properly for this function to be successful 223 */ 224 int MXC_RTC_TrimCrystal(mxc_tmr_regs_t *tmr); 225 226 /**@} end of group rtc */ 227 #ifdef __cplusplus 228 } 229 #endif 230 231 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32672_RTC_H_ 232