Skip to content

Commit 9ace277

Browse files
committed
Bugfixes in PwmOut.
1 parent dbbe5b6 commit 9ace277

File tree

1 file changed

+86
-60
lines changed
  • libraries/mbed/targets/hal/TARGET_Silicon_Labs/TARGET_EFM32

1 file changed

+86
-60
lines changed

libraries/mbed/targets/hal/TARGET_Silicon_Labs/TARGET_EFM32/pwmout_api.c

Lines changed: 86 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -24,85 +24,111 @@
2424
#include "pinmap.h"
2525
#include "PeripheralPins.h"
2626
#include "device_peripherals.h"
27+
#include "sleepmodes.h"
2728

2829
#include "em_cmu.h"
2930
#include "em_gpio.h"
3031
#include "em_timer.h"
3132

32-
static int clockfreq;
33-
static int prescaler_div;
33+
static int pwm_clockfreq;
34+
static int pwm_prescaler_div;
35+
36+
uint32_t pwmout_get_channel_route(pwmout_t *obj) {
37+
MBED_ASSERT(obj->channel != (PWMName) NC);
38+
39+
switch (obj->channel) {
40+
case PWM_CH0:
41+
return TIMER_ROUTE_CC0PEN;
42+
break;
43+
case PWM_CH1:
44+
return TIMER_ROUTE_CC1PEN;
45+
break;
46+
case PWM_CH2:
47+
return TIMER_ROUTE_CC2PEN;
48+
break;
49+
default:
50+
return 0;
51+
}
52+
}
3453

35-
uint8_t pwmout_get_index(pwmout_t *obj)
54+
void pwmout_enable_pins(pwmout_t *obj, uint8_t enable)
3655
{
37-
return 0;
56+
if (enable) {
57+
pin_mode(obj->pin, PushPull);
58+
} else {
59+
// TODO_LP return PinMode to the previous state
60+
pin_mode(obj->pin, Disabled);
61+
}
3862
}
3963

40-
void pwmout_preinit(pwmout_t *obj, PinName pin)
64+
void pwmout_enable(pwmout_t *obj, uint8_t enable)
4165
{
42-
obj->channel = (PWMName) pinmap_peripheral(pin, PinMap_PWM);
43-
obj->pin = pin;
44-
MBED_ASSERT(obj->channel != (PWMName) NC);
66+
/* Start with default CC (Compare/Capture) channel parameters */
67+
TIMER_InitCC_TypeDef timerCCInit = TIMER_INITCC_DEFAULT;
68+
if (enable) {
69+
/* Set mode to PWM */
70+
timerCCInit.mode = timerCCModePWM;
71+
}
72+
73+
/* Configure CC channel */
74+
TIMER_InitCC(PWM_TIMER, obj->channel, &timerCCInit);
4575
}
4676

4777
void pwmout_init(pwmout_t *obj, PinName pin)
4878
{
49-
pwmout_preinit(obj, pin);
79+
obj->channel = (PWMName) pinmap_peripheral(pin, PinMap_PWM);
80+
obj->pin = pin;
81+
MBED_ASSERT(obj->channel != (PWMName) NC);
5082

51-
/* Enable correct channel */
52-
switch (obj->channel) {
53-
case PWM_CH0:
54-
PWM_TIMER->ROUTE |= TIMER_ROUTE_CC0PEN;
55-
break;
56-
case PWM_CH1:
57-
PWM_TIMER->ROUTE |= TIMER_ROUTE_CC1PEN;
58-
break;
59-
case PWM_CH2:
60-
PWM_TIMER->ROUTE |= TIMER_ROUTE_CC2PEN;
61-
break;
83+
/* Turn on clock */
84+
CMU_ClockEnable(PWM_TIMER_CLOCK, true);
6285

63-
}
86+
/* Turn on timer */
87+
if(!(PWM_TIMER->STATUS & TIMER_STATUS_RUNNING)) {
88+
TIMER_Init_TypeDef timerInit = TIMER_INIT_DEFAULT;
89+
TIMER_Init(PWM_TIMER, &timerInit);
90+
}
91+
92+
/* Enable correct channel */
93+
uint32_t routeloc = pwmout_get_channel_route(obj);
94+
if(PWM_TIMER->ROUTE & routeloc) {
95+
//This channel was already in use
96+
//TODO: gracefully handle this case
97+
} else {
98+
//This channel was unused up to now
99+
PWM_TIMER->ROUTE |= routeloc;
100+
blockSleepMode(EM1);
101+
102+
//TODO: check if any channel was up already, then don't re-init timer
103+
pwmout_enable(obj, true);
104+
pwmout_enable_pins(obj, true);
105+
}
64106

65107
/* Route correct channel to location 1 */
108+
PWM_TIMER->ROUTE &= ~_TIMER_ROUTE_LOCATION_MASK;
66109
PWM_TIMER->ROUTE |= PWM_ROUTE;
67110

68111
/*HFPER is the default clock we will use. It has a frequency of 14MHz*/
69-
clockfreq = REFERENCE_FREQUENCY;
112+
pwm_clockfreq = REFERENCE_FREQUENCY;
70113

71114
/* Set default 20ms frequency and 0ms pulse width */
72115
pwmout_period(obj, 0.02);
73116
}
74117

75-
void pwmout_enable_pins(pwmout_t *obj, uint8_t enable)
76-
{
77-
if (enable) {
78-
pin_mode(obj->pin, PushPull);
79-
} else {
80-
// TODO_LP return PinMode to the previous state
81-
pin_mode(obj->pin, Disabled);
82-
}
118+
void pwmout_free(pwmout_t *obj) {
119+
uint32_t routeloc = pwmout_get_channel_route(obj);
120+
if(PWM_TIMER->ROUTE & routeloc) {
121+
//This channel was in use, so disable
122+
PWM_TIMER->ROUTE &= ~routeloc;
123+
pwmout_enable_pins(obj, false);
124+
unblockSleepMode(EM1);
125+
126+
//TODO: check if all channels are down, then switch off timer
127+
} else {
128+
//This channel was disabled already
129+
}
83130
}
84131

85-
void pwmout_enable(pwmout_t *obj, uint8_t enable)
86-
{
87-
TIMER_Init_TypeDef timerInit = TIMER_INIT_DEFAULT;
88-
89-
if (enable) {
90-
/* Start with default CC (Compare/Capture) channel parameters */
91-
TIMER_InitCC_TypeDef timerCCInit = TIMER_INITCC_DEFAULT;
92-
93-
/* Set mode to PWM */
94-
timerCCInit.mode = timerCCModePWM;
95-
96-
/* Configure CC channel */
97-
TIMER_InitCC(PWM_TIMER, obj->channel, &timerCCInit);
98-
TIMER_Init(PWM_TIMER, &timerInit);
99-
} else {
100-
timerInit.enable = false;
101-
TIMER_Init(PWM_TIMER, &timerInit);
102-
}
103-
}
104-
105-
106132
void pwmout_write(pwmout_t *obj, float value)
107133
{
108134
if (value < 0.0f) {
@@ -111,7 +137,7 @@ void pwmout_write(pwmout_t *obj, float value)
111137
value = 1;
112138
}
113139

114-
float pulse_period_in_s = obj->period_cycles / (float) clockfreq;
140+
float pulse_period_in_s = obj->period_cycles / (float) pwm_clockfreq;
115141
pwmout_pulsewidth(obj, value * pulse_period_in_s);
116142
}
117143

@@ -127,17 +153,17 @@ void pwmout_period(pwmout_t *obj, float seconds)
127153
// This gives us max resolution for a given period
128154

129155
//The value of the top register if prescaler is set to 0
130-
int cycles = clockfreq * seconds;
131-
prescaler_div = 0;
156+
int cycles = pwm_clockfreq * seconds;
157+
pwm_prescaler_div = 0;
132158

133159
//The top register is only 16 bits, so we keep dividing till we are below 0xFFFF
134160
while (cycles > 0xFFFF) {
135161
cycles /= 2;
136-
prescaler_div++;
162+
pwm_prescaler_div++;
137163

138-
//Max prescaler_div supported is 10
139-
if (prescaler_div > 10) {
140-
prescaler_div = 10;
164+
//Max pwm_prescaler_div supported is 10
165+
if (pwm_prescaler_div > 10) {
166+
pwm_prescaler_div = 10;
141167
cycles = 0xFFFF; //Set it to max possible value;
142168
break;
143169
}
@@ -146,7 +172,7 @@ void pwmout_period(pwmout_t *obj, float seconds)
146172
obj->period_cycles = cycles;
147173

148174
//Set prescaler
149-
PWM_TIMER->CTRL = (PWM_TIMER->CTRL & ~_TIMER_CTRL_PRESC_MASK) | (prescaler_div << _TIMER_CTRL_PRESC_SHIFT);
175+
PWM_TIMER->CTRL = (PWM_TIMER->CTRL & ~_TIMER_CTRL_PRESC_MASK) | (pwm_prescaler_div << _TIMER_CTRL_PRESC_SHIFT);
150176

151177
/* Set Top Value, which controls the PWM period */
152178
TIMER_TopSet(PWM_TIMER, obj->period_cycles);
@@ -164,7 +190,7 @@ void pwmout_period_us(pwmout_t *obj, int us)
164190

165191
void pwmout_pulsewidth(pwmout_t *obj, float seconds)
166192
{
167-
obj->width_cycles = clockfreq * seconds;
193+
obj->width_cycles = pwm_clockfreq * seconds;
168194
TIMER_CompareBufSet(PWM_TIMER, obj->channel, obj->width_cycles);
169195
}
170196

0 commit comments

Comments
 (0)