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.
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:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
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.
33 #include "mraa_internal.h"
37 mraa_board_t* plat = NULL;
38 static mraa_platform_t platform_type = MRAA_UNKNOWN_PLATFORM;
39 mraa_adv_func_t* advance_func;
48 mraa_set_log_level(int level)
50 if (level <= 7 && level >= 0) {
51 setlogmask(LOG_UPTO(level));
54 return MRAA_ERROR_INVALID_PARAMETER;
57 mraa_result_t __attribute__((constructor))
61 setlogmask(LOG_UPTO(LOG_DEBUG));
63 setlogmask(LOG_UPTO(LOG_NOTICE));
66 openlog("libmraa", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
67 syslog(LOG_DEBUG, "libmraa initialised by user %d", getuid());
70 return MRAA_ERROR_PLATFORM_ALREADY_INITIALISED;
73 // Initialise python threads, this allows use to grab the GIL when we are
78 advance_func = (mraa_adv_func_t*) malloc(sizeof(mraa_adv_func_t));
79 memset(advance_func, 0, sizeof(mraa_adv_func_t));
82 // Use runtime x86 platform detection
83 platform_type = mraa_x86_platform();
85 // Use runtime ARM platform detection
86 platform_type = mraa_arm_platform();
88 #error mraa_ARCH NOTHING
92 printf("mraa: FATAL error, failed to initialise platform\n");
93 return MRAA_ERROR_PLATFORM_NOT_INITIALISED;
95 syslog(LOG_INFO, "libmraa initialised for platform %d", platform_type);
108 mraa_set_priority(const unsigned int priority)
110 struct sched_param sched_s;
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);
117 sched_s.sched_priority = priority;
120 return sched_setscheduler(0, SCHED_RR, &sched_s);
124 mraa_setup_mux_mapped(mraa_pin_t meta)
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);
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;
140 mraa_result_print(mraa_result_t result)
144 fprintf(stdout, "MRAA: SUCCESS\n");
146 case MRAA_ERROR_FEATURE_NOT_IMPLEMENTED:
147 fprintf(stdout, "MRAA: Feature not implemented.\n");
149 case MRAA_ERROR_FEATURE_NOT_SUPPORTED:
150 fprintf(stdout, "MRAA: Feature not supported by Hardware.\n");
152 case MRAA_ERROR_INVALID_VERBOSITY_LEVEL:
153 fprintf(stdout, "MRAA: Invalid verbosity level.\n");
155 case MRAA_ERROR_INVALID_PARAMETER:
156 fprintf(stdout, "MRAA: Invalid parameter.\n");
158 case MRAA_ERROR_INVALID_HANDLE:
159 fprintf(stdout, "MRAA: Invalid Handle.\n");
161 case MRAA_ERROR_NO_RESOURCES:
162 fprintf(stdout, "MRAA: No resources.\n");
164 case MRAA_ERROR_INVALID_RESOURCE:
165 fprintf(stdout, "MRAA: Invalid resource.\n");
167 case MRAA_ERROR_INVALID_QUEUE_TYPE:
168 fprintf(stdout, "MRAA: Invalid Queue Type.\n");
170 case MRAA_ERROR_NO_DATA_AVAILABLE:
171 fprintf(stdout, "MRAA: No Data available.\n");
173 case MRAA_ERROR_INVALID_PLATFORM:
174 fprintf(stdout, "MRAA: Platform not recognised.\n");
176 case MRAA_ERROR_PLATFORM_NOT_INITIALISED:
177 fprintf(stdout, "MRAA: Platform not initialised.\n");
179 case MRAA_ERROR_PLATFORM_ALREADY_INITIALISED:
180 fprintf(stdout, "MRAA: Platform already initialised.\n");
182 case MRAA_ERROR_UNSPECIFIED:
183 fprintf(stdout, "MRAA: Unspecified Error.\n");
186 fprintf(stdout, "MRAA: Unrecognised error.\n");
192 mraa_pin_mode_test(int pin, mraa_pinmodes_t mode)
199 if (pin > plat->phy_pin_count || pin < 0)
204 if (plat->pins[pin].capabilites.valid == 1)
208 if (plat->pins[pin].capabilites.gpio ==1)
212 if (plat->pins[pin].capabilites.pwm ==1)
215 case MRAA_PIN_FAST_GPIO:
216 if (plat->pins[pin].capabilites.fast_gpio ==1)
220 if (plat->pins[pin].capabilites.spi ==1)
224 if (plat->pins[pin].capabilites.i2c ==1)
228 if (pin < plat->aio_count)
229 pin = pin + plat->gpio_count;
230 if (plat->pins[pin].capabilites.aio ==1)
238 mraa_platform_t mraa_get_platform_type()
240 return platform_type;
249 if (plat->aio_count == 0)
252 return plat->adc_raw;
256 mraa_adc_supported_bits()
261 if (plat->aio_count == 0)
264 return plat->adc_supported;
268 mraa_setup_uart(int index)
271 return MRAA_ERROR_PLATFORM_NOT_INITIALISED;
273 if (plat->uart_dev_count == 0)
274 return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
276 if (plat->uart_dev_count <= index)
277 return MRAA_ERROR_NO_RESOURCES;
279 int pos = plat->uart_dev[index].rx;
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;
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;
297 mraa_get_platform_name()
301 return plat->platform_name;