Modify flags in PwmOut to center align pwm signals.

Dependents:   BLDC_RPM_meter

Files at this revision

API Documentation at this revision

Comitter:
acracan
Date:
Tue Jul 08 17:37:24 2014 +0000
Commit message:
Update.

Changed in this revision

CenteredPwmOut.h Show annotated file Show diff for this revision Revisions of this file
Device/CenteredPwmOut_KLXX.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CenteredPwmOut.h	Tue Jul 08 17:37:24 2014 +0000
@@ -0,0 +1,26 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2006-2013 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef CENTEREDPWMOUT_H
+#define CENTEREDPWMOUT_H
+
+#include "mbed.h"
+
+class CenteredPwmOut: public PwmOut {
+
+public:
+    CenteredPwmOut(PinName pin);
+};
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Device/CenteredPwmOut_KLXX.cpp	Tue Jul 08 17:37:24 2014 +0000
@@ -0,0 +1,37 @@
+#ifdef TARGET_KLXX
+
+#include "CenteredPwmOut.h"
+#include "PeripheralPins.h"
+
+CenteredPwmOut::CenteredPwmOut(PinName pin):
+    PwmOut(pin)
+{
+    PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
+    if (pwm == (PWMName)NC)
+        return;
+    unsigned int tpm_n = (pwm >> TPM_SHIFT);
+    
+    TPM_Type *tpm = (TPM_Type *)(TPM0_BASE + 0x1000 * tpm_n);
+
+    // Enable PWM interrupts
+    tpm->SC |= TPM_SC_TOIE_MASK;
+
+    unsigned int clkdiv = (tpm->SC & TPM_SC_PS_MASK) >> TPM_SC_PS_SHIFT;
+    bool centered = tpm->SC & TPM_SC_CPWMS_MASK;
+    
+    if (centered)
+        return;
+    // Change PWM to center-aligned mode
+    
+    // First, disable counter
+    tpm->SC &= ~TPM_SC_CMOD_MASK;
+    // Change the prescaler factor
+    //tpm->SC &= ~TPM_SC_PS_MASK;
+    //tpm->SC |= TPM_SC_PS(clkdiv - 1);
+    // Then change mode
+    tpm->SC |= TPM_SC_CPWMS_MASK;
+    // Finally, enable counter
+    tpm->SC |= TPM_SC_CMOD(1);
+}
+
+#endif
\ No newline at end of file