Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-dev by
Diff: targets/hal/TARGET_NXP/TARGET_LPC11U6X/pwmout_api.c
- Revision:
- 82:98895dd43cc3
- Parent:
- 57:791e51e3acc9
- Child:
- 144:ef7eb2e8f9f7
--- a/targets/hal/TARGET_NXP/TARGET_LPC11U6X/pwmout_api.c Fri Mar 04 11:30:11 2016 +0000
+++ b/targets/hal/TARGET_NXP/TARGET_LPC11U6X/pwmout_api.c Sat Mar 05 06:00:11 2016 +0000
@@ -137,16 +137,23 @@
uint32_t t_on = (uint32_t)((float)(pwm->MATCHREL0 + 1) * value);
if (t_on > 0) {
pwm->MATCHREL1 = t_on - 1;
- pwm->CTRL &= ~(1 << 2);
+
+ // Un-halt the timer and ensure the new pulse-width takes immediate effect if necessary
+ if (pwm->CTRL & (1 << 2)) {
+ pwm->MATCH1 = pwm->MATCHREL1;
+ pwm->CTRL &= ~(1 << 2);
+ }
} else {
+ // Halt the timer and force the output low
pwm->CTRL |= (1 << 2) | (1 << 3);
pwm->OUTPUT = 0x00000000;
}
}
float pwmout_read(pwmout_t* obj) {
- uint32_t t_off = obj->pwm->MATCHREL0 + 1;
- uint32_t t_on = obj->pwm->MATCHREL1 + 1;
+ LPC_SCT0_Type* pwm = obj->pwm;
+ uint32_t t_off = pwm->MATCHREL0 + 1;
+ uint32_t t_on = (!(pwm->CTRL & (1 << 2))) ? pwm->MATCHREL1 + 1 : 0;
float v = (float)t_on/(float)t_off;
return (v > 1.0f) ? (1.0f) : (v);
}
@@ -163,17 +170,27 @@
void pwmout_period_us(pwmout_t* obj, int us) {
LPC_SCT0_Type* pwm = obj->pwm;
uint32_t t_off = pwm->MATCHREL0 + 1;
- uint32_t t_on = pwm->MATCHREL1 + 1;
+ uint32_t t_on = (!(pwm->CTRL & (1 << 2))) ? pwm->MATCHREL1 + 1 : 0;
float v = (float)t_on/(float)t_off;
uint32_t period_ticks = (uint32_t)(((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000);
uint32_t pulsewidth_ticks = period_ticks * v;
pwm->MATCHREL0 = period_ticks - 1;
if (pulsewidth_ticks > 0) {
pwm->MATCHREL1 = pulsewidth_ticks - 1;
- pwm->CTRL &= ~(1 << 2);
+
+ // Un-halt the timer and ensure the new period & pulse-width take immediate effect if necessary
+ if (pwm->CTRL & (1 << 2)) {
+ pwm->MATCH0 = pwm->MATCHREL0;
+ pwm->MATCH1 = pwm->MATCHREL1;
+ pwm->CTRL &= ~(1 << 2);
+ }
} else {
+ // Halt the timer and force the output low
pwm->CTRL |= (1 << 2) | (1 << 3);
pwm->OUTPUT = 0x00000000;
+
+ // Ensure the new period will take immediate effect when the timer is un-halted
+ pwm->MATCH0 = pwm->MATCHREL0;
}
}
@@ -189,8 +206,14 @@
LPC_SCT0_Type* pwm = obj->pwm;
if (us > 0) {
pwm->MATCHREL1 = (uint32_t)(((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000) - 1;
- pwm->CTRL &= ~(1 << 2);
+
+ // Un-halt the timer and ensure the new pulse-width takes immediate effect if necessary
+ if (pwm->CTRL & (1 << 2)) {
+ pwm->MATCH1 = pwm->MATCHREL1;
+ pwm->CTRL &= ~(1 << 2);
+ }
} else {
+ // Halt the timer and force the output low
pwm->CTRL |= (1 << 2) | (1 << 3);
pwm->OUTPUT = 0x00000000;
}
