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
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 ;
}
// 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)
}
+/*
+ * 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:
*********************************************************************************
*/
-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)
*********************************************************************************
*/
-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)
nanosleep (&sleeper, &dummy) ;
}
+
/*
* delayMicroseconds:
* This is somewhat intersting. It seems that on the Pi, a single call
digitalRead = digitalReadWPi ;
waitForInterrupt = waitForInterruptWPi ;
delayMicroseconds = delayMicrosecondsWPi ;
+ pwmSetMode = pwmSetModeWPi ;
+ pwmSetRange = pwmSetRangeWPi ;
// Open the master /dev/memory device
digitalRead = digitalReadGpio ;
waitForInterrupt = waitForInterruptGpio ;
delayMicroseconds = delayMicrosecondsWPi ; // Same
+ pwmSetMode = pwmSetModeWPi ;
+ pwmSetRange = pwmSetRangeWPi ;
return 0 ;
}
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
return 0 ;
}
-
-