changed for STM32F4

Fork of FastPWM by Erik -

Committer:
Sissors
Date:
Tue Aug 13 16:54:06 2013 +0000
Revision:
4:a7b9f778c4b4
Child:
7:1b5df740bcd7
v2.0
; Added KL25Z/LPC11u24 support
; Added prescalers
; Direct tick changes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sissors 4:a7b9f778c4b4 1 #ifdef TARGET_LPC11U24
Sissors 4:a7b9f778c4b4 2
Sissors 4:a7b9f778c4b4 3 #include "FastPWM.h"
Sissors 4:a7b9f778c4b4 4
Sissors 4:a7b9f778c4b4 5
Sissors 4:a7b9f778c4b4 6 volatile uint32_t *PWM_MR;
Sissors 4:a7b9f778c4b4 7 LPC_CTxxBx_Type *pwm_obj;
Sissors 4:a7b9f778c4b4 8
Sissors 4:a7b9f778c4b4 9 typedef struct {
Sissors 4:a7b9f778c4b4 10 uint8_t timer;
Sissors 4:a7b9f778c4b4 11 uint8_t mr;
Sissors 4:a7b9f778c4b4 12 } timer_mr;
Sissors 4:a7b9f778c4b4 13
Sissors 4:a7b9f778c4b4 14 static timer_mr pwm_timer_map[11] = {
Sissors 4:a7b9f778c4b4 15 {0, 0}, {0, 1}, {0, 2},
Sissors 4:a7b9f778c4b4 16 {1, 0}, {1, 1},
Sissors 4:a7b9f778c4b4 17 {2, 0}, {2, 1}, {2, 2},
Sissors 4:a7b9f778c4b4 18 {3, 0}, {3, 1}, {3, 2},
Sissors 4:a7b9f778c4b4 19 };
Sissors 4:a7b9f778c4b4 20
Sissors 4:a7b9f778c4b4 21 static LPC_CTxxBx_Type *Timers[4] = {
Sissors 4:a7b9f778c4b4 22 LPC_CT16B0, LPC_CT16B1,
Sissors 4:a7b9f778c4b4 23 LPC_CT32B0, LPC_CT32B1
Sissors 4:a7b9f778c4b4 24 };
Sissors 4:a7b9f778c4b4 25
Sissors 4:a7b9f778c4b4 26
Sissors 4:a7b9f778c4b4 27 void FastPWM::initFastPWM( void ) {
Sissors 4:a7b9f778c4b4 28 //Sadly the 11u24 pwm object does not store match register/pwm object for some reason
Sissors 4:a7b9f778c4b4 29 //It recalculates it everytime, we just do it once because we are awesome
Sissors 4:a7b9f778c4b4 30 timer_mr tid = pwm_timer_map[_pwm.pwm];
Sissors 4:a7b9f778c4b4 31 pwm_obj = Timers[tid.timer];
Sissors 4:a7b9f778c4b4 32 PWM_MR = &pwm_obj->MR[tid.mr];
Sissors 4:a7b9f778c4b4 33
Sissors 4:a7b9f778c4b4 34 if (tid.timer < 2)
Sissors 4:a7b9f778c4b4 35 //16-bit timer
Sissors 4:a7b9f778c4b4 36 bits = 16;
Sissors 4:a7b9f778c4b4 37 else
Sissors 4:a7b9f778c4b4 38 //32-bit timer
Sissors 4:a7b9f778c4b4 39 bits = 32;
Sissors 4:a7b9f778c4b4 40
Sissors 4:a7b9f778c4b4 41 printf("Bits = %d, PWM = %d, MR = %d\n\r", bits, tid.timer, tid.mr);
Sissors 4:a7b9f778c4b4 42
Sissors 4:a7b9f778c4b4 43 }
Sissors 4:a7b9f778c4b4 44
Sissors 4:a7b9f778c4b4 45 void FastPWM::pulsewidth_ticks( uint32_t ticks ) {
Sissors 4:a7b9f778c4b4 46 pwm_obj->TCR = 0x02;
Sissors 4:a7b9f778c4b4 47 *PWM_MR = pwm_obj->MR3 - ticks; //They inverted PWM on the 11u24
Sissors 4:a7b9f778c4b4 48 pwm_obj->TCR = 0x01;
Sissors 4:a7b9f778c4b4 49 }
Sissors 4:a7b9f778c4b4 50
Sissors 4:a7b9f778c4b4 51 void FastPWM::period_ticks( uint32_t ticks ) {
Sissors 4:a7b9f778c4b4 52 pwm_obj->TCR = 0x02;
Sissors 4:a7b9f778c4b4 53 pwm_obj->MR3 = ticks;
Sissors 4:a7b9f778c4b4 54 pwm_obj->TCR = 0x01;
Sissors 4:a7b9f778c4b4 55 }
Sissors 4:a7b9f778c4b4 56
Sissors 4:a7b9f778c4b4 57 uint32_t FastPWM::getPeriod( void ) {
Sissors 4:a7b9f778c4b4 58 return pwm_obj->MR3;
Sissors 4:a7b9f778c4b4 59 }
Sissors 4:a7b9f778c4b4 60
Sissors 4:a7b9f778c4b4 61 uint32_t FastPWM::setPrescaler(uint32_t reqScale) {
Sissors 4:a7b9f778c4b4 62 //If 32-bit, disable auto-scaling, return 1
Sissors 4:a7b9f778c4b4 63 if (bits == 32) {
Sissors 4:a7b9f778c4b4 64 dynamicPrescaler = false;
Sissors 4:a7b9f778c4b4 65 return 1;
Sissors 4:a7b9f778c4b4 66 }
Sissors 4:a7b9f778c4b4 67
Sissors 4:a7b9f778c4b4 68 //Else 16-bit timer:
Sissors 4:a7b9f778c4b4 69 if (reqScale == 0)
Sissors 4:a7b9f778c4b4 70 //Return prescaler
Sissors 4:a7b9f778c4b4 71 return pwm_obj->PR + 1;
Sissors 4:a7b9f778c4b4 72 if (reqScale > (uint32_t)(1<<16))
Sissors 4:a7b9f778c4b4 73 reqScale = 1<<16;
Sissors 4:a7b9f778c4b4 74 //Else set prescaler, we have to substract one from reqScale since a 0 in PCVAL is prescaler of 1
Sissors 4:a7b9f778c4b4 75 pwm_obj->PR = reqScale - 1;
Sissors 4:a7b9f778c4b4 76
Sissors 4:a7b9f778c4b4 77 return reqScale;
Sissors 4:a7b9f778c4b4 78 }
Sissors 4:a7b9f778c4b4 79
Sissors 4:a7b9f778c4b4 80 #endif