Library that allows for higher resolution and speed than standard mbed PWM library using same syntax (drop-in replacement).

Dependents:   PwmOscillator FastStepDriver TLC5940 CameraTest ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FastPWM_KSDK.cpp Source File

FastPWM_KSDK.cpp

00001 #if defined(TARGET_KPSDK_MCUS)
00002 
00003 #include "FastPWM.h"
00004 #include "fsl_ftm.h"
00005 
00006 
00007 #define PWM_CNV              (*(((fastpwm_struct*)fast_obj)->CnV))  
00008 #define PWM_MOD              (*(((fastpwm_struct*)fast_obj)->MOD))  
00009 #define PWM_SC               (*(((fastpwm_struct*)fast_obj)->SC))  
00010 #define PWM_SYNC             (*(((fastpwm_struct*)fast_obj)->SYNC))  
00011 
00012 typedef struct  {
00013     __IO uint32_t *CnV;
00014     __IO uint32_t *MOD;
00015     __IO uint32_t *SC;
00016     __IO uint32_t *SYNC;
00017 } fastpwm_struct;
00018 
00019 static uint32_t pwm_prescaler;
00020 static FTM_Type *const ftm_addrs[] = FTM_BASE_PTRS;
00021 
00022 void FastPWM::initFastPWM( void ) {
00023     fast_obj = malloc(sizeof(fastpwm_struct));
00024     bits = 16;
00025 
00026     pwm_prescaler = SystemCoreClock / CLOCK_GetFreq(kCLOCK_BusClk);;
00027 
00028     unsigned int ch_n = (_pwm.pwm_name & 0xF);
00029     FTM_Type *ftm = ftm_addrs[_pwm.pwm_name >> TPM_SHIFT];
00030     
00031     ((fastpwm_struct*)fast_obj)->CnV = &ftm->CONTROLS[ch_n].CnV;
00032     ((fastpwm_struct*)fast_obj)->MOD = &ftm->MOD;
00033     ((fastpwm_struct*)fast_obj)->SC = &ftm->SC;
00034     ((fastpwm_struct*)fast_obj)->SYNC = &ftm->SYNC;
00035     
00036     //Do not clear counter when writing new value, set end of period as loading value
00037     ftm->SYNCONF &= ~FTM_SYNCONF_SWRSTCNT_MASK;
00038     ftm->SYNC |= FTM_SYNC_CNTMAX_MASK;
00039 }
00040 
00041 void FastPWM::pulsewidth_ticks( uint32_t ticks ) {
00042     PWM_CNV = ticks;
00043     PWM_SYNC |= FTM_SYNC_SWSYNC_MASK;
00044 }
00045 
00046 void FastPWM::period_ticks( uint32_t ticks ) {
00047     PWM_MOD = ticks - 1;
00048     PWM_SYNC |= FTM_SYNC_SWSYNC_MASK;  
00049 }
00050 
00051 uint32_t FastPWM::getPeriod( void ) {
00052     return PWM_MOD + 1;
00053 }
00054 
00055 uint32_t FastPWM::setPrescaler(uint32_t reqScale) {
00056  
00057     uint32_t prescalers[] = {1, 2, 4, 8, 16, 32, 64, 128};
00058     
00059     for (int i = 0; i<8; i++)
00060          prescalers[i] = prescalers[i] * pwm_prescaler;
00061     
00062     //If prescaler is 0, return current one
00063     if (reqScale == 0)
00064         return (prescalers[(PWM_SC) & 0x07]);
00065     
00066     uint32_t retval = 0;
00067     char bin;
00068     
00069     for (bin = 0; bin<8; bin++) {
00070         retval = prescalers[bin];
00071         if (retval >= reqScale)
00072             break;
00073     }
00074     if (bin == 8)
00075         bin = 7;
00076     
00077     //Clear lower 5 bits, write new value:
00078     char clockbits = PWM_SC & (3<<3);
00079     
00080     //For some reason clearing them takes some effort
00081     while ((PWM_SC & 0x1F) != 0)
00082         PWM_SC &= ~0x1F;
00083         
00084     
00085     PWM_SC = bin + clockbits;
00086     return retval;   
00087 }
00088 #endif