changed for STM32F4
Fork of FastPWM by
Device/FastPWM_LPC11U24.cpp@10:36362e9067b0, 2014-06-27 (annotated)
- Committer:
- Sissors
- Date:
- Fri Jun 27 06:18:23 2014 +0000
- Revision:
- 10:36362e9067b0
- Parent:
- 7:1b5df740bcd7
- Child:
- 12:4600daab8a83
Same error as for the KLxx removed when multiple FastPWMs are used. Contrary to KLxx this gives a bit more overhead, but not much I can do about it while keeping the lib tidy
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 | 4:a7b9f778c4b4 | 5 | typedef struct { |
Sissors | 4:a7b9f778c4b4 | 6 | uint8_t timer; |
Sissors | 4:a7b9f778c4b4 | 7 | uint8_t mr; |
Sissors | 4:a7b9f778c4b4 | 8 | } timer_mr; |
Sissors | 10:36362e9067b0 | 9 | |
Sissors | 10:36362e9067b0 | 10 | LPC_CTxxBx_Type *pwm_obj; |
Sissors | 10:36362e9067b0 | 11 | timer_mr tid; |
Sissors | 4:a7b9f778c4b4 | 12 | |
Sissors | 4:a7b9f778c4b4 | 13 | static timer_mr pwm_timer_map[11] = { |
Sissors | 4:a7b9f778c4b4 | 14 | {0, 0}, {0, 1}, {0, 2}, |
Sissors | 4:a7b9f778c4b4 | 15 | {1, 0}, {1, 1}, |
Sissors | 4:a7b9f778c4b4 | 16 | {2, 0}, {2, 1}, {2, 2}, |
Sissors | 4:a7b9f778c4b4 | 17 | {3, 0}, {3, 1}, {3, 2}, |
Sissors | 4:a7b9f778c4b4 | 18 | }; |
Sissors | 4:a7b9f778c4b4 | 19 | |
Sissors | 4:a7b9f778c4b4 | 20 | static LPC_CTxxBx_Type *Timers[4] = { |
Sissors | 4:a7b9f778c4b4 | 21 | LPC_CT16B0, LPC_CT16B1, |
Sissors | 4:a7b9f778c4b4 | 22 | LPC_CT32B0, LPC_CT32B1 |
Sissors | 4:a7b9f778c4b4 | 23 | }; |
Sissors | 4:a7b9f778c4b4 | 24 | |
Sissors | 4:a7b9f778c4b4 | 25 | |
Sissors | 4:a7b9f778c4b4 | 26 | void FastPWM::initFastPWM( void ) { |
Sissors | 10:36362e9067b0 | 27 | tid = pwm_timer_map[_pwm.pwm]; |
Sissors | 4:a7b9f778c4b4 | 28 | pwm_obj = Timers[tid.timer]; |
Sissors | 4:a7b9f778c4b4 | 29 | |
Sissors | 4:a7b9f778c4b4 | 30 | if (tid.timer < 2) |
Sissors | 4:a7b9f778c4b4 | 31 | //16-bit timer |
Sissors | 4:a7b9f778c4b4 | 32 | bits = 16; |
Sissors | 4:a7b9f778c4b4 | 33 | else |
Sissors | 4:a7b9f778c4b4 | 34 | //32-bit timer |
Sissors | 7:1b5df740bcd7 | 35 | bits = 32; |
Sissors | 4:a7b9f778c4b4 | 36 | } |
Sissors | 4:a7b9f778c4b4 | 37 | |
Sissors | 4:a7b9f778c4b4 | 38 | void FastPWM::pulsewidth_ticks( uint32_t ticks ) { |
Sissors | 10:36362e9067b0 | 39 | tid = pwm_timer_map[_pwm.pwm]; |
Sissors | 10:36362e9067b0 | 40 | pwm_obj = Timers[tid.timer]; |
Sissors | 10:36362e9067b0 | 41 | |
Sissors | 7:1b5df740bcd7 | 42 | if (ticks) |
Sissors | 10:36362e9067b0 | 43 | pwm_obj->MR[tid.mr] = pwm_obj->MR3 - ticks; //They inverted PWM on the 11u24 |
Sissors | 7:1b5df740bcd7 | 44 | else |
Sissors | 10:36362e9067b0 | 45 | pwm_obj->MR[tid.mr] = 0xFFFFFFFF; //If MR3 = ticks 1 clock cycle wide errors appear, this prevents that (unless MR3 = max). |
Sissors | 4:a7b9f778c4b4 | 46 | } |
Sissors | 4:a7b9f778c4b4 | 47 | |
Sissors | 4:a7b9f778c4b4 | 48 | void FastPWM::period_ticks( uint32_t ticks ) { |
Sissors | 10:36362e9067b0 | 49 | tid = pwm_timer_map[_pwm.pwm]; |
Sissors | 10:36362e9067b0 | 50 | pwm_obj = Timers[tid.timer]; |
Sissors | 10:36362e9067b0 | 51 | |
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 | 10:36362e9067b0 | 58 | tid = pwm_timer_map[_pwm.pwm]; |
Sissors | 10:36362e9067b0 | 59 | pwm_obj = Timers[tid.timer]; |
Sissors | 10:36362e9067b0 | 60 | |
Sissors | 4:a7b9f778c4b4 | 61 | return pwm_obj->MR3; |
Sissors | 4:a7b9f778c4b4 | 62 | } |
Sissors | 4:a7b9f778c4b4 | 63 | |
Sissors | 4:a7b9f778c4b4 | 64 | uint32_t FastPWM::setPrescaler(uint32_t reqScale) { |
Sissors | 10:36362e9067b0 | 65 | tid = pwm_timer_map[_pwm.pwm]; |
Sissors | 10:36362e9067b0 | 66 | pwm_obj = Timers[tid.timer]; |
Sissors | 10:36362e9067b0 | 67 | |
Sissors | 4:a7b9f778c4b4 | 68 | //If 32-bit, disable auto-scaling, return 1 |
Sissors | 4:a7b9f778c4b4 | 69 | if (bits == 32) { |
Sissors | 4:a7b9f778c4b4 | 70 | dynamicPrescaler = false; |
Sissors | 4:a7b9f778c4b4 | 71 | return 1; |
Sissors | 4:a7b9f778c4b4 | 72 | } |
Sissors | 4:a7b9f778c4b4 | 73 | |
Sissors | 4:a7b9f778c4b4 | 74 | //Else 16-bit timer: |
Sissors | 4:a7b9f778c4b4 | 75 | if (reqScale == 0) |
Sissors | 4:a7b9f778c4b4 | 76 | //Return prescaler |
Sissors | 4:a7b9f778c4b4 | 77 | return pwm_obj->PR + 1; |
Sissors | 4:a7b9f778c4b4 | 78 | if (reqScale > (uint32_t)(1<<16)) |
Sissors | 4:a7b9f778c4b4 | 79 | reqScale = 1<<16; |
Sissors | 4:a7b9f778c4b4 | 80 | //Else set prescaler, we have to substract one from reqScale since a 0 in PCVAL is prescaler of 1 |
Sissors | 4:a7b9f778c4b4 | 81 | pwm_obj->PR = reqScale - 1; |
Sissors | 4:a7b9f778c4b4 | 82 | |
Sissors | 4:a7b9f778c4b4 | 83 | return reqScale; |
Sissors | 4:a7b9f778c4b4 | 84 | } |
Sissors | 4:a7b9f778c4b4 | 85 | |
Sissors | 4:a7b9f778c4b4 | 86 | #endif |