Fork of Erik Olieman's FastPWM library created to add the targets I need.
Fork of FastPWM by
Device/FastPWM_LPC11U24.cpp@12:4600daab8a83, 2014-07-20 (annotated)
- Committer:
- Sissors
- Date:
- Sun Jul 20 12:49:21 2014 +0000
- Revision:
- 12:4600daab8a83
- Parent:
- 10:36362e9067b0
Preparation for Nucleo devices: Added option to have a device specific container with variables (such as pointer to match registers, since nucleo pwmout class lacks those).
; And practised on the LPC11u24 to implement it.
Who changed what in which revision?
User | Revision | Line number | New 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 | 12:4600daab8a83 | 5 | #define PWM_MR (*(((fastpwm_struct*)fast_obj)->MR)) |
Sissors | 12:4600daab8a83 | 6 | #define PWM_TIMER (((fastpwm_struct*)fast_obj)->timer) |
Sissors | 12:4600daab8a83 | 7 | |
Sissors | 12:4600daab8a83 | 8 | typedef struct { |
Sissors | 12:4600daab8a83 | 9 | __IO uint32_t *MR; |
Sissors | 12:4600daab8a83 | 10 | LPC_CTxxBx_Type *timer; |
Sissors | 12:4600daab8a83 | 11 | } fastpwm_struct; |
Sissors | 12:4600daab8a83 | 12 | |
Sissors | 4:a7b9f778c4b4 | 13 | typedef struct { |
Sissors | 4:a7b9f778c4b4 | 14 | uint8_t timer; |
Sissors | 4:a7b9f778c4b4 | 15 | uint8_t mr; |
Sissors | 4:a7b9f778c4b4 | 16 | } timer_mr; |
Sissors | 4:a7b9f778c4b4 | 17 | |
Sissors | 4:a7b9f778c4b4 | 18 | static timer_mr pwm_timer_map[11] = { |
Sissors | 4:a7b9f778c4b4 | 19 | {0, 0}, {0, 1}, {0, 2}, |
Sissors | 4:a7b9f778c4b4 | 20 | {1, 0}, {1, 1}, |
Sissors | 4:a7b9f778c4b4 | 21 | {2, 0}, {2, 1}, {2, 2}, |
Sissors | 4:a7b9f778c4b4 | 22 | {3, 0}, {3, 1}, {3, 2}, |
Sissors | 4:a7b9f778c4b4 | 23 | }; |
Sissors | 4:a7b9f778c4b4 | 24 | |
Sissors | 4:a7b9f778c4b4 | 25 | static LPC_CTxxBx_Type *Timers[4] = { |
Sissors | 4:a7b9f778c4b4 | 26 | LPC_CT16B0, LPC_CT16B1, |
Sissors | 4:a7b9f778c4b4 | 27 | LPC_CT32B0, LPC_CT32B1 |
Sissors | 4:a7b9f778c4b4 | 28 | }; |
Sissors | 4:a7b9f778c4b4 | 29 | |
Sissors | 4:a7b9f778c4b4 | 30 | |
Sissors | 4:a7b9f778c4b4 | 31 | void FastPWM::initFastPWM( void ) { |
Sissors | 12:4600daab8a83 | 32 | fast_obj = new fastpwm_struct; |
Sissors | 12:4600daab8a83 | 33 | timer_mr tid = pwm_timer_map[_pwm.pwm]; |
Sissors | 12:4600daab8a83 | 34 | PWM_TIMER = Timers[tid.timer]; |
Sissors | 12:4600daab8a83 | 35 | (((fastpwm_struct*)fast_obj)->MR) = &PWM_TIMER->MR[tid.mr]; |
Sissors | 4:a7b9f778c4b4 | 36 | |
Sissors | 4:a7b9f778c4b4 | 37 | if (tid.timer < 2) |
Sissors | 4:a7b9f778c4b4 | 38 | //16-bit timer |
Sissors | 4:a7b9f778c4b4 | 39 | bits = 16; |
Sissors | 4:a7b9f778c4b4 | 40 | else |
Sissors | 4:a7b9f778c4b4 | 41 | //32-bit timer |
Sissors | 7:1b5df740bcd7 | 42 | bits = 32; |
Sissors | 4:a7b9f778c4b4 | 43 | } |
Sissors | 4:a7b9f778c4b4 | 44 | |
Sissors | 4:a7b9f778c4b4 | 45 | void FastPWM::pulsewidth_ticks( uint32_t ticks ) { |
Sissors | 7:1b5df740bcd7 | 46 | if (ticks) |
Sissors | 12:4600daab8a83 | 47 | PWM_MR = PWM_TIMER->MR3 - ticks; //They inverted PWM on the 11u24 |
Sissors | 7:1b5df740bcd7 | 48 | else |
Sissors | 12:4600daab8a83 | 49 | PWM_MR = 0xFFFFFFFF; //If MR3 = ticks 1 clock cycle wide errors appear, this prevents that (unless MR3 = max). |
Sissors | 4:a7b9f778c4b4 | 50 | } |
Sissors | 4:a7b9f778c4b4 | 51 | |
Sissors | 12:4600daab8a83 | 52 | void FastPWM::period_ticks( uint32_t ticks ) { |
Sissors | 12:4600daab8a83 | 53 | PWM_TIMER->TCR = 0x02; |
Sissors | 12:4600daab8a83 | 54 | PWM_TIMER->MR3 = ticks; |
Sissors | 12:4600daab8a83 | 55 | PWM_TIMER->TCR = 0x01; |
Sissors | 4:a7b9f778c4b4 | 56 | } |
Sissors | 4:a7b9f778c4b4 | 57 | |
Sissors | 4:a7b9f778c4b4 | 58 | uint32_t FastPWM::getPeriod( void ) { |
Sissors | 12:4600daab8a83 | 59 | return PWM_TIMER->MR3; |
Sissors | 4:a7b9f778c4b4 | 60 | } |
Sissors | 4:a7b9f778c4b4 | 61 | |
Sissors | 4:a7b9f778c4b4 | 62 | uint32_t FastPWM::setPrescaler(uint32_t reqScale) { |
Sissors | 4:a7b9f778c4b4 | 63 | //If 32-bit, disable auto-scaling, return 1 |
Sissors | 4:a7b9f778c4b4 | 64 | if (bits == 32) { |
Sissors | 4:a7b9f778c4b4 | 65 | dynamicPrescaler = false; |
Sissors | 4:a7b9f778c4b4 | 66 | return 1; |
Sissors | 4:a7b9f778c4b4 | 67 | } |
Sissors | 4:a7b9f778c4b4 | 68 | |
Sissors | 4:a7b9f778c4b4 | 69 | //Else 16-bit timer: |
Sissors | 4:a7b9f778c4b4 | 70 | if (reqScale == 0) |
Sissors | 4:a7b9f778c4b4 | 71 | //Return prescaler |
Sissors | 12:4600daab8a83 | 72 | return PWM_TIMER->PR + 1; |
Sissors | 4:a7b9f778c4b4 | 73 | if (reqScale > (uint32_t)(1<<16)) |
Sissors | 4:a7b9f778c4b4 | 74 | reqScale = 1<<16; |
Sissors | 4:a7b9f778c4b4 | 75 | //Else set prescaler, we have to substract one from reqScale since a 0 in PCVAL is prescaler of 1 |
Sissors | 12:4600daab8a83 | 76 | PWM_TIMER->PR = reqScale - 1; |
Sissors | 4:a7b9f778c4b4 | 77 | |
Sissors | 4:a7b9f778c4b4 | 78 | return reqScale; |
Sissors | 4:a7b9f778c4b4 | 79 | } |
Sissors | 4:a7b9f778c4b4 | 80 | |
Sissors | 4:a7b9f778c4b4 | 81 | #endif |