Change SBC to Jetson Orin

This commit is contained in:
2025-02-25 07:35:38 +07:00
parent 21e8ab1e82
commit ce5c8d5946
16 changed files with 1057 additions and 80 deletions

View File

@@ -0,0 +1,38 @@
package SBC;
import lombok.Getter;
public class I2C_BUS {
private final Linux_C_lib C = Linux_C_lib.INSTANCE;
private @Getter boolean opened = false;
private final @Getter String i2c_name;
private @Getter int i2c_handle = -1;
public I2C_BUS(int bus_number){
this.i2c_name = "/dev/i2c-"+bus_number;
}
public boolean Open_Bus(){
if (!opened){
final int O_RDWR = 0x00000002;
i2c_handle = C.open(i2c_name, O_RDWR);
if (i2c_handle>=0){
opened = true;
return true;
}
}
return false;
}
public void Close_Bus(){
if (opened){
if (i2c_handle>=0){
C.close(i2c_handle);
i2c_handle = -1;
}
opened = false;
}
}
}

View File

@@ -0,0 +1,147 @@
package SBC;
import lombok.Getter;
@SuppressWarnings("unused")
public class I2C_Device {
Linux_C_lib C = Linux_C_lib.INSTANCE;
private @Getter int bus_handle;
private @Getter int device_address;
private @Getter int dev_handle;
private @Getter boolean opened = false;
/**
* Create I2C Device
* @param bus_handle I2C Bus Handle, from opening I2C_Bus
* @param device_address I2C Device Address
*/
public I2C_Device(int bus_handle, int device_address){
opened = false;
if (bus_handle<0 || device_address<0) return;
this.bus_handle = bus_handle;
this.device_address = device_address;
}
/**
* Open I2C Device in Slave mode
* @return true if successfully opened
*/
public boolean Open_Slave(){
boolean sucess = false;
dev_handle = C.ioctl(bus_handle, I2C_Flags.I2C_SLAVE, device_address);
if (dev_handle>=0){
sucess = true;
}
opened = sucess;
return opened;
}
/**
* Open I2C Device in Slave mode with Force
* @return true if successfully opened
*/
public boolean Open_SlaveForce(){
boolean sucess = false;
dev_handle = C.ioctl(bus_handle, I2C_Flags.I2C_SLAVE_FORCE, device_address);
if (dev_handle>=0){
C.ioctl(bus_handle,I2C_Flags.I2C_RETRIES, 3);
C.ioctl(bus_handle,I2C_Flags.I2C_TIMEOUT, 1000);
sucess = true;
}
opened = sucess;
return opened;
}
/**
* Open I2C Device in Read Write mode
* @return true if successfully opened
*/
public boolean Open_RDWR(){
boolean sucess = false;
dev_handle = C.ioctl(bus_handle, I2C_Flags.I2C_RDWR, device_address);
if (dev_handle>=0){
sucess = true;
}
opened = sucess;
return opened;
}
/**
* Open I2C Device in SMBus mode
* @return true if successfully opened
*/
public boolean Open_SMBus(){
boolean sucess = false;
dev_handle = C.ioctl(bus_handle, I2C_Flags.I2C_SMBUS, device_address);
if (dev_handle>=0){
sucess = true;
}
opened = sucess;
return opened;
}
/**
* Close I2C Device
*/
public void Close(){
C.close(dev_handle);
opened = false;
dev_handle = -1;
}
/**
* Write Bytes to I2C Device
* @param data bytes to write
* @return number of bytes written
*/
public int WriteBytes(byte[] data){
if (bus_handle<0 || dev_handle<0 || device_address<0) return -1;
return C.write(bus_handle, data, data.length);
}
/**
* Write Byte to I2C Device
* @param register_address register address
* @param data byte to write
* @return number of bytes written
*/
public int WriteBytes(int register_address, byte data){
byte[] cmd = new byte[2];
cmd[0] = (byte) register_address;
cmd[1] = data;
return WriteBytes(cmd);
}
/**
* Write Bytes to I2C Device
* @param register_address register address
* @param data bytes to write
* @return number of bytes written
*/
public int WriteBytes(int register_address, byte[] data){
byte[] cmd = new byte[data.length+1];
cmd[0] = (byte) register_address;
System.arraycopy(data, 0, cmd, 1, data.length);
return WriteBytes(cmd);
}
/**
* Read Bytes from I2C Device
* @param readsize number of bytes to read
* @return bytes read
*/
public byte[] ReadBytes(int readsize){
if (bus_handle<0 || dev_handle<0 || device_address<0) return null;
byte[] data = new byte[readsize];
int read = C.read(bus_handle, data, readsize);
if (read<0) return null;
if (read<readsize){
byte[] temp = new byte[read];
System.arraycopy(data, 0, temp, 0, read);
return temp;
}
return data;
}
}

View File

@@ -0,0 +1,13 @@
package SBC;
public class I2C_Flags {
public final static int I2C_RETRIES = 0x701; /* number of times a device address should be polled when not acknowledging */
public final static int I2C_TIMEOUT = 0x702; /* set timeout in units of 10 ms */
public final static int I2C_SLAVE = 0x703; /* Command at ioctl, means : Use this slave address */
public final static int I2C_TENBIT = 0x704; /* 0 for 7 bit addrs, != 0 for 10 bit */
public final static int I2C_FUNCS = 0x705; /* Command at ioctl, means : Get the adapter functionality */
public final static int I2C_SLAVE_FORCE = 0x706; /* Command at ioctl, means : Use this slave address, even if it is already in use by a driver! */
public final static int I2C_RDWR = 0x707; /* Command at ioctl, means : Combined R/W transfer (one stop only) */
public final static int I2C_PEC = 0x708; /* != 0 to use PEC with SMBus */
public final static int I2C_SMBUS = 0x720; /* SMBus transfer */
}

View File

@@ -0,0 +1,69 @@
package SBC;
import java.util.Arrays;
import java.util.List;
import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
public interface Linux_C_lib extends com.sun.jna.Library {
Linux_C_lib INSTANCE = (Linux_C_lib) Native.load("c", Linux_C_lib.class);
long memcpy(int[] dst, short[] src, long n);
int memcpy(int[] dst, short[] src, int n);
int pipe(int[] fds);
int tcdrain(int fd);
int fcntl(int bus_handle, int command, int args);
int ioctl(int bus_handle, int command, int args);
int ioctl(int bus_handle, int command, int... args);
int ioctl(int bus_handle, int command, Pointer args);
int open(String path, int flags);
int close(int fd);
int write(int bus_handle, byte[] buffer, int count);
int read(int bus_handle, byte[] buffer, int count);
long write(int bus_handle, byte[] buffer, long count);
long read(int bus_handle, byte[] buffer, long count);
int select(int n, int[] read, int[] write, int[] error, timeval timeout);
int poll(int[] fds, int nfds, int timeout);
int tcflush(int fd, int qs);
void perror(String msg);
int tcsendbreak(int fd, int duration);
public class timeval extends Structure {
public NativeLong tv_sec;
public NativeLong tv_usec;
protected List<String> getFieldOrder() {
return Arrays.asList(//
"tv_sec",//
"tv_usec"//
);
}
public timeval(long second, long usecond) {
tv_sec = new NativeLong(second);
tv_usec = new NativeLong(usecond);
}
}
}

View File

@@ -0,0 +1,159 @@
package SBC;
import lombok.Getter;
@SuppressWarnings("unused")
public class PCF8574 extends I2C_Device{
private byte data = 0x00;
private final @Getter String[] pinNames = new String[8];
/**
* Create new PCF8574
* @param bus_number I2C Bus Number
* @param address PCF8574 Address
*/
public PCF8574(int bus_number, int address){
super(bus_number, address);
for(int i=0; i<8; i++){
pinNames[i] = i+"";
}
}
/**
* Set new Pin Name
* @param pin pin number, 0-7
* @param name new name
*/
public void setPinName(int pin, String name){
if (pin<0 || pin>7) return;
pinNames[pin] = name;
}
/**
* Get Pin Name
* @param pin pin number, 0-7
* @return pin name, or null if not available
*/
public String getPinName(int pin){
if (pin<0 || pin>7) return null;
return pinNames[pin];
}
/**
* Set All Pins High
* @return true if successful
*/
public boolean AllOn(){
if (super.isOpened()){
if (super.WriteBytes(new byte[]{(byte)0xFF})>0){
data = (byte)0xFF;
return true;
}
}
return false;
}
/**
* Set all Pins Low
* @return true if successful
*/
public boolean AllOff(){
if (super.isOpened()){
if (super.WriteBytes(new byte[]{(byte)0x00})>0){
data = (byte)0x00;
return true;
}
}
return false;
}
/**
* Set Pin by Pin Name
* @param pinName pin name
* @return true if successful
*/
public boolean SetPin(String pinName){
for(int i=0; i<8; i++){
if (pinNames[i].equals(pinName)){
return SetPin(i);
}
}
return false;
}
/**
* Set Pin High
* @param pin pin number 0-7
* @return true if successful
*/
public boolean SetPin(int pin){
if (pin<0 || pin>7) return false;
if (super.isOpened()){
byte temp = (byte) (data | (1<<pin));
return super.WriteBytes(new byte[]{temp})>0;
}
return false;
}
/**
* Clear Pin by Pin Name
* @param pinName pin name
* @return true if successful
*/
public boolean ClearPin(String pinName){
for(int i=0; i<8; i++){
if (pinNames[i].equals(pinName)){
return ClearPin(i);
}
}
return false;
}
/**
* Set Pin Low
* @param pin pin number 0-7
* @return true if successful
*/
public boolean ClearPin(int pin){
if (pin<0 || pin>7) return false;
if (super.isOpened()){
byte temp = (byte) (data & ~(1<<pin));
return super.WriteBytes(new byte[]{temp})>0;
}
return false;
}
/**
* Read Pin by Pin Name
* @param pinName pin name
* @return 1 if high, 0 if low, -1 if error
*/
public int ReadPin(String pinName){
for(int i=0; i<8; i++){
if (pinNames[i].equals(pinName)){
return ReadPin(i);
}
}
return -1;
}
/**
* Read Pin
* @param pin pin to read, 0 - 7
* @return 1 if high, 0 if low, -1 if error
*/
public int ReadPin(int pin){
if (pin<0 || pin>7) return -1;
if (super.isOpened()){
byte[] buffer = super.ReadBytes(1);
byte masker = (byte) (1<<pin);
return (buffer[0] & masker) > 0 ? 1 : 0;
}
return -1;
}
}