FastPWM clone

Committer:
Sissors
Date:
Thu May 01 16:54:00 2014 +0000
Revision:
7:1b5df740bcd7
Parent:
4:a7b9f778c4b4
Child:
10:36362e9067b0
Fixed https://mbed.org/forum/bugs-suggestions/topic/4886/?page=1#comment-24302 and improved in general LPC11u24 code

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 7:1b5df740bcd7 39 bits = 32;
Sissors 4:a7b9f778c4b4 40 }
Sissors 4:a7b9f778c4b4 41
Sissors 4:a7b9f778c4b4 42 void FastPWM::pulsewidth_ticks( uint32_t ticks ) {
Sissors 7:1b5df740bcd7 43 if (ticks)
Sissors 7:1b5df740bcd7 44 *PWM_MR = pwm_obj->MR3 - ticks; //They inverted PWM on the 11u24
Sissors 7:1b5df740bcd7 45 else
Sissors 7:1b5df740bcd7 46 *PWM_MR = 0xFFFFFFFF; //If MR3 = ticks 1 clock cycle wide errors appear, this prevents that (unless MR3 = max).
Sissors 4:a7b9f778c4b4 47 }
Sissors 4:a7b9f778c4b4 48
Sissors 4:a7b9f778c4b4 49 void FastPWM::period_ticks( uint32_t ticks ) {
Sissors 4:a7b9f778c4b4 50 pwm_obj->TCR = 0x02;
Sissors 4:a7b9f778c4b4 51 pwm_obj->MR3 = ticks;
Sissors 4:a7b9f778c4b4 52 pwm_obj->TCR = 0x01;
Sissors 4:a7b9f778c4b4 53 }
Sissors 4:a7b9f778c4b4 54
Sissors 4:a7b9f778c4b4 55 uint32_t FastPWM::getPeriod( void ) {
Sissors 4:a7b9f778c4b4 56 return pwm_obj->MR3;
Sissors 4:a7b9f778c4b4 57 }
Sissors 4:a7b9f778c4b4 58
Sissors 4:a7b9f778c4b4 59 uint32_t FastPWM::setPrescaler(uint32_t reqScale) {
Sissors 4:a7b9f778c4b4 60 //If 32-bit, disable auto-scaling, return 1
Sissors 4:a7b9f778c4b4 61 if (bits == 32) {
Sissors 4:a7b9f778c4b4 62 dynamicPrescaler = false;
Sissors 4:a7b9f778c4b4 63 return 1;
Sissors 4:a7b9f778c4b4 64 }
Sissors 4:a7b9f778c4b4 65
Sissors 4:a7b9f778c4b4 66 //Else 16-bit timer:
Sissors 4:a7b9f778c4b4 67 if (reqScale == 0)
Sissors 4:a7b9f778c4b4 68 //Return prescaler
Sissors 4:a7b9f778c4b4 69 return pwm_obj->PR + 1;
Sissors 4:a7b9f778c4b4 70 if (reqScale > (uint32_t)(1<<16))
Sissors 4:a7b9f778c4b4 71 reqScale = 1<<16;
Sissors 4:a7b9f778c4b4 72 //Else set prescaler, we have to substract one from reqScale since a 0 in PCVAL is prescaler of 1
Sissors 4:a7b9f778c4b4 73 pwm_obj->PR = reqScale - 1;
Sissors 4:a7b9f778c4b4 74
Sissors 4:a7b9f778c4b4 75 return reqScale;
Sissors 4:a7b9f778c4b4 76 }
Sissors 4:a7b9f778c4b4 77
Sissors 4:a7b9f778c4b4 78 #endif