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