001package com.pi4j.wiringpi; 002 003/* 004 * #%L 005 * ********************************************************************** 006 * ORGANIZATION : Pi4J 007 * PROJECT : Pi4J :: Java Library (Core) 008 * FILENAME : Spi.java 009 * 010 * This file is part of the Pi4J project. More information about 011 * this project can be found here: http://www.pi4j.com/ 012 * ********************************************************************** 013 * %% 014 * Copyright (C) 2012 - 2015 Pi4J 015 * %% 016 * This program is free software: you can redistribute it and/or modify 017 * it under the terms of the GNU Lesser General Public License as 018 * published by the Free Software Foundation, either version 3 of the 019 * License, or (at your option) any later version. 020 * 021 * This program is distributed in the hope that it will be useful, 022 * but WITHOUT ANY WARRANTY; without even the implied warranty of 023 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 024 * GNU General Lesser Public License for more details. 025 * 026 * You should have received a copy of the GNU General Lesser Public 027 * License along with this program. If not, see 028 * <http://www.gnu.org/licenses/lgpl-3.0.html>. 029 * #L% 030 */ 031 032 033import com.pi4j.util.NativeLibraryLoader; 034 035/** 036 * <p> 037 * WiringPi includes a library which can make it easier to use the Raspberry Pi’s on-board SPI interface. 038 * </p> 039 * 040 * <p> 041 * Before you can use SPI interface, you may need to use the gpio utility to load the SPI drivers into the kernel: 042 * > gpio load spi 043 * 044 * If you need a buffer size of greater than 4KB, then you can specify the size (in KB) on the command line: 045 * > gpio load spi 100 046 * 047 * will allocate a 100KB buffer. (You should rarely need this though, the default is more than enough 048 * for most applications). 049 * </p> 050 * 051 * <p> 052 * <blockquote> This library depends on the wiringPi native system library.</br> (developed by 053 * Gordon Henderson @ <a href="http://wiringpi.com/">http://wiringpi.com</a>) 054 * </blockquote> 055 * </p> 056 * 057 * @see <a href="http://www.pi4j.com/">http://www.pi4j.com</a> 058 * @see <a 059 * href="http://wiringpi.com/reference/spi-library/">http://wiringpi.com/reference/spi-library</a> 060 * @author Robert Savage (<a 061 * href="http://www.savagehomeautomation.com">http://www.savagehomeautomation.com</a>) 062 */ 063public class Spi { 064 065 public static int CHANNEL_0 = 0; 066 public static int CHANNEL_1 = 1; 067 068 public static int MODE_0 = 0; 069 public static int MODE_1 = 1; 070 public static int MODE_2 = 2; 071 public static int MODE_3 = 3; 072 073 // private constructor 074 private Spi() { 075 // forbid object construction 076 } 077 078 static { 079 // Load the platform library 080 NativeLibraryLoader.load("libpi4j.so"); 081 } 082 083 /** 084 * <p>wiringPiSPISetup:</p> 085 * 086 * <p> 087 * This is the way to initialise a channel (The Pi has 2 channels; 0 and 1). The speed parameter is an integer in 088 * the range 500,000 through 32,000,000 and represents the SPI clock speed in Hz. 089 * </p> 090 * 091 * <p> 092 * The returned value is the Linux file-descriptor for the device, or -1 on error. If an error has happened, you 093 * may use the standard errno global variable to see why. 094 * </p> 095 * 096 * @see <a 097 * href="http://wiringpi.com/reference/spi-library/">http://wiringpi.com/reference/spi-library</a> 098 * @param channel SPI channel 099 * @param speed SPI speed 100 * @return return -1 on error 101 */ 102 public static native int wiringPiSPISetup(int channel, int speed); 103 104 105 /** 106 * <p>wiringPiSPISetupMode:</p> 107 * 108 * <p> 109 * This is the way to initialise a channel (The Pi has 2 channels; 0 and 1). The speed parameter is an integer in 110 * the range 500,000 through 32,000,000 and represents the SPI clock speed in Hz. 111 * </p> 112 * 113 * <p> 114 * The returned value is the Linux file-descriptor for the device, or -1 on error. If an error has happened, you 115 * may use the standard errno global variable to see why. 116 * </p> 117 * 118 * @see <a 119 * href="http://wiringpi.com/reference/spi-library/">http://wiringpi.com/reference/spi-library</a> 120 * @see <a 121 * hfref="http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus#Mode_numbers">http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus#Mode_numbers</a> 122 * @param channel SPI channel 123 * @param speed SPI speed 124 * @param mode SPI mode (Mode is 0, 1, 2 or 3; see http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus#Mode_numbers) 125 * @return return -1 on error 126 */ 127 public static native int wiringPiSPISetupMode (int channel, int speed, int mode); 128 129 /** 130 * <p>wiringPiSPIGetFd:</p> 131 * 132 * <p> 133 * Return the file-descriptor for the given channel 134 * </p> 135 * 136 * @see <a 137 * href="http://wiringpi.com/reference/spi-library/">http://wiringpi.com/reference/spi-library</a> 138 * @param channel SPI channel 139 * @return file-descriptor for the given channel 140 */ 141 public static native int wiringPiSPIGetFd(int channel); 142 143 /** 144 * <p>wiringPiSPIDataRW:</p> 145 * 146 * <p> 147 * This performs a simultaneous write/read transaction over the selected SPI bus. Data that was in your buffer is 148 * overwritten by data returned from the SPI bus. 149 * </p> 150 * 151 * <p> 152 * (ATTENTION: the 'data' argument can only be a maximum of 1024 characters.) 153 * </p> 154 * 155 * @see <a 156 * href="http://wiringpi.com/reference/spi-library/">http://wiringpi.com/reference/spi-library</a> 157 * @param channel SPI channel</p> 158 * @param data string data payload 159 * @param len length of characters in string 160 * @return return -1 on error 161 */ 162 public static native int wiringPiSPIDataRW(int channel, String data, int len); 163 164 /** 165 * <p>wiringPiSPIDataRW:</p> 166 * 167 * <p> 168 * This performs a simultaneous write/read transaction over the selected SPI bus. Data that was in your buffer is 169 * overwritten by data returned from the SPI bus. 170 * </p> 171 * 172 * <p> 173 * (ATTENTION: the 'data' argument can only be a maximum of 1024 characters.) 174 * </p> 175 * 176 * @see <a 177 * href="http://wiringpi.com/reference/spi-library/">http://wiringpi.com/reference/spi-library</a> 178 * @param channel 179 * SPI channel 180 * @param data 181 * byte array data payload 182 * @param len 183 * length of bytes in data array argument 184 * @return return -1 on error 185 */ 186 public static native int wiringPiSPIDataRW(int channel, byte[] data, int len); 187 188 189 /** 190 * <p>wiringPiSPIDataRW:</p> 191 * 192 * <p> 193 * This performs a simultaneous write/read transaction over the selected SPI bus. Data that was in your buffer is 194 * overwritten by data returned from the SPI bus. 195 * </p> 196 * 197 * <p> 198 * (ATTENTION: the 'data' argument can only be a maximum of 1024 characters.) 199 * </p> 200 * 201 * @see <a 202 * href="http://wiringpi.com/reference/spi-library/">http://wiringpi.com/reference/spi-library</a> 203 * @param channel 204 * SPI channel 205 * @param data 206 * byte array data payload 207 * @return return -1 on error 208 */ 209 public static int wiringPiSPIDataRW(int channel, byte[] data){ 210 return wiringPiSPIDataRW(channel, data, data.length); 211 } 212 213 /** 214 * <p> 215 * wiringPiSPIDataRW: 216 * </p> 217 * 218 * <p> 219 * This performs a simultaneous write/read transaction over the selected SPI bus. The data argument is passed into the wiringPI function as the argument and the output from Spi is returned by this 220 * method 221 * </p> 222 * 223 * @see <a href="http://wiringpi.com/reference/spi-library/">http://wiringpi.com/reference/spi-library</a> 224 * @param channel 225 * SPI channel 226 * @param data 227 * short array data payload. Note that wiringPi uses unsigned char for the data transmission. That is 8-bit. in other words values 0-255. So make sure the values in data do not exceed 228 * this range, otherwise the numbers would overflow in the native code and unexpected results would yield 229 * @param len 230 * length of bytes in data array argument 231 * @return return -1 on error 232 */ 233 public static native int wiringPiSPIDataRW(int channel, short[] data, int len); 234 235 /** 236 * <p> 237 * wiringPiSPIDataRW: 238 * </p> 239 * 240 * <p> 241 * This performs a simultaneous write/read transaction over the selected SPI bus. The data argument is passed into the wiringPI function as the argument and the output from Spi is returned by this 242 * method 243 * </p> 244 * 245 * @see <a href="http://wiringpi.com/reference/spi-library/">http://wiringpi.com/reference/spi-library</a> 246 * @param channel 247 * SPI channel 248 * @param data 249 * short array data payload. Note that wiringPi uses unsigned char for the data transmission. That is 8-bit. in other words values 0-255. So make sure the values in data do not exceed 250 * this range, otherwise the numbers would overflow in the native code and unexpected results would yield 251 * @return return -1 on error 252 */ 253 public static int wiringPiSPIDataRW(int channel, short[] data){ 254 return wiringPiSPIDataRW(channel, data, data.length); 255 } 256}