Potmeter FastPWM.h

Dependencies:   mbed

Committer:
vd
Date:
Tue Sep 19 16:52:01 2017 +0000
Revision:
0:16be67f4d9ac
Potmeter FastPWM.h

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vd 0:16be67f4d9ac 1 #if defined(TARGET_KPSDK_MCUS)
vd 0:16be67f4d9ac 2
vd 0:16be67f4d9ac 3 #include "FastPWM.h"
vd 0:16be67f4d9ac 4 #include "fsl_ftm.h"
vd 0:16be67f4d9ac 5
vd 0:16be67f4d9ac 6
vd 0:16be67f4d9ac 7 #define PWM_CNV (*(((fastpwm_struct*)fast_obj)->CnV))
vd 0:16be67f4d9ac 8 #define PWM_MOD (*(((fastpwm_struct*)fast_obj)->MOD))
vd 0:16be67f4d9ac 9 #define PWM_SC (*(((fastpwm_struct*)fast_obj)->SC))
vd 0:16be67f4d9ac 10 #define PWM_SYNC (*(((fastpwm_struct*)fast_obj)->SYNC))
vd 0:16be67f4d9ac 11
vd 0:16be67f4d9ac 12 typedef struct {
vd 0:16be67f4d9ac 13 __IO uint32_t *CnV;
vd 0:16be67f4d9ac 14 __IO uint32_t *MOD;
vd 0:16be67f4d9ac 15 __IO uint32_t *SC;
vd 0:16be67f4d9ac 16 __IO uint32_t *SYNC;
vd 0:16be67f4d9ac 17 } fastpwm_struct;
vd 0:16be67f4d9ac 18
vd 0:16be67f4d9ac 19 static uint32_t pwm_prescaler;
vd 0:16be67f4d9ac 20 static FTM_Type *const ftm_addrs[] = FTM_BASE_PTRS;
vd 0:16be67f4d9ac 21
vd 0:16be67f4d9ac 22 void FastPWM::initFastPWM( void ) {
vd 0:16be67f4d9ac 23 fast_obj = new fastpwm_struct;
vd 0:16be67f4d9ac 24 bits = 16;
vd 0:16be67f4d9ac 25
vd 0:16be67f4d9ac 26 pwm_prescaler = SystemCoreClock / CLOCK_GetFreq(kCLOCK_BusClk);;
vd 0:16be67f4d9ac 27
vd 0:16be67f4d9ac 28 unsigned int ch_n = (_pwm.pwm_name & 0xF);
vd 0:16be67f4d9ac 29 FTM_Type *ftm = ftm_addrs[_pwm.pwm_name >> TPM_SHIFT];
vd 0:16be67f4d9ac 30
vd 0:16be67f4d9ac 31 ((fastpwm_struct*)fast_obj)->CnV = &ftm->CONTROLS[ch_n].CnV;
vd 0:16be67f4d9ac 32 ((fastpwm_struct*)fast_obj)->MOD = &ftm->MOD;
vd 0:16be67f4d9ac 33 ((fastpwm_struct*)fast_obj)->SC = &ftm->SC;
vd 0:16be67f4d9ac 34 ((fastpwm_struct*)fast_obj)->SYNC = &ftm->SYNC;
vd 0:16be67f4d9ac 35
vd 0:16be67f4d9ac 36 //Do not clear counter when writing new value, set end of period as loading value
vd 0:16be67f4d9ac 37 ftm->SYNCONF &= ~FTM_SYNCONF_SWRSTCNT_MASK;
vd 0:16be67f4d9ac 38 ftm->SYNC |= FTM_SYNC_CNTMAX_MASK;
vd 0:16be67f4d9ac 39 }
vd 0:16be67f4d9ac 40
vd 0:16be67f4d9ac 41 void FastPWM::pulsewidth_ticks( uint32_t ticks ) {
vd 0:16be67f4d9ac 42 PWM_CNV = ticks;
vd 0:16be67f4d9ac 43 PWM_SYNC |= FTM_SYNC_SWSYNC_MASK;
vd 0:16be67f4d9ac 44 }
vd 0:16be67f4d9ac 45
vd 0:16be67f4d9ac 46 void FastPWM::period_ticks( uint32_t ticks ) {
vd 0:16be67f4d9ac 47 PWM_MOD = ticks - 1;
vd 0:16be67f4d9ac 48 PWM_SYNC |= FTM_SYNC_SWSYNC_MASK;
vd 0:16be67f4d9ac 49 }
vd 0:16be67f4d9ac 50
vd 0:16be67f4d9ac 51 uint32_t FastPWM::getPeriod( void ) {
vd 0:16be67f4d9ac 52 return PWM_MOD + 1;
vd 0:16be67f4d9ac 53 }
vd 0:16be67f4d9ac 54
vd 0:16be67f4d9ac 55 uint32_t FastPWM::setPrescaler(uint32_t reqScale) {
vd 0:16be67f4d9ac 56
vd 0:16be67f4d9ac 57 uint32_t prescalers[] = {1, 2, 4, 8, 16, 32, 64, 128};
vd 0:16be67f4d9ac 58
vd 0:16be67f4d9ac 59 for (int i = 0; i<8; i++)
vd 0:16be67f4d9ac 60 prescalers[i] = prescalers[i] * pwm_prescaler;
vd 0:16be67f4d9ac 61
vd 0:16be67f4d9ac 62 //If prescaler is 0, return current one
vd 0:16be67f4d9ac 63 if (reqScale == 0)
vd 0:16be67f4d9ac 64 return (prescalers[(PWM_SC) & 0x07]);
vd 0:16be67f4d9ac 65
vd 0:16be67f4d9ac 66 uint32_t retval = 0;
vd 0:16be67f4d9ac 67 char bin;
vd 0:16be67f4d9ac 68
vd 0:16be67f4d9ac 69 for (bin = 0; bin<8; bin++) {
vd 0:16be67f4d9ac 70 retval = prescalers[bin];
vd 0:16be67f4d9ac 71 if (retval >= reqScale)
vd 0:16be67f4d9ac 72 break;
vd 0:16be67f4d9ac 73 }
vd 0:16be67f4d9ac 74 if (bin == 8)
vd 0:16be67f4d9ac 75 bin = 7;
vd 0:16be67f4d9ac 76
vd 0:16be67f4d9ac 77 //Clear lower 5 bits, write new value:
vd 0:16be67f4d9ac 78 char clockbits = PWM_SC & (3<<3);
vd 0:16be67f4d9ac 79
vd 0:16be67f4d9ac 80 //For some reason clearing them takes some effort
vd 0:16be67f4d9ac 81 while ((PWM_SC & 0x1F) != 0)
vd 0:16be67f4d9ac 82 PWM_SC &= ~0x1F;
vd 0:16be67f4d9ac 83
vd 0:16be67f4d9ac 84
vd 0:16be67f4d9ac 85 PWM_SC = bin + clockbits;
vd 0:16be67f4d9ac 86 return retval;
vd 0:16be67f4d9ac 87 }
vd 0:16be67f4d9ac 88 #endif