Fork of Erik Olieman's FastPWM library created to add the targets I need.
Fork of FastPWM by
Device/FastPWM_KSDK.cpp@14:b30038fbba51, 2014-09-30 (annotated)
- Committer:
- Sissors
- Date:
- Tue Sep 30 19:00:27 2014 +0000
- Revision:
- 14:b30038fbba51
Added KSDK support, probably smashed buggy on KLxx platform.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sissors | 14:b30038fbba51 | 1 | #if defined(TARGET_KPSDK_MCUS) |
Sissors | 14:b30038fbba51 | 2 | |
Sissors | 14:b30038fbba51 | 3 | #include "FastPWM.h" |
Sissors | 14:b30038fbba51 | 4 | #include "fsl_clock_manager.h" |
Sissors | 14:b30038fbba51 | 5 | |
Sissors | 14:b30038fbba51 | 6 | |
Sissors | 14:b30038fbba51 | 7 | #define PWM_CNV (*(((fastpwm_struct*)fast_obj)->CnV)) |
Sissors | 14:b30038fbba51 | 8 | #define PWM_MOD (*(((fastpwm_struct*)fast_obj)->MOD)) |
Sissors | 14:b30038fbba51 | 9 | #define PWM_SC (*(((fastpwm_struct*)fast_obj)->SC)) |
Sissors | 14:b30038fbba51 | 10 | |
Sissors | 14:b30038fbba51 | 11 | typedef struct { |
Sissors | 14:b30038fbba51 | 12 | __IO uint32_t *CnV; |
Sissors | 14:b30038fbba51 | 13 | __IO uint32_t *MOD; |
Sissors | 14:b30038fbba51 | 14 | __IO uint32_t *SC; |
Sissors | 14:b30038fbba51 | 15 | } fastpwm_struct; |
Sissors | 14:b30038fbba51 | 16 | |
Sissors | 14:b30038fbba51 | 17 | static uint32_t pwm_prescaler; |
Sissors | 14:b30038fbba51 | 18 | |
Sissors | 14:b30038fbba51 | 19 | void FastPWM::initFastPWM( void ) { |
Sissors | 14:b30038fbba51 | 20 | fast_obj = new fastpwm_struct; |
Sissors | 14:b30038fbba51 | 21 | bits = 16; |
Sissors | 14:b30038fbba51 | 22 | |
Sissors | 14:b30038fbba51 | 23 | uint32_t pwm_base_clock; |
Sissors | 14:b30038fbba51 | 24 | CLOCK_SYS_GetFreq(kBusClock, &pwm_base_clock); |
Sissors | 14:b30038fbba51 | 25 | pwm_prescaler = SystemCoreClock / pwm_base_clock; |
Sissors | 14:b30038fbba51 | 26 | |
Sissors | 14:b30038fbba51 | 27 | uint32_t ftms[] = FTM_BASE_ADDRS; |
Sissors | 14:b30038fbba51 | 28 | unsigned int ch_n = (_pwm.pwm_name & 0xFF); |
Sissors | 14:b30038fbba51 | 29 | FTM_Type *ftm = (FTM_Type *)ftms[_pwm.pwm_name >> TPM_SHIFT]; |
Sissors | 14:b30038fbba51 | 30 | |
Sissors | 14:b30038fbba51 | 31 | ((fastpwm_struct*)fast_obj)->CnV = &ftm->CONTROLS[ch_n].CnV; |
Sissors | 14:b30038fbba51 | 32 | ((fastpwm_struct*)fast_obj)->MOD = &ftm->MOD; |
Sissors | 14:b30038fbba51 | 33 | ((fastpwm_struct*)fast_obj)->SC = &ftm->SC; |
Sissors | 14:b30038fbba51 | 34 | } |
Sissors | 14:b30038fbba51 | 35 | |
Sissors | 14:b30038fbba51 | 36 | void FastPWM::pulsewidth_ticks( uint32_t ticks ) { |
Sissors | 14:b30038fbba51 | 37 | PWM_CNV = ticks; |
Sissors | 14:b30038fbba51 | 38 | } |
Sissors | 14:b30038fbba51 | 39 | |
Sissors | 14:b30038fbba51 | 40 | void FastPWM::period_ticks( uint32_t ticks ) { |
Sissors | 14:b30038fbba51 | 41 | PWM_MOD = ticks - 1; |
Sissors | 14:b30038fbba51 | 42 | } |
Sissors | 14:b30038fbba51 | 43 | |
Sissors | 14:b30038fbba51 | 44 | uint32_t FastPWM::getPeriod( void ) { |
Sissors | 14:b30038fbba51 | 45 | return PWM_MOD + 1; |
Sissors | 14:b30038fbba51 | 46 | } |
Sissors | 14:b30038fbba51 | 47 | |
Sissors | 14:b30038fbba51 | 48 | uint32_t FastPWM::setPrescaler(uint32_t reqScale) { |
Sissors | 14:b30038fbba51 | 49 | |
Sissors | 14:b30038fbba51 | 50 | uint32_t prescalers[] = {1, 2, 4, 8, 16, 32, 64, 128}; |
Sissors | 14:b30038fbba51 | 51 | |
Sissors | 14:b30038fbba51 | 52 | for (int i = 0; i<8; i++) |
Sissors | 14:b30038fbba51 | 53 | prescalers[i] = prescalers[i] * pwm_prescaler; |
Sissors | 14:b30038fbba51 | 54 | |
Sissors | 14:b30038fbba51 | 55 | //If prescaler is 0, return current one |
Sissors | 14:b30038fbba51 | 56 | if (reqScale == 0) |
Sissors | 14:b30038fbba51 | 57 | return (prescalers[(PWM_SC) & 0x07]); |
Sissors | 14:b30038fbba51 | 58 | |
Sissors | 14:b30038fbba51 | 59 | uint32_t retval = 0; |
Sissors | 14:b30038fbba51 | 60 | char bin; |
Sissors | 14:b30038fbba51 | 61 | |
Sissors | 14:b30038fbba51 | 62 | for (bin = 0; bin<8; bin++) { |
Sissors | 14:b30038fbba51 | 63 | retval = prescalers[bin]; |
Sissors | 14:b30038fbba51 | 64 | if (retval >= reqScale) |
Sissors | 14:b30038fbba51 | 65 | break; |
Sissors | 14:b30038fbba51 | 66 | } |
Sissors | 14:b30038fbba51 | 67 | if (bin == 8) |
Sissors | 14:b30038fbba51 | 68 | bin = 7; |
Sissors | 14:b30038fbba51 | 69 | |
Sissors | 14:b30038fbba51 | 70 | //Clear lower 5 bits, write new value: |
Sissors | 14:b30038fbba51 | 71 | char clockbits = PWM_SC & (3<<3); |
Sissors | 14:b30038fbba51 | 72 | |
Sissors | 14:b30038fbba51 | 73 | //For some reason clearing them takes some effort |
Sissors | 14:b30038fbba51 | 74 | while ((PWM_SC & 0x1F) != 0) |
Sissors | 14:b30038fbba51 | 75 | PWM_SC &= ~0x1F; |
Sissors | 14:b30038fbba51 | 76 | |
Sissors | 14:b30038fbba51 | 77 | |
Sissors | 14:b30038fbba51 | 78 | PWM_SC = bin + clockbits; |
Sissors | 14:b30038fbba51 | 79 | |
Sissors | 14:b30038fbba51 | 80 | return retval; |
Sissors | 14:b30038fbba51 | 81 | } |
Sissors | 14:b30038fbba51 | 82 | #endif |