mirror of
https://github.com/clockworkpi/WiringPi.git
synced 2026-03-19 18:32:49 +01:00
changed to pin mode to support softPwm.
bugfix in blink.sh - wring pin improving the maxdetect routing - a little. gpio pins
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* softPwm.c:
|
||||
* Provide 2 channels of software driven PWM.
|
||||
* Copyright (c) 2012 Gordon Henderson
|
||||
* Copyright (c) 2012-2014 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
@@ -28,17 +28,22 @@
|
||||
#include "wiringPi.h"
|
||||
#include "softPwm.h"
|
||||
|
||||
// MAX_PINS:
|
||||
// This is more than the number of Pi pins because we can actually softPwm
|
||||
// pins that are on GPIO expanders. It's not that efficient and more than 1 or
|
||||
// 2 pins on e.g. (SPI) mcp23s17 won't really be that effective, however...
|
||||
|
||||
#define MAX_PINS 1024
|
||||
|
||||
// The PWM Frequency is derived from the "pulse time" below. Essentially,
|
||||
// the frequency is a function of the range and this pulse time.
|
||||
// The total period will be range * pulse time in uS, so a pulse time
|
||||
// of 100 and a range of 100 gives a period of 100 * 100 = 10,000 uS
|
||||
// The total period will be range * pulse time in µS, so a pulse time
|
||||
// of 100 and a range of 100 gives a period of 100 * 100 = 10,000 µS
|
||||
// which is a frequency of 100Hz.
|
||||
//
|
||||
// It's possible to get a higher frequency by lowering the pulse time,
|
||||
// however CPU uage will skyrocket as wiringPi uses a hard-loop to time
|
||||
// periods under 100uS - this is because the Linux timer calls are just
|
||||
// periods under 100µS - this is because the Linux timer calls are just
|
||||
// accurate at all, and have an overhead.
|
||||
//
|
||||
// Another way to increase the frequency is to reduce the range - however
|
||||
@@ -46,8 +51,9 @@
|
||||
|
||||
#define PULSE_TIME 100
|
||||
|
||||
static int marks [MAX_PINS] ;
|
||||
static int range [MAX_PINS] ;
|
||||
static int marks [MAX_PINS] ;
|
||||
static int range [MAX_PINS] ;
|
||||
static pthread_t threads [MAX_PINS] ;
|
||||
|
||||
int newPin = -1 ;
|
||||
|
||||
@@ -106,13 +112,20 @@ void softPwmWrite (int pin, int value)
|
||||
|
||||
/*
|
||||
* softPwmCreate:
|
||||
* Create a new PWM thread.
|
||||
* Create a new softPWM thread.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int softPwmCreate (int pin, int initialValue, int pwmRange)
|
||||
{
|
||||
int res ;
|
||||
pthread_t myThread ;
|
||||
|
||||
if (range [pin] != 0) // Already running on this pin
|
||||
return -1 ;
|
||||
|
||||
if (range <= 0)
|
||||
return -1 ;
|
||||
|
||||
pinMode (pin, OUTPUT) ;
|
||||
digitalWrite (pin, LOW) ;
|
||||
@@ -121,10 +134,30 @@ int softPwmCreate (int pin, int initialValue, int pwmRange)
|
||||
range [pin] = pwmRange ;
|
||||
|
||||
newPin = pin ;
|
||||
res = piThreadCreate (softPwmThread) ;
|
||||
res = pthread_create (&myThread, NULL, softPwmThread, NULL) ;
|
||||
|
||||
while (newPin != -1)
|
||||
delay (1) ;
|
||||
|
||||
threads [pin] = myThread ;
|
||||
|
||||
return res ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* softPwmStop:
|
||||
* Stop an existing softPWM thread
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void softPwmStop (int pin)
|
||||
{
|
||||
if (range [pin] != 0)
|
||||
{
|
||||
pthread_cancel (threads [pin]) ;
|
||||
pthread_join (threads [pin], NULL) ;
|
||||
range [pin] = 0 ;
|
||||
digitalWrite (pin, LOW) ;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ extern "C" {
|
||||
|
||||
extern int softPwmCreate (int pin, int value, int range) ;
|
||||
extern void softPwmWrite (int pin, int value) ;
|
||||
extern void softPwmStop (int pin) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -70,6 +70,8 @@
|
||||
#include <sys/wait.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
#include "softPwm.h"
|
||||
|
||||
#include "wiringPi.h"
|
||||
|
||||
#ifndef TRUE
|
||||
@@ -961,6 +963,7 @@ void pinMode (int pin, int mode)
|
||||
{
|
||||
int fSel, shift, alt ;
|
||||
struct wiringPiNodeStruct *node = wiringPiNodes ;
|
||||
int origPin = pin ;
|
||||
|
||||
if ((pin & PI_GPIO_MASK) == 0) // On-board pin
|
||||
{
|
||||
@@ -971,6 +974,8 @@ void pinMode (int pin, int mode)
|
||||
else if (wiringPiMode != WPI_MODE_GPIO)
|
||||
return ;
|
||||
|
||||
softPwmStop (origPin) ;
|
||||
|
||||
fSel = gpioToGPFSEL [pin] ;
|
||||
shift = gpioToShift [pin] ;
|
||||
|
||||
@@ -978,9 +983,11 @@ void pinMode (int pin, int mode)
|
||||
*(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) ; // Sets bits to zero = input
|
||||
else if (mode == OUTPUT)
|
||||
*(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (1 << shift) ;
|
||||
else if (mode == SOFT_PWM_OUTPUT)
|
||||
softPwmCreate (origPin, 0, 100) ;
|
||||
else if (mode == PWM_OUTPUT)
|
||||
{
|
||||
if ((alt = gpioToPwmALT [pin]) == 0) // Not a PWM pin
|
||||
if ((alt = gpioToPwmALT [pin]) == 0) // Not a hardware capable PWM pin
|
||||
return ;
|
||||
|
||||
// Set pin to PWM mode
|
||||
@@ -990,7 +997,7 @@ void pinMode (int pin, int mode)
|
||||
|
||||
pwmSetMode (PWM_MODE_BAL) ; // Pi default mode
|
||||
pwmSetRange (1024) ; // Default range of 1024
|
||||
pwmSetClock (32) ; // 19.2 / 32 = 600KHz - Also starts the PWM
|
||||
pwmSetClock (32) ; // 19.2 / 32 = 600KHz - Also starts the PWM
|
||||
}
|
||||
else if (mode == GPIO_CLOCK)
|
||||
{
|
||||
|
||||
@@ -42,6 +42,7 @@
|
||||
#define OUTPUT 1
|
||||
#define PWM_OUTPUT 2
|
||||
#define GPIO_CLOCK 3
|
||||
#define SOFT_PWM_OUTPUT 4
|
||||
|
||||
#define LOW 0
|
||||
#define HIGH 1
|
||||
|
||||
Reference in New Issue
Block a user