FastPWM clone

Committer:
Sissors
Date:
Sun Jul 20 19:49:43 2014 +0000
Revision:
13:cdefd9d75b64
Child:
17:8378bc456f0d
Added NUCLEO support (F030 + F401, but others should be easily added).

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sissors 13:cdefd9d75b64 1 //This should (hopefully) work on all STM targets which use TIM timers for PWM
Sissors 13:cdefd9d75b64 2
Sissors 13:cdefd9d75b64 3 #ifdef TARGET_STM
Sissors 13:cdefd9d75b64 4
Sissors 13:cdefd9d75b64 5 #include "FastPWM.h"
Sissors 13:cdefd9d75b64 6
Sissors 13:cdefd9d75b64 7 #define PWM_CHANNEL (**(__IO uint32_t**)fast_obj)
Sissors 13:cdefd9d75b64 8 #define PWM_TIMER ((TIM_TypeDef*)_pwm.pwm)
Sissors 13:cdefd9d75b64 9
Sissors 13:cdefd9d75b64 10 extern __IO uint32_t* getChannel(TIM_TypeDef* pwm, PinName pin);
Sissors 13:cdefd9d75b64 11
Sissors 13:cdefd9d75b64 12 void FastPWM::initFastPWM( void ) {
Sissors 13:cdefd9d75b64 13 fast_obj = new (__IO uint32_t*);
Sissors 13:cdefd9d75b64 14 *(__IO uint32_t**)fast_obj = getChannel(PWM_TIMER, _pwm.pin);
Sissors 13:cdefd9d75b64 15 bits = 16;
Sissors 13:cdefd9d75b64 16 }
Sissors 13:cdefd9d75b64 17
Sissors 13:cdefd9d75b64 18 void FastPWM::pulsewidth_ticks( uint32_t ticks ) {
Sissors 13:cdefd9d75b64 19 PWM_CHANNEL = ticks;
Sissors 13:cdefd9d75b64 20 }
Sissors 13:cdefd9d75b64 21
Sissors 13:cdefd9d75b64 22 void FastPWM::period_ticks( uint32_t ticks ) {
Sissors 13:cdefd9d75b64 23 PWM_TIMER->ARR = ticks - 1;
Sissors 13:cdefd9d75b64 24 }
Sissors 13:cdefd9d75b64 25
Sissors 13:cdefd9d75b64 26 uint32_t FastPWM::getPeriod( void ) {
Sissors 13:cdefd9d75b64 27 return PWM_TIMER->ARR + 1;
Sissors 13:cdefd9d75b64 28 }
Sissors 13:cdefd9d75b64 29
Sissors 13:cdefd9d75b64 30 uint32_t FastPWM::setPrescaler(uint32_t reqScale) {
Sissors 13:cdefd9d75b64 31 if (reqScale == 0)
Sissors 13:cdefd9d75b64 32 //Return prescaler
Sissors 13:cdefd9d75b64 33 return PWM_TIMER->PSC + 1;
Sissors 13:cdefd9d75b64 34 if (reqScale > (uint32_t)(1<<16))
Sissors 13:cdefd9d75b64 35 reqScale = 1<<16;
Sissors 13:cdefd9d75b64 36 //Else set prescaler, we have to substract one from reqScale since a 0 in PCVAL is prescaler of 1
Sissors 13:cdefd9d75b64 37 PWM_TIMER->PSC = reqScale - 1;
Sissors 13:cdefd9d75b64 38
Sissors 13:cdefd9d75b64 39 return reqScale;
Sissors 13:cdefd9d75b64 40 }
Sissors 13:cdefd9d75b64 41
Sissors 13:cdefd9d75b64 42 #endif