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