3 * Provide 2 channels of software driven PWM.
4 * Copyright (c) 2012-2014 Gordon Henderson
5 ***********************************************************************
6 * This file is part of wiringPi:
7 * https://projects.drogon.net/raspberry-pi/wiringpi/
9 * wiringPi is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as
11 * published by the Free Software Foundation, either version 3 of the
12 * License, or (at your option) any later version.
14 * wiringPi is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with wiringPi.
21 * If not, see <http://www.gnu.org/licenses/>.
22 ***********************************************************************
32 // This is more than the number of Pi pins because we can actually softPwm
33 // pins that are on GPIO expanders. It's not that efficient and more than 1 or
34 // 2 pins on e.g. (SPI) mcp23s17 won't really be that effective, however...
38 // The PWM Frequency is derived from the "pulse time" below. Essentially,
39 // the frequency is a function of the range and this pulse time.
40 // The total period will be range * pulse time in µS, so a pulse time
41 // of 100 and a range of 100 gives a period of 100 * 100 = 10,000 µS
42 // which is a frequency of 100Hz.
44 // It's possible to get a higher frequency by lowering the pulse time,
45 // however CPU uage will skyrocket as wiringPi uses a hard-loop to time
46 // periods under 100µS - this is because the Linux timer calls are just
47 // accurate at all, and have an overhead.
49 // Another way to increase the frequency is to reduce the range - however
50 // that reduces the overall output accuracy...
52 #define PULSE_TIME 100
54 static int marks [MAX_PINS] ;
55 static int range [MAX_PINS] ;
56 static pthread_t threads [MAX_PINS] ;
63 * Thread to do the actual PWM output
64 *********************************************************************************
67 static PI_THREAD (softPwmThread)
69 int pin, mark, space ;
70 struct sched_param param ;
72 param.sched_priority = sched_get_priority_max (SCHED_RR) ;
73 pthread_setschedparam (pthread_self (), SCHED_RR, ¶m) ;
83 space = range [pin] - mark ;
86 digitalWrite (pin, HIGH) ;
87 delayMicroseconds (mark * 100) ;
90 digitalWrite (pin, LOW) ;
91 delayMicroseconds (space * 100) ;
100 * Write a PWM value to the given pin
101 *********************************************************************************
104 void softPwmWrite (int pin, int value)
106 pin &= (MAX_PINS - 1) ;
110 else if (value > range [pin])
111 value = range [pin] ;
113 marks [pin] = value ;
119 * Create a new softPWM thread.
120 *********************************************************************************
123 int softPwmCreate (int pin, int initialValue, int pwmRange)
128 if (range [pin] != 0) // Already running on this pin
134 pinMode (pin, OUTPUT) ;
135 digitalWrite (pin, LOW) ;
137 marks [pin] = initialValue ;
138 range [pin] = pwmRange ;
141 res = pthread_create (&myThread, NULL, softPwmThread, NULL) ;
146 threads [pin] = myThread ;
154 * Stop an existing softPWM thread
155 *********************************************************************************
158 void softPwmStop (int pin)
160 if (range [pin] != 0)
162 pthread_cancel (threads [pin]) ;
163 pthread_join (threads [pin], NULL) ;
165 digitalWrite (pin, LOW) ;