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;
51 return (const mraa_adv_func*) advance;
58 /** Once more board definitions have been added,
59 * A method for detecting them will need to be devised.
62 return MRAA_ERROR_PLATFORM_ALREADY_INITIALISED;
65 // Initialise python threads, this allows use to grab the GIL when we are
70 // detect a galileo gen2 board
72 // let getline allocate memory for *line
74 FILE *fh = fopen("/sys/devices/virtual/dmi/id/board_name", "r");
76 if (getline(&line, &len, fh) != -1) {
77 if (strncmp(line, "GalileoGen2", 10) == 0) {
78 platform_type = MRAA_INTEL_GALILEO_GEN2;
80 platform_type = MRAA_INTEL_GALILEO_GEN1;
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();
95 plat = mraa_intel_galileo_rev_d();
96 fprintf(stderr, "Platform not found, initialising MRAA_INTEL_GALILEO_GEN1\n");
110 mraa_set_priority(const unsigned int priority)
112 struct sched_param sched_s;
114 memset(&sched_s, 0, sizeof(struct sched_param));
115 if (priority > sched_get_priority_max(SCHED_RR)) {
116 sched_s.sched_priority = sched_get_priority_max(SCHED_RR);
119 sched_s.sched_priority = priority;
122 return sched_setscheduler(0, SCHED_RR, &sched_s);
126 mraa_setup_mux_mapped(mraa_pin_t meta)
129 for (mi = 0; mi < meta.mux_total; mi++) {
130 mraa_gpio_context mux_i;
131 mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
133 return MRAA_ERROR_INVALID_HANDLE;
134 mraa_gpio_dir(mux_i, MRAA_GPIO_OUT);
135 if (mraa_gpio_write(mux_i, meta.mux[mi].value) != MRAA_SUCCESS)
136 return MRAA_ERROR_INVALID_RESOURCE;
142 mraa_setup_gpio(int pin)
147 if (pin < 0 || pin > plat->phy_pin_count)
150 if(plat->pins[pin].capabilites.gpio != 1)
153 if (plat->pins[pin].gpio.mux_total > 0)
154 if (mraa_setup_mux_mapped(plat->pins[pin].gpio) != MRAA_SUCCESS)
156 return plat->pins[pin].gpio.pinmap;
160 mraa_setup_aio(int aio)
165 if (aio < 0 || aio > plat->aio_count)
168 int pin = aio + plat->gpio_count;
170 if (plat->pins[pin].capabilites.aio != 1)
173 if (plat->pins[pin].aio.mux_total > 0)
174 if (mraa_setup_mux_mapped(plat->pins[pin].aio) != MRAA_SUCCESS)
176 return plat->pins[pin].aio.pinmap;
180 mraa_setup_i2c(int bus)
185 if (plat->i2c_bus_count >! 0) {
186 fprintf(stderr, "No i2c buses defined in platform");
189 if (bus >= plat->i2c_bus_count) {
190 fprintf(stderr, "Above i2c bus count");
194 int pos = plat->i2c_bus[bus].sda;
195 if (plat->pins[pos].i2c.mux_total > 0)
196 if (mraa_setup_mux_mapped(plat->pins[pos].i2c) != MRAA_SUCCESS)
199 pos = plat->i2c_bus[bus].scl;
200 if (plat->pins[pos].i2c.mux_total > 0)
201 if (mraa_setup_mux_mapped(plat->pins[pos].i2c) != MRAA_SUCCESS)
204 return plat->i2c_bus[bus].bus_id;
208 mraa_setup_spi(int bus)
213 if (plat->spi_bus_count >! 0) {
214 fprintf(stderr, "No spi buses defined in platform");
217 if (bus >= plat->spi_bus_count) {
218 fprintf(stderr, "Above spi bus count");
222 int pos = plat->spi_bus[bus].sclk;
223 if (plat->pins[pos].spi.mux_total > 0)
224 if (mraa_setup_mux_mapped(plat->pins[pos].spi) != MRAA_SUCCESS)
227 pos = plat->spi_bus[bus].mosi;
228 if (plat->pins[pos].spi.mux_total > 0)
229 if (mraa_setup_mux_mapped(plat->pins[pos].spi) != MRAA_SUCCESS)
232 pos = plat->spi_bus[bus].miso;
233 if (plat->pins[pos].spi.mux_total > 0)
234 if (mraa_setup_mux_mapped(plat->pins[pos].spi) != MRAA_SUCCESS)
237 mraa_spi_bus_t *spi = &(plat->spi_bus[bus]);
242 mraa_setup_pwm(int pin)
247 if (plat->pins[pin].capabilites.pwm != 1)
250 if (plat->pins[pin].capabilites.gpio == 1) {
251 mraa_gpio_context mux_i;
252 mux_i = mraa_gpio_init_raw(plat->pins[pin].gpio.pinmap);
255 if (mraa_gpio_dir(mux_i, MRAA_GPIO_OUT) != MRAA_SUCCESS)
257 // Current REV D quirk. //TODO GEN 2
258 if (mraa_gpio_write(mux_i, 1) != MRAA_SUCCESS)
260 if (mraa_gpio_close(mux_i) != MRAA_SUCCESS)
264 if (plat->pins[pin].pwm.mux_total > 0)
265 if (mraa_setup_mux_mapped(plat->pins[pin].pwm) != MRAA_SUCCESS)
269 ret = (mraa_pin_t*) malloc(sizeof(mraa_pin_t));
270 ret->pinmap = plat->pins[pin].pwm.pinmap;
271 ret->parent_id = plat->pins[pin].pwm.parent_id;
276 mraa_result_print(mraa_result_t result)
279 case MRAA_SUCCESS: fprintf(stderr, "MRAA: SUCCESS\n");
281 case MRAA_ERROR_FEATURE_NOT_IMPLEMENTED:
282 fprintf(stderr, "MRAA: Feature not implemented.\n");
284 case MRAA_ERROR_FEATURE_NOT_SUPPORTED:
285 fprintf(stderr, "MRAA: Feature not supported by Hardware.\n");
287 case MRAA_ERROR_INVALID_VERBOSITY_LEVEL:
288 fprintf(stderr, "MRAA: Invalid verbosity level.\n");
290 case MRAA_ERROR_INVALID_PARAMETER:
291 fprintf(stderr, "MRAA: Invalid parameter.\n");
293 case MRAA_ERROR_INVALID_HANDLE:
294 fprintf(stderr, "MRAA: Invalid Handle.\n");
296 case MRAA_ERROR_NO_RESOURCES:
297 fprintf(stderr, "MRAA: No resources.\n");
299 case MRAA_ERROR_INVALID_RESOURCE:
300 fprintf(stderr, "MRAA: Invalid resource.\n");
302 case MRAA_ERROR_INVALID_QUEUE_TYPE:
303 fprintf(stderr, "MRAA: Invalid Queue Type.\n");
305 case MRAA_ERROR_NO_DATA_AVAILABLE:
306 fprintf(stderr, "MRAA: No Data available.\n");
308 case MRAA_ERROR_INVALID_PLATFORM:
309 fprintf(stderr, "MRAA: Platform not recognised.\n");
311 case MRAA_ERROR_PLATFORM_NOT_INITIALISED:
312 fprintf(stderr, "MRAA: Platform not initialised.\n");
314 case MRAA_ERROR_PLATFORM_ALREADY_INITIALISED:
315 fprintf(stderr, "MRAA: Platform already initialised.\n");
317 case MRAA_ERROR_UNSPECIFIED:
318 fprintf(stderr, "MRAA: Unspecified Error.\n");
320 default: fprintf(stderr, "MRAA: Unrecognised error.\n");
326 mraa_pin_mode_test(int pin, mraa_pinmodes_t mode)
333 if (pin > plat->phy_pin_count || pin < 0)
338 if (plat->pins[pin].capabilites.valid == 1)
342 if (plat->pins[pin].capabilites.gpio ==1)
346 if (plat->pins[pin].capabilites.pwm ==1)
349 case MRAA_PIN_FAST_GPIO:
350 if (plat->pins[pin].capabilites.fast_gpio ==1)
354 if (plat->pins[pin].capabilites.spi ==1)
358 if (plat->pins[pin].capabilites.i2c ==1)
362 if (pin < plat->aio_count)
363 pin = pin + plat->gpio_count;
364 if (plat->pins[pin].capabilites.aio ==1)
373 mraa_setup_mmap_gpio(int pin)
378 if (plat->pins[pin].capabilites.fast_gpio != 1)
381 if (plat->pins[pin].mmap.gpio.mux_total > 0)
382 if (mraa_setup_mux_mapped(plat->pins[pin].mmap.gpio) != MRAA_SUCCESS)
385 if (mraa_setup_mux_mapped(plat->pins[pin].mmap.gpio) != MRAA_SUCCESS)
387 mraa_mmap_pin_t *ret = &(plat->pins[pin].mmap);
392 mraa_swap_complex_gpio(int pin, int out)
395 return MRAA_ERROR_INVALID_PLATFORM;
397 switch (platform_type) {
398 case MRAA_INTEL_GALILEO_GEN2:
399 if (plat->pins[pin].gpio.complex_cap.complex_pin != 1)
401 if (plat->pins[pin].gpio.complex_cap.output_en == 1) {
402 mraa_gpio_context output_e;
403 output_e = mraa_gpio_init_raw(plat->pins[pin].gpio.output_enable);
404 if (mraa_gpio_dir(output_e, MRAA_GPIO_OUT) != MRAA_SUCCESS)
405 return MRAA_ERROR_INVALID_RESOURCE;
407 if (plat->pins[pin].gpio.complex_cap.output_en_high == 1)
414 if (mraa_gpio_write(output_e, output_val) != MRAA_SUCCESS)
415 return MRAA_ERROR_INVALID_RESOURCE;
417 //if (plat->pins[pin].gpio.complex_cap.pullup_en == 1) {
418 // mraa_gpio_context pullup_e;
419 // pullup_e = mraa_gpio_init_raw(plat->pins[pin].gpio.pullup_enable);
420 // if (mraa_gpio_mode(pullup_e, MRAA_GPIO_HIZ) != MRAA_SUCCESS)
421 // return MRAA_ERROR_INVALID_RESOURCE;
424 default: return MRAA_SUCCESS;
428 mraa_platform_t mraa_get_platform_type()
430 return platform_type;
439 if (plat->aio_count == 0)
442 return plat->adc_raw;
446 mraa_adc_supported_bits()
451 if (plat->aio_count == 0)
454 return plat->adc_supported;