Skip to content

Commit

Permalink
linuxfs used for SPI operations. .. Most this code base is from mpilo…
Browse files Browse the repository at this point in the history
…ne. I had no access to Mikes repo so I copied the files to ours. From Mikes initial work I made a few fixes for code he had not uniuti tested, and changes to config parms. Also parm bit shift direction and LSB first are not complete. Question whether we update the spi config. I do not want to use/allow the flags parm as it has bits no longe supported and would confuse things. I did read write and transfer testing for basic operation. I know there is a problem when read() is called mutiple times in succession. Al pushing this for discussion
  • Loading branch information
taartspi committed Dec 27, 2024
1 parent 32edebd commit 2c800f4
Show file tree
Hide file tree
Showing 9 changed files with 607 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ protected DefaultSpiConfig(Map<String,String> properties){
if(properties.containsKey(FLAGS_KEY)){
this.flags = StringUtil.parseLong(properties.get(FLAGS_KEY), null);
} else {
this.flags = 0L; // default flags (0)
this.flags = null; // set null, same as parseLong would
}

// define default property values if any are missing (based on the required address value)
Expand Down
5 changes: 5 additions & 0 deletions plugins/pi4j-plugin-linuxfs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
<artifactId>jsch</artifactId>
<version>${jsch.version}</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>${jna.version}</version>
</dependency>
<dependency>
<groupId>com.pi4j</groupId>
<artifactId>pi4j-library-linuxfs</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.pi4j.plugin.linuxfs.provider.gpio.digital.LinuxFsDigitalOutputProvider;
import com.pi4j.plugin.linuxfs.provider.pwm.LinuxFsPwmProvider;
import com.pi4j.plugin.linuxfs.internal.LinuxPwm;
import com.pi4j.plugin.linuxfs.provider.spi.LinuxFsSpiProvider;
import com.pi4j.provider.Provider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -100,14 +101,15 @@ public class LinuxFsPlugin implements Plugin {
public static final String I2C_PROVIDER_NAME = NAME + " I2C Provider";
public static final String I2C_PROVIDER_ID = ID + "-i2c";

// // SPI Provider name and unique ID
// public static final String SPI_PROVIDER_NAME = NAME + " SPI Provider";
// public static final String SPI_PROVIDER_ID = ID + "-spi";
//

// // Serial Provider name and unique ID
// public static final String SERIAL_PROVIDER_NAME = NAME + " Serial Provider";
// public static final String SERIAL_PROVIDER_ID = ID + "-serial";

// SPI Provider name and unique ID
public static final String SPI_PROVIDER_NAME = NAME + " SPI Provider";
public static final String SPI_PROVIDER_ID = ID + "-spi";

public static String DEFAULT_GPIO_FILESYSTEM_PATH = LinuxGpio.DEFAULT_SYSTEM_PATH;
public static String DEFAULT_PWM_FILESYSTEM_PATH = LinuxPwm.DEFAULT_SYSTEM_PATH;

Expand Down Expand Up @@ -155,7 +157,8 @@ public void initialize(PluginService service) {
LinuxFsDigitalInputProvider.newInstance(gpioFileSystemPath),
LinuxFsDigitalOutputProvider.newInstance(gpioFileSystemPath),
LinuxFsPwmProvider.newInstance(pwmFileSystemPath, pwmChip),
LinuxFsI2CProvider.newInstance()
LinuxFsI2CProvider.newInstance(),
LinuxFsSpiProvider.newInstance()
};

// register the LinuxFS I/O Providers with the plugin service
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* * Copyright (C) 2012 - 2024 Pi4J
* * %%
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* -
* #%L
* **********************************************************************
* ORGANIZATION : Pi4J
* PROJECT : Pi4J :: EXTENSION
* FILENAME : LinuxLibC.java
*
* This file is part of the Pi4J project. More information about
* this project can be found here: https://pi4j.com/
* **********************************************************************
* %%
*/

package com.pi4j.plugin.linuxfs.internal;

import com.sun.jna.Library;
import com.sun.jna.Native;

/**
* C library functions.
*
* @author mpilone
* @since 10/3/24.
*/
public interface LinuxLibC extends Library {

// This class could extend c.s.j.platform.linux.LibC, but we're not using any
// of that functionality right now so we can avoid the jna-platform dependency
// until we need it.

LinuxLibC INSTANCE = LinuxLibC.LibLoader.load();

class LibLoader {
static LinuxLibC load() {
return Native.load("c", LinuxLibC.class);
}
}

///////////////////////////////////
// fcntl.h
int O_WRONLY = 00000001;
int O_RDWR = 00000002;
int O_NONBLOCK = 00004000;

///////////////////////////////////
// ioctl.h
int _IOC_NRBITS = 8;
int _IOC_TYPEBITS = 8;
int _IOC_SIZEBITS = 14;

int _IOC_NRSHIFT = 0;
int _IOC_TYPESHIFT = (_IOC_NRSHIFT + _IOC_NRBITS);
int _IOC_SIZESHIFT = (_IOC_TYPESHIFT + _IOC_TYPEBITS);
int _IOC_DIRSHIFT = (_IOC_SIZESHIFT + _IOC_SIZEBITS);

byte _IOC_NONE = 0;
byte _IOC_WRITE = 1;
byte _IOC_READ = 2;

static int _IOC(byte dir, byte type, byte nr, int size) {
return (((dir) << _IOC_DIRSHIFT) |
((type) << _IOC_TYPESHIFT) |
((nr) << _IOC_NRSHIFT) |
((size) << _IOC_SIZESHIFT));
}

int ioctl(int filedes, long op, Object... args);

int open(String pathname, int flags);

int close(int fd);
}
Loading

0 comments on commit 2c800f4

Please sign in to comment.