Mouse code for the MacroRat
mbed-dev/targets/TARGET_ARM_SSG/TARGET_BEETLE/device/apb_dualtimer.c@18:6a4db94011d3, 2017-05-14 (annotated)
- Committer:
- sahilmgandhi
- Date:
- Sun May 14 23:18:57 2017 +0000
- Revision:
- 18:6a4db94011d3
Publishing again
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sahilmgandhi | 18:6a4db94011d3 | 1 | /* mbed Microcontroller Library |
sahilmgandhi | 18:6a4db94011d3 | 2 | * Copyright (c) 2016 ARM Limited |
sahilmgandhi | 18:6a4db94011d3 | 3 | * |
sahilmgandhi | 18:6a4db94011d3 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
sahilmgandhi | 18:6a4db94011d3 | 5 | * you may not use this file except in compliance with the License. |
sahilmgandhi | 18:6a4db94011d3 | 6 | * You may obtain a copy of the License at |
sahilmgandhi | 18:6a4db94011d3 | 7 | * |
sahilmgandhi | 18:6a4db94011d3 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
sahilmgandhi | 18:6a4db94011d3 | 9 | * |
sahilmgandhi | 18:6a4db94011d3 | 10 | * Unless required by applicable law or agreed to in writing, software |
sahilmgandhi | 18:6a4db94011d3 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
sahilmgandhi | 18:6a4db94011d3 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sahilmgandhi | 18:6a4db94011d3 | 13 | * See the License for the specific language governing permissions and |
sahilmgandhi | 18:6a4db94011d3 | 14 | * limitations under the License. |
sahilmgandhi | 18:6a4db94011d3 | 15 | */ |
sahilmgandhi | 18:6a4db94011d3 | 16 | #include "cmsis.h" |
sahilmgandhi | 18:6a4db94011d3 | 17 | #include "apb_dualtimer.h" |
sahilmgandhi | 18:6a4db94011d3 | 18 | |
sahilmgandhi | 18:6a4db94011d3 | 19 | /* DualTimer Private Data */ |
sahilmgandhi | 18:6a4db94011d3 | 20 | typedef struct { |
sahilmgandhi | 18:6a4db94011d3 | 21 | /* DualTimer 1 Definition */ |
sahilmgandhi | 18:6a4db94011d3 | 22 | CMSDK_DUALTIMER_SINGLE_TypeDef *dualtimer1; |
sahilmgandhi | 18:6a4db94011d3 | 23 | /* DualTimer 2 Definition */ |
sahilmgandhi | 18:6a4db94011d3 | 24 | CMSDK_DUALTIMER_SINGLE_TypeDef *dualtimer2; |
sahilmgandhi | 18:6a4db94011d3 | 25 | /* Dual Timer IRQn */ |
sahilmgandhi | 18:6a4db94011d3 | 26 | uint32_t dualtimerIRQn; |
sahilmgandhi | 18:6a4db94011d3 | 27 | /* DualTimer 1 Reload Value */ |
sahilmgandhi | 18:6a4db94011d3 | 28 | uint32_t dualtimer1Reload; |
sahilmgandhi | 18:6a4db94011d3 | 29 | /* DualTimer 2 Reload Value */ |
sahilmgandhi | 18:6a4db94011d3 | 30 | uint32_t dualtimer2Reload; |
sahilmgandhi | 18:6a4db94011d3 | 31 | /* Timer state */ |
sahilmgandhi | 18:6a4db94011d3 | 32 | uint32_t state; |
sahilmgandhi | 18:6a4db94011d3 | 33 | } apb_dualtimer_t; |
sahilmgandhi | 18:6a4db94011d3 | 34 | |
sahilmgandhi | 18:6a4db94011d3 | 35 | /* Timer state definitions */ |
sahilmgandhi | 18:6a4db94011d3 | 36 | #define DUALTIMER_INITIALIZED (1) |
sahilmgandhi | 18:6a4db94011d3 | 37 | #define DUALTIMER_ENABLED (1 << 1) |
sahilmgandhi | 18:6a4db94011d3 | 38 | |
sahilmgandhi | 18:6a4db94011d3 | 39 | /* |
sahilmgandhi | 18:6a4db94011d3 | 40 | * This Timer is written for MBED OS and keeps count |
sahilmgandhi | 18:6a4db94011d3 | 41 | * of the ticks. All the elaboration logic is demanded |
sahilmgandhi | 18:6a4db94011d3 | 42 | * to the upper layers. |
sahilmgandhi | 18:6a4db94011d3 | 43 | */ |
sahilmgandhi | 18:6a4db94011d3 | 44 | #define DUALTIMER_MAX_VALUE 0xFFFFFFFF |
sahilmgandhi | 18:6a4db94011d3 | 45 | #define DUALTIMER_TICKS_US (SystemCoreClock/1000000) |
sahilmgandhi | 18:6a4db94011d3 | 46 | |
sahilmgandhi | 18:6a4db94011d3 | 47 | /* Dual Timers Array */ |
sahilmgandhi | 18:6a4db94011d3 | 48 | static apb_dualtimer_t DualTimers[NUM_DUALTIMERS]; |
sahilmgandhi | 18:6a4db94011d3 | 49 | |
sahilmgandhi | 18:6a4db94011d3 | 50 | /* |
sahilmgandhi | 18:6a4db94011d3 | 51 | * DualTimer_Initialize(): Initializes a hardware timer |
sahilmgandhi | 18:6a4db94011d3 | 52 | * timer: timer to be Initialized |
sahilmgandhi | 18:6a4db94011d3 | 53 | * time_us: timer reload value in us - 0 to reload to timer max value |
sahilmgandhi | 18:6a4db94011d3 | 54 | * time_us = ticks_value / TIMER_TICK_US |
sahilmgandhi | 18:6a4db94011d3 | 55 | */ |
sahilmgandhi | 18:6a4db94011d3 | 56 | void DualTimer_Initialize(uint32_t timer, uint32_t time_us) |
sahilmgandhi | 18:6a4db94011d3 | 57 | { |
sahilmgandhi | 18:6a4db94011d3 | 58 | uint32_t reload = 0; |
sahilmgandhi | 18:6a4db94011d3 | 59 | |
sahilmgandhi | 18:6a4db94011d3 | 60 | if (timer < NUM_DUALTIMERS) |
sahilmgandhi | 18:6a4db94011d3 | 61 | { |
sahilmgandhi | 18:6a4db94011d3 | 62 | if (time_us == 0) |
sahilmgandhi | 18:6a4db94011d3 | 63 | reload = DUALTIMER_MAX_VALUE; |
sahilmgandhi | 18:6a4db94011d3 | 64 | else |
sahilmgandhi | 18:6a4db94011d3 | 65 | reload = (time_us) * DUALTIMER_TICKS_US; |
sahilmgandhi | 18:6a4db94011d3 | 66 | |
sahilmgandhi | 18:6a4db94011d3 | 67 | switch(timer) { |
sahilmgandhi | 18:6a4db94011d3 | 68 | case 0: DualTimers[timer].dualtimer1 = CMSDK_DUALTIMER1; |
sahilmgandhi | 18:6a4db94011d3 | 69 | DualTimers[timer].dualtimer2 = CMSDK_DUALTIMER2; |
sahilmgandhi | 18:6a4db94011d3 | 70 | DualTimers[timer].dualtimerIRQn = DUALTIMER_IRQn; |
sahilmgandhi | 18:6a4db94011d3 | 71 | DualTimers[timer].dualtimer1Reload = reload; |
sahilmgandhi | 18:6a4db94011d3 | 72 | DualTimers[timer].dualtimer2Reload = reload; |
sahilmgandhi | 18:6a4db94011d3 | 73 | DualTimers[timer].state = DUALTIMER_INITIALIZED; |
sahilmgandhi | 18:6a4db94011d3 | 74 | default: break; |
sahilmgandhi | 18:6a4db94011d3 | 75 | } |
sahilmgandhi | 18:6a4db94011d3 | 76 | } |
sahilmgandhi | 18:6a4db94011d3 | 77 | } |
sahilmgandhi | 18:6a4db94011d3 | 78 | |
sahilmgandhi | 18:6a4db94011d3 | 79 | /* |
sahilmgandhi | 18:6a4db94011d3 | 80 | * DualTimer_ReturnMode(): returns the correct mode for Dual Timer Control |
sahilmgandhi | 18:6a4db94011d3 | 81 | * mode: mode set by user |
sahilmgandhi | 18:6a4db94011d3 | 82 | * @return: mode for TimeControl register |
sahilmgandhi | 18:6a4db94011d3 | 83 | */ |
sahilmgandhi | 18:6a4db94011d3 | 84 | uint32_t DualTimer_ReturnMode(timerenable_t mode) |
sahilmgandhi | 18:6a4db94011d3 | 85 | { |
sahilmgandhi | 18:6a4db94011d3 | 86 | uint32_t return_mode = 0; |
sahilmgandhi | 18:6a4db94011d3 | 87 | /* Check Interrupt Enable */ |
sahilmgandhi | 18:6a4db94011d3 | 88 | if (((mode & DUALTIMER_INT) >> DUALTIMER_INT_MASK) == 1) |
sahilmgandhi | 18:6a4db94011d3 | 89 | return_mode |= CMSDK_DUALTIMER_CTRL_INTEN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 90 | /* Check 32 bit Counter */ |
sahilmgandhi | 18:6a4db94011d3 | 91 | if (((mode & DUALTIMER_COUNT_32) >> DUALTIMER_COUNT_32_MASK) == 1) |
sahilmgandhi | 18:6a4db94011d3 | 92 | return_mode |= CMSDK_DUALTIMER_CTRL_SIZE_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 93 | /* Check Periodic Mode */ |
sahilmgandhi | 18:6a4db94011d3 | 94 | if (((mode & DUALTIMER_PERIODIC) >> DUALTIMER_PERIODIC_MASK) == 1) |
sahilmgandhi | 18:6a4db94011d3 | 95 | return_mode |= CMSDK_DUALTIMER_CTRL_MODE_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 96 | /* Check OneShot Mode */ |
sahilmgandhi | 18:6a4db94011d3 | 97 | if (((mode & DUALTIMER_ONESHOT) >> DUALTIMER_ONESHOT_MASK) == 1) |
sahilmgandhi | 18:6a4db94011d3 | 98 | return_mode |= CMSDK_DUALTIMER_CTRL_ONESHOOT_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 99 | |
sahilmgandhi | 18:6a4db94011d3 | 100 | return return_mode; |
sahilmgandhi | 18:6a4db94011d3 | 101 | } |
sahilmgandhi | 18:6a4db94011d3 | 102 | |
sahilmgandhi | 18:6a4db94011d3 | 103 | /* |
sahilmgandhi | 18:6a4db94011d3 | 104 | * DualTimer_Enable(): Enables a hardware timer |
sahilmgandhi | 18:6a4db94011d3 | 105 | * timer: timer to be enabled |
sahilmgandhi | 18:6a4db94011d3 | 106 | * mode: enable mode |
sahilmgandhi | 18:6a4db94011d3 | 107 | */ |
sahilmgandhi | 18:6a4db94011d3 | 108 | void DualTimer_Enable(uint32_t timer, timerenable_t mode) |
sahilmgandhi | 18:6a4db94011d3 | 109 | { |
sahilmgandhi | 18:6a4db94011d3 | 110 | uint32_t dualtimerControl = 0; |
sahilmgandhi | 18:6a4db94011d3 | 111 | /* The timer has to be contained in a valid range */ |
sahilmgandhi | 18:6a4db94011d3 | 112 | if (timer < NUM_DUALTIMERS) { |
sahilmgandhi | 18:6a4db94011d3 | 113 | /* Timer has to be already initialized */ |
sahilmgandhi | 18:6a4db94011d3 | 114 | if (DualTimers[timer].state == DUALTIMER_INITIALIZED) { |
sahilmgandhi | 18:6a4db94011d3 | 115 | /* Disable Timer */ |
sahilmgandhi | 18:6a4db94011d3 | 116 | (DualTimers[timer].dualtimer1)->TimerControl = 0x0; |
sahilmgandhi | 18:6a4db94011d3 | 117 | (DualTimers[timer].dualtimer2)->TimerControl = 0x0; |
sahilmgandhi | 18:6a4db94011d3 | 118 | /* Reload Value */ |
sahilmgandhi | 18:6a4db94011d3 | 119 | (DualTimers[timer].dualtimer1)->TimerLoad = |
sahilmgandhi | 18:6a4db94011d3 | 120 | DualTimers[timer].dualtimer1Reload; |
sahilmgandhi | 18:6a4db94011d3 | 121 | (DualTimers[timer].dualtimer2)->TimerLoad = |
sahilmgandhi | 18:6a4db94011d3 | 122 | DualTimers[timer].dualtimer2Reload; |
sahilmgandhi | 18:6a4db94011d3 | 123 | /* Set up Dual Timer Control */ |
sahilmgandhi | 18:6a4db94011d3 | 124 | dualtimerControl = DualTimer_ReturnMode(mode); |
sahilmgandhi | 18:6a4db94011d3 | 125 | (DualTimers[timer].dualtimer1)->TimerControl = dualtimerControl; |
sahilmgandhi | 18:6a4db94011d3 | 126 | (DualTimers[timer].dualtimer2)->TimerControl = dualtimerControl; |
sahilmgandhi | 18:6a4db94011d3 | 127 | /* Enable Counter */ |
sahilmgandhi | 18:6a4db94011d3 | 128 | (DualTimers[timer].dualtimer1)->TimerControl |= |
sahilmgandhi | 18:6a4db94011d3 | 129 | CMSDK_DUALTIMER_CTRL_EN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 130 | (DualTimers[timer].dualtimer2)->TimerControl |= |
sahilmgandhi | 18:6a4db94011d3 | 131 | CMSDK_DUALTIMER_CTRL_EN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 132 | /* Change timer state */ |
sahilmgandhi | 18:6a4db94011d3 | 133 | DualTimers[timer].state |= DUALTIMER_ENABLED; |
sahilmgandhi | 18:6a4db94011d3 | 134 | } |
sahilmgandhi | 18:6a4db94011d3 | 135 | } |
sahilmgandhi | 18:6a4db94011d3 | 136 | } |
sahilmgandhi | 18:6a4db94011d3 | 137 | |
sahilmgandhi | 18:6a4db94011d3 | 138 | /* |
sahilmgandhi | 18:6a4db94011d3 | 139 | * DualTimer_Disable(): Disables a hardware timer |
sahilmgandhi | 18:6a4db94011d3 | 140 | * timer: timer to be disabled |
sahilmgandhi | 18:6a4db94011d3 | 141 | * dis_timer: 0 both - 1 dual timer 1 - 2 dual timer 2 |
sahilmgandhi | 18:6a4db94011d3 | 142 | */ |
sahilmgandhi | 18:6a4db94011d3 | 143 | void DualTimer_Disable(uint32_t timer, uint32_t dis_timer) |
sahilmgandhi | 18:6a4db94011d3 | 144 | { |
sahilmgandhi | 18:6a4db94011d3 | 145 | /* The timer has to be contained in a valid range */ |
sahilmgandhi | 18:6a4db94011d3 | 146 | if (timer < NUM_DUALTIMERS) { |
sahilmgandhi | 18:6a4db94011d3 | 147 | /* Timer has to be already initialized and enabled */ |
sahilmgandhi | 18:6a4db94011d3 | 148 | if (DualTimers[timer].state == (DUALTIMER_INITIALIZED | DUALTIMER_ENABLED)) { |
sahilmgandhi | 18:6a4db94011d3 | 149 | /* Disable Timer */ |
sahilmgandhi | 18:6a4db94011d3 | 150 | switch (dis_timer) |
sahilmgandhi | 18:6a4db94011d3 | 151 | { |
sahilmgandhi | 18:6a4db94011d3 | 152 | case 0: (DualTimers[timer].dualtimer1)->TimerControl = 0x0; |
sahilmgandhi | 18:6a4db94011d3 | 153 | (DualTimers[timer].dualtimer2)->TimerControl = 0x0; |
sahilmgandhi | 18:6a4db94011d3 | 154 | break; |
sahilmgandhi | 18:6a4db94011d3 | 155 | case 1: (DualTimers[timer].dualtimer1)->TimerControl = 0x0; |
sahilmgandhi | 18:6a4db94011d3 | 156 | break; |
sahilmgandhi | 18:6a4db94011d3 | 157 | case 2: (DualTimers[timer].dualtimer2)->TimerControl = 0x0; |
sahilmgandhi | 18:6a4db94011d3 | 158 | break; |
sahilmgandhi | 18:6a4db94011d3 | 159 | default: break; |
sahilmgandhi | 18:6a4db94011d3 | 160 | } |
sahilmgandhi | 18:6a4db94011d3 | 161 | /* Change timer state */ |
sahilmgandhi | 18:6a4db94011d3 | 162 | DualTimers[timer].state = DUALTIMER_INITIALIZED; |
sahilmgandhi | 18:6a4db94011d3 | 163 | } |
sahilmgandhi | 18:6a4db94011d3 | 164 | } |
sahilmgandhi | 18:6a4db94011d3 | 165 | } |
sahilmgandhi | 18:6a4db94011d3 | 166 | |
sahilmgandhi | 18:6a4db94011d3 | 167 | /* |
sahilmgandhi | 18:6a4db94011d3 | 168 | * DualTimer_isEnabled(): verifies if a timer is enabled |
sahilmgandhi | 18:6a4db94011d3 | 169 | * timer: timer to be verified |
sahilmgandhi | 18:6a4db94011d3 | 170 | * @return: 0 disabled - 1 enabled |
sahilmgandhi | 18:6a4db94011d3 | 171 | */ |
sahilmgandhi | 18:6a4db94011d3 | 172 | uint32_t DualTimer_isEnabled(uint32_t timer) |
sahilmgandhi | 18:6a4db94011d3 | 173 | { |
sahilmgandhi | 18:6a4db94011d3 | 174 | /* The timer has to be contained in a valid range */ |
sahilmgandhi | 18:6a4db94011d3 | 175 | if (timer < NUM_DUALTIMERS) { |
sahilmgandhi | 18:6a4db94011d3 | 176 | /* Timer has to be already initialized and enabled */ |
sahilmgandhi | 18:6a4db94011d3 | 177 | if (DualTimers[timer].state == (DUALTIMER_INITIALIZED | DUALTIMER_ENABLED)) |
sahilmgandhi | 18:6a4db94011d3 | 178 | return 1; |
sahilmgandhi | 18:6a4db94011d3 | 179 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 180 | return 0; |
sahilmgandhi | 18:6a4db94011d3 | 181 | } |
sahilmgandhi | 18:6a4db94011d3 | 182 | return 0; |
sahilmgandhi | 18:6a4db94011d3 | 183 | } |
sahilmgandhi | 18:6a4db94011d3 | 184 | |
sahilmgandhi | 18:6a4db94011d3 | 185 | /* |
sahilmgandhi | 18:6a4db94011d3 | 186 | * DualTimer_Read_1(): provides single timer 1 VALUE |
sahilmgandhi | 18:6a4db94011d3 | 187 | * timer: timer to be read |
sahilmgandhi | 18:6a4db94011d3 | 188 | * @return: timer VALUE |
sahilmgandhi | 18:6a4db94011d3 | 189 | */ |
sahilmgandhi | 18:6a4db94011d3 | 190 | uint32_t DualTimer_Read_1(uint32_t timer) |
sahilmgandhi | 18:6a4db94011d3 | 191 | { |
sahilmgandhi | 18:6a4db94011d3 | 192 | uint32_t return_value = 0; |
sahilmgandhi | 18:6a4db94011d3 | 193 | /* Verify if the Timer is enabled */ |
sahilmgandhi | 18:6a4db94011d3 | 194 | if (DualTimer_isEnabled(timer) == 1) { |
sahilmgandhi | 18:6a4db94011d3 | 195 | return_value = (DualTimers[timer].dualtimer1Reload |
sahilmgandhi | 18:6a4db94011d3 | 196 | - (DualTimers[timer].dualtimer1)->TimerValue) |
sahilmgandhi | 18:6a4db94011d3 | 197 | / DUALTIMER_TICKS_US; |
sahilmgandhi | 18:6a4db94011d3 | 198 | } |
sahilmgandhi | 18:6a4db94011d3 | 199 | |
sahilmgandhi | 18:6a4db94011d3 | 200 | return return_value; |
sahilmgandhi | 18:6a4db94011d3 | 201 | } |
sahilmgandhi | 18:6a4db94011d3 | 202 | |
sahilmgandhi | 18:6a4db94011d3 | 203 | /* |
sahilmgandhi | 18:6a4db94011d3 | 204 | * DualTimer_Read_2(): provides single timer 2 VALUE |
sahilmgandhi | 18:6a4db94011d3 | 205 | * timer: timer to be read |
sahilmgandhi | 18:6a4db94011d3 | 206 | * @return: timer VALUE |
sahilmgandhi | 18:6a4db94011d3 | 207 | */ |
sahilmgandhi | 18:6a4db94011d3 | 208 | uint32_t DualTimer_Read_2(uint32_t timer) |
sahilmgandhi | 18:6a4db94011d3 | 209 | { |
sahilmgandhi | 18:6a4db94011d3 | 210 | uint32_t return_value = 0; |
sahilmgandhi | 18:6a4db94011d3 | 211 | /* Verify if the Timer is enabled */ |
sahilmgandhi | 18:6a4db94011d3 | 212 | if (DualTimer_isEnabled(timer) == 1) { |
sahilmgandhi | 18:6a4db94011d3 | 213 | return_value = (DualTimers[timer].dualtimer2Reload |
sahilmgandhi | 18:6a4db94011d3 | 214 | - (DualTimers[timer].dualtimer2)->TimerValue) |
sahilmgandhi | 18:6a4db94011d3 | 215 | / DUALTIMER_TICKS_US; |
sahilmgandhi | 18:6a4db94011d3 | 216 | } |
sahilmgandhi | 18:6a4db94011d3 | 217 | |
sahilmgandhi | 18:6a4db94011d3 | 218 | return return_value; |
sahilmgandhi | 18:6a4db94011d3 | 219 | } |
sahilmgandhi | 18:6a4db94011d3 | 220 | |
sahilmgandhi | 18:6a4db94011d3 | 221 | /* |
sahilmgandhi | 18:6a4db94011d3 | 222 | * DualTimer_SetInterrupt_1(): sets timer 1 Interrupt |
sahilmgandhi | 18:6a4db94011d3 | 223 | * timer: timer on which interrupt is set |
sahilmgandhi | 18:6a4db94011d3 | 224 | * time_us: reloading value us |
sahilmgandhi | 18:6a4db94011d3 | 225 | * mode: enable mode |
sahilmgandhi | 18:6a4db94011d3 | 226 | */ |
sahilmgandhi | 18:6a4db94011d3 | 227 | void DualTimer_SetInterrupt_1(uint32_t timer, uint32_t time_us, |
sahilmgandhi | 18:6a4db94011d3 | 228 | timerenable_t mode) |
sahilmgandhi | 18:6a4db94011d3 | 229 | { |
sahilmgandhi | 18:6a4db94011d3 | 230 | uint32_t dualtimerControl = 0; |
sahilmgandhi | 18:6a4db94011d3 | 231 | uint32_t load_time_us = 0; |
sahilmgandhi | 18:6a4db94011d3 | 232 | /* Verify if the Timer is enabled */ |
sahilmgandhi | 18:6a4db94011d3 | 233 | if (DualTimer_isEnabled(timer) == 1) { |
sahilmgandhi | 18:6a4db94011d3 | 234 | /* Disable Timer */ |
sahilmgandhi | 18:6a4db94011d3 | 235 | DualTimer_Disable(timer, SINGLETIMER1); |
sahilmgandhi | 18:6a4db94011d3 | 236 | /* Set up Dual Timer Control */ |
sahilmgandhi | 18:6a4db94011d3 | 237 | dualtimerControl = DualTimer_ReturnMode(mode); |
sahilmgandhi | 18:6a4db94011d3 | 238 | (DualTimers[timer].dualtimer1)->TimerControl = |
sahilmgandhi | 18:6a4db94011d3 | 239 | CMSDK_DUALTIMER_CTRL_INTEN_Msk |
sahilmgandhi | 18:6a4db94011d3 | 240 | | dualtimerControl; |
sahilmgandhi | 18:6a4db94011d3 | 241 | |
sahilmgandhi | 18:6a4db94011d3 | 242 | /* Check time us condition */ |
sahilmgandhi | 18:6a4db94011d3 | 243 | if(time_us == DUALTIMER_DEFAULT_RELOAD) |
sahilmgandhi | 18:6a4db94011d3 | 244 | load_time_us = DUALTIMER_MAX_VALUE; |
sahilmgandhi | 18:6a4db94011d3 | 245 | else |
sahilmgandhi | 18:6a4db94011d3 | 246 | load_time_us = time_us * DUALTIMER_TICKS_US; |
sahilmgandhi | 18:6a4db94011d3 | 247 | |
sahilmgandhi | 18:6a4db94011d3 | 248 | /* Reload Value */ |
sahilmgandhi | 18:6a4db94011d3 | 249 | DualTimers[timer].dualtimer1Reload = load_time_us; |
sahilmgandhi | 18:6a4db94011d3 | 250 | (DualTimers[timer].dualtimer1)->TimerLoad = |
sahilmgandhi | 18:6a4db94011d3 | 251 | DualTimers[timer].dualtimer1Reload; |
sahilmgandhi | 18:6a4db94011d3 | 252 | /* Enable Counter */ |
sahilmgandhi | 18:6a4db94011d3 | 253 | (DualTimers[timer].dualtimer1)->TimerControl |= |
sahilmgandhi | 18:6a4db94011d3 | 254 | CMSDK_DUALTIMER_CTRL_EN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 255 | /* Change timer state */ |
sahilmgandhi | 18:6a4db94011d3 | 256 | DualTimers[timer].state |= DUALTIMER_ENABLED; |
sahilmgandhi | 18:6a4db94011d3 | 257 | } |
sahilmgandhi | 18:6a4db94011d3 | 258 | } |
sahilmgandhi | 18:6a4db94011d3 | 259 | |
sahilmgandhi | 18:6a4db94011d3 | 260 | /* |
sahilmgandhi | 18:6a4db94011d3 | 261 | * DualTimer_SetInterrupt_2(): sets timer 2 Interrupt |
sahilmgandhi | 18:6a4db94011d3 | 262 | * timer: timer on which interrupt is set |
sahilmgandhi | 18:6a4db94011d3 | 263 | * time_us: reloading value us |
sahilmgandhi | 18:6a4db94011d3 | 264 | * mode: enable mode |
sahilmgandhi | 18:6a4db94011d3 | 265 | */ |
sahilmgandhi | 18:6a4db94011d3 | 266 | void DualTimer_SetInterrupt_2(uint32_t timer, uint32_t time_us, |
sahilmgandhi | 18:6a4db94011d3 | 267 | timerenable_t mode) |
sahilmgandhi | 18:6a4db94011d3 | 268 | { |
sahilmgandhi | 18:6a4db94011d3 | 269 | uint32_t dualtimerControl = 0; |
sahilmgandhi | 18:6a4db94011d3 | 270 | uint32_t load_time_us = 0; |
sahilmgandhi | 18:6a4db94011d3 | 271 | /* Verify if the Timer is enabled */ |
sahilmgandhi | 18:6a4db94011d3 | 272 | if (DualTimer_isEnabled(timer) == 1) { |
sahilmgandhi | 18:6a4db94011d3 | 273 | /* Disable Timer */ |
sahilmgandhi | 18:6a4db94011d3 | 274 | DualTimer_Disable(timer, SINGLETIMER2); |
sahilmgandhi | 18:6a4db94011d3 | 275 | /* Set up Dual Timer Control */ |
sahilmgandhi | 18:6a4db94011d3 | 276 | dualtimerControl = DualTimer_ReturnMode(mode); |
sahilmgandhi | 18:6a4db94011d3 | 277 | (DualTimers[timer].dualtimer2)->TimerControl = |
sahilmgandhi | 18:6a4db94011d3 | 278 | CMSDK_DUALTIMER_CTRL_INTEN_Msk |
sahilmgandhi | 18:6a4db94011d3 | 279 | | dualtimerControl; |
sahilmgandhi | 18:6a4db94011d3 | 280 | |
sahilmgandhi | 18:6a4db94011d3 | 281 | /* Check time us condition */ |
sahilmgandhi | 18:6a4db94011d3 | 282 | if(time_us == DUALTIMER_DEFAULT_RELOAD) |
sahilmgandhi | 18:6a4db94011d3 | 283 | load_time_us = DUALTIMER_MAX_VALUE; |
sahilmgandhi | 18:6a4db94011d3 | 284 | else |
sahilmgandhi | 18:6a4db94011d3 | 285 | load_time_us = time_us * DUALTIMER_TICKS_US; |
sahilmgandhi | 18:6a4db94011d3 | 286 | |
sahilmgandhi | 18:6a4db94011d3 | 287 | /* Reload Value */ |
sahilmgandhi | 18:6a4db94011d3 | 288 | DualTimers[timer].dualtimer2Reload = load_time_us; |
sahilmgandhi | 18:6a4db94011d3 | 289 | (DualTimers[timer].dualtimer2)->TimerLoad = |
sahilmgandhi | 18:6a4db94011d3 | 290 | DualTimers[timer].dualtimer2Reload; |
sahilmgandhi | 18:6a4db94011d3 | 291 | /* Enable Counter */ |
sahilmgandhi | 18:6a4db94011d3 | 292 | (DualTimers[timer].dualtimer2)->TimerControl |= |
sahilmgandhi | 18:6a4db94011d3 | 293 | CMSDK_DUALTIMER_CTRL_EN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 294 | /* Change timer state */ |
sahilmgandhi | 18:6a4db94011d3 | 295 | DualTimers[timer].state |= DUALTIMER_ENABLED; |
sahilmgandhi | 18:6a4db94011d3 | 296 | } |
sahilmgandhi | 18:6a4db94011d3 | 297 | } |
sahilmgandhi | 18:6a4db94011d3 | 298 | |
sahilmgandhi | 18:6a4db94011d3 | 299 | /* |
sahilmgandhi | 18:6a4db94011d3 | 300 | * DualTimer_DisableInterrupt(): disables timer interrupts |
sahilmgandhi | 18:6a4db94011d3 | 301 | * dualimer: dualtimer on which interrupt is disabled |
sahilmgandhi | 18:6a4db94011d3 | 302 | * single_timer: single timer in the dualtimer on which |
sahilmgandhi | 18:6a4db94011d3 | 303 | * interrupt is disabled |
sahilmgandhi | 18:6a4db94011d3 | 304 | */ |
sahilmgandhi | 18:6a4db94011d3 | 305 | void DualTimer_DisableInterrupt(uint32_t dualtimer, |
sahilmgandhi | 18:6a4db94011d3 | 306 | uint32_t single_timer) |
sahilmgandhi | 18:6a4db94011d3 | 307 | { |
sahilmgandhi | 18:6a4db94011d3 | 308 | /* Verify if the Timer is enabled */ |
sahilmgandhi | 18:6a4db94011d3 | 309 | if (DualTimer_isEnabled(dualtimer) == 1) { |
sahilmgandhi | 18:6a4db94011d3 | 310 | switch(single_timer) { |
sahilmgandhi | 18:6a4db94011d3 | 311 | case SINGLETIMER1: |
sahilmgandhi | 18:6a4db94011d3 | 312 | /* Disable Interrupt for single timer 1 */ |
sahilmgandhi | 18:6a4db94011d3 | 313 | (DualTimers[dualtimer].dualtimer1)->TimerControl &= |
sahilmgandhi | 18:6a4db94011d3 | 314 | CMSDK_DUALTIMER_CTRL_EN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 315 | break; |
sahilmgandhi | 18:6a4db94011d3 | 316 | case SINGLETIMER2: |
sahilmgandhi | 18:6a4db94011d3 | 317 | /* Disable Interrupt for single timer 2 */ |
sahilmgandhi | 18:6a4db94011d3 | 318 | (DualTimers[dualtimer].dualtimer2)->TimerControl &= |
sahilmgandhi | 18:6a4db94011d3 | 319 | CMSDK_DUALTIMER_CTRL_EN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 320 | break; |
sahilmgandhi | 18:6a4db94011d3 | 321 | case ALL_SINGLETIMERS: |
sahilmgandhi | 18:6a4db94011d3 | 322 | /* Disable Interrupt for single timer 1 */ |
sahilmgandhi | 18:6a4db94011d3 | 323 | (DualTimers[dualtimer].dualtimer1)->TimerControl &= |
sahilmgandhi | 18:6a4db94011d3 | 324 | CMSDK_DUALTIMER_CTRL_EN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 325 | /* Disable Interrupt for single timer 2 */ |
sahilmgandhi | 18:6a4db94011d3 | 326 | (DualTimers[dualtimer].dualtimer2)->TimerControl &= |
sahilmgandhi | 18:6a4db94011d3 | 327 | CMSDK_DUALTIMER_CTRL_EN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 328 | break; |
sahilmgandhi | 18:6a4db94011d3 | 329 | default: |
sahilmgandhi | 18:6a4db94011d3 | 330 | break; |
sahilmgandhi | 18:6a4db94011d3 | 331 | } |
sahilmgandhi | 18:6a4db94011d3 | 332 | } |
sahilmgandhi | 18:6a4db94011d3 | 333 | } |
sahilmgandhi | 18:6a4db94011d3 | 334 | |
sahilmgandhi | 18:6a4db94011d3 | 335 | /* |
sahilmgandhi | 18:6a4db94011d3 | 336 | * DualTimer_ClearInterrupt(): clear timer interrupt |
sahilmgandhi | 18:6a4db94011d3 | 337 | * timer: timer on which interrupt needs to be cleared |
sahilmgandhi | 18:6a4db94011d3 | 338 | */ |
sahilmgandhi | 18:6a4db94011d3 | 339 | void DualTimer_ClearInterrupt(uint32_t timer) |
sahilmgandhi | 18:6a4db94011d3 | 340 | { |
sahilmgandhi | 18:6a4db94011d3 | 341 | /* Verify if the Timer is enabled */ |
sahilmgandhi | 18:6a4db94011d3 | 342 | if (DualTimer_isEnabled(timer) == 1) { |
sahilmgandhi | 18:6a4db94011d3 | 343 | /* Clear Interrupt */ |
sahilmgandhi | 18:6a4db94011d3 | 344 | (DualTimers[timer].dualtimer1)->TimerIntClr = |
sahilmgandhi | 18:6a4db94011d3 | 345 | CMSDK_DUALTIMER_INTCLR_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 346 | (DualTimers[timer].dualtimer2)->TimerIntClr = |
sahilmgandhi | 18:6a4db94011d3 | 347 | CMSDK_DUALTIMER_INTCLR_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 348 | } |
sahilmgandhi | 18:6a4db94011d3 | 349 | } |
sahilmgandhi | 18:6a4db94011d3 | 350 | |
sahilmgandhi | 18:6a4db94011d3 | 351 | /* |
sahilmgandhi | 18:6a4db94011d3 | 352 | * DualTimer_GetIRQn(): returns IRQn of a DualTimer |
sahilmgandhi | 18:6a4db94011d3 | 353 | * timer: timer on which IRQn is defined - 0 if it is not defined |
sahilmgandhi | 18:6a4db94011d3 | 354 | */ |
sahilmgandhi | 18:6a4db94011d3 | 355 | uint32_t DualTimer_GetIRQn(uint32_t timer) |
sahilmgandhi | 18:6a4db94011d3 | 356 | { |
sahilmgandhi | 18:6a4db94011d3 | 357 | /* Verify if the Timer is enabled */ |
sahilmgandhi | 18:6a4db94011d3 | 358 | if (DualTimer_isEnabled(timer) == 1) { |
sahilmgandhi | 18:6a4db94011d3 | 359 | return DualTimers[timer].dualtimerIRQn; |
sahilmgandhi | 18:6a4db94011d3 | 360 | } |
sahilmgandhi | 18:6a4db94011d3 | 361 | return 0; |
sahilmgandhi | 18:6a4db94011d3 | 362 | } |
sahilmgandhi | 18:6a4db94011d3 | 363 | |
sahilmgandhi | 18:6a4db94011d3 | 364 | /* |
sahilmgandhi | 18:6a4db94011d3 | 365 | * DualTimer_GetIRQInfo(): provides the single timer who caused |
sahilmgandhi | 18:6a4db94011d3 | 366 | * the interrupt. |
sahilmgandhi | 18:6a4db94011d3 | 367 | * dualtimer: dualtimer that triggered the IRQ |
sahilmgandhi | 18:6a4db94011d3 | 368 | * @return: a single timer - 0 if it is not defined |
sahilmgandhi | 18:6a4db94011d3 | 369 | */ |
sahilmgandhi | 18:6a4db94011d3 | 370 | uint32_t DualTimer_GetIRQInfo(uint32_t timer) |
sahilmgandhi | 18:6a4db94011d3 | 371 | { |
sahilmgandhi | 18:6a4db94011d3 | 372 | /* Verify if the Timer is enabled */ |
sahilmgandhi | 18:6a4db94011d3 | 373 | if (DualTimer_isEnabled(timer) == 1) { |
sahilmgandhi | 18:6a4db94011d3 | 374 | if((DualTimers[timer].dualtimer1)->TimerRIS) |
sahilmgandhi | 18:6a4db94011d3 | 375 | return SINGLETIMER1; |
sahilmgandhi | 18:6a4db94011d3 | 376 | else |
sahilmgandhi | 18:6a4db94011d3 | 377 | return SINGLETIMER2; |
sahilmgandhi | 18:6a4db94011d3 | 378 | } |
sahilmgandhi | 18:6a4db94011d3 | 379 | return 0; |
sahilmgandhi | 18:6a4db94011d3 | 380 | } |
sahilmgandhi | 18:6a4db94011d3 | 381 | |
sahilmgandhi | 18:6a4db94011d3 | 382 | /* |
sahilmgandhi | 18:6a4db94011d3 | 383 | * DualTimer_GetTicksUS(): returns the Ticks per us |
sahilmgandhi | 18:6a4db94011d3 | 384 | * timer: timer associated with the Ticks per us |
sahilmgandhi | 18:6a4db94011d3 | 385 | * @return: Ticks per us - 0 if the timer is disables |
sahilmgandhi | 18:6a4db94011d3 | 386 | */ |
sahilmgandhi | 18:6a4db94011d3 | 387 | uint32_t DualTimer_GetTicksUS(uint32_t timer) |
sahilmgandhi | 18:6a4db94011d3 | 388 | { |
sahilmgandhi | 18:6a4db94011d3 | 389 | /* Verify if the Timer is enabled */ |
sahilmgandhi | 18:6a4db94011d3 | 390 | if (DualTimer_isEnabled(timer) == 1) { |
sahilmgandhi | 18:6a4db94011d3 | 391 | return DUALTIMER_TICKS_US; |
sahilmgandhi | 18:6a4db94011d3 | 392 | } |
sahilmgandhi | 18:6a4db94011d3 | 393 | return 0; |
sahilmgandhi | 18:6a4db94011d3 | 394 | } |
sahilmgandhi | 18:6a4db94011d3 | 395 | |
sahilmgandhi | 18:6a4db94011d3 | 396 | /* |
sahilmgandhi | 18:6a4db94011d3 | 397 | * DualTimer_GetReloadValue(): returns the load value of the selected |
sahilmgandhi | 18:6a4db94011d3 | 398 | * singletimer. |
sahilmgandhi | 18:6a4db94011d3 | 399 | * timer: timer associated with the Ticks per us |
sahilmgandhi | 18:6a4db94011d3 | 400 | * singletimer: selected singletimer |
sahilmgandhi | 18:6a4db94011d3 | 401 | * @return: reload value of the selected singletimer - 0 if timer is disabled |
sahilmgandhi | 18:6a4db94011d3 | 402 | */ |
sahilmgandhi | 18:6a4db94011d3 | 403 | uint32_t DualTimer_GetReloadValue(uint32_t timer, uint32_t singletimer) |
sahilmgandhi | 18:6a4db94011d3 | 404 | { |
sahilmgandhi | 18:6a4db94011d3 | 405 | /* Verify if the Timer is enabled */ |
sahilmgandhi | 18:6a4db94011d3 | 406 | if (DualTimer_isEnabled(timer) == 1) { |
sahilmgandhi | 18:6a4db94011d3 | 407 | if (singletimer == SINGLETIMER1) |
sahilmgandhi | 18:6a4db94011d3 | 408 | return DualTimers[timer].dualtimer1Reload / DUALTIMER_TICKS_US; |
sahilmgandhi | 18:6a4db94011d3 | 409 | else |
sahilmgandhi | 18:6a4db94011d3 | 410 | return DualTimers[timer].dualtimer2Reload / DUALTIMER_TICKS_US; |
sahilmgandhi | 18:6a4db94011d3 | 411 | } |
sahilmgandhi | 18:6a4db94011d3 | 412 | return 0; |
sahilmgandhi | 18:6a4db94011d3 | 413 | } |