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