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@9:c50f688cad07, 2014-06-27 (annotated)
- Committer:
- Sissors
- Date:
- Fri Jun 27 06:02:37 2014 +0000
- Revision:
- 9:c50f688cad07
- Parent:
- 8:a7745c791b0c
Period of KLxx decreased by 1 before writing to make sure it is exactly correct for my beloved users.
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 | void FastPWM::initFastPWM( void ) { |
Sissors | 4:a7b9f778c4b4 | 6 | bits = 16; |
Sissors | 4:a7b9f778c4b4 | 7 | } |
Sissors | 4:a7b9f778c4b4 | 8 | |
Sissors | 4:a7b9f778c4b4 | 9 | void FastPWM::pulsewidth_ticks( uint32_t ticks ) { |
Sissors | 4:a7b9f778c4b4 | 10 | *(_pwm.CnV) = ticks; |
Sissors | 4:a7b9f778c4b4 | 11 | } |
Sissors | 4:a7b9f778c4b4 | 12 | |
Sissors | 4:a7b9f778c4b4 | 13 | void FastPWM::period_ticks( uint32_t ticks ) { |
Sissors | 9:c50f688cad07 | 14 | *(_pwm.MOD) = ticks - 1; |
Sissors | 4:a7b9f778c4b4 | 15 | } |
Sissors | 4:a7b9f778c4b4 | 16 | |
Sissors | 4:a7b9f778c4b4 | 17 | uint32_t FastPWM::getPeriod( void ) { |
Sissors | 9:c50f688cad07 | 18 | return *(_pwm.MOD) + 1; |
Sissors | 4:a7b9f778c4b4 | 19 | } |
Sissors | 4:a7b9f778c4b4 | 20 | |
Sissors | 4:a7b9f778c4b4 | 21 | uint32_t FastPWM::setPrescaler(uint32_t reqScale) { |
Sissors | 8:a7745c791b0c | 22 | |
Sissors | 8:a7745c791b0c | 23 | //Yes this is ugly, yes I should feel bad about it |
Sissors | 8:a7745c791b0c | 24 | volatile uint32_t *TPM_SC = _pwm.MOD - 2; |
Sissors | 8:a7745c791b0c | 25 | |
Sissors | 4:a7b9f778c4b4 | 26 | const char prescalers[] = {1, 2, 4, 8, 16, 32, 64, 128}; |
Sissors | 4:a7b9f778c4b4 | 27 | |
Sissors | 4:a7b9f778c4b4 | 28 | //If prescaler is 0, return current one |
Sissors | 4:a7b9f778c4b4 | 29 | if (reqScale == 0) |
Sissors | 4:a7b9f778c4b4 | 30 | return (prescalers[(*TPM_SC) & 0x07]); |
Sissors | 4:a7b9f778c4b4 | 31 | |
Sissors | 4:a7b9f778c4b4 | 32 | uint32_t retval = 0; |
Sissors | 4:a7b9f778c4b4 | 33 | char bin; |
Sissors | 4:a7b9f778c4b4 | 34 | |
Sissors | 4:a7b9f778c4b4 | 35 | for (bin = 0; bin<8; bin++) { |
Sissors | 4:a7b9f778c4b4 | 36 | retval = prescalers[bin]; |
Sissors | 4:a7b9f778c4b4 | 37 | if (retval >= reqScale) |
Sissors | 4:a7b9f778c4b4 | 38 | break; |
Sissors | 4:a7b9f778c4b4 | 39 | } |
Sissors | 4:a7b9f778c4b4 | 40 | |
Sissors | 4:a7b9f778c4b4 | 41 | //Clear lower 5 bits, write new value: |
Sissors | 4:a7b9f778c4b4 | 42 | char clockbits = *TPM_SC & (3<<3); |
Sissors | 4:a7b9f778c4b4 | 43 | |
Sissors | 4:a7b9f778c4b4 | 44 | //For some reason clearing them takes some effort |
Sissors | 4:a7b9f778c4b4 | 45 | while ((*TPM_SC & 0x1F) != 0) |
Sissors | 4:a7b9f778c4b4 | 46 | *TPM_SC &= ~0x1F; |
Sissors | 4:a7b9f778c4b4 | 47 | |
Sissors | 4:a7b9f778c4b4 | 48 | |
Sissors | 4:a7b9f778c4b4 | 49 | *TPM_SC |= bin + clockbits; |
Sissors | 4:a7b9f778c4b4 | 50 | |
Sissors | 4:a7b9f778c4b4 | 51 | return retval; |
Sissors | 4:a7b9f778c4b4 | 52 | } |
Sissors | 4:a7b9f778c4b4 | 53 | #endif |