added prescaler for 16 bit pwm in LPC1347 target

Fork of mbed-dev by mbed official

Revision:
50:a417edff4437
Parent:
0:9b334a45a8ff
Child:
144:ef7eb2e8f9f7
diff -r 57ac6e3cdfd3 -r a417edff4437 targets/hal/TARGET_Silicon_Labs/TARGET_EFM32/pwmout_api.c
--- a/targets/hal/TARGET_Silicon_Labs/TARGET_EFM32/pwmout_api.c	Wed Jan 13 12:45:11 2016 +0000
+++ b/targets/hal/TARGET_Silicon_Labs/TARGET_EFM32/pwmout_api.c	Fri Jan 15 07:45:16 2016 +0000
@@ -46,11 +46,28 @@
 
 static int pwm_prescaler_div;
 
-uint32_t pwmout_get_channel_route(pwmout_t *obj)
+float   pwmout_calculate_duty(uint32_t width_cycles, uint32_t period_cycles);
+void    pwmout_write_channel(uint32_t channel, float value);
+
+uint32_t pwmout_get_channel_route(uint32_t channel)
 {
-    MBED_ASSERT(obj->channel != (PWMName) NC);
+    MBED_ASSERT(channel != (PWMName) NC);
 
-    switch (obj->channel) {
+    switch (channel) {
+#ifdef TIMER_ROUTEPEN_CC0PEN
+        case PWM_CH0:
+            return TIMER_ROUTEPEN_CC0PEN;
+            break;
+        case PWM_CH1:
+            return TIMER_ROUTEPEN_CC1PEN;
+            break;
+        case PWM_CH2:
+            return TIMER_ROUTEPEN_CC2PEN;
+            break;
+        case PWM_CH3:
+            return TIMER_ROUTEPEN_CC3PEN;
+            break;
+#else
         case PWM_CH0:
             return TIMER_ROUTE_CC0PEN;
             break;
@@ -60,11 +77,75 @@
         case PWM_CH2:
             return TIMER_ROUTE_CC2PEN;
             break;
+#endif
         default:
             return 0;
     }
 }
 
+/*
+* Disables the route location given. Returns true if it was enabled, false if it wasn't.
+*/
+bool pwmout_disable_channel_route(uint32_t routeloc) {
+#ifdef TIMER_ROUTEPEN_CC0PEN
+    if(PWM_TIMER->ROUTEPEN & routeloc) {
+        //This channel was in use, so disable
+        PWM_TIMER->ROUTEPEN &= ~routeloc;
+        return true;
+    }
+#else
+    if(PWM_TIMER->ROUTE & routeloc) {
+        //This channel was in use, so disable
+        PWM_TIMER->ROUTE &= ~routeloc;
+        return true;
+    }
+#endif
+    return false;
+}
+
+/*
+* Check if a channel is active
+*/
+bool pwmout_channel_route_active(uint32_t routeloc) {
+#ifdef TIMER_ROUTEPEN_CC0PEN
+    if(PWM_TIMER->ROUTEPEN & routeloc) {
+        return true;
+    }
+#else
+    if(PWM_TIMER->ROUTE & routeloc) {
+        return true;
+    }
+#endif
+    return false;
+}
+
+/*
+* Set the given route PEN flag
+*/
+void pwmout_set_channel_route(uint32_t routeloc) {
+#ifdef TIMER_ROUTEPEN_CC0PEN
+    PWM_TIMER->ROUTEPEN |= routeloc;
+#else
+    PWM_TIMER->ROUTE    |= routeloc;
+#endif
+}
+
+/*
+* Check if all routes are disabled
+*/
+bool pwmout_all_inactive(void) {
+#ifdef TIMER_ROUTEPEN_CC0PEN
+    if(PWM_TIMER->ROUTEPEN == _TIMER_ROUTEPEN_RESETVALUE) {
+        return true;
+    }
+#else
+    if(PWM_TIMER->ROUTE == PWM_ROUTE) {
+        return true;
+    }
+#endif
+    return false;
+}
+
 void pwmout_enable_pins(pwmout_t *obj, uint8_t enable)
 {
     if (enable) {
@@ -75,17 +156,14 @@
     }
 }
 
-void pwmout_enable(pwmout_t *obj, uint8_t enable)
-{
-    /* Start with default CC (Compare/Capture) channel parameters */
-    TIMER_InitCC_TypeDef timerCCInit = TIMER_INITCC_DEFAULT;
+void pwmout_enable(pwmout_t *obj, uint8_t enable){
     if (enable) {
-        /* Set mode to PWM */
-        timerCCInit.mode = timerCCModePWM;
+        // Set mode to PWM
+        PWM_TIMER->CC[obj->channel].CTRL = TIMER_CC_CTRL_MODE_PWM;
+    } else {
+        // Set mode to default (== disabled)
+        PWM_TIMER->CC[obj->channel].CTRL = _TIMER_CC_CTRL_MODE_DEFAULT;
     }
-
-    /* Configure CC channel */
-    TIMER_InitCC(PWM_TIMER, obj->channel, &timerCCInit);
 }
 
 void pwmout_init(pwmout_t *obj, PinName pin)
@@ -103,62 +181,107 @@
         TIMER_Init(PWM_TIMER, &timerInit);
     }
 
-    /* Enable correct channel */
-    uint32_t routeloc = pwmout_get_channel_route(obj);
-    if(PWM_TIMER->ROUTE & routeloc) {
+    // Set route enable
+    if(pwmout_channel_route_active(pwmout_get_channel_route(obj->channel))) {
         //This channel was already in use
-        //TODO: gracefully handle this case
+        //TODO: gracefully handle this case. mbed_error?
+        return;
     } else {
-        //This channel was unused up to now
-        PWM_TIMER->ROUTE |= routeloc;
+        pwmout_set_channel_route(pwmout_get_channel_route(obj->channel));
         blockSleepMode(EM1);
-
-        //TODO: check if any channel was up already, then don't re-init timer
         pwmout_enable(obj, true);
         pwmout_enable_pins(obj, true);
     }
 
-    /* Route correct channel to location 1 */
+    // Set route location
+#ifdef _TIMER_ROUTELOC0_CC0LOC_LOC0
+    switch (obj->channel) {
+        case PWM_CH0:
+            PWM_TIMER->ROUTELOC0 &= ~_TIMER_ROUTELOC0_CC0LOC_MASK;
+            PWM_TIMER->ROUTELOC0 |= pinmap_find_function(pin,PinMap_PWM) << _TIMER_ROUTELOC0_CC0LOC_SHIFT;
+            break;
+        case PWM_CH1:
+            PWM_TIMER->ROUTELOC0 &= ~_TIMER_ROUTELOC0_CC1LOC_MASK;
+            PWM_TIMER->ROUTELOC0 |= pinmap_find_function(pin,PinMap_PWM)<< _TIMER_ROUTELOC0_CC1LOC_SHIFT;
+            break;
+        case PWM_CH2:
+            PWM_TIMER->ROUTELOC0 &= ~_TIMER_ROUTELOC0_CC2LOC_MASK;
+            PWM_TIMER->ROUTELOC0 |= pinmap_find_function(pin,PinMap_PWM) << _TIMER_ROUTELOC0_CC2LOC_SHIFT;
+            break;
+        case PWM_CH3:
+            PWM_TIMER->ROUTELOC0 &= ~_TIMER_ROUTELOC0_CC3LOC_MASK;
+            PWM_TIMER->ROUTELOC0 |= pinmap_find_function(pin,PinMap_PWM) << _TIMER_ROUTELOC0_CC3LOC_SHIFT;
+            break;
+        default:
+            MBED_ASSERT(false);
+    }
+#else
+    // On P1, the route location is statically defined for the entire timer.
     PWM_TIMER->ROUTE &= ~_TIMER_ROUTE_LOCATION_MASK;
     PWM_TIMER->ROUTE |= PWM_ROUTE;
+#endif
 
-    /* Set default 20ms frequency and 0ms pulse width */
+    // Set default 20ms frequency and 0ms pulse width
     pwmout_period(obj, 0.02);
 }
 
 void pwmout_free(pwmout_t *obj)
 {
-    uint32_t routeloc = pwmout_get_channel_route(obj);
-    if(PWM_TIMER->ROUTE & routeloc) {
-        //This channel was in use, so disable
-        PWM_TIMER->ROUTE &= ~routeloc;
-        pwmout_enable_pins(obj, false);
+    if(pwmout_disable_channel_route(pwmout_get_channel_route(obj->channel))) {
+        //Channel was previously enabled, so do housekeeping
         unblockSleepMode(EM1);
-
-        //TODO: check if all channels are down, then switch off timer
     } else {
         //This channel was disabled already
     }
+    
+    pwmout_enable_pins(obj, false);
+    
+    if(pwmout_all_inactive()) {
+        //Stop timer
+        PWM_TIMER->CMD = TIMER_CMD_STOP;
+        while(PWM_TIMER->STATUS & TIMER_STATUS_RUNNING);
+        
+        //Disable clock
+        CMU_ClockEnable(PWM_TIMER_CLOCK, false);
+    }
 }
 
 void pwmout_write(pwmout_t *obj, float value)
 {
+    pwmout_write_channel(obj->channel, value);
+}
+
+void pwmout_write_channel(uint32_t channel, float value) {
+    uint32_t width_cycles = 0;
     if (value < 0.0f) {
-        value = 0;
-    } else if (value > 1.0f) {
-        value = 1;
+        width_cycles = 0;
+    } else if (value >= 1.0f) {
+        width_cycles = PWM_TIMER->TOPB + 1;
+    } else {
+       width_cycles = (uint16_t)((float)PWM_TIMER->TOPB * value);
     }
 
-    float pulse_period_in_s = obj->period_cycles / ((float) (REFERENCE_FREQUENCY >> pwm_prescaler_div));
-    pwmout_pulsewidth(obj, value * pulse_period_in_s);
+    TIMER_CompareBufSet(PWM_TIMER, channel, width_cycles);
 }
 
 float pwmout_read(pwmout_t *obj)
 {
-    return obj->width_cycles / (float) obj->period_cycles;
+    return pwmout_calculate_duty(TIMER_CaptureGet(PWM_TIMER, obj->channel), TIMER_TopGet(PWM_TIMER));
 }
 
-// Set the PWM period, keeping the absolute pulse width the same.
+float pwmout_calculate_duty(uint32_t width_cycles, uint32_t period_cycles) {
+    if(width_cycles > period_cycles) {
+        return 1.0f;
+    }
+    else if (width_cycles == 0) {
+        return 0.0f;
+    }
+    else {
+        return (float) width_cycles / (float) period_cycles;
+    }
+}
+
+// Set the PWM period, keeping the duty cycle the same.
 void pwmout_period(pwmout_t *obj, float seconds)
 {
     // Find the lowest prescaler divider possible.
@@ -181,13 +304,27 @@
         }
     }
 
-    obj->period_cycles = cycles;
+    //Check if anything changed
+    if(((PWM_TIMER->CTRL & ~_TIMER_CTRL_PRESC_MASK) == (pwm_prescaler_div << _TIMER_CTRL_PRESC_SHIFT)) && (TIMER_TopGet(PWM_TIMER) == cycles)) return;
+
+    //Save previous period for recalculation of duty cycles
+    uint32_t previous_period_cycles = PWM_TIMER->TOPB;
 
     //Set prescaler
     PWM_TIMER->CTRL = (PWM_TIMER->CTRL & ~_TIMER_CTRL_PRESC_MASK) | (pwm_prescaler_div << _TIMER_CTRL_PRESC_SHIFT);
 
-    /* Set Top Value, which controls the PWM period */
-    TIMER_TopSet(PWM_TIMER, obj->period_cycles);
+    //Set Top Value, which controls the PWM period
+    TIMER_TopBufSet(PWM_TIMER, cycles);
+
+    //For each active channel, re-calculate the compare value
+    uint32_t channel = 0;
+    while(pwmout_get_channel_route(channel) != 0) {
+        if(pwmout_channel_route_active(channel)) {
+            //recalc and reset compare value
+            pwmout_write_channel(channel, pwmout_calculate_duty(PWM_TIMER->CC[channel].CCVB, previous_period_cycles));
+        }
+        channel++;
+    }
 }
 
 void pwmout_period_ms(pwmout_t *obj, int ms)
@@ -202,20 +339,20 @@
 
 void pwmout_pulsewidth(pwmout_t *obj, float seconds)
 {
-    obj->width_cycles = (uint32_t) (((float) (REFERENCE_FREQUENCY >> pwm_prescaler_div)) * seconds);
-    TIMER_CompareBufSet(PWM_TIMER, obj->channel, obj->width_cycles);
+    uint16_t width_cycles = (uint16_t) (((float) (REFERENCE_FREQUENCY >> pwm_prescaler_div)) * seconds);
+    TIMER_CompareBufSet(PWM_TIMER, obj->channel, width_cycles);
 }
 
 void pwmout_pulsewidth_ms(pwmout_t *obj, int ms)
 {
-    obj->width_cycles = (uint32_t) ((REFERENCE_FREQUENCY >> pwm_prescaler_div) * ms) / 1000;
-    TIMER_CompareBufSet(PWM_TIMER, obj->channel, obj->width_cycles);
+    uint16_t width_cycles = (uint16_t) ((REFERENCE_FREQUENCY >> pwm_prescaler_div) * ms) / 1000;
+    TIMER_CompareBufSet(PWM_TIMER, obj->channel, width_cycles);
 }
 
 void pwmout_pulsewidth_us(pwmout_t *obj, int us)
 {
-    obj->width_cycles = (uint32_t) ((REFERENCE_FREQUENCY >> pwm_prescaler_div) * us) / 1000000;
-    TIMER_CompareBufSet(PWM_TIMER, obj->channel, obj->width_cycles);
+    uint16_t width_cycles = (uint16_t) ((REFERENCE_FREQUENCY >> pwm_prescaler_div) * us) / 1000000;
+    TIMER_CompareBufSet(PWM_TIMER, obj->channel, width_cycles);
 }
 
 #endif