1 /** 2 * @file tmr.h 3 * @brief Timer (TMR) function prototypes and data types. 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_MAX32520_TMR_H_ 28 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32520_TMR_H_ 29 30 /* **** Includes **** */ 31 #include <stdint.h> 32 #include "mxc_device.h" 33 #include "tmr_regs.h" 34 #include "mxc_sys.h" 35 #include "gcr_regs.h" 36 37 #ifdef __cplusplus 38 extern "C" { 39 #endif 40 41 /** 42 * @defgroup tmr Timer (TMR) 43 * @ingroup periphlibs 44 * @{ 45 */ 46 47 /** 48 * @brief Timer prescaler values 49 */ 50 typedef enum { 51 MXC_TMR_PRES_1 = MXC_S_TMR_CN_PRES_DIV1, ///< Divide input clock by 1 52 MXC_TMR_PRES_2 = MXC_S_TMR_CN_PRES_DIV2, ///< Divide input clock by 2 53 MXC_TMR_PRES_4 = MXC_S_TMR_CN_PRES_DIV4, ///< Divide input clock by 4 54 MXC_TMR_PRES_8 = MXC_S_TMR_CN_PRES_DIV8, ///< Divide input clock by 8 55 MXC_TMR_PRES_16 = MXC_S_TMR_CN_PRES_DIV16, ///< Divide input clock by 16 56 MXC_TMR_PRES_32 = MXC_S_TMR_CN_PRES_DIV32, ///< Divide input clock by 32 57 MXC_TMR_PRES_64 = MXC_S_TMR_CN_PRES_DIV64, ///< Divide input clock by 64 58 MXC_TMR_PRES_128 = MXC_S_TMR_CN_PRES_DIV128, ///< Divide input clock by 128 59 MXC_TMR_PRES_256 = MXC_F_TMR_CN_PRES3 | MXC_S_TMR_CN_PRES_DIV1, ///< Divide input clock by 256 60 MXC_TMR_PRES_512 = MXC_F_TMR_CN_PRES3 | MXC_S_TMR_CN_PRES_DIV2, ///< Divide input clock by 512 61 MXC_TMR_PRES_1024 = MXC_F_TMR_CN_PRES3 | MXC_S_TMR_CN_PRES_DIV4, ///< Divide input clock by 1024 62 MXC_TMR_PRES_2048 = MXC_F_TMR_CN_PRES3 | MXC_S_TMR_CN_PRES_DIV8, ///< Divide input clock by 2048 63 MXC_TMR_PRES_4096 = MXC_F_TMR_CN_PRES3 | 64 MXC_S_TMR_CN_PRES_DIV16, ///< Divide input clock by 4096 65 66 // Legacy names 67 TMR_PRES_1 = MXC_TMR_PRES_1, 68 TMR_PRES_2 = MXC_TMR_PRES_2, 69 TMR_PRES_4 = MXC_TMR_PRES_4, 70 TMR_PRES_8 = MXC_TMR_PRES_8, 71 TMR_PRES_16 = MXC_TMR_PRES_16, 72 TMR_PRES_32 = MXC_TMR_PRES_32, 73 TMR_PRES_64 = MXC_TMR_PRES_64, 74 TMR_PRES_128 = MXC_TMR_PRES_128, 75 TMR_PRES_256 = MXC_TMR_PRES_256, 76 TMR_PRES_512 = MXC_TMR_PRES_512, 77 TMR_PRES_1024 = MXC_TMR_PRES_1024, 78 TMR_PRES_2048 = MXC_TMR_PRES_2048, 79 TMR_PRES_4096 = MXC_TMR_PRES_4096 80 } mxc_tmr_pres_t; 81 82 /** 83 * @brief Timer modes 84 */ 85 typedef enum { 86 MXC_TMR_MODE_ONESHOT = MXC_V_TMR_CN_TMODE_ONESHOT, ///< Timer Mode ONESHOT 87 MXC_TMR_MODE_CONTINUOUS = MXC_V_TMR_CN_TMODE_CONTINUOUS, ///< Timer Mode CONTINUOUS 88 MXC_TMR_MODE_COUNTER = MXC_V_TMR_CN_TMODE_COUNTER, ///< Timer Mode COUNTER 89 MXC_TMR_MODE_PWM = MXC_V_TMR_CN_TMODE_PWM, ///< Timer Mode PWM 90 MXC_TMR_MODE_CAPTURE = MXC_V_TMR_CN_TMODE_CAPTURE, ///< Timer Mode CAPTURE 91 MXC_TMR_MODE_COMPARE = MXC_V_TMR_CN_TMODE_COMPARE, ///< Timer Mode COMPARE 92 MXC_TMR_MODE_GATED = MXC_V_TMR_CN_TMODE_GATED, ///< Timer Mode GATED 93 MXC_TMR_MODE_CAPTURE_COMPARE = MXC_V_TMR_CN_TMODE_CAPTURECOMPARE, ///< Timer Mode CAPTURECOMPARE 94 95 // Legacy names 96 TMR_MODE_ONESHOT = MXC_TMR_MODE_ONESHOT, 97 TMR_MODE_CONTINUOUS = MXC_TMR_MODE_CONTINUOUS, 98 TMR_MODE_COUNTER = MXC_TMR_MODE_COUNTER, 99 TMR_MODE_PWM = MXC_TMR_MODE_PWM, 100 TMR_MODE_CAPTURE = MXC_TMR_MODE_CAPTURE, 101 TMR_MODE_COMPARE = MXC_TMR_MODE_COMPARE, 102 TMR_MODE_GATED = MXC_TMR_MODE_GATED, 103 TMR_MODE_CAPTURE_COMPARE = MXC_TMR_MODE_CAPTURE_COMPARE 104 } mxc_tmr_mode_t; 105 106 /** 107 * @brief Timer units of time enumeration 108 */ 109 typedef enum { 110 MXC_TMR_UNIT_NANOSEC = 0, ///< Nanosecond Unit Indicator 111 MXC_TMR_UNIT_MICROSEC, ///< Microsecond Unit Indicator 112 MXC_TMR_UNIT_MILLISEC, ///< Millisecond Unit Indicator 113 MXC_TMR_UNIT_SEC, ///< Second Unit Indicator 114 115 // Legacy names 116 TMR_UNIT_NANOSEC = MXC_TMR_UNIT_NANOSEC, 117 TMR_UNIT_MICROSEC = MXC_TMR_UNIT_MICROSEC, 118 TMR_UNIT_MILLISEC = MXC_TMR_UNIT_MILLISEC, 119 TMR_UNIT_SEC = MXC_TMR_UNIT_SEC, 120 } mxc_tmr_unit_t; 121 122 /** 123 * @brief Timer Configuration 124 */ 125 typedef struct { 126 mxc_tmr_pres_t pres; ///< Desired timer prescaler 127 mxc_tmr_mode_t mode; ///< Desired timer mode 128 uint32_t cmp_cnt; ///< Compare register value in timer ticks 129 unsigned pol; ///< Polarity (0 or 1) 130 } mxc_tmr_cfg_t; 131 132 /* **** Definitions **** */ 133 typedef void (*mxc_tmr_complete_t)(int error); 134 135 /* **** Function Prototypes **** */ 136 137 /** 138 * @brief Initialize timer module clock. 139 * @note On default this function enables TMR peripheral clock and related GPIOs. 140 * if you wish to manage clock and gpio related things in upper level instead of here. 141 * Define MSDK_NO_GPIO_CLK_INIT flag in project.mk file. 142 * By this flag this function will remove clock and gpio related codes from file. 143 * 144 * @param tmr Pointer to timer module to initialize. 145 * @param cfg configuration object 146 */ 147 void MXC_TMR_Init(mxc_tmr_regs_t *tmr, mxc_tmr_cfg_t *cfg); 148 149 /** 150 * @brief Shutdown timer module clock. 151 * @param tmr Pointer to timer module to initialize. 152 */ 153 void MXC_TMR_Shutdown(mxc_tmr_regs_t *tmr); 154 155 /** 156 * @brief Start the timer counting. 157 * @param tmr Pointer to timer module to initialize. 158 */ 159 void MXC_TMR_Start(mxc_tmr_regs_t *tmr); 160 161 /** 162 * @brief Stop the timer. 163 * @param tmr Pointer to timer module to initialize. 164 */ 165 void MXC_TMR_Stop(mxc_tmr_regs_t *tmr); 166 167 /** 168 * @brief Set the value of the first transition in PWM mode 169 * @param tmr Pointer to timer module to initialize. 170 * @param pwm New pwm count. 171 * @note Will block until safe to change the period count. 172 * @return E_BAD_PARAM if pwm > cnt. 173 */ 174 int MXC_TMR_SetPWM(mxc_tmr_regs_t *tmr, uint32_t pwm); 175 176 /** 177 * @brief Get the timer compare count. 178 * @param tmr Pointer to timer module to initialize. 179 * @return Returns the current compare count. 180 */ 181 uint32_t MXC_TMR_GetCompare(mxc_tmr_regs_t *tmr); 182 183 /** 184 * @brief Get the timer capture count. 185 * @param tmr Pointer to timer module to initialize. 186 * @return Returns the most recent capture count. 187 */ 188 uint32_t MXC_TMR_GetCapture(mxc_tmr_regs_t *tmr); 189 190 /** 191 * @brief Get the timer count. 192 * @param tmr Pointer to timer module to initialize. 193 * @return Returns the current count. 194 */ 195 uint32_t MXC_TMR_GetCount(mxc_tmr_regs_t *tmr); 196 197 /** 198 * @brief Clear the timer interrupt. 199 * @param tmr Pointer to timer module to initialize. 200 */ 201 void MXC_TMR_ClearFlags(mxc_tmr_regs_t *tmr); 202 203 /** 204 * @brief Get the timer interrupt status. 205 * @param tmr Pointer to timer module to initialize. 206 * @return Returns the interrupt status. 1 if interrupt has occured. 207 */ 208 uint32_t MXC_TMR_GetFlags(mxc_tmr_regs_t *tmr); 209 210 /** 211 * @brief enable interupt 212 * 213 * @param tmr The timer 214 */ 215 void MXC_TMR_EnableInt(mxc_tmr_regs_t *tmr); 216 217 /** 218 * @brief disable interupt 219 * 220 * @param tmr The timer 221 */ 222 void MXC_TMR_DisableInt(mxc_tmr_regs_t *tmr); 223 224 /** 225 * @brief Set the timer compare count. 226 * @param tmr Pointer to timer module to initialize. 227 * @param cmp_cnt New compare count. 228 * @note In PWM Mode use this to set the value of the second transition. 229 */ 230 void MXC_TMR_SetCompare(mxc_tmr_regs_t *tmr, uint32_t cmp_cnt); 231 232 /** 233 * @brief Set the timer count. 234 * @param tmr Pointer to timer module to initialize. 235 * @param cnt New count. 236 */ 237 void MXC_TMR_SetCount(mxc_tmr_regs_t *tmr, uint32_t cnt); 238 239 /** 240 * @brief Dealay for a set periord of time measured in microseconds 241 * 242 * @param tmr The timer 243 * @param[in] us microseconds to delay for 244 */ 245 void MXC_TMR_Delay(mxc_tmr_regs_t *tmr, uint32_t us); 246 247 /** 248 * @brief Start a timer that will time out after a certain number of microseconds 249 * 250 * @param tmr The timer 251 * @param[in] us microseconds to time out after 252 */ 253 void MXC_TMR_TO_Start(mxc_tmr_regs_t *tmr, uint32_t us); 254 255 /** 256 * @brief Check on time out timer 257 * 258 * @param tmr The timer 259 * 260 * @return Returns E_TIMEOUT if timer timed out or E_NO_ERROR if it has not timed out 261 */ 262 int MXC_TMR_TO_Check(mxc_tmr_regs_t *tmr); 263 264 /** 265 * @brief Stop the Timeout timer 266 * 267 * @param tmr The timer 268 */ 269 void MXC_TMR_TO_Stop(mxc_tmr_regs_t *tmr); 270 271 /** 272 * @brief Clear timeout timer back to zero 273 * 274 * @param tmr The timer 275 */ 276 void MXC_TMR_TO_Clear(mxc_tmr_regs_t *tmr); 277 278 /** 279 * @brief Get elapsed time of timeout timer 280 * 281 * @param tmr The timer 282 * 283 * @return Time that has elapsed in timeout timer 284 */ 285 unsigned int MXC_TMR_TO_Elapsed(mxc_tmr_regs_t *tmr); 286 287 /** 288 * @brief Amount of time remaining until timeour 289 * 290 * @param tmr The timer 291 * 292 * @return Time that is left until timeout 293 */ 294 unsigned int MXC_TMR_TO_Remaining(mxc_tmr_regs_t *tmr); 295 296 /** 297 * @brief Start stopwatch 298 * 299 * @param tmr The timer 300 */ 301 void MXC_TMR_SW_Start(mxc_tmr_regs_t *tmr); 302 303 /** 304 * @brief Stopwatch stop 305 * 306 * @param tmr The timer 307 * 308 * @return the time when the stopwatch is stopped. 309 */ 310 unsigned int MXC_TMR_SW_Stop(mxc_tmr_regs_t *tmr); 311 312 /** 313 * @brief Get time from timer 314 * 315 * @param tmr The timer 316 * @param[in] ticks The ticks 317 * @param time The time 318 * @param units The units 319 * 320 * @return E_NO_ERROR or error check mxc_errors. 321 */ 322 int MXC_TMR_GetTime(mxc_tmr_regs_t *tmr, uint32_t ticks, uint32_t *time, mxc_tmr_unit_t *units); 323 324 /**@} end of group tmr */ 325 326 #ifdef __cplusplus 327 } 328 #endif 329 330 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32520_TMR_H_ 331