chiark / gitweb /
a4f0fc43f6b0fc3e0dc434c1cef9087e0833a982
[wiringPi.git] / wiringPi / softPwm.c
1 /*
2  * softPwm.c:
3  *      Provide 2 channels of software driven PWM.
4  *      Copyright (c) 2012 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 #define MAX_PINS        1024
32
33 // The PWM Frequency is derived from the "pulse time" below. Essentially,
34 //      the frequency is a function of the range and this pulse time.
35 //      The total period will be range * pulse time in uS, so a pulse time
36 //      of 100 and a range of 100 gives a period of 100 * 100 = 10,000 uS
37 //      which is a frequency of 100Hz.
38 //
39 //      It's possible to get a higher frequency by lowering the pulse time,
40 //      however CPU uage will skyrocket as wiringPi uses a hard-loop to time
41 //      periods under 100uS - this is because the Linux timer calls are just
42 //      accurate at all, and have an overhead.
43 //
44 //      Another way to increase the frequency is to reduce the range - however
45 //      that reduces the overall output accuracy...
46
47 #define PULSE_TIME      100
48
49 static int marks [MAX_PINS] ;
50 static int range [MAX_PINS] ;
51
52 int newPin = -1 ;
53
54
55 /*
56  * softPwmThread:
57  *      Thread to do the actual PWM output
58  *********************************************************************************
59  */
60
61 static PI_THREAD (softPwmThread)
62 {
63   int pin, mark, space ;
64
65   pin    = newPin ;
66   newPin = -1 ;
67
68   piHiPri (50) ;
69
70   for (;;)
71   {
72     mark  = marks [pin] ;
73     space = range [pin] - mark ;
74
75     if (mark != 0)
76       digitalWrite (pin, HIGH) ;
77     delayMicroseconds (mark * 100) ;
78
79     if (space != 0)
80       digitalWrite (pin, LOW) ;
81     delayMicroseconds (space * 100) ;
82   }
83
84   return NULL ;
85 }
86
87
88 /*
89  * softPwmWrite:
90  *      Write a PWM value to the given pin
91  *********************************************************************************
92  */
93
94 void softPwmWrite (int pin, int value)
95 {
96   pin &= (MAX_PINS - 1) ;
97
98   /**/ if (value < 0)
99     value = 0 ;
100   else if (value > range [pin])
101     value = range [pin] ;
102
103   marks [pin] = value ;
104 }
105
106
107 /*
108  * softPwmCreate:
109  *      Create a new PWM thread.
110  *********************************************************************************
111  */
112
113 int softPwmCreate (int pin, int initialValue, int pwmRange)
114 {
115   int res ;
116
117   pinMode      (pin, OUTPUT) ;
118   digitalWrite (pin, LOW) ;
119
120   marks [pin] = initialValue ;
121   range [pin] = pwmRange ;
122
123   newPin = pin ;
124   res = piThreadCreate (softPwmThread) ;
125
126   while (newPin != -1)
127     delay (1) ;
128
129   return res ;
130 }