Fork of Erik Olieman's FastPWM library created to add the targets I need.

Fork of FastPWM by Erik -

Revision:
4:a7b9f778c4b4
Parent:
3:3094d3806cfc
Child:
6:0f57969697b6
--- a/FastPWM.h	Wed Jul 25 07:14:39 2012 +0000
+++ b/FastPWM.h	Tue Aug 13 16:54:06 2013 +0000
@@ -17,14 +17,9 @@
 
 #include "mbed.h"
 
-
 #ifndef FASTPWM_H
 #define FASTPWM_H
 
-#ifndef F_CLK
-#define F_CLK   96000000
-#endif
-
 /** Library that allows faster and/or higher resolution PWM output
   *
   * Library can directly replace standard mbed PWM library. Only limitation is that the maximum PWM period is four times shorter
@@ -33,19 +28,16 @@
   *
   * Contrary to the default mbed library, this library takes doubles instead of floats. The compiler will autocast if needed,
   * but do take into account it is done for a reason, your accuracy will otherwise be limitted by the floating point precision.
-  *
-  * In your program you can define F_CLK if you use a different clock frequency than the default one. 
-  *
-  * Only works on LPC1768 for now. If you want support for the other one, send a PM and I will have a look, but I cannot even compile for it.
   */
-class FastPWM {
+class FastPWM : public PwmOut {
 public:
     /**
     * Create a FastPWM object connected to the specified pin
     *
     * @param pin - PWM pin to connect to
+    * @param prescaler - Clock prescaler, -1 is dynamic (default), 0 is bit random, everything else normal
     */
-    FastPWM(PinName pin);
+    FastPWM(PinName pin, int prescaler = -1);
     
     /**
     * Set the PWM period, specified in seconds (double), keeping the duty cycle the same.
@@ -68,6 +60,14 @@
     void period_us(double us);
     
     /**
+    * Set the PWM period, specified in clock ticks, keeping _pulse width_ the same.
+    *
+    * This function can be used if low overhead is required. Do take into account the result is
+    * board (clock frequency) dependent, and this does not keep an equal duty cycle!
+    */
+    void period_ticks(uint32_t ticks);
+    
+    /**
     * Set the PWM pulsewidth, specified in seconds (double), keeping the period the same.
     */
     void pulsewidth(double seconds);
@@ -88,6 +88,14 @@
     void pulsewidth_us(double us);
     
     /**
+    * Set the PWM period, specified in clock ticks, keeping the period the same.
+    *
+    * This function can be used if low overhead is required. Do take into account the result is
+    * board (clock frequency) dependent!
+    */
+    void pulsewidth_ticks(uint32_t ticks);
+    
+    /**
     * Set the ouput duty-cycle, specified as a percentage (double)
     *
     * @param duty - A double value representing the output duty-cycle, specified as a percentage.  The value should lie between 0.0 (representing on 0%) and 1.0 (representing on 100%).
@@ -111,13 +119,33 @@
     */
     operator double();
     
+    /**
+    * Set the PWM prescaler
+    *
+    * The period of all PWM pins on the same PWM unit have to be reset after using this!
+    *
+    * @param value - The required prescaler. Special values: 0 = lock current prescaler, -1 = use dynamic prescaler
+    * @param return - The prescaler which was set (can differ from requested prescaler if not possible)
+    */
+    int prescaler(int value);
+    
 private:
-    PwmOut PWMObject;
+    void initFastPWM(void);
+    
+    uint32_t setPrescaler( uint32_t reqScale );
+    int calcPrescaler(uint64_t clocks);
+    uint32_t getPeriod( void );
+    
+    void updateTicks( uint32_t prescaler );
+    uint32_t bits;
+    
     double _duty;
-    double _period;
-    unsigned int PWMUnit;
     
-    __IO uint32_t *MR;
+    double dticks, dticks_us;
+    int iticks_ms, iticks_us;
+    
+    bool dynamicPrescaler;
+    
 
 };
 #endif
\ No newline at end of file