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 - 2013 Pi4J
015 * %%
016 * Licensed under the Apache License, Version 2.0 (the "License");
017 * you may not use this file except in compliance with the License.
018 * You may obtain a copy of the License at
019 * 
020 *      http://www.apache.org/licenses/LICENSE-2.0
021 * 
022 * Unless required by applicable law or agreed to in writing, software
023 * distributed under the License is distributed on an "AS IS" BASIS,
024 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
025 * See the License for the specific language governing permissions and
026 * limitations under the License.
027 * #L%
028 */
029
030
031import com.pi4j.util.NativeLibraryLoader;
032
033/**
034 * <p>
035 * WiringPi includes a software-driven PWM handler capable of outputting a PWM signal on any of the
036 * Raspberry Pi's GPIO pins.
037 * </p>
038 * 
039 * <p>
040 * There are some limitations. To maintain a low CPU usage, the minimum pulse width is 100uS. That
041 * combined with the default suggested range of 100 gives a PWM frequency of 100Hz. You can lower
042 * the range to get a higher frequency, at the expense of resolution, or increase to get more
043 * resolution, but that will lower the frequency. If you change the pulse-width in the drive code,
044 * then be aware that at delays of less than 100uS wiringPi does it in a software loop, which means
045 * that CPU usage will rise dramatically, and controlling more than one pin will be almost
046 * impossible.
047 * </p>
048 * 
049 * <p>
050 * Also note that while the routines run themselves at a higher and real-time priority, Linux can
051 * still affect the accuracy of the generated signal.
052 * </p>
053 * 
054 * <p>
055 * However, within these limitations, control of a light/LED or a motor is very achievable.
056 * </p>
057 * 
058 * <p>
059 * <b>You must initialize wiringPi with one of wiringPiSetup() or wiringPiSetupGpio() functions
060 * beforehand. wiringPiSetupSys() is not fast enough, so you must run your programs with sudo.</b>
061 * </p>
062 * 
063 * <p>
064 * Before using the Pi4J library, you need to ensure that the Java VM in configured with access to
065 * the following system libraries:
066 * <ul>
067 * <li>pi4j</li>
068 * <li>wiringPi</li>
069 * <li>pthread</li>
070 * </ul>
071 * <blockquote> This library depends on the wiringPi native system library.</br> (developed by
072 * Gordon Henderson @ <a href="https://projects.drogon.net/">https://projects.drogon.net/</a>)
073 * </blockquote>
074 * </p>
075 * 
076 * @see <a href="http://www.pi4j.com/">http://www.pi4j.com/</a>
077 * @author Robert Savage (<a
078 *         href="http://www.savagehomeautomation.com">http://www.savagehomeautomation.com</a>)
079 */
080public class Spi {
081
082    public static int CHANNEL_0 = 0;
083    public static int CHANNEL_1 = 1;
084    
085    // private constructor 
086    private Spi()  {
087        // forbid object construction 
088    }
089    
090    static {
091        // Load the platform library
092        NativeLibraryLoader.load("pi4j", "libpi4j.so");
093    }
094
095    /**
096     * <p>wiringPiSPIGetFd:</p>
097     * 
098     * <p>
099     * Return the file-descriptor for the given channel
100     * </p>
101     * 
102     * @param channel SPI channel
103     * @return file-descriptor for the given channel
104     */
105    public static native int wiringPiSPIGetFd(int channel);
106
107    /**
108     * <p>wiringPiSPIDataRW:</p>
109     * 
110     * <p>
111     * Write and Read a block of data over the SPI bus. Note the data is being read into the
112     * transmit buffer, so will overwrite it! This is also a full-duplex operation.
113     * </p>
114     * 
115     * <p>
116     * (ATTENTION: the 'data' argument can only be a maximum of 1024 characters.)
117     * </p>
118     * 
119     * @param channel SPI channel</p>
120     * @param data
121     * @param len
122     * @return return -1 on error
123     */
124    public static native int wiringPiSPIDataRW(int channel, String data, int len);
125
126    /**
127     * <p>wiringPiSPIDataRW:</p>
128     * 
129     * <p>
130     * Write and Read a block of data over the SPI bus. Note the data is being read into the
131     * transmit buffer, so will overwrite it! This is also a full-duplex operation.
132     * </p>
133     * 
134     * <p>
135     * (ATTENTION: the 'data' argument can only be a maximum of 1024 characters.)
136     * </p>
137     * 
138     * @param channel SPI channel</p>
139     * @param data
140     * @param len
141     * @return return -1 on error
142     */
143    public static native int wiringPiSPIDataRW(int channel, byte[] data, int len);
144    
145    /**
146     * <p>wiringPiSPISetup:</p>
147     * 
148     * <p>
149     * Open the SPI device, and set it up, etc.</b>
150     * 
151     * @param channel SPI channel
152     * @param speed SPI speed
153     * @return return -1 on error
154     */
155    public static native int wiringPiSPISetup(int channel, int speed);
156}