Skip to content

Commit

Permalink
pwm: stm32: Do not hardcode the prescalers
Browse files Browse the repository at this point in the history
The prescalers are currently hardcoded and are not user-selectable. As a
result, the frequency of the timer can be inadequate to the task. For
instance, the frequency of the 16-bit timers (prescaler 10000) is
usually too low to correctly generate of PWM of a few kilohertz.

Hardcoded prescalers are replaced by Kconfigs so the user can choose at
compile time. The default value of each Kconfig matches the hardcoded
prescaler, so the change will be transparent.

Signed-off-by: Florian Vaussard <florian.vaussard@gmail.com>
  • Loading branch information
vaussard authored and galak committed Jun 12, 2018
1 parent f34e74d commit 20365ba
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 20 deletions.
140 changes: 140 additions & 0 deletions drivers/pwm/Kconfig.stm32
Expand Up @@ -31,6 +31,13 @@ config PWM_STM32_1_DEV_NAME
help
Specify the device name for the PWM driver.

config PWM_STM32_1_PRESCALER
int "STM32 PWM 1 Prescaler"
default 10000
depends on PWM_STM32_1
help
Clock prescaler at the input of the timer.

config PWM_STM32_2
bool "STM32 PWM 2 Output"
depends on PWM_STM32
Expand All @@ -46,6 +53,13 @@ config PWM_STM32_2_DEV_NAME
help
Specify the device name for the PWM driver.

config PWM_STM32_2_PRESCALER
int "STM32 PWM 2 Prescaler"
default 0
depends on PWM_STM32_2
help
Clock prescaler at the input of the timer.

config PWM_STM32_3
bool "STM32 PWM 3 Output"
depends on PWM_STM32
Expand All @@ -61,6 +75,13 @@ config PWM_STM32_3_DEV_NAME
help
Specify the device name for the PWM driver.

config PWM_STM32_3_PRESCALER
int "STM32 PWM 3 Prescaler"
default 10000
depends on PWM_STM32_3
help
Clock prescaler at the input of the timer.

config PWM_STM32_4
bool "STM32 PWM 4 Output"
depends on PWM_STM32
Expand All @@ -76,6 +97,13 @@ config PWM_STM32_4_DEV_NAME
help
Specify the device name for the PWM driver.

config PWM_STM32_4_PRESCALER
int "STM32 PWM 4 Prescaler"
default 10000
depends on PWM_STM32_4
help
Clock prescaler at the input of the timer.

config PWM_STM32_5
bool "STM32 PWM 5 Output"
depends on PWM_STM32
Expand All @@ -91,6 +119,13 @@ config PWM_STM32_5_DEV_NAME
help
Specify the device name for the PWM driver.

config PWM_STM32_5_PRESCALER
int "STM32 PWM 5 Prescaler"
default 0
depends on PWM_STM32_5
help
Clock prescaler at the input of the timer.

config PWM_STM32_6
bool "STM32 PWM 6 Output"
depends on PWM_STM32
Expand All @@ -106,6 +141,13 @@ config PWM_STM32_6_DEV_NAME
help
Specify the device name for the PWM driver.

config PWM_STM32_6_PRESCALER
int "STM32 PWM 6 Prescaler"
default 10000
depends on PWM_STM32_6
help
Clock prescaler at the input of the timer.

config PWM_STM32_7
bool "STM32 PWM 7 Output"
depends on PWM_STM32
Expand All @@ -121,6 +163,13 @@ config PWM_STM32_7_DEV_NAME
help
Specify the device name for the PWM driver.

config PWM_STM32_7_PRESCALER
int "STM32 PWM 7 Prescaler"
default 10000
depends on PWM_STM32_7
help
Clock prescaler at the input of the timer.

config PWM_STM32_8
bool "STM32 PWM 8 Output"
depends on PWM_STM32
Expand All @@ -136,6 +185,13 @@ config PWM_STM32_8_DEV_NAME
help
Specify the device name for the PWM driver.

config PWM_STM32_8_PRESCALER
int "STM32 PWM 8 Prescaler"
default 10000
depends on PWM_STM32_8
help
Clock prescaler at the input of the timer.

config PWM_STM32_9
bool "STM32 PWM 9 Output"
depends on PWM_STM32
Expand All @@ -151,6 +207,13 @@ config PWM_STM32_9_DEV_NAME
help
Specify the device name for the PWM driver.

config PWM_STM32_9_PRESCALER
int "STM32 PWM 9 Prescaler"
default 10000
depends on PWM_STM32_9
help
Clock prescaler at the input of the timer.

config PWM_STM32_10
bool "STM32 PWM 10 Output"
depends on PWM_STM32
Expand All @@ -166,6 +229,13 @@ config PWM_STM32_10_DEV_NAME
help
Specify the device name for the PWM driver.

config PWM_STM32_10_PRESCALER
int "STM32 PWM 10 Prescaler"
default 10000
depends on PWM_STM32_10
help
Clock prescaler at the input of the timer.

config PWM_STM32_11
bool "STM32 PWM 11 Output"
depends on PWM_STM32
Expand All @@ -181,6 +251,13 @@ config PWM_STM32_11_DEV_NAME
help
Specify the device name for the PWM driver.

config PWM_STM32_11_PRESCALER
int "STM32 PWM 11 Prescaler"
default 10000
depends on PWM_STM32_11
help
Clock prescaler at the input of the timer.

config PWM_STM32_12
bool "STM32 PWM 12 Output"
depends on PWM_STM32
Expand All @@ -196,6 +273,13 @@ config PWM_STM32_12_DEV_NAME
help
Specify the device name for the PWM driver.

config PWM_STM32_12_PRESCALER
int "STM32 PWM 12 Prescaler"
default 10000
depends on PWM_STM32_12
help
Clock prescaler at the input of the timer.

config PWM_STM32_13
bool "STM32 PWM 13 Output"
depends on PWM_STM32
Expand All @@ -211,6 +295,13 @@ config PWM_STM32_13_DEV_NAME
help
Specify the device name for the PWM driver.

config PWM_STM32_13_PRESCALER
int "STM32 PWM 13 Prescaler"
default 10000
depends on PWM_STM32_13
help
Clock prescaler at the input of the timer.

config PWM_STM32_14
bool "STM32 PWM 14 Output"
depends on PWM_STM32
Expand All @@ -226,6 +317,13 @@ config PWM_STM32_14_DEV_NAME
help
Specify the device name for the PWM driver.

config PWM_STM32_14_PRESCALER
int "STM32 PWM 14 Prescaler"
default 10000
depends on PWM_STM32_14
help
Clock prescaler at the input of the timer.

config PWM_STM32_15
bool "STM32 PWM 15 Output"
depends on PWM_STM32
Expand All @@ -241,6 +339,13 @@ config PWM_STM32_15_DEV_NAME
help
Specify the device name for the PWM driver.

config PWM_STM32_15_PRESCALER
int "STM32 PWM 15 Prescaler"
default 10000
depends on PWM_STM32_15
help
Clock prescaler at the input of the timer.

config PWM_STM32_16
bool "STM32 PWM 16 Output"
depends on PWM_STM32
Expand All @@ -256,6 +361,13 @@ config PWM_STM32_16_DEV_NAME
help
Specify the device name for the PWM driver.

config PWM_STM32_16_PRESCALER
int "STM32 PWM 16 Prescaler"
default 10000
depends on PWM_STM32_16
help
Clock prescaler at the input of the timer.

config PWM_STM32_17
bool "STM32 PWM 17 Output"
depends on PWM_STM32
Expand All @@ -271,6 +383,13 @@ config PWM_STM32_17_DEV_NAME
help
Specify the device name for the PWM driver.

config PWM_STM32_17_PRESCALER
int "STM32 PWM 17 Prescaler"
default 10000
depends on PWM_STM32_17
help
Clock prescaler at the input of the timer.

config PWM_STM32_18
bool "STM32 PWM 18 Output"
depends on PWM_STM32
Expand All @@ -286,6 +405,13 @@ config PWM_STM32_18_DEV_NAME
help
Specify the device name for the PWM driver.

config PWM_STM32_18_PRESCALER
int "STM32 PWM 18 Prescaler"
default 10000
depends on PWM_STM32_18
help
Clock prescaler at the input of the timer.

config PWM_STM32_19
bool "STM32 PWM 19 Output"
depends on PWM_STM32
Expand All @@ -301,6 +427,13 @@ config PWM_STM32_19_DEV_NAME
help
Specify the device name for the PWM driver.

config PWM_STM32_19_PRESCALER
int "STM32 PWM 19 Prescaler"
default 10000
depends on PWM_STM32_19
help
Clock prescaler at the input of the timer.

config PWM_STM32_20
bool "STM32 PWM 20 Output"
depends on PWM_STM32
Expand All @@ -315,3 +448,10 @@ config PWM_STM32_20_DEV_NAME
depends on PWM_STM32_20
help
Specify the device name for the PWM driver.

config PWM_STM32_20_PRESCALER
int "STM32 PWM 20 Prescaler"
default 10000
depends on PWM_STM32_20
help
Clock prescaler at the input of the timer.
40 changes: 20 additions & 20 deletions drivers/pwm/pwm_stm32.c
Expand Up @@ -221,100 +221,100 @@ static int pwm_stm32_init(struct device *dev)

#ifdef CONFIG_PWM_STM32_1
/* 16-bit advanced-control timer */
PWM_DEVICE_INIT_STM32(1, APB2, 10000)
PWM_DEVICE_INIT_STM32(1, APB2, CONFIG_PWM_STM32_1_PRESCALER)
#endif /* CONFIG_PWM_STM32_1 */

#ifdef CONFIG_PWM_STM32_2
/* 32-bit general-purpose timer */
PWM_DEVICE_INIT_STM32(2, APB1, 0)
PWM_DEVICE_INIT_STM32(2, APB1, CONFIG_PWM_STM32_2_PRESCALER)
#endif /* CONFIG_PWM_STM32_2 */

#ifdef CONFIG_PWM_STM32_3
/* 16-bit general-purpose timer */
PWM_DEVICE_INIT_STM32(3, APB1, 10000)
PWM_DEVICE_INIT_STM32(3, APB1, CONFIG_PWM_STM32_3_PRESCALER)
#endif /* CONFIG_PWM_STM32_3 */

#ifdef CONFIG_PWM_STM32_4
/* 16-bit general-purpose timer */
PWM_DEVICE_INIT_STM32(4, APB1, 10000)
PWM_DEVICE_INIT_STM32(4, APB1, CONFIG_PWM_STM32_4_PRESCALER)
#endif /* CONFIG_PWM_STM32_4 */

#ifdef CONFIG_PWM_STM32_5
/* 32-bit general-purpose timer */
PWM_DEVICE_INIT_STM32(5, APB1, 0)
PWM_DEVICE_INIT_STM32(5, APB1, CONFIG_PWM_STM32_5_PRESCALER)
#endif /* CONFIG_PWM_STM32_5 */

#ifdef CONFIG_PWM_STM32_6
/* 16-bit basic timer */
PWM_DEVICE_INIT_STM32(6, APB1, 10000)
PWM_DEVICE_INIT_STM32(6, APB1, CONFIG_PWM_STM32_6_PRESCALER)
#endif /* CONFIG_PWM_STM32_6 */

#ifdef CONFIG_PWM_STM32_7
/* 16-bit basic timer */
PWM_DEVICE_INIT_STM32(7, APB1, 10000)
PWM_DEVICE_INIT_STM32(7, APB1, CONFIG_PWM_STM32_7_PRESCALER)
#endif /* CONFIG_PWM_STM32_7 */

#ifdef CONFIG_PWM_STM32_8
/* 16-bit advanced-control timer */
PWM_DEVICE_INIT_STM32(8, APB2, 10000)
PWM_DEVICE_INIT_STM32(8, APB2, CONFIG_PWM_STM32_8_PRESCALER)
#endif /* CONFIG_PWM_STM32_8 */

#ifdef CONFIG_PWM_STM32_9
/* 16-bit general-purpose timer */
PWM_DEVICE_INIT_STM32(9, APB2, 10000)
PWM_DEVICE_INIT_STM32(9, APB2, CONFIG_PWM_STM32_9_PRESCALER)
#endif /* CONFIG_PWM_STM32_9 */

#ifdef CONFIG_PWM_STM32_10
/* 16-bit general-purpose timer */
PWM_DEVICE_INIT_STM32(10, APB2, 10000)
PWM_DEVICE_INIT_STM32(10, APB2, CONFIG_PWM_STM32_10_PRESCALER)
#endif /* CONFIG_PWM_STM32_10 */

#ifdef CONFIG_PWM_STM32_11
/* 16-bit general-purpose timer */
PWM_DEVICE_INIT_STM32(11, APB2, 10000)
PWM_DEVICE_INIT_STM32(11, APB2, CONFIG_PWM_STM32_11_PRESCALER)
#endif /* CONFIG_PWM_STM32_11 */

#ifdef CONFIG_PWM_STM32_12
/* 16-bit general-purpose timer */
PWM_DEVICE_INIT_STM32(12, APB1, 10000)
PWM_DEVICE_INIT_STM32(12, APB1, CONFIG_PWM_STM32_12_PRESCALER)
#endif /* CONFIG_PWM_STM32_12 */

#ifdef CONFIG_PWM_STM32_13
/* 16-bit general-purpose timer */
PWM_DEVICE_INIT_STM32(13, APB1, 10000)
PWM_DEVICE_INIT_STM32(13, APB1, CONFIG_PWM_STM32_13_PRESCALER)
#endif /* CONFIG_PWM_STM32_13 */

#ifdef CONFIG_PWM_STM32_14
/* 16-bit general-purpose timer */
PWM_DEVICE_INIT_STM32(14, APB1, 10000)
PWM_DEVICE_INIT_STM32(14, APB1, CONFIG_PWM_STM32_14_PRESCALER)
#endif /* CONFIG_PWM_STM32_14 */

#ifdef CONFIG_PWM_STM32_15
/* 16-bit general-purpose timer */
PWM_DEVICE_INIT_STM32(15, APB2, 10000)
PWM_DEVICE_INIT_STM32(15, APB2, CONFIG_PWM_STM32_15_PRESCALER)
#endif /* CONFIG_PWM_STM32_15 */

#ifdef CONFIG_PWM_STM32_16
/* 16-bit general-purpose timer */
PWM_DEVICE_INIT_STM32(16, APB2, 10000)
PWM_DEVICE_INIT_STM32(16, APB2, CONFIG_PWM_STM32_16_PRESCALER)
#endif /* CONFIG_PWM_STM32_16 */

#ifdef CONFIG_PWM_STM32_17
/* 16-bit general-purpose timer */
PWM_DEVICE_INIT_STM32(17, APB2, 10000)
PWM_DEVICE_INIT_STM32(17, APB2, CONFIG_PWM_STM32_17_PRESCALER)
#endif /* CONFIG_PWM_STM32_17 */

#ifdef CONFIG_PWM_STM32_18
/* 16-bit advanced timer */
PWM_DEVICE_INIT_STM32(18, APB1, 10000)
PWM_DEVICE_INIT_STM32(18, APB1, CONFIG_PWM_STM32_18_PRESCALER)
#endif /* CONFIG_PWM_STM32_18 */

#ifdef CONFIG_PWM_STM32_19
/* 16-bit general-purpose timer */
PWM_DEVICE_INIT_STM32(19, APB2, 10000)
PWM_DEVICE_INIT_STM32(19, APB2, CONFIG_PWM_STM32_19_PRESCALER)
#endif /* CONFIG_PWM_STM32_19 */

#ifdef CONFIG_PWM_STM32_20
/* 16-bit advanced timer */
PWM_DEVICE_INIT_STM32(20, APB2, 10000)
PWM_DEVICE_INIT_STM32(20, APB2, CONFIG_PWM_STM32_20_PRESCALER)
#endif /* CONFIG_PWM_STM32_20 */

0 comments on commit 20365ba

Please sign in to comment.