FASTPWM

Dependents:   OV7670_make

Revision:
29:3e4d3b900850
Child:
30:87e38b846651
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Device/FastPWM_LPC_SCT.cpp	Sun Mar 13 11:43:18 2016 +0000
@@ -0,0 +1,55 @@
+//For targets which use the SCT
+#if defined(TARGET_LPC81X) || defined(TARGET_LPC82X)
+
+#ifdef TARGET_LPC82X
+#define CTRL_U CTRL
+#endif
+
+#include "FastPWM.h"
+
+void FastPWM::initFastPWM( void ) {
+    //Mbed uses the timer as a single unified 32-bit timer, who are we to argue with this, and it is easier
+    bits = 32;
+    
+    //With 32-bit we fix prescaler to 1
+    _pwm.pwm->CTRL_U |= (1 << 2) | (1 << 3);
+    _pwm.pwm->CTRL_U &= ~(0x7F << 5);
+    _pwm.pwm->CTRL_U &= ~(1 << 2);
+
+}
+
+void FastPWM::pulsewidth_ticks( uint32_t ticks ) {
+    #ifdef TARGET_LPC81X
+    _pwm.pwm->MATCHREL[_pwm.pwm_ch + 1].U = ticks;
+    #else
+    _pwm.pwm->MATCHREL[_pwm.pwm_ch + 1] = ticks;
+    #endif
+}
+
+void FastPWM::period_ticks( uint32_t ticks ) {
+    #ifdef TARGET_LPC81X
+    _pwm.pwm->MATCHREL[0].U = ticks;
+    #else
+    _pwm.pwm->MATCHREL[0] = ticks;
+    #endif
+}
+
+uint32_t FastPWM::getPeriod( void ) {
+    #ifdef TARGET_LPC81X
+    return _pwm.pwm->MATCHREL[0].U;
+    #else
+    return _pwm.pwm->MATCHREL[0];
+    #endif
+}
+
+//Maybe implemented later, but needing to change the prescaler for a 32-bit
+//timer used in PWM mode is kinda unlikely.
+//If you really need to do it, rejoice, you can make it run so slow a period is over 40,000 year
+uint32_t FastPWM::setPrescaler(uint32_t reqScale) {
+    //Disable dynamic prescaling
+    dynamicPrescaler = false;
+    
+    return 1;
+}
+
+#endif
\ No newline at end of file