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_KL25Z.cpp@4:a7b9f778c4b4, 2013-08-13 (annotated)
- Committer:
- Sissors
- Date:
- Tue Aug 13 16:54:06 2013 +0000
- Revision:
- 4:a7b9f778c4b4
v2.0
; Added KL25Z/LPC11u24 support
; Added prescalers
; Direct tick changes
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Sissors | 4:a7b9f778c4b4 | 1 | #ifdef TARGET_KL25Z |
| Sissors | 4:a7b9f778c4b4 | 2 | |
| Sissors | 4:a7b9f778c4b4 | 3 | #include "FastPWM.h" |
| Sissors | 4:a7b9f778c4b4 | 4 | |
| Sissors | 4:a7b9f778c4b4 | 5 | volatile uint32_t *TPM_SC; |
| Sissors | 4:a7b9f778c4b4 | 6 | |
| Sissors | 4:a7b9f778c4b4 | 7 | void FastPWM::initFastPWM( void ) { |
| Sissors | 4:a7b9f778c4b4 | 8 | bits = 16; |
| Sissors | 4:a7b9f778c4b4 | 9 | |
| Sissors | 4:a7b9f778c4b4 | 10 | //Yes this is ugly, yes I should feel bad about it |
| Sissors | 4:a7b9f778c4b4 | 11 | TPM_SC = _pwm.MOD - 2; |
| Sissors | 4:a7b9f778c4b4 | 12 | } |
| Sissors | 4:a7b9f778c4b4 | 13 | |
| Sissors | 4:a7b9f778c4b4 | 14 | void FastPWM::pulsewidth_ticks( uint32_t ticks ) { |
| Sissors | 4:a7b9f778c4b4 | 15 | *(_pwm.CnV) = ticks; |
| Sissors | 4:a7b9f778c4b4 | 16 | *_pwm.CNT = 0; //Not yet sure why this is needed! |
| Sissors | 4:a7b9f778c4b4 | 17 | } |
| Sissors | 4:a7b9f778c4b4 | 18 | |
| Sissors | 4:a7b9f778c4b4 | 19 | void FastPWM::period_ticks( uint32_t ticks ) { |
| Sissors | 4:a7b9f778c4b4 | 20 | *(_pwm.MOD) = ticks; |
| Sissors | 4:a7b9f778c4b4 | 21 | *_pwm.CNT = 0; |
| Sissors | 4:a7b9f778c4b4 | 22 | } |
| Sissors | 4:a7b9f778c4b4 | 23 | |
| Sissors | 4:a7b9f778c4b4 | 24 | uint32_t FastPWM::getPeriod( void ) { |
| Sissors | 4:a7b9f778c4b4 | 25 | return *(_pwm.MOD); |
| Sissors | 4:a7b9f778c4b4 | 26 | } |
| Sissors | 4:a7b9f778c4b4 | 27 | |
| Sissors | 4:a7b9f778c4b4 | 28 | uint32_t FastPWM::setPrescaler(uint32_t reqScale) { |
| Sissors | 4:a7b9f778c4b4 | 29 | const char prescalers[] = {1, 2, 4, 8, 16, 32, 64, 128}; |
| Sissors | 4:a7b9f778c4b4 | 30 | |
| Sissors | 4:a7b9f778c4b4 | 31 | //If prescaler is 0, return current one |
| Sissors | 4:a7b9f778c4b4 | 32 | if (reqScale == 0) |
| Sissors | 4:a7b9f778c4b4 | 33 | return (prescalers[(*TPM_SC) & 0x07]); |
| Sissors | 4:a7b9f778c4b4 | 34 | |
| Sissors | 4:a7b9f778c4b4 | 35 | uint32_t retval = 0; |
| Sissors | 4:a7b9f778c4b4 | 36 | char bin; |
| Sissors | 4:a7b9f778c4b4 | 37 | |
| Sissors | 4:a7b9f778c4b4 | 38 | for (bin = 0; bin<8; bin++) { |
| Sissors | 4:a7b9f778c4b4 | 39 | retval = prescalers[bin]; |
| Sissors | 4:a7b9f778c4b4 | 40 | if (retval >= reqScale) |
| Sissors | 4:a7b9f778c4b4 | 41 | break; |
| Sissors | 4:a7b9f778c4b4 | 42 | } |
| Sissors | 4:a7b9f778c4b4 | 43 | |
| Sissors | 4:a7b9f778c4b4 | 44 | //Clear lower 5 bits, write new value: |
| Sissors | 4:a7b9f778c4b4 | 45 | char clockbits = *TPM_SC & (3<<3); |
| Sissors | 4:a7b9f778c4b4 | 46 | |
| Sissors | 4:a7b9f778c4b4 | 47 | //For some reason clearing them takes some effort |
| Sissors | 4:a7b9f778c4b4 | 48 | while ((*TPM_SC & 0x1F) != 0) |
| Sissors | 4:a7b9f778c4b4 | 49 | *TPM_SC &= ~0x1F; |
| Sissors | 4:a7b9f778c4b4 | 50 | |
| Sissors | 4:a7b9f778c4b4 | 51 | |
| Sissors | 4:a7b9f778c4b4 | 52 | *TPM_SC |= bin + clockbits; |
| Sissors | 4:a7b9f778c4b4 | 53 | |
| Sissors | 4:a7b9f778c4b4 | 54 | return retval; |
| Sissors | 4:a7b9f778c4b4 | 55 | } |
| Sissors | 4:a7b9f778c4b4 | 56 | #endif |
