mraa: initial implementation of "hooks"
[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 int
102 mraa_set_priority(const unsigned int priority)
103 {
104     struct sched_param sched_s;
105
106     memset(&sched_s, 0, sizeof(struct sched_param));
107     if (priority > sched_get_priority_max(SCHED_RR)) {
108         sched_s.sched_priority = sched_get_priority_max(SCHED_RR);
109     }
110     else {
111         sched_s.sched_priority = priority;
112     }
113
114     return sched_setscheduler(0, SCHED_RR, &sched_s);
115 }
116
117 static mraa_result_t
118 mraa_setup_mux_mapped(mraa_pin_t meta)
119 {
120     int mi;
121     for (mi = 0; mi < meta.mux_total; mi++) {
122         mraa_gpio_context mux_i;
123         mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
124         if (mux_i == NULL)
125             return MRAA_ERROR_INVALID_HANDLE;
126         mraa_gpio_dir(mux_i, MRAA_GPIO_OUT);
127         if (mraa_gpio_write(mux_i, meta.mux[mi].value) != MRAA_SUCCESS)
128             return MRAA_ERROR_INVALID_RESOURCE;
129     }
130     return MRAA_SUCCESS;
131 }
132
133 unsigned int
134 mraa_setup_gpio(int pin)
135 {
136     if (plat == NULL)
137         return -1;
138
139     if (pin < 0 || pin > plat->phy_pin_count)
140         return -1;
141
142     if(plat->pins[pin].capabilites.gpio != 1)
143       return -1;
144
145     if (plat->pins[pin].gpio.mux_total > 0)
146        if (mraa_setup_mux_mapped(plat->pins[pin].gpio) != MRAA_SUCCESS)
147             return -1;
148     return plat->pins[pin].gpio.pinmap;
149 }
150
151 unsigned int
152 mraa_setup_aio(int aio)
153 {
154     if (plat == NULL)
155         return -3;
156
157     if (aio < 0 || aio > plat->aio_count)
158         return -1;
159
160     int pin = aio + plat->gpio_count;
161
162     if (plat->pins[pin].capabilites.aio != 1)
163       return -1;
164
165     if (plat->pins[pin].aio.mux_total > 0)
166        if (mraa_setup_mux_mapped(plat->pins[pin].aio) != MRAA_SUCCESS)
167             return -1;
168     return plat->pins[pin].aio.pinmap;
169 }
170
171 unsigned int
172 mraa_setup_i2c(int bus)
173 {
174     if (plat == NULL)
175         return -3;
176
177     if (plat->i2c_bus_count >! 0) {
178         fprintf(stderr, "No i2c buses defined in platform");
179         return -1;
180     }
181     if (bus >= plat->i2c_bus_count) {
182         fprintf(stderr, "Above i2c bus count");
183         return -1;
184     }
185
186     int pos = plat->i2c_bus[bus].sda;
187     if (plat->pins[pos].i2c.mux_total > 0)
188         if (mraa_setup_mux_mapped(plat->pins[pos].i2c) != MRAA_SUCCESS)
189              return -2;
190
191     pos = plat->i2c_bus[bus].scl;
192     if (plat->pins[pos].i2c.mux_total > 0)
193         if (mraa_setup_mux_mapped(plat->pins[pos].i2c) != MRAA_SUCCESS)
194              return -2;
195
196     return plat->i2c_bus[bus].bus_id;
197 }
198
199 mraa_spi_bus_t*
200 mraa_setup_spi(int bus)
201 {
202     if (plat == NULL)
203         return NULL;
204
205     if (plat->spi_bus_count >! 0) {
206         fprintf(stderr, "No spi buses defined in platform");
207         return NULL;
208     }
209     if (bus >= plat->spi_bus_count) {
210         fprintf(stderr, "Above spi bus count");
211         return NULL;
212     }
213
214     int pos = plat->spi_bus[bus].sclk;
215     if (plat->pins[pos].spi.mux_total > 0)
216         if (mraa_setup_mux_mapped(plat->pins[pos].spi) != MRAA_SUCCESS)
217              return NULL;
218
219     pos = plat->spi_bus[bus].mosi;
220     if (plat->pins[pos].spi.mux_total > 0)
221         if (mraa_setup_mux_mapped(plat->pins[pos].spi) != MRAA_SUCCESS)
222              return NULL;
223
224     pos = plat->spi_bus[bus].miso;
225     if (plat->pins[pos].spi.mux_total > 0)
226         if (mraa_setup_mux_mapped(plat->pins[pos].spi) != MRAA_SUCCESS)
227              return NULL;
228
229     mraa_spi_bus_t *spi = &(plat->spi_bus[bus]);
230     return spi;
231 }
232
233 mraa_pin_t*
234 mraa_setup_pwm(int pin)
235 {
236     if (plat == NULL)
237         return NULL;
238
239     if (plat->pins[pin].capabilites.pwm != 1)
240         return NULL;
241
242     if (plat->pins[pin].capabilites.gpio == 1) {
243         mraa_gpio_context mux_i;
244         mux_i = mraa_gpio_init_raw(plat->pins[pin].gpio.pinmap);
245         if (mux_i == NULL)
246             return NULL;
247         if (mraa_gpio_dir(mux_i, MRAA_GPIO_OUT) != MRAA_SUCCESS)
248             return NULL;
249         // Current REV D quirk. //TODO GEN 2
250         if (mraa_gpio_write(mux_i, 1) != MRAA_SUCCESS)
251             return NULL;
252         if (mraa_gpio_close(mux_i) != MRAA_SUCCESS)
253             return NULL;
254     }
255
256     if (plat->pins[pin].pwm.mux_total > 0)
257        if (mraa_setup_mux_mapped(plat->pins[pin].pwm) != MRAA_SUCCESS)
258             return NULL;
259
260     mraa_pin_t *ret;
261     ret = (mraa_pin_t*) malloc(sizeof(mraa_pin_t));
262     ret->pinmap = plat->pins[pin].pwm.pinmap;
263     ret->parent_id = plat->pins[pin].pwm.parent_id;
264     return ret;
265 }
266
267 void
268 mraa_result_print(mraa_result_t result)
269 {
270     switch (result) {
271         case MRAA_SUCCESS: fprintf(stderr, "MRAA: SUCCESS\n");
272                           break;
273         case MRAA_ERROR_FEATURE_NOT_IMPLEMENTED:
274                           fprintf(stderr, "MRAA: Feature not implemented.\n");
275                           break;
276         case MRAA_ERROR_FEATURE_NOT_SUPPORTED:
277                           fprintf(stderr, "MRAA: Feature not supported by Hardware.\n");
278                           break;
279         case MRAA_ERROR_INVALID_VERBOSITY_LEVEL:
280                           fprintf(stderr, "MRAA: Invalid verbosity level.\n");
281                           break;
282         case MRAA_ERROR_INVALID_PARAMETER:
283                           fprintf(stderr, "MRAA: Invalid parameter.\n");
284                           break;
285         case MRAA_ERROR_INVALID_HANDLE:
286                           fprintf(stderr, "MRAA: Invalid Handle.\n");
287                           break;
288         case MRAA_ERROR_NO_RESOURCES:
289                           fprintf(stderr, "MRAA: No resources.\n");
290                           break;
291         case MRAA_ERROR_INVALID_RESOURCE:
292                           fprintf(stderr, "MRAA: Invalid resource.\n");
293                           break;
294         case MRAA_ERROR_INVALID_QUEUE_TYPE:
295                           fprintf(stderr, "MRAA: Invalid Queue Type.\n");
296                           break;
297         case MRAA_ERROR_NO_DATA_AVAILABLE:
298                           fprintf(stderr, "MRAA: No Data available.\n");
299                           break;
300         case MRAA_ERROR_INVALID_PLATFORM:
301                           fprintf(stderr, "MRAA: Platform not recognised.\n");
302                           break;
303         case MRAA_ERROR_PLATFORM_NOT_INITIALISED:
304                           fprintf(stderr, "MRAA: Platform not initialised.\n");
305                           break;
306         case MRAA_ERROR_PLATFORM_ALREADY_INITIALISED:
307                           fprintf(stderr, "MRAA: Platform already initialised.\n");
308                           break;
309         case MRAA_ERROR_UNSPECIFIED:
310                           fprintf(stderr, "MRAA: Unspecified Error.\n");
311                           break;
312         default:     fprintf(stderr, "MRAA: Unrecognised error.\n");
313                           break;
314     }
315 }
316
317 mraa_boolean_t
318 mraa_pin_mode_test(int pin, mraa_pinmodes_t mode)
319 {
320     if (plat == NULL) {
321         mraa_init();
322         if (plat == NULL)
323             return 0;
324     }
325     if (pin > plat->phy_pin_count || pin < 0)
326         return 0;
327
328     switch(mode) {
329         case MRAA_PIN_VALID:
330             if (plat->pins[pin].capabilites.valid == 1)
331                 return 1;
332             break;
333         case MRAA_PIN_GPIO:
334             if (plat->pins[pin].capabilites.gpio ==1)
335                 return 1;
336             break;
337         case MRAA_PIN_PWM:
338             if (plat->pins[pin].capabilites.pwm ==1)
339                 return 1;
340             break;
341         case MRAA_PIN_FAST_GPIO:
342             if (plat->pins[pin].capabilites.fast_gpio ==1)
343                 return 1;
344             break;
345         case MRAA_PIN_SPI:
346             if (plat->pins[pin].capabilites.spi ==1)
347                 return 1;
348             break;
349         case MRAA_PIN_I2C:
350             if (plat->pins[pin].capabilites.i2c ==1)
351                 return 1;
352             break;
353         case MRAA_PIN_AIO:
354             if (pin < plat->aio_count)
355                 pin = pin + plat->gpio_count;
356             if (plat->pins[pin].capabilites.aio ==1)
357                 return 1;
358             break;
359         default: break;
360     }
361     return 0;
362 }
363
364 mraa_mmap_pin_t*
365 mraa_setup_mmap_gpio(int pin)
366 {
367     if (plat == NULL)
368         return NULL;
369
370     if (plat->pins[pin].capabilites.fast_gpio != 1)
371         return NULL;
372
373     if (plat->pins[pin].mmap.gpio.mux_total > 0)
374        if (mraa_setup_mux_mapped(plat->pins[pin].mmap.gpio) != MRAA_SUCCESS)
375             return NULL;
376
377     if (mraa_setup_mux_mapped(plat->pins[pin].mmap.gpio) != MRAA_SUCCESS)
378             return NULL;
379     mraa_mmap_pin_t *ret = &(plat->pins[pin].mmap);
380     return ret;
381 }
382
383 mraa_result_t
384 mraa_swap_complex_gpio(int pin, int out)
385 {
386     if (plat == NULL)
387         return MRAA_ERROR_INVALID_PLATFORM;
388
389     switch (platform_type) {
390         case MRAA_INTEL_GALILEO_GEN2:
391             if (plat->pins[pin].gpio.complex_cap.complex_pin != 1)
392                 return MRAA_SUCCESS;
393             if (plat->pins[pin].gpio.complex_cap.output_en == 1) {
394                 mraa_gpio_context output_e;
395                 output_e = mraa_gpio_init_raw(plat->pins[pin].gpio.output_enable);
396                 if (mraa_gpio_dir(output_e, MRAA_GPIO_OUT) != MRAA_SUCCESS)
397                     return MRAA_ERROR_INVALID_RESOURCE;
398                 int output_val;
399                 if (plat->pins[pin].gpio.complex_cap.output_en_high == 1)
400                     output_val = out;
401                 else
402                     if (out == 1)
403                         output_val = 0;
404                     else
405                         output_val = 1;
406                 if (mraa_gpio_write(output_e, output_val) != MRAA_SUCCESS)
407                     return MRAA_ERROR_INVALID_RESOURCE;
408             }
409             //if (plat->pins[pin].gpio.complex_cap.pullup_en == 1) {
410             //    mraa_gpio_context pullup_e;
411             //    pullup_e = mraa_gpio_init_raw(plat->pins[pin].gpio.pullup_enable);
412             //    if (mraa_gpio_mode(pullup_e, MRAA_GPIO_HIZ) != MRAA_SUCCESS)
413             //        return MRAA_ERROR_INVALID_RESOURCE;
414             //}
415             break;
416         default: return MRAA_SUCCESS;
417     }
418 }
419
420 mraa_platform_t mraa_get_platform_type()
421 {
422     return platform_type;
423 }
424
425 unsigned int
426 mraa_adc_raw_bits()
427 {
428     if (plat == NULL)
429         return 0;
430
431     if (plat->aio_count == 0)
432         return 0;
433
434     return plat->adc_raw;
435 }
436
437 unsigned int
438 mraa_adc_supported_bits()
439 {
440     if (plat == NULL)
441         return 0;
442
443     if (plat->aio_count == 0)
444         return 0;
445
446     return plat->adc_supported;
447 }
448
449 mraa_adv_func*
450 mraa_get_advance() {
451     return advance;
452 }