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.
32 #include "mraa_internal.h"
36 mraa_board_t* plat = NULL;
37 static mraa_platform_t platform_type = MRAA_UNKNOWN_PLATFORM;
38 mraa_adv_func_t* advance_func;
47 mraa_set_log_level(int level)
49 if (level <= 7 && level >= 0) {
50 setlogmask(LOG_UPTO(level));
53 return MRAA_ERROR_INVALID_PARAMETER;
56 mraa_result_t __attribute__((constructor))
60 return MRAA_ERROR_PLATFORM_ALREADY_INITIALISED;
63 uid_t proc_euid = geteuid();
64 struct passwd *proc_user = getpwuid(proc_euid);
67 setlogmask(LOG_UPTO(LOG_DEBUG));
69 setlogmask(LOG_UPTO(LOG_NOTICE));
72 openlog("libmraa", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
74 "libmraa version %s initialised by user '%s' with EUID %d",
76 (proc_user != NULL) ? proc_user->pw_name : "<unknown>",
80 char *err_msg = "mraa: FATAL error, "
81 "libmraa program must be run as root (EUID 0), "
83 syslog(LOG_ERR, "%s", err_msg);
84 fprintf(stderr, "%s", err_msg);
85 return MRAA_ERROR_PLATFORM_NOT_INITIALISED;
89 // Initialise python threads, this allows use to grab the GIL when we are
94 advance_func = (mraa_adv_func_t*) malloc(sizeof(mraa_adv_func_t));
95 memset(advance_func, 0, sizeof(mraa_adv_func_t));
98 // Use runtime x86 platform detection
99 platform_type = mraa_x86_platform();
101 // Use runtime ARM platform detection
102 platform_type = mraa_arm_platform();
104 #error mraa_ARCH NOTHING
108 printf("mraa: FATAL error, failed to initialise platform\n");
109 return MRAA_ERROR_PLATFORM_NOT_INITIALISED;
113 "libmraa initialised for platform '%s' of type %d",
114 mraa_get_platform_name(),
123 if (plat->pins != NULL) {
132 mraa_set_priority(const unsigned int priority)
134 struct sched_param sched_s;
136 memset(&sched_s, 0, sizeof(struct sched_param));
137 if (priority > sched_get_priority_max(SCHED_RR)) {
138 sched_s.sched_priority = sched_get_priority_max(SCHED_RR);
141 sched_s.sched_priority = priority;
144 return sched_setscheduler(0, SCHED_RR, &sched_s);
148 mraa_setup_mux_mapped(mraa_pin_t meta)
152 for (mi = 0; mi < meta.mux_total; mi++) {
153 mraa_gpio_context mux_i;
154 mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
156 return MRAA_ERROR_INVALID_HANDLE;
159 if (meta.mux[mi].value) {
160 if (mraa_gpio_dir(mux_i, MRAA_GPIO_OUT_HIGH) != MRAA_SUCCESS) {
161 mraa_gpio_close(mux_i);
162 return MRAA_ERROR_INVALID_RESOURCE;
165 if (mraa_gpio_dir(mux_i, MRAA_GPIO_OUT_LOW) != MRAA_SUCCESS) {
166 mraa_gpio_close(mux_i);
167 return MRAA_ERROR_INVALID_RESOURCE;
170 mraa_gpio_close(mux_i);
177 mraa_result_print(mraa_result_t result)
181 fprintf(stdout, "MRAA: SUCCESS\n");
183 case MRAA_ERROR_FEATURE_NOT_IMPLEMENTED:
184 fprintf(stdout, "MRAA: Feature not implemented.\n");
186 case MRAA_ERROR_FEATURE_NOT_SUPPORTED:
187 fprintf(stdout, "MRAA: Feature not supported by Hardware.\n");
189 case MRAA_ERROR_INVALID_VERBOSITY_LEVEL:
190 fprintf(stdout, "MRAA: Invalid verbosity level.\n");
192 case MRAA_ERROR_INVALID_PARAMETER:
193 fprintf(stdout, "MRAA: Invalid parameter.\n");
195 case MRAA_ERROR_INVALID_HANDLE:
196 fprintf(stdout, "MRAA: Invalid Handle.\n");
198 case MRAA_ERROR_NO_RESOURCES:
199 fprintf(stdout, "MRAA: No resources.\n");
201 case MRAA_ERROR_INVALID_RESOURCE:
202 fprintf(stdout, "MRAA: Invalid resource.\n");
204 case MRAA_ERROR_INVALID_QUEUE_TYPE:
205 fprintf(stdout, "MRAA: Invalid Queue Type.\n");
207 case MRAA_ERROR_NO_DATA_AVAILABLE:
208 fprintf(stdout, "MRAA: No Data available.\n");
210 case MRAA_ERROR_INVALID_PLATFORM:
211 fprintf(stdout, "MRAA: Platform not recognised.\n");
213 case MRAA_ERROR_PLATFORM_NOT_INITIALISED:
214 fprintf(stdout, "MRAA: Platform not initialised.\n");
216 case MRAA_ERROR_PLATFORM_ALREADY_INITIALISED:
217 fprintf(stdout, "MRAA: Platform already initialised.\n");
219 case MRAA_ERROR_UNSPECIFIED:
220 fprintf(stdout, "MRAA: Unspecified Error.\n");
223 fprintf(stdout, "MRAA: Unrecognised error.\n");
229 mraa_pin_mode_test(int pin, mraa_pinmodes_t mode)
236 if (pin > (plat->phy_pin_count -1) || pin < 0)
241 if (plat->pins[pin].capabilites.valid == 1)
245 if (plat->pins[pin].capabilites.gpio ==1)
249 if (plat->pins[pin].capabilites.pwm ==1)
252 case MRAA_PIN_FAST_GPIO:
253 if (plat->pins[pin].capabilites.fast_gpio ==1)
257 if (plat->pins[pin].capabilites.spi ==1)
261 if (plat->pins[pin].capabilites.i2c ==1)
265 if (plat->pins[pin].capabilites.aio ==1)
269 if (plat->pins[pin].capabilites.uart == 1)
273 syslog(LOG_NOTICE, "requested pinmode invalid");
279 mraa_platform_t mraa_get_platform_type()
281 return platform_type;
290 if (plat->aio_count == 0)
293 return plat->adc_raw;
297 mraa_adc_supported_bits()
302 if (plat->aio_count == 0)
305 return plat->adc_supported;
309 mraa_get_platform_name()
314 return (char*) plat->platform_name;
323 return plat->phy_pin_count;