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