other
Dependents: qonly_controller foc-ed_in_the_bot_compact foc-ed_in_the_bot_compact CurrentModeSine ... more
Fork of FastPWM by
FastPWM_common.cpp
00001 #include "FastPWM.h" 00002 00003 FastPWM::FastPWM(PinName pin, int prescaler) : PwmOut(pin) { 00004 fast_obj = NULL; 00005 initFastPWM(); 00006 this->prescaler(prescaler); 00007 00008 //Set duty cycle on 0%, period on 20ms 00009 period(0.02); 00010 write(0); 00011 00012 00013 } 00014 00015 FastPWM::~FastPWM( void ) { 00016 if (fast_obj != NULL) 00017 delete(fast_obj); 00018 } 00019 00020 void FastPWM::period(double seconds) { 00021 if (dynamicPrescaler) 00022 calcPrescaler((uint64_t)(seconds * (double) SystemCoreClock)); 00023 00024 period_ticks(seconds * dticks + 0.5); 00025 } 00026 00027 void FastPWM::period_ms(int ms) { 00028 if (dynamicPrescaler) 00029 calcPrescaler(ms * (SystemCoreClock / 1000)); 00030 00031 period_ticks(ms * iticks_ms); 00032 } 00033 00034 void FastPWM::period_us(int us) { 00035 if (dynamicPrescaler) 00036 calcPrescaler(us * (SystemCoreClock / 1000000)); 00037 00038 period_ticks(us * iticks_us); 00039 } 00040 00041 void FastPWM::period_us(double us) { 00042 if (dynamicPrescaler) 00043 calcPrescaler((uint64_t)(us * (double)(SystemCoreClock / 1000000))); 00044 00045 period_ticks(us * dticks_us + 0.5); 00046 } 00047 00048 void FastPWM::pulsewidth(double seconds) { 00049 pulsewidth_ticks(seconds * dticks + 0.5); 00050 } 00051 00052 void FastPWM::pulsewidth_ms(int ms) { 00053 pulsewidth_ticks(ms * iticks_ms); 00054 } 00055 00056 void FastPWM::pulsewidth_us(int us) { 00057 pulsewidth_ticks(us * iticks_us); 00058 } 00059 00060 void FastPWM::pulsewidth_us(double us) { 00061 pulsewidth_ticks(us * dticks_us + 0.5); 00062 } 00063 00064 void FastPWM::write(double duty) { 00065 _duty=duty; 00066 pulsewidth_ticks(duty*getPeriod()); 00067 } 00068 00069 double FastPWM::read( void ) { 00070 return _duty; 00071 } 00072 00073 FastPWM & FastPWM::operator= (double value) { 00074 write(value); 00075 return(*this); 00076 } 00077 00078 FastPWM::operator double() { 00079 return _duty; 00080 } 00081 00082 int FastPWM::prescaler(int value) { 00083 int retval; 00084 if (value == -1) { 00085 dynamicPrescaler = true; 00086 value = 0; 00087 } 00088 else 00089 dynamicPrescaler = false; 00090 00091 retval = setPrescaler(value); 00092 updateTicks(retval); 00093 return retval; 00094 } 00095 00096 void FastPWM::updateTicks( uint32_t prescaler ) { 00097 dticks = SystemCoreClock / (double)prescaler; 00098 dticks_us = dticks / 1000000.0f; 00099 iticks_us = (int)(dticks_us + 0.5); 00100 iticks_ms = (int)(dticks_us * 1000.0 + 0.5); 00101 } 00102 00103 int FastPWM::calcPrescaler(uint64_t clocks) { 00104 uint32_t scale = (clocks >> bits) + 1; 00105 uint32_t retval = setPrescaler(scale); 00106 updateTicks(retval); 00107 return retval; 00108 }
Generated on Wed Jul 13 2022 08:26:26 by 1.7.2