inported from local
FastPWM.h@0:60857722c2ff, 2020-03-16 (annotated)
- Committer:
- NYX
- Date:
- Mon Mar 16 06:36:03 2020 +0000
- Revision:
- 0:60857722c2ff
initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
NYX | 0:60857722c2ff | 1 | /* |
NYX | 0:60857722c2ff | 2 | .---. _....._ |
NYX | 0:60857722c2ff | 3 | / p `\ .-""`: :`"-. |
NYX | 0:60857722c2ff | 4 | |__ - | ,' . ' ', |
NYX | 0:60857722c2ff | 5 | ._> \ /: : ; :, |
NYX | 0:60857722c2ff | 6 | '-. '\`. . : ' \ |
NYX | 0:60857722c2ff | 7 | `. | .'._.' '._.' '._.'. | |
NYX | 0:60857722c2ff | 8 | `;-\. : : ' '/,__, |
NYX | 0:60857722c2ff | 9 | .-'`'._ ' . : _.'.__.' |
NYX | 0:60857722c2ff | 10 | ((((-'/ `";--..:..--;"` \ |
NYX | 0:60857722c2ff | 11 | .' / \ \ |
NYX | 0:60857722c2ff | 12 | jgs ((((-' ((((-' |
NYX | 0:60857722c2ff | 13 | |
NYX | 0:60857722c2ff | 14 | Yeah ASCII art turtle more fun than copyright stuff |
NYX | 0:60857722c2ff | 15 | */ |
NYX | 0:60857722c2ff | 16 | |
NYX | 0:60857722c2ff | 17 | |
NYX | 0:60857722c2ff | 18 | #include "mbed.h" |
NYX | 0:60857722c2ff | 19 | |
NYX | 0:60857722c2ff | 20 | #ifndef FASTPWM_H |
NYX | 0:60857722c2ff | 21 | #define FASTPWM_H |
NYX | 0:60857722c2ff | 22 | |
NYX | 0:60857722c2ff | 23 | /** Library that allows faster and/or higher resolution PWM output |
NYX | 0:60857722c2ff | 24 | * |
NYX | 0:60857722c2ff | 25 | * Library can directly replace standard mbed PWM library. |
NYX | 0:60857722c2ff | 26 | * |
NYX | 0:60857722c2ff | 27 | * Contrary to the default mbed library, this library takes doubles instead of floats. The compiler will autocast if needed, |
NYX | 0:60857722c2ff | 28 | * but do take into account it is done for a reason, your accuracy will otherwise be limitted by the floating point precision. |
NYX | 0:60857722c2ff | 29 | */ |
NYX | 0:60857722c2ff | 30 | class FastPWM : public PwmOut { |
NYX | 0:60857722c2ff | 31 | public: |
NYX | 0:60857722c2ff | 32 | /** |
NYX | 0:60857722c2ff | 33 | * Create a FastPWM object connected to the specified pin |
NYX | 0:60857722c2ff | 34 | * |
NYX | 0:60857722c2ff | 35 | * @param pin - PWM pin to connect to |
NYX | 0:60857722c2ff | 36 | * @param prescaler - Clock prescaler, -1 is dynamic (default), 0 is bit random, everything else normal |
NYX | 0:60857722c2ff | 37 | */ |
NYX | 0:60857722c2ff | 38 | FastPWM(PinName pin, int prescaler = -1); |
NYX | 0:60857722c2ff | 39 | ~FastPWM(); |
NYX | 0:60857722c2ff | 40 | |
NYX | 0:60857722c2ff | 41 | /** |
NYX | 0:60857722c2ff | 42 | * Set the PWM period, specified in seconds (double), keeping the pulsewidth the same. |
NYX | 0:60857722c2ff | 43 | */ |
NYX | 0:60857722c2ff | 44 | void period(double seconds); |
NYX | 0:60857722c2ff | 45 | |
NYX | 0:60857722c2ff | 46 | /** |
NYX | 0:60857722c2ff | 47 | * Set the PWM period, specified in milli-seconds (int), keeping the pulsewidth the same. |
NYX | 0:60857722c2ff | 48 | */ |
NYX | 0:60857722c2ff | 49 | void period_ms(int ms); |
NYX | 0:60857722c2ff | 50 | |
NYX | 0:60857722c2ff | 51 | /** |
NYX | 0:60857722c2ff | 52 | * Set the PWM period, specified in micro-seconds (int), keeping the pulsewidth the same. |
NYX | 0:60857722c2ff | 53 | */ |
NYX | 0:60857722c2ff | 54 | void period_us(int us); |
NYX | 0:60857722c2ff | 55 | |
NYX | 0:60857722c2ff | 56 | /** |
NYX | 0:60857722c2ff | 57 | * Set the PWM period, specified in micro-seconds (double), keeping the pulsewidth the same. |
NYX | 0:60857722c2ff | 58 | */ |
NYX | 0:60857722c2ff | 59 | void period_us(double us); |
NYX | 0:60857722c2ff | 60 | |
NYX | 0:60857722c2ff | 61 | /** |
NYX | 0:60857722c2ff | 62 | * Set the PWM period, specified in clock ticks, keeping _pulse width_ the same. |
NYX | 0:60857722c2ff | 63 | * |
NYX | 0:60857722c2ff | 64 | * This function can be used if low overhead is required. Do take into account the result is |
NYX | 0:60857722c2ff | 65 | * board (clock frequency) dependent, and this does not keep an equal duty cycle! |
NYX | 0:60857722c2ff | 66 | */ |
NYX | 0:60857722c2ff | 67 | void period_ticks(uint32_t ticks); |
NYX | 0:60857722c2ff | 68 | |
NYX | 0:60857722c2ff | 69 | /** |
NYX | 0:60857722c2ff | 70 | * Set the PWM pulsewidth, specified in seconds (double), keeping the period the same. |
NYX | 0:60857722c2ff | 71 | */ |
NYX | 0:60857722c2ff | 72 | void pulsewidth(double seconds); |
NYX | 0:60857722c2ff | 73 | |
NYX | 0:60857722c2ff | 74 | /** |
NYX | 0:60857722c2ff | 75 | * Set the PWM pulsewidth, specified in milli-seconds (int), keeping the period the same. |
NYX | 0:60857722c2ff | 76 | */ |
NYX | 0:60857722c2ff | 77 | void pulsewidth_ms(int ms); |
NYX | 0:60857722c2ff | 78 | |
NYX | 0:60857722c2ff | 79 | /** |
NYX | 0:60857722c2ff | 80 | * Set the PWM pulsewidth, specified in micro-seconds (int), keeping the period the same. |
NYX | 0:60857722c2ff | 81 | */ |
NYX | 0:60857722c2ff | 82 | void pulsewidth_us(int us); |
NYX | 0:60857722c2ff | 83 | |
NYX | 0:60857722c2ff | 84 | /** |
NYX | 0:60857722c2ff | 85 | * Set the PWM pulsewidth, specified in micro-seconds (double), keeping the period the same. |
NYX | 0:60857722c2ff | 86 | */ |
NYX | 0:60857722c2ff | 87 | void pulsewidth_us(double us); |
NYX | 0:60857722c2ff | 88 | |
NYX | 0:60857722c2ff | 89 | /** |
NYX | 0:60857722c2ff | 90 | * Set the PWM period, specified in clock ticks, keeping the period the same. |
NYX | 0:60857722c2ff | 91 | * |
NYX | 0:60857722c2ff | 92 | * This function can be used if low overhead is required. Do take into account the result is |
NYX | 0:60857722c2ff | 93 | * board (clock frequency) dependent! |
NYX | 0:60857722c2ff | 94 | */ |
NYX | 0:60857722c2ff | 95 | void pulsewidth_ticks(uint32_t ticks); |
NYX | 0:60857722c2ff | 96 | |
NYX | 0:60857722c2ff | 97 | /** |
NYX | 0:60857722c2ff | 98 | * Set the ouput duty-cycle, specified as a percentage (double) |
NYX | 0:60857722c2ff | 99 | * |
NYX | 0:60857722c2ff | 100 | * @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%). |
NYX | 0:60857722c2ff | 101 | */ |
NYX | 0:60857722c2ff | 102 | void write(double duty); |
NYX | 0:60857722c2ff | 103 | |
NYX | 0:60857722c2ff | 104 | /** |
NYX | 0:60857722c2ff | 105 | * Return the ouput duty-cycle, specified as a percentage (double) |
NYX | 0:60857722c2ff | 106 | * |
NYX | 0:60857722c2ff | 107 | * @param return - A double value representing the output duty-cycle, specified as a percentage. |
NYX | 0:60857722c2ff | 108 | */ |
NYX | 0:60857722c2ff | 109 | double read( void ); |
NYX | 0:60857722c2ff | 110 | |
NYX | 0:60857722c2ff | 111 | /** |
NYX | 0:60857722c2ff | 112 | * An operator shorthand for write() |
NYX | 0:60857722c2ff | 113 | */ |
NYX | 0:60857722c2ff | 114 | FastPWM& operator= (double value); |
NYX | 0:60857722c2ff | 115 | |
NYX | 0:60857722c2ff | 116 | /** |
NYX | 0:60857722c2ff | 117 | * An operator shorthand for read() |
NYX | 0:60857722c2ff | 118 | */ |
NYX | 0:60857722c2ff | 119 | operator double(); |
NYX | 0:60857722c2ff | 120 | |
NYX | 0:60857722c2ff | 121 | /** |
NYX | 0:60857722c2ff | 122 | * Set the PWM prescaler |
NYX | 0:60857722c2ff | 123 | * |
NYX | 0:60857722c2ff | 124 | * The period of all PWM pins on the same PWM unit have to be reset after using this! |
NYX | 0:60857722c2ff | 125 | * |
NYX | 0:60857722c2ff | 126 | * @param value - The required prescaler. Special values: 0 = lock current prescaler, -1 = use dynamic prescaler |
NYX | 0:60857722c2ff | 127 | * @param return - The prescaler which was set (can differ from requested prescaler if not possible) |
NYX | 0:60857722c2ff | 128 | */ |
NYX | 0:60857722c2ff | 129 | int prescaler(int value); |
NYX | 0:60857722c2ff | 130 | |
NYX | 0:60857722c2ff | 131 | private: |
NYX | 0:60857722c2ff | 132 | void initFastPWM(void); |
NYX | 0:60857722c2ff | 133 | |
NYX | 0:60857722c2ff | 134 | uint32_t setPrescaler( uint32_t reqScale ); |
NYX | 0:60857722c2ff | 135 | int calcPrescaler(uint64_t clocks); |
NYX | 0:60857722c2ff | 136 | uint32_t getPeriod( void ); |
NYX | 0:60857722c2ff | 137 | |
NYX | 0:60857722c2ff | 138 | void updateTicks( uint32_t prescaler ); |
NYX | 0:60857722c2ff | 139 | uint32_t bits; |
NYX | 0:60857722c2ff | 140 | |
NYX | 0:60857722c2ff | 141 | double _duty; |
NYX | 0:60857722c2ff | 142 | |
NYX | 0:60857722c2ff | 143 | double dticks, dticks_us; |
NYX | 0:60857722c2ff | 144 | int iticks_ms, iticks_us; |
NYX | 0:60857722c2ff | 145 | |
NYX | 0:60857722c2ff | 146 | bool dynamicPrescaler; |
NYX | 0:60857722c2ff | 147 | |
NYX | 0:60857722c2ff | 148 | void *fast_obj; |
NYX | 0:60857722c2ff | 149 | }; |
NYX | 0:60857722c2ff | 150 | #endif |