chiark / gitweb /
0bde1804d84e7764fd5f48c42db037156bf01fe0
[wiringPi.git] / wiringPi / softPwm.c
1 /*
2  * softPwm.c:
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/
8  *
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.
13  *
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.
18  *
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  ***********************************************************************
23  */
24
25 #include <stdio.h>
26 #include <pthread.h>
27
28 #include "wiringPi.h"
29 #include "softPwm.h"
30
31 // MAX_PINS:
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...
35
36 #define MAX_PINS        1024
37
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.
43 //
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.
48 //
49 //      Another way to increase the frequency is to reduce the range - however
50 //      that reduces the overall output accuracy...
51
52 #define PULSE_TIME      100
53
54 static int marks         [MAX_PINS] ;
55 static int range         [MAX_PINS] ;
56 static pthread_t threads [MAX_PINS] ;
57
58 int newPin = -1 ;
59
60
61 /*
62  * softPwmThread:
63  *      Thread to do the actual PWM output
64  *********************************************************************************
65  */
66
67 static PI_THREAD (softPwmThread)
68 {
69   int pin, mark, space ;
70
71   pin    = newPin ;
72   newPin = -1 ;
73
74   piHiPri (50) ;
75
76   for (;;)
77   {
78     mark  = marks [pin] ;
79     space = range [pin] - mark ;
80
81     if (mark != 0)
82       digitalWrite (pin, HIGH) ;
83     delayMicroseconds (mark * 100) ;
84
85     if (space != 0)
86       digitalWrite (pin, LOW) ;
87     delayMicroseconds (space * 100) ;
88   }
89
90   return NULL ;
91 }
92
93
94 /*
95  * softPwmWrite:
96  *      Write a PWM value to the given pin
97  *********************************************************************************
98  */
99
100 void softPwmWrite (int pin, int value)
101 {
102   pin &= (MAX_PINS - 1) ;
103
104   /**/ if (value < 0)
105     value = 0 ;
106   else if (value > range [pin])
107     value = range [pin] ;
108
109   marks [pin] = value ;
110 }
111
112
113 /*
114  * softPwmCreate:
115  *      Create a new softPWM thread.
116  *********************************************************************************
117  */
118
119 int softPwmCreate (int pin, int initialValue, int pwmRange)
120 {
121   int res ;
122   pthread_t myThread ;
123
124   if (range [pin] != 0) // Already running on this pin
125     return -1 ;
126
127   if (range <= 0)
128     return -1 ;
129
130   pinMode      (pin, OUTPUT) ;
131   digitalWrite (pin, LOW) ;
132
133   marks [pin] = initialValue ;
134   range [pin] = pwmRange ;
135
136   newPin = pin ;
137   res    = pthread_create (&myThread, NULL, softPwmThread, NULL) ;
138
139   while (newPin != -1)
140     delay (1) ;
141
142   threads [pin] = myThread ;
143
144   return res ;
145 }
146
147
148 /*
149  * softPwmStop:
150  *      Stop an existing softPWM thread
151  *********************************************************************************
152  */
153
154 void softPwmStop (int pin)
155 {
156   if (range [pin] != 0)
157   {
158     pthread_cancel (threads [pin]) ;
159     pthread_join   (threads [pin], NULL) ;
160     range [pin] = 0 ;
161     digitalWrite (pin, LOW) ;
162 }
163 }