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 //static mraa_pininfo_t* pindata;
38 static mraa_board_t* plat = NULL;
39 static mraa_platform_t platform_type = MRAA_UNKNOWN_PLATFORM;
40 mraa_adv_func advance_func;
48 mraa_result_t __attribute__((constructor))
51 /** Once more board definitions have been added,
52 * A method for detecting them will need to be devised.
55 return MRAA_ERROR_PLATFORM_ALREADY_INITIALISED;
58 // Initialise python threads, this allows use to grab the GIL when we are
63 // detect a galileo gen2 board
65 // let getline allocate memory for *line
67 FILE *fh = fopen("/sys/devices/virtual/dmi/id/board_name", "r");
69 if (getline(&line, &len, fh) != -1) {
70 if (strncmp(line, "GalileoGen2", 10) == 0) {
71 platform_type = MRAA_INTEL_GALILEO_GEN2;
73 platform_type = MRAA_INTEL_GALILEO_GEN1;
80 memset(&advance_func, 0, sizeof(mraa_adv_func));
81 switch(platform_type) {
82 case MRAA_INTEL_GALILEO_GEN2:
83 plat = mraa_intel_galileo_gen2(&advance_func);
85 case MRAA_INTEL_GALILEO_GEN1:
86 plat = mraa_intel_galileo_rev_d(&advance_func);
89 plat = mraa_intel_galileo_rev_d(&advance_func);
90 fprintf(stderr, "Platform not found, initialising MRAA_INTEL_GALILEO_GEN1\n");
104 mraa_set_priority(const unsigned int priority)
106 struct sched_param sched_s;
108 memset(&sched_s, 0, sizeof(struct sched_param));
109 if (priority > sched_get_priority_max(SCHED_RR)) {
110 sched_s.sched_priority = sched_get_priority_max(SCHED_RR);
113 sched_s.sched_priority = priority;
116 return sched_setscheduler(0, SCHED_RR, &sched_s);
120 mraa_setup_mux_mapped(mraa_pin_t meta)
123 for (mi = 0; mi < meta.mux_total; mi++) {
124 mraa_gpio_context mux_i;
125 mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
127 return MRAA_ERROR_INVALID_HANDLE;
128 mraa_gpio_dir(mux_i, MRAA_GPIO_OUT);
129 if (mraa_gpio_write(mux_i, meta.mux[mi].value) != MRAA_SUCCESS)
130 return MRAA_ERROR_INVALID_RESOURCE;
136 mraa_setup_gpio(int pin)
141 if (pin < 0 || pin > plat->phy_pin_count)
144 if(plat->pins[pin].capabilites.gpio != 1)
147 if (plat->pins[pin].gpio.mux_total > 0)
148 if (mraa_setup_mux_mapped(plat->pins[pin].gpio) != MRAA_SUCCESS)
150 return plat->pins[pin].gpio.pinmap;
154 mraa_setup_aio(int aio)
159 if (aio < 0 || aio > plat->aio_count)
162 int pin = aio + plat->gpio_count;
164 if (plat->pins[pin].capabilites.aio != 1)
167 if (plat->pins[pin].aio.mux_total > 0)
168 if (mraa_setup_mux_mapped(plat->pins[pin].aio) != MRAA_SUCCESS)
170 return plat->pins[pin].aio.pinmap;
174 mraa_setup_i2c(int bus)
179 if (plat->i2c_bus_count >! 0) {
180 fprintf(stderr, "No i2c buses defined in platform");
183 if (bus >= plat->i2c_bus_count) {
184 fprintf(stderr, "Above i2c bus count");
188 int pos = plat->i2c_bus[bus].sda;
189 if (plat->pins[pos].i2c.mux_total > 0)
190 if (mraa_setup_mux_mapped(plat->pins[pos].i2c) != MRAA_SUCCESS)
193 pos = plat->i2c_bus[bus].scl;
194 if (plat->pins[pos].i2c.mux_total > 0)
195 if (mraa_setup_mux_mapped(plat->pins[pos].i2c) != MRAA_SUCCESS)
198 return plat->i2c_bus[bus].bus_id;
202 mraa_setup_spi(int bus)
207 if (plat->spi_bus_count >! 0) {
208 fprintf(stderr, "No spi buses defined in platform");
211 if (bus >= plat->spi_bus_count) {
212 fprintf(stderr, "Above spi bus count");
216 int pos = plat->spi_bus[bus].sclk;
217 if (plat->pins[pos].spi.mux_total > 0)
218 if (mraa_setup_mux_mapped(plat->pins[pos].spi) != MRAA_SUCCESS)
221 pos = plat->spi_bus[bus].mosi;
222 if (plat->pins[pos].spi.mux_total > 0)
223 if (mraa_setup_mux_mapped(plat->pins[pos].spi) != MRAA_SUCCESS)
226 pos = plat->spi_bus[bus].miso;
227 if (plat->pins[pos].spi.mux_total > 0)
228 if (mraa_setup_mux_mapped(plat->pins[pos].spi) != MRAA_SUCCESS)
231 mraa_spi_bus_t *spi = &(plat->spi_bus[bus]);
236 mraa_setup_pwm(int pin)
241 if (plat->pins[pin].capabilites.pwm != 1)
244 if (plat->pins[pin].capabilites.gpio == 1) {
245 mraa_gpio_context mux_i;
246 mux_i = mraa_gpio_init_raw(plat->pins[pin].gpio.pinmap);
249 if (mraa_gpio_dir(mux_i, MRAA_GPIO_OUT) != MRAA_SUCCESS)
251 // Current REV D quirk. //TODO GEN 2
252 if (mraa_gpio_write(mux_i, 1) != MRAA_SUCCESS)
254 if (mraa_gpio_close(mux_i) != MRAA_SUCCESS)
258 if (plat->pins[pin].pwm.mux_total > 0)
259 if (mraa_setup_mux_mapped(plat->pins[pin].pwm) != MRAA_SUCCESS)
263 ret = (mraa_pin_t*) malloc(sizeof(mraa_pin_t));
264 ret->pinmap = plat->pins[pin].pwm.pinmap;
265 ret->parent_id = plat->pins[pin].pwm.parent_id;
270 mraa_result_print(mraa_result_t result)
273 case MRAA_SUCCESS: fprintf(stderr, "MRAA: SUCCESS\n");
275 case MRAA_ERROR_FEATURE_NOT_IMPLEMENTED:
276 fprintf(stderr, "MRAA: Feature not implemented.\n");
278 case MRAA_ERROR_FEATURE_NOT_SUPPORTED:
279 fprintf(stderr, "MRAA: Feature not supported by Hardware.\n");
281 case MRAA_ERROR_INVALID_VERBOSITY_LEVEL:
282 fprintf(stderr, "MRAA: Invalid verbosity level.\n");
284 case MRAA_ERROR_INVALID_PARAMETER:
285 fprintf(stderr, "MRAA: Invalid parameter.\n");
287 case MRAA_ERROR_INVALID_HANDLE:
288 fprintf(stderr, "MRAA: Invalid Handle.\n");
290 case MRAA_ERROR_NO_RESOURCES:
291 fprintf(stderr, "MRAA: No resources.\n");
293 case MRAA_ERROR_INVALID_RESOURCE:
294 fprintf(stderr, "MRAA: Invalid resource.\n");
296 case MRAA_ERROR_INVALID_QUEUE_TYPE:
297 fprintf(stderr, "MRAA: Invalid Queue Type.\n");
299 case MRAA_ERROR_NO_DATA_AVAILABLE:
300 fprintf(stderr, "MRAA: No Data available.\n");
302 case MRAA_ERROR_INVALID_PLATFORM:
303 fprintf(stderr, "MRAA: Platform not recognised.\n");
305 case MRAA_ERROR_PLATFORM_NOT_INITIALISED:
306 fprintf(stderr, "MRAA: Platform not initialised.\n");
308 case MRAA_ERROR_PLATFORM_ALREADY_INITIALISED:
309 fprintf(stderr, "MRAA: Platform already initialised.\n");
311 case MRAA_ERROR_UNSPECIFIED:
312 fprintf(stderr, "MRAA: Unspecified Error.\n");
314 default: fprintf(stderr, "MRAA: Unrecognised error.\n");
320 mraa_pin_mode_test(int pin, mraa_pinmodes_t mode)
327 if (pin > plat->phy_pin_count || pin < 0)
332 if (plat->pins[pin].capabilites.valid == 1)
336 if (plat->pins[pin].capabilites.gpio ==1)
340 if (plat->pins[pin].capabilites.pwm ==1)
343 case MRAA_PIN_FAST_GPIO:
344 if (plat->pins[pin].capabilites.fast_gpio ==1)
348 if (plat->pins[pin].capabilites.spi ==1)
352 if (plat->pins[pin].capabilites.i2c ==1)
356 if (pin < plat->aio_count)
357 pin = pin + plat->gpio_count;
358 if (plat->pins[pin].capabilites.aio ==1)
367 mraa_setup_mmap_gpio(int pin)
372 if (plat->pins[pin].capabilites.fast_gpio != 1)
375 if (plat->pins[pin].mmap.gpio.mux_total > 0)
376 if (mraa_setup_mux_mapped(plat->pins[pin].mmap.gpio) != MRAA_SUCCESS)
379 if (mraa_setup_mux_mapped(plat->pins[pin].mmap.gpio) != MRAA_SUCCESS)
381 mraa_mmap_pin_t *ret = &(plat->pins[pin].mmap);
386 mraa_swap_complex_gpio(int pin, int out)
389 return MRAA_ERROR_INVALID_PLATFORM;
391 switch (platform_type) {
392 case MRAA_INTEL_GALILEO_GEN2:
393 if (plat->pins[pin].gpio.complex_cap.complex_pin != 1)
395 if (plat->pins[pin].gpio.complex_cap.output_en == 1) {
396 mraa_gpio_context output_e;
397 output_e = mraa_gpio_init_raw(plat->pins[pin].gpio.output_enable);
398 if (mraa_gpio_dir(output_e, MRAA_GPIO_OUT) != MRAA_SUCCESS)
399 return MRAA_ERROR_INVALID_RESOURCE;
401 if (plat->pins[pin].gpio.complex_cap.output_en_high == 1)
408 if (mraa_gpio_write(output_e, output_val) != MRAA_SUCCESS)
409 return MRAA_ERROR_INVALID_RESOURCE;
411 //if (plat->pins[pin].gpio.complex_cap.pullup_en == 1) {
412 // mraa_gpio_context pullup_e;
413 // pullup_e = mraa_gpio_init_raw(plat->pins[pin].gpio.pullup_enable);
414 // if (mraa_gpio_mode(pullup_e, MRAA_GPIO_HIZ) != MRAA_SUCCESS)
415 // return MRAA_ERROR_INVALID_RESOURCE;
418 default: return MRAA_SUCCESS;
422 mraa_platform_t mraa_get_platform_type()
424 return platform_type;
433 if (plat->aio_count == 0)
436 return plat->adc_raw;
440 mraa_adc_supported_bits()
445 if (plat->aio_count == 0)
448 return plat->adc_supported;
452 mraa_setup_uart(int index)
455 return MRAA_ERROR_PLATFORM_NOT_INITIALISED;
457 if (plat->uart_dev_count == 0)
458 return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
460 if (plat->uart_dev_count <= index)
461 return MRAA_ERROR_NO_RESOURCES;
463 int pos = plat->uart_dev[index].rx;
465 if (plat->pins[pos].uart.mux_total > 0)
466 if (mraa_setup_mux_mapped(plat->pins[pos].uart) != MRAA_SUCCESS)
467 return MRAA_ERROR_INVALID_RESOURCE;
471 pos = plat->uart_dev[index].tx;
472 if (plat->pins[pos].uart.mux_total > 0)
473 if (mraa_setup_mux_mapped(plat->pins[pos].uart) != MRAA_SUCCESS)
474 return MRAA_ERROR_INVALID_RESOURCE;