chiark / gitweb /
Updated the GPIO command to add in new features for PWM
[wiringPi.git] / wiringPi / wiringPi.c
index 35717ef7c07237f510c95e18385dc7e3b69b9f74..bd3c999deda0ddfa21134be2eea8b5e8936b0eaa 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,8 +406,15 @@ void pinModeGpio (int pin, int mode)
   }
 
 // When we change mode of any pin, we remove the pull up/downs
+//     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) ;
 
-  pullUpDnControl (pin, PUD_OFF) ;
 }
 
 void pinModeWPi (int pin, int mode)
@@ -416,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:
@@ -479,24 +523,19 @@ void digitalWriteSys (int pin, int value)
  *********************************************************************************
  */
 
-void pwmWriteWPi (int pin, int value)
+void pwmWriteGpio (int pin, int value)
 {
   int port ;
 
-  pin  = pinToGpio [pin & 63] ;
+  pin  = pin & 63 ;
   port = gpioToPwmPort [pin] ;
 
   *(pwm + port) = value & 0x3FF ;
 }
 
-void pwmWriteGpio (int pin, int value)
+void pwmWriteWPi (int pin, int value)
 {
-  int port ;
-
-  pin  = pin & 63 ;
-  port = gpioToPwmPort [pin] ;
-
-  *(pwm + port) = value & 0x3FF ;
+  pwmWriteGpio (pinToGpio [pin & 63], value) ;
 }
 
 void pwmWriteSys (int pin, int value)
@@ -588,28 +627,21 @@ int digitalReadSys (int pin)
  *********************************************************************************
  */
 
-void pullUpDnControlWPi (int pin, int pud)
+void pullUpDnControlGpio (int pin, int pud)
 {
-  pin  = pinToGpio [pin & 63] ;
+  pin &= 63 ;
   pud &=  3 ;
 
-  *(gpio + GPPUD)              = pud ;                 delayMicroseconds (10) ;
-  *(gpio + gpioToPUDCLK [pin]) = 1 << (pin & 31) ;     delayMicroseconds (10) ;
+  *(gpio + GPPUD)              = pud ;                 delayMicroseconds (5) ;
+  *(gpio + gpioToPUDCLK [pin]) = 1 << (pin & 31) ;     delayMicroseconds (5) ;
   
-  *(gpio + GPPUD)              = 0 ;
-  *(gpio + gpioToPUDCLK [pin]) = 0 ;
+  *(gpio + GPPUD)              = 0 ;                   delayMicroseconds (5) ;
+  *(gpio + gpioToPUDCLK [pin]) = 0 ;                   delayMicroseconds (5) ;
 }
 
-void pullUpDnControlGpio (int pin, int pud)
+void pullUpDnControlWPi (int pin, int pud)
 {
-  pin &= 63 ;
-  pud &=  3 ;
-
-  *(gpio + GPPUD)              = pud ;                 delayMicroseconds (10) ;
-  *(gpio + gpioToPUDCLK [pin]) = 1 << (pin & 31) ;     delayMicroseconds (10) ;
-  
-  *(gpio + GPPUD)              = 0 ;
-  *(gpio + gpioToPUDCLK [pin]) = 0 ;
+  pullUpDnControlGpio (pinToGpio [pin & 63], pud) ;
 }
 
 void pullUpDnControlSys (int pin, int pud)
@@ -685,6 +717,7 @@ void delay (unsigned int howLong)
   nanosleep (&sleeper, &dummy) ;
 }
 
+
 /*
  * delayMicroseconds:
  *     This is somewhat intersting. It seems that on the Pi, a single call
@@ -778,6 +811,8 @@ int wiringPiSetup (void)
         digitalRead =       digitalReadWPi ;
    waitForInterrupt =  waitForInterruptWPi ;
   delayMicroseconds = delayMicrosecondsWPi ;
+         pwmSetMode =        pwmSetModeWPi ;
+        pwmSetRange =       pwmSetRangeWPi ;
   
 // Open the master /dev/memory device
 
@@ -932,6 +967,8 @@ int wiringPiSetupGpio (void)
         digitalRead =       digitalReadGpio ;
    waitForInterrupt =  waitForInterruptGpio ;
   delayMicroseconds = delayMicrosecondsWPi ;   // Same
+         pwmSetMode =        pwmSetModeWPi ;
+        pwmSetRange =       pwmSetRangeWPi ;
 
   return 0 ;
 }
@@ -960,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
@@ -977,5 +1017,3 @@ int wiringPiSetupSys (void)
 
   return 0 ;
 }
-
-