Gordon Craig / mbed-dev

Fork of mbed-dev by mbed official

Committer:
Kojto
Date:
Thu Aug 03 13:13:39 2017 +0100
Revision:
170:19eb464bc2be
This updates the lib to the mbed lib v 148

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kojto 170:19eb464bc2be 1 /*
Kojto 170:19eb464bc2be 2 * Copyright (c) 2016, Freescale Semiconductor, Inc.
Kojto 170:19eb464bc2be 3 * Copyright 2016-2017 NXP
Kojto 170:19eb464bc2be 4 *
Kojto 170:19eb464bc2be 5 * Redistribution and use in source and binary forms, with or without modification,
Kojto 170:19eb464bc2be 6 * are permitted provided that the following conditions are met:
Kojto 170:19eb464bc2be 7 *
Kojto 170:19eb464bc2be 8 * o Redistributions of source code must retain the above copyright notice, this list
Kojto 170:19eb464bc2be 9 * of conditions and the following disclaimer.
Kojto 170:19eb464bc2be 10 *
Kojto 170:19eb464bc2be 11 * o Redistributions in binary form must reproduce the above copyright notice, this
Kojto 170:19eb464bc2be 12 * list of conditions and the following disclaimer in the documentation and/or
Kojto 170:19eb464bc2be 13 * other materials provided with the distribution.
Kojto 170:19eb464bc2be 14 *
Kojto 170:19eb464bc2be 15 * o Neither the name of the copyright holder nor the names of its
Kojto 170:19eb464bc2be 16 * contributors may be used to endorse or promote products derived from this
Kojto 170:19eb464bc2be 17 * software without specific prior written permission.
Kojto 170:19eb464bc2be 18 *
Kojto 170:19eb464bc2be 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
Kojto 170:19eb464bc2be 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
Kojto 170:19eb464bc2be 21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Kojto 170:19eb464bc2be 22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
Kojto 170:19eb464bc2be 23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
Kojto 170:19eb464bc2be 24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
Kojto 170:19eb464bc2be 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
Kojto 170:19eb464bc2be 26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Kojto 170:19eb464bc2be 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Kojto 170:19eb464bc2be 28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Kojto 170:19eb464bc2be 29 */
Kojto 170:19eb464bc2be 30
Kojto 170:19eb464bc2be 31 #include "fsl_sctimer.h"
Kojto 170:19eb464bc2be 32
Kojto 170:19eb464bc2be 33 /*******************************************************************************
Kojto 170:19eb464bc2be 34 * Definitions
Kojto 170:19eb464bc2be 35 ******************************************************************************/
Kojto 170:19eb464bc2be 36 /*! @brief Typedef for interrupt handler. */
Kojto 170:19eb464bc2be 37 typedef void (*sctimer_isr_t)(SCT_Type *base);
Kojto 170:19eb464bc2be 38
Kojto 170:19eb464bc2be 39 /*******************************************************************************
Kojto 170:19eb464bc2be 40 * Prototypes
Kojto 170:19eb464bc2be 41 ******************************************************************************/
Kojto 170:19eb464bc2be 42 /*!
Kojto 170:19eb464bc2be 43 * @brief Gets the instance from the base address
Kojto 170:19eb464bc2be 44 *
Kojto 170:19eb464bc2be 45 * @param base SCTimer peripheral base address
Kojto 170:19eb464bc2be 46 *
Kojto 170:19eb464bc2be 47 * @return The SCTimer instance
Kojto 170:19eb464bc2be 48 */
Kojto 170:19eb464bc2be 49 static uint32_t SCTIMER_GetInstance(SCT_Type *base);
Kojto 170:19eb464bc2be 50
Kojto 170:19eb464bc2be 51 /*******************************************************************************
Kojto 170:19eb464bc2be 52 * Variables
Kojto 170:19eb464bc2be 53 ******************************************************************************/
Kojto 170:19eb464bc2be 54 /*! @brief Pointers to SCT bases for each instance. */
Kojto 170:19eb464bc2be 55 static SCT_Type *const s_sctBases[] = SCT_BASE_PTRS;
Kojto 170:19eb464bc2be 56
Kojto 170:19eb464bc2be 57 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
Kojto 170:19eb464bc2be 58 /*! @brief Pointers to SCT clocks for each instance. */
Kojto 170:19eb464bc2be 59 static const clock_ip_name_t s_sctClocks[] = SCT_CLOCKS;
Kojto 170:19eb464bc2be 60 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
Kojto 170:19eb464bc2be 61
Kojto 170:19eb464bc2be 62 /*! @brief Pointers to SCT resets for each instance. */
Kojto 170:19eb464bc2be 63 static const reset_ip_name_t s_sctResets[] = SCT_RSTS;
Kojto 170:19eb464bc2be 64
Kojto 170:19eb464bc2be 65 /*!< @brief SCTimer event Callback function. */
Kojto 170:19eb464bc2be 66 static sctimer_event_callback_t s_eventCallback[FSL_FEATURE_SCT_NUMBER_OF_EVENTS];
Kojto 170:19eb464bc2be 67
Kojto 170:19eb464bc2be 68 /*!< @brief Keep track of SCTimer event number */
Kojto 170:19eb464bc2be 69 static uint32_t s_currentEvent;
Kojto 170:19eb464bc2be 70
Kojto 170:19eb464bc2be 71 /*!< @brief Keep track of SCTimer state number */
Kojto 170:19eb464bc2be 72 static uint32_t s_currentState;
Kojto 170:19eb464bc2be 73
Kojto 170:19eb464bc2be 74 /*!< @brief Keep track of SCTimer match/capture register number */
Kojto 170:19eb464bc2be 75 static uint32_t s_currentMatch;
Kojto 170:19eb464bc2be 76
Kojto 170:19eb464bc2be 77 /*! @brief Pointer to SCTimer IRQ handler */
Kojto 170:19eb464bc2be 78 static sctimer_isr_t s_sctimerIsr;
Kojto 170:19eb464bc2be 79
Kojto 170:19eb464bc2be 80 /*******************************************************************************
Kojto 170:19eb464bc2be 81 * Code
Kojto 170:19eb464bc2be 82 ******************************************************************************/
Kojto 170:19eb464bc2be 83 static uint32_t SCTIMER_GetInstance(SCT_Type *base)
Kojto 170:19eb464bc2be 84 {
Kojto 170:19eb464bc2be 85 uint32_t instance;
Kojto 170:19eb464bc2be 86 uint32_t sctArrayCount = (sizeof(s_sctBases) / sizeof(s_sctBases[0]));
Kojto 170:19eb464bc2be 87
Kojto 170:19eb464bc2be 88 /* Find the instance index from base address mappings. */
Kojto 170:19eb464bc2be 89 for (instance = 0; instance < sctArrayCount; instance++)
Kojto 170:19eb464bc2be 90 {
Kojto 170:19eb464bc2be 91 if (s_sctBases[instance] == base)
Kojto 170:19eb464bc2be 92 {
Kojto 170:19eb464bc2be 93 break;
Kojto 170:19eb464bc2be 94 }
Kojto 170:19eb464bc2be 95 }
Kojto 170:19eb464bc2be 96
Kojto 170:19eb464bc2be 97 assert(instance < sctArrayCount);
Kojto 170:19eb464bc2be 98
Kojto 170:19eb464bc2be 99 return instance;
Kojto 170:19eb464bc2be 100 }
Kojto 170:19eb464bc2be 101
Kojto 170:19eb464bc2be 102 status_t SCTIMER_Init(SCT_Type *base, const sctimer_config_t *config)
Kojto 170:19eb464bc2be 103 {
Kojto 170:19eb464bc2be 104 assert(config);
Kojto 170:19eb464bc2be 105 uint32_t i;
Kojto 170:19eb464bc2be 106
Kojto 170:19eb464bc2be 107 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
Kojto 170:19eb464bc2be 108 /* Enable the SCTimer clock*/
Kojto 170:19eb464bc2be 109 CLOCK_EnableClock(s_sctClocks[SCTIMER_GetInstance(base)]);
Kojto 170:19eb464bc2be 110 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
Kojto 170:19eb464bc2be 111
Kojto 170:19eb464bc2be 112 /* Reset the module */
Kojto 170:19eb464bc2be 113 RESET_PeripheralReset(s_sctResets[SCTIMER_GetInstance(base)]);
Kojto 170:19eb464bc2be 114
Kojto 170:19eb464bc2be 115 /* Setup the counter operation */
Kojto 170:19eb464bc2be 116 base->CONFIG = SCT_CONFIG_CKSEL(config->clockSelect) | SCT_CONFIG_CLKMODE(config->clockMode) |
Kojto 170:19eb464bc2be 117 SCT_CONFIG_UNIFY(config->enableCounterUnify);
Kojto 170:19eb464bc2be 118
Kojto 170:19eb464bc2be 119 /* Write to the control register, clear the counter and keep the counters halted */
Kojto 170:19eb464bc2be 120 base->CTRL = SCT_CTRL_BIDIR_L(config->enableBidirection_l) | SCT_CTRL_PRE_L(config->prescale_l) |
Kojto 170:19eb464bc2be 121 SCT_CTRL_CLRCTR_L_MASK | SCT_CTRL_HALT_L_MASK;
Kojto 170:19eb464bc2be 122
Kojto 170:19eb464bc2be 123 if (!(config->enableCounterUnify))
Kojto 170:19eb464bc2be 124 {
Kojto 170:19eb464bc2be 125 base->CTRL |= SCT_CTRL_BIDIR_H(config->enableBidirection_h) | SCT_CTRL_PRE_H(config->prescale_h) |
Kojto 170:19eb464bc2be 126 SCT_CTRL_CLRCTR_H_MASK | SCT_CTRL_HALT_H_MASK;
Kojto 170:19eb464bc2be 127 }
Kojto 170:19eb464bc2be 128
Kojto 170:19eb464bc2be 129 /* Initial state of channel output */
Kojto 170:19eb464bc2be 130 base->OUTPUT = config->outInitState;
Kojto 170:19eb464bc2be 131
Kojto 170:19eb464bc2be 132 /* Clear the global variables */
Kojto 170:19eb464bc2be 133 s_currentEvent = 0;
Kojto 170:19eb464bc2be 134 s_currentState = 0;
Kojto 170:19eb464bc2be 135 s_currentMatch = 0;
Kojto 170:19eb464bc2be 136
Kojto 170:19eb464bc2be 137 /* Clear the callback array */
Kojto 170:19eb464bc2be 138 for (i = 0; i < FSL_FEATURE_SCT_NUMBER_OF_EVENTS; i++)
Kojto 170:19eb464bc2be 139 {
Kojto 170:19eb464bc2be 140 s_eventCallback[i] = NULL;
Kojto 170:19eb464bc2be 141 }
Kojto 170:19eb464bc2be 142
Kojto 170:19eb464bc2be 143 /* Save interrupt handler */
Kojto 170:19eb464bc2be 144 s_sctimerIsr = SCTIMER_EventHandleIRQ;
Kojto 170:19eb464bc2be 145
Kojto 170:19eb464bc2be 146 return kStatus_Success;
Kojto 170:19eb464bc2be 147 }
Kojto 170:19eb464bc2be 148
Kojto 170:19eb464bc2be 149 void SCTIMER_Deinit(SCT_Type *base)
Kojto 170:19eb464bc2be 150 {
Kojto 170:19eb464bc2be 151 /* Halt the counters */
Kojto 170:19eb464bc2be 152 base->CTRL |= (SCT_CTRL_HALT_L_MASK | SCT_CTRL_HALT_H_MASK);
Kojto 170:19eb464bc2be 153
Kojto 170:19eb464bc2be 154 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
Kojto 170:19eb464bc2be 155 /* Disable the SCTimer clock*/
Kojto 170:19eb464bc2be 156 CLOCK_DisableClock(s_sctClocks[SCTIMER_GetInstance(base)]);
Kojto 170:19eb464bc2be 157 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
Kojto 170:19eb464bc2be 158 }
Kojto 170:19eb464bc2be 159
Kojto 170:19eb464bc2be 160 void SCTIMER_GetDefaultConfig(sctimer_config_t *config)
Kojto 170:19eb464bc2be 161 {
Kojto 170:19eb464bc2be 162 assert(config);
Kojto 170:19eb464bc2be 163
Kojto 170:19eb464bc2be 164 /* SCT operates as a unified 32-bit counter */
Kojto 170:19eb464bc2be 165 config->enableCounterUnify = true;
Kojto 170:19eb464bc2be 166 /* System clock clocks the entire SCT module */
Kojto 170:19eb464bc2be 167 config->clockMode = kSCTIMER_System_ClockMode;
Kojto 170:19eb464bc2be 168 /* This is used only by certain clock modes */
Kojto 170:19eb464bc2be 169 config->clockSelect = kSCTIMER_Clock_On_Rise_Input_0;
Kojto 170:19eb464bc2be 170 /* Up count mode only for the unified counter */
Kojto 170:19eb464bc2be 171 config->enableBidirection_l = false;
Kojto 170:19eb464bc2be 172 /* Up count mode only for Counte_H */
Kojto 170:19eb464bc2be 173 config->enableBidirection_h = false;
Kojto 170:19eb464bc2be 174 /* Prescale factor of 1 */
Kojto 170:19eb464bc2be 175 config->prescale_l = 0;
Kojto 170:19eb464bc2be 176 /* Prescale factor of 1 for Counter_H*/
Kojto 170:19eb464bc2be 177 config->prescale_h = 0;
Kojto 170:19eb464bc2be 178 /* Clear outputs */
Kojto 170:19eb464bc2be 179 config->outInitState = 0;
Kojto 170:19eb464bc2be 180 }
Kojto 170:19eb464bc2be 181
Kojto 170:19eb464bc2be 182 status_t SCTIMER_SetupPwm(SCT_Type *base,
Kojto 170:19eb464bc2be 183 const sctimer_pwm_signal_param_t *pwmParams,
Kojto 170:19eb464bc2be 184 sctimer_pwm_mode_t mode,
Kojto 170:19eb464bc2be 185 uint32_t pwmFreq_Hz,
Kojto 170:19eb464bc2be 186 uint32_t srcClock_Hz,
Kojto 170:19eb464bc2be 187 uint32_t *event)
Kojto 170:19eb464bc2be 188 {
Kojto 170:19eb464bc2be 189 assert(pwmParams);
Kojto 170:19eb464bc2be 190 assert(srcClock_Hz);
Kojto 170:19eb464bc2be 191 assert(pwmFreq_Hz);
Kojto 170:19eb464bc2be 192
Kojto 170:19eb464bc2be 193 uint32_t period, pulsePeriod = 0;
Kojto 170:19eb464bc2be 194 uint32_t sctClock = srcClock_Hz / (((base->CTRL & SCT_CTRL_PRE_L_MASK) >> SCT_CTRL_PRE_L_SHIFT) + 1);
Kojto 170:19eb464bc2be 195 uint32_t periodEvent, pulseEvent;
Kojto 170:19eb464bc2be 196 uint32_t reg;
Kojto 170:19eb464bc2be 197
Kojto 170:19eb464bc2be 198 /* This function will create 2 events, return an error if we do not have enough events available */
Kojto 170:19eb464bc2be 199 if ((s_currentEvent + 2) > FSL_FEATURE_SCT_NUMBER_OF_EVENTS)
Kojto 170:19eb464bc2be 200 {
Kojto 170:19eb464bc2be 201 return kStatus_Fail;
Kojto 170:19eb464bc2be 202 }
Kojto 170:19eb464bc2be 203
Kojto 170:19eb464bc2be 204 if (pwmParams->dutyCyclePercent == 0)
Kojto 170:19eb464bc2be 205 {
Kojto 170:19eb464bc2be 206 return kStatus_Fail;
Kojto 170:19eb464bc2be 207 }
Kojto 170:19eb464bc2be 208
Kojto 170:19eb464bc2be 209 /* Set unify bit to operate in 32-bit counter mode */
Kojto 170:19eb464bc2be 210 base->CONFIG |= SCT_CONFIG_UNIFY_MASK;
Kojto 170:19eb464bc2be 211
Kojto 170:19eb464bc2be 212 /* Use bi-directional mode for center-aligned PWM */
Kojto 170:19eb464bc2be 213 if (mode == kSCTIMER_CenterAlignedPwm)
Kojto 170:19eb464bc2be 214 {
Kojto 170:19eb464bc2be 215 base->CTRL |= SCT_CTRL_BIDIR_L_MASK;
Kojto 170:19eb464bc2be 216 }
Kojto 170:19eb464bc2be 217
Kojto 170:19eb464bc2be 218 /* Calculate PWM period match value */
Kojto 170:19eb464bc2be 219 if (mode == kSCTIMER_EdgeAlignedPwm)
Kojto 170:19eb464bc2be 220 {
Kojto 170:19eb464bc2be 221 period = (sctClock / pwmFreq_Hz) - 1;
Kojto 170:19eb464bc2be 222 }
Kojto 170:19eb464bc2be 223 else
Kojto 170:19eb464bc2be 224 {
Kojto 170:19eb464bc2be 225 period = sctClock / (pwmFreq_Hz * 2);
Kojto 170:19eb464bc2be 226 }
Kojto 170:19eb464bc2be 227
Kojto 170:19eb464bc2be 228 /* Calculate pulse width match value */
Kojto 170:19eb464bc2be 229 pulsePeriod = (period * pwmParams->dutyCyclePercent) / 100;
Kojto 170:19eb464bc2be 230
Kojto 170:19eb464bc2be 231 /* For 100% dutycyle, make pulse period greater than period so the event will never occur */
Kojto 170:19eb464bc2be 232 if (pwmParams->dutyCyclePercent >= 100)
Kojto 170:19eb464bc2be 233 {
Kojto 170:19eb464bc2be 234 pulsePeriod = period + 2;
Kojto 170:19eb464bc2be 235 }
Kojto 170:19eb464bc2be 236
Kojto 170:19eb464bc2be 237 /* Schedule an event when we reach the PWM period */
Kojto 170:19eb464bc2be 238 SCTIMER_CreateAndScheduleEvent(base, kSCTIMER_MatchEventOnly, period, 0, kSCTIMER_Counter_L, &periodEvent);
Kojto 170:19eb464bc2be 239
Kojto 170:19eb464bc2be 240 /* Schedule an event when we reach the pulse width */
Kojto 170:19eb464bc2be 241 SCTIMER_CreateAndScheduleEvent(base, kSCTIMER_MatchEventOnly, pulsePeriod, 0, kSCTIMER_Counter_L, &pulseEvent);
Kojto 170:19eb464bc2be 242
Kojto 170:19eb464bc2be 243 /* Reset the counter when we reach the PWM period */
Kojto 170:19eb464bc2be 244 SCTIMER_SetupCounterLimitAction(base, kSCTIMER_Counter_L, periodEvent);
Kojto 170:19eb464bc2be 245
Kojto 170:19eb464bc2be 246 /* Return the period event to the user */
Kojto 170:19eb464bc2be 247 *event = periodEvent;
Kojto 170:19eb464bc2be 248
Kojto 170:19eb464bc2be 249 /* For high-true level */
Kojto 170:19eb464bc2be 250 if (pwmParams->level == kSCTIMER_HighTrue)
Kojto 170:19eb464bc2be 251 {
Kojto 170:19eb464bc2be 252 /* Set the initial output level to low which is the inactive state */
Kojto 170:19eb464bc2be 253 base->OUTPUT &= ~(1U << pwmParams->output);
Kojto 170:19eb464bc2be 254
Kojto 170:19eb464bc2be 255 if (mode == kSCTIMER_EdgeAlignedPwm)
Kojto 170:19eb464bc2be 256 {
Kojto 170:19eb464bc2be 257 /* Set the output when we reach the PWM period */
Kojto 170:19eb464bc2be 258 SCTIMER_SetupOutputSetAction(base, pwmParams->output, periodEvent);
Kojto 170:19eb464bc2be 259 /* Clear the output when we reach the PWM pulse value */
Kojto 170:19eb464bc2be 260 SCTIMER_SetupOutputClearAction(base, pwmParams->output, pulseEvent);
Kojto 170:19eb464bc2be 261 }
Kojto 170:19eb464bc2be 262 else
Kojto 170:19eb464bc2be 263 {
Kojto 170:19eb464bc2be 264 /* Clear the output when we reach the PWM pulse event */
Kojto 170:19eb464bc2be 265 SCTIMER_SetupOutputClearAction(base, pwmParams->output, pulseEvent);
Kojto 170:19eb464bc2be 266 /* Reverse output when down counting */
Kojto 170:19eb464bc2be 267 reg = base->OUTPUTDIRCTRL;
Kojto 170:19eb464bc2be 268 reg &= ~(SCT_OUTPUTDIRCTRL_SETCLR0_MASK << (2 * pwmParams->output));
Kojto 170:19eb464bc2be 269 reg |= (1U << (2 * pwmParams->output));
Kojto 170:19eb464bc2be 270 base->OUTPUTDIRCTRL = reg;
Kojto 170:19eb464bc2be 271 }
Kojto 170:19eb464bc2be 272 }
Kojto 170:19eb464bc2be 273 /* For low-true level */
Kojto 170:19eb464bc2be 274 else
Kojto 170:19eb464bc2be 275 {
Kojto 170:19eb464bc2be 276 /* Set the initial output level to high which is the inactive state */
Kojto 170:19eb464bc2be 277 base->OUTPUT |= (1U << pwmParams->output);
Kojto 170:19eb464bc2be 278
Kojto 170:19eb464bc2be 279 if (mode == kSCTIMER_EdgeAlignedPwm)
Kojto 170:19eb464bc2be 280 {
Kojto 170:19eb464bc2be 281 /* Clear the output when we reach the PWM period */
Kojto 170:19eb464bc2be 282 SCTIMER_SetupOutputClearAction(base, pwmParams->output, periodEvent);
Kojto 170:19eb464bc2be 283 /* Set the output when we reach the PWM pulse value */
Kojto 170:19eb464bc2be 284 SCTIMER_SetupOutputSetAction(base, pwmParams->output, pulseEvent);
Kojto 170:19eb464bc2be 285 }
Kojto 170:19eb464bc2be 286 else
Kojto 170:19eb464bc2be 287 {
Kojto 170:19eb464bc2be 288 /* Set the output when we reach the PWM pulse event */
Kojto 170:19eb464bc2be 289 SCTIMER_SetupOutputSetAction(base, pwmParams->output, pulseEvent);
Kojto 170:19eb464bc2be 290 /* Reverse output when down counting */
Kojto 170:19eb464bc2be 291 reg = base->OUTPUTDIRCTRL;
Kojto 170:19eb464bc2be 292 reg &= ~(SCT_OUTPUTDIRCTRL_SETCLR0_MASK << (2 * pwmParams->output));
Kojto 170:19eb464bc2be 293 reg |= (1U << (2 * pwmParams->output));
Kojto 170:19eb464bc2be 294 base->OUTPUTDIRCTRL = reg;
Kojto 170:19eb464bc2be 295 }
Kojto 170:19eb464bc2be 296 }
Kojto 170:19eb464bc2be 297
Kojto 170:19eb464bc2be 298 return kStatus_Success;
Kojto 170:19eb464bc2be 299 }
Kojto 170:19eb464bc2be 300
Kojto 170:19eb464bc2be 301 void SCTIMER_UpdatePwmDutycycle(SCT_Type *base, sctimer_out_t output, uint8_t dutyCyclePercent, uint32_t event)
Kojto 170:19eb464bc2be 302
Kojto 170:19eb464bc2be 303 {
Kojto 170:19eb464bc2be 304 assert(dutyCyclePercent > 0);
Kojto 170:19eb464bc2be 305
Kojto 170:19eb464bc2be 306 uint32_t periodMatchReg, pulseMatchReg;
Kojto 170:19eb464bc2be 307 uint32_t pulsePeriod = 0, period;
Kojto 170:19eb464bc2be 308
Kojto 170:19eb464bc2be 309 /* Retrieve the match register number for the PWM period */
Kojto 170:19eb464bc2be 310 periodMatchReg = base->EVENT[event].CTRL & SCT_EVENT_CTRL_MATCHSEL_MASK;
Kojto 170:19eb464bc2be 311
Kojto 170:19eb464bc2be 312 /* Retrieve the match register number for the PWM pulse period */
Kojto 170:19eb464bc2be 313 pulseMatchReg = base->EVENT[event + 1].CTRL & SCT_EVENT_CTRL_MATCHSEL_MASK;
Kojto 170:19eb464bc2be 314
Kojto 170:19eb464bc2be 315 period = base->SCTMATCH[periodMatchReg];
Kojto 170:19eb464bc2be 316
Kojto 170:19eb464bc2be 317 /* Calculate pulse width match value */
Kojto 170:19eb464bc2be 318 pulsePeriod = (period * dutyCyclePercent) / 100;
Kojto 170:19eb464bc2be 319
Kojto 170:19eb464bc2be 320 /* For 100% dutycyle, make pulse period greater than period so the event will never occur */
Kojto 170:19eb464bc2be 321 if (dutyCyclePercent >= 100)
Kojto 170:19eb464bc2be 322 {
Kojto 170:19eb464bc2be 323 pulsePeriod = period + 2;
Kojto 170:19eb464bc2be 324 }
Kojto 170:19eb464bc2be 325
Kojto 170:19eb464bc2be 326 /* Stop the counter before updating match register */
Kojto 170:19eb464bc2be 327 SCTIMER_StopTimer(base, kSCTIMER_Counter_L);
Kojto 170:19eb464bc2be 328
Kojto 170:19eb464bc2be 329 /* Update dutycycle */
Kojto 170:19eb464bc2be 330 base->SCTMATCH[pulseMatchReg] = SCT_SCTMATCH_MATCHn_L(pulsePeriod);
Kojto 170:19eb464bc2be 331 base->SCTMATCHREL[pulseMatchReg] = SCT_SCTMATCHREL_RELOADn_L(pulsePeriod);
Kojto 170:19eb464bc2be 332
Kojto 170:19eb464bc2be 333 /* Restart the counter */
Kojto 170:19eb464bc2be 334 SCTIMER_StartTimer(base, kSCTIMER_Counter_L);
Kojto 170:19eb464bc2be 335 }
Kojto 170:19eb464bc2be 336
Kojto 170:19eb464bc2be 337 status_t SCTIMER_CreateAndScheduleEvent(SCT_Type *base,
Kojto 170:19eb464bc2be 338 sctimer_event_t howToMonitor,
Kojto 170:19eb464bc2be 339 uint32_t matchValue,
Kojto 170:19eb464bc2be 340 uint32_t whichIO,
Kojto 170:19eb464bc2be 341 sctimer_counter_t whichCounter,
Kojto 170:19eb464bc2be 342 uint32_t *event)
Kojto 170:19eb464bc2be 343 {
Kojto 170:19eb464bc2be 344 uint32_t combMode = (((uint32_t)howToMonitor & SCT_EVENT_CTRL_COMBMODE_MASK) >> SCT_EVENT_CTRL_COMBMODE_SHIFT);
Kojto 170:19eb464bc2be 345 uint32_t currentCtrlVal = howToMonitor;
Kojto 170:19eb464bc2be 346
Kojto 170:19eb464bc2be 347 /* Return an error if we have hit the limit in terms of number of events created */
Kojto 170:19eb464bc2be 348 if (s_currentEvent >= FSL_FEATURE_SCT_NUMBER_OF_EVENTS)
Kojto 170:19eb464bc2be 349 {
Kojto 170:19eb464bc2be 350 return kStatus_Fail;
Kojto 170:19eb464bc2be 351 }
Kojto 170:19eb464bc2be 352
Kojto 170:19eb464bc2be 353 /* IO only mode */
Kojto 170:19eb464bc2be 354 if (combMode == 0x2U)
Kojto 170:19eb464bc2be 355 {
Kojto 170:19eb464bc2be 356 base->EVENT[s_currentEvent].CTRL = currentCtrlVal | SCT_EVENT_CTRL_IOSEL(whichIO);
Kojto 170:19eb464bc2be 357 }
Kojto 170:19eb464bc2be 358 /* Match mode only */
Kojto 170:19eb464bc2be 359 else if (combMode == 0x1U)
Kojto 170:19eb464bc2be 360 {
Kojto 170:19eb464bc2be 361 /* Return an error if we have hit the limit in terms of number of number of match registers */
Kojto 170:19eb464bc2be 362 if (s_currentMatch >= FSL_FEATURE_SCT_NUMBER_OF_MATCH_CAPTURE)
Kojto 170:19eb464bc2be 363 {
Kojto 170:19eb464bc2be 364 return kStatus_Fail;
Kojto 170:19eb464bc2be 365 }
Kojto 170:19eb464bc2be 366
Kojto 170:19eb464bc2be 367 currentCtrlVal |= SCT_EVENT_CTRL_MATCHSEL(s_currentMatch);
Kojto 170:19eb464bc2be 368 /* Use Counter_L bits if counter is operating in 32-bit mode or user wants to setup the L counter */
Kojto 170:19eb464bc2be 369 if ((base->CONFIG & SCT_CONFIG_UNIFY_MASK) || (whichCounter == kSCTIMER_Counter_L))
Kojto 170:19eb464bc2be 370 {
Kojto 170:19eb464bc2be 371 base->SCTMATCH[s_currentMatch] = SCT_SCTMATCH_MATCHn_L(matchValue);
Kojto 170:19eb464bc2be 372 base->SCTMATCHREL[s_currentMatch] = SCT_SCTMATCHREL_RELOADn_L(matchValue);
Kojto 170:19eb464bc2be 373 }
Kojto 170:19eb464bc2be 374 else
Kojto 170:19eb464bc2be 375 {
Kojto 170:19eb464bc2be 376 /* Select the counter, no need for this if operating in 32-bit mode */
Kojto 170:19eb464bc2be 377 currentCtrlVal |= SCT_EVENT_CTRL_HEVENT(whichCounter);
Kojto 170:19eb464bc2be 378 base->SCTMATCH[s_currentMatch] = SCT_SCTMATCH_MATCHn_H(matchValue);
Kojto 170:19eb464bc2be 379 base->SCTMATCHREL[s_currentMatch] = SCT_SCTMATCHREL_RELOADn_H(matchValue);
Kojto 170:19eb464bc2be 380 }
Kojto 170:19eb464bc2be 381 base->EVENT[s_currentEvent].CTRL = currentCtrlVal;
Kojto 170:19eb464bc2be 382 /* Increment the match register number */
Kojto 170:19eb464bc2be 383 s_currentMatch++;
Kojto 170:19eb464bc2be 384 }
Kojto 170:19eb464bc2be 385 /* Use both Match & IO */
Kojto 170:19eb464bc2be 386 else
Kojto 170:19eb464bc2be 387 {
Kojto 170:19eb464bc2be 388 /* Return an error if we have hit the limit in terms of number of number of match registers */
Kojto 170:19eb464bc2be 389 if (s_currentMatch >= FSL_FEATURE_SCT_NUMBER_OF_MATCH_CAPTURE)
Kojto 170:19eb464bc2be 390 {
Kojto 170:19eb464bc2be 391 return kStatus_Fail;
Kojto 170:19eb464bc2be 392 }
Kojto 170:19eb464bc2be 393
Kojto 170:19eb464bc2be 394 currentCtrlVal |= SCT_EVENT_CTRL_MATCHSEL(s_currentMatch) | SCT_EVENT_CTRL_IOSEL(whichIO);
Kojto 170:19eb464bc2be 395 /* Use Counter_L bits if counter is operating in 32-bit mode or user wants to setup the L counter */
Kojto 170:19eb464bc2be 396 if ((base->CONFIG & SCT_CONFIG_UNIFY_MASK) || (whichCounter == kSCTIMER_Counter_L))
Kojto 170:19eb464bc2be 397 {
Kojto 170:19eb464bc2be 398 base->SCTMATCH[s_currentMatch] = SCT_SCTMATCH_MATCHn_L(matchValue);
Kojto 170:19eb464bc2be 399 base->SCTMATCHREL[s_currentMatch] = SCT_SCTMATCHREL_RELOADn_L(matchValue);
Kojto 170:19eb464bc2be 400 }
Kojto 170:19eb464bc2be 401 else
Kojto 170:19eb464bc2be 402 {
Kojto 170:19eb464bc2be 403 /* Select the counter, no need for this if operating in 32-bit mode */
Kojto 170:19eb464bc2be 404 currentCtrlVal |= SCT_EVENT_CTRL_HEVENT(whichCounter);
Kojto 170:19eb464bc2be 405 base->SCTMATCH[s_currentMatch] = SCT_SCTMATCH_MATCHn_H(matchValue);
Kojto 170:19eb464bc2be 406 base->SCTMATCHREL[s_currentMatch] = SCT_SCTMATCHREL_RELOADn_H(matchValue);
Kojto 170:19eb464bc2be 407 }
Kojto 170:19eb464bc2be 408 base->EVENT[s_currentEvent].CTRL = currentCtrlVal;
Kojto 170:19eb464bc2be 409 /* Increment the match register number */
Kojto 170:19eb464bc2be 410 s_currentMatch++;
Kojto 170:19eb464bc2be 411 }
Kojto 170:19eb464bc2be 412
Kojto 170:19eb464bc2be 413 /* Enable the event in the current state */
Kojto 170:19eb464bc2be 414 base->EVENT[s_currentEvent].STATE = (1U << s_currentState);
Kojto 170:19eb464bc2be 415
Kojto 170:19eb464bc2be 416 /* Return the event number */
Kojto 170:19eb464bc2be 417 *event = s_currentEvent;
Kojto 170:19eb464bc2be 418
Kojto 170:19eb464bc2be 419 /* Increment the event number */
Kojto 170:19eb464bc2be 420 s_currentEvent++;
Kojto 170:19eb464bc2be 421
Kojto 170:19eb464bc2be 422 return kStatus_Success;
Kojto 170:19eb464bc2be 423 }
Kojto 170:19eb464bc2be 424
Kojto 170:19eb464bc2be 425 void SCTIMER_ScheduleEvent(SCT_Type *base, uint32_t event)
Kojto 170:19eb464bc2be 426 {
Kojto 170:19eb464bc2be 427 /* Enable event in the current state */
Kojto 170:19eb464bc2be 428 base->EVENT[event].STATE |= (1U << s_currentState);
Kojto 170:19eb464bc2be 429 }
Kojto 170:19eb464bc2be 430
Kojto 170:19eb464bc2be 431 status_t SCTIMER_IncreaseState(SCT_Type *base)
Kojto 170:19eb464bc2be 432 {
Kojto 170:19eb464bc2be 433 /* Return an error if we have hit the limit in terms of states used */
Kojto 170:19eb464bc2be 434 if (s_currentState >= FSL_FEATURE_SCT_NUMBER_OF_STATES)
Kojto 170:19eb464bc2be 435 {
Kojto 170:19eb464bc2be 436 return kStatus_Fail;
Kojto 170:19eb464bc2be 437 }
Kojto 170:19eb464bc2be 438
Kojto 170:19eb464bc2be 439 s_currentState++;
Kojto 170:19eb464bc2be 440
Kojto 170:19eb464bc2be 441 return kStatus_Success;
Kojto 170:19eb464bc2be 442 }
Kojto 170:19eb464bc2be 443
Kojto 170:19eb464bc2be 444 uint32_t SCTIMER_GetCurrentState(SCT_Type *base)
Kojto 170:19eb464bc2be 445 {
Kojto 170:19eb464bc2be 446 return s_currentState;
Kojto 170:19eb464bc2be 447 }
Kojto 170:19eb464bc2be 448
Kojto 170:19eb464bc2be 449 void SCTIMER_SetupOutputToggleAction(SCT_Type *base, uint32_t whichIO, uint32_t event)
Kojto 170:19eb464bc2be 450 {
Kojto 170:19eb464bc2be 451 uint32_t reg;
Kojto 170:19eb464bc2be 452
Kojto 170:19eb464bc2be 453 /* Set the same event to set and clear the output */
Kojto 170:19eb464bc2be 454 base->OUT[whichIO].CLR |= (1U << event);
Kojto 170:19eb464bc2be 455 base->OUT[whichIO].SET |= (1U << event);
Kojto 170:19eb464bc2be 456
Kojto 170:19eb464bc2be 457 /* Set the conflict resolution to toggle output */
Kojto 170:19eb464bc2be 458 reg = base->RES;
Kojto 170:19eb464bc2be 459 reg &= ~(SCT_RES_O0RES_MASK << (2 * whichIO));
Kojto 170:19eb464bc2be 460 reg |= (uint32_t)(kSCTIMER_ResolveToggle << (2 * whichIO));
Kojto 170:19eb464bc2be 461 base->RES = reg;
Kojto 170:19eb464bc2be 462 }
Kojto 170:19eb464bc2be 463
Kojto 170:19eb464bc2be 464 status_t SCTIMER_SetupCaptureAction(SCT_Type *base,
Kojto 170:19eb464bc2be 465 sctimer_counter_t whichCounter,
Kojto 170:19eb464bc2be 466 uint32_t *captureRegister,
Kojto 170:19eb464bc2be 467 uint32_t event)
Kojto 170:19eb464bc2be 468 {
Kojto 170:19eb464bc2be 469 /* Return an error if we have hit the limit in terms of number of capture/match registers used */
Kojto 170:19eb464bc2be 470 if (s_currentMatch >= FSL_FEATURE_SCT_NUMBER_OF_MATCH_CAPTURE)
Kojto 170:19eb464bc2be 471 {
Kojto 170:19eb464bc2be 472 return kStatus_Fail;
Kojto 170:19eb464bc2be 473 }
Kojto 170:19eb464bc2be 474
Kojto 170:19eb464bc2be 475 /* Use Counter_L bits if counter is operating in 32-bit mode or user wants to setup the L counter */
Kojto 170:19eb464bc2be 476 if ((base->CONFIG & SCT_CONFIG_UNIFY_MASK) || (whichCounter == kSCTIMER_Counter_L))
Kojto 170:19eb464bc2be 477 {
Kojto 170:19eb464bc2be 478 /* Set the bit to enable event */
Kojto 170:19eb464bc2be 479 base->SCTCAPCTRL[s_currentMatch] |= SCT_SCTCAPCTRL_CAPCONn_L(1 << event);
Kojto 170:19eb464bc2be 480
Kojto 170:19eb464bc2be 481 /* Set this resource to be a capture rather than match */
Kojto 170:19eb464bc2be 482 base->REGMODE |= SCT_REGMODE_REGMOD_L(1 << s_currentMatch);
Kojto 170:19eb464bc2be 483 }
Kojto 170:19eb464bc2be 484 else
Kojto 170:19eb464bc2be 485 {
Kojto 170:19eb464bc2be 486 /* Set bit to enable event */
Kojto 170:19eb464bc2be 487 base->SCTCAPCTRL[s_currentMatch] |= SCT_SCTCAPCTRL_CAPCONn_H(1 << event);
Kojto 170:19eb464bc2be 488
Kojto 170:19eb464bc2be 489 /* Set this resource to be a capture rather than match */
Kojto 170:19eb464bc2be 490 base->REGMODE |= SCT_REGMODE_REGMOD_H(1 << s_currentMatch);
Kojto 170:19eb464bc2be 491 }
Kojto 170:19eb464bc2be 492
Kojto 170:19eb464bc2be 493 /* Return the match register number */
Kojto 170:19eb464bc2be 494 *captureRegister = s_currentMatch;
Kojto 170:19eb464bc2be 495
Kojto 170:19eb464bc2be 496 /* Increase the match register number */
Kojto 170:19eb464bc2be 497 s_currentMatch++;
Kojto 170:19eb464bc2be 498
Kojto 170:19eb464bc2be 499 return kStatus_Success;
Kojto 170:19eb464bc2be 500 }
Kojto 170:19eb464bc2be 501
Kojto 170:19eb464bc2be 502 void SCTIMER_SetCallback(SCT_Type *base, sctimer_event_callback_t callback, uint32_t event)
Kojto 170:19eb464bc2be 503 {
Kojto 170:19eb464bc2be 504 s_eventCallback[event] = callback;
Kojto 170:19eb464bc2be 505 }
Kojto 170:19eb464bc2be 506
Kojto 170:19eb464bc2be 507 void SCTIMER_EventHandleIRQ(SCT_Type *base)
Kojto 170:19eb464bc2be 508 {
Kojto 170:19eb464bc2be 509 uint32_t eventFlag = SCT0->EVFLAG;
Kojto 170:19eb464bc2be 510 /* Only clear the flags whose interrupt field is enabled */
Kojto 170:19eb464bc2be 511 uint32_t clearFlag = (eventFlag & SCT0->EVEN);
Kojto 170:19eb464bc2be 512 uint32_t mask = eventFlag;
Kojto 170:19eb464bc2be 513 int i = 0;
Kojto 170:19eb464bc2be 514
Kojto 170:19eb464bc2be 515 /* Invoke the callback for certain events */
Kojto 170:19eb464bc2be 516 for (i = 0; (i < FSL_FEATURE_SCT_NUMBER_OF_EVENTS) && (mask != 0); i++)
Kojto 170:19eb464bc2be 517 {
Kojto 170:19eb464bc2be 518 if (mask & 0x1)
Kojto 170:19eb464bc2be 519 {
Kojto 170:19eb464bc2be 520 if (s_eventCallback[i] != NULL)
Kojto 170:19eb464bc2be 521 {
Kojto 170:19eb464bc2be 522 s_eventCallback[i]();
Kojto 170:19eb464bc2be 523 }
Kojto 170:19eb464bc2be 524 }
Kojto 170:19eb464bc2be 525 mask >>= 1;
Kojto 170:19eb464bc2be 526 }
Kojto 170:19eb464bc2be 527
Kojto 170:19eb464bc2be 528 /* Clear event interrupt flag */
Kojto 170:19eb464bc2be 529 SCT0->EVFLAG = clearFlag;
Kojto 170:19eb464bc2be 530 }
Kojto 170:19eb464bc2be 531
Kojto 170:19eb464bc2be 532 void SCT0_IRQHandler(void)
Kojto 170:19eb464bc2be 533 {
Kojto 170:19eb464bc2be 534 s_sctimerIsr(SCT0);
Kojto 170:19eb464bc2be 535 }