Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: qonly_controller foc-ed_in_the_bot_compact foc-ed_in_the_bot_compact CurrentModeSine ... more
Fork of FastPWM by
Device/FastPWM_STM_TIM.cpp
- Committer:
- benkatz
- Date:
- 2015-12-13
- Revision:
- 26:51c979bca21e
- Parent:
- 24:1f451660d8c0
File content as of revision 26:51c979bca21e:
//This should (hopefully) work on all STM targets which use TIM timers for PWM
#ifdef TARGET_STM
#include "FastPWM.h"
typedef __IO uint32_t* CHANNEL_P_T;
#define PWM_CHANNEL (**(CHANNEL_P_T*)fast_obj)
#define PWM_TIMER ((TIM_TypeDef*)_pwm.pwm)
extern CHANNEL_P_T getChannel(TIM_TypeDef* pwm, PinName pin);
void FastPWM::initFastPWM( void ) {
fast_obj = new (CHANNEL_P_T);
*(CHANNEL_P_T*)fast_obj = getChannel(PWM_TIMER, _pwm.pin);
//Enable PWM period syncing for glitch free result
PWM_TIMER->CR1 |= TIM_CR1_ARPE;
bits = 16;
}
void FastPWM::pulsewidth_ticks( uint32_t ticks ) {
PWM_CHANNEL = ticks;
}
void FastPWM::period_ticks( uint32_t ticks ) {
PWM_TIMER->ARR = ticks - 1;
}
uint32_t FastPWM::getPeriod( void ) {
return PWM_TIMER->ARR + 1;
}
uint32_t FastPWM::setPrescaler(uint32_t reqScale) {
if (reqScale == 0)
//Return prescaler
return PWM_TIMER->PSC + 1;
if (reqScale > (uint32_t)(1<<16))
reqScale = 1<<16;
//Else set prescaler, we have to substract one from reqScale since a 0 in PCVAL is prescaler of 1
PWM_TIMER->PSC = reqScale - 1;
return reqScale;
}
#endif
