|
30 | 30 | #include <stdlib.h> |
31 | 31 | #include <string.h> |
32 | 32 | #include <sys/ioctl.h> |
| 33 | +#include <sys/mman.h> |
33 | 34 | #include <sys/param.h> |
34 | 35 | #include <time.h> |
35 | 36 | #include <unistd.h> |
@@ -120,6 +121,143 @@ JNIEXPORT jint JNICALL Java_processing_io_NativeInterface_writeFile |
120 | 121 | } |
121 | 122 |
|
122 | 123 |
|
| 124 | +JNIEXPORT jint JNICALL Java_processing_io_NativeInterface_raspbianGpioMemRead |
| 125 | + (JNIEnv *env, jclass cls, jint offset) |
| 126 | +{ |
| 127 | + // validate offset |
| 128 | + if (4096 <= offset) { |
| 129 | + return -EINVAL; |
| 130 | + } |
| 131 | + |
| 132 | + int file = open("/dev/gpiomem", O_RDWR|O_SYNC); |
| 133 | + if (file < 0) { |
| 134 | + return -errno; |
| 135 | + } |
| 136 | + |
| 137 | + uint32_t *mem = mmap(NULL, 4096, PROT_READ, MAP_SHARED, file, 0); |
| 138 | + if (mem == MAP_FAILED) { |
| 139 | + close(file); |
| 140 | + return -errno; |
| 141 | + } |
| 142 | + |
| 143 | + uint32_t value = mem[offset]; |
| 144 | + |
| 145 | + munmap(mem, 4096); |
| 146 | + close(file); |
| 147 | + return value; |
| 148 | +} |
| 149 | + |
| 150 | + |
| 151 | +JNIEXPORT jint JNICALL Java_processing_io_NativeInterface_raspbianGpioMemWrite |
| 152 | + (JNIEnv *env, jclass cls, jint offset, jint mask, jint value) |
| 153 | +{ |
| 154 | + // validate offset |
| 155 | + if (4096 <= offset) { |
| 156 | + return -EINVAL; |
| 157 | + } |
| 158 | + |
| 159 | + int file = open("/dev/gpiomem", O_RDWR|O_SYNC); |
| 160 | + if (file < 0) { |
| 161 | + return -errno; |
| 162 | + } |
| 163 | + |
| 164 | + uint32_t *mem = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, file, 0); |
| 165 | + if (mem == MAP_FAILED) { |
| 166 | + close(file); |
| 167 | + return -errno; |
| 168 | + } |
| 169 | + |
| 170 | + mem[offset] = (mem[offset] & ~mask) | (value & mask); |
| 171 | + |
| 172 | + munmap(mem, 4096); |
| 173 | + close(file); |
| 174 | + return 1; // number of bytes written |
| 175 | +} |
| 176 | + |
| 177 | + |
| 178 | +#define BCM2835_GPPUD_OFFSET (0x94 >> 2) |
| 179 | +#define BCM2835_GPPUDCLK0_OFFSET (0x98 >> 2) |
| 180 | +#define BCM2835_GPPUDCLK1_OFFSET (0x9c >> 2) |
| 181 | + |
| 182 | +JNIEXPORT jint JNICALL Java_processing_io_NativeInterface_raspbianGpioMemSetPinBias |
| 183 | + (JNIEnv *env, jclass cls, jint gpio, jint mode) |
| 184 | +{ |
| 185 | + int ret = 0; // success |
| 186 | + |
| 187 | + int file = open("/dev/gpiomem", O_RDWR|O_SYNC); |
| 188 | + if (file < 0) { |
| 189 | + return -errno; |
| 190 | + } |
| 191 | + |
| 192 | + uint32_t *mem = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, file, 0); |
| 193 | + if (mem == MAP_FAILED) { |
| 194 | + close(file); |
| 195 | + return -errno; |
| 196 | + } |
| 197 | + |
| 198 | + // validate arguments |
| 199 | + if (gpio < 0 || 53 < gpio) { |
| 200 | + ret = -EINVAL; |
| 201 | + goto out; |
| 202 | + } |
| 203 | + |
| 204 | + // see BCM2835 datasheet, p. 101 |
| 205 | + uint32_t pud; |
| 206 | + if (mode == 0) { |
| 207 | + pud = 0; // floating |
| 208 | + } else if (mode == 2) { |
| 209 | + pud = 2; // pull-up |
| 210 | + } else if (mode == 3) { |
| 211 | + pud = 1; // pull-down |
| 212 | + } else { |
| 213 | + ret = -EINVAL; |
| 214 | + goto out; |
| 215 | + } |
| 216 | + |
| 217 | + /* |
| 218 | + * From the BCM2835 datasheet, p. 101: |
| 219 | + * |
| 220 | + * The following sequence of events is required: |
| 221 | + * 1. Write to GPPUD to set the required control signal (i.e. Pull-up or |
| 222 | + * Pull-Down or neither to remove the current Pull-up/down) |
| 223 | + * 2. Wait 150 cycles – this provides the required set-up time for the |
| 224 | + * control signal |
| 225 | + * 3. Write to GPPUDCLK0/1 to clock the control signal into the GPIO pads |
| 226 | + * you wish to modify – NOTE only the pads which receive a clock will |
| 227 | + * be modified, all others will retain their previous state. |
| 228 | + * 4. Wait 150 cycles – this provides the required hold time for the |
| 229 | + * control signal |
| 230 | + * 5. Write to GPPUD to remove the control signal |
| 231 | + * 6. Write to GPPUDCLK0/1 to remove the clock |
| 232 | + */ |
| 233 | + |
| 234 | + // python-gpiozero uses a delay of 214 ns, so we do the same |
| 235 | + struct timespec wait; |
| 236 | + wait.tv_sec = 0; |
| 237 | + wait.tv_nsec = 214; |
| 238 | + |
| 239 | + mem[BCM2835_GPPUD_OFFSET] = pud; |
| 240 | + nanosleep(&wait, NULL); |
| 241 | + if (gpio < 32) { |
| 242 | + mem[BCM2835_GPPUDCLK0_OFFSET] = 1 << gpio; |
| 243 | + } else { |
| 244 | + mem[BCM2835_GPPUDCLK1_OFFSET] = 1 << (gpio-32); |
| 245 | + } |
| 246 | + nanosleep(&wait, NULL); |
| 247 | + mem[BCM2835_GPPUD_OFFSET] = 0; |
| 248 | + if (gpio < 32) { |
| 249 | + mem[BCM2835_GPPUDCLK0_OFFSET] = 0; |
| 250 | + } else { |
| 251 | + mem[BCM2835_GPPUDCLK1_OFFSET] = 0; |
| 252 | + } |
| 253 | + |
| 254 | +out: |
| 255 | + munmap(mem, 4096); |
| 256 | + close(file); |
| 257 | + return ret; |
| 258 | +} |
| 259 | + |
| 260 | + |
123 | 261 | JNIEXPORT jint JNICALL Java_processing_io_NativeInterface_pollDevice |
124 | 262 | (JNIEnv *env, jclass cls, jstring _fn, jint timeout) |
125 | 263 | { |
|
0 commit comments