My forked repository. DISCO_F407VG, DISCO_F303VC, DISCO_F051R8 and DISCO_F100RB maybe added.
Dependents: FastPWM-DISCO-test
Fork of FastPWM by
Device/FastPWM_STM_TIM.cpp@24:3467fafa8a21, 2016-02-11 (annotated)
- Committer:
- mimi3
- Date:
- Thu Feb 11 03:35:05 2016 +0900
- Revision:
- 24:3467fafa8a21
- Parent:
- 22:db9c0cf445e2
Added: DISCO-{F051R8, F100RB, F303VC, F407VG}
NUCLEO-L152RE
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 | |
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 | |
mimi3 | 24:3467fafa8a21 | 48 | #endif |