t
Fork of mbed-dev by
targets/TARGET_Maxim/TARGET_MAX32625/mxc/tmr_utils.c@178:c26431f84b0d, 2017-11-09 (annotated)
- Committer:
- amithy
- Date:
- Thu Nov 09 22:14:37 2017 +0000
- Revision:
- 178:c26431f84b0d
- Parent:
- 150:02e0a0aed4ec
test export
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 150:02e0a0aed4ec | 1 | /******************************************************************************* |
<> | 150:02e0a0aed4ec | 2 | * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. |
<> | 150:02e0a0aed4ec | 3 | * |
<> | 150:02e0a0aed4ec | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
<> | 150:02e0a0aed4ec | 5 | * copy of this software and associated documentation files (the "Software"), |
<> | 150:02e0a0aed4ec | 6 | * to deal in the Software without restriction, including without limitation |
<> | 150:02e0a0aed4ec | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
<> | 150:02e0a0aed4ec | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
<> | 150:02e0a0aed4ec | 9 | * Software is furnished to do so, subject to the following conditions: |
<> | 150:02e0a0aed4ec | 10 | * |
<> | 150:02e0a0aed4ec | 11 | * The above copyright notice and this permission notice shall be included |
<> | 150:02e0a0aed4ec | 12 | * in all copies or substantial portions of the Software. |
<> | 150:02e0a0aed4ec | 13 | * |
<> | 150:02e0a0aed4ec | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
<> | 150:02e0a0aed4ec | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
<> | 150:02e0a0aed4ec | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
<> | 150:02e0a0aed4ec | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
<> | 150:02e0a0aed4ec | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
<> | 150:02e0a0aed4ec | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
<> | 150:02e0a0aed4ec | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
<> | 150:02e0a0aed4ec | 21 | * |
<> | 150:02e0a0aed4ec | 22 | * Except as contained in this notice, the name of Maxim Integrated |
<> | 150:02e0a0aed4ec | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
<> | 150:02e0a0aed4ec | 24 | * Products, Inc. Branding Policy. |
<> | 150:02e0a0aed4ec | 25 | * |
<> | 150:02e0a0aed4ec | 26 | * The mere transfer of this software does not imply any licenses |
<> | 150:02e0a0aed4ec | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
<> | 150:02e0a0aed4ec | 28 | * trademarks, maskwork rights, or any other form of intellectual |
<> | 150:02e0a0aed4ec | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
<> | 150:02e0a0aed4ec | 30 | * ownership rights. |
<> | 150:02e0a0aed4ec | 31 | * |
<> | 150:02e0a0aed4ec | 32 | * $Date: 2016-03-11 11:46:37 -0600 (Fri, 11 Mar 2016) $ |
<> | 150:02e0a0aed4ec | 33 | * $Revision: 21839 $ |
<> | 150:02e0a0aed4ec | 34 | * |
<> | 150:02e0a0aed4ec | 35 | ******************************************************************************/ |
<> | 150:02e0a0aed4ec | 36 | |
<> | 150:02e0a0aed4ec | 37 | /** |
<> | 150:02e0a0aed4ec | 38 | * @file tmr_utils.c |
<> | 150:02e0a0aed4ec | 39 | * @brief Timer utility functions. |
<> | 150:02e0a0aed4ec | 40 | */ |
<> | 150:02e0a0aed4ec | 41 | |
<> | 150:02e0a0aed4ec | 42 | /***** Includes *****/ |
<> | 150:02e0a0aed4ec | 43 | #include <stddef.h> |
<> | 150:02e0a0aed4ec | 44 | #include "mxc_assert.h" |
<> | 150:02e0a0aed4ec | 45 | #include "tmr.h" |
<> | 150:02e0a0aed4ec | 46 | #include "tmr_utils.h" |
<> | 150:02e0a0aed4ec | 47 | |
<> | 150:02e0a0aed4ec | 48 | /***** Definitions *****/ |
<> | 150:02e0a0aed4ec | 49 | |
<> | 150:02e0a0aed4ec | 50 | /***** Globals *****/ |
<> | 150:02e0a0aed4ec | 51 | |
<> | 150:02e0a0aed4ec | 52 | /***** Functions *****/ |
<> | 150:02e0a0aed4ec | 53 | |
<> | 150:02e0a0aed4ec | 54 | /******************************************************************************/ |
<> | 150:02e0a0aed4ec | 55 | void TMR_Delay(mxc_tmr_regs_t* tmr, unsigned long us) |
<> | 150:02e0a0aed4ec | 56 | { |
<> | 150:02e0a0aed4ec | 57 | TMR_TO_Start(tmr, us); |
<> | 150:02e0a0aed4ec | 58 | |
<> | 150:02e0a0aed4ec | 59 | while(TMR_TO_Check(tmr) != E_TIME_OUT) {} |
<> | 150:02e0a0aed4ec | 60 | } |
<> | 150:02e0a0aed4ec | 61 | |
<> | 150:02e0a0aed4ec | 62 | /******************************************************************************/ |
<> | 150:02e0a0aed4ec | 63 | void TMR_TO_Start(mxc_tmr_regs_t* tmr, unsigned long us) |
<> | 150:02e0a0aed4ec | 64 | { |
<> | 150:02e0a0aed4ec | 65 | unsigned clk_shift = 0; |
<> | 150:02e0a0aed4ec | 66 | uint64_t max_us; |
<> | 150:02e0a0aed4ec | 67 | uint32_t ticks; |
<> | 150:02e0a0aed4ec | 68 | |
<> | 150:02e0a0aed4ec | 69 | // Adjust the clk shift amout by how long the timeout is |
<> | 150:02e0a0aed4ec | 70 | // Start with the fastest clock to give the greatest accuracy |
<> | 150:02e0a0aed4ec | 71 | do { |
<> | 150:02e0a0aed4ec | 72 | max_us = (uint64_t)((0xFFFFFFFFUL / ((uint64_t)SystemCoreClock >> clk_shift++)) * 1000000UL); |
<> | 150:02e0a0aed4ec | 73 | } while(us > max_us); |
<> | 150:02e0a0aed4ec | 74 | |
<> | 150:02e0a0aed4ec | 75 | // Calculate the number of timer ticks we need to wait |
<> | 150:02e0a0aed4ec | 76 | TMR_Init(tmr, (tmr_prescale_t)clk_shift, NULL); |
<> | 150:02e0a0aed4ec | 77 | TMR32_TimeToTicks(tmr, us, TMR_UNIT_MICROSEC, &ticks); |
<> | 150:02e0a0aed4ec | 78 | |
<> | 150:02e0a0aed4ec | 79 | // Initialize the timer in one-shot mode |
<> | 150:02e0a0aed4ec | 80 | tmr32_cfg_t cfg; |
<> | 150:02e0a0aed4ec | 81 | cfg.mode = TMR32_MODE_ONE_SHOT; |
<> | 150:02e0a0aed4ec | 82 | cfg.compareCount = ticks; |
<> | 150:02e0a0aed4ec | 83 | TMR32_Stop(tmr); |
<> | 150:02e0a0aed4ec | 84 | TMR32_Config(tmr, &cfg); |
<> | 150:02e0a0aed4ec | 85 | |
<> | 150:02e0a0aed4ec | 86 | TMR32_ClearFlag(tmr); |
<> | 150:02e0a0aed4ec | 87 | TMR32_Start(tmr); |
<> | 150:02e0a0aed4ec | 88 | } |
<> | 150:02e0a0aed4ec | 89 | |
<> | 150:02e0a0aed4ec | 90 | /******************************************************************************/ |
<> | 150:02e0a0aed4ec | 91 | int TMR_TO_Check(mxc_tmr_regs_t* tmr) |
<> | 150:02e0a0aed4ec | 92 | { |
<> | 150:02e0a0aed4ec | 93 | if(TMR32_GetFlag(tmr)) { |
<> | 150:02e0a0aed4ec | 94 | return E_TIME_OUT; |
<> | 150:02e0a0aed4ec | 95 | } |
<> | 150:02e0a0aed4ec | 96 | return E_NO_ERROR; |
<> | 150:02e0a0aed4ec | 97 | } |
<> | 150:02e0a0aed4ec | 98 | |
<> | 150:02e0a0aed4ec | 99 | /******************************************************************************/ |
<> | 150:02e0a0aed4ec | 100 | void TMR_TO_Stop(mxc_tmr_regs_t* tmr) |
<> | 150:02e0a0aed4ec | 101 | { |
<> | 150:02e0a0aed4ec | 102 | TMR32_Stop(tmr); |
<> | 150:02e0a0aed4ec | 103 | TMR32_SetCount(tmr, 0x0); |
<> | 150:02e0a0aed4ec | 104 | } |
<> | 150:02e0a0aed4ec | 105 | |
<> | 150:02e0a0aed4ec | 106 | /******************************************************************************/ |
<> | 150:02e0a0aed4ec | 107 | void TMR_TO_Clear(mxc_tmr_regs_t* tmr) |
<> | 150:02e0a0aed4ec | 108 | { |
<> | 150:02e0a0aed4ec | 109 | TMR32_ClearFlag(tmr); |
<> | 150:02e0a0aed4ec | 110 | TMR32_SetCount(tmr, 0x0); |
<> | 150:02e0a0aed4ec | 111 | } |
<> | 150:02e0a0aed4ec | 112 | |
<> | 150:02e0a0aed4ec | 113 | /******************************************************************************/ |
<> | 150:02e0a0aed4ec | 114 | unsigned TMR_TO_Elapsed(mxc_tmr_regs_t* tmr) |
<> | 150:02e0a0aed4ec | 115 | { |
<> | 150:02e0a0aed4ec | 116 | uint32_t elapsed; |
<> | 150:02e0a0aed4ec | 117 | tmr_unit_t units; |
<> | 150:02e0a0aed4ec | 118 | |
<> | 150:02e0a0aed4ec | 119 | TMR_TicksToTime(tmr, TMR32_GetCount(tmr), &elapsed, &units); |
<> | 150:02e0a0aed4ec | 120 | |
<> | 150:02e0a0aed4ec | 121 | switch(units) { |
<> | 150:02e0a0aed4ec | 122 | case TMR_UNIT_NANOSEC: |
<> | 150:02e0a0aed4ec | 123 | default: |
<> | 150:02e0a0aed4ec | 124 | return (elapsed / 1000); |
<> | 150:02e0a0aed4ec | 125 | case TMR_UNIT_MICROSEC: |
<> | 150:02e0a0aed4ec | 126 | return (elapsed); |
<> | 150:02e0a0aed4ec | 127 | case TMR_UNIT_MILLISEC: |
<> | 150:02e0a0aed4ec | 128 | return (elapsed * 1000); |
<> | 150:02e0a0aed4ec | 129 | case TMR_UNIT_SEC: |
<> | 150:02e0a0aed4ec | 130 | return (elapsed * 1000000); |
<> | 150:02e0a0aed4ec | 131 | } |
<> | 150:02e0a0aed4ec | 132 | } |
<> | 150:02e0a0aed4ec | 133 | |
<> | 150:02e0a0aed4ec | 134 | /******************************************************************************/ |
<> | 150:02e0a0aed4ec | 135 | unsigned TMR_TO_Remaining(mxc_tmr_regs_t* tmr) |
<> | 150:02e0a0aed4ec | 136 | { |
<> | 150:02e0a0aed4ec | 137 | uint32_t remaining_ticks, remaining_time; |
<> | 150:02e0a0aed4ec | 138 | tmr_unit_t units; |
<> | 150:02e0a0aed4ec | 139 | |
<> | 150:02e0a0aed4ec | 140 | remaining_ticks = TMR32_GetCompare(tmr) - TMR32_GetCount(tmr); |
<> | 150:02e0a0aed4ec | 141 | TMR_TicksToTime(tmr, remaining_ticks, &remaining_time, &units); |
<> | 150:02e0a0aed4ec | 142 | |
<> | 150:02e0a0aed4ec | 143 | switch(units) { |
<> | 150:02e0a0aed4ec | 144 | case TMR_UNIT_NANOSEC: |
<> | 150:02e0a0aed4ec | 145 | default: |
<> | 150:02e0a0aed4ec | 146 | return (remaining_time / 1000); |
<> | 150:02e0a0aed4ec | 147 | case TMR_UNIT_MICROSEC: |
<> | 150:02e0a0aed4ec | 148 | return (remaining_time); |
<> | 150:02e0a0aed4ec | 149 | case TMR_UNIT_MILLISEC: |
<> | 150:02e0a0aed4ec | 150 | return (remaining_time * 1000); |
<> | 150:02e0a0aed4ec | 151 | case TMR_UNIT_SEC: |
<> | 150:02e0a0aed4ec | 152 | return (remaining_time * 1000000); |
<> | 150:02e0a0aed4ec | 153 | } |
<> | 150:02e0a0aed4ec | 154 | } |
<> | 150:02e0a0aed4ec | 155 | |
<> | 150:02e0a0aed4ec | 156 | /******************************************************************************/ |
<> | 150:02e0a0aed4ec | 157 | void TMR_SW_Start(mxc_tmr_regs_t* tmr) |
<> | 150:02e0a0aed4ec | 158 | { |
<> | 150:02e0a0aed4ec | 159 | TMR_TO_Start(tmr, 0xFFFFFFFF); |
<> | 150:02e0a0aed4ec | 160 | } |
<> | 150:02e0a0aed4ec | 161 | |
<> | 150:02e0a0aed4ec | 162 | /******************************************************************************/ |
<> | 150:02e0a0aed4ec | 163 | unsigned TMR_SW_Stop(mxc_tmr_regs_t* tmr) |
<> | 150:02e0a0aed4ec | 164 | { |
<> | 150:02e0a0aed4ec | 165 | unsigned elapsed = TMR_TO_Elapsed(tmr); |
<> | 150:02e0a0aed4ec | 166 | TMR_TO_Stop(tmr); |
<> | 150:02e0a0aed4ec | 167 | return elapsed; |
<> | 150:02e0a0aed4ec | 168 | } |