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

Pi5 spi ioctl #391

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
360 changes: 360 additions & 0 deletions libraries/pi4j-library-kernel/pom.xml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@


Creation
Application code:
SPIapiImpl functionsV1 = SPIapiIntrf.createSPIapiImpl();

return(SPIapiImpl.newInstance("libpi4j-kernel.so", "pi4j-kernel"));
SPIapiImpl newInstance called with lib and so
impl created
impl calls its initialize( uses name .so and library name )
The initialize sets functionsNative reference to
the SO
Next create and initialize the spiApi_t structure.
Call functionsV1.spiApi_init opens SPI path and sets some SPI details.


Operation.
functionsV1.spiApi_write(SPIapi.spiApi_t spiApi, byte[] b, int offset, int len, int speed);
In the Impl method. it uses the contained NativeFunc pointer to all the C code
C linkage uses JNA to pass control to C library, upon completion returns to java test case






Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package com.pi4j.library.kernel;


import com.pi4j.Pi4J;
import com.pi4j.context.Context;
import com.pi4j.util.Console;
import com.sun.jna.*;
import com.sun.jna.Structure.*;


public class SPIapi {

// Console console = new Console();
// Context pi4j = Pi4J.newAutoContext();






@FieldOrder({"returnEnumGood","returnEnumFail"})
public static class spiApi_return_t extends Structure {
public int return_code;
}

@FieldOrder({"waldo","where"})
public static class spiApi_test extends Structure {
public int waldo;
public int where;
}


@FieldOrder({"max_freq","spi_FD", "bus", "cs", "dev"})
public static class spiApi_t extends Structure{
public int max_freq;
public int spi_FD;
public int bus;
public int cs; //
public spiApi_device_p.ByReference dev;

public spiApi_t() {
super();
}
}

@FieldOrder({"driver_mode","CEx","CExResrv","AuxSPI","wire","bits","firstBitTX","firstBitRX","wSize"})
public static class spiApi_device_p extends Structure {
public int driver_mode;
public int CEx; // Chip enable low:0
public int CExResrv; // chip enable SPI1 Not used
public int AuxSPI; // Auxiliary SPI if 1, not used
public int wire; // 0:not 3 wire, 1 is 3 wire
public int bits; // bits before MISO begins, valid wire is 3
public int firstBitTX; // 1, LSB sent first, 0 LSB first
public int firstBitRX; // 1, LSB received first, 0 LSB first
public int wSize; // 0 : 8 bit word. range 0-32

public spiApi_device_p(Pointer peer) {
super(peer);
}

public spiApi_device_p() {
super();
}

//ALIGN_NONE
public static class ByReference extends spiApi_device_p implements Structure.ByReference {

};

public static class ByValue extends spiApi_device_p implements Structure.ByValue {
};
}


/*
typedef struct {
uint32_t type;
#define RPI_HWVER_TYPE_UNKNOWN 0
#define RPI_HWVER_TYPE_PI1 1
#define RPI_HWVER_TYPE_PI2 2
#define RPI_HWVER_TYPE_PI4 3
uint32_t hwver;
uint32_t periph_base;
uint32_t videocore_base;
char *desc;
} rpi_hw_t;
*/





/*typedef struct spiApi_device
{
int driver_mode;
volatile uint8_t *pxl_raw;
volatile dma_t *dma;
volatile pwm_t *pwm;
volatile pcm_t *pcm;
int spi_fd;
volatile dma_cb_t *dma_cb;
uint32_t dma_cb_addr;
volatile gpio_t *gpio;
volatile cm_clk_t *cm_clk;
videocore_mbox_t mbox;
int max_count;
} spiApi_device_t; */


// pointer
@FieldOrder({"spi_mode","spi_FD"})
public static class spiApi_device_p_old extends Structure{
public int spi_mode;
public int spi_FD;
// public Pointer p;


/* public spiApi_device_p() {
super();
// handle fixed-size array fields correctly
ensureAllocated();
}*/
}





}
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package com.pi4j.library.kernel;

import com.pi4j.library.kernel.util.NativeLibraryLoader;
import com.pi4j.library.kernel.util.StringUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;

import static com.sun.jna.NativeLibrary.getInstance;


public class SPIapiImpl implements SPIapiIntrf {

SPIapiIntrf functionsNative = null;


public int x = 42;
public SPIapiImpl(String fileName) throws java.io.IOException {
}

public SPIapiImpl() {
}

static {
// Load the platform library
NativeLibraryLoader.load("libpi4j-kernel.so", "pi4j-kernel");
// System.setProperty("pi4j.library.path", "/lib/aarch64/pi4j-kernel");
System.setProperty("pi4j.library.path", "/home/pi/Pi4J_V2/pi4j-v2/libraries/pi4j-library-kernel/target/lib/aarch64/pi4j-kernel");

}


public static SPIapiImpl newInstance(String fileName, String libName) throws IOException {
SPIapiImpl impl = new SPIapiImpl();
impl.initialize(fileName, libName);
return(impl);
}

@Override
//SPIapi.spiApi_return_t
public int spiApi_render_test(SPIapi.spiApi_test spiApi) {
int ret = jna_rc.x0;
ret = this.functionsNative.spiApi_render_test(spiApi);

return ret;
}





@Override
public int spiApi_render(SPIapi.spiApi_t spiApi) {
return 0;
}

@Override
public int spiApi_init(SPIapi.spiApi_t spiApi) {
int ret = 0;
ret = this.functionsNative.spiApi_init(spiApi);

return ret;
}


public int spiApi_write(SPIapi.spiApi_t spiApi, byte[] b, int offset, int len, int speed){
// todo test len within buffer
Objects.checkFromIndexSize(offset, len, b.length);
return this.functionsNative.spiApi_write(spiApi, b, offset,len, speed);
}

@Override
public int spiApi_write_b(SPIapi.spiApi_t spiApi, byte b, int speed) {
return 0;
}

@Override
public int spiApi_read_b(SPIapi.spiApi_t spiApi, byte b, int speed) {
return 0;
}

@Override
public int spiApi_read(SPIapi.spiApi_t spiApi, byte[] buffer, int offset, int length, int speed) {
Objects.checkFromIndexSize(offset, length, buffer.length);
return 0;
}

@Override
public int transfer(SPIapi.spiApi_t spiApi, byte[] write, int writeOffset, byte[] read, int readOffset, int numberOfBytes, int speed) {
Objects.checkFromIndexSize(writeOffset, numberOfBytes, write.length);
Objects.checkFromIndexSize(readOffset, numberOfBytes, read.length);
return 0;
}

public void initialize(String fileName, String libName) throws java.io.IOException {
// the following allows my usage of the JNA code, rather than cloning my own version
Path workingDirectory=Paths.get(".").toAbsolutePath();
String classPath = System.getProperty("java.class.path");
System.out.println("properties : " + System.getProperties());

System.out.println("classpath" + classPath);

System.out.println("working DIR " + workingDirectory);
// get CPU architecture from system properties
String osArch = System.getProperty("os.arch").toLowerCase();

// sanitize CPU architecture string
switch (osArch) {
case "arm":
osArch = "armhf";
break;
case "arm64":
osArch = "aarch64";
break;
case "aarch64":
break;
default:
throw new IllegalStateException("Pi4J has detected and UNKNOWN/UNSUPPORTED 'os.arch' : [" +
osArch + "]; only 'arm|armhf' and 'arm64|aarch64' are supported.");
}
// String possibleJnaPath = workingDirectory+"/lib/" +osArch + "/pi4j-kernel/";
// String possibleJnaPath = "/lib/" + osArch + "/" + libName + "/" + fileName;
String possibleJnaPath = "/lib/" +osArch + "/pi4j-kernel/";
// Also, handle the user relying upon "pi4j.library.path" to locate so's
String pi4jLibpath = System.getProperty("pi4j.library.path");

if(StringUtil.isNotNullOrEmpty(pi4jLibpath, true)){
possibleJnaPath += ":"+pi4jLibpath;
System.out.println("libPath " + pi4jLibpath);
}
System.out.println(" possible JNA " + possibleJnaPath);
// TO_DO
// Determine the JNA path in the NativeLibrarylOADER
System.setProperty("jna.library.path", possibleJnaPath);
NativeLibraryLoader.load(fileName, libName);
NativeLibrary lib = NativeLibrary.getInstance(fileName); //SPIapiIntrf.getCLibName(LIBPROPFILE));
SPIapiIntrf INSTANCE = Native.load( SPIapiIntrf.class);
this.functionsNative = INSTANCE; //Native.loadLibrary(extractFile(fileName), String.valueOf(SPIapiIntrf.class));
}


static final int DEFAULT_BUFFER_SIZE = 8192;

private static void copyInputStreamToFile(InputStream inputStream, File file)
throws com.pi4j.io.exception.IOException, java.io.IOException {
// append = false
try (FileOutputStream outputStream = new FileOutputStream(file, false)) {
int read;
byte[] bytes = new byte[DEFAULT_BUFFER_SIZE];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.pi4j.library.kernel;

import com.sun.jna.Library;

import java.io.IOException;

public interface SPIapiIntrf extends Library {

static interface jna_rc{
public static final int x0 = 0;
public static final int x1 = 1;
public static final int x2 = 2;
public static final int x3 = 3;
public static final int x4 = 4;
public static final int x5 = 5;
public static final int x6 = 6;
public static final int x7 = 7;
public static final int x9 = 9;
public static final int x10 = 10;
public static final int x11 = 11;
public static final int x12 = 12;
public static final int x13 = 13;
public static final int x14 = 14;
public static final int xCount = 16;
}
int spiApi_render_test(SPIapi.spiApi_test spiApi); // TODO * SPIapi.spiApi_return_t




int spiApi_write(SPIapi.spiApi_t spiApi, byte[] b, int offset, int len, int speed);

public int spiApi_write_b(SPIapi.spiApi_t spiApi, byte b, int speed);

public int spiApi_read_b(SPIapi.spiApi_t spiApi, byte b, int speed) ;

public int spiApi_read(SPIapi.spiApi_t spiApi, byte[] buffer, int offset, int length, int speed) ;

public int transfer(SPIapi.spiApi_t spiApi, byte[] write, int writeOffset, byte[] read, int readOffset, int numberOfBytes, int speed);





int spiApi_init(SPIapi.spiApi_t spiApi);
int spiApi_render(SPIapi.spiApi_t spiApi); // TODO *


public static SPIapiImpl newNativeInstance(String soName, String libName) throws java.io.IOException {
SPIapiImpl rval = new SPIapiImpl(soName);
rval.initialize(soName, libName);
return(rval);
}

public static SPIapiImpl createSPIapiImpl() throws IOException {
return(SPIapiImpl.newInstance("libpi4j-kernel.so", "pi4j-kernel"));
}

}



Loading
Loading