From da89f6ac5fcaf6c39b0c0aa488f05838d093db94 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Sun, 26 Apr 2020 22:26:01 +0200 Subject: [PATCH 1/8] cpu/samd21: don't hard-code number of channels Each TCC can have 8 PWM channels, so don't hard-code 3 channels/TCC. --- cpu/samd21/include/periph_cpu.h | 3 ++- cpu/samd21/periph/pwm.c | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/cpu/samd21/include/periph_cpu.h b/cpu/samd21/include/periph_cpu.h index eb36f825db..8c70774a2d 100644 --- a/cpu/samd21/include/periph_cpu.h +++ b/cpu/samd21/include/periph_cpu.h @@ -84,7 +84,8 @@ typedef struct { */ typedef struct { Tcc *dev; /**< TCC device to use */ - pwm_conf_chan_t chan[3]; /**< channel configuration */ + const pwm_conf_chan_t *chan;/**< channel configuration */ + const uint8_t chan_numof; /**< number of channels */ } pwm_conf_t; /** diff --git a/cpu/samd21/periph/pwm.c b/cpu/samd21/periph/pwm.c index d6389c5821..1806df1f13 100644 --- a/cpu/samd21/periph/pwm.c +++ b/cpu/samd21/periph/pwm.c @@ -151,7 +151,7 @@ uint32_t pwm_init(pwm_t dev, pwm_mode_t mode, uint32_t freq, uint16_t res) f_real = (CLOCK_CORECLOCK / (scale * res)); /* configure the used pins */ - for (unsigned i = 0; i < PWM_MAX_CHANNELS; i++) { + for (unsigned i = 0; i < pwm_config[dev].chan_numof; i++) { if (pwm_config[dev].chan[i].pin != GPIO_UNDEF) { gpio_init(pwm_config[dev].chan[i].pin, GPIO_OUT); gpio_init_mux(pwm_config[dev].chan[i].pin, pwm_config[dev].chan[i].mux); @@ -195,12 +195,12 @@ uint32_t pwm_init(pwm_t dev, pwm_mode_t mode, uint32_t freq, uint16_t res) uint8_t pwm_channels(pwm_t dev) { - return ARRAY_SIZE(pwm_config[dev].chan); + return pwm_config[dev].chan_numof; } void pwm_set(pwm_t dev, uint8_t channel, uint16_t value) { - if ((channel >= PWM_MAX_CHANNELS) || + if ((channel >= pwm_config[dev].chan_numof) || (pwm_config[dev].chan[channel].pin == GPIO_UNDEF)) { return; } From cc3779671eca7494890dccb0e8d0b0834efbd65f Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Sun, 26 Apr 2020 22:28:36 +0200 Subject: [PATCH 2/8] boards/samr21-xpro: update PWM configuration --- boards/samr21-xpro/include/periph_conf.h | 38 +++++++++++++----------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/boards/samr21-xpro/include/periph_conf.h b/boards/samr21-xpro/include/periph_conf.h index f84d2acf67..847103a57c 100644 --- a/boards/samr21-xpro/include/periph_conf.h +++ b/boards/samr21-xpro/include/periph_conf.h @@ -185,33 +185,37 @@ static const uart_conf_t uart_config[] = { */ #define PWM_0_EN 1 #define PWM_1_EN 1 -#define PWM_MAX_CHANNELS 3 -/* for compatibility with test application */ -#define PWM_0_CHANNELS PWM_MAX_CHANNELS -#define PWM_1_CHANNELS PWM_MAX_CHANNELS + +#if PWM_0_EN +/* PWM0 channels */ +static const pwm_conf_chan_t pwm_chan0_config[] = { + /* GPIO pin, MUX value, TCC channel */ + { GPIO_PIN(PA, 6), GPIO_MUX_E, 0 }, + { GPIO_PIN(PA, 7), GPIO_MUX_E, 1 }, +}; +#endif +#if PWM_1_EN +/* PWM1 channels */ +static const pwm_conf_chan_t pwm_chan1_config[] = { + /* GPIO pin, MUX value, TCC channel */ + { GPIO_PIN(PA, 16), GPIO_MUX_F, 0 }, + { GPIO_PIN(PA, 18), GPIO_MUX_F, 2 }, + { GPIO_PIN(PA, 19), GPIO_MUX_F, 3 }, +}; +#endif /* PWM device configuration */ static const pwm_conf_t pwm_config[] = { #if PWM_0_EN - {TCC1, { - /* GPIO pin, MUX value, TCC channel */ - { GPIO_PIN(PA, 6), GPIO_MUX_E, 0 }, - { GPIO_PIN(PA, 7), GPIO_MUX_E, 1 }, - { GPIO_UNDEF, (gpio_mux_t)0, 2 } - }}, + {TCC1, pwm_chan0_config, ARRAY_SIZE(pwm_chan0_config)}, #endif #if PWM_1_EN - {TCC0, { - /* GPIO pin, MUX value, TCC channel */ - { GPIO_PIN(PA, 16), GPIO_MUX_F, 0 }, - { GPIO_PIN(PA, 18), GPIO_MUX_F, 2 }, - { GPIO_PIN(PA, 19), GPIO_MUX_F, 3 } - }} + {TCC0, pwm_chan1_config, ARRAY_SIZE(pwm_chan1_config)}, #endif }; /* number of devices that are actually defined */ -#define PWM_NUMOF (2U) +#define PWM_NUMOF ARRAY_SIZE(pwm_config) /** @} */ /** From ed3f951825753f2cbb959c78df82ed5c27914558 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Sun, 26 Apr 2020 22:35:24 +0200 Subject: [PATCH 3/8] boards/samd21-xpro: update PWM configuration --- boards/samd21-xpro/include/periph_conf.h | 50 ++++++++++++++---------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/boards/samd21-xpro/include/periph_conf.h b/boards/samd21-xpro/include/periph_conf.h index 33e30efa36..426305b10a 100644 --- a/boards/samd21-xpro/include/periph_conf.h +++ b/boards/samd21-xpro/include/periph_conf.h @@ -201,39 +201,47 @@ static const uart_conf_t uart_config[] = { #define PWM_0_EN 1 #define PWM_1_EN 0 #define PWM_2_EN 0 -#define PWM_MAX_CHANNELS 2 -/* for compatibility with test application */ -#define PWM_0_CHANNELS PWM_MAX_CHANNELS -#define PWM_1_CHANNELS PWM_MAX_CHANNELS -#define PWM_2_CHANNELS PWM_MAX_CHANNELS + +#if PWM_0_EN +/* PWM0 channels */ +static const pwm_conf_chan_t pwm_chan0_config[] = { + /* GPIO pin, MUX value, TCC channel */ + { GPIO_PIN(PA, 12), GPIO_MUX_E, 0 }, + { GPIO_PIN(PA, 13), GPIO_MUX_E, 1 }, +}; +#endif +#if PWM_1_EN +/* PWM1 channels */ +static const pwm_conf_chan_t pwm_chan1_config[] = { + /* GPIO pin, MUX value, TCC channel */ + { GPIO_PIN(PB, 12), GPIO_MUX_E, 0 }, + { GPIO_PIN(PB, 13), GPIO_MUX_E, 1 }, +}; +#endif +#if PWM_2_EN +/* PWM2 channels */ +static const pwm_conf_chan_t pwm_chan2_config[] = { + /* GPIO pin, MUX value, TCC channel */ + { GPIO_PIN(PB, 02), GPIO_MUX_E, 0 }, + { GPIO_PIN(PB, 03), GPIO_MUX_E, 1 }, +}; +#endif /* PWM device configuration */ static const pwm_conf_t pwm_config[] = { #if PWM_0_EN - {TCC2, { - /* GPIO pin, MUX value, TCC channel */ - { GPIO_PIN(PA, 12), GPIO_MUX_E, 0 }, - { GPIO_PIN(PA, 13), GPIO_MUX_E, 1 }, - }}, + {TCC2, pwm_chan0_config, ARRAY_SIZE(pwm_chan0_config)}, #endif #if PWM_1_EN - {TC4, { - /* GPIO pin, MUX value, TCC channel */ - { GPIO_PIN(PB, 12), GPIO_MUX_E, 0 }, - { GPIO_PIN(PB, 13), GPIO_MUX_E, 1 }, - }} + {TC4, pwm_chan1_config, ARRAY_SIZE(pwm_chan1_config)}, #endif #if PWM_2_EN - {TC6, { - /* GPIO pin, MUX value, TCC channel */ - { GPIO_PIN(PB, 02), GPIO_MUX_E, 0 }, - { GPIO_PIN(PB, 03), GPIO_MUX_E, 1 }, - }} + {TC6, pwm_chan2_config, ARRAY_SIZE(pwm_chan2_config)}, #endif }; /* number of devices that are actually defined */ -#define PWM_NUMOF (3U) +#define PWM_NUMOF ARRAY_SIZE(pwm_config) /** @} */ /** From e03b692ff72689e9235ae21ac5dd30de04959031 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Sun, 26 Apr 2020 22:39:38 +0200 Subject: [PATCH 4/8] boards/arduino-zero: update PWM configuration --- boards/arduino-zero/include/periph_conf.h | 35 +++++++++++++---------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/boards/arduino-zero/include/periph_conf.h b/boards/arduino-zero/include/periph_conf.h index 929371e32f..5bb3d1fb2d 100644 --- a/boards/arduino-zero/include/periph_conf.h +++ b/boards/arduino-zero/include/periph_conf.h @@ -171,31 +171,36 @@ static const uart_conf_t uart_config[] = { */ #define PWM_0_EN 1 #define PWM_1_EN 1 -#define PWM_MAX_CHANNELS 2 -/* for compatibility with test application */ -#define PWM_0_CHANNELS PWM_MAX_CHANNELS -#define PWM_1_CHANNELS PWM_MAX_CHANNELS + +#if PWM_0_EN +/* PWM0 channels */ +static const pwm_conf_chan_t pwm_chan0_config[] = { + /* GPIO pin, MUX value, TCC channel */ + { GPIO_PIN(PA, 8), GPIO_MUX_E, 0 }, + { GPIO_PIN(PA, 9), GPIO_MUX_E, 1 }, +}; +#endif +#if PWM_1_EN +/* PWM1 channels */ +static const pwm_conf_chan_t pwm_chan1_config[] = { + /* GPIO pin, MUX value, TCC channel */ + { GPIO_PIN(PA, 6), GPIO_MUX_E, 0 }, + { GPIO_PIN(PA, 7), GPIO_MUX_E, 1 }, +}; +#endif /* PWM device configuration */ static const pwm_conf_t pwm_config[] = { #if PWM_0_EN - {TCC0, { - /* GPIO pin, MUX value, TCC channel */ - { GPIO_PIN(PA, 8), GPIO_MUX_E, 0 }, - { GPIO_PIN(PA, 9), GPIO_MUX_E, 1 }, - }}, + {TCC0, pwm_chan0_config, ARRAY_SIZE(pwm_chan0_config)}, #endif #if PWM_1_EN - {TCC1, { - /* GPIO pin, MUX value, TCC channel */ - { GPIO_PIN(PA, 6), GPIO_MUX_E, 0 }, - { GPIO_PIN(PA, 7), GPIO_MUX_E, 1 }, - }}, + {TCC1, pwm_chan1_config, ARRAY_SIZE(pwm_chan1_config)}, #endif }; /* number of devices that are actually defined */ -#define PWM_NUMOF (2U) +#define PWM_NUMOF ARRAY_SIZE(pwm_config) /** @} */ /** From 335f669933292f13b6423f4c75c44bcabf7739c6 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Sun, 26 Apr 2020 22:42:20 +0200 Subject: [PATCH 5/8] boards/hamilton: update PWM configuration --- boards/hamilton/include/periph_conf.h | 45 ++++++++++++++++----------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/boards/hamilton/include/periph_conf.h b/boards/hamilton/include/periph_conf.h index 4909c4bae7..68d8881736 100644 --- a/boards/hamilton/include/periph_conf.h +++ b/boards/hamilton/include/periph_conf.h @@ -179,28 +179,37 @@ static const adc_conf_chan_t adc_channels[] = { */ #define PWM_0_EN 1 #define PWM_1_EN 1 -#define PWM_NUMOF (PWM_0_EN + PWM_1_EN) -#define PWM_MAX_CHANNELS 2 -/* PWM device configuration */ -#if PWM_NUMOF -static const pwm_conf_t pwm_config[] = { #if PWM_0_EN - {TCC1, { - /* GPIO pin, MUX value, TCC channel */ - {GPIO_PIN(PA, 6), GPIO_MUX_E, 0}, - {GPIO_PIN(PA, 7), GPIO_MUX_E, 1} - }}, -#endif -#if PWM_1_EN - {TCC0, { - /* GPIO pin, MUX value, TCC channel */ - {GPIO_PIN(PA, 18), GPIO_MUX_F, 2}, - {GPIO_PIN(PA, 19), GPIO_MUX_F, 3} - }}, -#endif +/* PWM0 channels */ +static const pwm_conf_chan_t pwm_chan0_config[] = { + /* GPIO pin, MUX value, TCC channel */ + {GPIO_PIN(PA, 6), GPIO_MUX_E, 0}, + {GPIO_PIN(PA, 7), GPIO_MUX_E, 1}, }; #endif +#if PWM_1_EN +/* PWM1 channels */ +static const pwm_conf_chan_t pwm_chan1_config[] = { + /* GPIO pin, MUX value, TCC channel */ + {GPIO_PIN(PA, 18), GPIO_MUX_F, 2}, + {GPIO_PIN(PA, 19), GPIO_MUX_F, 3}, +}; +#endif + + +/* PWM device configuration */ +static const pwm_conf_t pwm_config[] = { +#if PWM_0_EN + {TCC1, pwm_chan0_config, ARRAY_SIZE(pwm_chan0_config)}, +#endif +#if PWM_1_EN + {TCC0, pwm_chan1_config, ARRAY_SIZE(pwm_chan1_config)}, +#endif +}; + +/* number of devices that are actually defined */ +#define PWM_NUMOF ARRAY_SIZE(pwm_config) /** @} */ /** From 393105a9e1dfd815b859464ec73198dd6cfa6b65 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Sun, 26 Apr 2020 22:52:29 +0200 Subject: [PATCH 6/8] boards/common/arduino-mkr: update PWM configuration --- .../arduino-mkr/include/periph_conf_common.h | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/boards/common/arduino-mkr/include/periph_conf_common.h b/boards/common/arduino-mkr/include/periph_conf_common.h index 646aafe445..c64724ca5d 100644 --- a/boards/common/arduino-mkr/include/periph_conf_common.h +++ b/boards/common/arduino-mkr/include/periph_conf_common.h @@ -128,18 +128,20 @@ static const tc32_conf_t timer_config[] = { * @{ */ #define PWM_0_EN 1 -#define PWM_MAX_CHANNELS (2U) -/* for compatibility with test application */ -#define PWM_0_CHANNELS PWM_MAX_CHANNELS + +#if PWM_0_EN +/* PWM0 channels */ +static const pwm_conf_chan_t pwm_chan0_config[] = { + /* GPIO pin, MUX value, TCC channel */ + { GPIO_PIN(PA, 10), GPIO_MUX_F, 2 }, /* ~2 */ + { GPIO_PIN(PA, 11), GPIO_MUX_F, 3 }, /* ~3 */ +}; +#endif /* PWM device configuration */ static const pwm_conf_t pwm_config[] = { #if PWM_0_EN - {TCC0, { - /* GPIO pin, MUX value, TCC channel */ - { GPIO_PIN(PA, 10), GPIO_MUX_F, 2 }, /* ~2 */ - { GPIO_PIN(PA, 11), GPIO_MUX_F, 3 }, /* ~3 */ - }} + {TCC0, pwm_chan0_config, ARRAY_SIZE(pwm_chan0_config)}, #endif }; From f76f6f49355b3808cec1ad74777b8f73d8f024e7 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Sun, 26 Apr 2020 22:54:12 +0200 Subject: [PATCH 7/8] boards/sodaq-autonomo: update PWM configuration --- boards/sodaq-autonomo/include/periph_conf.h | 38 ++++++++++++--------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/boards/sodaq-autonomo/include/periph_conf.h b/boards/sodaq-autonomo/include/periph_conf.h index 310c716008..f44adb8e30 100644 --- a/boards/sodaq-autonomo/include/periph_conf.h +++ b/boards/sodaq-autonomo/include/periph_conf.h @@ -149,33 +149,37 @@ static const adc_conf_chan_t adc_channels[] = { */ #define PWM_0_EN 1 #define PWM_1_EN 1 -#define PWM_MAX_CHANNELS 3 -/* for compatibility with test application */ -#define PWM_0_CHANNELS PWM_MAX_CHANNELS -#define PWM_1_CHANNELS PWM_MAX_CHANNELS + +#if PWM_0_EN +/* PWM0 channels */ +static const pwm_conf_chan_t pwm_chan0_config[] = { + /* GPIO pin, MUX value, TCC channel */ + { GPIO_PIN(PA, 6), GPIO_MUX_E, 0 }, + { GPIO_PIN(PA, 7), GPIO_MUX_E, 1 }, +}; +#endif +#if PWM_1_EN +/* PWM1 channels */ +static const pwm_conf_chan_t pwm_chan1_config[] = { + /* GPIO pin, MUX value, TCC channel */ + { GPIO_PIN(PA, 16), GPIO_MUX_F, 0 }, + { GPIO_PIN(PA, 18), GPIO_MUX_F, 2 }, + { GPIO_PIN(PA, 19), GPIO_MUX_F, 3 } +}; +#endif /* PWM device configuration */ static const pwm_conf_t pwm_config[] = { #if PWM_0_EN - {TCC1, { - /* GPIO pin, MUX value, TCC channel */ - { GPIO_PIN(PA, 6), GPIO_MUX_E, 0 }, - { GPIO_PIN(PA, 7), GPIO_MUX_E, 1 }, - { GPIO_UNDEF, (gpio_mux_t)0, 2 } - }}, + {TCC1, pwm_chan0_config, ARRAY_SIZE(pwm_chan0_config)}, #endif #if PWM_1_EN - {TCC0, { - /* GPIO pin, MUX value, TCC channel */ - { GPIO_PIN(PA, 16), GPIO_MUX_F, 0 }, - { GPIO_PIN(PA, 18), GPIO_MUX_F, 2 }, - { GPIO_PIN(PA, 19), GPIO_MUX_F, 3 } - }} + {TCC0, pwm_chan1_config, ARRAY_SIZE(pwm_chan1_config)}, #endif }; /* number of devices that are actually defined */ -#define PWM_NUMOF (2U) +#define PWM_NUMOF ARRAY_SIZE(pwm_config) /** @} */ /** From 5bb1978de21efeb2fe2d2081003e3d3859ffca12 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Sun, 26 Apr 2020 22:56:39 +0200 Subject: [PATCH 8/8] boards/feather-m0: update PWM configuration --- boards/feather-m0/include/periph_conf.h | 33 ++++++++++++++----------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/boards/feather-m0/include/periph_conf.h b/boards/feather-m0/include/periph_conf.h index e697bf222b..adaccac4fc 100644 --- a/boards/feather-m0/include/periph_conf.h +++ b/boards/feather-m0/include/periph_conf.h @@ -151,31 +151,34 @@ static const uart_conf_t uart_config[] = { */ #define PWM_0_EN 1 #define PWM_1_EN 1 -#define PWM_MAX_CHANNELS 2 -/* for compatibility with test application */ -#define PWM_0_CHANNELS PWM_MAX_CHANNELS -#define PWM_1_CHANNELS PWM_MAX_CHANNELS + +#if PWM_0_EN +/* PWM0 channels */ +static const pwm_conf_chan_t pwm_chan0_config[] = { + /* GPIO pin, MUX value, TCC channel */ + { GPIO_PIN(PA, 7), GPIO_MUX_E, 1 }, /* ~9 */ +}; +#endif +#if PWM_1_EN +/* PWM1 channels */ +static const pwm_conf_chan_t pwm_chan1_config[] = { + /* GPIO pin, MUX value, TCC channel */ + { GPIO_PIN(PA, 16), GPIO_MUX_E, 0 }, /* ~11 */ +}; +#endif /* PWM device configuration */ static const pwm_conf_t pwm_config[] = { #if PWM_0_EN - {TCC0, { - /* GPIO pin, MUX value, TCC channel */ - { GPIO_UNDEF, (gpio_mux_t)0, 0 }, - { GPIO_PIN(PA, 7), GPIO_MUX_E, 1 }, /* ~9 */ - }}, + {TCC0, pwm_chan0_config, ARRAY_SIZE(pwm_chan0_config)}, #endif #if PWM_1_EN - {TCC2, { - /* GPIO pin, MUX value, TCC channel */ - { GPIO_PIN(PA, 16), GPIO_MUX_E, 0 }, /* ~11 */ - { GPIO_UNDEF, (gpio_mux_t)0, 1 }, - }}, + {TCC2, pwm_chan1_config, ARRAY_SIZE(pwm_chan1_config)}, #endif }; /* number of devices that are actually defined */ -#define PWM_NUMOF (2U) +#define PWM_NUMOF ARRAY_SIZE(pwm_config) /** @} */ /**