changed for STM32F4

Fork of FastPWM by Erik -

Committer:
gurh_an
Date:
Sun Aug 09 20:24:55 2015 +0000
Revision:
25:0014e85fe8b8
Parent:
24:1f451660d8c0
Hal interface for Linuxcnc 5 axis step/dir - 5 axis encoder - 32 DIO

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