FASTPWM

Dependents:   OV7670_make

Committer:
jocis
Date:
Sat Oct 04 13:14:44 2014 +0000
Revision:
17:8378bc456f0d
Parent:
13:cdefd9d75b64
Child:
22:db9c0cf445e2
Added NUCLEO F103RB; adapted t 32/16 bit pwm struct for NUCLEO

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
jocis 17:8378bc456f0d 7 #if defined TARGET_NUCLEO_F103RB
jocis 17:8378bc456f0d 8 typedef __IO uint16_t* CHANNEL_P_T;
jocis 17:8378bc456f0d 9 #else
jocis 17:8378bc456f0d 10 typedef __IO uint32_t* CHANNEL_P_T;
jocis 17:8378bc456f0d 11 #endif
jocis 17:8378bc456f0d 12
jocis 17:8378bc456f0d 13 #define PWM_CHANNEL (**(CHANNEL_P_T*)fast_obj)
Sissors 13:cdefd9d75b64 14 #define PWM_TIMER ((TIM_TypeDef*)_pwm.pwm)
Sissors 13:cdefd9d75b64 15
jocis 17:8378bc456f0d 16 extern CHANNEL_P_T getChannel(TIM_TypeDef* pwm, PinName pin);
Sissors 13:cdefd9d75b64 17
Sissors 13:cdefd9d75b64 18 void FastPWM::initFastPWM( void ) {
jocis 17:8378bc456f0d 19 fast_obj = new (CHANNEL_P_T);
jocis 17:8378bc456f0d 20 *(CHANNEL_P_T*)fast_obj = getChannel(PWM_TIMER, _pwm.pin);
jocis 17:8378bc456f0d 21
Sissors 13:cdefd9d75b64 22 bits = 16;
Sissors 13:cdefd9d75b64 23 }
Sissors 13:cdefd9d75b64 24
Sissors 13:cdefd9d75b64 25 void FastPWM::pulsewidth_ticks( uint32_t ticks ) {
Sissors 13:cdefd9d75b64 26 PWM_CHANNEL = ticks;
Sissors 13:cdefd9d75b64 27 }
Sissors 13:cdefd9d75b64 28
Sissors 13:cdefd9d75b64 29 void FastPWM::period_ticks( uint32_t ticks ) {
Sissors 13:cdefd9d75b64 30 PWM_TIMER->ARR = ticks - 1;
Sissors 13:cdefd9d75b64 31 }
Sissors 13:cdefd9d75b64 32
Sissors 13:cdefd9d75b64 33 uint32_t FastPWM::getPeriod( void ) {
Sissors 13:cdefd9d75b64 34 return PWM_TIMER->ARR + 1;
Sissors 13:cdefd9d75b64 35 }
Sissors 13:cdefd9d75b64 36
Sissors 13:cdefd9d75b64 37 uint32_t FastPWM::setPrescaler(uint32_t reqScale) {
Sissors 13:cdefd9d75b64 38 if (reqScale == 0)
Sissors 13:cdefd9d75b64 39 //Return prescaler
Sissors 13:cdefd9d75b64 40 return PWM_TIMER->PSC + 1;
Sissors 13:cdefd9d75b64 41 if (reqScale > (uint32_t)(1<<16))
Sissors 13:cdefd9d75b64 42 reqScale = 1<<16;
Sissors 13:cdefd9d75b64 43 //Else set prescaler, we have to substract one from reqScale since a 0 in PCVAL is prescaler of 1
Sissors 13:cdefd9d75b64 44 PWM_TIMER->PSC = reqScale - 1;
Sissors 13:cdefd9d75b64 45
Sissors 13:cdefd9d75b64 46 return reqScale;
Sissors 13:cdefd9d75b64 47 }
Sissors 13:cdefd9d75b64 48
Sissors 13:cdefd9d75b64 49 #endif