mbed library sources. Supersedes mbed-src.
Dependents: Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more
Diff: targets/hal/TARGET_NXP/TARGET_LPC15XX/pwmout_api.c
- Revision:
- 82:98895dd43cc3
- Parent:
- 57:791e51e3acc9
- Child:
- 144:ef7eb2e8f9f7
--- a/targets/hal/TARGET_NXP/TARGET_LPC15XX/pwmout_api.c Fri Mar 04 11:30:11 2016 +0000 +++ b/targets/hal/TARGET_NXP/TARGET_LPC15XX/pwmout_api.c Sat Mar 05 06:00:11 2016 +0000 @@ -118,16 +118,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); } @@ -144,17 +151,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; } } @@ -170,8 +187,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; }