mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
cpu/stm32f4: adapted to PWM interface changes
This commit is contained in:
parent
cf7299c09e
commit
85f85a3760
@ -23,13 +23,16 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "cpu.h"
|
#include "cpu.h"
|
||||||
#include "periph/pwm.h"
|
|
||||||
#include "periph_conf.h"
|
#include "periph_conf.h"
|
||||||
|
|
||||||
/* ignore file in case no PWM devices are defined */
|
/* guard file in case no PWM device is defined */
|
||||||
#if PWM_0_EN || PWM_1_EN
|
#if (PWM_0_EN || PWM_1_EN)
|
||||||
|
|
||||||
int pwm_init(pwm_t dev, pwm_mode_t mode, unsigned int frequency, unsigned int resolution)
|
/* pull the PWM header inside the guards for now. Guards will be removed on
|
||||||
|
* adapting this driver implementation... */
|
||||||
|
#include "periph/pwm.h"
|
||||||
|
|
||||||
|
uint32_t pwm_init(pwm_t dev, pwm_mode_t mode, uint32_t freq, uint16_t res)
|
||||||
{
|
{
|
||||||
TIM_TypeDef *tim = NULL;
|
TIM_TypeDef *tim = NULL;
|
||||||
GPIO_TypeDef *port = NULL;
|
GPIO_TypeDef *port = NULL;
|
||||||
@ -114,14 +117,16 @@ int pwm_init(pwm_t dev, pwm_mode_t mode, unsigned int frequency, unsigned int re
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* set prescale and auto-reload registers to matching values for resolution and frequency */
|
/* set prescale and auto-reload registers to matching values for resolution
|
||||||
if (resolution > 0xffff || (resolution * frequency) > pwm_clk) {
|
* and frequency */
|
||||||
return -2;
|
if (res > 0xffff || (res * freq) > pwm_clk) {
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
tim->PSC = (pwm_clk / (resolution * frequency)) - 1;
|
tim->PSC = (pwm_clk / (res * freq)) - 1;
|
||||||
tim->ARR = resolution - 1;
|
tim->ARR = res - 1;
|
||||||
|
|
||||||
/* calculate the actual PWM frequency */
|
/* calculate the actual PWM frequency */
|
||||||
frequency = (pwm_clk / (resolution * (tim->PSC + 1)));
|
freq = (pwm_clk / (res * (tim->PSC + 1)));
|
||||||
|
|
||||||
/* set PWM mode */
|
/* set PWM mode */
|
||||||
switch (mode) {
|
switch (mode) {
|
||||||
@ -159,10 +164,26 @@ int pwm_init(pwm_t dev, pwm_mode_t mode, unsigned int frequency, unsigned int re
|
|||||||
/* enable timer ergo the PWM generation */
|
/* enable timer ergo the PWM generation */
|
||||||
pwm_start(dev);
|
pwm_start(dev);
|
||||||
|
|
||||||
return frequency;
|
return freq;
|
||||||
}
|
}
|
||||||
|
|
||||||
int pwm_set(pwm_t dev, int channel, unsigned int value)
|
uint8_t pwm_channels(pwm_t dev)
|
||||||
|
{
|
||||||
|
switch (dev) {
|
||||||
|
#if PWM_0_EN
|
||||||
|
case PWM_0:
|
||||||
|
return PWM_0_CHANNELS;
|
||||||
|
#endif
|
||||||
|
#if PWM_1_EN
|
||||||
|
case PWM_1:
|
||||||
|
return PWM_1_CHANNELS;
|
||||||
|
#endif
|
||||||
|
default:
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void pwm_set(pwm_t dev, uint8_t channel, uint16_t value)
|
||||||
{
|
{
|
||||||
TIM_TypeDef *tim = NULL;
|
TIM_TypeDef *tim = NULL;
|
||||||
|
|
||||||
@ -171,7 +192,7 @@ int pwm_set(pwm_t dev, int channel, unsigned int value)
|
|||||||
case PWM_0:
|
case PWM_0:
|
||||||
tim = PWM_0_DEV;
|
tim = PWM_0_DEV;
|
||||||
if (channel >= PWM_0_CHANNELS) {
|
if (channel >= PWM_0_CHANNELS) {
|
||||||
return -1;
|
return;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
#endif
|
#endif
|
||||||
@ -179,7 +200,7 @@ int pwm_set(pwm_t dev, int channel, unsigned int value)
|
|||||||
case PWM_1:
|
case PWM_1:
|
||||||
tim = PWM_1_DEV;
|
tim = PWM_1_DEV;
|
||||||
if (channel >= PWM_1_CHANNELS) {
|
if (channel >= PWM_1_CHANNELS) {
|
||||||
return -1;
|
return;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
#endif
|
#endif
|
||||||
@ -187,7 +208,7 @@ int pwm_set(pwm_t dev, int channel, unsigned int value)
|
|||||||
|
|
||||||
/* norm value to maximum possible value */
|
/* norm value to maximum possible value */
|
||||||
if (value > tim->ARR) {
|
if (value > tim->ARR) {
|
||||||
value = (unsigned int) tim->ARR;
|
value = (uint32_t)tim->ARR;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (channel) {
|
switch (channel) {
|
||||||
@ -204,10 +225,8 @@ int pwm_set(pwm_t dev, int channel, unsigned int value)
|
|||||||
tim->CCR4 = value;
|
tim->CCR4 = value;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return -1;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void pwm_start(pwm_t dev)
|
void pwm_start(pwm_t dev)
|
||||||
@ -274,4 +293,4 @@ void pwm_poweroff(pwm_t dev)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* PWM_0_EN || PWM_1_EN */
|
#endif /* (PWM_0_EN || PWM_1_EN) */
|
||||||
|
Loading…
Reference in New Issue
Block a user