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.
31 #include "mraa_internal.h"
32 #include "intel_galileo_rev_d.h"
33 #include "intel_galileo_rev_g.h"
34 #include "intel_edison_fab_c.h"
35 #include "intel_de3815.h"
39 mraa_board_t* plat = NULL;
40 static mraa_platform_t platform_type = MRAA_UNKNOWN_PLATFORM;
41 mraa_adv_func_t* advance_func;
50 mraa_set_log_level(int level)
52 if (level <= 7 && level >= 0) {
53 setlogmask(LOG_UPTO(level));
56 return MRAA_ERROR_INVALID_PARAMETER;
59 mraa_result_t __attribute__((constructor))
63 setlogmask(LOG_UPTO(LOG_DEBUG));
65 setlogmask(LOG_UPTO(LOG_NOTICE));
68 openlog("libmraa", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
69 syslog(LOG_DEBUG, "libmraa initialised by user %d", getuid());
72 return MRAA_ERROR_PLATFORM_ALREADY_INITIALISED;
75 // Initialise python threads, this allows use to grab the GIL when we are
80 // detect a galileo gen2 board
82 // let getline allocate memory for *line
84 FILE *fh = fopen("/sys/devices/virtual/dmi/id/board_name", "r");
86 if (getline(&line, &len, fh) != -1) {
87 if (strncmp(line, "GalileoGen2", 10) == 0) {
88 platform_type = MRAA_INTEL_GALILEO_GEN2;
89 } else if (strncmp(line, "BODEGA BAY", 10) == 0) {
90 platform_type = MRAA_INTEL_EDISON_FAB_C;
91 } else if (strncmp(line, "SALT BAY", 7) == 0) {
92 platform_type = MRAA_INTEL_EDISON_FAB_C;
93 } else if (strncmp(line, "DE3815", 6) == 0) {
94 platform_type = MRAA_INTEL_DE3815;
96 platform_type = MRAA_INTEL_GALILEO_GEN1;
103 advance_func = (mraa_adv_func_t*) malloc(sizeof(mraa_adv_func_t));
104 memset(advance_func, 0, sizeof(mraa_adv_func_t));
106 switch(platform_type) {
107 case MRAA_INTEL_GALILEO_GEN2:
108 plat = mraa_intel_galileo_gen2();
110 case MRAA_INTEL_GALILEO_GEN1:
111 plat = mraa_intel_galileo_rev_d();
113 case MRAA_INTEL_EDISON_FAB_C:
114 plat = mraa_intel_edison_fab_c();
116 case MRAA_INTEL_DE3815:
117 plat = mraa_intel_de3815();
120 plat = mraa_intel_galileo_rev_d();
121 syslog(LOG_ERR, "Platform not found, initialising MRAA_INTEL_GALILEO_GEN1");
124 syslog(LOG_NOTICE, "libmraa initialised for platform %d", platform_type);
137 mraa_set_priority(const unsigned int priority)
139 struct sched_param sched_s;
141 memset(&sched_s, 0, sizeof(struct sched_param));
142 if (priority > sched_get_priority_max(SCHED_RR)) {
143 sched_s.sched_priority = sched_get_priority_max(SCHED_RR);
146 sched_s.sched_priority = priority;
149 return sched_setscheduler(0, SCHED_RR, &sched_s);
153 mraa_setup_mux_mapped(mraa_pin_t meta)
156 for (mi = 0; mi < meta.mux_total; mi++) {
157 mraa_gpio_context mux_i;
158 mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
160 return MRAA_ERROR_INVALID_HANDLE;
161 mraa_gpio_dir(mux_i, MRAA_GPIO_OUT);
162 if (mraa_gpio_write(mux_i, meta.mux[mi].value) != MRAA_SUCCESS)
163 return MRAA_ERROR_INVALID_RESOURCE;
169 mraa_setup_spi(int bus)
174 if (plat->spi_bus_count >! 0) {
175 syslog(LOG_ERR, "No spi buses defined in platform");
178 if (plat->spi_bus_count == 1) {
179 bus = plat->def_spi_bus;
181 if (bus >= plat->spi_bus_count) {
182 syslog(LOG_ERR, "Above spi bus count");
186 int pos = plat->spi_bus[bus].sclk;
187 if (plat->pins[pos].spi.mux_total > 0)
188 if (mraa_setup_mux_mapped(plat->pins[pos].spi) != MRAA_SUCCESS)
191 pos = plat->spi_bus[bus].mosi;
192 if (plat->pins[pos].spi.mux_total > 0)
193 if (mraa_setup_mux_mapped(plat->pins[pos].spi) != MRAA_SUCCESS)
196 pos = plat->spi_bus[bus].miso;
197 if (plat->pins[pos].spi.mux_total > 0)
198 if (mraa_setup_mux_mapped(plat->pins[pos].spi) != MRAA_SUCCESS)
201 pos = plat->spi_bus[bus].cs;
202 if (plat->pins[pos].spi.mux_total > 0)
203 if (mraa_setup_mux_mapped(plat->pins[pos].spi) != MRAA_SUCCESS)
206 pos = plat->spi_bus[bus].cs;
207 if (plat->pins[pos].spi.mux_total > 0)
208 if (mraa_setup_mux_mapped(plat->pins[pos].spi) != MRAA_SUCCESS)
211 mraa_spi_bus_t *spi = &(plat->spi_bus[bus]);
216 mraa_setup_pwm(int pin)
221 if (plat->pins[pin].capabilites.pwm != 1)
224 if (plat->pins[pin].capabilites.gpio == 1) {
225 mraa_gpio_context mux_i;
226 mux_i = mraa_gpio_init_raw(plat->pins[pin].gpio.pinmap);
229 if (mraa_gpio_dir(mux_i, MRAA_GPIO_OUT) != MRAA_SUCCESS)
231 // Current REV D quirk. //TODO GEN 2
232 if (mraa_gpio_write(mux_i, 1) != MRAA_SUCCESS)
234 if (mraa_gpio_close(mux_i) != MRAA_SUCCESS)
238 if (plat->pins[pin].pwm.mux_total > 0)
239 if (mraa_setup_mux_mapped(plat->pins[pin].pwm) != MRAA_SUCCESS)
243 ret = (mraa_pin_t*) malloc(sizeof(mraa_pin_t));
244 ret->pinmap = plat->pins[pin].pwm.pinmap;
245 ret->parent_id = plat->pins[pin].pwm.parent_id;
250 mraa_result_print(mraa_result_t result)
254 fprintf(stdout, "MRAA: SUCCESS\n");
256 case MRAA_ERROR_FEATURE_NOT_IMPLEMENTED:
257 fprintf(stdout, "MRAA: Feature not implemented.\n");
259 case MRAA_ERROR_FEATURE_NOT_SUPPORTED:
260 fprintf(stdout, "MRAA: Feature not supported by Hardware.\n");
262 case MRAA_ERROR_INVALID_VERBOSITY_LEVEL:
263 fprintf(stdout, "MRAA: Invalid verbosity level.\n");
265 case MRAA_ERROR_INVALID_PARAMETER:
266 fprintf(stdout, "MRAA: Invalid parameter.\n");
268 case MRAA_ERROR_INVALID_HANDLE:
269 fprintf(stdout, "MRAA: Invalid Handle.\n");
271 case MRAA_ERROR_NO_RESOURCES:
272 fprintf(stdout, "MRAA: No resources.\n");
274 case MRAA_ERROR_INVALID_RESOURCE:
275 fprintf(stdout, "MRAA: Invalid resource.\n");
277 case MRAA_ERROR_INVALID_QUEUE_TYPE:
278 fprintf(stdout, "MRAA: Invalid Queue Type.\n");
280 case MRAA_ERROR_NO_DATA_AVAILABLE:
281 fprintf(stdout, "MRAA: No Data available.\n");
283 case MRAA_ERROR_INVALID_PLATFORM:
284 fprintf(stdout, "MRAA: Platform not recognised.\n");
286 case MRAA_ERROR_PLATFORM_NOT_INITIALISED:
287 fprintf(stdout, "MRAA: Platform not initialised.\n");
289 case MRAA_ERROR_PLATFORM_ALREADY_INITIALISED:
290 fprintf(stdout, "MRAA: Platform already initialised.\n");
292 case MRAA_ERROR_UNSPECIFIED:
293 fprintf(stdout, "MRAA: Unspecified Error.\n");
296 fprintf(stdout, "MRAA: Unrecognised error.\n");
302 mraa_pin_mode_test(int pin, mraa_pinmodes_t mode)
309 if (pin > plat->phy_pin_count || pin < 0)
314 if (plat->pins[pin].capabilites.valid == 1)
318 if (plat->pins[pin].capabilites.gpio ==1)
322 if (plat->pins[pin].capabilites.pwm ==1)
325 case MRAA_PIN_FAST_GPIO:
326 if (plat->pins[pin].capabilites.fast_gpio ==1)
330 if (plat->pins[pin].capabilites.spi ==1)
334 if (plat->pins[pin].capabilites.i2c ==1)
338 if (pin < plat->aio_count)
339 pin = pin + plat->gpio_count;
340 if (plat->pins[pin].capabilites.aio ==1)
349 mraa_setup_mmap_gpio(int pin)
354 if (plat->pins[pin].capabilites.fast_gpio != 1)
357 if (plat->pins[pin].mmap.gpio.mux_total > 0)
358 if (mraa_setup_mux_mapped(plat->pins[pin].mmap.gpio) != MRAA_SUCCESS)
361 if (mraa_setup_mux_mapped(plat->pins[pin].mmap.gpio) != MRAA_SUCCESS)
363 mraa_mmap_pin_t *ret = &(plat->pins[pin].mmap);
367 mraa_platform_t mraa_get_platform_type()
369 return platform_type;
378 if (plat->aio_count == 0)
381 return plat->adc_raw;
385 mraa_adc_supported_bits()
390 if (plat->aio_count == 0)
393 return plat->adc_supported;
397 mraa_setup_uart(int index)
400 return MRAA_ERROR_PLATFORM_NOT_INITIALISED;
402 if (plat->uart_dev_count == 0)
403 return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
405 if (plat->uart_dev_count <= index)
406 return MRAA_ERROR_NO_RESOURCES;
408 int pos = plat->uart_dev[index].rx;
410 if (plat->pins[pos].uart.mux_total > 0)
411 if (mraa_setup_mux_mapped(plat->pins[pos].uart) != MRAA_SUCCESS)
412 return MRAA_ERROR_INVALID_RESOURCE;
416 pos = plat->uart_dev[index].tx;
417 if (plat->pins[pos].uart.mux_total > 0)
418 if (mraa_setup_mux_mapped(plat->pins[pos].uart) != MRAA_SUCCESS)
419 return MRAA_ERROR_INVALID_RESOURCE;