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 "maa_internal.h"
32 #include "intel_galileo_rev_d.h"
36 //static maa_pininfo_t* pindata;
37 static maa_board_t* plat = NULL;
49 maa_result_t __attribute__((constructor))
53 /** Once more board definitions have been added,
54 * A method for detecting them will need to be devised.
57 return MAA_ERROR_PLATFORM_ALREADY_INITIALISED;
60 // Initialise python threads, this allows use to grab the GIL when we are
65 plat = maa_intel_galileo_rev_d();
70 maa_set_priority(const unsigned int priority)
72 struct sched_param sched_s;
74 memset(&sched_s, 0, sizeof(struct sched_param));
75 if (priority > sched_get_priority_max(SCHED_RR)) {
76 sched_s.sched_priority = sched_get_priority_max(SCHED_RR);
79 sched_s.sched_priority = priority;
82 return sched_setscheduler(0, SCHED_RR, &sched_s);
86 maa_setup_mux_mapped(maa_pin_t meta)
89 for (mi = 0; mi < meta.mux_total; mi++) {
90 maa_gpio_context mux_i;
91 mux_i = maa_gpio_init_raw(meta.mux[mi].pin);
93 return MAA_ERROR_INVALID_HANDLE;
94 if (maa_gpio_dir(mux_i, MAA_GPIO_OUT) != MAA_SUCCESS)
95 return MAA_ERROR_INVALID_RESOURCE;
96 if (maa_gpio_write(mux_i, meta.mux[mi].value) != MAA_SUCCESS)
97 return MAA_ERROR_INVALID_RESOURCE;
103 maa_setup_gpio(int pin)
108 if (pin < 0 || pin > plat->phy_pin_count)
111 if(plat->pins[pin].capabilites.gpio != 1)
114 if (plat->pins[pin].gpio.mux_total > 0)
115 if (maa_setup_mux_mapped(plat->pins[pin].gpio) != MAA_SUCCESS)
117 return plat->pins[pin].gpio.pinmap;
121 maa_setup_aio(int aio)
126 if (aio < 0 || aio > plat->aio_count)
129 int pin = aio + plat->gpio_count;
131 if (plat->pins[pin].capabilites.aio != 1)
134 if (plat->pins[pin].aio.mux_total > 0)
135 if (maa_setup_mux_mapped(plat->pins[pin].aio) != MAA_SUCCESS)
137 return plat->pins[pin].aio.pinmap;
141 maa_setup_i2c(int bus)
146 if (plat->i2c_bus_count >! 0) {
147 fprintf(stderr, "No i2c buses defined in platform");
150 if (bus >= plat->i2c_bus_count) {
151 fprintf(stderr, "Above i2c bus count");
155 int pos = plat->i2c_bus[bus].sda;
156 if (plat->pins[pos].i2c.mux_total > 0)
157 if (maa_setup_mux_mapped(plat->pins[pos].i2c) != MAA_SUCCESS)
160 pos = plat->i2c_bus[bus].scl;
161 if (plat->pins[pos].i2c.mux_total > 0)
162 if (maa_setup_mux_mapped(plat->pins[pos].i2c) != MAA_SUCCESS)
165 return plat->i2c_bus[bus].bus_id;
169 maa_setup_spi(int bus)
174 if (plat->spi_bus_count >! 0) {
175 fprintf(stderr, "No spi buses defined in platform");
178 if (bus >= plat->spi_bus_count) {
179 fprintf(stderr, "Above spi bus count");
183 int pos = plat->spi_bus[bus].sclk;
184 if (plat->pins[pos].spi.mux_total > 0)
185 if (maa_setup_mux_mapped(plat->pins[pos].spi) != MAA_SUCCESS)
188 pos = plat->spi_bus[bus].mosi;
189 if (plat->pins[pos].spi.mux_total > 0)
190 if (maa_setup_mux_mapped(plat->pins[pos].spi) != MAA_SUCCESS)
193 pos = plat->spi_bus[bus].miso;
194 if (plat->pins[pos].spi.mux_total > 0)
195 if (maa_setup_mux_mapped(plat->pins[pos].spi) != MAA_SUCCESS)
198 maa_spi_bus_t *spi = &(plat->spi_bus[bus]);
203 maa_setup_pwm(int pin)
208 if (plat->pins[pin].capabilites.pwm != 1)
211 if (plat->pins[pin].capabilites.gpio == 1) {
212 maa_gpio_context mux_i;
213 mux_i = maa_gpio_init_raw(plat->pins[pin].gpio.pinmap);
216 if (maa_gpio_dir(mux_i, MAA_GPIO_OUT) != MAA_SUCCESS)
218 // Current REV D quirk. //TODO GEN 2
219 if (maa_gpio_write(mux_i, 1) != MAA_SUCCESS)
221 if (maa_gpio_close(mux_i) != MAA_SUCCESS)
225 if (plat->pins[pin].pwm.mux_total > 0)
226 if (maa_setup_mux_mapped(plat->pins[pin].pwm) != MAA_SUCCESS)
230 ret = (maa_pin_t*) malloc(sizeof(maa_pin_t));
231 ret->pinmap = plat->pins[pin].pwm.pinmap;
232 ret->parent_id = plat->pins[pin].pwm.parent_id;
237 maa_result_print(maa_result_t result)
240 case MAA_SUCCESS: fprintf(stderr, "MAA: SUCCESS\n");
242 case MAA_ERROR_FEATURE_NOT_IMPLEMENTED:
243 fprintf(stderr, "MAA: Feature not implemented.\n");
245 case MAA_ERROR_FEATURE_NOT_SUPPORTED:
246 fprintf(stderr, "MAA: Feature not supported by Hardware.\n");
248 case MAA_ERROR_INVALID_VERBOSITY_LEVEL:
249 fprintf(stderr, "MAA: Invalid verbosity level.\n");
251 case MAA_ERROR_INVALID_PARAMETER:
252 fprintf(stderr, "MAA: Invalid parameter.\n");
254 case MAA_ERROR_INVALID_HANDLE:
255 fprintf(stderr, "MAA: Invalid Handle.\n");
257 case MAA_ERROR_NO_RESOURCES:
258 fprintf(stderr, "MAA: No resources.\n");
260 case MAA_ERROR_INVALID_RESOURCE:
261 fprintf(stderr, "MAA: Invalid resource.\n");
263 case MAA_ERROR_INVALID_QUEUE_TYPE:
264 fprintf(stderr, "MAA: Invalid Queue Type.\n");
266 case MAA_ERROR_NO_DATA_AVAILABLE:
267 fprintf(stderr, "MAA: No Data available.\n");
269 case MAA_ERROR_INVALID_PLATFORM:
270 fprintf(stderr, "MAA: Platform not recognised.\n");
272 case MAA_ERROR_PLATFORM_NOT_INITIALISED:
273 fprintf(stderr, "MAA: Platform not initialised.\n");
275 case MAA_ERROR_PLATFORM_ALREADY_INITIALISED:
276 fprintf(stderr, "MAA: Platform already initialised.\n");
278 case MAA_ERROR_UNSPECIFIED:
279 fprintf(stderr, "MAA: Unspecified Error.\n");
281 default: fprintf(stderr, "MAA: Unrecognised error.\n");
287 maa_pin_mode_test(int pin, maa_pinmodes_t mode)
294 if (pin > plat->phy_pin_count || pin < 0)
299 if (plat->pins[pin].capabilites.valid == 1)
303 if (plat->pins[pin].capabilites.gpio ==1)
307 if (plat->pins[pin].capabilites.pwm ==1)
310 case MAA_PIN_FAST_GPIO:
311 if (plat->pins[pin].capabilites.fast_gpio ==1)
315 if (plat->pins[pin].capabilites.spi ==1)
319 if (plat->pins[pin].capabilites.i2c ==1)
323 if (pin < plat->aio_count)
324 pin = pin + plat->gpio_count;
325 if (plat->pins[pin].capabilites.aio ==1)
334 maa_setup_mmap_gpio(int pin)
339 if (plat->pins[pin].capabilites.fast_gpio != 1)
342 if (plat->pins[pin].mmap.gpio.mux_total > 0)
343 if (maa_setup_mux_mapped(plat->pins[pin].mmap.gpio) != MAA_SUCCESS)
346 if (maa_setup_mux_mapped(plat->pins[pin].mmap.gpio) != MAA_SUCCESS)
348 maa_mmap_pin_t *ret = &(plat->pins[pin].mmap);