Skip to content

Commit 6b0cb87

Browse files
committed
Dynamic prescaler adjustment, adjust pulse resolution
1 parent 4de5a33 commit 6b0cb87

File tree

1 file changed

+37
-32
lines changed
  • ports/stm32f4/common-hal/pulseio

1 file changed

+37
-32
lines changed

ports/stm32f4/common-hal/pulseio/PWMOut.c

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
#include "stm32f4xx_hal.h"
3636
#include "common-hal/microcontroller/Pin.h"
3737

38-
#define PWM_MAX_FREQ 6000000
38+
#define PULSE_RESOLUTION 256 //8 bit
3939
#define ALL_CLOCKS 0xFFFF
4040

4141
STATIC uint8_t reserved_tim[TIM_BANK_ARRAY_LEN];
@@ -45,8 +45,6 @@ STATIC bool never_reset_tim[TIM_BANK_ARRAY_LEN];
4545
STATIC void tim_clock_enable(uint16_t mask);
4646
STATIC void tim_clock_disable(uint16_t mask);
4747

48-
RCC->CFGR & RCC_CFGR_PPRE1
49-
5048
// Get the frequency (in Hz) of the source clock for the given timer.
5149
// On STM32F405/407/415/417 there are 2 cases for how the clock freq is set.
5250
// If the APB prescaler is 1, then the timer clock is equal to its respective
@@ -107,10 +105,6 @@ pwmout_result_t common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self,
107105
uint16_t duty,
108106
uint32_t frequency,
109107
bool variable_frequency) {
110-
if (frequency == 0 || frequency > 6000000) {
111-
mp_raise_ValueError(translate("Invalid frequency supplied"));
112-
}
113-
114108
TIM_TypeDef * TIMx;
115109
uint8_t tim_num = sizeof(mcu_tim_pin_list)/sizeof(*mcu_tim_pin_list);
116110
bool tim_chan_taken = false;
@@ -189,43 +183,49 @@ pwmout_result_t common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self,
189183
}
190184

191185
uint32_t source_freq = timer_get_source_freq(self->tim->tim_index);
192-
uint32_t period = PWM_MAX_FREQ/frequency;
186+
if (frequency == 0 || frequency * PULSE_RESOLUTION > (source_freq)) {
187+
mp_raise_ValueError(translate("Invalid frequency supplied"));
188+
}
189+
uint32_t prescaler = source_freq/(frequency*PULSE_RESOLUTION);
190+
uint32_t period = PULSE_RESOLUTION;
191+
uint32_t input = (duty*PULSE_RESOLUTION)/65535;
193192
//Used for Debugging
194-
// mp_printf(&mp_plat_print, "SysCoreClock: %d\n", SystemCoreClock);
195-
// mp_printf(&mp_plat_print, "Source Freq: %d\n", source_freq);
196-
// mp_printf(&mp_plat_print, "Timer Freq: %d\n", source_freq/(source_freq / PWM_MAX_FREQ));
197-
// mp_printf(&mp_plat_print, "Actual Freq: %d\n", (source_freq/(source_freq / PWM_MAX_FREQ))/period);
198-
// mp_printf(&mp_plat_print, "Duty: %d\n", duty);
199-
// mp_printf(&mp_plat_print, "TIM#:%d CH:%d ALTF:%d\n", self->tim->tim_index, self->tim->channel_index, self->tim->altfn_index);
193+
mp_printf(&mp_plat_print, "Duty:%d, Pulses:%d\n", duty,input);
194+
mp_printf(&mp_plat_print, "SysCoreClock: %d\n", SystemCoreClock);
195+
mp_printf(&mp_plat_print, "Source Freq: %d\n", source_freq);
196+
mp_printf(&mp_plat_print, "Prescaler %d, Timer Freq: %d\n", prescaler, source_freq/prescaler);
197+
mp_printf(&mp_plat_print, "Output Freq: %d\n", (source_freq/prescaler)/period);
198+
mp_printf(&mp_plat_print, "Duty: %d\n", duty);
199+
mp_printf(&mp_plat_print, "TIM#:%d CH:%d ALTF:%d\n", self->tim->tim_index, self->tim->channel_index, self->tim->altfn_index);
200200

201201
//Timer init
202202
self->handle.Instance = TIMx;
203203
self->handle.Init.Period = period - 1;
204-
self->handle.Init.Prescaler = (source_freq / PWM_MAX_FREQ) - 1; // TIM runs at ~6MHz
204+
self->handle.Init.Prescaler = prescaler - 1;
205205
self->handle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
206206
self->handle.Init.CounterMode = TIM_COUNTERMODE_UP;
207207
self->handle.Init.RepetitionCounter = 0;
208208

209209
//only run init if this is the first instance of this timer
210210
if (first_time_setup) {
211211
if (HAL_TIM_PWM_Init(&self->handle) != HAL_OK) {
212-
mp_raise_ValueError(translate("Timer Init Error"));
212+
mp_raise_ValueError(translate("Could not initialize timer"));
213213
}
214214
}
215215

216216
//Channel/PWM init
217217
self->chan_handle.OCMode = TIM_OCMODE_PWM1;
218-
self->chan_handle.Pulse = (period*duty)/100 - 1;
218+
self->chan_handle.Pulse = input; //-1?
219219
self->chan_handle.OCPolarity = TIM_OCPOLARITY_LOW;
220220
self->chan_handle.OCFastMode = TIM_OCFAST_DISABLE;
221221
self->chan_handle.OCNPolarity = TIM_OCNPOLARITY_LOW; // needed for TIM1 and TIM8
222222
self->chan_handle.OCIdleState = TIM_OCIDLESTATE_SET; // needed for TIM1 and TIM8
223223
self->chan_handle.OCNIdleState = TIM_OCNIDLESTATE_SET; // needed for TIM1 and TIM8
224224
if (HAL_TIM_PWM_ConfigChannel(&self->handle, &self->chan_handle, self->channel) != HAL_OK) {
225-
mp_raise_ValueError(translate("Channel Init Error"));
225+
mp_raise_ValueError(translate("Could not initialize channel"));
226226
}
227227
if (HAL_TIM_PWM_Start(&self->handle, self->channel) != HAL_OK) {
228-
mp_raise_ValueError(translate("Error starting PWM"));
228+
mp_raise_ValueError(translate("Could not start PWM"));
229229
}
230230

231231
self->variable_frequency = variable_frequency;
@@ -261,46 +261,51 @@ void common_hal_pulseio_pwmout_deinit(pulseio_pwmout_obj_t* self) {
261261
}
262262

263263
void common_hal_pulseio_pwmout_set_duty_cycle(pulseio_pwmout_obj_t* self, uint16_t duty_cycle) {
264-
uint16_t duty = duty_cycle/655;
265-
uint32_t period = PWM_MAX_FREQ/self->frequency;
266-
uint32_t input = (period*duty)/100;
264+
uint32_t input = (duty_cycle*PULSE_RESOLUTION)/65535;
267265
//Used for debugging
268266
//mp_printf(&mp_plat_print, "duty_cycle %d, Duty: %d, Input %d\n", duty_cycle, duty, input);
269267
__HAL_TIM_SET_COMPARE(&self->handle, self->channel, input);
270268

271-
self->duty_cycle = duty;
269+
self->duty_cycle = duty_cycle;
272270
}
273271

274272
uint16_t common_hal_pulseio_pwmout_get_duty_cycle(pulseio_pwmout_obj_t* self) {
275273
return self->duty_cycle;
276274
}
277275

278276
void common_hal_pulseio_pwmout_set_frequency(pulseio_pwmout_obj_t* self, uint32_t frequency) {
279-
if (frequency == 0 || frequency > 6000000) {
280-
mp_raise_ValueError(translate("Invalid PWM frequency"));
281-
}
277+
//don't halt setup for the same frequency
282278
if (frequency == self->frequency) return;
283279

280+
//calculate new values
284281
uint32_t source_freq = timer_get_source_freq(self->tim->tim_index);
285-
uint32_t period = PWM_MAX_FREQ/frequency;
282+
if (frequency == 0 || frequency*PULSE_RESOLUTION > (source_freq)) {
283+
mp_raise_ValueError(translate("Invalid frequency supplied"));
284+
}
285+
uint32_t prescaler = source_freq/(frequency*PULSE_RESOLUTION);
286+
uint32_t period = PULSE_RESOLUTION;
287+
//this shouldn't ever exceed 0xffff*0xffff = 0xfffe0001, so it won't integer overflow.
288+
uint32_t input = (self->duty_cycle*PULSE_RESOLUTION)/65535;
286289

287290
//shut down
288291
HAL_TIM_PWM_Stop(&self->handle, self->channel);
289292

290293
//Only change altered values
291294
self->handle.Init.Period = period - 1;
292-
self->handle.Init.Prescaler = (source_freq / PWM_MAX_FREQ) - 1; // TIM runs at ~6MHz
295+
self->handle.Init.Prescaler = prescaler - 1;
293296

294297
//restart everything, adjusting for new speed
295298
if (HAL_TIM_PWM_Init(&self->handle) != HAL_OK) {
296-
mp_raise_ValueError(translate("Timer Re-Init Error"));
299+
mp_raise_ValueError(translate("Could not re-init timer"));
297300
}
298-
self->chan_handle.Pulse = (period*self->duty_cycle)/100 - 1;
301+
302+
self->chan_handle.Pulse = input;
303+
299304
if (HAL_TIM_PWM_ConfigChannel(&self->handle, &self->chan_handle, self->channel) != HAL_OK) {
300-
mp_raise_ValueError(translate("Channel Re-Init Error"));
305+
mp_raise_ValueError(translate("Could not re-init channel"));
301306
}
302307
if (HAL_TIM_PWM_Start(&self->handle, self->channel) != HAL_OK) {
303-
mp_raise_ValueError(translate("Error restarting PWM"));
308+
mp_raise_ValueError(translate("Could not restart PWM"));
304309
}
305310

306311
tim_frequencies[self->tim->tim_index-1] = frequency;

0 commit comments

Comments
 (0)