示例#1
0
文件: pwm.c 项目: thehobn/ec
/**
 * Preset PWM operation clock.
 *
 * @param   none
 * @return  none
 * @notes   changed when initial or HOOK_FREQ_CHANGE command
 */
void pwm_freq_changed(void)
{
	uint32_t prescaler_divider = 0;

	if (pwm_init_ch == PWM_CH_FAN) {
		/*
		 * Using PWM Frequency and Resolution we calculate
		 * prescaler for input clock
		 */
		/* (Benson_TBD_9) pwm_clock/freq/resolution not confirm */
#ifdef CONFIG_PWM_INPUT_LFCLK
		prescaler_divider = (uint32_t)(32768 /
				pwm_channels[pwm_init_ch].freq);
#else
		prescaler_divider = (uint32_t)(clock_get_apb2_freq()
				/ pwm_channels[pwm_init_ch].freq);
#endif
	} else {
		prescaler_divider = (uint32_t)(clock_get_apb2_freq()
				/ pwm_channels[pwm_init_ch].freq);
	}
	/* Set clock prescalre divider to ADC module*/
	if (prescaler_divider >= 1)
		prescaler_divider = prescaler_divider - 1;
	if (prescaler_divider > 0xFFFF)
		prescaler_divider = 0xFFFF;

	/* Configure computed prescaler and resolution */
	NPCX_PRSC(pwm_channels[pwm_init_ch].channel) =
			(uint16_t)prescaler_divider;
}
示例#2
0
/**
 * Set PWM operation clock.
 *
 * @param   ch      operation channel
 * @param   freq    desired PWM frequency
 * @param   res     resolution for duty cycle
 * @notes   changed when initialization
 */
void pwm_set_freq(enum pwm_channel ch, uint32_t freq, uint32_t res)
{
	int mdl = pwm_channels[ch].channel;
	uint32_t prescaler_divider = 0;
	uint32_t clock;

	/* Disable PWM for module configuration */
	pwm_enable(ch, 0);

	/* Get PWM clock frequency */
	if (pwm_channels[ch].flags & PWM_CONFIG_DSLEEP_CLK)
		clock = INT_32K_CLOCK;
	else
		clock = clock_get_apb2_freq();

	/*
	 * Using PWM Frequency and Resolution we calculate
	 * prescaler for input clock
	 */
	prescaler_divider = ((clock / freq)/res);

	/* Set clock prescaler divider to PWM module*/
	if (prescaler_divider >= 1)
		prescaler_divider = prescaler_divider - 1;
	if (prescaler_divider > 0xFFFF)
		prescaler_divider = 0xFFFF;

	/* Configure computed prescaler and resolution */
	NPCX_PRSC(mdl) = (uint16_t)prescaler_divider - 1;

	/* Set PWM cycle time */
	NPCX_CTR(mdl) = res - 1;

	/* Set the duty cycle to 0% since DCR > CTR */
	NPCX_DCR(mdl) = res;
}