arm: add initial skeleton for arm platform support
[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 #define DEBUG
32
33 #include "mraa_internal.h"
34 #include "gpio.h"
35 #include "version.h"
36
37 mraa_board_t* plat = NULL;
38 static mraa_platform_t platform_type = MRAA_UNKNOWN_PLATFORM;
39 mraa_adv_func_t* advance_func;
40
41 const char *
42 mraa_get_version()
43 {
44     return gVERSION;
45 }
46
47 mraa_result_t
48 mraa_set_log_level(int level)
49 {
50     if (level <= 7 && level >= 0) {
51         setlogmask(LOG_UPTO(level));
52         return MRAA_SUCCESS;
53     }
54     return MRAA_ERROR_INVALID_PARAMETER;
55 }
56
57 mraa_result_t __attribute__((constructor))
58 mraa_init()
59 {
60 #ifdef DEBUG
61     setlogmask(LOG_UPTO(LOG_DEBUG));
62 #else
63     setlogmask(LOG_UPTO(LOG_NOTICE));
64 #endif
65
66     openlog("libmraa", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
67     syslog(LOG_DEBUG, "libmraa initialised by user %d", getuid());
68
69     if (plat != NULL) {
70         return MRAA_ERROR_PLATFORM_ALREADY_INITIALISED;
71     }
72 #ifdef SWIGPYTHON
73     // Initialise python threads, this allows use to grab the GIL when we are
74     // required to do so
75     Py_InitializeEx(0);
76     PyEval_InitThreads();
77 #endif
78     advance_func = (mraa_adv_func_t*) malloc(sizeof(mraa_adv_func_t));
79     memset(advance_func, 0, sizeof(mraa_adv_func_t));
80
81 #ifdef X86PLAT
82     // Use runtime x86 platform detection
83     platform_type = mraa_x86_platform();
84 #elif ARMPLAT
85     // Use runtime ARM platform detection
86     platform_type = mraa_arm_platform();
87 #else
88     #error mraa_ARCH NOTHING
89 #endif
90
91     if (plat == NULL) {
92         printf("mraa: FATAL error, failed to initialise platform\n");
93         return MRAA_ERROR_PLATFORM_NOT_INITIALISED;
94     }
95     syslog(LOG_INFO, "libmraa initialised for platform %d", platform_type);
96     return MRAA_SUCCESS;
97 }
98
99 void
100 mraa_deinit()
101 {
102     free(plat->pins);
103     free(plat);
104     closelog();
105 }
106
107 int
108 mraa_set_priority(const unsigned int priority)
109 {
110     struct sched_param sched_s;
111
112     memset(&sched_s, 0, sizeof(struct sched_param));
113     if (priority > sched_get_priority_max(SCHED_RR)) {
114         sched_s.sched_priority = sched_get_priority_max(SCHED_RR);
115     }
116     else {
117         sched_s.sched_priority = priority;
118     }
119
120     return sched_setscheduler(0, SCHED_RR, &sched_s);
121 }
122
123 mraa_result_t
124 mraa_setup_mux_mapped(mraa_pin_t meta)
125 {
126     int mi;
127     for (mi = 0; mi < meta.mux_total; mi++) {
128         mraa_gpio_context mux_i;
129         mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
130         if (mux_i == NULL)
131             return MRAA_ERROR_INVALID_HANDLE;
132         mraa_gpio_dir(mux_i, MRAA_GPIO_OUT);
133         if (mraa_gpio_write(mux_i, meta.mux[mi].value) != MRAA_SUCCESS)
134             return MRAA_ERROR_INVALID_RESOURCE;
135     }
136     return MRAA_SUCCESS;
137 }
138
139 void
140 mraa_result_print(mraa_result_t result)
141 {
142     switch (result) {
143         case MRAA_SUCCESS:
144             fprintf(stdout, "MRAA: SUCCESS\n");
145             break;
146         case MRAA_ERROR_FEATURE_NOT_IMPLEMENTED:
147             fprintf(stdout, "MRAA: Feature not implemented.\n");
148             break;
149         case MRAA_ERROR_FEATURE_NOT_SUPPORTED:
150             fprintf(stdout, "MRAA: Feature not supported by Hardware.\n");
151             break;
152         case MRAA_ERROR_INVALID_VERBOSITY_LEVEL:
153             fprintf(stdout, "MRAA: Invalid verbosity level.\n");
154             break;
155         case MRAA_ERROR_INVALID_PARAMETER:
156             fprintf(stdout, "MRAA: Invalid parameter.\n");
157             break;
158         case MRAA_ERROR_INVALID_HANDLE:
159             fprintf(stdout, "MRAA: Invalid Handle.\n");
160             break;
161         case MRAA_ERROR_NO_RESOURCES:
162             fprintf(stdout, "MRAA: No resources.\n");
163             break;
164         case MRAA_ERROR_INVALID_RESOURCE:
165             fprintf(stdout, "MRAA: Invalid resource.\n");
166             break;
167         case MRAA_ERROR_INVALID_QUEUE_TYPE:
168             fprintf(stdout, "MRAA: Invalid Queue Type.\n");
169             break;
170         case MRAA_ERROR_NO_DATA_AVAILABLE:
171             fprintf(stdout, "MRAA: No Data available.\n");
172             break;
173         case MRAA_ERROR_INVALID_PLATFORM:
174             fprintf(stdout, "MRAA: Platform not recognised.\n");
175             break;
176         case MRAA_ERROR_PLATFORM_NOT_INITIALISED:
177             fprintf(stdout, "MRAA: Platform not initialised.\n");
178             break;
179         case MRAA_ERROR_PLATFORM_ALREADY_INITIALISED:
180             fprintf(stdout, "MRAA: Platform already initialised.\n");
181             break;
182         case MRAA_ERROR_UNSPECIFIED:
183             fprintf(stdout, "MRAA: Unspecified Error.\n");
184             break;
185         default:
186             fprintf(stdout, "MRAA: Unrecognised error.\n");
187             break;
188     }
189 }
190
191 mraa_boolean_t
192 mraa_pin_mode_test(int pin, mraa_pinmodes_t mode)
193 {
194     if (plat == NULL) {
195         mraa_init();
196         if (plat == NULL)
197             return 0;
198     }
199     if (pin > plat->phy_pin_count || pin < 0)
200         return 0;
201
202     switch(mode) {
203         case MRAA_PIN_VALID:
204             if (plat->pins[pin].capabilites.valid == 1)
205                 return 1;
206             break;
207         case MRAA_PIN_GPIO:
208             if (plat->pins[pin].capabilites.gpio ==1)
209                 return 1;
210             break;
211         case MRAA_PIN_PWM:
212             if (plat->pins[pin].capabilites.pwm ==1)
213                 return 1;
214             break;
215         case MRAA_PIN_FAST_GPIO:
216             if (plat->pins[pin].capabilites.fast_gpio ==1)
217                 return 1;
218             break;
219         case MRAA_PIN_SPI:
220             if (plat->pins[pin].capabilites.spi ==1)
221                 return 1;
222             break;
223         case MRAA_PIN_I2C:
224             if (plat->pins[pin].capabilites.i2c ==1)
225                 return 1;
226             break;
227         case MRAA_PIN_AIO:
228             if (pin < plat->aio_count)
229                 pin = pin + plat->gpio_count;
230             if (plat->pins[pin].capabilites.aio ==1)
231                 return 1;
232             break;
233         default: break;
234     }
235     return 0;
236 }
237
238 mraa_platform_t mraa_get_platform_type()
239 {
240     return platform_type;
241 }
242
243 unsigned int
244 mraa_adc_raw_bits()
245 {
246     if (plat == NULL)
247         return 0;
248
249     if (plat->aio_count == 0)
250         return 0;
251
252     return plat->adc_raw;
253 }
254
255 unsigned int
256 mraa_adc_supported_bits()
257 {
258     if (plat == NULL)
259         return 0;
260
261     if (plat->aio_count == 0)
262         return 0;
263
264     return plat->adc_supported;
265 }
266
267 mraa_result_t
268 mraa_setup_uart(int index)
269 {
270     if (plat == NULL)
271         return MRAA_ERROR_PLATFORM_NOT_INITIALISED;
272
273     if (plat->uart_dev_count == 0)
274         return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
275
276     if (plat->uart_dev_count <= index)
277         return MRAA_ERROR_NO_RESOURCES;
278
279     int pos = plat->uart_dev[index].rx;
280     if (pos >= 0) {
281         if (plat->pins[pos].uart.mux_total > 0)
282             if (mraa_setup_mux_mapped(plat->pins[pos].uart) != MRAA_SUCCESS)
283                 return MRAA_ERROR_INVALID_RESOURCE;
284     }
285
286     if (pos >= 0) {
287         pos = plat->uart_dev[index].tx;
288         if (plat->pins[pos].uart.mux_total > 0)
289             if (mraa_setup_mux_mapped(plat->pins[pos].uart) != MRAA_SUCCESS)
290                 return MRAA_ERROR_INVALID_RESOURCE;
291     }
292
293     return MRAA_SUCCESS;
294 }
295
296 char*
297 mraa_get_platform_name()
298 {
299     if (plat == NULL)
300         return "Unknown";
301     return plat->platform_name;
302 }