My forked repository. DISCO_F407VG, DISCO_F303VC, DISCO_F051R8 and DISCO_F100RB maybe added.
Dependents: FastPWM-DISCO-test
Fork of FastPWM by
FastPWM_STM_TIM.cpp
00001 //This should (hopefully) work on all STM targets which use TIM timers for PWM 00002 00003 #ifdef TARGET_STM 00004 00005 #include "FastPWM.h" 00006 00007 typedef __IO uint32_t* CHANNEL_P_T; 00008 00009 #define PWM_CHANNEL (**(CHANNEL_P_T*)fast_obj) 00010 #define PWM_TIMER ((TIM_TypeDef*)_pwm.pwm) 00011 00012 extern CHANNEL_P_T getChannel(TIM_TypeDef* pwm, PinName pin); 00013 00014 void FastPWM::initFastPWM( void ) { 00015 fast_obj = new (CHANNEL_P_T); 00016 *(CHANNEL_P_T*)fast_obj = getChannel(PWM_TIMER, _pwm.pin); 00017 00018 //Enable PWM period syncing for glitch free result 00019 PWM_TIMER->CR1 |= TIM_CR1_ARPE; 00020 00021 bits = 16; 00022 } 00023 00024 void FastPWM::pulsewidth_ticks( uint32_t ticks ) { 00025 PWM_CHANNEL = ticks; 00026 } 00027 00028 void FastPWM::period_ticks( uint32_t ticks ) { 00029 PWM_TIMER->ARR = ticks - 1; 00030 } 00031 00032 uint32_t FastPWM::getPeriod( void ) { 00033 return PWM_TIMER->ARR + 1; 00034 } 00035 00036 uint32_t FastPWM::setPrescaler(uint32_t reqScale) { 00037 if (reqScale == 0) 00038 //Return prescaler 00039 return PWM_TIMER->PSC + 1; 00040 if (reqScale > (uint32_t)(1<<16)) 00041 reqScale = 1<<16; 00042 //Else set prescaler, we have to substract one from reqScale since a 0 in PCVAL is prescaler of 1 00043 PWM_TIMER->PSC = reqScale - 1; 00044 00045 return reqScale; 00046 } 00047 00048 #endif
Generated on Tue Jul 19 2022 04:55:37 by 1.7.2