drivers/pwm/pwm_stm32.c
/*
 * Set the period and pulse width for a PWM pin.
 *
 * Parameters
 * dev: Pointer to PWM device structure
 * pwm: PWM channel to set
 * period_cycles: Period (in timer count)
 * pulse_cycles: Pulse width (in timer count).
 *
 * return 0, or negative errno code
 */
Set the period and pulse width for a PWM pin.
Parameters
dev: Pointer to PWM device structure
pwm: PWM channel to set
period_cycles: Period (in timer count)
pulse_cycles: Pulse width (in timer count).

return 0, or negative errno code
Set the period and pulse width for a PWM pin . Parameters dev : Pointer to PWM device structure pwm : PWM channel to set period_cycles : Period ( in timer count ) pulse_cycles : Pulse width ( in timer count ) . return 0 or negative errno code
static int pwm_stm32_pin_set(struct device *dev, u32_t pwm,
			     u32_t period_cycles, u32_t pulse_cycles)
{
	struct pwm_stm32_data *data = DEV_DATA(dev);
	TIM_HandleTypeDef *TimerHandle = &data->hpwm;
	TIM_OC_InitTypeDef sConfig;
	u32_t channel;
	bool counter_32b;
	if (period_cycles == 0 || pulse_cycles > period_cycles) {
		return -EINVAL;
	}
	channel = (pwm - 1)*CHANNEL_LENGTH;
	if (!IS_TIM_INSTANCE(PWM_STRUCT(dev)) ||
		!IS_TIM_CHANNELS(channel)) {
		return -ENOTSUP;
	}
#ifdef CONFIG_SOC_SERIES_STM32F1X
	counter_32b = 0;
#else
	counter_32b = IS_TIM_32B_COUNTER_INSTANCE(PWM_STRUCT(dev));
#endif
	if (!counter_32b && (period_cycles > 0xFFFF)) {
		return -ENOTSUP;
	}
	TimerHandle->Instance = PWM_STRUCT(dev);
	TimerHandle->Init.Prescaler = data->pwm_prescaler;
	TimerHandle->Init.ClockDivision = 0;
	TimerHandle->Init.CounterMode = TIM_COUNTERMODE_UP;
	TimerHandle->Init.RepetitionCounter = 0;
	TimerHandle->Init.Period = period_cycles;
	HAL_TIM_PWM_Init(TimerHandle);
	sConfig.OCMode       = TIM_OCMODE_PWM1;
	sConfig.OCPolarity   = TIM_OCPOLARITY_HIGH;
	sConfig.OCFastMode   = TIM_OCFAST_DISABLE;
	sConfig.OCNPolarity  = TIM_OCNPOLARITY_HIGH;
	sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;
	sConfig.OCIdleState  = TIM_OCIDLESTATE_RESET;
	sConfig.Pulse = pulse_cycles;
	HAL_TIM_PWM_ConfigChannel(TimerHandle, &sConfig, channel);
	return HAL_TIM_PWM_Start(TimerHandle, channel);
}
static int pwm_stm32_pin_set ( struct device * dev , u32_t pwm , u32_t period_cycles , u32_t pulse_cycles ) { struct pwm_stm32_data * data = DEV_DATA ( dev ) ; TIM_HandleTypeDef * TimerHandle = & data -> hpwm ; TIM_OC_InitTypeDef sConfig ; u32_t channel ; bool counter_32b ; if ( period_cycles == 0 || pulse_cycles > period_cycles ) { return - EINVAL ; } channel = ( pwm - 1 ) * CHANNEL_LENGTH ; if ( ! IS_TIM_INSTANCE ( PWM_STRUCT ( dev ) ) || ! IS_TIM_CHANNELS ( channel ) ) { return - ENOTSUP ; } #ifdef CONFIG_SOC_SERIES_STM32F1X counter_32b = 0 ; #else counter_32b = IS_TIM_32B_COUNTER_INSTANCE ( PWM_STRUCT ( dev ) ) ; #endif if ( ! counter_32b && ( period_cycles > 0xFFFF ) ) { return - ENOTSUP ; } TimerHandle -> Instance = PWM_STRUCT ( dev ) ; TimerHandle -> Init . Prescaler = data -> pwm_prescaler ; TimerHandle -> Init . ClockDivision = 0 ; TimerHandle -> Init . CounterMode = TIM_COUNTERMODE_UP ; TimerHandle -> Init . RepetitionCounter = 0 ; TimerHandle -> Init . Period = period_cycles ; HAL_TIM_PWM_Init ( TimerHandle ) ; sConfig . OCMode = TIM_OCMODE_PWM1 ; sConfig . OCPolarity = TIM_OCPOLARITY_HIGH ; sConfig . OCFastMode = TIM_OCFAST_DISABLE ; sConfig . OCNPolarity = TIM_OCNPOLARITY_HIGH ; sConfig . OCNIdleState = TIM_OCNIDLESTATE_RESET ; sConfig . OCIdleState = TIM_OCIDLESTATE_RESET ; sConfig . Pulse = pulse_cycles ; HAL_TIM_PWM_ConfigChannel ( TimerHandle , & sConfig , channel ) ; return HAL_TIM_PWM_Start ( TimerHandle , channel ) ; }
Set the period and pulse width for a PWM pin.
Set the period and pulse width for a PWM pin .
/* configure channel */ /* FIXME: IS_TIM_32B_COUNTER_INSTANCE not available on
	 * SMT32F1 Cube HAL since all timer counters are 16 bits
	 */ /* 16 bits counter does not support requested period
		 * You might want to adapt PWM output clock to adjust
		 * cycle durations to fit requested period into 16 bits
		 * counter
		 */ /* Configure Timer IP */ /* Set period value */ /* Configure PWM channel */ /* Set the pulse value */