FastPWM clone
Device/FastPWM_STM_TIM.cpp@28:3c8a0d977bc3, 2016-02-29 (annotated)
- Committer:
- Sissors
- Date:
- Mon Feb 29 19:18:42 2016 +0000
- Revision:
- 28:3c8a0d977bc3
- Parent:
- 24:1f451660d8c0
- Child:
- 32:e880dcb178f4
STM32F3, F4, L4, etc now use the channels number from the mbed library. The others still need to use the manual way.
Who changed what in which revision?
User | Revision | Line number | New 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 | |
Sissors | 28:3c8a0d977bc3 | 12 | #if defined(TARGET_STM32F0) || defined (TARGET_STM32F1) || defined (TARGET_STM32L1) |
Sissors | 28:3c8a0d977bc3 | 13 | extern __IO uint32_t* getChannel(TIM_TypeDef* pwm, PinName pin); |
Sissors | 28:3c8a0d977bc3 | 14 | #endif |
Sissors | 13:cdefd9d75b64 | 15 | |
Sissors | 13:cdefd9d75b64 | 16 | void FastPWM::initFastPWM( void ) { |
jocis | 17:8378bc456f0d | 17 | fast_obj = new (CHANNEL_P_T); |
Sissors | 28:3c8a0d977bc3 | 18 | |
Sissors | 28:3c8a0d977bc3 | 19 | #if defined(TARGET_STM32F0) || defined (TARGET_STM32F1) || defined (TARGET_STM32L1) |
jocis | 17:8378bc456f0d | 20 | *(CHANNEL_P_T*)fast_obj = getChannel(PWM_TIMER, _pwm.pin); |
Sissors | 28:3c8a0d977bc3 | 21 | #else |
Sissors | 28:3c8a0d977bc3 | 22 | *(CHANNEL_P_T*)fast_obj = &PWM_TIMER->CCR1 + _pwm.channel - 1; |
Sissors | 28:3c8a0d977bc3 | 23 | #endif |
jocis | 17:8378bc456f0d | 24 | |
Sissors | 22:db9c0cf445e2 | 25 | //Enable PWM period syncing for glitch free result |
Sissors | 22:db9c0cf445e2 | 26 | PWM_TIMER->CR1 |= TIM_CR1_ARPE; |
Sissors | 22:db9c0cf445e2 | 27 | |
Sissors | 13:cdefd9d75b64 | 28 | bits = 16; |
Sissors | 13:cdefd9d75b64 | 29 | } |
Sissors | 13:cdefd9d75b64 | 30 | |
Sissors | 13:cdefd9d75b64 | 31 | void FastPWM::pulsewidth_ticks( uint32_t ticks ) { |
Sissors | 13:cdefd9d75b64 | 32 | PWM_CHANNEL = ticks; |
Sissors | 13:cdefd9d75b64 | 33 | } |
Sissors | 13:cdefd9d75b64 | 34 | |
Sissors | 13:cdefd9d75b64 | 35 | void FastPWM::period_ticks( uint32_t ticks ) { |
Sissors | 13:cdefd9d75b64 | 36 | PWM_TIMER->ARR = ticks - 1; |
Sissors | 13:cdefd9d75b64 | 37 | } |
Sissors | 13:cdefd9d75b64 | 38 | |
Sissors | 13:cdefd9d75b64 | 39 | uint32_t FastPWM::getPeriod( void ) { |
Sissors | 13:cdefd9d75b64 | 40 | return PWM_TIMER->ARR + 1; |
Sissors | 13:cdefd9d75b64 | 41 | } |
Sissors | 13:cdefd9d75b64 | 42 | |
Sissors | 13:cdefd9d75b64 | 43 | uint32_t FastPWM::setPrescaler(uint32_t reqScale) { |
Sissors | 13:cdefd9d75b64 | 44 | if (reqScale == 0) |
Sissors | 13:cdefd9d75b64 | 45 | //Return prescaler |
Sissors | 13:cdefd9d75b64 | 46 | return PWM_TIMER->PSC + 1; |
Sissors | 13:cdefd9d75b64 | 47 | if (reqScale > (uint32_t)(1<<16)) |
Sissors | 13:cdefd9d75b64 | 48 | reqScale = 1<<16; |
Sissors | 13:cdefd9d75b64 | 49 | //Else set prescaler, we have to substract one from reqScale since a 0 in PCVAL is prescaler of 1 |
Sissors | 13:cdefd9d75b64 | 50 | PWM_TIMER->PSC = reqScale - 1; |
Sissors | 13:cdefd9d75b64 | 51 | |
Sissors | 13:cdefd9d75b64 | 52 | return reqScale; |
Sissors | 13:cdefd9d75b64 | 53 | } |
Sissors | 13:cdefd9d75b64 | 54 | |
Sissors | 13:cdefd9d75b64 | 55 | #endif |