/***************************************************************************//** * @brief * Initialize TIMER. * * @details * Notice that counter top must be configured separately with for instance * TIMER_TopSet(). In addition, compare/capture and dead-time insertion * init must be initialized separately if used. That should probably * be done prior to the use of this function if configuring the TIMER to * start when initialization is completed. * * @param[in] timer * Pointer to TIMER peripheral register block. * * @param[in] init * Pointer to TIMER initialization structure. ******************************************************************************/ void TIMER_Init(TIMER_TypeDef *timer, const TIMER_Init_TypeDef *init) { EFM_ASSERT(TIMER_REF_VALID(timer)); /* Stop timer if specified to be disabled (dosn't hurt if already stopped) */ if (!(init->enable)) { timer->CMD = TIMER_CMD_STOP; } /* Reset counter */ timer->CNT = _TIMER_CNT_RESETVALUE; timer->CTRL = ((uint32_t)(init->prescale) << _TIMER_CTRL_PRESC_SHIFT) | ((uint32_t)(init->clkSel) << _TIMER_CTRL_CLKSEL_SHIFT) | ((uint32_t)(init->fallAction) << _TIMER_CTRL_FALLA_SHIFT) | ((uint32_t)(init->riseAction) << _TIMER_CTRL_RISEA_SHIFT) | ((uint32_t)(init->mode) << _TIMER_CTRL_MODE_SHIFT) | (init->debugRun ? TIMER_CTRL_DEBUGRUN : 0) | (init->dmaClrAct ? TIMER_CTRL_DMACLRACT : 0) | (init->quadModeX4 ? TIMER_CTRL_QDM_X4 : 0) | (init->oneShot ? TIMER_CTRL_OSMEN : 0) #if defined(TIMER_CTRL_X2CNT) && defined(TIMER_CTRL_ATI) | (init->count2x ? TIMER_CTRL_X2CNT : 0) | (init->ati ? TIMER_CTRL_ATI : 0) #endif | (init->sync ? TIMER_CTRL_SYNC : 0); /* Start timer if specified to be enabled (dosn't hurt if already started) */ if (init->enable) { timer->CMD = TIMER_CMD_START; } }
/***************************************************************************//** * @brief * Initialize TIMER. * * @details * Notice that counter top must be configured separately with for instance * TIMER_TopSet(). In addition, compare/capture and dead-time insertion * init must be initialized separately if used. That should probably * be done prior to the use of this function if configuring the TIMER to * start when initialization is completed. * * @param[in] timer * Pointer to TIMER peripheral register block. * * @param[in] init * Pointer to TIMER initialization structure. ******************************************************************************/ void TIMER_Init(TIMER_TypeDef *timer, const TIMER_Init_TypeDef *init) { uint32_t tmp; EFM_ASSERT(TIMER_REF_VALID(timer)); /* Stop timer if specified to be disabled (dosn't hurt if already stopped) */ if (!(init->enable)) { timer->CMD = TIMER_CMD_STOP; } tmp = ((uint32_t)(init->prescale) << _TIMER_CTRL_PRESC_SHIFT) | ((uint32_t)(init->clkSel) << _TIMER_CTRL_CLKSEL_SHIFT) | ((uint32_t)(init->fallAction) << _TIMER_CTRL_FALLA_SHIFT) | ((uint32_t)(init->riseAction) << _TIMER_CTRL_RISEA_SHIFT) | ((uint32_t)(init->mode) << _TIMER_CTRL_MODE_SHIFT); /* Configure DEBUGRUN flag, sets whether or not counter should be * updated when debugger is active */ if (init->debugRun) { tmp |= TIMER_CTRL_DEBUGRUN; } if (init->dmaClrAct) { tmp |= TIMER_CTRL_DMACLRACT; } if (init->quadModeX4) { tmp |= TIMER_CTRL_QDM_X4; } if (init->oneShot) { tmp |= TIMER_CTRL_OSMEN; } if (init->sync) { tmp |= TIMER_CTRL_SYNC; } timer->CTRL = tmp; /* Start timer if specified to be enabled (dosn't hurt if already started) */ if (init->enable) { timer->CMD = TIMER_CMD_START; } }
/***************************************************************************//** * @brief * Start/stop TIMER. * * @param[in] timer * Pointer to TIMER peripheral register block. * * @param[in] enable * true to enable counting, false to disable. ******************************************************************************/ void TIMER_Enable(TIMER_TypeDef *timer, bool enable) { EFM_ASSERT(TIMER_REF_VALID(timer)); if (enable) { timer->CMD = TIMER_CMD_START; } else { timer->CMD = TIMER_CMD_STOP; } }
/***************************************************************************//** * @brief * Initialize TIMER compare/capture channel. * * @details * Notice that if operating channel in compare mode, the CCV and CCVB register * must be set separately as required. * * @param[in] timer * Pointer to TIMER peripheral register block. * * @param[in] ch * Compare/capture channel to init for. * * @param[in] init * Pointer to TIMER initialization structure. ******************************************************************************/ void TIMER_InitCC(TIMER_TypeDef *timer, unsigned int ch, const TIMER_InitCC_TypeDef *init) { EFM_ASSERT(TIMER_REF_VALID(timer)); EFM_ASSERT(TIMER_CH_VALID(ch)); timer->CC[ch].CTRL = ((uint32_t)(init->eventCtrl) << _TIMER_CC_CTRL_ICEVCTRL_SHIFT) | ((uint32_t)(init->edge) << _TIMER_CC_CTRL_ICEDGE_SHIFT) | ((uint32_t)(init->prsSel) << _TIMER_CC_CTRL_PRSSEL_SHIFT) | ((uint32_t)(init->cufoa) << _TIMER_CC_CTRL_CUFOA_SHIFT) | ((uint32_t)(init->cofoa) << _TIMER_CC_CTRL_COFOA_SHIFT) | ((uint32_t)(init->cmoa) << _TIMER_CC_CTRL_CMOA_SHIFT) | ((uint32_t)(init->mode) << _TIMER_CC_CTRL_MODE_SHIFT) | (init->filter ? TIMER_CC_CTRL_FILT_ENABLE : 0) | (init->prsInput ? TIMER_CC_CTRL_INSEL_PRS : 0) | (init->coist ? TIMER_CC_CTRL_COIST : 0) | (init->outInvert ? TIMER_CC_CTRL_OUTINV : 0); }
/***************************************************************************//** * @brief * Reset TIMER to same state as after a HW reset. * * @note * The ROUTE register is NOT reset by this function, in order to allow for * centralized setup of this feature. * * @param[in] timer * Pointer to TIMER peripheral register block. ******************************************************************************/ void TIMER_Reset(TIMER_TypeDef *timer) { int i; EFM_ASSERT(TIMER_REF_VALID(timer)); /* Make sure disabled first, before resetting other registers */ timer->CMD = TIMER_CMD_STOP; timer->CTRL = _TIMER_CTRL_RESETVALUE; timer->IEN = _TIMER_IEN_RESETVALUE; timer->IFC = _TIMER_IFC_MASK; timer->TOPB = _TIMER_TOPB_RESETVALUE; /* Write TOP after TOPB to invalidate TOPB (clear TIMER_STATUS_TOPBV) */ timer->TOP = _TIMER_TOP_RESETVALUE; timer->CNT = _TIMER_CNT_RESETVALUE; /* Do not reset route register, setting should be done independently */ /* (Note: ROUTE register may be locked by DTLOCK register.) */ for (i = 0; TIMER_CH_VALID(i); i++) { timer->CC[i].CTRL = _TIMER_CC_CTRL_RESETVALUE; timer->CC[i].CCV = _TIMER_CC_CCV_RESETVALUE; timer->CC[i].CCVB = _TIMER_CC_CCVB_RESETVALUE; } /* Reset dead time insertion module, no effect on timers without DTI */ #if defined(TIMER_DTLOCK_LOCKKEY_UNLOCK) /* Unlock DTI registers first in case locked */ timer->DTLOCK = TIMER_DTLOCK_LOCKKEY_UNLOCK; timer->DTCTRL = _TIMER_DTCTRL_RESETVALUE; timer->DTTIME = _TIMER_DTTIME_RESETVALUE; timer->DTFC = _TIMER_DTFC_RESETVALUE; timer->DTOGEN = _TIMER_DTOGEN_RESETVALUE; timer->DTFAULTC = _TIMER_DTFAULTC_MASK; #endif }
/***************************************************************************//** * @brief * Unlock the TIMER so that writing to locked registers again is possible. * * @param[in] timer * Pointer to TIMER peripheral register block. ******************************************************************************/ void TIMER_Unlock(TIMER_TypeDef *timer) { EFM_ASSERT(TIMER_REF_VALID(timer)); timer->DTLOCK = TIMER_DTLOCK_LOCKKEY_UNLOCK; }