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");
97 mraa_set_priority(const unsigned int priority)
99 struct sched_param sched_s;
101 memset(&sched_s, 0, sizeof(struct sched_param));
102 if (priority > sched_get_priority_max(SCHED_RR)) {
103 sched_s.sched_priority = sched_get_priority_max(SCHED_RR);
106 sched_s.sched_priority = priority;
109 return sched_setscheduler(0, SCHED_RR, &sched_s);
113 mraa_setup_mux_mapped(mraa_pin_t meta)
116 for (mi = 0; mi < meta.mux_total; mi++) {
117 mraa_gpio_context mux_i;
118 mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
120 return MRAA_ERROR_INVALID_HANDLE;
121 mraa_gpio_dir(mux_i, MRAA_GPIO_OUT);
122 if (mraa_gpio_write(mux_i, meta.mux[mi].value) != MRAA_SUCCESS)
123 return MRAA_ERROR_INVALID_RESOURCE;
129 mraa_setup_gpio(int pin)
134 if (pin < 0 || pin > plat->phy_pin_count)
137 if(plat->pins[pin].capabilites.gpio != 1)
140 if (plat->pins[pin].gpio.mux_total > 0)
141 if (mraa_setup_mux_mapped(plat->pins[pin].gpio) != MRAA_SUCCESS)
143 return plat->pins[pin].gpio.pinmap;
147 mraa_setup_aio(int aio)
152 if (aio < 0 || aio > plat->aio_count)
155 int pin = aio + plat->gpio_count;
157 if (plat->pins[pin].capabilites.aio != 1)
160 if (plat->pins[pin].aio.mux_total > 0)
161 if (mraa_setup_mux_mapped(plat->pins[pin].aio) != MRAA_SUCCESS)
163 return plat->pins[pin].aio.pinmap;
167 mraa_setup_i2c(int bus)
172 if (plat->i2c_bus_count >! 0) {
173 fprintf(stderr, "No i2c buses defined in platform");
176 if (bus >= plat->i2c_bus_count) {
177 fprintf(stderr, "Above i2c bus count");
181 int pos = plat->i2c_bus[bus].sda;
182 if (plat->pins[pos].i2c.mux_total > 0)
183 if (mraa_setup_mux_mapped(plat->pins[pos].i2c) != MRAA_SUCCESS)
186 pos = plat->i2c_bus[bus].scl;
187 if (plat->pins[pos].i2c.mux_total > 0)
188 if (mraa_setup_mux_mapped(plat->pins[pos].i2c) != MRAA_SUCCESS)
191 return plat->i2c_bus[bus].bus_id;
195 mraa_setup_spi(int bus)
200 if (plat->spi_bus_count >! 0) {
201 fprintf(stderr, "No spi buses defined in platform");
204 if (bus >= plat->spi_bus_count) {
205 fprintf(stderr, "Above spi bus count");
209 int pos = plat->spi_bus[bus].sclk;
210 if (plat->pins[pos].spi.mux_total > 0)
211 if (mraa_setup_mux_mapped(plat->pins[pos].spi) != MRAA_SUCCESS)
214 pos = plat->spi_bus[bus].mosi;
215 if (plat->pins[pos].spi.mux_total > 0)
216 if (mraa_setup_mux_mapped(plat->pins[pos].spi) != MRAA_SUCCESS)
219 pos = plat->spi_bus[bus].miso;
220 if (plat->pins[pos].spi.mux_total > 0)
221 if (mraa_setup_mux_mapped(plat->pins[pos].spi) != MRAA_SUCCESS)
224 mraa_spi_bus_t *spi = &(plat->spi_bus[bus]);
229 mraa_setup_pwm(int pin)
234 if (plat->pins[pin].capabilites.pwm != 1)
237 if (plat->pins[pin].capabilites.gpio == 1) {
238 mraa_gpio_context mux_i;
239 mux_i = mraa_gpio_init_raw(plat->pins[pin].gpio.pinmap);
242 if (mraa_gpio_dir(mux_i, MRAA_GPIO_OUT) != MRAA_SUCCESS)
244 // Current REV D quirk. //TODO GEN 2
245 if (mraa_gpio_write(mux_i, 1) != MRAA_SUCCESS)
247 if (mraa_gpio_close(mux_i) != MRAA_SUCCESS)
251 if (plat->pins[pin].pwm.mux_total > 0)
252 if (mraa_setup_mux_mapped(plat->pins[pin].pwm) != MRAA_SUCCESS)
256 ret = (mraa_pin_t*) malloc(sizeof(mraa_pin_t));
257 ret->pinmap = plat->pins[pin].pwm.pinmap;
258 ret->parent_id = plat->pins[pin].pwm.parent_id;
263 mraa_result_print(mraa_result_t result)
266 case MRAA_SUCCESS: fprintf(stderr, "MRAA: SUCCESS\n");
268 case MRAA_ERROR_FEATURE_NOT_IMPLEMENTED:
269 fprintf(stderr, "MRAA: Feature not implemented.\n");
271 case MRAA_ERROR_FEATURE_NOT_SUPPORTED:
272 fprintf(stderr, "MRAA: Feature not supported by Hardware.\n");
274 case MRAA_ERROR_INVALID_VERBOSITY_LEVEL:
275 fprintf(stderr, "MRAA: Invalid verbosity level.\n");
277 case MRAA_ERROR_INVALID_PARAMETER:
278 fprintf(stderr, "MRAA: Invalid parameter.\n");
280 case MRAA_ERROR_INVALID_HANDLE:
281 fprintf(stderr, "MRAA: Invalid Handle.\n");
283 case MRAA_ERROR_NO_RESOURCES:
284 fprintf(stderr, "MRAA: No resources.\n");
286 case MRAA_ERROR_INVALID_RESOURCE:
287 fprintf(stderr, "MRAA: Invalid resource.\n");
289 case MRAA_ERROR_INVALID_QUEUE_TYPE:
290 fprintf(stderr, "MRAA: Invalid Queue Type.\n");
292 case MRAA_ERROR_NO_DATA_AVAILABLE:
293 fprintf(stderr, "MRAA: No Data available.\n");
295 case MRAA_ERROR_INVALID_PLATFORM:
296 fprintf(stderr, "MRAA: Platform not recognised.\n");
298 case MRAA_ERROR_PLATFORM_NOT_INITIALISED:
299 fprintf(stderr, "MRAA: Platform not initialised.\n");
301 case MRAA_ERROR_PLATFORM_ALREADY_INITIALISED:
302 fprintf(stderr, "MRAA: Platform already initialised.\n");
304 case MRAA_ERROR_UNSPECIFIED:
305 fprintf(stderr, "MRAA: Unspecified Error.\n");
307 default: fprintf(stderr, "MRAA: Unrecognised error.\n");
313 mraa_pin_mode_test(int pin, mraa_pinmodes_t mode)
320 if (pin > plat->phy_pin_count || pin < 0)
325 if (plat->pins[pin].capabilites.valid == 1)
329 if (plat->pins[pin].capabilites.gpio ==1)
333 if (plat->pins[pin].capabilites.pwm ==1)
336 case MRAA_PIN_FAST_GPIO:
337 if (plat->pins[pin].capabilites.fast_gpio ==1)
341 if (plat->pins[pin].capabilites.spi ==1)
345 if (plat->pins[pin].capabilites.i2c ==1)
349 if (pin < plat->aio_count)
350 pin = pin + plat->gpio_count;
351 if (plat->pins[pin].capabilites.aio ==1)
360 mraa_setup_mmap_gpio(int pin)
365 if (plat->pins[pin].capabilites.fast_gpio != 1)
368 if (plat->pins[pin].mmap.gpio.mux_total > 0)
369 if (mraa_setup_mux_mapped(plat->pins[pin].mmap.gpio) != MRAA_SUCCESS)
372 if (mraa_setup_mux_mapped(plat->pins[pin].mmap.gpio) != MRAA_SUCCESS)
374 mraa_mmap_pin_t *ret = &(plat->pins[pin].mmap);
379 mraa_swap_complex_gpio(int pin, int out)
382 return MRAA_ERROR_INVALID_PLATFORM;
384 switch (platform_type) {
385 case MRAA_INTEL_GALILEO_GEN2:
386 if (plat->pins[pin].gpio.complex_cap.complex_pin != 1)
388 if (plat->pins[pin].gpio.complex_cap.output_en == 1) {
389 mraa_gpio_context output_e;
390 output_e = mraa_gpio_init_raw(plat->pins[pin].gpio.output_enable);
391 if (mraa_gpio_dir(output_e, MRAA_GPIO_OUT) != MRAA_SUCCESS)
392 return MRAA_ERROR_INVALID_RESOURCE;
394 if (plat->pins[pin].gpio.complex_cap.output_en_high == 1)
401 if (mraa_gpio_write(output_e, output_val) != MRAA_SUCCESS)
402 return MRAA_ERROR_INVALID_RESOURCE;
404 //if (plat->pins[pin].gpio.complex_cap.pullup_en == 1) {
405 // mraa_gpio_context pullup_e;
406 // pullup_e = mraa_gpio_init_raw(plat->pins[pin].gpio.pullup_enable);
407 // if (mraa_gpio_mode(pullup_e, MRAA_GPIO_HIZ) != MRAA_SUCCESS)
408 // return MRAA_ERROR_INVALID_RESOURCE;
411 default: return MRAA_SUCCESS;
415 mraa_platform_t mraa_get_platform_type()
417 return platform_type;
426 if (plat->aio_count == 0)
429 return plat->adc_raw;
433 mraa_adc_supported_bits()
438 if (plat->aio_count == 0)
441 return plat->adc_supported;