My forked repository. DISCO_F407VG, DISCO_F303VC, DISCO_F051R8 and DISCO_F100RB maybe added.
Dependents: FastPWM-DISCO-test
Fork of FastPWM by
Device/FastPWM_KLXX.cpp@5:2812f0a115f7, 2014-02-28 (annotated)
- Committer:
- Sissors
- Date:
- Fri Feb 28 18:18:52 2014 +0000
- Revision:
- 5:2812f0a115f7
- Parent:
- Device/FastPWM_KL25Z.cpp@4:a7b9f778c4b4
- Child:
- 6:0f57969697b6
KL25Z renamed to KLXX, should work fine for all KLXX devices.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sissors | 5:2812f0a115f7 | 1 | #ifdef TARGET_KLXX |
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 |