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