raspiezo / mbed-dev

Dependents:   Nucleo_L432KC_Quadrature_Decoder_with_ADC_and_DAC

Fork of mbed-dev by mbed official

Revision:
57:791e51e3acc9
Parent:
0:9b334a45a8ff
Child:
82:98895dd43cc3
--- 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