Commit 8e45c61e authored by Mathias Kreider's avatar Mathias Kreider

First working etherbone for Python Demo. Wrapper code is minimal but okay…

First working etherbone for Python Demo. Wrapper code is minimal but okay quality, Makefile is hacked crap and needs to be reworked for mininmal working example
parent 90d405a9
EBPATH ?= /home/mkreider/projects/bel_projects/ip_cores/etherbone-core/api
EB_LIB =-letherbone
EB_INCPATH = $(EBPATH)/include
LIBPATH = /home/mkreider/projects/ebPy
PREFIX ?= /usr/local
INSTALLPATH = $(PREFIX)/bin
T_CXX = g++
T_CXXFLAGS = -g -std=c++11 -fPIC -I$(LIBPATH) -I$(EB_INCPATH) -Wall -DETHERBONE_THROWS=1
SRC_FILES = ebwrapper.cpp ebwrapper_impl.cpp
T_SOURCES = $(addprefix $(SRCPATH)/, $(SRC_FILES))
T_OBJECTS = $(subst .cpp,.o,$(SRC_FILES))
T_LIBS = -Wl,-rpath,/usr/local/lib $(EB_LIB)
TOOLPATH = $(LIBPATH)
SRCPATH = $(LIBPATH)
all: lib foo
lib: libebwrapper.so _eb.so
eb_wrap.cxx: ebwrapper.h eb.i
swig -Wall -c++ -python eb.i
eb_wrap.o: eb_wrap.cxx
$(T_CXX) $(FOO) $(T_CXXFLAGS) -c $< -o $@ -lpython2.7 -I/usr/include/python2.7/
%.o: $(SRCPATH)/%.cpp
$(T_CXX) $(FOO) $(T_CXXFLAGS) -c $< -o $@ $(T_LIBS)
libebwrapper.so: $(T_OBJECTS)
$(T_CXX) $(T_CXXFLAGS) -shared -o $@ $^ $(T_LIBS)
_eb.so: $(T_OBJECTS) eb_wrap.o libebwrapper.so
$(T_CXX) $(T_CXXFLAGS) -shared -o $@ eb_wrap.o $(LIBPATH)/libebwrapper.so -lpython2.7 -I/usr/include/python2.7/
clean::
rm -f $(TOOLPATH)/*.o $(TOOLPATH)/*.a $(TOOLPATH)/*.so $(TOOLPATH)/*.elf $(TOOLPATH)/libebwrapper.so $(TOOLPATH)/_eb.so $(TOOLPATH)/foo $(TOOLPATH)/*.pyc $(TOOLPATH)/eb.py $(TOOLPATH)/eb_wrap.cxx
.SECONDEXPANSION:
foo: main.cpp libebwrapper.so
$(T_CXX) $(T_CXXFLAGS) -o $@ $< $(LIBPATH)/libebwrapper.so $(T_LIBS)
#
#g++ -fPIC -Wall -Wextra -shared etherbone_wrap.cxx -o _etherbone.so -lstdc++ -L. -letherbone -lpython2.7 -Wl,--verbose -I/usr/include/python2.7/
%module eb
%include stdint.i
%include std_string.i
%include typemaps.i
%include cpointer.i
// Make SWIG look into this header:
%include "ebwrapper.h"
// Make etherbone_wrap.cxx include this header:
%{
#include "ebwrapper.h"
%}
import eb
ebobj = eb.EbWrapper()
ebobj.connect("dev/ttyUSB0")
print("0x%8x" % ebobj.read(0x4120000))
\ No newline at end of file
#include "ebwrapper.h"
#include "ebwrapper_impl.h"
EbWrapper::EbWrapper() : impl_(new EbWrapperImpl()) {}
EbWrapper::~EbWrapper() = default;
// Etherbone interface
bool EbWrapper::connect(const std::string& en) {return impl_->connect(en);} //Open connection to a DM via Etherbone
bool EbWrapper::disconnect() {return impl_->disconnect();} //Close connection
void EbWrapper::write(const unsigned addr, const unsigned value) const {return impl_->write(addr, value);} //Open connection to a DM via Etherbone
unsigned EbWrapper::read(const unsigned addr) const {return impl_->read(addr);} //Close connection
\ No newline at end of file
#ifndef _EB_WRAPPER_H_
#define _EB_WRAPPER_H_
#include <stdio.h>
#include <iostream>
#include <string>
#include <inttypes.h>
#include <memory>
class EbWrapper {
private:
class EbWrapperImpl;
std::unique_ptr<EbWrapperImpl> impl_;
public:
EbWrapper();
~EbWrapper();
bool connect(const std::string& ebdevname);
bool disconnect(); //Close connection
//bool writeCycle(std::vector<unsigned> vVal) const;
void write(const unsigned addr, const unsigned value) const;
//std::vector<unsigned> readCycle(std::vector<unsigned> vAddr) const;
unsigned read(const unsigned addr) const;
};
#endif
\ No newline at end of file
#include "ebwrapper_impl.h"
EbWrapper::EbWrapperImpl::EbWrapperImpl() {}
EbWrapper::EbWrapperImpl::~EbWrapperImpl() {}
bool EbWrapper::EbWrapperImpl::connect(const std::string& dev) {
ebdevname = dev;
bool ret;
try {
ebs.open(0, EB_DATAX|EB_ADDRX);
ebd.open(ebs, ebdevname.c_str(), EB_DATAX|EB_ADDRX, 3);
ret = true;
} catch (etherbone::exception_t const& ex) {
ret = false;
} return ret;
}
bool EbWrapper::EbWrapperImpl::disconnect() {
bool ret;
try {
ebd.close();
ebs.close();
ret = true;
} catch (etherbone::exception_t const& ex) {
ret = false;
}
return ret;
}
void EbWrapper::EbWrapperImpl::write(const unsigned addr, const unsigned value) {
ebd.write((eb_address_t)addr, EB_DATAX|EB_ADDRX, (eb_data_t)value);
}
//std::vector<unsigned> readCycle(std::vector<unsigned> vAddr) const;
unsigned EbWrapper::EbWrapperImpl::read(const unsigned addr) {
eb_data_t ret;
ebd.read((eb_address_t)addr, EB_DATAX|EB_ADDRX, (eb_data_t*)&ret);
return (unsigned)ret;
}
\ No newline at end of file
#ifndef _EB_WRAPPER_IMPL_H_
#define _EB_WRAPPER_IMPL_H_
#include <stdio.h>
#include <iostream>
#include <string>
#include <inttypes.h>
#include "etherbone.h"
#include "ebwrapper.h"
#define SDB_VENDOR_GSI 0x0000000000000651ULL
#define SDB_DEVICE_LM32_RAM 0x54111351
#define SDB_DEVICE_DIAG 0x18060200
using namespace etherbone;
class EbWrapper::EbWrapperImpl {
private:
Socket ebs;
Device ebd;
std::string ebdevname;
public:
EbWrapperImpl();
~EbWrapperImpl();
bool connect(const std::string& ebdevname);
bool disconnect(); //Close connection
//bool writeCycle(std::vector<unsigned> vVal) const;
void write(const unsigned addr, const unsigned value);
//std::vector<unsigned> readCycle(std::vector<unsigned> vAddr) const;
unsigned read(const unsigned addr);
};
#endif
\ No newline at end of file
#include <stdio.h>
#include <iostream>
#include <string>
#include <inttypes.h>
#include <time.h>
#include <unistd.h>
#include "ebwrapper.h"
int main(int argc, char* argv[]) {
EbWrapper ebw;
try {
ebw.connect(std::string("dev/ttyUSB0"));
} catch (std::runtime_error const& err) {
std::cerr << ": Could not connect to TR. Cause: " << err.what() << std::endl; return -20;
}
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment