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 return MRAA_ERROR_PLATFORM_ALREADY_INITIALISED;
65 setlogmask(LOG_UPTO(LOG_DEBUG));
67 setlogmask(LOG_UPTO(LOG_NOTICE));
70 openlog("libmraa", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
71 syslog(LOG_DEBUG, "libmraa initialised by user %d", getuid());
74 // Initialise python threads, this allows use to grab the GIL when we are
79 advance_func = (mraa_adv_func_t*) malloc(sizeof(mraa_adv_func_t));
80 memset(advance_func, 0, sizeof(mraa_adv_func_t));
83 // Use runtime x86 platform detection
84 platform_type = mraa_x86_platform();
86 // Use runtime ARM platform detection
87 platform_type = mraa_arm_platform();
89 #error mraa_ARCH NOTHING
93 printf("mraa: FATAL error, failed to initialise platform\n");
94 return MRAA_ERROR_PLATFORM_NOT_INITIALISED;
96 syslog(LOG_INFO, "libmraa initialised for platform %d", platform_type);
109 mraa_set_priority(const unsigned int priority)
111 struct sched_param sched_s;
113 memset(&sched_s, 0, sizeof(struct sched_param));
114 if (priority > sched_get_priority_max(SCHED_RR)) {
115 sched_s.sched_priority = sched_get_priority_max(SCHED_RR);
118 sched_s.sched_priority = priority;
121 return sched_setscheduler(0, SCHED_RR, &sched_s);
125 mraa_setup_mux_mapped(mraa_pin_t meta)
128 for (mi = 0; mi < meta.mux_total; mi++) {
129 mraa_gpio_context mux_i;
130 mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
132 return MRAA_ERROR_INVALID_HANDLE;
133 mraa_gpio_dir(mux_i, MRAA_GPIO_OUT);
134 if (mraa_gpio_write(mux_i, meta.mux[mi].value) != MRAA_SUCCESS)
135 return MRAA_ERROR_INVALID_RESOURCE;
141 mraa_result_print(mraa_result_t result)
145 fprintf(stdout, "MRAA: SUCCESS\n");
147 case MRAA_ERROR_FEATURE_NOT_IMPLEMENTED:
148 fprintf(stdout, "MRAA: Feature not implemented.\n");
150 case MRAA_ERROR_FEATURE_NOT_SUPPORTED:
151 fprintf(stdout, "MRAA: Feature not supported by Hardware.\n");
153 case MRAA_ERROR_INVALID_VERBOSITY_LEVEL:
154 fprintf(stdout, "MRAA: Invalid verbosity level.\n");
156 case MRAA_ERROR_INVALID_PARAMETER:
157 fprintf(stdout, "MRAA: Invalid parameter.\n");
159 case MRAA_ERROR_INVALID_HANDLE:
160 fprintf(stdout, "MRAA: Invalid Handle.\n");
162 case MRAA_ERROR_NO_RESOURCES:
163 fprintf(stdout, "MRAA: No resources.\n");
165 case MRAA_ERROR_INVALID_RESOURCE:
166 fprintf(stdout, "MRAA: Invalid resource.\n");
168 case MRAA_ERROR_INVALID_QUEUE_TYPE:
169 fprintf(stdout, "MRAA: Invalid Queue Type.\n");
171 case MRAA_ERROR_NO_DATA_AVAILABLE:
172 fprintf(stdout, "MRAA: No Data available.\n");
174 case MRAA_ERROR_INVALID_PLATFORM:
175 fprintf(stdout, "MRAA: Platform not recognised.\n");
177 case MRAA_ERROR_PLATFORM_NOT_INITIALISED:
178 fprintf(stdout, "MRAA: Platform not initialised.\n");
180 case MRAA_ERROR_PLATFORM_ALREADY_INITIALISED:
181 fprintf(stdout, "MRAA: Platform already initialised.\n");
183 case MRAA_ERROR_UNSPECIFIED:
184 fprintf(stdout, "MRAA: Unspecified Error.\n");
187 fprintf(stdout, "MRAA: Unrecognised error.\n");
193 mraa_pin_mode_test(int pin, mraa_pinmodes_t mode)
200 if (pin > plat->phy_pin_count || pin < 0)
205 if (plat->pins[pin].capabilites.valid == 1)
209 if (plat->pins[pin].capabilites.gpio ==1)
213 if (plat->pins[pin].capabilites.pwm ==1)
216 case MRAA_PIN_FAST_GPIO:
217 if (plat->pins[pin].capabilites.fast_gpio ==1)
221 if (plat->pins[pin].capabilites.spi ==1)
225 if (plat->pins[pin].capabilites.i2c ==1)
229 if (pin < plat->aio_count)
230 pin = pin + plat->gpio_count;
231 if (plat->pins[pin].capabilites.aio ==1)
239 mraa_platform_t mraa_get_platform_type()
241 return platform_type;
250 if (plat->aio_count == 0)
253 return plat->adc_raw;
257 mraa_adc_supported_bits()
262 if (plat->aio_count == 0)
265 return plat->adc_supported;
269 mraa_setup_uart(int index)
272 return MRAA_ERROR_PLATFORM_NOT_INITIALISED;
274 if (plat->uart_dev_count == 0)
275 return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
277 if (plat->uart_dev_count <= index)
278 return MRAA_ERROR_NO_RESOURCES;
280 int pos = plat->uart_dev[index].rx;
282 if (plat->pins[pos].uart.mux_total > 0)
283 if (mraa_setup_mux_mapped(plat->pins[pos].uart) != MRAA_SUCCESS)
284 return MRAA_ERROR_INVALID_RESOURCE;
288 pos = plat->uart_dev[index].tx;
289 if (plat->pins[pos].uart.mux_total > 0)
290 if (mraa_setup_mux_mapped(plat->pins[pos].uart) != MRAA_SUCCESS)
291 return MRAA_ERROR_INVALID_RESOURCE;
298 mraa_get_platform_name()
302 return plat->platform_name;