Anasse Abdoul / Mbed 2 deprecated Test_MPU6050

Dependencies:   mbed

Committer:
anasse
Date:
Thu Mar 31 07:43:50 2022 +0000
Revision:
0:a59a3d743804
vers0

Who changed what in which revision?

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