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 static mraa_adv_func* advance = NULL;
52 mraa_result_t __attribute__((constructor))
56 /** Once more board definitions have been added,
57 * A method for detecting them will need to be devised.
60 return MRAA_ERROR_PLATFORM_ALREADY_INITIALISED;
63 // Initialise python threads, this allows use to grab the GIL when we are
68 // detect a galileo gen2 board
70 // let getline allocate memory for *line
72 FILE *fh = fopen("/sys/devices/virtual/dmi/id/board_name", "r");
74 if (getline(&line, &len, fh) != -1) {
75 if (strncmp(line, "GalileoGen2", 10) == 0) {
76 platform_type = MRAA_INTEL_GALILEO_GEN2;
78 platform_type = MRAA_INTEL_GALILEO_GEN1;
85 advance = (mraa_adv_func*) malloc(sizeof(mraa_adv_func));
86 switch(platform_type) {
87 case MRAA_INTEL_GALILEO_GEN2:
88 plat = mraa_intel_galileo_gen2(advance);
90 case MRAA_INTEL_GALILEO_GEN1:
91 plat = mraa_intel_galileo_rev_d(advance);
94 plat = mraa_intel_galileo_rev_d(advance);
95 fprintf(stderr, "Platform not found, initialising MRAA_INTEL_GALILEO_GEN1\n");
102 mraa_set_priority(const unsigned int priority)
104 struct sched_param sched_s;
106 memset(&sched_s, 0, sizeof(struct sched_param));
107 if (priority > sched_get_priority_max(SCHED_RR)) {
108 sched_s.sched_priority = sched_get_priority_max(SCHED_RR);
111 sched_s.sched_priority = priority;
114 return sched_setscheduler(0, SCHED_RR, &sched_s);
118 mraa_setup_mux_mapped(mraa_pin_t meta)
121 for (mi = 0; mi < meta.mux_total; mi++) {
122 mraa_gpio_context mux_i;
123 mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
125 return MRAA_ERROR_INVALID_HANDLE;
126 mraa_gpio_dir(mux_i, MRAA_GPIO_OUT);
127 if (mraa_gpio_write(mux_i, meta.mux[mi].value) != MRAA_SUCCESS)
128 return MRAA_ERROR_INVALID_RESOURCE;
134 mraa_setup_gpio(int pin)
139 if (pin < 0 || pin > plat->phy_pin_count)
142 if(plat->pins[pin].capabilites.gpio != 1)
145 if (plat->pins[pin].gpio.mux_total > 0)
146 if (mraa_setup_mux_mapped(plat->pins[pin].gpio) != MRAA_SUCCESS)
148 return plat->pins[pin].gpio.pinmap;
152 mraa_setup_aio(int aio)
157 if (aio < 0 || aio > plat->aio_count)
160 int pin = aio + plat->gpio_count;
162 if (plat->pins[pin].capabilites.aio != 1)
165 if (plat->pins[pin].aio.mux_total > 0)
166 if (mraa_setup_mux_mapped(plat->pins[pin].aio) != MRAA_SUCCESS)
168 return plat->pins[pin].aio.pinmap;
172 mraa_setup_i2c(int bus)
177 if (plat->i2c_bus_count >! 0) {
178 fprintf(stderr, "No i2c buses defined in platform");
181 if (bus >= plat->i2c_bus_count) {
182 fprintf(stderr, "Above i2c bus count");
186 int pos = plat->i2c_bus[bus].sda;
187 if (plat->pins[pos].i2c.mux_total > 0)
188 if (mraa_setup_mux_mapped(plat->pins[pos].i2c) != MRAA_SUCCESS)
191 pos = plat->i2c_bus[bus].scl;
192 if (plat->pins[pos].i2c.mux_total > 0)
193 if (mraa_setup_mux_mapped(plat->pins[pos].i2c) != MRAA_SUCCESS)
196 return plat->i2c_bus[bus].bus_id;
200 mraa_setup_spi(int bus)
205 if (plat->spi_bus_count >! 0) {
206 fprintf(stderr, "No spi buses defined in platform");
209 if (bus >= plat->spi_bus_count) {
210 fprintf(stderr, "Above spi bus count");
214 int pos = plat->spi_bus[bus].sclk;
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].mosi;
220 if (plat->pins[pos].spi.mux_total > 0)
221 if (mraa_setup_mux_mapped(plat->pins[pos].spi) != MRAA_SUCCESS)
224 pos = plat->spi_bus[bus].miso;
225 if (plat->pins[pos].spi.mux_total > 0)
226 if (mraa_setup_mux_mapped(plat->pins[pos].spi) != MRAA_SUCCESS)
229 mraa_spi_bus_t *spi = &(plat->spi_bus[bus]);
234 mraa_setup_pwm(int pin)
239 if (plat->pins[pin].capabilites.pwm != 1)
242 if (plat->pins[pin].capabilites.gpio == 1) {
243 mraa_gpio_context mux_i;
244 mux_i = mraa_gpio_init_raw(plat->pins[pin].gpio.pinmap);
247 if (mraa_gpio_dir(mux_i, MRAA_GPIO_OUT) != MRAA_SUCCESS)
249 // Current REV D quirk. //TODO GEN 2
250 if (mraa_gpio_write(mux_i, 1) != MRAA_SUCCESS)
252 if (mraa_gpio_close(mux_i) != MRAA_SUCCESS)
256 if (plat->pins[pin].pwm.mux_total > 0)
257 if (mraa_setup_mux_mapped(plat->pins[pin].pwm) != MRAA_SUCCESS)
261 ret = (mraa_pin_t*) malloc(sizeof(mraa_pin_t));
262 ret->pinmap = plat->pins[pin].pwm.pinmap;
263 ret->parent_id = plat->pins[pin].pwm.parent_id;
268 mraa_result_print(mraa_result_t result)
271 case MRAA_SUCCESS: fprintf(stderr, "MRAA: SUCCESS\n");
273 case MRAA_ERROR_FEATURE_NOT_IMPLEMENTED:
274 fprintf(stderr, "MRAA: Feature not implemented.\n");
276 case MRAA_ERROR_FEATURE_NOT_SUPPORTED:
277 fprintf(stderr, "MRAA: Feature not supported by Hardware.\n");
279 case MRAA_ERROR_INVALID_VERBOSITY_LEVEL:
280 fprintf(stderr, "MRAA: Invalid verbosity level.\n");
282 case MRAA_ERROR_INVALID_PARAMETER:
283 fprintf(stderr, "MRAA: Invalid parameter.\n");
285 case MRAA_ERROR_INVALID_HANDLE:
286 fprintf(stderr, "MRAA: Invalid Handle.\n");
288 case MRAA_ERROR_NO_RESOURCES:
289 fprintf(stderr, "MRAA: No resources.\n");
291 case MRAA_ERROR_INVALID_RESOURCE:
292 fprintf(stderr, "MRAA: Invalid resource.\n");
294 case MRAA_ERROR_INVALID_QUEUE_TYPE:
295 fprintf(stderr, "MRAA: Invalid Queue Type.\n");
297 case MRAA_ERROR_NO_DATA_AVAILABLE:
298 fprintf(stderr, "MRAA: No Data available.\n");
300 case MRAA_ERROR_INVALID_PLATFORM:
301 fprintf(stderr, "MRAA: Platform not recognised.\n");
303 case MRAA_ERROR_PLATFORM_NOT_INITIALISED:
304 fprintf(stderr, "MRAA: Platform not initialised.\n");
306 case MRAA_ERROR_PLATFORM_ALREADY_INITIALISED:
307 fprintf(stderr, "MRAA: Platform already initialised.\n");
309 case MRAA_ERROR_UNSPECIFIED:
310 fprintf(stderr, "MRAA: Unspecified Error.\n");
312 default: fprintf(stderr, "MRAA: Unrecognised error.\n");
318 mraa_pin_mode_test(int pin, mraa_pinmodes_t mode)
325 if (pin > plat->phy_pin_count || pin < 0)
330 if (plat->pins[pin].capabilites.valid == 1)
334 if (plat->pins[pin].capabilites.gpio ==1)
338 if (plat->pins[pin].capabilites.pwm ==1)
341 case MRAA_PIN_FAST_GPIO:
342 if (plat->pins[pin].capabilites.fast_gpio ==1)
346 if (plat->pins[pin].capabilites.spi ==1)
350 if (plat->pins[pin].capabilites.i2c ==1)
354 if (pin < plat->aio_count)
355 pin = pin + plat->gpio_count;
356 if (plat->pins[pin].capabilites.aio ==1)
365 mraa_setup_mmap_gpio(int pin)
370 if (plat->pins[pin].capabilites.fast_gpio != 1)
373 if (plat->pins[pin].mmap.gpio.mux_total > 0)
374 if (mraa_setup_mux_mapped(plat->pins[pin].mmap.gpio) != MRAA_SUCCESS)
377 if (mraa_setup_mux_mapped(plat->pins[pin].mmap.gpio) != MRAA_SUCCESS)
379 mraa_mmap_pin_t *ret = &(plat->pins[pin].mmap);
384 mraa_swap_complex_gpio(int pin, int out)
387 return MRAA_ERROR_INVALID_PLATFORM;
389 switch (platform_type) {
390 case MRAA_INTEL_GALILEO_GEN2:
391 if (plat->pins[pin].gpio.complex_cap.complex_pin != 1)
393 if (plat->pins[pin].gpio.complex_cap.output_en == 1) {
394 mraa_gpio_context output_e;
395 output_e = mraa_gpio_init_raw(plat->pins[pin].gpio.output_enable);
396 if (mraa_gpio_dir(output_e, MRAA_GPIO_OUT) != MRAA_SUCCESS)
397 return MRAA_ERROR_INVALID_RESOURCE;
399 if (plat->pins[pin].gpio.complex_cap.output_en_high == 1)
406 if (mraa_gpio_write(output_e, output_val) != MRAA_SUCCESS)
407 return MRAA_ERROR_INVALID_RESOURCE;
409 //if (plat->pins[pin].gpio.complex_cap.pullup_en == 1) {
410 // mraa_gpio_context pullup_e;
411 // pullup_e = mraa_gpio_init_raw(plat->pins[pin].gpio.pullup_enable);
412 // if (mraa_gpio_mode(pullup_e, MRAA_GPIO_HIZ) != MRAA_SUCCESS)
413 // return MRAA_ERROR_INVALID_RESOURCE;
416 default: return MRAA_SUCCESS;
420 mraa_platform_t mraa_get_platform_type()
422 return platform_type;
431 if (plat->aio_count == 0)
434 return plat->adc_raw;
438 mraa_adc_supported_bits()
443 if (plat->aio_count == 0)
446 return plat->adc_supported;