Skip to content

Commit 4de5a33

Browse files
committed
Style changes
1 parent 69bf926 commit 4de5a33

File tree

4 files changed

+55
-62
lines changed

4 files changed

+55
-62
lines changed

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

Lines changed: 51 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2018 Dan Halbert for Adafruit Industries
76
* Copyright (c) 2019 Lucian Copeland for Adafruit Industries
87
* Uses code from Micropython, Copyright (c) 2013-2016 Damien P. George
98
*
@@ -46,7 +45,13 @@ STATIC bool never_reset_tim[TIM_BANK_ARRAY_LEN];
4645
STATIC void tim_clock_enable(uint16_t mask);
4746
STATIC void tim_clock_disable(uint16_t mask);
4847

49-
// Get the frequency (in Hz)
48+
RCC->CFGR & RCC_CFGR_PPRE1
49+
50+
// Get the frequency (in Hz) of the source clock for the given timer.
51+
// On STM32F405/407/415/417 there are 2 cases for how the clock freq is set.
52+
// If the APB prescaler is 1, then the timer clock is equal to its respective
53+
// APB clock. Otherwise (APB prescaler > 1) the timer clock is twice its
54+
// respective APB clock. See DM00031020 Rev 4, page 115.
5055
static uint32_t timer_get_source_freq(uint32_t tim_id) {
5156
uint32_t source, clk_div;
5257
if (tim_id == 1 || (8 <= tim_id && tim_id <= 11)) {
@@ -68,7 +73,7 @@ static uint32_t timer_get_source_freq(uint32_t tim_id) {
6873
void pwmout_reset(void) {
6974
uint16_t never_reset_mask = 0x00;
7075
for(int i=0;i<TIM_BANK_ARRAY_LEN;i++) {
71-
if(!never_reset_tim[i]) {
76+
if (!never_reset_tim[i]) {
7277
reserved_tim[i] = 0x00;
7378
tim_frequencies[i] = 0x00;
7479
} else {
@@ -89,8 +94,6 @@ void common_hal_pulseio_pwmout_never_reset(pulseio_pwmout_obj_t *self) {
8994
}
9095

9196
void common_hal_pulseio_pwmout_reset_ok(pulseio_pwmout_obj_t *self) {
92-
//TODO: doesn't this need an equivalent release pin in microcontroller.c?
93-
//I don't see that implemented in any port.
9497
for(size_t i = 0 ; i < TIM_BANK_ARRAY_LEN; i++) {
9598
if (mcu_tim_banks[i] == self->handle.Instance) {
9699
never_reset_tim[i] = false;
@@ -106,36 +109,32 @@ pwmout_result_t common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self,
106109
bool variable_frequency) {
107110
if (frequency == 0 || frequency > 6000000) {
108111
mp_raise_ValueError(translate("Invalid frequency supplied"));
109-
//return PWMOUT_INVALID_FREQUENCY;
110112
}
111113

112114
TIM_TypeDef * TIMx;
113-
//TODO: style/safety question, use this instead?
114-
//uint8_t tim_num_pins = TIM_PIN_ARRAY_LEN;
115115
uint8_t tim_num = sizeof(mcu_tim_pin_list)/sizeof(*mcu_tim_pin_list);
116-
//TODO: use an enum to make this prettier?
117116
bool tim_chan_taken = false;
118117
bool tim_taken_f_mismatch = false;
119118
bool var_freq_mismatch = false;
120119
bool first_time_setup = true;
121120

122121
for(uint i = 0; i < tim_num; i++) {
123122
//if pin is same
124-
if(mcu_tim_pin_list[i].pin == pin) {
123+
if (mcu_tim_pin_list[i].pin == pin) {
125124
//check if the timer has a channel active
126125
if (reserved_tim[mcu_tim_pin_list[i].tim_index-1] != 0) {
127126
//is it the same channel? (or all channels reserved by a var-freq)
128-
if(reserved_tim[mcu_tim_pin_list[i].tim_index-1] & 1<<(mcu_tim_pin_list[i].channel_index-1)) {
127+
if (reserved_tim[mcu_tim_pin_list[i].tim_index-1] & 1<<(mcu_tim_pin_list[i].channel_index-1)) {
129128
tim_chan_taken = true;
130129
continue; //keep looking, might be another viable option
131130
}
132131
//If the frequencies are the same it's ok
133-
if(tim_frequencies[mcu_tim_pin_list[i].tim_index-1] != frequency) {
132+
if (tim_frequencies[mcu_tim_pin_list[i].tim_index-1] != frequency) {
134133
tim_taken_f_mismatch = true;
135134
continue; //keep looking
136135
}
137136
//you can't put a variable frequency on a partially reserved timer
138-
if(variable_frequency) {
137+
if (variable_frequency) {
139138
var_freq_mismatch = true;
140139
continue; //keep looking
141140
}
@@ -148,9 +147,7 @@ pwmout_result_t common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self,
148147
}
149148

150149
//handle valid/invalid timer instance
151-
//TODO: why doesn't PWM handle its own value errors like every other module?
152-
//The samd value errors don't really fit for STM32.
153-
if(self->tim!=NULL) {
150+
if (self->tim!=NULL) {
154151
//create instance
155152
TIMx = mcu_tim_banks[self->tim->tim_index-1];
156153
//reserve timer/channel
@@ -162,17 +159,13 @@ pwmout_result_t common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self,
162159
tim_frequencies[self->tim->tim_index-1] = frequency;
163160
} else { //no match found
164161
if (tim_chan_taken) {
165-
mp_raise_ValueError(translate("Timer hardware is reserved"));
166-
//return PWMOUT_ALL_TIMERS_ON_PIN_IN_USE;
162+
mp_raise_ValueError(translate("No more timers available on this pin."));
167163
} else if (tim_taken_f_mismatch) {
168-
mp_raise_ValueError(translate("Frequency mismatch with existing reserved channel"));
169-
//return PWMOUT_INVALID_FREQUENCY;
164+
mp_raise_ValueError(translate("Frequency must be the same as as the existing PWMOut using this timer"));
170165
} else if (var_freq_mismatch) {
171-
mp_raise_ValueError(translate("Cannot vary frequency of a partially reserved timer"));
172-
//return PWMOUT_INVALID_FREQUENCY; //I guess?
166+
mp_raise_ValueError(translate("Cannot vary frequency on a timer that is already in use"));
173167
} else {
174168
mp_raise_ValueError(translate("Invalid pins"));
175-
//return PWMOUT_INVALID_PIN;
176169
}
177170
}
178171

@@ -197,7 +190,7 @@ pwmout_result_t common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self,
197190

198191
uint32_t source_freq = timer_get_source_freq(self->tim->tim_index);
199192
uint32_t period = PWM_MAX_FREQ/frequency;
200-
//TODO: remove before merging.
193+
//Used for Debugging
201194
// mp_printf(&mp_plat_print, "SysCoreClock: %d\n", SystemCoreClock);
202195
// mp_printf(&mp_plat_print, "Source Freq: %d\n", source_freq);
203196
// mp_printf(&mp_plat_print, "Timer Freq: %d\n", source_freq/(source_freq / PWM_MAX_FREQ));
@@ -214,8 +207,8 @@ pwmout_result_t common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self,
214207
self->handle.Init.RepetitionCounter = 0;
215208

216209
//only run init if this is the first instance of this timer
217-
if(first_time_setup) {
218-
if(HAL_TIM_PWM_Init(&self->handle) != HAL_OK) {
210+
if (first_time_setup) {
211+
if (HAL_TIM_PWM_Init(&self->handle) != HAL_OK) {
219212
mp_raise_ValueError(translate("Timer Init Error"));
220213
}
221214
}
@@ -228,10 +221,10 @@ pwmout_result_t common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self,
228221
self->chan_handle.OCNPolarity = TIM_OCNPOLARITY_LOW; // needed for TIM1 and TIM8
229222
self->chan_handle.OCIdleState = TIM_OCIDLESTATE_SET; // needed for TIM1 and TIM8
230223
self->chan_handle.OCNIdleState = TIM_OCNIDLESTATE_SET; // needed for TIM1 and TIM8
231-
if(HAL_TIM_PWM_ConfigChannel(&self->handle, &self->chan_handle, self->channel) != HAL_OK) {
224+
if (HAL_TIM_PWM_ConfigChannel(&self->handle, &self->chan_handle, self->channel) != HAL_OK) {
232225
mp_raise_ValueError(translate("Channel Init Error"));
233226
}
234-
if(HAL_TIM_PWM_Start(&self->handle, self->channel) != HAL_OK) {
227+
if (HAL_TIM_PWM_Start(&self->handle, self->channel) != HAL_OK) {
235228
mp_raise_ValueError(translate("Error starting PWM"));
236229
}
237230

@@ -251,7 +244,7 @@ void common_hal_pulseio_pwmout_deinit(pulseio_pwmout_obj_t* self) {
251244
return;
252245
}
253246
//var freq shuts down entire timer, others just their channel
254-
if(self->variable_frequency) {
247+
if (self->variable_frequency) {
255248
reserved_tim[self->tim->tim_index-1] = 0x00;
256249
} else {
257250
reserved_tim[self->tim->tim_index-1] &= ~(1<<self->tim->channel_index);
@@ -271,7 +264,7 @@ void common_hal_pulseio_pwmout_set_duty_cycle(pulseio_pwmout_obj_t* self, uint16
271264
uint16_t duty = duty_cycle/655;
272265
uint32_t period = PWM_MAX_FREQ/self->frequency;
273266
uint32_t input = (period*duty)/100;
274-
//TODO: remove before merging
267+
//Used for debugging
275268
//mp_printf(&mp_plat_print, "duty_cycle %d, Duty: %d, Input %d\n", duty_cycle, duty, input);
276269
__HAL_TIM_SET_COMPARE(&self->handle, self->channel, input);
277270

@@ -299,14 +292,14 @@ void common_hal_pulseio_pwmout_set_frequency(pulseio_pwmout_obj_t* self, uint32_
299292
self->handle.Init.Prescaler = (source_freq / PWM_MAX_FREQ) - 1; // TIM runs at ~6MHz
300293

301294
//restart everything, adjusting for new speed
302-
if(HAL_TIM_PWM_Init(&self->handle) != HAL_OK) {
295+
if (HAL_TIM_PWM_Init(&self->handle) != HAL_OK) {
303296
mp_raise_ValueError(translate("Timer Re-Init Error"));
304297
}
305298
self->chan_handle.Pulse = (period*self->duty_cycle)/100 - 1;
306-
if(HAL_TIM_PWM_ConfigChannel(&self->handle, &self->chan_handle, self->channel) != HAL_OK) {
299+
if (HAL_TIM_PWM_ConfigChannel(&self->handle, &self->chan_handle, self->channel) != HAL_OK) {
307300
mp_raise_ValueError(translate("Channel Re-Init Error"));
308301
}
309-
if(HAL_TIM_PWM_Start(&self->handle, self->channel) != HAL_OK) {
302+
if (HAL_TIM_PWM_Start(&self->handle, self->channel) != HAL_OK) {
310303
mp_raise_ValueError(translate("Error restarting PWM"));
311304
}
312305

@@ -324,80 +317,80 @@ bool common_hal_pulseio_pwmout_get_variable_frequency(pulseio_pwmout_obj_t* self
324317

325318
STATIC void tim_clock_enable(uint16_t mask) {
326319
#ifdef TIM1
327-
if(mask & 1<<0) __HAL_RCC_TIM1_CLK_ENABLE();
320+
if (mask & 1<<0) __HAL_RCC_TIM1_CLK_ENABLE();
328321
#endif
329322
#ifdef TIM2
330-
if(mask & 1<<1) __HAL_RCC_TIM2_CLK_ENABLE();
323+
if (mask & 1<<1) __HAL_RCC_TIM2_CLK_ENABLE();
331324
#endif
332325
#ifdef TIM3
333-
if(mask & 1<<2) __HAL_RCC_TIM3_CLK_ENABLE();
326+
if (mask & 1<<2) __HAL_RCC_TIM3_CLK_ENABLE();
334327
#endif
335328
#ifdef TIM4
336-
if(mask & 1<<3) __HAL_RCC_TIM4_CLK_ENABLE();
329+
if (mask & 1<<3) __HAL_RCC_TIM4_CLK_ENABLE();
337330
#endif
338331
#ifdef TIM5
339-
if(mask & 1<<4) __HAL_RCC_TIM5_CLK_ENABLE();
332+
if (mask & 1<<4) __HAL_RCC_TIM5_CLK_ENABLE();
340333
#endif
341334
//6 and 7 are reserved ADC timers
342335
#ifdef TIM8
343-
if(mask & 1<<7) __HAL_RCC_TIM8_CLK_ENABLE();
336+
if (mask & 1<<7) __HAL_RCC_TIM8_CLK_ENABLE();
344337
#endif
345338
#ifdef TIM9
346-
if(mask & 1<<8) __HAL_RCC_TIM9_CLK_ENABLE();
339+
if (mask & 1<<8) __HAL_RCC_TIM9_CLK_ENABLE();
347340
#endif
348341
#ifdef TIM10
349-
if(mask & 1<<9) __HAL_RCC_TIM10_CLK_ENABLE();
342+
if (mask & 1<<9) __HAL_RCC_TIM10_CLK_ENABLE();
350343
#endif
351344
#ifdef TIM11
352-
if(mask & 1<<10) __HAL_RCC_TIM11_CLK_ENABLE();
345+
if (mask & 1<<10) __HAL_RCC_TIM11_CLK_ENABLE();
353346
#endif
354347
#ifdef TIM12
355-
if(mask & 1<<11) __HAL_RCC_TIM12_CLK_ENABLE();
348+
if (mask & 1<<11) __HAL_RCC_TIM12_CLK_ENABLE();
356349
#endif
357350
#ifdef TIM13
358-
if(mask & 1<<12) __HAL_RCC_TIM13_CLK_ENABLE();
351+
if (mask & 1<<12) __HAL_RCC_TIM13_CLK_ENABLE();
359352
#endif
360353
#ifdef TIM14
361-
if(mask & 1<<13) __HAL_RCC_TIM14_CLK_ENABLE();
354+
if (mask & 1<<13) __HAL_RCC_TIM14_CLK_ENABLE();
362355
#endif
363356
}
364357

365358
STATIC void tim_clock_disable(uint16_t mask) {
366359
#ifdef TIM1
367-
if(mask & 1<<0) __HAL_RCC_TIM1_CLK_DISABLE();
360+
if (mask & 1<<0) __HAL_RCC_TIM1_CLK_DISABLE();
368361
#endif
369362
#ifdef TIM2
370-
if(mask & 1<<1) __HAL_RCC_TIM2_CLK_DISABLE();
363+
if (mask & 1<<1) __HAL_RCC_TIM2_CLK_DISABLE();
371364
#endif
372365
#ifdef TIM3
373-
if(mask & 1<<2) __HAL_RCC_TIM3_CLK_DISABLE();
366+
if (mask & 1<<2) __HAL_RCC_TIM3_CLK_DISABLE();
374367
#endif
375368
#ifdef TIM4
376-
if(mask & 1<<3) __HAL_RCC_TIM4_CLK_DISABLE();
369+
if (mask & 1<<3) __HAL_RCC_TIM4_CLK_DISABLE();
377370
#endif
378371
#ifdef TIM5
379-
if(mask & 1<<4) __HAL_RCC_TIM5_CLK_DISABLE();
372+
if (mask & 1<<4) __HAL_RCC_TIM5_CLK_DISABLE();
380373
#endif
381374
//6 and 7 are reserved ADC timers
382375
#ifdef TIM8
383-
if(mask & 1<<7) __HAL_RCC_TIM8_CLK_DISABLE();
376+
if (mask & 1<<7) __HAL_RCC_TIM8_CLK_DISABLE();
384377
#endif
385378
#ifdef TIM9
386-
if(mask & 1<<8) __HAL_RCC_TIM9_CLK_DISABLE();
379+
if (mask & 1<<8) __HAL_RCC_TIM9_CLK_DISABLE();
387380
#endif
388381
#ifdef TIM10
389-
if(mask & 1<<9) __HAL_RCC_TIM10_CLK_DISABLE();
382+
if (mask & 1<<9) __HAL_RCC_TIM10_CLK_DISABLE();
390383
#endif
391384
#ifdef TIM11
392-
if(mask & 1<<10) __HAL_RCC_TIM11_CLK_DISABLE();
385+
if (mask & 1<<10) __HAL_RCC_TIM11_CLK_DISABLE();
393386
#endif
394387
#ifdef TIM12
395-
if(mask & 1<<11) __HAL_RCC_TIM12_CLK_DISABLE();
388+
if (mask & 1<<11) __HAL_RCC_TIM12_CLK_DISABLE();
396389
#endif
397390
#ifdef TIM13
398-
if(mask & 1<<12) __HAL_RCC_TIM13_CLK_DISABLE();
391+
if (mask & 1<<12) __HAL_RCC_TIM13_CLK_DISABLE();
399392
#endif
400393
#ifdef TIM14
401-
if(mask & 1<<13) __HAL_RCC_TIM14_CLK_DISABLE();
394+
if (mask & 1<<13) __HAL_RCC_TIM14_CLK_DISABLE();
402395
#endif
403-
}
396+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
6+
* Copyright (c) 2017 Lucian Copeland for Adafruit Industries
77
*
88
* Permission is hereby granted, free of charge, to any person obtaining a copy
99
* of this software and associated documentation files (the "Software"), to deal

ports/stm32f4/peripherals/stm32f4/stm32f405xx/periph.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ const mcu_tim_pin_obj_t mcu_tim_pin_list[56] = {
148148
TIM(11,3,1,&pin_PF07),
149149
TIM(13,9,1,&pin_PF08),
150150
TIM(14,9,1,&pin_PF09),
151-
// TIM(12,9,1,&pin_PH06),
151+
// TIM(12,9,1,&pin_PH06), //TODO: include these when pin map is expanded
152152
// TIM(12,9,2,&pin_PH09),
153153
// TIM(5,2,1,&pin_PH10),
154154
// TIM(5,2,2,&pin_PH11),

ports/stm32f4/peripherals/stm32f4/stm32f405xx/periph.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ extern const mcu_spi_miso_obj_t mcu_spi_miso_list[6];
4242
extern const mcu_spi_nss_obj_t mcu_spi_nss_list[6];
4343

4444
#define TIM_BANK_ARRAY_LEN 14
45-
TIM_TypeDef * mcu_tim_banks[14];
4645
#define TIM_PIN_ARRAY_LEN 56
47-
const mcu_tim_pin_obj_t mcu_tim_pin_list[56];
46+
TIM_TypeDef * mcu_tim_banks[TIM_BANK_ARRAY_LEN];
47+
const mcu_tim_pin_obj_t mcu_tim_pin_list[TIM_PIN_ARRAY_LEN];
4848

4949

5050
#endif // MICROPY_INCLUDED_STM32F4_PERIPHERALS_STM32F405XX_PERIPH_H

0 commit comments

Comments
 (0)