Fork of Erik Olieman's FastPWM library created to add the targets I need.
Fork of FastPWM by
Device/FastPWM_KLXX_K20D50M.cpp@14:b30038fbba51, 2014-09-30 (annotated)
- Committer:
- Sissors
- Date:
- Tue Sep 30 19:00:27 2014 +0000
- Revision:
- 14:b30038fbba51
- Parent:
- 11:e0a8f0fcb1c9
Added KSDK support, probably smashed buggy on KLxx platform.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sissors | 11:e0a8f0fcb1c9 | 1 | #if defined(TARGET_KLXX) || defined(TARGET_K20D50M) |
Sissors | 11:e0a8f0fcb1c9 | 2 | |
Sissors | 11:e0a8f0fcb1c9 | 3 | #include "FastPWM.h" |
Sissors | 11:e0a8f0fcb1c9 | 4 | |
Sissors | 11:e0a8f0fcb1c9 | 5 | void FastPWM::initFastPWM( void ) { |
Sissors | 11:e0a8f0fcb1c9 | 6 | bits = 16; |
Sissors | 11:e0a8f0fcb1c9 | 7 | } |
Sissors | 11:e0a8f0fcb1c9 | 8 | |
Sissors | 11:e0a8f0fcb1c9 | 9 | void FastPWM::pulsewidth_ticks( uint32_t ticks ) { |
Sissors | 11:e0a8f0fcb1c9 | 10 | *(_pwm.CnV) = ticks; |
Sissors | 11:e0a8f0fcb1c9 | 11 | } |
Sissors | 11:e0a8f0fcb1c9 | 12 | |
Sissors | 11:e0a8f0fcb1c9 | 13 | void FastPWM::period_ticks( uint32_t ticks ) { |
Sissors | 11:e0a8f0fcb1c9 | 14 | *(_pwm.MOD) = ticks - 1; |
Sissors | 11:e0a8f0fcb1c9 | 15 | } |
Sissors | 11:e0a8f0fcb1c9 | 16 | |
Sissors | 11:e0a8f0fcb1c9 | 17 | uint32_t FastPWM::getPeriod( void ) { |
Sissors | 11:e0a8f0fcb1c9 | 18 | return *(_pwm.MOD) + 1; |
Sissors | 11:e0a8f0fcb1c9 | 19 | } |
Sissors | 11:e0a8f0fcb1c9 | 20 | |
Sissors | 11:e0a8f0fcb1c9 | 21 | uint32_t FastPWM::setPrescaler(uint32_t reqScale) { |
Sissors | 11:e0a8f0fcb1c9 | 22 | |
Sissors | 11:e0a8f0fcb1c9 | 23 | //Yes this is ugly, yes I should feel bad about it |
Sissors | 11:e0a8f0fcb1c9 | 24 | volatile uint32_t *TPM_SC = _pwm.MOD - 2; |
Sissors | 11:e0a8f0fcb1c9 | 25 | |
Sissors | 11:e0a8f0fcb1c9 | 26 | const char prescalers[] = {1, 2, 4, 8, 16, 32, 64, 128}; |
Sissors | 11:e0a8f0fcb1c9 | 27 | |
Sissors | 11:e0a8f0fcb1c9 | 28 | //If prescaler is 0, return current one |
Sissors | 11:e0a8f0fcb1c9 | 29 | if (reqScale == 0) |
Sissors | 11:e0a8f0fcb1c9 | 30 | return (prescalers[(*TPM_SC) & 0x07]); |
Sissors | 11:e0a8f0fcb1c9 | 31 | |
Sissors | 11:e0a8f0fcb1c9 | 32 | uint32_t retval = 0; |
Sissors | 11:e0a8f0fcb1c9 | 33 | char bin; |
Sissors | 11:e0a8f0fcb1c9 | 34 | |
Sissors | 11:e0a8f0fcb1c9 | 35 | for (bin = 0; bin<8; bin++) { |
Sissors | 11:e0a8f0fcb1c9 | 36 | retval = prescalers[bin]; |
Sissors | 11:e0a8f0fcb1c9 | 37 | if (retval >= reqScale) |
Sissors | 11:e0a8f0fcb1c9 | 38 | break; |
Sissors | 11:e0a8f0fcb1c9 | 39 | } |
Sissors | 14:b30038fbba51 | 40 | if (bin == 8) |
Sissors | 14:b30038fbba51 | 41 | bin = 7; |
Sissors | 11:e0a8f0fcb1c9 | 42 | |
Sissors | 11:e0a8f0fcb1c9 | 43 | //Clear lower 5 bits, write new value: |
Sissors | 11:e0a8f0fcb1c9 | 44 | char clockbits = *TPM_SC & (3<<3); |
Sissors | 11:e0a8f0fcb1c9 | 45 | |
Sissors | 11:e0a8f0fcb1c9 | 46 | //For some reason clearing them takes some effort |
Sissors | 11:e0a8f0fcb1c9 | 47 | while ((*TPM_SC & 0x1F) != 0) |
Sissors | 11:e0a8f0fcb1c9 | 48 | *TPM_SC &= ~0x1F; |
Sissors | 11:e0a8f0fcb1c9 | 49 | |
Sissors | 11:e0a8f0fcb1c9 | 50 | |
Sissors | 14:b30038fbba51 | 51 | *TPM_SC = bin + clockbits; |
Sissors | 11:e0a8f0fcb1c9 | 52 | |
Sissors | 11:e0a8f0fcb1c9 | 53 | return retval; |
Sissors | 11:e0a8f0fcb1c9 | 54 | } |
Sissors | 11:e0a8f0fcb1c9 | 55 | #endif |