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