chiark / gitweb /
Updated pwmWrite to not mask the value written in-case a call
[wiringPi.git] / wiringPi / wiringPi.c
index fb5e7c68f6e37205c14fb323101ce440d0b7f10c..39c34da75b76519e9ebb88bc68995d1fbaae75e8 100644 (file)
@@ -79,6 +79,8 @@ void (*setPadDrive)       (int group, int value) ;
 int  (*digitalRead)       (int pin) ;
 int  (*waitForInterrupt)  (int pin, int mS) ;
 void (*delayMicroseconds) (unsigned int howLong) ;
+void (*pwmSetMode)        (int mode) ;
+void (*pwmSetRange)       (unsigned int range) ;
 
 
 #ifndef        TRUE
@@ -377,22 +379,25 @@ void pinModeGpio (int pin, int mode)
     if (!pwmRunning)
     {
 
+      *(pwm + PWM_CONTROL) = 0 ;                       // Stop PWM
+      delayMicroseconds (10) ;
+       
 //     Gert/Doms Values
-      *(clk + PWMCLK_DIV)  = BCM_PASSWORD | (32<<12) ; // set pwm div to 32 (19.2/3 = 600KHz)
+      *(clk + PWMCLK_DIV)  = BCM_PASSWORD | (32<<12) ; // set pwm div to 32 (19.2/32 = 600KHz)
       *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x11 ;     // Source=osc and enable
-      digitalWrite (pin, LOW) ;
-      *(pwm + PWM_CONTROL) = 0 ;                       // Disable PWM
-      delayMicroseconds (10) ;
-      *(pwm + PWM0_RANGE) = 0x400 ;
-      delayMicroseconds (10) ;
-      *(pwm + PWM1_RANGE) = 0x400 ;
+
       delayMicroseconds (10) ;
 
+      *(pwm + PWM0_RANGE) = 0x400 ; delayMicroseconds (10) ;
+      *(pwm + PWM1_RANGE) = 0x400 ; delayMicroseconds (10) ;
+
 // Enable PWMs
 
       *(pwm + PWM0_DATA) = 512 ;
       *(pwm + PWM1_DATA) = 512 ;
 
+// Balanced mode (default)
+
       *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ;
 
       pwmRunning = TRUE ;
@@ -401,9 +406,15 @@ void pinModeGpio (int pin, int mode)
   }
 
 // When we change mode of any pin, we remove the pull up/downs
-
-//  delayMicroseconds (300) ;
+//     Or we used to... Hm. Commented out now because for some wieird reason,
+//     it seems to block subsequent attempts to set the pull up/downs and I've
+//     not quite gotten to the bottom of why this happens
+//     The down-side is that the pull up/downs are rememberd in the SoC between
+//     power cycles, so it's going to be a good idea to explicitly set them in
+//     any new code.
+//
 //  pullUpDnControl (pin, PUD_OFF) ;
+
 }
 
 void pinModeWPi (int pin, int mode)
@@ -417,6 +428,38 @@ void pinModeSys (int pin, int mode)
 }
 
 
+/*
+ * pwmControl:
+ *     Allow the user to control some of the PWM functions
+ *********************************************************************************
+ */
+
+void pwmSetModeWPi (int mode)
+{
+  if (mode == PWM_MODE_MS)
+    *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE | PWM0_MS_MODE | PWM1_MS_MODE ;
+  else
+    *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ;
+}
+
+void pwmSetModeSys (int mode)
+{
+  return ;
+}
+
+
+void pwmSetRangeWPi (unsigned int range)
+{
+  *(pwm + PWM0_RANGE) = range ; delayMicroseconds (10) ;
+  *(pwm + PWM1_RANGE) = range ; delayMicroseconds (10) ;
+}
+
+void pwmSetRangeSys (unsigned int range)
+{
+  return ;
+}
+
+
 #ifdef notYetReady
 /*
  * pinED01:
@@ -487,7 +530,7 @@ void pwmWriteGpio (int pin, int value)
   pin  = pin & 63 ;
   port = gpioToPwmPort [pin] ;
 
-  *(pwm + port) = value & 0x3FF ;
+  *(pwm + port) = value ;
 }
 
 void pwmWriteWPi (int pin, int value)
@@ -768,6 +811,8 @@ int wiringPiSetup (void)
         digitalRead =       digitalReadWPi ;
    waitForInterrupt =  waitForInterruptWPi ;
   delayMicroseconds = delayMicrosecondsWPi ;
+         pwmSetMode =        pwmSetModeWPi ;
+        pwmSetRange =       pwmSetRangeWPi ;
   
 // Open the master /dev/memory device
 
@@ -922,6 +967,8 @@ int wiringPiSetupGpio (void)
         digitalRead =       digitalReadGpio ;
    waitForInterrupt =  waitForInterruptGpio ;
   delayMicroseconds = delayMicrosecondsWPi ;   // Same
+         pwmSetMode =        pwmSetModeWPi ;
+        pwmSetRange =       pwmSetRangeWPi ;
 
   return 0 ;
 }
@@ -950,6 +997,9 @@ int wiringPiSetupSys (void)
         digitalRead =       digitalReadSys ;
    waitForInterrupt =  waitForInterruptSys ;
   delayMicroseconds = delayMicrosecondsSys ;
+         pwmSetMode =        pwmSetModeSys ;
+        pwmSetRange =       pwmSetRangeSys ;
+
 
 // Open and scan the directory, looking for exported GPIOs, and pre-open
 //     the 'value' interface to speed things up for later