(Almost done) pin refactoring
[matches/MCTX3420.git] / server / bbb_pin.c
1 /**
2  * @file bbb_pin.c
3  * @purpose Implementation of BBB pin control functions and structures
4  * THIS CODE IS NOT THREADSAFE
5  */
6
7 #define _BBB_PIN_SRC
8 #include "bbb_pin.h"
9
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #include <ctype.h>
14 #include "options.h"
15
16 /**
17  * Structure to represent a GPIO pin
18  * Note: Only accessable to this file; to use the functions pass a GPIOId
19  */
20 typedef struct
21 {
22         bool initialised;
23         int fd_value;
24         int fd_direction;
25 } GPIO_Pin;
26
27 /**
28  * Structure to represent an ADC pin
29  * Note: Only accessable to this file; to use the functions pass a ADCId
30  */
31 typedef struct
32 {
33         bool initialised;
34         int fd_value;
35 } ADC_Pin;
36
37 /**
38  * Structure to represent a PWM pin
39  * Note: Only accessable to this file; to use the functions pass a PWMId
40  */
41 typedef struct
42 {
43         bool initialised;
44         int fd_run;
45         FILE * file_duty;
46         FILE * file_period;
47         int fd_polarity;
48 } PWM_Pin;
49
50 /** Array of GPIO pins **/
51 static GPIO_Pin g_gpio[GPIO_NUM_PINS] = {{0}};
52 /** Array of ADC pins **/
53 static ADC_Pin g_adc[ADC_NUM_PINS] = {{0}};
54 /** Array of PWM pins **/
55 static PWM_Pin g_pwm[PWM_NUM_PINS] = {{0}};
56
57 static char g_buffer[BUFSIZ] = {0};
58
59 /**
60  * Export a GPIO pin and open the file descriptors
61  * @param pin The GPIO number to be exported
62  * @return true on success, false otherwise
63  */
64 bool GPIO_Export(int pin)
65 {
66         if (pin < 0 || pin > GPIO_MAX_NUMBER || g_pin_gpio_to_index[pin] == 128)
67         {
68                 AbortBool("Not a useable pin number: %d", pin);
69         }
70
71         GPIO_Pin *gpio = &g_gpio[g_pin_gpio_to_index[pin]];
72         if (gpio->initialised)
73         {
74                 Log(LOGNOTE, "GPIO %d already initialised.", pin);
75                 return true;
76         }
77
78         // Export the pin
79         sprintf(g_buffer, "%s/export", GPIO_DEVICE_PATH);
80         FILE * file_export = fopen(g_buffer, "w");
81         if (file_export == NULL)
82         {
83                 AbortBool("Couldn't open %s to export GPIO pin %d - %s", g_buffer, pin, strerror(errno));
84         }
85         fprintf(file_export, "%d", pin);        
86         fclose(file_export);
87         
88         // Setup direction file descriptor
89         sprintf(g_buffer, "%s/gpio%d/direction", GPIO_DEVICE_PATH, pin);
90         gpio->fd_direction = open(g_buffer, O_RDWR);
91         if (gpio->fd_direction < 0)
92         {
93                 AbortBool("Couldn't open %s for GPIO pin %d - %s", g_buffer, pin, strerror(errno));
94         }
95
96         // Setup value file descriptor
97         sprintf(g_buffer, "%s/gpio%d/value", GPIO_DEVICE_PATH, pin);
98         gpio->fd_value = open(g_buffer, O_RDWR);
99         if (gpio->fd_value < 0)
100         {
101                 close(gpio->fd_direction);
102                 AbortBool("Couldn't open %s for GPIO pin %d - %s", g_buffer, pin, strerror(errno));
103         }
104
105         gpio->initialised = true;
106         Log(LOGDEBUG, "Exported GPIO%d", pin);
107         return true;
108 }
109
110 /**
111  * Unexport a GPIO pin and close its' file descriptors
112  */
113 void GPIO_Unexport(int pin)
114 {
115         if (pin < 0 || pin > GPIO_MAX_NUMBER || g_pin_gpio_to_index[pin] == 128)
116         {
117                 Abort("Not a useable pin number: %d", pin);
118         }
119
120         GPIO_Pin *gpio = &g_gpio[g_pin_gpio_to_index[pin]];
121         if (!gpio->initialised)
122         {
123                 Abort("GPIO %d is already uninitialised", pin);
124         }
125
126         // Close file descriptors
127         close(gpio->fd_value);
128         close(gpio->fd_direction);
129         // Uninitialise this one
130         gpio->initialised = false;
131
132         // Unexport the pin
133         if (g_buffer[0] == '\0')
134                 sprintf(g_buffer, "%s/unexport", GPIO_DEVICE_PATH);     
135         FILE * file_export = fopen(g_buffer, "w");
136         if (file_export == NULL)
137         {
138                 Abort("Couldn't open %s to export GPIO pin %d - %s", g_buffer, pin, strerror(errno));
139         }
140
141         fprintf(file_export, "%d", pin);        
142         fclose(file_export);
143 }
144
145 /**
146  * Initialise all PWM pins and open file descriptors
147  * @param pin - The sysfs pin number
148  * @return true if exported, false otherwise
149  */
150 bool PWM_Export(int pin)
151 {
152         //goto would make this easier...
153         if (pin < 0 || pin >= PWM_NUM_PINS)
154         {
155                 AbortBool("Invalid PWM pin %d specified.", pin);
156         }
157
158         PWM_Pin *pwm = &g_pwm[pin];
159         if (pwm->initialised)
160         {
161                 Log(LOGNOTE, "PWM %d already exported.", pin);
162                 return true;
163         }
164
165         // Try export the pin, doesn't matter if it's already exported.
166         sprintf(g_buffer, "%s/export", PWM_DEVICE_PATH);
167         FILE * file_export = fopen(g_buffer, "w");
168         if (file_export == NULL)
169         {
170                 AbortBool("Couldn't open %s to export PWM pin %d - %s", 
171                                 g_buffer, pin, strerror(errno));
172         }
173         fprintf(file_export, "%d\n", pin);
174         fclose(file_export);
175
176         // Open file descriptors
177         sprintf(g_buffer, "%s/pwm%d/run", PWM_DEVICE_PATH, pin);
178         pwm->fd_run = open(g_buffer, O_WRONLY);
179         if (pwm->fd_run < 0)
180         {
181                 AbortBool("Couldn't open %s for PWM%d - %s", g_buffer, pin, strerror(errno));
182         }
183
184         sprintf(g_buffer, "%s/pwm%d/polarity", PWM_DEVICE_PATH, pin);
185         pwm->fd_polarity = open(g_buffer, O_WRONLY);
186         if (pwm->fd_polarity < 0)
187         {
188                 close(pwm->fd_run);
189                 AbortBool("Couldn't open %s for PWM%d - %s", g_buffer, pin, strerror(errno));
190         }
191
192         sprintf(g_buffer, "%s/pwm%d/period_ns", PWM_DEVICE_PATH, pin);
193         pwm->file_period = fopen(g_buffer, "w");
194         if (pwm->file_period == NULL)
195         {
196                 close(pwm->fd_run);
197                 close(pwm->fd_polarity);
198                 AbortBool("Couldn't open %s for PWM%d - %s", g_buffer, pin, strerror(errno));
199         }
200
201         sprintf(g_buffer, "%s/pwm%d/duty_ns", PWM_DEVICE_PATH, pin);
202         pwm->file_duty = fopen(g_buffer, "w");
203         if (pwm->file_duty == NULL)
204         {
205                 close(pwm->fd_run);
206                 close(pwm->fd_polarity);
207                 fclose(pwm->file_period);
208                 AbortBool("Couldn't open %s for PWM%d - %s", g_buffer, pin, strerror(errno));
209         }
210
211         // Don't buffer the streams
212         setbuf(pwm->file_period, NULL);
213         setbuf(pwm->file_duty, NULL);   
214
215         pwm->initialised = true;
216         Log(LOGDEBUG, "Exported PWM%d", pin);
217         return true;
218 }
219
220
221 /**
222  * Unexport a PWM pin and close its file descriptors
223  * @param pin - The sysfs pin number
224  */
225 void PWM_Unexport(int pin)
226 {
227         if (pin < 0 || pin >= PWM_NUM_PINS)
228         {
229                 Abort("Invalid PWM pin number %d specified.", pin);
230         }
231
232         PWM_Pin *pwm = &g_pwm[pin];
233         if (!pwm->initialised)
234         {
235                 Abort("PWM %d not initialised", pin);
236         }
237
238         // Close the file descriptors
239         close(pwm->fd_polarity);
240         //Stop it, if it's still running
241         pwrite(pwm->fd_run, "0", 1, 0);
242         close(pwm->fd_run);
243         fclose(pwm->file_period);
244         fclose(pwm->file_duty);
245
246         pwm->initialised = false;
247
248         // Try unexport the pin, doesn't matter if it's already unexported.
249         sprintf(g_buffer, "%s/unexport", PWM_DEVICE_PATH);
250         FILE * file_unexport = fopen(g_buffer, "w");
251         if (file_unexport == NULL)
252         {
253                 Abort("Couldn't open %s to unexport PWM pin %d - %s", g_buffer, pin, strerror(errno));
254         }
255         fprintf(file_unexport, "%d\n", pin);
256         fclose(file_unexport);
257 }
258
259 /**
260  * Initialise ADC structures
261  * @param pin The ADC pin number
262  */
263 bool ADC_Export(int pin)
264 {
265         if (pin < 0 || pin >= ADC_NUM_PINS)
266         {
267                 AbortBool("Invalid ADC pin %d specified.", pin);
268         }
269         else if (g_adc[pin].initialised)
270         {
271                 Log(LOGNOTE, "ADC %d already initialised", pin);
272                 return true;
273         }
274
275         sprintf(g_buffer, "%s/in_voltage%d_raw", g_options.adc_device_path, pin);
276         g_adc[pin].fd_value = open(g_buffer, O_RDONLY);
277         if (g_adc[pin].fd_value <0)
278         {
279                 AbortBool("Couldn't open ADC %d device file %s - %s", pin, g_buffer, strerror(errno));
280         }
281
282         g_adc[pin].initialised = true;
283         Log(LOGDEBUG, "Opened ADC %d", pin);
284         return true;
285 }
286
287 /**
288  * Unexport ADC pins
289  * @param pin The ADC pin number
290  */
291 void ADC_Unexport(int pin)
292 {
293         if (pin < 0 || pin >= ADC_NUM_PINS)
294         {
295                 Abort("Invalid ADC pin %d specified.", pin);
296         }
297         else if (!g_adc[pin].initialised)
298         {
299                 Abort("ADC %d already uninitialised", pin);
300         }
301
302         close(g_adc[pin].fd_value);     
303         g_adc[pin].fd_value = -1;
304         g_adc[pin].initialised = false;
305 }
306
307 /**
308  * Set a GPIO pin
309  * @param pin - The pin to set. MUST have been exported before calling this function.
310  */
311 bool GPIO_Set(int pin, bool value)
312 {
313         if (pin < 0 || pin > GPIO_MAX_NUMBER || g_pin_gpio_to_index[pin] == 128)
314         {
315                 AbortBool("Not a useable pin number: %d", pin);
316         }
317
318         GPIO_Pin *gpio = &g_gpio[g_pin_gpio_to_index[pin]];
319         if (gpio->initialised)
320         {
321                 AbortBool("GPIO %d is not initialised.", pin);
322         }
323         //Set the pin direction
324         if (pwrite(gpio->fd_direction, "out", 3, 0) != 3)
325         {
326                 AbortBool("Couldn't set GPIO %d direction - %s", pin, strerror(errno));
327         }
328
329         char c = value ? '1' : '0';
330         if (pwrite(gpio->fd_value, &c, 1, 0) != 1)
331         {
332                 AbortBool("Couldn't read GPIO %d value - %s", pin, strerror(errno));
333         }
334
335         return true;
336 }
337
338 /** 
339  * Read from a GPIO Pin
340  * @param pin - The pin to read
341  * @param result A pointer to store the result
342  * @return true on success, false otherwise
343  */
344 bool GPIO_Read(int pin, bool *result)
345 {
346         if (pin < 0 || pin > GPIO_MAX_NUMBER || g_pin_gpio_to_index[pin] == 128)
347         {
348                 AbortBool("Not a useable pin number: %d", pin);
349         }
350
351         GPIO_Pin *gpio = &g_gpio[g_pin_gpio_to_index[pin]];
352
353         if (pwrite(gpio->fd_direction, "in", 2, 0) != 2)
354                 AbortBool("Couldn't set GPIO %d direction - %s", pin, strerror(errno)); 
355         
356         char c = '0';
357         if (pread(gpio->fd_value, &c, 1, 0) != 1)
358                 AbortBool("Couldn't read GPIO %d value - %s", pin, strerror(errno));
359
360         *result = (c == '1');
361         return true;
362 }
363
364 /**
365  * Activate a PWM pin
366  * @param pin - The sysfs pin number
367  * @param polarity - if true, pin is active high, else active low
368  * @param period - The period in ns
369  * @param duty - The time the pin is active in ns
370  */
371 bool PWM_Set(int pin, bool polarity, long period, long duty)
372 {
373         Log(LOGDEBUG, "Pin %d, pol %d, period: %lu, duty: %lu", pin, polarity, period, duty);
374         
375         if (pin < 0 || pin >= PWM_NUM_PINS)
376         {
377                 AbortBool("Invalid PWM pin number %d specified.", pin);
378         }
379
380         PWM_Pin *pwm = &g_pwm[pin];
381         if (!pwm->initialised)
382         {
383                 AbortBool("PWM %d is not initialised.", pin);
384         }
385
386         // Have to stop PWM before changing it
387         if (pwrite(pwm->fd_run, "0", 1, 0) != 1)
388         {
389                 AbortBool("Couldn't stop PWM%d - %s", pin, strerror(errno));
390         }
391
392         char c = polarity ? '1' : '0';
393         if (pwrite(pwm->fd_polarity, &c, 1, 0) != 1)
394         {
395                 AbortBool("Couldn't set PWM%d polarity - %s", pin, strerror(errno));
396         }
397
398         //This must be done first, otherwise period/duty settings can conflict
399         if (fwrite("0", 1, 1, pwm->file_duty) < 1)
400         {
401                 AbortBool("Couldn't zero the duty for PWM%d - %s", pin, strerror(errno));
402         }
403
404         if (fprintf(pwm->file_period, "%lu", period) < 0)
405         {
406                 AbortBool("Couldn't set period for PWM%d - %s", pin, strerror(errno));
407         }
408
409
410         if (fprintf(pwm->file_duty, "%lu", duty) < 0)
411         {
412                 AbortBool("Couldn't set duty cycle for PWM%d - %s", pin, strerror(errno));
413         }
414
415
416         if (pwrite(pwm->fd_run, "1", 1, 0) != 1)
417         {
418                 AbortBool("Couldn't start PWM%d - %s", pin, strerror(errno));
419         }
420
421         return true;
422 }
423
424 /**
425  * Deactivate a PWM pin
426  * @param pin - The syfs pin number
427  */
428 void PWM_Stop(int pin)
429 {
430         if (pin < 0 || pin >= PWM_NUM_PINS)
431         {
432                 Abort("Invalid PWM pin number %d specified.", pin);
433         }
434
435         if (pwrite(g_pwm[pin].fd_run, "0", 1, 0) != 1)
436         {
437                 Abort("Couldn't stop PWM %d - %s", pin, strerror(errno));
438         }
439 }
440
441 /**
442  * Read an ADC value
443  * @param id - The ID of the ADC pin to read
444  * @param value - A pointer to store the value read from the ADC
445  * @returns - The true if succeeded, false otherwise.
446  */
447 bool ADC_Read(int id, int *value)
448 {
449         char adc_str[ADC_DIGITS] = {0};
450
451         if (id < 0 || id >= ADC_NUM_PINS)
452         {
453                 AbortBool("Invalid ADC pin %d specified.", id);
454         }
455         else if (!g_adc[id].initialised)
456         {
457                 AbortBool("ADC %d is not initialised.", id);
458         }
459
460         if (pread(g_adc[id].fd_value, adc_str, ADC_DIGITS-1, 0) == -1)
461         {
462                 AbortBool("ADC %d read failed: %s", id, strerror(errno));
463         }
464         *value = strtol(adc_str, NULL, 10);
465         return true;
466 }

UCC git Repository :: git.ucc.asn.au