Merge branch 'hook'
[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 mraa_adv_func advance_func;
41
42 const char *
43 mraa_get_version()
44 {
45     return gVERSION;
46 }
47
48 mraa_result_t __attribute__((constructor))
49 mraa_init()
50 {
51     /** Once more board definitions have been added,
52      *  A method for detecting them will need to be devised.
53      */
54     if (plat != NULL) {
55         return MRAA_ERROR_PLATFORM_ALREADY_INITIALISED;
56     }
57 #ifdef SWIGPYTHON
58     // Initialise python threads, this allows use to grab the GIL when we are
59     // required to do so
60     Py_InitializeEx(0);
61     PyEval_InitThreads();
62 #endif
63     // detect a galileo gen2 board
64     char *line = NULL;
65     // let getline allocate memory for *line
66     size_t len = 0;
67     FILE *fh = fopen("/sys/devices/virtual/dmi/id/board_name", "r");
68     if (fh != NULL) {
69         if (getline(&line, &len, fh) != -1) {
70             if (strncmp(line, "GalileoGen2", 10) == 0) {
71                 platform_type = MRAA_INTEL_GALILEO_GEN2;
72             } else {
73                 platform_type = MRAA_INTEL_GALILEO_GEN1;
74             }
75         }
76     }
77     free(line);
78     fclose(fh);
79
80     memset(&advance_func, 0, sizeof(mraa_adv_func));
81     switch(platform_type) {
82         case MRAA_INTEL_GALILEO_GEN2:
83             plat = mraa_intel_galileo_gen2(&advance_func);
84             break;
85         case MRAA_INTEL_GALILEO_GEN1:
86             plat = mraa_intel_galileo_rev_d(&advance_func);
87             break;
88         default:
89             plat = mraa_intel_galileo_rev_d(&advance_func);
90             fprintf(stderr, "Platform not found, initialising MRAA_INTEL_GALILEO_GEN1\n");
91     }
92
93     return MRAA_SUCCESS;
94 }
95
96 void
97 mraa_deinit()
98 {
99     free(plat->pins);
100     free(plat);
101 }
102
103 int
104 mraa_set_priority(const unsigned int priority)
105 {
106     struct sched_param sched_s;
107
108     memset(&sched_s, 0, sizeof(struct sched_param));
109     if (priority > sched_get_priority_max(SCHED_RR)) {
110         sched_s.sched_priority = sched_get_priority_max(SCHED_RR);
111     }
112     else {
113         sched_s.sched_priority = priority;
114     }
115
116     return sched_setscheduler(0, SCHED_RR, &sched_s);
117 }
118
119 static mraa_result_t
120 mraa_setup_mux_mapped(mraa_pin_t meta)
121 {
122     int mi;
123     for (mi = 0; mi < meta.mux_total; mi++) {
124         mraa_gpio_context mux_i;
125         mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
126         if (mux_i == NULL)
127             return MRAA_ERROR_INVALID_HANDLE;
128         mraa_gpio_dir(mux_i, MRAA_GPIO_OUT);
129         if (mraa_gpio_write(mux_i, meta.mux[mi].value) != MRAA_SUCCESS)
130             return MRAA_ERROR_INVALID_RESOURCE;
131     }
132     return MRAA_SUCCESS;
133 }
134
135 unsigned int
136 mraa_setup_gpio(int pin)
137 {
138     if (plat == NULL)
139         return -1;
140
141     if (pin < 0 || pin > plat->phy_pin_count)
142         return -1;
143
144     if(plat->pins[pin].capabilites.gpio != 1)
145       return -1;
146
147     if (plat->pins[pin].gpio.mux_total > 0)
148        if (mraa_setup_mux_mapped(plat->pins[pin].gpio) != MRAA_SUCCESS)
149             return -1;
150     return plat->pins[pin].gpio.pinmap;
151 }
152
153 unsigned int
154 mraa_setup_aio(int aio)
155 {
156     if (plat == NULL)
157         return -3;
158
159     if (aio < 0 || aio > plat->aio_count)
160         return -1;
161
162     int pin = aio + plat->gpio_count;
163
164     if (plat->pins[pin].capabilites.aio != 1)
165       return -1;
166
167     if (plat->pins[pin].aio.mux_total > 0)
168        if (mraa_setup_mux_mapped(plat->pins[pin].aio) != MRAA_SUCCESS)
169             return -1;
170     return plat->pins[pin].aio.pinmap;
171 }
172
173 unsigned int
174 mraa_setup_i2c(int bus)
175 {
176     if (plat == NULL)
177         return -3;
178
179     if (plat->i2c_bus_count >! 0) {
180         fprintf(stderr, "No i2c buses defined in platform");
181         return -1;
182     }
183     if (bus >= plat->i2c_bus_count) {
184         fprintf(stderr, "Above i2c bus count");
185         return -1;
186     }
187
188     int pos = plat->i2c_bus[bus].sda;
189     if (plat->pins[pos].i2c.mux_total > 0)
190         if (mraa_setup_mux_mapped(plat->pins[pos].i2c) != MRAA_SUCCESS)
191              return -2;
192
193     pos = plat->i2c_bus[bus].scl;
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     return plat->i2c_bus[bus].bus_id;
199 }
200
201 mraa_spi_bus_t*
202 mraa_setup_spi(int bus)
203 {
204     if (plat == NULL)
205         return NULL;
206
207     if (plat->spi_bus_count >! 0) {
208         fprintf(stderr, "No spi buses defined in platform");
209         return NULL;
210     }
211     if (bus >= plat->spi_bus_count) {
212         fprintf(stderr, "Above spi bus count");
213         return NULL;
214     }
215
216     int pos = plat->spi_bus[bus].sclk;
217     if (plat->pins[pos].spi.mux_total > 0)
218         if (mraa_setup_mux_mapped(plat->pins[pos].spi) != MRAA_SUCCESS)
219              return NULL;
220
221     pos = plat->spi_bus[bus].mosi;
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].miso;
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     mraa_spi_bus_t *spi = &(plat->spi_bus[bus]);
232     return spi;
233 }
234
235 mraa_pin_t*
236 mraa_setup_pwm(int pin)
237 {
238     if (plat == NULL)
239         return NULL;
240
241     if (plat->pins[pin].capabilites.pwm != 1)
242         return NULL;
243
244     if (plat->pins[pin].capabilites.gpio == 1) {
245         mraa_gpio_context mux_i;
246         mux_i = mraa_gpio_init_raw(plat->pins[pin].gpio.pinmap);
247         if (mux_i == NULL)
248             return NULL;
249         if (mraa_gpio_dir(mux_i, MRAA_GPIO_OUT) != MRAA_SUCCESS)
250             return NULL;
251         // Current REV D quirk. //TODO GEN 2
252         if (mraa_gpio_write(mux_i, 1) != MRAA_SUCCESS)
253             return NULL;
254         if (mraa_gpio_close(mux_i) != MRAA_SUCCESS)
255             return NULL;
256     }
257
258     if (plat->pins[pin].pwm.mux_total > 0)
259        if (mraa_setup_mux_mapped(plat->pins[pin].pwm) != MRAA_SUCCESS)
260             return NULL;
261
262     mraa_pin_t *ret;
263     ret = (mraa_pin_t*) malloc(sizeof(mraa_pin_t));
264     ret->pinmap = plat->pins[pin].pwm.pinmap;
265     ret->parent_id = plat->pins[pin].pwm.parent_id;
266     return ret;
267 }
268
269 void
270 mraa_result_print(mraa_result_t result)
271 {
272     switch (result) {
273         case MRAA_SUCCESS: fprintf(stderr, "MRAA: SUCCESS\n");
274                           break;
275         case MRAA_ERROR_FEATURE_NOT_IMPLEMENTED:
276                           fprintf(stderr, "MRAA: Feature not implemented.\n");
277                           break;
278         case MRAA_ERROR_FEATURE_NOT_SUPPORTED:
279                           fprintf(stderr, "MRAA: Feature not supported by Hardware.\n");
280                           break;
281         case MRAA_ERROR_INVALID_VERBOSITY_LEVEL:
282                           fprintf(stderr, "MRAA: Invalid verbosity level.\n");
283                           break;
284         case MRAA_ERROR_INVALID_PARAMETER:
285                           fprintf(stderr, "MRAA: Invalid parameter.\n");
286                           break;
287         case MRAA_ERROR_INVALID_HANDLE:
288                           fprintf(stderr, "MRAA: Invalid Handle.\n");
289                           break;
290         case MRAA_ERROR_NO_RESOURCES:
291                           fprintf(stderr, "MRAA: No resources.\n");
292                           break;
293         case MRAA_ERROR_INVALID_RESOURCE:
294                           fprintf(stderr, "MRAA: Invalid resource.\n");
295                           break;
296         case MRAA_ERROR_INVALID_QUEUE_TYPE:
297                           fprintf(stderr, "MRAA: Invalid Queue Type.\n");
298                           break;
299         case MRAA_ERROR_NO_DATA_AVAILABLE:
300                           fprintf(stderr, "MRAA: No Data available.\n");
301                           break;
302         case MRAA_ERROR_INVALID_PLATFORM:
303                           fprintf(stderr, "MRAA: Platform not recognised.\n");
304                           break;
305         case MRAA_ERROR_PLATFORM_NOT_INITIALISED:
306                           fprintf(stderr, "MRAA: Platform not initialised.\n");
307                           break;
308         case MRAA_ERROR_PLATFORM_ALREADY_INITIALISED:
309                           fprintf(stderr, "MRAA: Platform already initialised.\n");
310                           break;
311         case MRAA_ERROR_UNSPECIFIED:
312                           fprintf(stderr, "MRAA: Unspecified Error.\n");
313                           break;
314         default:     fprintf(stderr, "MRAA: Unrecognised error.\n");
315                           break;
316     }
317 }
318
319 mraa_boolean_t
320 mraa_pin_mode_test(int pin, mraa_pinmodes_t mode)
321 {
322     if (plat == NULL) {
323         mraa_init();
324         if (plat == NULL)
325             return 0;
326     }
327     if (pin > plat->phy_pin_count || pin < 0)
328         return 0;
329
330     switch(mode) {
331         case MRAA_PIN_VALID:
332             if (plat->pins[pin].capabilites.valid == 1)
333                 return 1;
334             break;
335         case MRAA_PIN_GPIO:
336             if (plat->pins[pin].capabilites.gpio ==1)
337                 return 1;
338             break;
339         case MRAA_PIN_PWM:
340             if (plat->pins[pin].capabilites.pwm ==1)
341                 return 1;
342             break;
343         case MRAA_PIN_FAST_GPIO:
344             if (plat->pins[pin].capabilites.fast_gpio ==1)
345                 return 1;
346             break;
347         case MRAA_PIN_SPI:
348             if (plat->pins[pin].capabilites.spi ==1)
349                 return 1;
350             break;
351         case MRAA_PIN_I2C:
352             if (plat->pins[pin].capabilites.i2c ==1)
353                 return 1;
354             break;
355         case MRAA_PIN_AIO:
356             if (pin < plat->aio_count)
357                 pin = pin + plat->gpio_count;
358             if (plat->pins[pin].capabilites.aio ==1)
359                 return 1;
360             break;
361         default: break;
362     }
363     return 0;
364 }
365
366 mraa_mmap_pin_t*
367 mraa_setup_mmap_gpio(int pin)
368 {
369     if (plat == NULL)
370         return NULL;
371
372     if (plat->pins[pin].capabilites.fast_gpio != 1)
373         return NULL;
374
375     if (plat->pins[pin].mmap.gpio.mux_total > 0)
376        if (mraa_setup_mux_mapped(plat->pins[pin].mmap.gpio) != MRAA_SUCCESS)
377             return NULL;
378
379     if (mraa_setup_mux_mapped(plat->pins[pin].mmap.gpio) != MRAA_SUCCESS)
380             return NULL;
381     mraa_mmap_pin_t *ret = &(plat->pins[pin].mmap);
382     return ret;
383 }
384
385 mraa_result_t
386 mraa_swap_complex_gpio(int pin, int out)
387 {
388     if (plat == NULL)
389         return MRAA_ERROR_INVALID_PLATFORM;
390
391     switch (platform_type) {
392         case MRAA_INTEL_GALILEO_GEN2:
393             if (plat->pins[pin].gpio.complex_cap.complex_pin != 1)
394                 return MRAA_SUCCESS;
395             if (plat->pins[pin].gpio.complex_cap.output_en == 1) {
396                 mraa_gpio_context output_e;
397                 output_e = mraa_gpio_init_raw(plat->pins[pin].gpio.output_enable);
398                 if (mraa_gpio_dir(output_e, MRAA_GPIO_OUT) != MRAA_SUCCESS)
399                     return MRAA_ERROR_INVALID_RESOURCE;
400                 int output_val;
401                 if (plat->pins[pin].gpio.complex_cap.output_en_high == 1)
402                     output_val = out;
403                 else
404                     if (out == 1)
405                         output_val = 0;
406                     else
407                         output_val = 1;
408                 if (mraa_gpio_write(output_e, output_val) != MRAA_SUCCESS)
409                     return MRAA_ERROR_INVALID_RESOURCE;
410             }
411             //if (plat->pins[pin].gpio.complex_cap.pullup_en == 1) {
412             //    mraa_gpio_context pullup_e;
413             //    pullup_e = mraa_gpio_init_raw(plat->pins[pin].gpio.pullup_enable);
414             //    if (mraa_gpio_mode(pullup_e, MRAA_GPIO_HIZ) != MRAA_SUCCESS)
415             //        return MRAA_ERROR_INVALID_RESOURCE;
416             //}
417             break;
418         default: return MRAA_SUCCESS;
419     }
420 }
421
422 mraa_platform_t mraa_get_platform_type()
423 {
424     return platform_type;
425 }
426
427 unsigned int
428 mraa_adc_raw_bits()
429 {
430     if (plat == NULL)
431         return 0;
432
433     if (plat->aio_count == 0)
434         return 0;
435
436     return plat->adc_raw;
437 }
438
439 unsigned int
440 mraa_adc_supported_bits()
441 {
442     if (plat == NULL)
443         return 0;
444
445     if (plat->aio_count == 0)
446         return 0;
447
448     return plat->adc_supported;
449 }
450
451 mraa_result_t
452 mraa_setup_uart(int index)
453 {
454     if (plat == NULL)
455         return MRAA_ERROR_PLATFORM_NOT_INITIALISED;
456
457     if (plat->uart_dev_count == 0)
458         return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
459
460     if (plat->uart_dev_count <= index)
461         return MRAA_ERROR_NO_RESOURCES;
462
463     int pos = plat->uart_dev[index].rx;
464     if (pos >= 0) {
465         if (plat->pins[pos].uart.mux_total > 0)
466             if (mraa_setup_mux_mapped(plat->pins[pos].uart) != MRAA_SUCCESS)
467                 return MRAA_ERROR_INVALID_RESOURCE;
468     }
469
470     if (pos >= 0) {
471         pos = plat->uart_dev[index].tx;
472         if (plat->pins[pos].uart.mux_total > 0)
473             if (mraa_setup_mux_mapped(plat->pins[pos].uart) != MRAA_SUCCESS)
474                 return MRAA_ERROR_INVALID_RESOURCE;
475     }
476
477     return MRAA_SUCCESS;
478 }