Skip to content

Commit 10a39f4

Browse files
committed
Merge pull request #1416 from leibin2014/master
Free hardware resource in pwmout_free()
2 parents 9ccd193 + 6127c0b commit 10a39f4

File tree

1 file changed

+36
-3
lines changed
  • libraries/mbed/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822

1 file changed

+36
-3
lines changed

libraries/mbed/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/pwmout_api.c

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,19 @@ void timer_init(uint8_t pwmChoice)
116116
timer->TASKS_START = 0x01;
117117
}
118118

119+
static void timer_free()
120+
{
121+
NRF_TIMER_Type *timer = Timers[0];
122+
for(uint8_t i = 1; i < NO_PWMS; i++){
123+
if(PWM_taken[i]){
124+
break;
125+
}
126+
if((i == NO_PWMS - 1) && (!PWM_taken[i]))
127+
timer->TASKS_STOP = 0x01;
128+
}
129+
}
130+
131+
119132
/** @brief Function for initializing the GPIO Tasks/Events peripheral.
120133
*/
121134
void gpiote_init(PinName pin, uint8_t channel_number)
@@ -154,6 +167,14 @@ void gpiote_init(PinName pin, uint8_t channel_number)
154167
__NOP();
155168
}
156169

170+
static void gpiote_free(PinName pin,uint8_t channel_number)
171+
{
172+
NRF_GPIOTE->TASKS_OUT[channel_number] = 0;
173+
NRF_GPIOTE->CONFIG[channel_number] = 0;
174+
NRF_GPIO->PIN_CNF[pin] = (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos);
175+
176+
}
177+
157178
/** @brief Function for initializing the Programmable Peripheral Interconnect peripheral.
158179
*/
159180
static void ppi_init(uint8_t pwm)
@@ -173,6 +194,16 @@ static void ppi_init(uint8_t pwm)
173194
(1 << (channel_number + 1));
174195
}
175196

197+
static void ppi_free(uint8_t pwm)
198+
{
199+
//using ppi channels 0-7 (only 0-7 are available)
200+
uint8_t channel_number = 2*pwm;
201+
202+
// Disable PPI channels.
203+
NRF_PPI->CHEN &= (~(1 << channel_number))
204+
& (~(1 << (channel_number+1)));
205+
}
206+
176207
void setModulation(pwmout_t *obj, uint8_t toggle, uint8_t high)
177208
{
178209
if (high) {
@@ -239,11 +270,13 @@ void pwmout_init(pwmout_t *obj, PinName pin)
239270
pwmout_write (obj, 0);
240271
}
241272

242-
void pwmout_free(pwmout_t *obj)
243-
{
273+
void pwmout_free(pwmout_t* obj) {
244274
MBED_ASSERT(obj->pwm != (PWMName)NC);
245-
PWM_taken[obj->pwm] = 0;
246275
pwmout_write(obj, 0);
276+
PWM_taken[obj->pwm] = 0;
277+
timer_free();
278+
ppi_free(obj->pwm);
279+
gpiote_free(obj->pin,obj->pwm);
247280
}
248281

249282
void pwmout_write(pwmout_t *obj, float value)

0 commit comments

Comments
 (0)