Debian Codegraph @

source/soft_pwm.c

raw symbols SCIP
1 /*
2 Copyright (c) 2013-2021 Ben Croston
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23 #include <stdlib.h>
24 #include <pthread.h>
25 #include <time.h>
26 #include "c_gpio.h"
27 #include "soft_pwm.h"
28
29 struct pwm
30 {
31 unsigned int gpio;
32 float freq;
33 float dutycycle;
34 float basetime;
35 float slicetime;
36 struct timespec req_on, req_off;
37 int running;
38 struct pwm *next;
39 };
40 struct pwm *pwm_list = NULL;
41
42 void remove_pwm(unsigned int gpio)
43 {
44 struct pwm *p = pwm_list;
45 struct pwm *prev = NULL;
46 struct pwm *temp;
47
48 while (p != NULL)
49 {
50 if (p->gpio == gpio)
51 {
52 if (prev == NULL) {
53 pwm_list = p->next;
54 } else {
55 prev->next = p->next;
56 }
57 temp = p;
58 p = p->next;
59 temp->running = 0; // signal the thread to stop. The thread will free() the pwm struct when it's done with it.
60 } else {
61 prev = p;
62 p = p->next;
63 }
64 }
65 }
66
67 void calculate_times(struct pwm *p)
68 {
69 long long usec;
70
71 usec = (long long)(p->dutycycle * p->slicetime * 1000.0);
72 p->req_on.tv_sec = (int)(usec / 1000000LL);
73 usec -= (long long)p->req_on.tv_sec * 1000000LL;
74 p->req_on.tv_nsec = (long)usec * 1000L;
75
76 usec = (long long)((100.0-p->dutycycle) * p->slicetime * 1000.0);
77 p->req_off.tv_sec = (int)(usec / 1000000LL);
78 usec -= (long long)p->req_off.tv_sec * 1000000LL;
79 p->req_off.tv_nsec = (long)usec * 1000L;
80 }
81
82 void full_sleep(struct timespec *req)
83 {
84 struct timespec rem = {0};
85
86 if (nanosleep(req,&rem) == -1)
87 full_sleep(&rem);
88 }
89
90 void *pwm_thread(void *threadarg)
91 {
92 struct pwm *p = (struct pwm *)threadarg;
93
94 while (p->running)
95 {
96
97 if (p->dutycycle > 0.0)
98 {
99 output_gpio(p->gpio, 1);
100 full_sleep(&p->req_on);
101 }
102
103 if (p->dutycycle < 100.0)
104 {
105 output_gpio(p->gpio, 0);
106 full_sleep(&p->req_off);
107 }
108 }
109
110 // clean up
111 output_gpio(p->gpio, 0);
112 free(p);
113 pthread_exit(NULL);
114 }
115
116 struct pwm *add_new_pwm(unsigned int gpio)
117 {
118 struct pwm *new_pwm;
119
120 new_pwm = malloc(sizeof(struct pwm));
121 new_pwm->gpio = gpio;
122 new_pwm->running = 0;
123 new_pwm->next = NULL;
124 // default to 1 kHz frequency, dutycycle 0.0
125 new_pwm->freq = 1000.0;
126 new_pwm->dutycycle = 0.0;
127 new_pwm->basetime = 1.0; // 1 ms
128 new_pwm->slicetime = 0.01; // 0.01 ms
129 calculate_times(new_pwm);
130 return new_pwm;
131 }
132
133 struct pwm *find_pwm(unsigned int gpio)
134 /* Return the pwm record for gpio, creating it if it does not exist */
135 {
136 struct pwm *p = pwm_list;
137
138 if (pwm_list == NULL)
139 {
140 pwm_list = add_new_pwm(gpio);
141 return pwm_list;
142 }
143
144 while (p != NULL)
145 {
146 if (p->gpio == gpio)
147 return p;
148 if (p->next == NULL)
149 {
150 p->next = add_new_pwm(gpio);
151 return p->next;
152 }
153 p = p->next;
154 }
155 return NULL;
156 }
157
158 void pwm_set_duty_cycle(unsigned int gpio, float dutycycle)
159 {
160 struct pwm *p;
161
162 if (dutycycle < 0.0 || dutycycle > 100.0)
163 {
164 // btc fixme - error
165 return;
166 }
167
168 if ((p = find_pwm(gpio)) != NULL)
169 {
170 p->dutycycle = dutycycle;
171 calculate_times(p);
172 }
173 }
174
175 void pwm_set_frequency(unsigned int gpio, float freq)
176 {
177 struct pwm *p;
178
179 if (freq <= 0.0) // to avoid divide by zero
180 {
181 // btc fixme - error
182 return;
183 }
184
185 if ((p = find_pwm(gpio)) != NULL)
186 {
187 p->basetime = 1000.0 / freq; // calculated in ms
188 p->slicetime = p->basetime / 100.0;
189 calculate_times(p);
190 }
191 }
192
193 void pwm_start(unsigned int gpio)
194 {
195 pthread_t threads;
196 struct pwm *p;
197
198 if (((p = find_pwm(gpio)) == NULL) || p->running)
199 return;
200
201 p->running = 1;
202 if (pthread_create(&threads, NULL, pwm_thread, (void *)p) != 0)
203 {
204 // btc fixme - error
205 p->running = 0;
206 return;
207 }
208 pthread_detach(threads);
209 }
210
211 void pwm_stop(unsigned int gpio)
212 {
213 remove_pwm(gpio);
214 }
215
216 // returns 1 if there is a PWM for this gpio, 0 otherwise
217 int pwm_exists(unsigned int gpio)
218 {
219 struct pwm *p = pwm_list;
220
221 while (p != NULL)
222 {
223 if (p->gpio == gpio)
224 {
225 return 1;
226 } else {
227 p = p->next;
228 }
229 }
230 return 0;
231 }