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