Skip to content

Commit abf43a3

Browse files
committed
Merge pull request #1587 from neilt6/master
[LPC11U68, LPC1549] Fixed PwmOut SCT Bugs
2 parents 1020d7c + b3e2763 commit abf43a3

File tree

2 files changed

+58
-12
lines changed

2 files changed

+58
-12
lines changed

libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11U6X/pwmout_api.c

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,16 +137,23 @@ void pwmout_write(pwmout_t* obj, float value) {
137137
uint32_t t_on = (uint32_t)((float)(pwm->MATCHREL0 + 1) * value);
138138
if (t_on > 0) {
139139
pwm->MATCHREL1 = t_on - 1;
140-
pwm->CTRL &= ~(1 << 2);
140+
141+
// Un-halt the timer and ensure the new pulse-width takes immediate effect if necessary
142+
if (pwm->CTRL & (1 << 2)) {
143+
pwm->MATCH1 = pwm->MATCHREL1;
144+
pwm->CTRL &= ~(1 << 2);
145+
}
141146
} else {
147+
// Halt the timer and force the output low
142148
pwm->CTRL |= (1 << 2) | (1 << 3);
143149
pwm->OUTPUT = 0x00000000;
144150
}
145151
}
146152

147153
float pwmout_read(pwmout_t* obj) {
148-
uint32_t t_off = obj->pwm->MATCHREL0 + 1;
149-
uint32_t t_on = obj->pwm->MATCHREL1 + 1;
154+
LPC_SCT0_Type* pwm = obj->pwm;
155+
uint32_t t_off = pwm->MATCHREL0 + 1;
156+
uint32_t t_on = (!(pwm->CTRL & (1 << 2))) ? pwm->MATCHREL1 + 1 : 0;
150157
float v = (float)t_on/(float)t_off;
151158
return (v > 1.0f) ? (1.0f) : (v);
152159
}
@@ -163,17 +170,27 @@ void pwmout_period_ms(pwmout_t* obj, int ms) {
163170
void pwmout_period_us(pwmout_t* obj, int us) {
164171
LPC_SCT0_Type* pwm = obj->pwm;
165172
uint32_t t_off = pwm->MATCHREL0 + 1;
166-
uint32_t t_on = pwm->MATCHREL1 + 1;
173+
uint32_t t_on = (!(pwm->CTRL & (1 << 2))) ? pwm->MATCHREL1 + 1 : 0;
167174
float v = (float)t_on/(float)t_off;
168175
uint32_t period_ticks = (uint32_t)(((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000);
169176
uint32_t pulsewidth_ticks = period_ticks * v;
170177
pwm->MATCHREL0 = period_ticks - 1;
171178
if (pulsewidth_ticks > 0) {
172179
pwm->MATCHREL1 = pulsewidth_ticks - 1;
173-
pwm->CTRL &= ~(1 << 2);
180+
181+
// Un-halt the timer and ensure the new period & pulse-width take immediate effect if necessary
182+
if (pwm->CTRL & (1 << 2)) {
183+
pwm->MATCH0 = pwm->MATCHREL0;
184+
pwm->MATCH1 = pwm->MATCHREL1;
185+
pwm->CTRL &= ~(1 << 2);
186+
}
174187
} else {
188+
// Halt the timer and force the output low
175189
pwm->CTRL |= (1 << 2) | (1 << 3);
176190
pwm->OUTPUT = 0x00000000;
191+
192+
// Ensure the new period will take immediate effect when the timer is un-halted
193+
pwm->MATCH0 = pwm->MATCHREL0;
177194
}
178195
}
179196

@@ -189,8 +206,14 @@ void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
189206
LPC_SCT0_Type* pwm = obj->pwm;
190207
if (us > 0) {
191208
pwm->MATCHREL1 = (uint32_t)(((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000) - 1;
192-
pwm->CTRL &= ~(1 << 2);
209+
210+
// Un-halt the timer and ensure the new pulse-width takes immediate effect if necessary
211+
if (pwm->CTRL & (1 << 2)) {
212+
pwm->MATCH1 = pwm->MATCHREL1;
213+
pwm->CTRL &= ~(1 << 2);
214+
}
193215
} else {
216+
// Halt the timer and force the output low
194217
pwm->CTRL |= (1 << 2) | (1 << 3);
195218
pwm->OUTPUT = 0x00000000;
196219
}

libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC15XX/pwmout_api.c

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,23 @@ void pwmout_write(pwmout_t* obj, float value) {
118118
uint32_t t_on = (uint32_t)((float)(pwm->MATCHREL0 + 1) * value);
119119
if (t_on > 0) {
120120
pwm->MATCHREL1 = t_on - 1;
121-
pwm->CTRL &= ~(1 << 2);
121+
122+
// Un-halt the timer and ensure the new pulse-width takes immediate effect if necessary
123+
if (pwm->CTRL & (1 << 2)) {
124+
pwm->MATCH1 = pwm->MATCHREL1;
125+
pwm->CTRL &= ~(1 << 2);
126+
}
122127
} else {
128+
// Halt the timer and force the output low
123129
pwm->CTRL |= (1 << 2) | (1 << 3);
124130
pwm->OUTPUT = 0x00000000;
125131
}
126132
}
127133

128134
float pwmout_read(pwmout_t* obj) {
129-
uint32_t t_off = obj->pwm->MATCHREL0 + 1;
130-
uint32_t t_on = obj->pwm->MATCHREL1 + 1;
135+
LPC_SCT0_Type* pwm = obj->pwm;
136+
uint32_t t_off = pwm->MATCHREL0 + 1;
137+
uint32_t t_on = (!(pwm->CTRL & (1 << 2))) ? pwm->MATCHREL1 + 1 : 0;
131138
float v = (float)t_on/(float)t_off;
132139
return (v > 1.0f) ? (1.0f) : (v);
133140
}
@@ -144,17 +151,27 @@ void pwmout_period_ms(pwmout_t* obj, int ms) {
144151
void pwmout_period_us(pwmout_t* obj, int us) {
145152
LPC_SCT0_Type* pwm = obj->pwm;
146153
uint32_t t_off = pwm->MATCHREL0 + 1;
147-
uint32_t t_on = pwm->MATCHREL1 + 1;
154+
uint32_t t_on = (!(pwm->CTRL & (1 << 2))) ? pwm->MATCHREL1 + 1 : 0;
148155
float v = (float)t_on/(float)t_off;
149156
uint32_t period_ticks = (uint32_t)(((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000);
150157
uint32_t pulsewidth_ticks = period_ticks * v;
151158
pwm->MATCHREL0 = period_ticks - 1;
152159
if (pulsewidth_ticks > 0) {
153160
pwm->MATCHREL1 = pulsewidth_ticks - 1;
154-
pwm->CTRL &= ~(1 << 2);
161+
162+
// Un-halt the timer and ensure the new period & pulse-width take immediate effect if necessary
163+
if (pwm->CTRL & (1 << 2)) {
164+
pwm->MATCH0 = pwm->MATCHREL0;
165+
pwm->MATCH1 = pwm->MATCHREL1;
166+
pwm->CTRL &= ~(1 << 2);
167+
}
155168
} else {
169+
// Halt the timer and force the output low
156170
pwm->CTRL |= (1 << 2) | (1 << 3);
157171
pwm->OUTPUT = 0x00000000;
172+
173+
// Ensure the new period will take immediate effect when the timer is un-halted
174+
pwm->MATCH0 = pwm->MATCHREL0;
158175
}
159176
}
160177

@@ -170,8 +187,14 @@ void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
170187
LPC_SCT0_Type* pwm = obj->pwm;
171188
if (us > 0) {
172189
pwm->MATCHREL1 = (uint32_t)(((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000) - 1;
173-
pwm->CTRL &= ~(1 << 2);
190+
191+
// Un-halt the timer and ensure the new pulse-width takes immediate effect if necessary
192+
if (pwm->CTRL & (1 << 2)) {
193+
pwm->MATCH1 = pwm->MATCHREL1;
194+
pwm->CTRL &= ~(1 << 2);
195+
}
174196
} else {
197+
// Halt the timer and force the output low
175198
pwm->CTRL |= (1 << 2) | (1 << 3);
176199
pwm->OUTPUT = 0x00000000;
177200
}

0 commit comments

Comments
 (0)