Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Quadrature_Test PID Flow measurement
Fork of FastPWM by
Revision 14:b30038fbba51, committed 2014-09-30
- Comitter:
- Sissors
- Date:
- Tue Sep 30 19:00:27 2014 +0000
- Parent:
- 13:cdefd9d75b64
- Child:
- 15:49a7eff133b3
- Commit message:
- Added KSDK support, probably smashed buggy on KLxx platform.
Changed in this revision
| Device/FastPWM_KLXX_K20D50M.cpp | Show annotated file Show diff for this revision Revisions of this file |
| Device/FastPWM_KSDK.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/Device/FastPWM_KLXX_K20D50M.cpp Sun Jul 20 19:49:43 2014 +0000
+++ b/Device/FastPWM_KLXX_K20D50M.cpp Tue Sep 30 19:00:27 2014 +0000
@@ -37,6 +37,8 @@
if (retval >= reqScale)
break;
}
+ if (bin == 8)
+ bin = 7;
//Clear lower 5 bits, write new value:
char clockbits = *TPM_SC & (3<<3);
@@ -46,7 +48,7 @@
*TPM_SC &= ~0x1F;
- *TPM_SC |= bin + clockbits;
+ *TPM_SC = bin + clockbits;
return retval;
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Device/FastPWM_KSDK.cpp Tue Sep 30 19:00:27 2014 +0000
@@ -0,0 +1,82 @@
+#if defined(TARGET_KPSDK_MCUS)
+
+#include "FastPWM.h"
+#include "fsl_clock_manager.h"
+
+
+#define PWM_CNV (*(((fastpwm_struct*)fast_obj)->CnV))
+#define PWM_MOD (*(((fastpwm_struct*)fast_obj)->MOD))
+#define PWM_SC (*(((fastpwm_struct*)fast_obj)->SC))
+
+typedef struct {
+ __IO uint32_t *CnV;
+ __IO uint32_t *MOD;
+ __IO uint32_t *SC;
+} fastpwm_struct;
+
+static uint32_t pwm_prescaler;
+
+void FastPWM::initFastPWM( void ) {
+ fast_obj = new fastpwm_struct;
+ bits = 16;
+
+ uint32_t pwm_base_clock;
+ CLOCK_SYS_GetFreq(kBusClock, &pwm_base_clock);
+ pwm_prescaler = SystemCoreClock / pwm_base_clock;
+
+ uint32_t ftms[] = FTM_BASE_ADDRS;
+ unsigned int ch_n = (_pwm.pwm_name & 0xFF);
+ FTM_Type *ftm = (FTM_Type *)ftms[_pwm.pwm_name >> TPM_SHIFT];
+
+ ((fastpwm_struct*)fast_obj)->CnV = &ftm->CONTROLS[ch_n].CnV;
+ ((fastpwm_struct*)fast_obj)->MOD = &ftm->MOD;
+ ((fastpwm_struct*)fast_obj)->SC = &ftm->SC;
+}
+
+void FastPWM::pulsewidth_ticks( uint32_t ticks ) {
+ PWM_CNV = ticks;
+}
+
+void FastPWM::period_ticks( uint32_t ticks ) {
+ PWM_MOD = ticks - 1;
+}
+
+uint32_t FastPWM::getPeriod( void ) {
+ return PWM_MOD + 1;
+}
+
+uint32_t FastPWM::setPrescaler(uint32_t reqScale) {
+
+ uint32_t prescalers[] = {1, 2, 4, 8, 16, 32, 64, 128};
+
+ for (int i = 0; i<8; i++)
+ prescalers[i] = prescalers[i] * pwm_prescaler;
+
+ //If prescaler is 0, return current one
+ if (reqScale == 0)
+ return (prescalers[(PWM_SC) & 0x07]);
+
+ uint32_t retval = 0;
+ char bin;
+
+ for (bin = 0; bin<8; bin++) {
+ retval = prescalers[bin];
+ if (retval >= reqScale)
+ break;
+ }
+ if (bin == 8)
+ bin = 7;
+
+ //Clear lower 5 bits, write new value:
+ char clockbits = PWM_SC & (3<<3);
+
+ //For some reason clearing them takes some effort
+ while ((PWM_SC & 0x1F) != 0)
+ PWM_SC &= ~0x1F;
+
+
+ PWM_SC = bin + clockbits;
+
+ return retval;
+}
+#endif
\ No newline at end of file
