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"
38 mraa_board_t* plat = NULL;
39 static mraa_platform_t platform_type = MRAA_UNKNOWN_PLATFORM;
40 mraa_adv_func_t* 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;
72 } else if (strncmp(line, "BODEGA BAY", 10) == 0) {
73 platform_type = MRAA_INTEL_EDISON_FAB_C;
74 } else if (strncmp(line, "SALT BAY", 7) == 0) {
75 platform_type = MRAA_INTEL_EDISON_FAB_C;
77 platform_type = MRAA_INTEL_GALILEO_GEN1;
84 advance_func = (mraa_adv_func_t*) malloc(sizeof(mraa_adv_func_t));
85 memset(advance_func, 0, sizeof(mraa_adv_func_t));
87 switch(platform_type) {
88 case MRAA_INTEL_GALILEO_GEN2:
89 plat = mraa_intel_galileo_gen2();
91 case MRAA_INTEL_GALILEO_GEN1:
92 plat = mraa_intel_galileo_rev_d();
94 case MRAA_INTEL_EDISON_FAB_C:
95 plat = mraa_intel_edison_fab_c();
98 plat = mraa_intel_galileo_rev_d();
99 fprintf(stderr, "Platform not found, initialising MRAA_INTEL_GALILEO_GEN1\n");
113 mraa_set_priority(const unsigned int priority)
115 struct sched_param sched_s;
117 memset(&sched_s, 0, sizeof(struct sched_param));
118 if (priority > sched_get_priority_max(SCHED_RR)) {
119 sched_s.sched_priority = sched_get_priority_max(SCHED_RR);
122 sched_s.sched_priority = priority;
125 return sched_setscheduler(0, SCHED_RR, &sched_s);
129 mraa_setup_mux_mapped(mraa_pin_t meta)
132 for (mi = 0; mi < meta.mux_total; mi++) {
133 mraa_gpio_context mux_i;
134 mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
136 return MRAA_ERROR_INVALID_HANDLE;
137 mraa_gpio_dir(mux_i, MRAA_GPIO_OUT);
138 if (mraa_gpio_write(mux_i, meta.mux[mi].value) != MRAA_SUCCESS)
139 return MRAA_ERROR_INVALID_RESOURCE;
145 mraa_setup_gpio(int pin)
150 if (pin < 0 || pin > plat->phy_pin_count)
153 if(plat->pins[pin].capabilites.gpio != 1)
156 if (plat->pins[pin].gpio.mux_total > 0)
157 if (mraa_setup_mux_mapped(plat->pins[pin].gpio) != MRAA_SUCCESS)
159 return plat->pins[pin].gpio.pinmap;
163 mraa_setup_aio(int aio)
168 if (aio < 0 || aio > plat->aio_count)
171 int pin = aio + plat->gpio_count;
173 if (plat->pins[pin].capabilites.aio != 1)
176 if (plat->pins[pin].aio.mux_total > 0)
177 if (mraa_setup_mux_mapped(plat->pins[pin].aio) != MRAA_SUCCESS)
179 return plat->pins[pin].aio.pinmap;
183 mraa_setup_i2c(int bus)
188 if (plat->i2c_bus_count >! 0) {
189 fprintf(stderr, "No i2c buses defined in platform");
192 if (bus >= plat->i2c_bus_count) {
193 fprintf(stderr, "Above i2c bus count");
197 int pos = plat->i2c_bus[bus].sda;
198 if (plat->pins[pos].i2c.mux_total > 0)
199 if (mraa_setup_mux_mapped(plat->pins[pos].i2c) != MRAA_SUCCESS)
202 pos = plat->i2c_bus[bus].scl;
203 if (plat->pins[pos].i2c.mux_total > 0)
204 if (mraa_setup_mux_mapped(plat->pins[pos].i2c) != MRAA_SUCCESS)
207 return plat->i2c_bus[bus].bus_id;
211 mraa_setup_spi(int bus)
216 if (plat->spi_bus_count >! 0) {
217 fprintf(stderr, "No spi buses defined in platform");
220 if (plat->spi_bus_count == 1) {
221 bus = plat->def_spi_bus;
223 if (bus >= plat->spi_bus_count) {
224 fprintf(stderr, "Above spi bus count");
228 int pos = plat->spi_bus[bus].sclk;
229 if (plat->pins[pos].spi.mux_total > 0)
230 if (mraa_setup_mux_mapped(plat->pins[pos].spi) != MRAA_SUCCESS)
233 pos = plat->spi_bus[bus].mosi;
234 if (plat->pins[pos].spi.mux_total > 0)
235 if (mraa_setup_mux_mapped(plat->pins[pos].spi) != MRAA_SUCCESS)
238 pos = plat->spi_bus[bus].miso;
239 if (plat->pins[pos].spi.mux_total > 0)
240 if (mraa_setup_mux_mapped(plat->pins[pos].spi) != MRAA_SUCCESS)
243 mraa_spi_bus_t *spi = &(plat->spi_bus[bus]);
248 mraa_setup_pwm(int pin)
253 if (plat->pins[pin].capabilites.pwm != 1)
256 if (plat->pins[pin].capabilites.gpio == 1) {
257 mraa_gpio_context mux_i;
258 mux_i = mraa_gpio_init_raw(plat->pins[pin].gpio.pinmap);
261 if (mraa_gpio_dir(mux_i, MRAA_GPIO_OUT) != MRAA_SUCCESS)
263 // Current REV D quirk. //TODO GEN 2
264 if (mraa_gpio_write(mux_i, 1) != MRAA_SUCCESS)
266 if (mraa_gpio_close(mux_i) != MRAA_SUCCESS)
270 if (plat->pins[pin].pwm.mux_total > 0)
271 if (mraa_setup_mux_mapped(plat->pins[pin].pwm) != MRAA_SUCCESS)
275 ret = (mraa_pin_t*) malloc(sizeof(mraa_pin_t));
276 ret->pinmap = plat->pins[pin].pwm.pinmap;
277 ret->parent_id = plat->pins[pin].pwm.parent_id;
282 mraa_result_print(mraa_result_t result)
285 case MRAA_SUCCESS: fprintf(stderr, "MRAA: SUCCESS\n");
287 case MRAA_ERROR_FEATURE_NOT_IMPLEMENTED:
288 fprintf(stderr, "MRAA: Feature not implemented.\n");
290 case MRAA_ERROR_FEATURE_NOT_SUPPORTED:
291 fprintf(stderr, "MRAA: Feature not supported by Hardware.\n");
293 case MRAA_ERROR_INVALID_VERBOSITY_LEVEL:
294 fprintf(stderr, "MRAA: Invalid verbosity level.\n");
296 case MRAA_ERROR_INVALID_PARAMETER:
297 fprintf(stderr, "MRAA: Invalid parameter.\n");
299 case MRAA_ERROR_INVALID_HANDLE:
300 fprintf(stderr, "MRAA: Invalid Handle.\n");
302 case MRAA_ERROR_NO_RESOURCES:
303 fprintf(stderr, "MRAA: No resources.\n");
305 case MRAA_ERROR_INVALID_RESOURCE:
306 fprintf(stderr, "MRAA: Invalid resource.\n");
308 case MRAA_ERROR_INVALID_QUEUE_TYPE:
309 fprintf(stderr, "MRAA: Invalid Queue Type.\n");
311 case MRAA_ERROR_NO_DATA_AVAILABLE:
312 fprintf(stderr, "MRAA: No Data available.\n");
314 case MRAA_ERROR_INVALID_PLATFORM:
315 fprintf(stderr, "MRAA: Platform not recognised.\n");
317 case MRAA_ERROR_PLATFORM_NOT_INITIALISED:
318 fprintf(stderr, "MRAA: Platform not initialised.\n");
320 case MRAA_ERROR_PLATFORM_ALREADY_INITIALISED:
321 fprintf(stderr, "MRAA: Platform already initialised.\n");
323 case MRAA_ERROR_UNSPECIFIED:
324 fprintf(stderr, "MRAA: Unspecified Error.\n");
326 default: fprintf(stderr, "MRAA: Unrecognised error.\n");
332 mraa_pin_mode_test(int pin, mraa_pinmodes_t mode)
339 if (pin > plat->phy_pin_count || pin < 0)
344 if (plat->pins[pin].capabilites.valid == 1)
348 if (plat->pins[pin].capabilites.gpio ==1)
352 if (plat->pins[pin].capabilites.pwm ==1)
355 case MRAA_PIN_FAST_GPIO:
356 if (plat->pins[pin].capabilites.fast_gpio ==1)
360 if (plat->pins[pin].capabilites.spi ==1)
364 if (plat->pins[pin].capabilites.i2c ==1)
368 if (pin < plat->aio_count)
369 pin = pin + plat->gpio_count;
370 if (plat->pins[pin].capabilites.aio ==1)
379 mraa_setup_mmap_gpio(int pin)
384 if (plat->pins[pin].capabilites.fast_gpio != 1)
387 if (plat->pins[pin].mmap.gpio.mux_total > 0)
388 if (mraa_setup_mux_mapped(plat->pins[pin].mmap.gpio) != MRAA_SUCCESS)
391 if (mraa_setup_mux_mapped(plat->pins[pin].mmap.gpio) != MRAA_SUCCESS)
393 mraa_mmap_pin_t *ret = &(plat->pins[pin].mmap);
397 mraa_platform_t mraa_get_platform_type()
399 return platform_type;
408 if (plat->aio_count == 0)
411 return plat->adc_raw;
415 mraa_adc_supported_bits()
420 if (plat->aio_count == 0)
423 return plat->adc_supported;
427 mraa_setup_uart(int index)
430 return MRAA_ERROR_PLATFORM_NOT_INITIALISED;
432 if (plat->uart_dev_count == 0)
433 return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
435 if (plat->uart_dev_count <= index)
436 return MRAA_ERROR_NO_RESOURCES;
438 int pos = plat->uart_dev[index].rx;
440 if (plat->pins[pos].uart.mux_total > 0)
441 if (mraa_setup_mux_mapped(plat->pins[pos].uart) != MRAA_SUCCESS)
442 return MRAA_ERROR_INVALID_RESOURCE;
446 pos = plat->uart_dev[index].tx;
447 if (plat->pins[pos].uart.mux_total > 0)
448 if (mraa_setup_mux_mapped(plat->pins[pos].uart) != MRAA_SUCCESS)
449 return MRAA_ERROR_INVALID_RESOURCE;