Exemplo n.º 1
0
/**
 * \brief Change the setting of PWM comparison.
 *
 * \param p_pwm Pointer to a PWM instance.
 * \param p_cmp Configurations of PWM comparison tagged by unit.
 *
 * \retval 0 if changing succeeds, otherwise fails.
 */
uint32_t pwm_cmp_change_setting(Pwm *p_pwm, pwm_cmp_t *p_cmp)
{
	uint32_t unit = p_cmp->unit;

	p_pwm->PWM_CMP[unit].PWM_CMPVUPD = PWM_CMPV_CV(p_cmp->ul_value) |
			(p_cmp->b_is_decrementing << 24);

	p_pwm->PWM_CMP[unit].PWM_CMPMUPD = PWM_CMPM_CTR(p_cmp->ul_trigger) |
			PWM_CMPM_CPR(p_cmp->ul_period) |
			PWM_CMPM_CUPR(p_cmp->ul_update_period);

	/** Boolean of generating a match pulse */
	if (p_cmp->b_pulse_on_line_0) {
		p_pwm->PWM_ELMR[0] |= (1 << unit);
	} else {
		p_pwm->PWM_ELMR[0] &= ~((uint32_t) (1 << unit));
	}
	/** Boolean of generating a match pulse */
	if (p_cmp->b_pulse_on_line_1) {
		p_pwm->PWM_ELMR[1] |= (1 << unit);
	} else {
		p_pwm->PWM_ELMR[1] &= ~((uint32_t) (1 << unit));
	}

	/** Boolean of comparison enable */
	if (p_cmp->b_enable) {
		p_pwm->PWM_CMP[unit].PWM_CMPMUPD |= PWM_CMPM_CEN;
	} else {
		p_pwm->PWM_CMP[unit].PWM_CMPMUPD &= ~PWM_CMPM_CEN;
	}

	return 0;
}
Exemplo n.º 2
0
/*------------------------------------------------------------------------------
Name:           configurePWMC
parameters:     -
description:    initializes the PWM controller
------------------------------------------------------------------------------*/
void BldcControl::configurePWMC(void)
{
    uint32_t clka = 0; /* clock A not used */
    uint32_t clkb = 0; /* clock B not used */
    uint32_t mck = MCK_CLOCK_42MHZ;
    uint16_t duty = 0;
    uint16_t deadTime;
    
    pwmPeriod = MCK_CLOCK_42MHZ / this->periphery->Pwm.pwmSwFrq;
    
    deadTime = (uint16_t)(pwmPeriod * (DEAD_TIME * this->periphery->Pwm.pwmSwFrq * 2));

    /* disable all 3 channels */
    PWMC_DisableChannel(PWM, this->periphery->Pwm.pwmChU);
    PWMC_DisableChannel(PWM, this->periphery->Pwm.pwmChV);
    PWMC_DisableChannel(PWM, this->periphery->Pwm.pwmChW);

    PWMC_ConfigureClocks(clka, clkb, mck);

    /* initialize all 3 channels */
    PWMC_ConfigureChannelExt(PWM,
                          this->periphery->Pwm.pwmChU, /* channel ID */
                          PWM_CMR_CPRE_MCK, /* use main clock */
                          PWM_CMR_CALG, /* center alligned */
                          0, /* polarity low level */
                          0, /* event counter = 0 */
                          PWM_CMR_DTE, /* enable dead time */
                          0,  /* no inversion of dead time H */
                          0); /* no inversion of dead time L */
    PWMC_ConfigureChannelExt(PWM,
                          this->periphery->Pwm.pwmChV, /* channel ID */
                          PWM_CMR_CPRE_MCK,
                          PWM_CMR_CALG, /* center alligned */
                          0, /* polarity low level */
                          0, /* event counter = 0 */
                          PWM_CMR_DTE, /* enable dead time */
                          0,  /* no inversion of dead time H */
                          0); /* no inversion of dead time L */
    PWMC_ConfigureChannelExt(PWM,
                          this->periphery->Pwm.pwmChW, /* channel ID */
                          PWM_CMR_CPRE_MCK,
                          PWM_CMR_CALG, /* center alligned */
                          0, /* polarity low level */
                          0, /* event counter = 0 */
                          PWM_CMR_DTE, /* enable dead time */
                          0,  /* no inversion of dead time H */
                          0); /* no inversion of dead time L */
                             
    PWMC_ConfigureSyncChannel(PWM,
                              PWM_SCM_SYNC0|PWM_SCM_SYNC1|PWM_SCM_SYNC2,
                              PWM_SCM_UPDM_MODE0,
                              PWM_SCM_PTRM,
                              PWM_SCM_PTRCS(0)) ;

    /* set periods */
    PWMC_SetPeriod(PWM, this->periphery->Pwm.pwmChU, pwmPeriod);
    PWMC_SetPeriod(PWM, this->periphery->Pwm.pwmChV, pwmPeriod);
    PWMC_SetPeriod(PWM, this->periphery->Pwm.pwmChW, pwmPeriod);

    /* set duty cycles */
    PWMC_SetDutyCycle(PWM, this->periphery->Pwm.pwmChU, duty);
    PWMC_SetDutyCycle(PWM, this->periphery->Pwm.pwmChV, duty);
    PWMC_SetDutyCycle(PWM, this->periphery->Pwm.pwmChW, duty);

    /* set dead times */
    PWMC_SetDeadTime(PWM, this->periphery->Pwm.pwmChU, deadTime, deadTime);
    PWMC_SetDeadTime(PWM, this->periphery->Pwm.pwmChV, deadTime, deadTime);
    PWMC_SetDeadTime(PWM, this->periphery->Pwm.pwmChW, deadTime, deadTime);

    /* set overwrites to 0 */
    PWMC_SetOverrideValue(PWM, 0);

    /* set event for ADC trigger */
    PWM->PWM_CMP[this->periphery->Pwm.pwmChU].PWM_CMPM |= PWM_CMPM_CEN | /* enable */
                                           PWM_CMPM_CTR(0) | /* each period */
                                           PWM_CMPM_CPR(0);
    PWM->PWM_CMP[this->periphery->Pwm.pwmChU].PWM_CMPV = pwmPeriod/2; /* set trigger */
    PWM->PWM_ELMR[0] |= PWM_ELMR_CSEL0; /* enable event line 0 */

}