Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of FastPWM by
Device/FastPWM_KLXX_K20D50M.cpp@11:e0a8f0fcb1c9, 2014-07-16 (annotated)
- Committer:
- Sissors
- Date:
- Wed Jul 16 15:44:21 2014 +0000
- Revision:
- 11:e0a8f0fcb1c9
- Child:
- 14:b30038fbba51
K20D50M added
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 | 11:e0a8f0fcb1c9 | 40 | |
Sissors | 11:e0a8f0fcb1c9 | 41 | //Clear lower 5 bits, write new value: |
Sissors | 11:e0a8f0fcb1c9 | 42 | char clockbits = *TPM_SC & (3<<3); |
Sissors | 11:e0a8f0fcb1c9 | 43 | |
Sissors | 11:e0a8f0fcb1c9 | 44 | //For some reason clearing them takes some effort |
Sissors | 11:e0a8f0fcb1c9 | 45 | while ((*TPM_SC & 0x1F) != 0) |
Sissors | 11:e0a8f0fcb1c9 | 46 | *TPM_SC &= ~0x1F; |
Sissors | 11:e0a8f0fcb1c9 | 47 | |
Sissors | 11:e0a8f0fcb1c9 | 48 | |
Sissors | 11:e0a8f0fcb1c9 | 49 | *TPM_SC |= bin + clockbits; |
Sissors | 11:e0a8f0fcb1c9 | 50 | |
Sissors | 11:e0a8f0fcb1c9 | 51 | return retval; |
Sissors | 11:e0a8f0fcb1c9 | 52 | } |
Sissors | 11:e0a8f0fcb1c9 | 53 | #endif |