From 3f4aa04260351f372a840850451af9341cce6b0c Mon Sep 17 00:00:00 2001 From: Hauke Petersen Date: Wed, 16 Jul 2014 22:57:38 +0200 Subject: [PATCH 1/2] drivers: updated low-level PWM driver interface --- drivers/include/periph/pwm.h | 42 +++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/drivers/include/periph/pwm.h b/drivers/include/periph/pwm.h index 2776e6b792..eb53732fe3 100644 --- a/drivers/include/periph/pwm.h +++ b/drivers/include/periph/pwm.h @@ -22,6 +22,8 @@ #include "periph_conf.h" +/* ignore file in case no PWM devices are defined */ +#if PWM_NUMOF /** * @brief Definition of available PWM devices @@ -41,7 +43,6 @@ typedef enum { #if PWM_3_EN PWM_3, /*< 4th PWM device */ #endif - PWM_UNDEFINED } pwm_t; /** @@ -71,8 +72,8 @@ typedef enum { * @param[in] resolution the PWM resolution * * @return 0 on success - * @return -1 on invalid device - * @return -2 on requested mode and/or frequency and resolution not applicable + * @return -1 on mode not applicable + * @return -2 on frequency and resolution not applicable */ int pwm_init(pwm_t dev, pwm_mode_t mode, unsigned int frequency, unsigned int resolution); @@ -87,8 +88,7 @@ int pwm_init(pwm_t dev, pwm_mode_t mode, unsigned int frequency, unsigned int re * @param[in] value the desired duty-cycle to set * * @return 0 on success - * @return -1 on invalid device - * @return -2 on invalid channel + * @return -1 on invalid channel */ int pwm_set(pwm_t dev, int channel, unsigned int value); @@ -96,21 +96,37 @@ int pwm_set(pwm_t dev, int channel, unsigned int value); * @brief Start PWM generation on the given device * * @param[in] dev device to start - * - * @return 0 on success - * @return -1 if invalid device given */ -int pwm_start(pwm_t dev); +void pwm_start(pwm_t dev); /** * @brief Stop PWM generation on the given device * * @param[in] dev device to stop - * - * @return 0 on success - * @return -1 if invalid device given */ -int pwm_stop(pwm_t dev); +void pwm_stop(pwm_t dev); + +/** + * @brief Power on the PWM device + * + * The PWM deice is powered on. It is dependent on the implementing platform, + * if the previously set configuration is still available after power on. + * + * @param[in] dev device to power on + */ +void pwm_poweron(pwm_t dev); + +/** + * @brief Power off the given PWM device + * + * The given PWM is completely powered off. On most platform this means, that + * the clock for the PWM device is disabled. + * + * @param[in] dev device to power off + */ +void pwm_poweroff(pwm_t dev); + +#endif /* PWM_NUMOF */ #endif /* __PWM_H */ /** @} */ From 9a90eae1ba8f4b962086b06cced149e71641a3ad Mon Sep 17 00:00:00 2001 From: Hauke Petersen Date: Thu, 24 Jul 2014 16:08:03 +0200 Subject: [PATCH 2/2] cpu: adjusted pwm driver impl for lpc2387 --- boards/msba2/include/periph_conf.h | 1 + cpu/lpc2387/periph/pwm.c | 52 ++++++++++++++++++------------ 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/boards/msba2/include/periph_conf.h b/boards/msba2/include/periph_conf.h index 330d29c3bf..86b6b02608 100644 --- a/boards/msba2/include/periph_conf.h +++ b/boards/msba2/include/periph_conf.h @@ -28,6 +28,7 @@ #define PWM_0_EN (1) /* PWM_0 device configuration */ +#define PWM_0_CHANNELS (3) #define PWM_0_CH0 (3) #define PWM_0_CH0_MR PWM1MR3 #define PWM_0_CH1 (4) diff --git a/cpu/lpc2387/periph/pwm.c b/cpu/lpc2387/periph/pwm.c index 0387737418..7da96d59ea 100644 --- a/cpu/lpc2387/periph/pwm.c +++ b/cpu/lpc2387/periph/pwm.c @@ -23,6 +23,8 @@ #include "periph/pwm.h" #include "periph_conf.h" +/* guard file in case no PWM device is defined */ +#if PWM_NUMOF #define PCPWM1 BIT6 @@ -47,7 +49,7 @@ int pwm_init(pwm_t dev, pwm_mode_t mode, unsigned int frequency, unsigned int re (PWM_0_FUNC << PWM_0_CH2_PIN * 2); /* power on PWM1 */ - PCONP |= BIT6; + pwm_poweron(dev); /* select PWM1 clock */ PCLKSEL0 &= ~(BIT13); @@ -78,9 +80,6 @@ int pwm_init(pwm_t dev, pwm_mode_t mode, unsigned int frequency, unsigned int re PWM1LER = BIT0 | (1 << PWM_0_CH0) | (1 << PWM_0_CH1) | (1 << PWM_0_CH2); break; #endif - case PWM_UNDEFINED: - default: - return -1; } return 0; @@ -110,42 +109,53 @@ int pwm_set(pwm_t dev, int channel, unsigned int value) } break; #endif - case PWM_UNDEFINED: - default: - return -1; } return 0; } -int pwm_start(pwm_t dev) +void pwm_start(pwm_t dev) { switch (dev) { #if PWM_0_EN case PWM_0: - PCONP |= PCPWM1; /* enable PWM1 device */ + PWM1TCR |= BIT0; break; #endif - case PWM_UNDEFINED: - default: - return -1; } - - return 0; } -int pwm_stop(pwm_t dev) +void pwm_stop(pwm_t dev) { switch (dev) { #if PWM_0_EN case PWM_0: - PCONP &= ~PCPWM1; /* disable PWM1 device */ + PWM1TCR &= ~(BIT0); break; #endif - case PWM_UNDEFINED: - default: - return -1; } - - return 0; } + +void pwm_poweron(pwm_t dev) +{ + switch (dev) { +#if PWM_0_EN + case PWM_0: + PCONP |= PCPWM1; + break; +#endif + } +} + +void pwm_poweroff(pwm_t dev) +{ + switch (dev) { +#if PWM_0_EN + case PWM_0: + PCONP &= ~(PCPWM1); + break; +#endif + } +} + +#endif /* PWM_NUMOF */