Ley de Ohm

Fork of FastPWM by Erik -

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 
00011 typedef struct  {
00012     __IO uint32_t *CnV;
00013     __IO uint32_t *MOD;
00014     __IO uint32_t *SC;
00015 } fastpwm_struct;
00016 
00017 static uint32_t pwm_prescaler;
00018 static FTM_Type *const ftm_addrs[] = FTM_BASE_PTRS;
00019 
00020 void FastPWM::initFastPWM( void ) {
00021     fast_obj = new fastpwm_struct;
00022     bits = 16;
00023 
00024     pwm_prescaler = SystemCoreClock / CLOCK_GetFreq(kCLOCK_BusClk);;
00025 
00026     unsigned int ch_n = (_pwm.pwm_name & 0xF);
00027     FTM_Type *ftm = ftm_addrs[_pwm.pwm_name >> TPM_SHIFT];
00028     
00029     ((fastpwm_struct*)fast_obj)->CnV = &ftm->CONTROLS[ch_n].CnV;
00030     ((fastpwm_struct*)fast_obj)->MOD = &ftm->MOD;
00031     ((fastpwm_struct*)fast_obj)->SC = &ftm->SC;
00032 }
00033 
00034 void FastPWM::pulsewidth_ticks( uint32_t ticks ) {
00035     PWM_CNV = ticks;
00036     
00037     //Temporary work around until I figure out which settings mbed screwed up in this update
00038     FTM_Type *ftm = ftm_addrs[_pwm.pwm_name >> TPM_SHIFT];
00039     FTM_SetSoftwareTrigger(ftm, true);
00040 }
00041 
00042 void FastPWM::period_ticks( uint32_t ticks ) {
00043     PWM_MOD = ticks - 1;
00044 }
00045 
00046 uint32_t FastPWM::getPeriod( void ) {
00047     return PWM_MOD + 1;
00048 }
00049 
00050 uint32_t FastPWM::setPrescaler(uint32_t reqScale) {
00051  
00052     uint32_t prescalers[] = {1, 2, 4, 8, 16, 32, 64, 128};
00053     
00054     for (int i = 0; i<8; i++)
00055          prescalers[i] = prescalers[i] * pwm_prescaler;
00056     
00057     //If prescaler is 0, return current one
00058     if (reqScale == 0)
00059         return (prescalers[(PWM_SC) & 0x07]);
00060     
00061     uint32_t retval = 0;
00062     char bin;
00063     
00064     for (bin = 0; bin<8; bin++) {
00065         retval = prescalers[bin];
00066         if (retval >= reqScale)
00067             break;
00068     }
00069     if (bin == 8)
00070         bin = 7;
00071     
00072     //Clear lower 5 bits, write new value:
00073     char clockbits = PWM_SC & (3<<3);
00074     
00075     //For some reason clearing them takes some effort
00076     while ((PWM_SC & 0x1F) != 0)
00077         PWM_SC &= ~0x1F;
00078         
00079     
00080     PWM_SC = bin + clockbits;
00081     
00082     return retval;   
00083 }
00084 #endif