📄 spi.java
字号:
package etaai.lcd.tinispi;
/**
* This class is a thin wrapper over a bit-bang SPI native library.<br>
* The bit-bang SPI port is implemented using pins ... (PCE0\-PCE3\) of
* the DS80C390. This is a master only implementation.
*
* P5.4 (PCE0\) = SSCK (Serial clock)
* P5.5 (PCE1\) = MOSI (Master Out/Slave In)
* P5.6 (PCE2\) = MISO (Master In/Slave Out)
* P5.7 (PCE3\) = SS (Slave select)
*
* <ul>
* <li>
* Max data rate is 250Kbit/s. Lower frequencies are specified using a clock
* delay value that can be set using the <code>setClockDelay</code> method.
* </li>
* </ul>
*/
class SPI {
public static final int DEFAULT_CLOCK_DELAY = 1;
public static final int MAX_CLOCK_DELAY = 63;
// SPI config state
private int delay;
private boolean noskew;
private boolean use_ss;
/* Load native library in static intializer */
static {
try {
System.loadLibrary("spi.tlib");
} catch (UnsatisfiedLinkError ule) {
System.out.println("Unable to load/initialize library:" +
ule.getMessage());
throw ule;
}
}
/**
* Construct an SPI object with default configuration state which
* includes:
* DEFAULT_CLOCK_DELAY
* Allow clock skew (i.e. don't disable interrupts during byte transfers)
* Use the SS line
*/
public SPI() {
this(DEFAULT_CLOCK_DELAY, false, true);
}
/**
* Construct an SPI object with all configuration state specified.
*
* @param delay # of microseconds of delay between clock edges
* @param noskew set to true to disable interrupts for strict clock rates
* @param use_ss set to true to enable use of the SS line
*/
public SPI(int delay, boolean noskew, boolean use_ss) {
this.delay = delay;
this.noskew = noskew;
this.use_ss = use_ss;
}
/**
* Write <code>len</code> bytes of data to MOSI. Return data
* simultaneously read from MISO. Source data is contained
* in <code>ba</code> starting at <code>off</code>. Read data is returned
* in the same positions within the array.
*
* @param ba source byte array, contains data to be written to the SPI slave
* @param off starting offset into <code>ba</code>
* @param len number of bytes to write
*/
public synchronized int xmit(byte[] ba, int off, int len) {
// Bounds check array access (easier here than in native)
if ((len < 0) || (off < 0) || ((len + off) > ba.length))
throw new ArrayIndexOutOfBoundsException();
return xmitSPI(ba, off, len, delay, noskew, use_ss);
}
/**
* Specify the number of microseconds to delay between clock edges.
*
* @param delay The number of microseconds between clock edges
* @throws IllegalArgumentException if <code>delay</code> is negative
* or greater than <code>MAX_CLOCK_DELAY</code>.
*/
public void setClockDelay(int delay) throws IllegalArgumentException {
if ((delay < 0) || (delay > MAX_CLOCK_DELAY)) {
throw new IllegalArgumentException("Invalid clock delay:"+delay);
}
this.delay = delay;
}
/**
* Disable interrupts during byte transfers to ensure a stable clock
* frequency. Many SPI devices don't require a precise clock frequency
* range. In this case, the application should allow clock skew due to
* system interrupts during SPI communication. By default, interrupts
* are enabled during communication.
*
* @param noskew set to <code>true</code> to disable interrupts
*
*/
public void enableMinimumClockSkew(boolean noskew) {
this.noskew = noskew;
}
/**
* Enable the use of the SS (slave select) line during communication. If
* set to <code>true</code>, the SS line is asserted before each
* read or write operation and deasserted immediately following the
* operation. Otherwise this pin is ignored.
*
* @param use_ss set to <code>true</code> to force use of SS pin
*/
public void enableSlaveSelect(boolean use_ss) {
this.use_ss = use_ss;
}
// *** Native SPI read and write methods.
private static native int xmitSPI(byte[] ba, int off, int len,
int delay, boolean noskew, boolean use_ss);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -