chiark / gitweb /
Updated a technicality in softPwm, and added a suggested memset to zero
[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 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 ;
58
59
60 /*
61  * softPwmThread:
62  *      Thread to do the actual PWM output
63  *********************************************************************************
64  */
65
66 static PI_THREAD (softPwmThread)
67 {
68   int pin, mark, space ;
69   struct sched_param param ;
70
71   param.sched_priority = sched_get_priority_max (SCHED_RR) ;
72   pthread_setschedparam (pthread_self (), SCHED_RR, &param) ;
73
74   pin    = newPin ;
75   newPin = -1 ;
76
77   piHiPri (90) ;
78
79   for (;;)
80   {
81     mark  = marks [pin] ;
82     space = range [pin] - mark ;
83
84     if (mark != 0)
85       digitalWrite (pin, HIGH) ;
86     delayMicroseconds (mark * 100) ;
87
88     if (space != 0)
89       digitalWrite (pin, LOW) ;
90     delayMicroseconds (space * 100) ;
91   }
92
93   return NULL ;
94 }
95
96
97 /*
98  * softPwmWrite:
99  *      Write a PWM value to the given pin
100  *********************************************************************************
101  */
102
103 void softPwmWrite (int pin, int value)
104 {
105   pin &= (MAX_PINS - 1) ;
106
107   /**/ if (value < 0)
108     value = 0 ;
109   else if (value > range [pin])
110     value = range [pin] ;
111
112   marks [pin] = value ;
113 }
114
115
116 /*
117  * softPwmCreate:
118  *      Create a new softPWM thread.
119  *********************************************************************************
120  */
121
122 int softPwmCreate (int pin, int initialValue, int pwmRange)
123 {
124   int res ;
125   pthread_t myThread ;
126
127   if (range [pin] != 0) // Already running on this pin
128     return -1 ;
129
130   if (range <= 0)
131     return -1 ;
132
133   pinMode      (pin, OUTPUT) ;
134   digitalWrite (pin, LOW) ;
135
136   marks [pin] = initialValue ;
137   range [pin] = pwmRange ;
138
139   newPin = pin ;
140   res    = pthread_create (&myThread, NULL, softPwmThread, NULL) ;
141
142   while (newPin != -1)
143     delay (1) ;
144
145   threads [pin] = myThread ;
146
147   return res ;
148 }
149
150
151 /*
152  * softPwmStop:
153  *      Stop an existing softPWM thread
154  *********************************************************************************
155  */
156
157 void softPwmStop (int pin)
158 {
159   if (range [pin] != 0)
160   {
161     pthread_cancel (threads [pin]) ;
162     pthread_join   (threads [pin], NULL) ;
163     range [pin] = 0 ;
164     digitalWrite (pin, LOW) ;
165   }
166 }