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 volatile int marks [MAX_PINS] ;
55 static volatile int range [MAX_PINS] ;
56 static volatile pthread_t threads [MAX_PINS] ;
57 static volatile int newPin = -1 ;
62 * Thread to do the actual PWM output
63 *********************************************************************************
66 static PI_THREAD (softPwmThread)
68 int pin, mark, space ;
69 struct sched_param param ;
71 param.sched_priority = sched_get_priority_max (SCHED_RR) ;
72 pthread_setschedparam (pthread_self (), SCHED_RR, ¶m) ;
82 space = range [pin] - mark ;
85 digitalWrite (pin, HIGH) ;
86 delayMicroseconds (mark * 100) ;
89 digitalWrite (pin, LOW) ;
90 delayMicroseconds (space * 100) ;
99 * Write a PWM value to the given pin
100 *********************************************************************************
103 void softPwmWrite (int pin, int value)
105 pin &= (MAX_PINS - 1) ;
109 else if (value > range [pin])
110 value = range [pin] ;
112 marks [pin] = value ;
118 * Create a new softPWM thread.
119 *********************************************************************************
122 int softPwmCreate (int pin, int initialValue, int pwmRange)
127 if (range [pin] != 0) // Already running on this pin
133 pinMode (pin, OUTPUT) ;
134 digitalWrite (pin, LOW) ;
136 marks [pin] = initialValue ;
137 range [pin] = pwmRange ;
140 res = pthread_create (&myThread, NULL, softPwmThread, NULL) ;
145 threads [pin] = myThread ;
153 * Stop an existing softPWM thread
154 *********************************************************************************
157 void softPwmStop (int pin)
159 if (range [pin] != 0)
161 pthread_cancel (threads [pin]) ;
162 pthread_join (threads [pin], NULL) ;
164 digitalWrite (pin, LOW) ;