Ley de Ohm

Fork of FastPWM by Erik -

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FastPWM_STM_TIM.cpp Source File

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 #if defined(TARGET_STM32F0) || defined (TARGET_STM32F1) || defined (TARGET_STM32L1)
00013 extern __IO uint32_t* getChannel(TIM_TypeDef* pwm, PinName pin);
00014 #endif
00015 
00016 void FastPWM::initFastPWM( void ) {
00017     fast_obj = new (CHANNEL_P_T);
00018     
00019     #if defined(TARGET_STM32F0) || defined (TARGET_STM32F1) || defined (TARGET_STM32L1)
00020     *(CHANNEL_P_T*)fast_obj = getChannel(PWM_TIMER, _pwm.pin);
00021     #else
00022     *(CHANNEL_P_T*)fast_obj = &PWM_TIMER->CCR1 + _pwm.channel - 1; 
00023     #endif
00024     
00025     //Enable PWM period syncing for glitch free result
00026     PWM_TIMER->CR1 |= TIM_CR1_ARPE;
00027     
00028     bits = 16;
00029 }
00030 
00031 void FastPWM::pulsewidth_ticks( uint32_t ticks ) {
00032     PWM_CHANNEL = ticks;    
00033 }
00034 
00035 void FastPWM::period_ticks( uint32_t ticks ) {
00036     PWM_TIMER->ARR = ticks - 1;
00037 }
00038 
00039 uint32_t FastPWM::getPeriod( void ) {
00040     return PWM_TIMER->ARR + 1;
00041 }
00042 
00043 uint32_t FastPWM::setPrescaler(uint32_t reqScale) {
00044     if (reqScale == 0)
00045         //Return prescaler
00046         return PWM_TIMER->PSC + 1;
00047     if (reqScale > (uint32_t)(1<<16))
00048         reqScale = 1<<16;
00049     //Else set prescaler, we have to substract one from reqScale since a 0 in PCVAL is prescaler of 1
00050     PWM_TIMER->PSC = reqScale - 1;
00051 
00052     return reqScale;
00053 }
00054 
00055 #endif