[Resolved] Omega 2+, python3 and spi-gpio-driver



  • Hello Onioneers,

    Having a bit of difficulty getting the spi-gpio-driver (info + instructions) to work with my python3 code (with python2 it works fine) on my omega 2+. Hoping you might have some ideas about it.

    When running python3 program, I'm not getting past the import statement - as seen below.

    Traceback (most recent call last):
      File "demos/omega.py", line 6, in <module>
        import onionSpi
    ImportError: dynamic module does not define module export function (PyInit_onionSpi)
    

    What I've tried:

    • Ordinary opkg install of libonionspi, pyOnionSpi
    • Cloning the spi-gpio-driver. Added missing dependencies from another Onion project (onion-debug.c and .h). Compiled it with setup.py on my local machine:
    from distutils.core import setup, Extension
    
    spi = Extension('python-onion-spi',
                    sources = ['src/python/python-onion-spi.c'],
    		include_dirs = ['include'])
    
    setup (name = 'OnionSpi',
           version = '1.0',
           description = 'This is an omega onion 2+ SPI package',
           ext_modules = [spi])
    
    

    ...Compiles fine, installs fine locally. Copied it over to /usr/lib/python3.6/lib-dynload/ on the Omega2+. No dice. Added the .egg-info file to /usr/lib/python3.6/site-packages/. No dice.

    • Compiling on my local machine using mips-openwrt-linux-uclibc-gcc as discussed in this thread. Results in :
    src/python/python-onion-spi.c:1:20: fatal error: Python.h: No such file or directory
     #include <Python.h>
                        ^
    compilation terminated.
    

    ....Adding the Python.h file (copied from /usr/include/python3.6 on the omega) to the include dir does not change this error. (I have python2.7 and python3.5 installed on my local machine and referenced in PATH).

    • Followed these cross-compile instructions on my local machine. Notably: added src-git onionSpi https://github.com/OnionIoT/spi-gpio-driver.git to the feeds.conf.default file and ran the update command. But a blank onionSpi.index file is created and attempting to scripts/feeds install onionSpi gives the following error:
    WARNING: No feed for package 'onionSpi' found, maybe it's already part of the standard packages?
    

    ...After following through with the process anyways, the onion spi package never gets added to the final image (even though it was selected in Step 5).

    Looking for any ideas on how to get the spi-gpio-driver working with python3. Thank you!



  • Still no luck.

    Updated the makefile for spi-gpio-driver to :

    # main compiler
    CC := mips-openwrt-linux-uclibc-gcc
    # CC := clang --analyze # and comment out the linker last line for sanity
    
    # define the directories
    SRCDIR := src
    INCDIR := include
    BUILDDIR := build
    BINDIR := bin
    LIBDIR := lib
    LIB := lib
    PYLIBDIR := lib/python
    LDFLAGS := lib
    
    # define common variables
    SRCEXT := c
    SOURCES := $(shell find $(SRCDIR) -maxdepth 1 -type f \( -iname "*.$(SRCEXT)" ! -iname "*main-*.$(SRCEXT)" \) )
    OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.$(SRCEXT)=.o))
    CFLAGS := -g -fPIC # -Wall
    INC := $(shell find $(INCDIR) -maxdepth 1 -type d -exec echo -I {}  \;)
    
    PYINC := "-I/usr/include/python3.5"
    INC += $(PYINC)
    
    # define specific binaries to create
    # lib onion debug
    LIB1 := liboniondebug
    SOURCE_LIB1 := src/onion-debug.$(SRCEXT)
    OBJECT_LIB1 := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCE_LIB1:.$(SRCEXT)=.o))
    TARGET_LIB1 := $(LIBDIR)/$(LIB1).so
    LIB_LIB1 := -L$(LIBDIR)
    
    # onion SPI C library
    LIB0 := libonionspi
    SOURCE_LIB0 := src/onion-spi.$(SRCEXT)
    OBJECT_LIB0 := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCE_LIB0:.$(SRCEXT)=.o))
    TARGET_LIB0 := $(LIBDIR)/$(LIB0).so
    LIB_LIB0 := -L$(LIBDIR) -loniondebug
    
    APP0 := spi-tool
    SOURCE_APP0 := $(SRCDIR)/main-$(APP0).$(SRCEXT)
    OBJECT_APP0 := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCE_APP0:.$(SRCEXT)=.o))
    LIB_APP0 := -L$(LIBDIR) -lonionspi -loniondebug
    TARGET_APP0 := $(BINDIR)/$(APP0)
    
    PYLIB0 := onionSpi
    SOURCE_PYLIB0 := src/python/python-onion-spi.c
    OBJECT_PYLIB0 := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCE_PYLIB0:.$(SRCEXT)=.o))
    TARGET_PYLIB0 := $(PYLIBDIR)/$(PYLIB0).so
    LIB_PYLIB0 := -L$(LIBDIR) -lonionspi -lpython3, -loniondebug
    
    
    all: info $(TARGET_LIB1) $(TARGET_LIB0) $(TARGET_APP0) $(TARGET_PYLIB0)
    
    
    # libraries
    # onion debug dependency
    $(TARGET_LIB1): $(OBJECT_LIB1)
    	@echo " Compiling $@"
    	@mkdir -p $(LIBDIR)
    	$(CC) -shared -o $@ $^ $(LIB_LIB1)
    
    # onion spi driver (C)
    $(TARGET_LIB0): $(OBJECT_LIB0)
    	@echo " Compiling $@"
    	$(CC) -shared -o $@ $^ $(LIB_LIB0)
    
    # python libraries
    $(TARGET_PYLIB0): $(OBJECT_PYLIB0)
    	@echo " Compiling $@"
    	@mkdir -p $(PYLIBDIR)
    	$(CC) -shared -o $@  $^ $(LIB_PYLIB0)
    
    # application binaries
    $(TARGET_APP0): $(OBJECT_APP0)
    	@echo " Compiling $(APP0)"
    	@mkdir -p $(BINDIR)
    	@echo " Linking..."
    	$(CC) $^ $(CFLAGS) -o $(TARGET_APP0) $(LIB_APP0)
    
    
    # generic: build any object file required
    $(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT)
    	@mkdir -p $(dir $@)
    	@echo " $(CC) $(CFLAGS) $(INC) -c -o $@ $<"; $(CC) $(CFLAGS) $(INC) -c -o $@ $<
    
    clean:
    	@echo " Cleaning...";
    	$(RM) -r $(BUILDDIR) $(BINDIR) $(LIBDIR)
    
    info:
    	@echo "CC: $(CC)"
    	@echo "CCFLAGS: $(CCFLAGS)"
    	@echo "LDFLAGS: $(LDFLAGS)"
    	@echo "LIBDIR: $(LIBDIR)"
    	@echo "LIB: $(LIB)"
    	@echo "INC: $(INC)"
    	@echo "SOURCES: $(SOURCES)"
    	@echo "OBJECTS: $(OBJECTS)"
    
    # Tests
    tester:
    	$(CC) $(CFLAGS) test/tester.cpp $(INC) $(LIB) -o bin/tester
    
    # Spikes
    #ticket:
    #  $(CC) $(CFLAGS) spikes/ticket.cpp $(INC) $(LIB) -o bin/ticket
    
    .PHONY: clean
    

    The biggest changes are :

    1. Inclusion of the onion debug library
    2. Changing python 2.7 to python 3.5

    This makefile compiles fine when CC := gcc. With CC := mips-openwrt-linux-uclibc-gcc, I get the following error:

    In file included from /usr/include/python3.5/Python.h:8:0,
                     from src/python/python-onion-spi.c:1:
    /usr/include/python3.5/pyconfig.h:65:5: error: #error unknown multiarch location for pyconfig.h
     #   error unknown multiarch location for pyconfig.h
    

    ... Which might be caused by having multiple versions of python installed.

    I tried the compiled (gcc) version of the onion spi library on the omega just to see. Error when running a program:
    ImportError: Error loading shared library /usr/lib/python3.6/onionSpi.so: Exec format error

    Then I tried making a Docker container running a 32-bit python image and running the compile there - no dice.

    Even with the official cross compile instructions using the docker container - didn't get any further.



  • Resolved this by:

    1. Cloned library to omega
    2. Installed gcc
    3. Compiled the spi-gpio-driver using makefile (only changed refs to python to python3)
    4. Copied relevant .so file (from lib/...) to /usr/lib/python3.6
    5. Program runs !

    Not the .ipkg I had hoped for but it'll work for now.



Looks like your connection to Onion Community was lost, please wait while we try to reconnect.