Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contributions related to Medium Article #11

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Exporting to C++ and Python3
  • Loading branch information
nowinnovation authored and nOw Innovation committed Jun 9, 2022
commit e733c5c50b5fc2ec63206c515ce3f98657c32b17
1 change: 1 addition & 0 deletions lib_jtag_core/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ set(JTAG_BSDL_SRC
)

add_library(jtag_bsdl ${JTAG_BSDL_SRC})
set_property(TARGET jtag_bsdl PROPERTY POSITION_INDEPENDENT_CODE ON)

2 changes: 1 addition & 1 deletion lib_jtag_core/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ cmake_minimum_required (VERSION 2.8.11)
# Recurse into the linux, imports and apps subdirectories.
add_subdirectory (linux)
add_subdirectory (imports)
#add_subdirectory (apps)
add_subdirectory (apps)

10 changes: 10 additions & 0 deletions lib_jtag_core/test/apps/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Include language-specific user-interfaces
add_subdirectory(cxx)
find_package(PythonInterp 3 EXACT QUIET)
if(${PYTHONINTERP_FOUND})
find_package(PythonLibs 3 EXACT QUIET)
if(${PYTHONLIBS_FOUND})
add_subdirectory(py3)
endif()
endif()

13 changes: 13 additions & 0 deletions lib_jtag_core/test/apps/cxx/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Create a library called "xcSwigHere" and specify the source files.
# The extension is already found. Any number of sources could be listed here.
add_subdirectory(bindings)

# CPP console application
add_executable (cppUI cppUI.cxx)
set_property(TARGET cppUI PROPERTY POSITION_INDEPENDENT_CODE ON)
#set_target_properties(cppUI PROPERTIES LINK_LIBRARIES -pthread)

# Link the executable to the libraries upon which it depends.
# Libraries with public include directories will have those link
# directories automatically configured when building the executable
target_link_libraries (cppUI PUBLIC xcSwigHere epCoreJTAG_ jtag_bsdl)
18 changes: 18 additions & 0 deletions lib_jtag_core/test/apps/cxx/bindings/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Create static and shared libraries and specify the source files.
# Any number of sources could be listed here.
add_library(xcSwigHere_ STATIC xcSwigHere.cxx)
add_library(xcSwigHere SHARED xcSwigHere.cxx)

# For static library, enable position independent code
set_property(TARGET xcSwigHere_ PROPERTY POSITION_INDEPENDENT_CODE ON)

# Follow these directions to minimize the number of shared libraries generated:
# Link the static binding to the shared version of the project's API library
target_link_libraries (xcSwigHere_ LINK_PUBLIC epCoreJTAG)
# Link the shared binding to the static version of the project's API library
target_link_libraries (xcSwigHere LINK_PUBLIC epCoreJTAG_)

# Make sure the compiler can find include files for our library
# when other libraries or executables link to fftTOO
target_include_directories(xcSwigHere_ PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_include_directories(xcSwigHere PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
34 changes: 34 additions & 0 deletions lib_jtag_core/test/apps/cxx/bindings/xcSwigHere.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// xcSwigHere.cpp

#include "xcSwigHere.h"
#include <cstddef>
#include "stdio.h" // for debugging

static SWIGHERE_CONTEXT pContext=NULL;

extern void xcUninitSwigHere() {
if(pContext != NULL) {
epUninitSwigHere(pContext);
pContext=NULL;
}
}

extern void xcInitSwigHere() {
xcUninitSwigHere();
pContext=epInitSwigHere();
//return pContext;
}

extern unsigned long int xcBSDLDeviceId(char * pathToBSDL) {
return epBSDLDeviceId(pContext,pathToBSDL);
}

extern std::vector<pin_ctrl> xcBSDLPinSequence(char * pathToBSDL) {
std::vector<pin_ctrl> noresult;
unsigned bytesPerElement=0;
std::vector<pin_ctrl> pinSequence = epBSDLPinSequence(pContext, pathToBSDL,&bytesPerElement);
if( bytesPerElement == sizeof(pin_ctrl)) {
return pinSequence;
}
return noresult;
}
10 changes: 10 additions & 0 deletions lib_jtag_core/test/apps/cxx/bindings/xcSwigHere.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// xcSwigHere.h
#pragma once

#include <vector>
#include "epSwigHere.h"

extern void xcInitSwigHere();
extern unsigned long int xcBSDLDeviceId(char * pathToBSDL);
extern std::vector<pin_ctrl> xcBSDLPinSequence(char * pathToBSDL);
extern void xcUninitSwigHere();
26 changes: 26 additions & 0 deletions lib_jtag_core/test/apps/cxx/cppUI.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "xcSwigHere.h"

int main(int argc, char * argv[]) {
int lastResult=-1;
unsigned cbInt=0;
unsigned nInts=0;
const int * ptr;
xcInitSwigHere();
if(argc>1) {
long unsigned int id = xcBSDLDeviceId(argv[1]);
if(id > 0 ) {
std::vector<pin_ctrl> pinSequence = xcBSDLPinSequence(argv[1]);
printf("BSDL file describes device id %lx\r\n",id);
for(unsigned i = 0; i<pinSequence.size(); i++) {
printf("%i\t",i);
}
printf("\r\n");
}
}
xcUninitSwigHere();
return lastResult;
}
7 changes: 7 additions & 0 deletions lib_jtag_core/test/apps/py3/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Include bindings to projects API libraries
add_subdirectory(bindings)

# Demo Python3 file showing how to load and run the application
configure_file(py3UI.py py3UI.py @ONLY)
ADD_CUSTOM_TARGET(py3UI SOURCES py3UI.py)
set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "py3UI.py" )
36 changes: 36 additions & 0 deletions lib_jtag_core/test/apps/py3/bindings/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#load the cmake SWIG package
find_package(SWIG REQUIRED)
include(${SWIG_USE_FILE})

#load the package that SWIG uses to generate Python
find_package(PythonInterp 3 EXACT REQUIRED)
find_package(PythonLibs 3 EXACT REQUIRED)


#point to python headers
include_directories(${PYTHON_INCLUDE_DIRS})

# Need to be explicit about the include directoried used in SWIG
include_directories(${PROJECT_SOURCE_DIR}/test/imports)
include_directories(${CMAKE_CURRENT_SOURCE_DIR})

# specify the SWIG interface for the module
set_property(SOURCE py3SwigHere.i PROPERTY CPLUSPLUS ON)
set_property(SOURCE py3SwigHere.i PROPERTY SWIG_MODULE_NAME pySwigHere)
#set_property(SOURCE py3SwigHere.i PROPERTY SWIG_MODULE_NAME py3SwigHere)

swig_add_library(
pySwigHere
# ALIAS py3SwigHere
TYPE SHARED
LANGUAGE python
SOURCES py3SwigHere.i py3SwigHere.cxx)

SWIG_LINK_LIBRARIES(pySwigHere ${PYTHON_LIBRARIES} epCoreJTAG_ jtag_bsdl)

set_property(SOURCE ${swig_generated_file_fullname} PROPERTY OUTPUT_NAME pySwigHere)
get_property(SHARED_LIB_NAME SOURCE ${swig_generated_file_fullname} PROPERTY OUTPUT_NAME)

configure_file(init.in __init__.py @ONLY)
ADD_CUSTOM_TARGET(initPy3 ALL SOURCES __init__.py)
set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "__init__.py" )
2 changes: 2 additions & 0 deletions lib_jtag_core/test/apps/py3/bindings/init.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# __init__.py
from .@SHARED_LIB_NAME@ import *
43 changes: 43 additions & 0 deletions lib_jtag_core/test/apps/py3/bindings/py3SwigHere.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// py3SwigHere.cpp
// compile with SWIG

#include "py3SwigHere.h"
#include "epSwigHere.h"
#include <cstddef>

static SWIGHERE_CONTEXT pContext=NULL;

extern void pyUninitSwigHere() {
if(pContext != NULL) {
epUninitSwigHere(pContext);
pContext=NULL;
}
}

extern void pyInitSwigHere() {
pyUninitSwigHere();
pContext=epInitSwigHere();
//return pContext;
}

extern unsigned long int pyBSDLDeviceId(std::string pathToBSDL) {
if(pContext) {
return epBSDLDeviceId(pContext,(char*)pathToBSDL.c_str());
}
return 0;
}

extern std::vector<int> pyBSDLPinSequence(std::string pathToBSDL) {
std::vector<int> result;
if(pContext) {
unsigned bytesPerElement=0, elementCount=0;
std::vector<pin_ctrl> pinSequence = epBSDLPinSequence(pContext, (char*)pathToBSDL.c_str(), &bytesPerElement);
if( bytesPerElement == sizeof(pin_ctrl) ) {
for(unsigned i = 0; i<pinSequence.size(); i++) {
result.push_back(i);
}
}
}

return result;
}
10 changes: 10 additions & 0 deletions lib_jtag_core/test/apps/py3/bindings/py3SwigHere.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// py3SwigHere.h
#pragma once

#include <vector>
#include <string>

extern void pyInitSwigHere();
extern void pyUninitSwigHere();
extern unsigned long int pyBSDLDeviceId(std::string pathToBSDL);
extern std::vector<int> pyBSDLPinSequence(std::string pathToBSDL);
29 changes: 29 additions & 0 deletions lib_jtag_core/test/apps/py3/bindings/py3SwigHere.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// py3SwigHere.i
%module pySwigHere

%{
#define SWIG_FILE_WITH_INIT
%}

%init %{
%}

%include "std_vector.i"
%include "std_string.i"

%{
#include "py3SwigHere.h"
%}

%template(vectori) std::vector<int>;

%extend std::vector<int> {
void empty_and_delete() {
for (std::vector<int>::iterator it = $self->begin();
it != $self->end(); ++it) {
// if it was a pointer, we could delete *it;
}
$self->clear();
}
}
%include "py3SwigHere.h"
32 changes: 32 additions & 0 deletions lib_jtag_core/test/apps/py3/py3UI.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
Sample program showing import of external library in Python 3
"""
from __future__ import print_function
import bindings as SWIGHERE

class CSwigHere:
def __init__(self):
pass

def pyBSDLDeviceId(self,pathToBSDL):
return SWIGHERE.pyBSDLDeviceId(pathToBSDL);

def pyBSDLPinSequence(self,pathToBSDL):
return SWIGHERE.pyBSDLPinSequence(pathToBSDL);

def __enter__(self):
SWIGHERE.pyInitSwigHere();
return self

def __exit__(self,exType,exValue,trace):
SWIGHERE.pyUninitSwigHere();

if __name__=="__main__":
import sys
with CSwigHere() as instance:
if len(sys.argv) > 1:
deviceId = instance.pyBSDLDeviceId(sys.argv[1])
if deviceId > 0:
pinList = instance.pyBSDLPinSequence(sys.argv[1])
print(pinList)

6 changes: 4 additions & 2 deletions lib_jtag_core/test/imports/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ set_property(TARGET epCoreJTAG_ PROPERTY POSITION_INDEPENDENT_CODE ON)
# Make sure the compiler can find include files for our library
# when other libraries or executables link to fftTOO
target_include_directories (epCoreJTAG_ PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} "${STREETVIEWJTAG_SOURCE_DIR}/src")
target_include_directories (epCoreJTAG PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} "${STREETVIEWJTAG_SOURCE_DIR}/src")
set_property(TARGET epCoreJTAG_ PROPERTY INTERFACE_LINK_LIBRARIES jtag_bsdl)
target_link_libraries(epCoreJTAG_ jtag_bsdl)

target_include_directories (epCoreJTAG PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} "${STREETVIEWJTAG_SOURCE_DIR}/src")
set_property(TARGET epCoreJTAG PROPERTY INTERFACE_LINK_LIBRARIES jtag_bsdl)
set_property(TARGET epCoreJTAG_ PROPERTY INTERFACE_LINK_LIBRARIES jtag_bsdl)
target_link_libraries(epCoreJTAG jtag_bsdl)

add_executable(uiCoreJTAG uiCoreJTAG.cxx)
target_link_libraries(uiCoreJTAG epCoreJTAG jtag_bsdl)
Expand Down
31 changes: 28 additions & 3 deletions lib_jtag_core/test/imports/epCoreJTAG.cxx
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
#include <stddef.h>
#include <stdio.h>
#include "libjtag_bsdl.h"

#include "epSwigHere.h"
#include <memory>
#include <type_traits>
#include <vector>


static void logger(char * string) {
printf("%s",string);
Expand Down Expand Up @@ -70,3 +67,31 @@ extern long unsigned int epBSDLDeviceId(SWIGHERE_CONTEXT oContext, char * pathTo
}
return (long unsigned int) result;
}

extern std::vector<pin_ctrl> epBSDLPinSequence(SWIGHERE_CONTEXT oContext, char * pathToBSDL, unsigned * pBytesPerElement) {
int * p=NULL;
std::vector<pin_ctrl> result;

if(pBytesPerElement != NULL) {
*pBytesPerElement = sizeof(pin_ctrl);
}
if(oContext==(SWIGHERE_CONTEXT)&garbageCollector) {
jtag_bsdl * details = jtag_bsdl_load_file(logger,MSG_DEBUG, 0, pathToBSDL);
if ( details != NULL ) {
int number_of_pins = details->number_of_pins;
int i = 0;
if(number_of_pins>0) {
while(i<number_of_pins) {
pin_ctrl * pins_list = details->pins_list;
result.push_back(pins_list[i]);
i++;
}
}
}
}

return result;
};

//extern std::vector<jtag_chain> epBSDLChainSequence(SWIGHERE_CONTEXT oContext, char * pathToFile, unsigned * pBytesPerElement,unsigned * pElementCount);
//extern std::vector<pin_ctrl> epBSDLPinSequence(SWIGHERE_CONTEXT oContext, char * pathToBSDL, unsigned * pBytesPerElement,unsigned * pElementCount) {
11 changes: 7 additions & 4 deletions lib_jtag_core/test/imports/epSwigHere.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// epSWIGHere.h for CoreJTAG
// epSWIGHere.h for libjtag_bsdl
#pragma once
#include <utility>
#include <vector>
#include <string>

#include "libjtag_bsdl.h"

typedef enum _SWIGHERE_TYPE {
VOIDPTR=0,
INTPTR
Expand All @@ -14,7 +17,7 @@ typedef std::pair<SWIGHERE_TYPE,int *> SWIGHERE_INTLIST;
typedef volatile void * SWIGHERE_CONTEXT;

extern SWIGHERE_CONTEXT epInitSwigHere();
extern long unsigned int epBSDLDeviceId(SWIGHERE_CONTEXT oContext, char * pathToFile);
extern SWIGHERE_INTLIST epSequence(SWIGHERE_CONTEXT oContext,unsigned * pBytesPerElement,unsigned * pElementCount);
//extern SWIGHERE_INTLIST epBSDLPinMap(SWIGHERE_CONTEXT oContext,unsigned * pBytesPerElement,unsigned * pElementCount);
extern long unsigned int epBSDLDeviceId(SWIGHERE_CONTEXT oContext, char * pathToBSDL);
extern std::vector<pin_ctrl> epBSDLPinSequence(SWIGHERE_CONTEXT oContext, char * pathToBSDL, unsigned * pBytesPerElement);
extern std::vector<jtag_chain> epBSDLChainSequence(SWIGHERE_CONTEXT oContext, char * pathToBSDL, unsigned * pBytesPerElement);
extern void epUninitSwigHere(SWIGHERE_CONTEXT oContext);
2 changes: 0 additions & 2 deletions lib_jtag_core/test/imports/uiCoreJTAG.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ int main(int argc, char * argv[], char ** envp) {
if(context != NULL) {
if(argc>1) {
long unsigned int id = epBSDLDeviceId(context, argv[1]);
//SWIGHERE_INTLIST epSequence(SWIGHERE_CONTEXT oContext,unsigned * pBytesPerElement,unsigned * pElementCount);
//extern SWIGHERE_INTLIST epBSDLPinMap(SWIGHERE_CONTEXT oContext,unsigned * pBytesPerElement,unsigned * pElementCount);
printf("BSDL file describes device id %lx\r\n",id);
}
epUninitSwigHere(context);
Expand Down