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