From 20365bac0e856934281ba9002f47e1422024c0c6 Mon Sep 17 00:00:00 2001 From: Florian Vaussard Date: Thu, 14 Dec 2017 16:50:44 +0100 Subject: [PATCH] pwm: stm32: Do not hardcode the prescalers 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 --- drivers/pwm/Kconfig.stm32 | 140 ++++++++++++++++++++++++++++++++++++++ drivers/pwm/pwm_stm32.c | 40 +++++------ 2 files changed, 160 insertions(+), 20 deletions(-) diff --git a/drivers/pwm/Kconfig.stm32 b/drivers/pwm/Kconfig.stm32 index df4d770763981b..a4010dbdf4d19f 100644 --- a/drivers/pwm/Kconfig.stm32 +++ b/drivers/pwm/Kconfig.stm32 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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. diff --git a/drivers/pwm/pwm_stm32.c b/drivers/pwm/pwm_stm32.c index efd9f82b163ab6..838992f6c1b403 100644 --- a/drivers/pwm/pwm_stm32.c +++ b/drivers/pwm/pwm_stm32.c @@ -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 */