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