mbed library sources. Supersedes mbed-src. Edited target satm32f446 for user USART3 pins

Dependents:   IGLOO_board

Fork of mbed-dev by mbed official

Committer:
Anna Bridge
Date:
Fri Jun 22 16:45:37 2018 +0100
Revision:
186:707f6e361f3e
mbed-dev library. Release version 162

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Anna Bridge 186:707f6e361f3e 1 /**
Anna Bridge 186:707f6e361f3e 2 * @file
Anna Bridge 186:707f6e361f3e 3 * @brief Timer Peripheral Driver Source.
Anna Bridge 186:707f6e361f3e 4 */
Anna Bridge 186:707f6e361f3e 5 /* *****************************************************************************
Anna Bridge 186:707f6e361f3e 6 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
Anna Bridge 186:707f6e361f3e 7 *
Anna Bridge 186:707f6e361f3e 8 * Permission is hereby granted, free of charge, to any person obtaining a
Anna Bridge 186:707f6e361f3e 9 * copy of this software and associated documentation files (the "Software"),
Anna Bridge 186:707f6e361f3e 10 * to deal in the Software without restriction, including without limitation
Anna Bridge 186:707f6e361f3e 11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
Anna Bridge 186:707f6e361f3e 12 * and/or sell copies of the Software, and to permit persons to whom the
Anna Bridge 186:707f6e361f3e 13 * Software is furnished to do so, subject to the following conditions:
Anna Bridge 186:707f6e361f3e 14 *
Anna Bridge 186:707f6e361f3e 15 * The above copyright notice and this permission notice shall be included
Anna Bridge 186:707f6e361f3e 16 * in all copies or substantial portions of the Software.
Anna Bridge 186:707f6e361f3e 17 *
Anna Bridge 186:707f6e361f3e 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
Anna Bridge 186:707f6e361f3e 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Anna Bridge 186:707f6e361f3e 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
Anna Bridge 186:707f6e361f3e 21 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
Anna Bridge 186:707f6e361f3e 22 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
Anna Bridge 186:707f6e361f3e 23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
Anna Bridge 186:707f6e361f3e 24 * OTHER DEALINGS IN THE SOFTWARE.
Anna Bridge 186:707f6e361f3e 25 *
Anna Bridge 186:707f6e361f3e 26 * Except as contained in this notice, the name of Maxim Integrated
Anna Bridge 186:707f6e361f3e 27 * Products, Inc. shall not be used except as stated in the Maxim Integrated
Anna Bridge 186:707f6e361f3e 28 * Products, Inc. Branding Policy.
Anna Bridge 186:707f6e361f3e 29 *
Anna Bridge 186:707f6e361f3e 30 * The mere transfer of this software does not imply any licenses
Anna Bridge 186:707f6e361f3e 31 * of trade secrets, proprietary technology, copyrights, patents,
Anna Bridge 186:707f6e361f3e 32 * trademarks, maskwork rights, or any other form of intellectual
Anna Bridge 186:707f6e361f3e 33 * property whatsoever. Maxim Integrated Products, Inc. retains all
Anna Bridge 186:707f6e361f3e 34 * ownership rights.
Anna Bridge 186:707f6e361f3e 35 *
Anna Bridge 186:707f6e361f3e 36 * $Date: 2016-09-08 17:30:35 -0500 (Thu, 08 Sep 2016) $
Anna Bridge 186:707f6e361f3e 37 * $Revision: 24322 $
Anna Bridge 186:707f6e361f3e 38 *
Anna Bridge 186:707f6e361f3e 39 **************************************************************************** */
Anna Bridge 186:707f6e361f3e 40
Anna Bridge 186:707f6e361f3e 41 /* **** Includes **** */
Anna Bridge 186:707f6e361f3e 42 #include <stddef.h>
Anna Bridge 186:707f6e361f3e 43 #include "mxc_assert.h"
Anna Bridge 186:707f6e361f3e 44 #include "tmr.h"
Anna Bridge 186:707f6e361f3e 45
Anna Bridge 186:707f6e361f3e 46 /**
Anna Bridge 186:707f6e361f3e 47 * @ingroup tmr
Anna Bridge 186:707f6e361f3e 48 * @{
Anna Bridge 186:707f6e361f3e 49 */
Anna Bridge 186:707f6e361f3e 50 static tmr_prescale_t prescaler[MXC_CFG_TMR_INSTANCES];
Anna Bridge 186:707f6e361f3e 51
Anna Bridge 186:707f6e361f3e 52 /* ************************************************************************* */
Anna Bridge 186:707f6e361f3e 53 int TMR_Init(mxc_tmr_regs_t *tmr, tmr_prescale_t prescale, const sys_cfg_tmr_t *sysCfg)
Anna Bridge 186:707f6e361f3e 54 {
Anna Bridge 186:707f6e361f3e 55 int err;
Anna Bridge 186:707f6e361f3e 56 int tmrNum;
Anna Bridge 186:707f6e361f3e 57
Anna Bridge 186:707f6e361f3e 58 //get the timer number
Anna Bridge 186:707f6e361f3e 59 tmrNum = MXC_TMR_GET_IDX(tmr);
Anna Bridge 186:707f6e361f3e 60
Anna Bridge 186:707f6e361f3e 61 //check for valid pointer
Anna Bridge 186:707f6e361f3e 62 MXC_ASSERT(tmrNum >= 0);
Anna Bridge 186:707f6e361f3e 63
Anna Bridge 186:707f6e361f3e 64 //steup system GPIO config
Anna Bridge 186:707f6e361f3e 65 if((err = SYS_TMR_Init(tmr, sysCfg)) != E_NO_ERROR)
Anna Bridge 186:707f6e361f3e 66 return err;
Anna Bridge 186:707f6e361f3e 67
Anna Bridge 186:707f6e361f3e 68 //save the prescale value for this timer
Anna Bridge 186:707f6e361f3e 69 prescaler[tmrNum] = prescale;
Anna Bridge 186:707f6e361f3e 70
Anna Bridge 186:707f6e361f3e 71 //Disable timer and clear settings
Anna Bridge 186:707f6e361f3e 72 tmr->ctrl = 0;
Anna Bridge 186:707f6e361f3e 73
Anna Bridge 186:707f6e361f3e 74 //reset all counts to 0
Anna Bridge 186:707f6e361f3e 75 tmr->count32 = 0;
Anna Bridge 186:707f6e361f3e 76 tmr->count16_0 = 0;
Anna Bridge 186:707f6e361f3e 77 tmr->count16_1 = 0;
Anna Bridge 186:707f6e361f3e 78
Anna Bridge 186:707f6e361f3e 79 // Clear interrupt flag
Anna Bridge 186:707f6e361f3e 80 tmr->intfl = MXC_F_TMR_INTFL_TIMER0 | MXC_F_TMR_INTFL_TIMER1;
Anna Bridge 186:707f6e361f3e 81
Anna Bridge 186:707f6e361f3e 82 return E_NO_ERROR;
Anna Bridge 186:707f6e361f3e 83 }
Anna Bridge 186:707f6e361f3e 84
Anna Bridge 186:707f6e361f3e 85 /* ************************************************************************* */
Anna Bridge 186:707f6e361f3e 86 void TMR32_Config(mxc_tmr_regs_t *tmr, const tmr32_cfg_t *config)
Anna Bridge 186:707f6e361f3e 87 {
Anna Bridge 186:707f6e361f3e 88 //stop timer
Anna Bridge 186:707f6e361f3e 89 TMR32_Stop(tmr);
Anna Bridge 186:707f6e361f3e 90
Anna Bridge 186:707f6e361f3e 91 //setup timer configuration register
Anna Bridge 186:707f6e361f3e 92 //clear tmr2x16 (32bit mode), mode and polarity bits
Anna Bridge 186:707f6e361f3e 93 tmr->ctrl &= ~(MXC_F_TMR_CTRL_TMR2X16 | MXC_F_TMR_CTRL_MODE |
Anna Bridge 186:707f6e361f3e 94 MXC_F_TMR_CTRL_POLARITY);
Anna Bridge 186:707f6e361f3e 95
Anna Bridge 186:707f6e361f3e 96 //set mode and polarity
Anna Bridge 186:707f6e361f3e 97 tmr->ctrl |= ((config->mode << MXC_F_TMR_CTRL_MODE_POS) |
Anna Bridge 186:707f6e361f3e 98 (config->polarity << MXC_F_TMR_CTRL_POLARITY_POS));
Anna Bridge 186:707f6e361f3e 99
Anna Bridge 186:707f6e361f3e 100 //setup timer Tick registers
Anna Bridge 186:707f6e361f3e 101 tmr->term_cnt32 = config->compareCount;
Anna Bridge 186:707f6e361f3e 102
Anna Bridge 186:707f6e361f3e 103 return;
Anna Bridge 186:707f6e361f3e 104 }
Anna Bridge 186:707f6e361f3e 105
Anna Bridge 186:707f6e361f3e 106 /* ************************************************************************* */
Anna Bridge 186:707f6e361f3e 107 void TMR32_PWMConfig(mxc_tmr_regs_t *tmr, const tmr32_cfg_pwm_t *config)
Anna Bridge 186:707f6e361f3e 108 {
Anna Bridge 186:707f6e361f3e 109 //stop timer
Anna Bridge 186:707f6e361f3e 110 TMR32_Stop(tmr);
Anna Bridge 186:707f6e361f3e 111
Anna Bridge 186:707f6e361f3e 112 //setup timer configuration register
Anna Bridge 186:707f6e361f3e 113 //clear tmr2x16 (32bit mode), mode and polarity bits
Anna Bridge 186:707f6e361f3e 114 tmr->ctrl &= ~(MXC_F_TMR_CTRL_TMR2X16 | MXC_F_TMR_CTRL_MODE |
Anna Bridge 186:707f6e361f3e 115 MXC_F_TMR_CTRL_POLARITY);
Anna Bridge 186:707f6e361f3e 116
Anna Bridge 186:707f6e361f3e 117 //set mode and polarity
Anna Bridge 186:707f6e361f3e 118 tmr->ctrl |= ((TMR32_MODE_PWM << MXC_F_TMR_CTRL_MODE_POS) |
Anna Bridge 186:707f6e361f3e 119 (config->polarity << MXC_F_TMR_CTRL_POLARITY_POS));
Anna Bridge 186:707f6e361f3e 120
Anna Bridge 186:707f6e361f3e 121 tmr->pwm_cap32 = config->dutyCount;
Anna Bridge 186:707f6e361f3e 122
Anna Bridge 186:707f6e361f3e 123 //setup timer Tick registers
Anna Bridge 186:707f6e361f3e 124 tmr->count32 = 0;
Anna Bridge 186:707f6e361f3e 125 tmr->term_cnt32 = config->periodCount;
Anna Bridge 186:707f6e361f3e 126
Anna Bridge 186:707f6e361f3e 127 return;
Anna Bridge 186:707f6e361f3e 128 }
Anna Bridge 186:707f6e361f3e 129
Anna Bridge 186:707f6e361f3e 130 /* ************************************************************************* */
Anna Bridge 186:707f6e361f3e 131 void TMR16_Config(mxc_tmr_regs_t *tmr, uint8_t index, const tmr16_cfg_t *config)
Anna Bridge 186:707f6e361f3e 132 {
Anna Bridge 186:707f6e361f3e 133 //stop timer
Anna Bridge 186:707f6e361f3e 134 TMR16_Stop(tmr, index);
Anna Bridge 186:707f6e361f3e 135
Anna Bridge 186:707f6e361f3e 136 if(index > 0) { //configure timer 16_1
Anna Bridge 186:707f6e361f3e 137
Anna Bridge 186:707f6e361f3e 138 //setup timer configuration register
Anna Bridge 186:707f6e361f3e 139 tmr->ctrl |= MXC_F_TMR_CTRL_TMR2X16; //1 = 16bit mode
Anna Bridge 186:707f6e361f3e 140
Anna Bridge 186:707f6e361f3e 141 //set mode
Anna Bridge 186:707f6e361f3e 142 if(config->mode)
Anna Bridge 186:707f6e361f3e 143 tmr->ctrl |= MXC_F_TMR_CTRL_MODE_16_1;
Anna Bridge 186:707f6e361f3e 144 else
Anna Bridge 186:707f6e361f3e 145 tmr->ctrl &= ~MXC_F_TMR_CTRL_MODE_16_1;
Anna Bridge 186:707f6e361f3e 146
Anna Bridge 186:707f6e361f3e 147 //setup timer Ticks registers
Anna Bridge 186:707f6e361f3e 148 tmr->term_cnt16_1 = config->compareCount;
Anna Bridge 186:707f6e361f3e 149 } else { //configure timer 16_0
Anna Bridge 186:707f6e361f3e 150
Anna Bridge 186:707f6e361f3e 151 //setup timer configuration register
Anna Bridge 186:707f6e361f3e 152 tmr->ctrl |= MXC_F_TMR_CTRL_TMR2X16; //1 = 16bit mode
Anna Bridge 186:707f6e361f3e 153
Anna Bridge 186:707f6e361f3e 154 //set mode
Anna Bridge 186:707f6e361f3e 155 if(config->mode)
Anna Bridge 186:707f6e361f3e 156 tmr->ctrl |= MXC_F_TMR_CTRL_MODE_16_0;
Anna Bridge 186:707f6e361f3e 157 else
Anna Bridge 186:707f6e361f3e 158 tmr->ctrl &= ~MXC_F_TMR_CTRL_MODE_16_0;
Anna Bridge 186:707f6e361f3e 159
Anna Bridge 186:707f6e361f3e 160 //setup timer Ticks registers
Anna Bridge 186:707f6e361f3e 161 tmr->term_cnt16_0 = config->compareCount;
Anna Bridge 186:707f6e361f3e 162 }
Anna Bridge 186:707f6e361f3e 163
Anna Bridge 186:707f6e361f3e 164 return;
Anna Bridge 186:707f6e361f3e 165 }
Anna Bridge 186:707f6e361f3e 166
Anna Bridge 186:707f6e361f3e 167 /* ************************************************************************* */
Anna Bridge 186:707f6e361f3e 168 void TMR32_Start(mxc_tmr_regs_t *tmr)
Anna Bridge 186:707f6e361f3e 169 {
Anna Bridge 186:707f6e361f3e 170 int tmrNum;
Anna Bridge 186:707f6e361f3e 171 uint32_t ctrl;
Anna Bridge 186:707f6e361f3e 172
Anna Bridge 186:707f6e361f3e 173 //get the timer number
Anna Bridge 186:707f6e361f3e 174 tmrNum = MXC_TMR_GET_IDX(tmr);
Anna Bridge 186:707f6e361f3e 175
Anna Bridge 186:707f6e361f3e 176 //prescaler gets reset to 0 when timer is disabled
Anna Bridge 186:707f6e361f3e 177 //set the prescale to the saved value for this timer
Anna Bridge 186:707f6e361f3e 178 ctrl = tmr->ctrl;
Anna Bridge 186:707f6e361f3e 179 ctrl &= ~(MXC_F_TMR_CTRL_PRESCALE); //clear prescaler bits
Anna Bridge 186:707f6e361f3e 180 ctrl |= prescaler[tmrNum] << MXC_F_TMR_CTRL_PRESCALE_POS; //set prescaler
Anna Bridge 186:707f6e361f3e 181 ctrl |= MXC_F_TMR_CTRL_ENABLE0; //set enable to start the timer
Anna Bridge 186:707f6e361f3e 182
Anna Bridge 186:707f6e361f3e 183 tmr->ctrl = ctrl;
Anna Bridge 186:707f6e361f3e 184
Anna Bridge 186:707f6e361f3e 185 return;
Anna Bridge 186:707f6e361f3e 186 }
Anna Bridge 186:707f6e361f3e 187
Anna Bridge 186:707f6e361f3e 188 /* ************************************************************************* */
Anna Bridge 186:707f6e361f3e 189 void TMR16_Start(mxc_tmr_regs_t *tmr, uint8_t index)
Anna Bridge 186:707f6e361f3e 190 {
Anna Bridge 186:707f6e361f3e 191 int tmrNum;
Anna Bridge 186:707f6e361f3e 192 uint32_t ctrl;
Anna Bridge 186:707f6e361f3e 193
Anna Bridge 186:707f6e361f3e 194 //get the timer number
Anna Bridge 186:707f6e361f3e 195 tmrNum = MXC_TMR_GET_IDX(tmr);
Anna Bridge 186:707f6e361f3e 196
Anna Bridge 186:707f6e361f3e 197 ctrl = tmr->ctrl;
Anna Bridge 186:707f6e361f3e 198
Anna Bridge 186:707f6e361f3e 199 //prescaler gets reset to 0 when both 16 bit timers are disabled
Anna Bridge 186:707f6e361f3e 200 //set the prescale to the saved value for this timer if is is not already set
Anna Bridge 186:707f6e361f3e 201 if((ctrl & MXC_F_TMR_CTRL_PRESCALE) != (uint32_t)(prescaler[tmrNum] << MXC_F_TMR_CTRL_PRESCALE_POS)) {
Anna Bridge 186:707f6e361f3e 202 ctrl &= ~(MXC_F_TMR_CTRL_PRESCALE); //clear prescaler bits
Anna Bridge 186:707f6e361f3e 203 ctrl |= prescaler[tmrNum] << MXC_F_TMR_CTRL_PRESCALE_POS; //set prescaler
Anna Bridge 186:707f6e361f3e 204 }
Anna Bridge 186:707f6e361f3e 205
Anna Bridge 186:707f6e361f3e 206 if(index > 0)
Anna Bridge 186:707f6e361f3e 207 ctrl |= MXC_F_TMR_CTRL_ENABLE1; //start timer 16_1
Anna Bridge 186:707f6e361f3e 208 else
Anna Bridge 186:707f6e361f3e 209 ctrl |= MXC_F_TMR_CTRL_ENABLE0; //start timer 16_0
Anna Bridge 186:707f6e361f3e 210
Anna Bridge 186:707f6e361f3e 211 tmr->ctrl = ctrl;
Anna Bridge 186:707f6e361f3e 212
Anna Bridge 186:707f6e361f3e 213 return;
Anna Bridge 186:707f6e361f3e 214 }
Anna Bridge 186:707f6e361f3e 215
Anna Bridge 186:707f6e361f3e 216 /* ************************************************************************* */
Anna Bridge 186:707f6e361f3e 217 uint32_t TMR_GetPrescaler(mxc_tmr_regs_t *tmr)
Anna Bridge 186:707f6e361f3e 218 {
Anna Bridge 186:707f6e361f3e 219 int tmrNum;
Anna Bridge 186:707f6e361f3e 220
Anna Bridge 186:707f6e361f3e 221 //get the timer number
Anna Bridge 186:707f6e361f3e 222 tmrNum = MXC_TMR_GET_IDX(tmr);
Anna Bridge 186:707f6e361f3e 223
Anna Bridge 186:707f6e361f3e 224 return ((uint32_t)prescaler[tmrNum]);
Anna Bridge 186:707f6e361f3e 225 }
Anna Bridge 186:707f6e361f3e 226
Anna Bridge 186:707f6e361f3e 227
Anna Bridge 186:707f6e361f3e 228 /* ************************************************************************* */
Anna Bridge 186:707f6e361f3e 229 int TMR32_GetPWMTicks(mxc_tmr_regs_t *tmr, uint8_t dutyPercent, uint32_t freq, uint32_t *dutyTicks, uint32_t *periodTicks)
Anna Bridge 186:707f6e361f3e 230 {
Anna Bridge 186:707f6e361f3e 231 uint32_t timerClock;
Anna Bridge 186:707f6e361f3e 232 uint32_t prescale;
Anna Bridge 186:707f6e361f3e 233 uint64_t ticks;
Anna Bridge 186:707f6e361f3e 234
Anna Bridge 186:707f6e361f3e 235 if(dutyPercent > 100)
Anna Bridge 186:707f6e361f3e 236 return E_BAD_PARAM;
Anna Bridge 186:707f6e361f3e 237
Anna Bridge 186:707f6e361f3e 238 if(freq == 0)
Anna Bridge 186:707f6e361f3e 239 return E_BAD_PARAM;
Anna Bridge 186:707f6e361f3e 240
Anna Bridge 186:707f6e361f3e 241 timerClock = SYS_TMR_GetFreq(tmr);
Anna Bridge 186:707f6e361f3e 242 prescale = TMR_GetPrescaler(tmr);
Anna Bridge 186:707f6e361f3e 243
Anna Bridge 186:707f6e361f3e 244 if(timerClock == 0 || prescale > TMR_PRESCALE_DIV_2_12)
Anna Bridge 186:707f6e361f3e 245 return E_UNINITIALIZED;
Anna Bridge 186:707f6e361f3e 246
Anna Bridge 186:707f6e361f3e 247 ticks = timerClock / (1 << (prescale & 0xF)) / freq;
Anna Bridge 186:707f6e361f3e 248
Anna Bridge 186:707f6e361f3e 249 //make sure ticks is within a 32 bit value
Anna Bridge 186:707f6e361f3e 250 if (!(ticks & 0xffffffff00000000) && (ticks & 0xffffffff)) {
Anna Bridge 186:707f6e361f3e 251 *periodTicks = ticks;
Anna Bridge 186:707f6e361f3e 252
Anna Bridge 186:707f6e361f3e 253 *dutyTicks = ((uint64_t)*periodTicks * dutyPercent) / 100;
Anna Bridge 186:707f6e361f3e 254
Anna Bridge 186:707f6e361f3e 255 return E_NO_ERROR;
Anna Bridge 186:707f6e361f3e 256 }
Anna Bridge 186:707f6e361f3e 257
Anna Bridge 186:707f6e361f3e 258 return E_INVALID;
Anna Bridge 186:707f6e361f3e 259 }
Anna Bridge 186:707f6e361f3e 260
Anna Bridge 186:707f6e361f3e 261 /* ************************************************************************* */
Anna Bridge 186:707f6e361f3e 262 int TMR32_TimeToTicks(mxc_tmr_regs_t *tmr, uint32_t time, tmr_unit_t units, uint32_t *ticks)
Anna Bridge 186:707f6e361f3e 263 {
Anna Bridge 186:707f6e361f3e 264 uint32_t unit_div0, unit_div1;
Anna Bridge 186:707f6e361f3e 265 uint32_t timerClock;
Anna Bridge 186:707f6e361f3e 266 uint32_t prescale;
Anna Bridge 186:707f6e361f3e 267 uint64_t temp_ticks;
Anna Bridge 186:707f6e361f3e 268
Anna Bridge 186:707f6e361f3e 269 timerClock = SYS_TMR_GetFreq(tmr);
Anna Bridge 186:707f6e361f3e 270 prescale = TMR_GetPrescaler(tmr);
Anna Bridge 186:707f6e361f3e 271
Anna Bridge 186:707f6e361f3e 272 if(timerClock == 0 || prescale > TMR_PRESCALE_DIV_2_12)
Anna Bridge 186:707f6e361f3e 273 return E_UNINITIALIZED;
Anna Bridge 186:707f6e361f3e 274
Anna Bridge 186:707f6e361f3e 275 switch (units) {
Anna Bridge 186:707f6e361f3e 276 case TMR_UNIT_NANOSEC:
Anna Bridge 186:707f6e361f3e 277 unit_div0 = 1000000;
Anna Bridge 186:707f6e361f3e 278 unit_div1 = 1000;
Anna Bridge 186:707f6e361f3e 279 break;
Anna Bridge 186:707f6e361f3e 280 case TMR_UNIT_MICROSEC:
Anna Bridge 186:707f6e361f3e 281 unit_div0 = 1000;
Anna Bridge 186:707f6e361f3e 282 unit_div1 = 1000;
Anna Bridge 186:707f6e361f3e 283 break;
Anna Bridge 186:707f6e361f3e 284 case TMR_UNIT_MILLISEC:
Anna Bridge 186:707f6e361f3e 285 unit_div0 = 1;
Anna Bridge 186:707f6e361f3e 286 unit_div1 = 1000;
Anna Bridge 186:707f6e361f3e 287 break;
Anna Bridge 186:707f6e361f3e 288 case TMR_UNIT_SEC:
Anna Bridge 186:707f6e361f3e 289 unit_div0 = 1;
Anna Bridge 186:707f6e361f3e 290 unit_div1 = 1;
Anna Bridge 186:707f6e361f3e 291 break;
Anna Bridge 186:707f6e361f3e 292 default:
Anna Bridge 186:707f6e361f3e 293 return E_BAD_PARAM;
Anna Bridge 186:707f6e361f3e 294 }
Anna Bridge 186:707f6e361f3e 295
Anna Bridge 186:707f6e361f3e 296 temp_ticks = (uint64_t)time * (timerClock / unit_div0) / (unit_div1 * (1 << (prescale & 0xF)));
Anna Bridge 186:707f6e361f3e 297
Anna Bridge 186:707f6e361f3e 298 //make sure ticks is within a 32 bit value
Anna Bridge 186:707f6e361f3e 299 if (!(temp_ticks & 0xffffffff00000000) && (temp_ticks & 0xffffffff)) {
Anna Bridge 186:707f6e361f3e 300 *ticks = temp_ticks;
Anna Bridge 186:707f6e361f3e 301 return E_NO_ERROR;
Anna Bridge 186:707f6e361f3e 302 }
Anna Bridge 186:707f6e361f3e 303
Anna Bridge 186:707f6e361f3e 304 return E_INVALID;
Anna Bridge 186:707f6e361f3e 305 }
Anna Bridge 186:707f6e361f3e 306
Anna Bridge 186:707f6e361f3e 307 /* ************************************************************************* */
Anna Bridge 186:707f6e361f3e 308 int TMR16_TimeToTicks(mxc_tmr_regs_t *tmr, uint32_t time, tmr_unit_t units, uint16_t *ticks)
Anna Bridge 186:707f6e361f3e 309 {
Anna Bridge 186:707f6e361f3e 310 uint32_t unit_div0, unit_div1;
Anna Bridge 186:707f6e361f3e 311 uint32_t timerClock;
Anna Bridge 186:707f6e361f3e 312 uint32_t prescale;
Anna Bridge 186:707f6e361f3e 313 uint64_t temp_ticks;
Anna Bridge 186:707f6e361f3e 314
Anna Bridge 186:707f6e361f3e 315 timerClock = SYS_TMR_GetFreq(tmr);
Anna Bridge 186:707f6e361f3e 316 prescale = TMR_GetPrescaler(tmr);
Anna Bridge 186:707f6e361f3e 317
Anna Bridge 186:707f6e361f3e 318 if(timerClock == 0 || prescale > TMR_PRESCALE_DIV_2_12)
Anna Bridge 186:707f6e361f3e 319 return E_UNINITIALIZED;
Anna Bridge 186:707f6e361f3e 320
Anna Bridge 186:707f6e361f3e 321 switch (units) {
Anna Bridge 186:707f6e361f3e 322 case TMR_UNIT_NANOSEC:
Anna Bridge 186:707f6e361f3e 323 unit_div0 = 1000000;
Anna Bridge 186:707f6e361f3e 324 unit_div1 = 1000;
Anna Bridge 186:707f6e361f3e 325 break;
Anna Bridge 186:707f6e361f3e 326 case TMR_UNIT_MICROSEC:
Anna Bridge 186:707f6e361f3e 327 unit_div0 = 1000;
Anna Bridge 186:707f6e361f3e 328 unit_div1 = 1000;
Anna Bridge 186:707f6e361f3e 329 break;
Anna Bridge 186:707f6e361f3e 330 case TMR_UNIT_MILLISEC:
Anna Bridge 186:707f6e361f3e 331 unit_div0 = 1;
Anna Bridge 186:707f6e361f3e 332 unit_div1 = 1000;
Anna Bridge 186:707f6e361f3e 333 break;
Anna Bridge 186:707f6e361f3e 334 case TMR_UNIT_SEC:
Anna Bridge 186:707f6e361f3e 335 unit_div0 = 1;
Anna Bridge 186:707f6e361f3e 336 unit_div1 = 1;
Anna Bridge 186:707f6e361f3e 337 break;
Anna Bridge 186:707f6e361f3e 338 default:
Anna Bridge 186:707f6e361f3e 339 return E_BAD_PARAM;
Anna Bridge 186:707f6e361f3e 340 }
Anna Bridge 186:707f6e361f3e 341
Anna Bridge 186:707f6e361f3e 342 temp_ticks = (uint64_t)time * (timerClock / unit_div0) / (unit_div1 * (1 << (prescale & 0xF)));
Anna Bridge 186:707f6e361f3e 343
Anna Bridge 186:707f6e361f3e 344 //make sure ticks is within a 32 bit value
Anna Bridge 186:707f6e361f3e 345 if (!(temp_ticks & 0xffffffffffff0000) && (temp_ticks & 0xffff)) {
Anna Bridge 186:707f6e361f3e 346 *ticks = temp_ticks;
Anna Bridge 186:707f6e361f3e 347 return E_NO_ERROR;
Anna Bridge 186:707f6e361f3e 348 }
Anna Bridge 186:707f6e361f3e 349
Anna Bridge 186:707f6e361f3e 350 return E_INVALID;
Anna Bridge 186:707f6e361f3e 351 }
Anna Bridge 186:707f6e361f3e 352
Anna Bridge 186:707f6e361f3e 353
Anna Bridge 186:707f6e361f3e 354 /* ************************************************************************* */
Anna Bridge 186:707f6e361f3e 355 int TMR_TicksToTime(mxc_tmr_regs_t *tmr, uint32_t ticks, uint32_t *time, tmr_unit_t *units)
Anna Bridge 186:707f6e361f3e 356 {
Anna Bridge 186:707f6e361f3e 357 uint64_t temp_time = 0;
Anna Bridge 186:707f6e361f3e 358
Anna Bridge 186:707f6e361f3e 359 uint32_t timerClock = SYS_TMR_GetFreq(tmr);
Anna Bridge 186:707f6e361f3e 360 uint32_t prescale = TMR_GetPrescaler(tmr);
Anna Bridge 186:707f6e361f3e 361
Anna Bridge 186:707f6e361f3e 362 if(timerClock == 0 || prescale > TMR_PRESCALE_DIV_2_12)
Anna Bridge 186:707f6e361f3e 363 return E_UNINITIALIZED;
Anna Bridge 186:707f6e361f3e 364
Anna Bridge 186:707f6e361f3e 365 tmr_unit_t temp_unit = TMR_UNIT_NANOSEC;
Anna Bridge 186:707f6e361f3e 366 temp_time = (uint64_t)ticks * 1000 * (1 << (prescale & 0xF)) / (timerClock / 1000000);
Anna Bridge 186:707f6e361f3e 367 if (!(temp_time & 0xffffffff00000000)) {
Anna Bridge 186:707f6e361f3e 368 *time = temp_time;
Anna Bridge 186:707f6e361f3e 369 *units = temp_unit;
Anna Bridge 186:707f6e361f3e 370 return E_NO_ERROR;
Anna Bridge 186:707f6e361f3e 371 }
Anna Bridge 186:707f6e361f3e 372
Anna Bridge 186:707f6e361f3e 373 temp_unit = TMR_UNIT_MICROSEC;
Anna Bridge 186:707f6e361f3e 374 temp_time = (uint64_t)ticks * 1000 * (1 << (prescale & 0xF)) / (timerClock / 1000);
Anna Bridge 186:707f6e361f3e 375 if (!(temp_time & 0xffffffff00000000)) {
Anna Bridge 186:707f6e361f3e 376 *time = temp_time;
Anna Bridge 186:707f6e361f3e 377 *units = temp_unit;
Anna Bridge 186:707f6e361f3e 378 return E_NO_ERROR;
Anna Bridge 186:707f6e361f3e 379 }
Anna Bridge 186:707f6e361f3e 380
Anna Bridge 186:707f6e361f3e 381 temp_unit = TMR_UNIT_MILLISEC;
Anna Bridge 186:707f6e361f3e 382 temp_time = (uint64_t)ticks * 1000 * (1 << (prescale & 0xF)) / timerClock;
Anna Bridge 186:707f6e361f3e 383 if (!(temp_time & 0xffffffff00000000)) {
Anna Bridge 186:707f6e361f3e 384 *time = temp_time;
Anna Bridge 186:707f6e361f3e 385 *units = temp_unit;
Anna Bridge 186:707f6e361f3e 386 return E_NO_ERROR;
Anna Bridge 186:707f6e361f3e 387 }
Anna Bridge 186:707f6e361f3e 388
Anna Bridge 186:707f6e361f3e 389 temp_unit = TMR_UNIT_SEC;
Anna Bridge 186:707f6e361f3e 390 temp_time = (uint64_t)ticks * (1 << (prescale & 0xF)) / timerClock;
Anna Bridge 186:707f6e361f3e 391 if (!(temp_time & 0xffffffff00000000)) {
Anna Bridge 186:707f6e361f3e 392 *time = temp_time;
Anna Bridge 186:707f6e361f3e 393 *units = temp_unit;
Anna Bridge 186:707f6e361f3e 394 return E_NO_ERROR;
Anna Bridge 186:707f6e361f3e 395 }
Anna Bridge 186:707f6e361f3e 396
Anna Bridge 186:707f6e361f3e 397 return E_INVALID;
Anna Bridge 186:707f6e361f3e 398 }
Anna Bridge 186:707f6e361f3e 399 /**@} end of ingroup tmr */