Michael Ernst Peter / FastPWM

Dependents:   PM2_Libary PM2_Libary

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FastPWM_common.cpp Source File

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.0);
00011 }
00012 
00013 FastPWM::~FastPWM( void ) {
00014     if (fast_obj != NULL)
00015         free(fast_obj);
00016 } 
00017 
00018 void FastPWM::period(double seconds) {
00019     if (dynamicPrescaler)
00020         calcPrescaler((uint64_t)(seconds * (double) SystemCoreClock));
00021 
00022      period_ticks(seconds * dticks + 0.5);
00023 }
00024 
00025 void FastPWM::period(float seconds) {
00026     period( static_cast<double>(seconds) );
00027 }
00028 
00029 void FastPWM::period_ms(int ms) {
00030     if (dynamicPrescaler)
00031         calcPrescaler(ms * (SystemCoreClock / 1000));
00032         
00033     period_ticks(ms * iticks_ms);
00034 }
00035 
00036 void FastPWM::period_us(int us) {
00037     if (dynamicPrescaler)
00038         calcPrescaler(us * (SystemCoreClock / 1000000));
00039     
00040     period_ticks(us * iticks_us);
00041 }
00042 
00043 void FastPWM::period_us(double us) {
00044     if (dynamicPrescaler)
00045         calcPrescaler((uint64_t)(us * (double)(SystemCoreClock / 1000000)));
00046         
00047     period_ticks(us * dticks_us + 0.5);
00048 }
00049 
00050 void FastPWM::pulsewidth(double seconds) {
00051     pulsewidth_ticks(seconds * dticks + 0.5);
00052 }
00053 
00054 void FastPWM::pulsewidth_ms(int ms) {
00055     pulsewidth_ticks(ms * iticks_ms);
00056 }
00057 
00058 void FastPWM::pulsewidth_us(int us) {
00059     pulsewidth_ticks(us * iticks_us);
00060 }
00061 
00062 void FastPWM::pulsewidth_us(double us) {
00063     pulsewidth_ticks(us * dticks_us + 0.5);
00064 }
00065 
00066 void FastPWM::write(double duty) {
00067     _duty=duty;
00068     pulsewidth_ticks(duty*getPeriod());
00069 }
00070 
00071 void FastPWM::write(float duty) {
00072     write(static_cast<double>(duty));
00073 }
00074 
00075 double FastPWM::read( void ) {
00076     return _duty;
00077     }
00078     
00079 FastPWM & FastPWM::operator= (double value) {
00080     write(value);
00081     return(*this);
00082     }
00083     
00084 FastPWM::operator double() {
00085     return _duty;
00086 }
00087 
00088 int FastPWM::prescaler(int value) {
00089     int retval;
00090     if (value == -1) {
00091         dynamicPrescaler = true;
00092         value = 0;
00093     }
00094     else
00095         dynamicPrescaler = false;
00096     
00097     retval = setPrescaler(value);
00098     updateTicks(retval);
00099     return retval;
00100 }
00101 
00102 void FastPWM::updateTicks( uint32_t prescaler ) {
00103     dticks = SystemCoreClock / (double)prescaler;
00104     dticks_us = dticks / 1000000.0f;
00105     iticks_us = (int)(dticks_us + 0.5);
00106     iticks_ms = (int)(dticks_us * 1000.0 + 0.5);
00107 }
00108 
00109 int FastPWM::calcPrescaler(uint64_t clocks) {
00110     uint32_t scale = (clocks >> bits) + 1;
00111     uint32_t retval = setPrescaler(scale);
00112     updateTicks(retval);
00113     return retval;
00114 }