b luo / mbed-dev

Fork of mbed-dev by mbed official

Revision:
57:791e51e3acc9
Parent:
0:9b334a45a8ff
Child:
82:98895dd43cc3
diff -r 05912f50f004 -r 791e51e3acc9 targets/hal/TARGET_NXP/TARGET_LPC15XX/pwmout_api.c
--- 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;
+    }
 }