/*********************************************************************//** * @brief Initializes the pPwm peripheral corresponding to the specified * parameters in the PWM_ConfigStruct. * @param[in] pwmId The Id of the expected PWM component * * * @param[in] PWMTimerCounterMode Timer or Counter mode, should be: * - PWM_MODE_TIMER: Counter of PWM peripheral is in Timer mode * - PWM_MODE_COUNTER: Counter of PWM peripheral is in Counter mode * @param[in] PWM_ConfigStruct Pointer to structure (PWM_TIMERCFG_Type or * PWM_COUNTERCFG_Type) which will be initialized. * @return None * Note: PWM_ConfigStruct pointer will be assigned to corresponding structure * (PWM_TIMERCFG_Type or PWM_COUNTERCFG_Type) due to PWMTimerCounterMode. **********************************************************************/ void PWM_Init(uint8_t pwmId, uint32_t PWMTimerCounterMode, void *PWM_ConfigStruct) { LPC_PWM_TypeDef* pPwm = PWM_GetPointer(pwmId); PWM_TIMERCFG_Type *pTimeCfg; PWM_COUNTERCFG_Type *pCounterCfg; uint64_t clkdlycnt; pTimeCfg = (PWM_TIMERCFG_Type *)PWM_ConfigStruct; pCounterCfg = (PWM_COUNTERCFG_Type *)PWM_ConfigStruct; if(pwmId == PWM_0) { CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCPWM0, ENABLE); } else if(pwmId == PWM_1) { CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCPWM1, ENABLE); } else { //Trap the error while(1); } // Get peripheral clock of PWM1 clkdlycnt = CLKPWR_GetCLK(CLKPWR_CLKTYPE_PER); // Clear all interrupts pending pPwm->IR = 0xFF & PWM_IR_BITMASK; pPwm->TCR = 0x00; pPwm->CTCR = 0x00; pPwm->MCR = 0x00; pPwm->CCR = 0x00; pPwm->PCR = 0x00; pPwm->LER = 0x00; if (PWMTimerCounterMode == PWM_MODE_TIMER) { /* Absolute prescale value */ if (pTimeCfg->PrescaleOption == PWM_TIMER_PRESCALE_TICKVAL) { pPwm->PR = pTimeCfg->PrescaleValue - 1; } /* uSecond prescale value */ else { clkdlycnt = (clkdlycnt * pTimeCfg->PrescaleValue) / 1000000; pPwm->PR = ((uint32_t) clkdlycnt) - 1; } } else if (PWMTimerCounterMode == PWM_MODE_COUNTER) { pPwm->CTCR |= (PWM_CTCR_MODE((uint32_t)pCounterCfg->CounterOption)) \ | (PWM_CTCR_SELECT_INPUT((uint32_t)pCounterCfg->CountInputSelect)); } }
/*********************************************************************//** * @brief Initializes the PWMx peripheral corresponding to the specified * parameters in the PWM_ConfigStruct. * @param[in] PWMx PWM peripheral, should be LPC_PWM1 * @param[in] PWMTimerCounterMode Timer or Counter mode, should be: * - PWM_MODE_TIMER: Counter of PWM peripheral is in Timer mode * - PWM_MODE_COUNTER: Counter of PWM peripheral is in Counter mode * @param[in] PWM_ConfigStruct Pointer to structure (PWM_TIMERCFG_Type or * PWM_COUNTERCFG_Type) which will be initialized. * @return None * Note: PWM_ConfigStruct pointer will be assigned to corresponding structure * (PWM_TIMERCFG_Type or PWM_COUNTERCFG_Type) due to PWMTimerCounterMode. **********************************************************************/ void PWM_Init(LPC_PWM_TypeDef *PWMx, uint32_t PWMTimerCounterMode, void *PWM_ConfigStruct) { PWM_TIMERCFG_Type *pTimeCfg; PWM_COUNTERCFG_Type *pCounterCfg; uint64_t clkdlycnt; CHECK_PARAM(PARAM_PWMx(PWMx)); CHECK_PARAM(PARAM_PWM_TC_MODE(PWMTimerCounterMode)); pTimeCfg = (PWM_TIMERCFG_Type *)PWM_ConfigStruct; pCounterCfg = (PWM_COUNTERCFG_Type *)PWM_ConfigStruct; CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCPWM1, ENABLE); CLKPWR_SetPCLKDiv (CLKPWR_PCLKSEL_PWM1, CLKPWR_PCLKSEL_CCLK_DIV_4); // Get peripheral clock of PWM1 clkdlycnt = (uint64_t) CLKPWR_GetPCLK (CLKPWR_PCLKSEL_PWM1); // Clear all interrupts pending PWMx->IR = 0xFF & PWM_IR_BITMASK; PWMx->TCR = 0x00; PWMx->CTCR = 0x00; PWMx->MCR = 0x00; PWMx->CCR = 0x00; PWMx->PCR = 0x00; PWMx->LER = 0x00; if (PWMTimerCounterMode == PWM_MODE_TIMER) { CHECK_PARAM(PARAM_PWM_TIMER_PRESCALE(pTimeCfg->PrescaleOption)); /* Absolute prescale value */ if (pTimeCfg->PrescaleOption == PWM_TIMER_PRESCALE_TICKVAL) { PWMx->PR = pTimeCfg->PrescaleValue - 1; } /* uSecond prescale value */ else { clkdlycnt = (clkdlycnt * pTimeCfg->PrescaleValue) / 1000000; PWMx->PR = ((uint32_t) clkdlycnt) - 1; } } else if (PWMTimerCounterMode == PWM_MODE_COUNTER) { CHECK_PARAM(PARAM_PWM_COUNTER_INPUTSEL(pCounterCfg->CountInputSelect)); CHECK_PARAM(PARAM_PWM_COUNTER_EDGE(pCounterCfg->CounterOption)); PWMx->CTCR |= (PWM_CTCR_MODE((uint32_t)pCounterCfg->CounterOption)) \ | (PWM_CTCR_SELECT_INPUT((uint32_t)pCounterCfg->CountInputSelect)); } }