mbed library sources. Supersedes mbed-src.

Fork of mbed-dev by mbed official

Files at this revision

API Documentation at this revision

Comitter:
mbed_official
Date:
Sat Jan 30 17:00:10 2016 +0000
Parent:
56:05912f50f004
Child:
58:b0f5d49fdbcf
Commit message:
Synchronized with git revision 01e730cfb2fe9d1404668ad1b4d9a2877b3abdda

Full URL: https://github.com/mbedmicro/mbed/commit/01e730cfb2fe9d1404668ad1b4d9a2877b3abdda/

[LPC11U68, LPC1549] Fixed PwmOut SCT Bugs

Changed in this revision

targets/hal/TARGET_NXP/TARGET_LPC11U6X/pwmout_api.c Show annotated file Show diff for this revision Revisions of this file
targets/hal/TARGET_NXP/TARGET_LPC15XX/pwmout_api.c Show annotated file Show diff for this revision Revisions of this file
--- a/targets/hal/TARGET_NXP/TARGET_LPC11U6X/pwmout_api.c	Fri Jan 29 14:15:09 2016 +0000
+++ b/targets/hal/TARGET_NXP/TARGET_LPC11U6X/pwmout_api.c	Sat Jan 30 17:00:10 2016 +0000
@@ -83,10 +83,6 @@
     // halt and clear the counter
     pwm->CTRL |= (1 << 2) | (1 << 3);
     
-    // System Clock -> us_ticker (1)MHz
-    pwm->CTRL &= ~(0x7F << 5);
-    pwm->CTRL |= (((SystemCoreClock/1000000 - 1) & 0x7F) << 5);
-    
     switch(pwm_mapped) {
         case SCT0_0:
         case SCT1_0:
@@ -117,14 +113,6 @@
     // Event 1 : MATCH and MATCHSEL=1
     pwm->EV1_CTRL  = (1 << 12) | (1 << 0);
     pwm->EV1_STATE = 0xFFFFFFFF;
-    
-    // Match reload register
-    pwm->MATCHREL0 = 20000; // 20ms
-    pwm->MATCHREL1 = (pwm->MATCHREL0 / 4); // 50% duty
-    
-    // unhalt the counter:
-    //    - clearing bit 2 of the CTRL register
-    pwm->CTRL &= ~(1 << 2);
 
     // default to 20ms: standard for servos, and fine for e.g. brightness control
     pwmout_period_ms(obj, 20);
@@ -140,18 +128,25 @@
 }
 
 void pwmout_write(pwmout_t* obj, float value) {
+    LPC_SCT0_Type* pwm = obj->pwm;
     if (value < 0.0f) {
         value = 0.0;
     } else if (value > 1.0f) {
         value = 1.0;
     }
-    uint32_t t_on = (uint32_t)((float)(obj->pwm->MATCHREL0) * value);
-    obj->pwm->MATCHREL1 = t_on;
+    uint32_t t_on = (uint32_t)((float)(pwm->MATCHREL0 + 1) * value);
+    if (t_on > 0) {
+        pwm->MATCHREL1 = t_on - 1;
+        pwm->CTRL &= ~(1 << 2);
+    } else {
+        pwm->CTRL |= (1 << 2) | (1 << 3);
+        pwm->OUTPUT = 0x00000000;
+    }
 }
 
 float pwmout_read(pwmout_t* obj) {
-    uint32_t t_off = obj->pwm->MATCHREL0;
-    uint32_t t_on  = obj->pwm->MATCHREL1;
+    uint32_t t_off = obj->pwm->MATCHREL0 + 1;
+    uint32_t t_on  = obj->pwm->MATCHREL1 + 1;
     float v = (float)t_on/(float)t_off;
     return (v > 1.0f) ? (1.0f) : (v);
 }
@@ -166,11 +161,20 @@
 
 // Set the PWM period, keeping the duty cycle the same.
 void pwmout_period_us(pwmout_t* obj, int us) {
-    uint32_t t_off = obj->pwm->MATCHREL0;
-    uint32_t t_on  = obj->pwm->MATCHREL1;
+    LPC_SCT0_Type* pwm = obj->pwm;
+    uint32_t t_off = pwm->MATCHREL0 + 1;
+    uint32_t t_on  = pwm->MATCHREL1 + 1;
     float v = (float)t_on/(float)t_off;
-    obj->pwm->MATCHREL0 = (uint32_t)us;
-    obj->pwm->MATCHREL1 = (uint32_t)((float)us * (float)v);
+    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);
+    } else {
+        pwm->CTRL |= (1 << 2) | (1 << 3);
+        pwm->OUTPUT = 0x00000000;
+    }
 }
 
 void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
@@ -182,7 +186,14 @@
 }
 
 void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
-    obj->pwm->MATCHREL1 = (uint32_t)us;
+    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);
+    } else {
+        pwm->CTRL |= (1 << 2) | (1 << 3);
+        pwm->OUTPUT = 0x00000000;
+    }
 }
 
 #endif
--- a/targets/hal/TARGET_NXP/TARGET_LPC15XX/pwmout_api.c	Fri Jan 29 14:15:09 2016 +0000
+++ b/targets/hal/TARGET_NXP/TARGET_LPC15XX/pwmout_api.c	Sat Jan 30 17:00:10 2016 +0000
@@ -89,14 +89,6 @@
     // halt and clear the counter
     pwm->CTRL |= (1 << 2) | (1 << 3);
     
-    // System Clock -> us_ticker (1)MHz
-    pwm->CTRL &= ~(0x7F << 5);
-    pwm->CTRL |= (((SystemCoreClock/1000000 - 1) & 0x7F) << 5);
-    
-    // Match reload register
-    pwm->MATCHREL0 = 20000; // 20ms
-    pwm->MATCHREL1 = (pwm->MATCHREL0 / 4); // 50% duty
-    
     pwm->OUT0_SET = (1 << 0); // event 0
     pwm->OUT0_CLR = (1 << 1); // event 1
 
@@ -105,10 +97,6 @@
     pwm->EV1_CTRL  = (1 << 12) | (1 << 0);
     pwm->EV1_STATE = 0xFFFFFFFF;
 
-    // unhalt the counter:
-    //    - clearing bit 2 of the CTRL register
-    pwm->CTRL &= ~(1 << 2);
-
     // default to 20ms: standard for servos, and fine for e.g. brightness control
     pwmout_period_ms(obj, 20);
     pwmout_write    (obj, 0);
@@ -127,13 +115,19 @@
     } else if (value > 1.0f) {
         value = 1.0;
     }
-    uint32_t t_on = (uint32_t)((float)(pwm->MATCHREL0) * value);
-    pwm->MATCHREL1 = t_on;
+    uint32_t t_on = (uint32_t)((float)(pwm->MATCHREL0 + 1) * value);
+    if (t_on > 0) {
+        pwm->MATCHREL1 = t_on - 1;
+        pwm->CTRL &= ~(1 << 2);
+    } else {
+        pwm->CTRL |= (1 << 2) | (1 << 3);
+        pwm->OUTPUT = 0x00000000;
+    }
 }
 
 float pwmout_read(pwmout_t* obj) {
-    uint32_t t_off = obj->pwm->MATCHREL0;
-    uint32_t t_on  = obj->pwm->MATCHREL1;
+    uint32_t t_off = obj->pwm->MATCHREL0 + 1;
+    uint32_t t_on  = obj->pwm->MATCHREL1 + 1;
     float v = (float)t_on/(float)t_off;
     return (v > 1.0f) ? (1.0f) : (v);
 }
@@ -149,11 +143,19 @@
 // Set the PWM period, keeping the duty cycle the same.
 void pwmout_period_us(pwmout_t* obj, int us) {
     LPC_SCT0_Type* pwm = obj->pwm;
-    uint32_t t_off = pwm->MATCHREL0;
-    uint32_t t_on  = pwm->MATCHREL1;
+    uint32_t t_off = pwm->MATCHREL0 + 1;
+    uint32_t t_on  = pwm->MATCHREL1 + 1;
     float v = (float)t_on/(float)t_off;
-    pwm->MATCHREL0 = (uint32_t)us;
-    pwm->MATCHREL1 = (uint32_t)((float)us * (float)v);
+    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);
+    } else {
+        pwm->CTRL |= (1 << 2) | (1 << 3);
+        pwm->OUTPUT = 0x00000000;
+    }
 }
 
 void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
@@ -165,6 +167,13 @@
 }
 
 void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
-    obj->pwm->MATCHREL1 = (uint32_t)us;
+    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);
+    } else {
+        pwm->CTRL |= (1 << 2) | (1 << 3);
+        pwm->OUTPUT = 0x00000000;
+    }
 }