Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
32a8fbf
Serial: Add support for opening Raspbian-style named ports
gohai Mar 16, 2018
01ccaf1
Increase the maximum number of lights to 4 for Mesa vc4
gohai Mar 16, 2018
bf4e7a3
ARM Mali: Implement glRenderbufferStorageMultisample for gl3es3
gohai Apr 18, 2018
fada294
ARM Mali: Implement glReadBuffer for gl3es3
gohai Apr 18, 2018
e151628
ARM Mali: Report "es" as part of GLSL versions
gohai Apr 18, 2018
0965370
ARM Mali: Specify precision for _fragColor
gohai Apr 18, 2018
e5ba6a9
ARM Mali: Implement glBlitFramebuffer for gl3es3
gohai Apr 18, 2018
d96aa3a
ARM Mali: Implement gDrawBuffer for gl3es3
gohai Apr 18, 2018
f73f460
ARM Mali: Don't assume anisotropic filtering for gl3es3
gohai Apr 18, 2018
a476177
update to Java 8u172
benfry Apr 23, 2018
ec47beb
IO: Implement Raspbian-only method for setting pull{up,down} resistors
gohai Jun 16, 2018
7cde730
IO: Clarify SimpleInput example
gohai Jun 27, 2018
f7e530a
IO: Speed up GPIO.pinMode()
gohai Jun 25, 2018
dc11272
ARM Mali: Don't use GL_MULTISAMPLE or GL_POLYGON_SMOOTH on ES
gohai Apr 23, 2018
5518417
Unconditionally disable POLYGON_SMOOTH in restoreGL()
gohai Apr 24, 2018
89c3ffa
Enable exporting of Windows applications on ARM
gohai Apr 24, 2018
3b88eeb
IO: Fix crash when I2C.read() was called without prior write()
gohai Jun 28, 2018
657ce85
IO: Add a 100ms timeout to I2C to allow communication with Arduino
gohai Jun 28, 2018
ed6d36d
IO: Reorganize OOP examples
gohai Jun 29, 2018
4e30acd
IO: Add MPR121 capacitive touch example
gohai Jun 29, 2018
afe8609
IO: Add BME280 environmental sensor example
Jun 30, 2018
aea3f99
IO: Add TSL2561 environmental sensor example
Jun 30, 2018
961505d
IO: Add PCA9685 servo/PWM controller example
Jun 30, 2018
841f942
IO: Remove the duplicate "duty" argument from the reference for PWM.s…
gohai Jul 1, 2018
ab1b958
IO: Rename ServoSweep example
gohai Jul 1, 2018
2e1eccd
IO: Make I2C errors more verbose
gohai Jul 10, 2018
c1d9528
IO: Fix pinMode() retry logic
gohai Jul 11, 2018
05d67ba
IO: Add a dispose method to PCA9685 example, document pulse widths
gohai Jul 12, 2018
780cc16
OpenGL ES: Fix GLSL version number for 1.00 (#5582)
gohai Jul 20, 2018
8efa774
Update Java to 8u181
gohai Jul 22, 2018
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
IO: Implement Raspbian-only method for setting pull{up,down} resistors
Aarch64 version compiled courtesy of @xranby
  • Loading branch information
gohai committed Jul 1, 2018
commit ec47beb2b685c48fd5ed354b9e666d6f0579845d
5 changes: 4 additions & 1 deletion java/libraries/io/examples/SimpleInput/SimpleInput.pde
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import processing.io.*;
// see setup.png in the sketch folder for wiring details

void setup() {
GPIO.pinMode(4, GPIO.INPUT);
// INPUT_PULLUP enables the built-in pull-up resistor for this pin
// left alone, the pin will read as HIGH
// connected to ground (via e.g. a button or switch) it will read LOW
GPIO.pinMode(4, GPIO.INPUT_PULLUP);
}

void draw() {
Expand Down
Binary file modified java/libraries/io/library/linux-arm64/libprocessing-io.so
Binary file not shown.
Binary file modified java/libraries/io/library/linux-armv6hf/libprocessing-io.so
Binary file not shown.
Binary file modified java/libraries/io/library/linux32/libprocessing-io.so
Binary file not shown.
Binary file modified java/libraries/io/library/linux64/libprocessing-io.so
Binary file not shown.
24 changes: 24 additions & 0 deletions java/libraries/io/src/native/iface.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

138 changes: 138 additions & 0 deletions java/libraries/io/src/native/impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/param.h>
#include <time.h>
#include <unistd.h>
Expand Down Expand Up @@ -120,6 +121,143 @@ JNIEXPORT jint JNICALL Java_processing_io_NativeInterface_writeFile
}


JNIEXPORT jint JNICALL Java_processing_io_NativeInterface_raspbianGpioMemRead
(JNIEnv *env, jclass cls, jint offset)
{
// validate offset
if (4096 <= offset) {
return -EINVAL;
}

int file = open("/dev/gpiomem", O_RDWR|O_SYNC);
if (file < 0) {
return -errno;
}

uint32_t *mem = mmap(NULL, 4096, PROT_READ, MAP_SHARED, file, 0);
if (mem == MAP_FAILED) {
close(file);
return -errno;
}

uint32_t value = mem[offset];

munmap(mem, 4096);
close(file);
return value;
}


JNIEXPORT jint JNICALL Java_processing_io_NativeInterface_raspbianGpioMemWrite
(JNIEnv *env, jclass cls, jint offset, jint mask, jint value)
{
// validate offset
if (4096 <= offset) {
return -EINVAL;
}

int file = open("/dev/gpiomem", O_RDWR|O_SYNC);
if (file < 0) {
return -errno;
}

uint32_t *mem = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, file, 0);
if (mem == MAP_FAILED) {
close(file);
return -errno;
}

mem[offset] = (mem[offset] & ~mask) | (value & mask);

munmap(mem, 4096);
close(file);
return 1; // number of bytes written
}


#define BCM2835_GPPUD_OFFSET (0x94 >> 2)
#define BCM2835_GPPUDCLK0_OFFSET (0x98 >> 2)
#define BCM2835_GPPUDCLK1_OFFSET (0x9c >> 2)

JNIEXPORT jint JNICALL Java_processing_io_NativeInterface_raspbianGpioMemSetPinBias
(JNIEnv *env, jclass cls, jint gpio, jint mode)
{
int ret = 0; // success

int file = open("/dev/gpiomem", O_RDWR|O_SYNC);
if (file < 0) {
return -errno;
}

uint32_t *mem = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, file, 0);
if (mem == MAP_FAILED) {
close(file);
return -errno;
}

// validate arguments
if (gpio < 0 || 53 < gpio) {
ret = -EINVAL;
goto out;
}

// see BCM2835 datasheet, p. 101
uint32_t pud;
if (mode == 0) {
pud = 0; // floating
} else if (mode == 2) {
pud = 2; // pull-up
} else if (mode == 3) {
pud = 1; // pull-down
} else {
ret = -EINVAL;
goto out;
}

/*
* From the BCM2835 datasheet, p. 101:
*
* The following sequence of events is required:
* 1. Write to GPPUD to set the required control signal (i.e. Pull-up or
* Pull-Down or neither to remove the current Pull-up/down)
* 2. Wait 150 cycles – this provides the required set-up time for the
* control signal
* 3. Write to GPPUDCLK0/1 to clock the control signal into the GPIO pads
* you wish to modify – NOTE only the pads which receive a clock will
* be modified, all others will retain their previous state.
* 4. Wait 150 cycles – this provides the required hold time for the
* control signal
* 5. Write to GPPUD to remove the control signal
* 6. Write to GPPUDCLK0/1 to remove the clock
*/

// python-gpiozero uses a delay of 214 ns, so we do the same
struct timespec wait;
wait.tv_sec = 0;
wait.tv_nsec = 214;

mem[BCM2835_GPPUD_OFFSET] = pud;
nanosleep(&wait, NULL);
if (gpio < 32) {
mem[BCM2835_GPPUDCLK0_OFFSET] = 1 << gpio;
} else {
mem[BCM2835_GPPUDCLK1_OFFSET] = 1 << (gpio-32);
}
nanosleep(&wait, NULL);
mem[BCM2835_GPPUD_OFFSET] = 0;
if (gpio < 32) {
mem[BCM2835_GPPUDCLK0_OFFSET] = 0;
} else {
mem[BCM2835_GPPUDCLK1_OFFSET] = 0;
}

out:
munmap(mem, 4096);
close(file);
return ret;
}


JNIEXPORT jint JNICALL Java_processing_io_NativeInterface_pollDevice
(JNIEnv *env, jclass cls, jstring _fn, jint timeout)
{
Expand Down
17 changes: 15 additions & 2 deletions java/libraries/io/src/processing/io/GPIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public static void noInterrupts() {
/**
* Configures a pin to act either as input or output
* @param pin GPIO pin
* @param mode GPIO.INPUT or GPIO.OUTPUT
* @param mode GPIO.INPUT, GPIO.INPUT_PULLUP, GPIO.INPUT_PULLDOWN, or GPIO.OUTPUT
* @see digitalRead
* @see digitalWrite
* @see releasePin
Expand Down Expand Up @@ -369,16 +369,29 @@ public static void pinMode(int pin, int mode) {
String out;
if (mode == INPUT) {
out = "in";

// attempt to disable any pre-set pullups on the Raspberry Pi
NativeInterface.raspbianGpioMemSetPinBias(pin, mode);

} else if (mode == OUTPUT) {
if (values.get(pin)) {
out = "high";
} else {
out = "low";
}
} else if (mode == INPUT_PULLUP || mode == INPUT_PULLDOWN) {
out = "in";

// attempt to set pullups on the Raspberry Pi
ret = NativeInterface.raspbianGpioMemSetPinBias(pin, mode);
if (ret == -2) { // NOENT
System.err.println("Setting pullup or pulldown resistors is currently only supported on the Raspberry Pi running Raspbian. Continuing without.");
} else if (ret < 0) {
System.err.println("Error setting pullup or pulldown resistors: " + NativeInterface.getError(ret) + ". Continuing without.");
}
// currently this can't be done in a non-platform-specific way, see
// http://lists.infradead.org/pipermail/linux-rpi-kernel/2015-August/002146.html
throw new RuntimeException("Not yet implemented");

} else {
throw new IllegalArgumentException("Unknown mode");
}
Expand Down
3 changes: 3 additions & 0 deletions java/libraries/io/src/processing/io/NativeInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public static int writeFile(String fn, String out) {
}

/* GPIO */
public static native int raspbianGpioMemRead(int offset);
public static native int raspbianGpioMemWrite(int offset, int mask, int value);
public static native int raspbianGpioMemSetPinBias(int gpio, int mode);
public static native int pollDevice(String fn, int timeout);
/* I2C */
public static native int transferI2c(int handle, int slave, byte[] out, byte[] in);
Expand Down