inported from local

Committer:
NYX
Date:
Mon Mar 16 06:36:03 2020 +0000
Revision:
0:60857722c2ff
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
NYX 0:60857722c2ff 1 #include "FastPWM.h"
NYX 0:60857722c2ff 2
NYX 0:60857722c2ff 3 FastPWM::FastPWM(PinName pin, int prescaler) : PwmOut(pin) {
NYX 0:60857722c2ff 4 fast_obj = NULL;
NYX 0:60857722c2ff 5 initFastPWM();
NYX 0:60857722c2ff 6 this->prescaler(prescaler);
NYX 0:60857722c2ff 7
NYX 0:60857722c2ff 8 //Set duty cycle on 0%, period on 20ms
NYX 0:60857722c2ff 9 period(0.02);
NYX 0:60857722c2ff 10 write(0);
NYX 0:60857722c2ff 11
NYX 0:60857722c2ff 12
NYX 0:60857722c2ff 13 }
NYX 0:60857722c2ff 14
NYX 0:60857722c2ff 15 FastPWM::~FastPWM( void ) {
NYX 0:60857722c2ff 16 if (fast_obj != NULL)
NYX 0:60857722c2ff 17 delete(fast_obj);
NYX 0:60857722c2ff 18 }
NYX 0:60857722c2ff 19
NYX 0:60857722c2ff 20 void FastPWM::period(double seconds) {
NYX 0:60857722c2ff 21 if (dynamicPrescaler)
NYX 0:60857722c2ff 22 calcPrescaler((uint64_t)(seconds * (double) SystemCoreClock));
NYX 0:60857722c2ff 23
NYX 0:60857722c2ff 24 period_ticks(seconds * dticks + 0.5);
NYX 0:60857722c2ff 25 }
NYX 0:60857722c2ff 26
NYX 0:60857722c2ff 27 void FastPWM::period_ms(int ms) {
NYX 0:60857722c2ff 28 if (dynamicPrescaler)
NYX 0:60857722c2ff 29 calcPrescaler(ms * (SystemCoreClock / 1000));
NYX 0:60857722c2ff 30
NYX 0:60857722c2ff 31 period_ticks(ms * iticks_ms);
NYX 0:60857722c2ff 32 }
NYX 0:60857722c2ff 33
NYX 0:60857722c2ff 34 void FastPWM::period_us(int us) {
NYX 0:60857722c2ff 35 if (dynamicPrescaler)
NYX 0:60857722c2ff 36 calcPrescaler(us * (SystemCoreClock / 1000000));
NYX 0:60857722c2ff 37
NYX 0:60857722c2ff 38 period_ticks(us * iticks_us);
NYX 0:60857722c2ff 39 }
NYX 0:60857722c2ff 40
NYX 0:60857722c2ff 41 void FastPWM::period_us(double us) {
NYX 0:60857722c2ff 42 if (dynamicPrescaler)
NYX 0:60857722c2ff 43 calcPrescaler((uint64_t)(us * (double)(SystemCoreClock / 1000000)));
NYX 0:60857722c2ff 44
NYX 0:60857722c2ff 45 period_ticks(us * dticks_us + 0.5);
NYX 0:60857722c2ff 46 }
NYX 0:60857722c2ff 47
NYX 0:60857722c2ff 48 void FastPWM::pulsewidth(double seconds) {
NYX 0:60857722c2ff 49 pulsewidth_ticks(seconds * dticks + 0.5);
NYX 0:60857722c2ff 50 }
NYX 0:60857722c2ff 51
NYX 0:60857722c2ff 52 void FastPWM::pulsewidth_ms(int ms) {
NYX 0:60857722c2ff 53 pulsewidth_ticks(ms * iticks_ms);
NYX 0:60857722c2ff 54 }
NYX 0:60857722c2ff 55
NYX 0:60857722c2ff 56 void FastPWM::pulsewidth_us(int us) {
NYX 0:60857722c2ff 57 pulsewidth_ticks(us * iticks_us);
NYX 0:60857722c2ff 58 }
NYX 0:60857722c2ff 59
NYX 0:60857722c2ff 60 void FastPWM::pulsewidth_us(double us) {
NYX 0:60857722c2ff 61 pulsewidth_ticks(us * dticks_us + 0.5);
NYX 0:60857722c2ff 62 }
NYX 0:60857722c2ff 63
NYX 0:60857722c2ff 64 void FastPWM::write(double duty) {
NYX 0:60857722c2ff 65 _duty=duty;
NYX 0:60857722c2ff 66 pulsewidth_ticks(duty*getPeriod());
NYX 0:60857722c2ff 67 }
NYX 0:60857722c2ff 68
NYX 0:60857722c2ff 69 double FastPWM::read( void ) {
NYX 0:60857722c2ff 70 return _duty;
NYX 0:60857722c2ff 71 }
NYX 0:60857722c2ff 72
NYX 0:60857722c2ff 73 FastPWM & FastPWM::operator= (double value) {
NYX 0:60857722c2ff 74 write(value);
NYX 0:60857722c2ff 75 return(*this);
NYX 0:60857722c2ff 76 }
NYX 0:60857722c2ff 77
NYX 0:60857722c2ff 78 FastPWM::operator double() {
NYX 0:60857722c2ff 79 return _duty;
NYX 0:60857722c2ff 80 }
NYX 0:60857722c2ff 81
NYX 0:60857722c2ff 82 int FastPWM::prescaler(int value) {
NYX 0:60857722c2ff 83 int retval;
NYX 0:60857722c2ff 84 if (value == -1) {
NYX 0:60857722c2ff 85 dynamicPrescaler = true;
NYX 0:60857722c2ff 86 value = 0;
NYX 0:60857722c2ff 87 }
NYX 0:60857722c2ff 88 else
NYX 0:60857722c2ff 89 dynamicPrescaler = false;
NYX 0:60857722c2ff 90
NYX 0:60857722c2ff 91 retval = setPrescaler(value);
NYX 0:60857722c2ff 92 updateTicks(retval);
NYX 0:60857722c2ff 93 return retval;
NYX 0:60857722c2ff 94 }
NYX 0:60857722c2ff 95
NYX 0:60857722c2ff 96 void FastPWM::updateTicks( uint32_t prescaler ) {
NYX 0:60857722c2ff 97 dticks = SystemCoreClock / (double)prescaler;
NYX 0:60857722c2ff 98 dticks_us = dticks / 1000000.0f;
NYX 0:60857722c2ff 99 iticks_us = (int)(dticks_us + 0.5);
NYX 0:60857722c2ff 100 iticks_ms = (int)(dticks_us * 1000.0 + 0.5);
NYX 0:60857722c2ff 101 }
NYX 0:60857722c2ff 102
NYX 0:60857722c2ff 103 int FastPWM::calcPrescaler(uint64_t clocks) {
NYX 0:60857722c2ff 104 uint32_t scale = (clocks >> bits) + 1;
NYX 0:60857722c2ff 105 uint32_t retval = setPrescaler(scale);
NYX 0:60857722c2ff 106 updateTicks(retval);
NYX 0:60857722c2ff 107 return retval;
NYX 0:60857722c2ff 108 }