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)
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);
133 return MRAA_ERROR_INVALID_HANDLE;
135 // this function will sometimes fail, however this is not critical as
136 // long as the write succeeds - Test case galileo gen2 pin2
137 mraa_gpio_dir(mux_i, MRAA_GPIO_OUT);
139 if (mraa_gpio_write(mux_i, meta.mux[mi].value) != MRAA_SUCCESS) {
140 mraa_gpio_close(mux_i);
141 return MRAA_ERROR_INVALID_RESOURCE;
143 mraa_gpio_close(mux_i);
150 mraa_result_print(mraa_result_t result)
154 fprintf(stdout, "MRAA: SUCCESS\n");
156 case MRAA_ERROR_FEATURE_NOT_IMPLEMENTED:
157 fprintf(stdout, "MRAA: Feature not implemented.\n");
159 case MRAA_ERROR_FEATURE_NOT_SUPPORTED:
160 fprintf(stdout, "MRAA: Feature not supported by Hardware.\n");
162 case MRAA_ERROR_INVALID_VERBOSITY_LEVEL:
163 fprintf(stdout, "MRAA: Invalid verbosity level.\n");
165 case MRAA_ERROR_INVALID_PARAMETER:
166 fprintf(stdout, "MRAA: Invalid parameter.\n");
168 case MRAA_ERROR_INVALID_HANDLE:
169 fprintf(stdout, "MRAA: Invalid Handle.\n");
171 case MRAA_ERROR_NO_RESOURCES:
172 fprintf(stdout, "MRAA: No resources.\n");
174 case MRAA_ERROR_INVALID_RESOURCE:
175 fprintf(stdout, "MRAA: Invalid resource.\n");
177 case MRAA_ERROR_INVALID_QUEUE_TYPE:
178 fprintf(stdout, "MRAA: Invalid Queue Type.\n");
180 case MRAA_ERROR_NO_DATA_AVAILABLE:
181 fprintf(stdout, "MRAA: No Data available.\n");
183 case MRAA_ERROR_INVALID_PLATFORM:
184 fprintf(stdout, "MRAA: Platform not recognised.\n");
186 case MRAA_ERROR_PLATFORM_NOT_INITIALISED:
187 fprintf(stdout, "MRAA: Platform not initialised.\n");
189 case MRAA_ERROR_PLATFORM_ALREADY_INITIALISED:
190 fprintf(stdout, "MRAA: Platform already initialised.\n");
192 case MRAA_ERROR_UNSPECIFIED:
193 fprintf(stdout, "MRAA: Unspecified Error.\n");
196 fprintf(stdout, "MRAA: Unrecognised error.\n");
202 mraa_pin_mode_test(int pin, mraa_pinmodes_t mode)
209 if (pin > plat->phy_pin_count || pin < 0)
214 if (plat->pins[pin].capabilites.valid == 1)
218 if (plat->pins[pin].capabilites.gpio ==1)
222 if (plat->pins[pin].capabilites.pwm ==1)
225 case MRAA_PIN_FAST_GPIO:
226 if (plat->pins[pin].capabilites.fast_gpio ==1)
230 if (plat->pins[pin].capabilites.spi ==1)
234 if (plat->pins[pin].capabilites.i2c ==1)
238 if (pin < plat->aio_count)
239 pin = pin + plat->gpio_count;
240 if (plat->pins[pin].capabilites.aio ==1)
248 mraa_platform_t mraa_get_platform_type()
250 return platform_type;
259 if (plat->aio_count == 0)
262 return plat->adc_raw;
266 mraa_adc_supported_bits()
271 if (plat->aio_count == 0)
274 return plat->adc_supported;
278 mraa_setup_uart(int index)
281 return MRAA_ERROR_PLATFORM_NOT_INITIALISED;
283 if (plat->uart_dev_count == 0)
284 return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
286 if (plat->uart_dev_count <= index)
287 return MRAA_ERROR_NO_RESOURCES;
289 int pos = plat->uart_dev[index].rx;
291 if (plat->pins[pos].uart.mux_total > 0)
292 if (mraa_setup_mux_mapped(plat->pins[pos].uart) != MRAA_SUCCESS)
293 return MRAA_ERROR_INVALID_RESOURCE;
297 pos = plat->uart_dev[index].tx;
298 if (plat->pins[pos].uart.mux_total > 0)
299 if (mraa_setup_mux_mapped(plat->pins[pos].uart) != MRAA_SUCCESS)
300 return MRAA_ERROR_INVALID_RESOURCE;
307 mraa_get_platform_name()
311 return plat->platform_name;