From b0a21e2c300d47b425ab5bdcdd9e63cb00e9b84a Mon Sep 17 00:00:00 2001 From: Thomas Ingleby Date: Fri, 21 Nov 2014 00:01:08 +0000 Subject: [PATCH] arch: initial framework for supporting other architectures added. x86: all platforms moved into x86 directory Signed-off-by: Thomas Ingleby --- CMakeLists.txt | 17 ++++-- include/mraa_internal.h | 7 +++ src/CMakeLists.txt | 10 ++-- src/mraa.c | 61 +++----------------- src/x86/CMakeLists.txt | 10 ++++ src/{ => x86}/intel_de3815.c | 0 src/{ => x86}/intel_edison_fab_c.c | 0 src/{ => x86}/intel_galileo_rev_d.c | 0 src/{ => x86}/intel_galileo_rev_g.c | 0 src/{ => x86}/intel_minnow_max.c | 0 src/x86/x86.c | 86 +++++++++++++++++++++++++++++ 11 files changed, 129 insertions(+), 62 deletions(-) create mode 100644 src/x86/CMakeLists.txt rename src/{ => x86}/intel_de3815.c (100%) rename src/{ => x86}/intel_edison_fab_c.c (100%) rename src/{ => x86}/intel_galileo_rev_d.c (100%) rename src/{ => x86}/intel_galileo_rev_g.c (100%) rename src/{ => x86}/intel_minnow_max.c (100%) create mode 100644 src/x86/x86.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 07d7a02..716597c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -56,6 +56,19 @@ option (BUILDSWIGPYTHON "Build swig python modules." ON) option (BUILDSWIGNODE "Build swig node modules." ON) option (IPK "Generate IPK using CPack" OFF) +include (TargetArch) +target_architecture (DETECTED_ARCH) +message( INFO " - Target arch is ${DETECTED_ARCH}") + +if (DETECTED_ARCH STREQUAL "i586" OR DETECTED_ARCH STREQUAL "x86_64" + OR DETECTED_ARCH STREQUAL "i386") + set (X86PLAT ON) +elseif (DETECTED_ARCH MATCHES "arm.*") + set (ARMPLAT ON) +else () + message(FATAL_ERROR "Only x86 and arm platforms currently supported") +endif() + if (GTEST) enable_testing () add_subdirectory (tests) @@ -75,10 +88,6 @@ if (BUILDDOC) endif () if (IPK) - include (TargetArch) - target_architecture (DETECTED_ARCH) - message( INFO " - Target arch is ${DETECTED_ARCH}") - set(CPACK_GENERATOR "DEB" "TGZ") set(OPKG_ARCH ${DETECTED_ARCH}) set(CPACK_BINARY_DIR ${CMAKE_BINARY_DIR}) diff --git a/include/mraa_internal.h b/include/mraa_internal.h index 235dcfc..8905f65 100644 --- a/include/mraa_internal.h +++ b/include/mraa_internal.h @@ -53,6 +53,13 @@ mraa_result_t mraa_setup_mux_mapped(mraa_pin_t meta); */ mraa_result_t mraa_setup_uart(int index); +/** + * Runtime detect running x86 platform + * + * @return mraa_platform_t of the init'ed platform + */ +mraa_platform_t mraa_x86_platform(); + #ifdef __cplusplus } #endif diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2da70a8..adf4c71 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -16,13 +16,13 @@ set (mraa_LIB_SRCS_NOAUTO ${PROJECT_SOURCE_DIR}/src/spi/spi.c ${PROJECT_SOURCE_DIR}/src/aio/aio.c ${PROJECT_SOURCE_DIR}/src/uart/uart.c - ${PROJECT_SOURCE_DIR}/src/intel_galileo_rev_d.c - ${PROJECT_SOURCE_DIR}/src/intel_galileo_rev_g.c - ${PROJECT_SOURCE_DIR}/src/intel_edison_fab_c.c - ${PROJECT_SOURCE_DIR}/src/intel_de3815.c - ${PROJECT_SOURCE_DIR}/src/intel_minnow_max.c ) +if (X86PLAT) + add_subdirectory(x86) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DX86PLAT=1") +endif() + set (mraa_LIB_SRCS ${mraa_LIB_SRCS_NOAUTO} # autogenerated version file diff --git a/src/mraa.c b/src/mraa.c index 5b761f0..321e165 100644 --- a/src/mraa.c +++ b/src/mraa.c @@ -31,11 +31,6 @@ #define DEBUG #include "mraa_internal.h" -#include "intel_galileo_rev_d.h" -#include "intel_galileo_rev_g.h" -#include "intel_edison_fab_c.h" -#include "intel_de3815.h" -#include "intel_minnow_max.h" #include "gpio.h" #include "version.h" @@ -80,59 +75,19 @@ mraa_init() Py_InitializeEx(0); PyEval_InitThreads(); #endif - // detect a galileo gen2 board - char *line = NULL; - // let getline allocate memory for *line - size_t len = 0; - FILE *fh = fopen("/sys/devices/virtual/dmi/id/board_name", "r"); - if (fh != NULL) { - if (getline(&line, &len, fh) != -1) { - if (strncmp(line, "GalileoGen2", 11) == 0) { - platform_type = MRAA_INTEL_GALILEO_GEN2; - } else if (strncmp(line, "BODEGA BAY", 10) == 0) { - platform_type = MRAA_INTEL_EDISON_FAB_C; - } else if (strncmp(line, "SALT BAY", 8) == 0) { - platform_type = MRAA_INTEL_EDISON_FAB_C; - } else if (strncmp(line, "DE3815", 6) == 0) { - platform_type = MRAA_INTEL_DE3815; - } else if (strncmp(line, "NOTEBOOK", 8) == 0) { - platform_type = MRAA_INTEL_MINNOWBOARD_MAX; - } else if (strncasecmp(line, "MinnowBoard MAX", 15) == 0) { - platform_type = MRAA_INTEL_MINNOWBOARD_MAX; - } else { - platform_type = MRAA_INTEL_GALILEO_GEN1; - } - free(line); - } - fclose(fh); - } - advance_func = (mraa_adv_func_t*) malloc(sizeof(mraa_adv_func_t)); memset(advance_func, 0, sizeof(mraa_adv_func_t)); - switch(platform_type) { - case MRAA_INTEL_GALILEO_GEN2: - plat = mraa_intel_galileo_gen2(); - break; - case MRAA_INTEL_GALILEO_GEN1: - plat = mraa_intel_galileo_rev_d(); - break; - case MRAA_INTEL_EDISON_FAB_C: - plat = mraa_intel_edison_fab_c(); - break; - case MRAA_INTEL_DE3815: - plat = mraa_intel_de3815(); - break; - case MRAA_INTEL_MINNOWBOARD_MAX: - plat = mraa_intel_minnow_max(); - break; +#ifdef X86PLAT + // Use runtime x86 platform detection + platform_type = mraa_x86_platform(); +#endif - default: - plat = mraa_intel_galileo_rev_d(); - syslog(LOG_ERR, "Platform not supported, initialising as MRAA_INTEL_GALILEO_GEN1"); + if (plat == NULL) { + printf("mraa: FATAL error, failed to initialise platform\n"); + return MRAA_ERROR_PLATFORM_NOT_INITIALISED; } - - syslog(LOG_NOTICE, "libmraa initialised for platform %d", platform_type); + syslog(LOG_INFO, "libmraa initialised for platform %d", platform_type); return MRAA_SUCCESS; } diff --git a/src/x86/CMakeLists.txt b/src/x86/CMakeLists.txt new file mode 100644 index 0000000..7b14d6d --- /dev/null +++ b/src/x86/CMakeLists.txt @@ -0,0 +1,10 @@ +message (INFO " - Adding x86 platforms") +set (mraa_LIB_SRCS_NOAUTO ${mraa_LIB_SRCS_NOAUTO} + ${PROJECT_SOURCE_DIR}/src/x86/x86.c + ${PROJECT_SOURCE_DIR}/src/x86/intel_galileo_rev_d.c + ${PROJECT_SOURCE_DIR}/src/x86/intel_galileo_rev_g.c + ${PROJECT_SOURCE_DIR}/src/x86/intel_edison_fab_c.c + ${PROJECT_SOURCE_DIR}/src/x86/intel_de3815.c + ${PROJECT_SOURCE_DIR}/src/x86/intel_minnow_max.c + PARENT_SCOPE +) diff --git a/src/intel_de3815.c b/src/x86/intel_de3815.c similarity index 100% rename from src/intel_de3815.c rename to src/x86/intel_de3815.c diff --git a/src/intel_edison_fab_c.c b/src/x86/intel_edison_fab_c.c similarity index 100% rename from src/intel_edison_fab_c.c rename to src/x86/intel_edison_fab_c.c diff --git a/src/intel_galileo_rev_d.c b/src/x86/intel_galileo_rev_d.c similarity index 100% rename from src/intel_galileo_rev_d.c rename to src/x86/intel_galileo_rev_d.c diff --git a/src/intel_galileo_rev_g.c b/src/x86/intel_galileo_rev_g.c similarity index 100% rename from src/intel_galileo_rev_g.c rename to src/x86/intel_galileo_rev_g.c diff --git a/src/intel_minnow_max.c b/src/x86/intel_minnow_max.c similarity index 100% rename from src/intel_minnow_max.c rename to src/x86/intel_minnow_max.c diff --git a/src/x86/x86.c b/src/x86/x86.c new file mode 100644 index 0000000..1abbc73 --- /dev/null +++ b/src/x86/x86.c @@ -0,0 +1,86 @@ +/* + * Author: Thomas Ingleby + * Copyright (c) 2014 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include + +#include "mraa_internal.h" +#include "intel_galileo_rev_d.h" +#include "intel_galileo_rev_g.h" +#include "intel_edison_fab_c.h" +#include "intel_de3815.h" +#include "intel_minnow_max.h" + +mraa_platform_t +mraa_x86_platform() +{ + mraa_platform_t platform_type = MRAA_UNKNOWN_PLATFORM; + char *line = NULL; + // let getline allocate memory for *line + size_t len = 0; + FILE *fh = fopen("/sys/devices/virtual/dmi/id/board_name", "r"); + if (fh != NULL) { + if (getline(&line, &len, fh) != -1) { + if (strncmp(line, "GalileoGen2", 11) == 0) { + platform_type = MRAA_INTEL_GALILEO_GEN2; + } else if (strncmp(line, "BODEGA BAY", 10) == 0) { + platform_type = MRAA_INTEL_EDISON_FAB_C; + } else if (strncmp(line, "SALT BAY", 8) == 0) { + platform_type = MRAA_INTEL_EDISON_FAB_C; + } else if (strncmp(line, "DE3815", 6) == 0) { + platform_type = MRAA_INTEL_DE3815; + } else if (strncmp(line, "NOTEBOOK", 8) == 0) { + platform_type = MRAA_INTEL_MINNOWBOARD_MAX; + } else if (strncasecmp(line, "MinnowBoard MAX", 15) == 0) { + platform_type = MRAA_INTEL_MINNOWBOARD_MAX; + } else { + platform_type = MRAA_INTEL_GALILEO_GEN1; + } + free(line); + } + fclose(fh); + } + + switch(platform_type) { + case MRAA_INTEL_GALILEO_GEN2: + plat = mraa_intel_galileo_gen2(); + break; + case MRAA_INTEL_GALILEO_GEN1: + plat = mraa_intel_galileo_rev_d(); + break; + case MRAA_INTEL_EDISON_FAB_C: + plat = mraa_intel_edison_fab_c(); + break; + case MRAA_INTEL_DE3815: + plat = mraa_intel_de3815(); + break; + case MRAA_INTEL_MINNOWBOARD_MAX: + plat = mraa_intel_minnow_max(); + break; + + default: + plat = mraa_intel_galileo_rev_d(); + syslog(LOG_ERR, "Platform not supported, initialising as MRAA_INTEL_GALILEO_GEN1"); + } + return platform_type; +}