mraa: add deinit function for valgrind testing
[contrib/mraa.git] / src / mraa.c
1 /*
2  * Author: Brendan Le Foll <brendan.le.foll@intel.com>
3  * Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
4  * Copyright (c) 2014 Intel Corporation.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25
26 #include <stddef.h>
27 #include <stdlib.h>
28 #include <sched.h>
29 #include <string.h>
30
31 #include "mraa_internal.h"
32 #include "intel_galileo_rev_d.h"
33 #include "intel_galileo_rev_g.h"
34 #include "gpio.h"
35 #include "version.h"
36
37 //static mraa_pininfo_t* pindata;
38 static mraa_board_t* plat = NULL;
39 static mraa_platform_t platform_type = MRAA_UNKNOWN_PLATFORM;
40 static mraa_adv_func* advance = NULL;
41
42 const char *
43 mraa_get_version()
44 {
45     return gVERSION;
46 }
47
48 #ifdef SWIG
49 mraa_result_t
50 mraa_init()
51 #else
52 mraa_result_t __attribute__((constructor))
53 mraa_init()
54 #endif
55 {
56     /** Once more board definitions have been added,
57      *  A method for detecting them will need to be devised.
58      */
59     if (plat != NULL) {
60         return MRAA_ERROR_PLATFORM_ALREADY_INITIALISED;
61     }
62 #ifdef SWIGPYTHON
63     // Initialise python threads, this allows use to grab the GIL when we are
64     // required to do so
65     Py_InitializeEx(0);
66     PyEval_InitThreads();
67 #endif
68     // detect a galileo gen2 board
69     char *line = NULL;
70     // let getline allocate memory for *line
71     size_t len = 0;
72     FILE *fh = fopen("/sys/devices/virtual/dmi/id/board_name", "r");
73     if (fh != NULL) {
74         if (getline(&line, &len, fh) != -1) {
75             if (strncmp(line, "GalileoGen2", 10) == 0) {
76                 platform_type = MRAA_INTEL_GALILEO_GEN2;
77             } else {
78                 platform_type = MRAA_INTEL_GALILEO_GEN1;
79             }
80         }
81     }
82     free(line);
83     fclose(fh);
84
85     advance = (mraa_adv_func*) malloc(sizeof(mraa_adv_func));
86     switch(platform_type) {
87         case MRAA_INTEL_GALILEO_GEN2:
88             plat = mraa_intel_galileo_gen2(advance);
89             break;
90         case MRAA_INTEL_GALILEO_GEN1:
91             plat = mraa_intel_galileo_rev_d(advance);
92             break;
93         default:
94             plat = mraa_intel_galileo_rev_d(advance);
95             fprintf(stderr, "Platform not found, initialising MRAA_INTEL_GALILEO_GEN1\n");
96     }
97
98     return MRAA_SUCCESS;
99 }
100
101 void
102 mraa_deinit()
103 {
104     free(plat->pins);
105     free(plat);
106 }
107
108 int
109 mraa_set_priority(const unsigned int priority)
110 {
111     struct sched_param sched_s;
112
113     memset(&sched_s, 0, sizeof(struct sched_param));
114     if (priority > sched_get_priority_max(SCHED_RR)) {
115         sched_s.sched_priority = sched_get_priority_max(SCHED_RR);
116     }
117     else {
118         sched_s.sched_priority = priority;
119     }
120
121     return sched_setscheduler(0, SCHED_RR, &sched_s);
122 }
123
124 static mraa_result_t
125 mraa_setup_mux_mapped(mraa_pin_t meta)
126 {
127     int mi;
128     for (mi = 0; mi < meta.mux_total; mi++) {
129         mraa_gpio_context mux_i;
130         mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
131         if (mux_i == NULL)
132             return MRAA_ERROR_INVALID_HANDLE;
133         mraa_gpio_dir(mux_i, MRAA_GPIO_OUT);
134         if (mraa_gpio_write(mux_i, meta.mux[mi].value) != MRAA_SUCCESS)
135             return MRAA_ERROR_INVALID_RESOURCE;
136     }
137     return MRAA_SUCCESS;
138 }
139
140 unsigned int
141 mraa_setup_gpio(int pin)
142 {
143     if (plat == NULL)
144         return -1;
145
146     if (pin < 0 || pin > plat->phy_pin_count)
147         return -1;
148
149     if(plat->pins[pin].capabilites.gpio != 1)
150       return -1;
151
152     if (plat->pins[pin].gpio.mux_total > 0)
153        if (mraa_setup_mux_mapped(plat->pins[pin].gpio) != MRAA_SUCCESS)
154             return -1;
155     return plat->pins[pin].gpio.pinmap;
156 }
157
158 unsigned int
159 mraa_setup_aio(int aio)
160 {
161     if (plat == NULL)
162         return -3;
163
164     if (aio < 0 || aio > plat->aio_count)
165         return -1;
166
167     int pin = aio + plat->gpio_count;
168
169     if (plat->pins[pin].capabilites.aio != 1)
170       return -1;
171
172     if (plat->pins[pin].aio.mux_total > 0)
173        if (mraa_setup_mux_mapped(plat->pins[pin].aio) != MRAA_SUCCESS)
174             return -1;
175     return plat->pins[pin].aio.pinmap;
176 }
177
178 unsigned int
179 mraa_setup_i2c(int bus)
180 {
181     if (plat == NULL)
182         return -3;
183
184     if (plat->i2c_bus_count >! 0) {
185         fprintf(stderr, "No i2c buses defined in platform");
186         return -1;
187     }
188     if (bus >= plat->i2c_bus_count) {
189         fprintf(stderr, "Above i2c bus count");
190         return -1;
191     }
192
193     int pos = plat->i2c_bus[bus].sda;
194     if (plat->pins[pos].i2c.mux_total > 0)
195         if (mraa_setup_mux_mapped(plat->pins[pos].i2c) != MRAA_SUCCESS)
196              return -2;
197
198     pos = plat->i2c_bus[bus].scl;
199     if (plat->pins[pos].i2c.mux_total > 0)
200         if (mraa_setup_mux_mapped(plat->pins[pos].i2c) != MRAA_SUCCESS)
201              return -2;
202
203     return plat->i2c_bus[bus].bus_id;
204 }
205
206 mraa_spi_bus_t*
207 mraa_setup_spi(int bus)
208 {
209     if (plat == NULL)
210         return NULL;
211
212     if (plat->spi_bus_count >! 0) {
213         fprintf(stderr, "No spi buses defined in platform");
214         return NULL;
215     }
216     if (bus >= plat->spi_bus_count) {
217         fprintf(stderr, "Above spi bus count");
218         return NULL;
219     }
220
221     int pos = plat->spi_bus[bus].sclk;
222     if (plat->pins[pos].spi.mux_total > 0)
223         if (mraa_setup_mux_mapped(plat->pins[pos].spi) != MRAA_SUCCESS)
224              return NULL;
225
226     pos = plat->spi_bus[bus].mosi;
227     if (plat->pins[pos].spi.mux_total > 0)
228         if (mraa_setup_mux_mapped(plat->pins[pos].spi) != MRAA_SUCCESS)
229              return NULL;
230
231     pos = plat->spi_bus[bus].miso;
232     if (plat->pins[pos].spi.mux_total > 0)
233         if (mraa_setup_mux_mapped(plat->pins[pos].spi) != MRAA_SUCCESS)
234              return NULL;
235
236     mraa_spi_bus_t *spi = &(plat->spi_bus[bus]);
237     return spi;
238 }
239
240 mraa_pin_t*
241 mraa_setup_pwm(int pin)
242 {
243     if (plat == NULL)
244         return NULL;
245
246     if (plat->pins[pin].capabilites.pwm != 1)
247         return NULL;
248
249     if (plat->pins[pin].capabilites.gpio == 1) {
250         mraa_gpio_context mux_i;
251         mux_i = mraa_gpio_init_raw(plat->pins[pin].gpio.pinmap);
252         if (mux_i == NULL)
253             return NULL;
254         if (mraa_gpio_dir(mux_i, MRAA_GPIO_OUT) != MRAA_SUCCESS)
255             return NULL;
256         // Current REV D quirk. //TODO GEN 2
257         if (mraa_gpio_write(mux_i, 1) != MRAA_SUCCESS)
258             return NULL;
259         if (mraa_gpio_close(mux_i) != MRAA_SUCCESS)
260             return NULL;
261     }
262
263     if (plat->pins[pin].pwm.mux_total > 0)
264        if (mraa_setup_mux_mapped(plat->pins[pin].pwm) != MRAA_SUCCESS)
265             return NULL;
266
267     mraa_pin_t *ret;
268     ret = (mraa_pin_t*) malloc(sizeof(mraa_pin_t));
269     ret->pinmap = plat->pins[pin].pwm.pinmap;
270     ret->parent_id = plat->pins[pin].pwm.parent_id;
271     return ret;
272 }
273
274 void
275 mraa_result_print(mraa_result_t result)
276 {
277     switch (result) {
278         case MRAA_SUCCESS: fprintf(stderr, "MRAA: SUCCESS\n");
279                           break;
280         case MRAA_ERROR_FEATURE_NOT_IMPLEMENTED:
281                           fprintf(stderr, "MRAA: Feature not implemented.\n");
282                           break;
283         case MRAA_ERROR_FEATURE_NOT_SUPPORTED:
284                           fprintf(stderr, "MRAA: Feature not supported by Hardware.\n");
285                           break;
286         case MRAA_ERROR_INVALID_VERBOSITY_LEVEL:
287                           fprintf(stderr, "MRAA: Invalid verbosity level.\n");
288                           break;
289         case MRAA_ERROR_INVALID_PARAMETER:
290                           fprintf(stderr, "MRAA: Invalid parameter.\n");
291                           break;
292         case MRAA_ERROR_INVALID_HANDLE:
293                           fprintf(stderr, "MRAA: Invalid Handle.\n");
294                           break;
295         case MRAA_ERROR_NO_RESOURCES:
296                           fprintf(stderr, "MRAA: No resources.\n");
297                           break;
298         case MRAA_ERROR_INVALID_RESOURCE:
299                           fprintf(stderr, "MRAA: Invalid resource.\n");
300                           break;
301         case MRAA_ERROR_INVALID_QUEUE_TYPE:
302                           fprintf(stderr, "MRAA: Invalid Queue Type.\n");
303                           break;
304         case MRAA_ERROR_NO_DATA_AVAILABLE:
305                           fprintf(stderr, "MRAA: No Data available.\n");
306                           break;
307         case MRAA_ERROR_INVALID_PLATFORM:
308                           fprintf(stderr, "MRAA: Platform not recognised.\n");
309                           break;
310         case MRAA_ERROR_PLATFORM_NOT_INITIALISED:
311                           fprintf(stderr, "MRAA: Platform not initialised.\n");
312                           break;
313         case MRAA_ERROR_PLATFORM_ALREADY_INITIALISED:
314                           fprintf(stderr, "MRAA: Platform already initialised.\n");
315                           break;
316         case MRAA_ERROR_UNSPECIFIED:
317                           fprintf(stderr, "MRAA: Unspecified Error.\n");
318                           break;
319         default:     fprintf(stderr, "MRAA: Unrecognised error.\n");
320                           break;
321     }
322 }
323
324 mraa_boolean_t
325 mraa_pin_mode_test(int pin, mraa_pinmodes_t mode)
326 {
327     if (plat == NULL) {
328         mraa_init();
329         if (plat == NULL)
330             return 0;
331     }
332     if (pin > plat->phy_pin_count || pin < 0)
333         return 0;
334
335     switch(mode) {
336         case MRAA_PIN_VALID:
337             if (plat->pins[pin].capabilites.valid == 1)
338                 return 1;
339             break;
340         case MRAA_PIN_GPIO:
341             if (plat->pins[pin].capabilites.gpio ==1)
342                 return 1;
343             break;
344         case MRAA_PIN_PWM:
345             if (plat->pins[pin].capabilites.pwm ==1)
346                 return 1;
347             break;
348         case MRAA_PIN_FAST_GPIO:
349             if (plat->pins[pin].capabilites.fast_gpio ==1)
350                 return 1;
351             break;
352         case MRAA_PIN_SPI:
353             if (plat->pins[pin].capabilites.spi ==1)
354                 return 1;
355             break;
356         case MRAA_PIN_I2C:
357             if (plat->pins[pin].capabilites.i2c ==1)
358                 return 1;
359             break;
360         case MRAA_PIN_AIO:
361             if (pin < plat->aio_count)
362                 pin = pin + plat->gpio_count;
363             if (plat->pins[pin].capabilites.aio ==1)
364                 return 1;
365             break;
366         default: break;
367     }
368     return 0;
369 }
370
371 mraa_mmap_pin_t*
372 mraa_setup_mmap_gpio(int pin)
373 {
374     if (plat == NULL)
375         return NULL;
376
377     if (plat->pins[pin].capabilites.fast_gpio != 1)
378         return NULL;
379
380     if (plat->pins[pin].mmap.gpio.mux_total > 0)
381        if (mraa_setup_mux_mapped(plat->pins[pin].mmap.gpio) != MRAA_SUCCESS)
382             return NULL;
383
384     if (mraa_setup_mux_mapped(plat->pins[pin].mmap.gpio) != MRAA_SUCCESS)
385             return NULL;
386     mraa_mmap_pin_t *ret = &(plat->pins[pin].mmap);
387     return ret;
388 }
389
390 mraa_result_t
391 mraa_swap_complex_gpio(int pin, int out)
392 {
393     if (plat == NULL)
394         return MRAA_ERROR_INVALID_PLATFORM;
395
396     switch (platform_type) {
397         case MRAA_INTEL_GALILEO_GEN2:
398             if (plat->pins[pin].gpio.complex_cap.complex_pin != 1)
399                 return MRAA_SUCCESS;
400             if (plat->pins[pin].gpio.complex_cap.output_en == 1) {
401                 mraa_gpio_context output_e;
402                 output_e = mraa_gpio_init_raw(plat->pins[pin].gpio.output_enable);
403                 if (mraa_gpio_dir(output_e, MRAA_GPIO_OUT) != MRAA_SUCCESS)
404                     return MRAA_ERROR_INVALID_RESOURCE;
405                 int output_val;
406                 if (plat->pins[pin].gpio.complex_cap.output_en_high == 1)
407                     output_val = out;
408                 else
409                     if (out == 1)
410                         output_val = 0;
411                     else
412                         output_val = 1;
413                 if (mraa_gpio_write(output_e, output_val) != MRAA_SUCCESS)
414                     return MRAA_ERROR_INVALID_RESOURCE;
415             }
416             //if (plat->pins[pin].gpio.complex_cap.pullup_en == 1) {
417             //    mraa_gpio_context pullup_e;
418             //    pullup_e = mraa_gpio_init_raw(plat->pins[pin].gpio.pullup_enable);
419             //    if (mraa_gpio_mode(pullup_e, MRAA_GPIO_HIZ) != MRAA_SUCCESS)
420             //        return MRAA_ERROR_INVALID_RESOURCE;
421             //}
422             break;
423         default: return MRAA_SUCCESS;
424     }
425 }
426
427 mraa_platform_t mraa_get_platform_type()
428 {
429     return platform_type;
430 }
431
432 unsigned int
433 mraa_adc_raw_bits()
434 {
435     if (plat == NULL)
436         return 0;
437
438     if (plat->aio_count == 0)
439         return 0;
440
441     return plat->adc_raw;
442 }
443
444 unsigned int
445 mraa_adc_supported_bits()
446 {
447     if (plat == NULL)
448         return 0;
449
450     if (plat->aio_count == 0)
451         return 0;
452
453     return plat->adc_supported;
454 }
455
456 mraa_adv_func*
457 mraa_get_advance() {
458     return advance;
459 }