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