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"
37 mraa_board_t* plat = NULL;
38 static mraa_platform_t platform_type = MRAA_UNKNOWN_PLATFORM;
39 mraa_adv_func_t* advance_func;
47 mraa_result_t __attribute__((constructor))
50 /** Once more board definitions have been added,
51 * A method for detecting them will need to be devised.
54 return MRAA_ERROR_PLATFORM_ALREADY_INITIALISED;
57 // Initialise python threads, this allows use to grab the GIL when we are
62 // detect a galileo gen2 board
64 // let getline allocate memory for *line
66 FILE *fh = fopen("/sys/devices/virtual/dmi/id/board_name", "r");
68 if (getline(&line, &len, fh) != -1) {
69 if (strncmp(line, "GalileoGen2", 10) == 0) {
70 platform_type = MRAA_INTEL_GALILEO_GEN2;
72 platform_type = MRAA_INTEL_GALILEO_GEN1;
79 advance_func = (mraa_adv_func_t*) malloc(sizeof(mraa_adv_func_t));
80 memset(advance_func, 0, sizeof(mraa_adv_func_t));
82 switch(platform_type) {
83 case MRAA_INTEL_GALILEO_GEN2:
84 plat = mraa_intel_galileo_gen2();
86 case MRAA_INTEL_GALILEO_GEN1:
87 plat = mraa_intel_galileo_rev_d();
90 plat = mraa_intel_galileo_rev_d();
91 fprintf(stderr, "Platform not found, initialising MRAA_INTEL_GALILEO_GEN1\n");
105 mraa_set_priority(const unsigned int priority)
107 struct sched_param sched_s;
109 memset(&sched_s, 0, sizeof(struct sched_param));
110 if (priority > sched_get_priority_max(SCHED_RR)) {
111 sched_s.sched_priority = sched_get_priority_max(SCHED_RR);
114 sched_s.sched_priority = priority;
117 return sched_setscheduler(0, SCHED_RR, &sched_s);
121 mraa_setup_mux_mapped(mraa_pin_t meta)
124 for (mi = 0; mi < meta.mux_total; mi++) {
125 mraa_gpio_context mux_i;
126 mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
128 return MRAA_ERROR_INVALID_HANDLE;
129 mraa_gpio_dir(mux_i, MRAA_GPIO_OUT);
130 if (mraa_gpio_write(mux_i, meta.mux[mi].value) != MRAA_SUCCESS)
131 return MRAA_ERROR_INVALID_RESOURCE;
137 mraa_setup_gpio(int pin)
142 if (pin < 0 || pin > plat->phy_pin_count)
145 if(plat->pins[pin].capabilites.gpio != 1)
148 if (plat->pins[pin].gpio.mux_total > 0)
149 if (mraa_setup_mux_mapped(plat->pins[pin].gpio) != MRAA_SUCCESS)
151 return plat->pins[pin].gpio.pinmap;
155 mraa_setup_aio(int aio)
160 if (aio < 0 || aio > plat->aio_count)
163 int pin = aio + plat->gpio_count;
165 if (plat->pins[pin].capabilites.aio != 1)
168 if (plat->pins[pin].aio.mux_total > 0)
169 if (mraa_setup_mux_mapped(plat->pins[pin].aio) != MRAA_SUCCESS)
171 return plat->pins[pin].aio.pinmap;
175 mraa_setup_i2c(int bus)
180 if (plat->i2c_bus_count >! 0) {
181 fprintf(stderr, "No i2c buses defined in platform");
184 if (bus >= plat->i2c_bus_count) {
185 fprintf(stderr, "Above i2c bus count");
189 int pos = plat->i2c_bus[bus].sda;
190 if (plat->pins[pos].i2c.mux_total > 0)
191 if (mraa_setup_mux_mapped(plat->pins[pos].i2c) != MRAA_SUCCESS)
194 pos = plat->i2c_bus[bus].scl;
195 if (plat->pins[pos].i2c.mux_total > 0)
196 if (mraa_setup_mux_mapped(plat->pins[pos].i2c) != MRAA_SUCCESS)
199 return plat->i2c_bus[bus].bus_id;
203 mraa_setup_spi(int bus)
208 if (plat->spi_bus_count >! 0) {
209 fprintf(stderr, "No spi buses defined in platform");
212 if (plat->spi_bus_count == 1) {
213 bus = plat->def_spi_bus;
215 if (bus >= plat->spi_bus_count) {
216 fprintf(stderr, "Above spi bus count");
220 int pos = plat->spi_bus[bus].sclk;
221 if (plat->pins[pos].spi.mux_total > 0)
222 if (mraa_setup_mux_mapped(plat->pins[pos].spi) != MRAA_SUCCESS)
225 pos = plat->spi_bus[bus].mosi;
226 if (plat->pins[pos].spi.mux_total > 0)
227 if (mraa_setup_mux_mapped(plat->pins[pos].spi) != MRAA_SUCCESS)
230 pos = plat->spi_bus[bus].miso;
231 if (plat->pins[pos].spi.mux_total > 0)
232 if (mraa_setup_mux_mapped(plat->pins[pos].spi) != MRAA_SUCCESS)
235 mraa_spi_bus_t *spi = &(plat->spi_bus[bus]);
240 mraa_setup_pwm(int pin)
245 if (plat->pins[pin].capabilites.pwm != 1)
248 if (plat->pins[pin].capabilites.gpio == 1) {
249 mraa_gpio_context mux_i;
250 mux_i = mraa_gpio_init_raw(plat->pins[pin].gpio.pinmap);
253 if (mraa_gpio_dir(mux_i, MRAA_GPIO_OUT) != MRAA_SUCCESS)
255 // Current REV D quirk. //TODO GEN 2
256 if (mraa_gpio_write(mux_i, 1) != MRAA_SUCCESS)
258 if (mraa_gpio_close(mux_i) != MRAA_SUCCESS)
262 if (plat->pins[pin].pwm.mux_total > 0)
263 if (mraa_setup_mux_mapped(plat->pins[pin].pwm) != MRAA_SUCCESS)
267 ret = (mraa_pin_t*) malloc(sizeof(mraa_pin_t));
268 ret->pinmap = plat->pins[pin].pwm.pinmap;
269 ret->parent_id = plat->pins[pin].pwm.parent_id;
274 mraa_result_print(mraa_result_t result)
277 case MRAA_SUCCESS: fprintf(stderr, "MRAA: SUCCESS\n");
279 case MRAA_ERROR_FEATURE_NOT_IMPLEMENTED:
280 fprintf(stderr, "MRAA: Feature not implemented.\n");
282 case MRAA_ERROR_FEATURE_NOT_SUPPORTED:
283 fprintf(stderr, "MRAA: Feature not supported by Hardware.\n");
285 case MRAA_ERROR_INVALID_VERBOSITY_LEVEL:
286 fprintf(stderr, "MRAA: Invalid verbosity level.\n");
288 case MRAA_ERROR_INVALID_PARAMETER:
289 fprintf(stderr, "MRAA: Invalid parameter.\n");
291 case MRAA_ERROR_INVALID_HANDLE:
292 fprintf(stderr, "MRAA: Invalid Handle.\n");
294 case MRAA_ERROR_NO_RESOURCES:
295 fprintf(stderr, "MRAA: No resources.\n");
297 case MRAA_ERROR_INVALID_RESOURCE:
298 fprintf(stderr, "MRAA: Invalid resource.\n");
300 case MRAA_ERROR_INVALID_QUEUE_TYPE:
301 fprintf(stderr, "MRAA: Invalid Queue Type.\n");
303 case MRAA_ERROR_NO_DATA_AVAILABLE:
304 fprintf(stderr, "MRAA: No Data available.\n");
306 case MRAA_ERROR_INVALID_PLATFORM:
307 fprintf(stderr, "MRAA: Platform not recognised.\n");
309 case MRAA_ERROR_PLATFORM_NOT_INITIALISED:
310 fprintf(stderr, "MRAA: Platform not initialised.\n");
312 case MRAA_ERROR_PLATFORM_ALREADY_INITIALISED:
313 fprintf(stderr, "MRAA: Platform already initialised.\n");
315 case MRAA_ERROR_UNSPECIFIED:
316 fprintf(stderr, "MRAA: Unspecified Error.\n");
318 default: fprintf(stderr, "MRAA: Unrecognised error.\n");
324 mraa_pin_mode_test(int pin, mraa_pinmodes_t mode)
331 if (pin > plat->phy_pin_count || pin < 0)
336 if (plat->pins[pin].capabilites.valid == 1)
340 if (plat->pins[pin].capabilites.gpio ==1)
344 if (plat->pins[pin].capabilites.pwm ==1)
347 case MRAA_PIN_FAST_GPIO:
348 if (plat->pins[pin].capabilites.fast_gpio ==1)
352 if (plat->pins[pin].capabilites.spi ==1)
356 if (plat->pins[pin].capabilites.i2c ==1)
360 if (pin < plat->aio_count)
361 pin = pin + plat->gpio_count;
362 if (plat->pins[pin].capabilites.aio ==1)
371 mraa_setup_mmap_gpio(int pin)
376 if (plat->pins[pin].capabilites.fast_gpio != 1)
379 if (plat->pins[pin].mmap.gpio.mux_total > 0)
380 if (mraa_setup_mux_mapped(plat->pins[pin].mmap.gpio) != MRAA_SUCCESS)
383 if (mraa_setup_mux_mapped(plat->pins[pin].mmap.gpio) != MRAA_SUCCESS)
385 mraa_mmap_pin_t *ret = &(plat->pins[pin].mmap);
389 mraa_platform_t mraa_get_platform_type()
391 return platform_type;
400 if (plat->aio_count == 0)
403 return plat->adc_raw;
407 mraa_adc_supported_bits()
412 if (plat->aio_count == 0)
415 return plat->adc_supported;
419 mraa_setup_uart(int index)
422 return MRAA_ERROR_PLATFORM_NOT_INITIALISED;
424 if (plat->uart_dev_count == 0)
425 return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
427 if (plat->uart_dev_count <= index)
428 return MRAA_ERROR_NO_RESOURCES;
430 int pos = plat->uart_dev[index].rx;
432 if (plat->pins[pos].uart.mux_total > 0)
433 if (mraa_setup_mux_mapped(plat->pins[pos].uart) != MRAA_SUCCESS)
434 return MRAA_ERROR_INVALID_RESOURCE;
438 pos = plat->uart_dev[index].tx;
439 if (plat->pins[pos].uart.mux_total > 0)
440 if (mraa_setup_mux_mapped(plat->pins[pos].uart) != MRAA_SUCCESS)
441 return MRAA_ERROR_INVALID_RESOURCE;