📄 memorybankappreg.java
字号:
} /** * Read a Universal Data Packet and extra information. See the * method 'readPagePacket()' for a description of the packet structure. * See the method 'hasExtraInfo()' for a description of the optional * extra information some devices have. * * @param page page number to read packet from * @param readContinue if 'true' then device read is continued without * re-selecting. This can only be used if the new * readPagePacket() continious where the last one * stopped and it is inside a * 'beginExclusive/endExclusive' block. * @param readBuf byte array to put data read. Must have at least * 'getMaxPacketDataLength()' elements. * @param offset offset into readBuf to place data * * @return number of data bytes written to readBuf at the offset. * * @throws OneWireIOException * @throws OneWireException */ public int readPagePacket (int page, boolean readContinue, byte[] readBuf, int offset) throws OneWireIOException, OneWireException { byte[] raw_buf = new byte [PAGE_SIZE]; // read the entire page data read(page, true, raw_buf, 0, PAGE_SIZE); // check if length is realistic if (raw_buf [0] > (PAGE_SIZE - 2)) throw new OneWireIOException("Invalid length in packet"); // verify the CRC is correct if (CRC16.compute(raw_buf, 0, raw_buf [0] + 3, page) == 0x0000B001) { // extract the data out of the packet System.arraycopy(raw_buf, 1, readBuf, offset, raw_buf [0]); // return the length return raw_buf [0]; } else throw new OneWireIOException("Invalid CRC16 in packet read"); } /** * Write a Universal Data Packet. See the method 'readPagePacket()' * for a description of the packet structure. * * @param page page number to write packet to * @param writeBuf data byte array to write * @param offset offset into writeBuf where data to write is * @param len number of bytes to write * * @throws OneWireIOException * @throws OneWireException */ public void writePagePacket (int page, byte[] writeBuf, int offset, int len) throws OneWireIOException, OneWireException { // make sure length does not exceed max if (len > (PAGE_SIZE - 2)) throw new OneWireIOException( "Length of packet requested exceeds page size"); // construct the packet to write byte[] raw_buf = new byte [len + 3]; raw_buf [0] = ( byte ) len; System.arraycopy(writeBuf, offset, raw_buf, 1, len); int crc = CRC16.compute(raw_buf, 0, len + 1, page); raw_buf [len + 1] = ( byte ) (~crc & 0xFF); raw_buf [len + 2] = ( byte ) (((~crc & 0xFFFF) >>> 8) & 0xFF); // write the packet, return result write(page * PAGE_SIZE, raw_buf, 0, len + 3); } /** * Read a complete memory page with CRC verification provided by the * device. Not supported by all devices. See the method * 'hasPageAutoCRC()'. * * @param page page number to read * @param readContinue if 'true' then device read is continued without * re-selecting. This can only be used if the new * readPagePacket() continious where the last one * stopped and it is inside a * 'beginExclusive/endExclusive' block. * @param readBuf byte array to put data read. Must have at least * 'getMaxPacketDataLength()' elements. * @param offset offset into readBuf to place data * * @throws OneWireIOException * @throws OneWireException */ public void readPageCRC (int page, boolean readContinue, byte[] readBuf, int offset) throws OneWireIOException, OneWireException { // only needs to be implemented if supported by hardware throw new OneWireException( "Read page with CRC not supported by this memory bank"); } /** * Read a complete memory page with CRC verification provided by the * device with extra information. Not supported by all devices. * See the method 'hasPageAutoCRC()'. * See the method 'hasExtraInfo()' for a description of the optional * extra information. * * @param page page number to read * @param readContinue if 'true' then device read is continued without * re-selecting. This can only be used if the new * readPagePacket() continious where the last one * stopped and it is inside a * 'beginExclusive/endExclusive' block. * @param readBuf byte array to put data read. Must have at least * 'getMaxPacketDataLength()' elements. * @param offset offset into readBuf to place data * @param extraInfo byte array to put extra info read into * * @throws OneWireIOException * @throws OneWireException */ public void readPageCRC (int page, boolean readContinue, byte[] readBuf, int offset, byte[] extraInfo) throws OneWireIOException, OneWireException { // only needs to be implemented if supported by hardware throw new OneWireException( "Read page with CRC not supported by this memory bank"); } //-------- //-------- OTPMemoryBank I/O methods //-------- /** * Lock the specifed page in the current memory bank. Not supported * by all devices. See the method 'canLockPage()'. * * @param page number of page to lock * * @throws OneWireIOException * @throws OneWireException */ public void lockPage (int page) throws OneWireIOException, OneWireException { // attempt to put device at the correct speed ib.doSpeed(); // select the device if (!ib.adapter.select(ib.address)) throw new OneWireIOException("Device select failed"); // do the copy/lock sequence ib.adapter.putByte(COPY_LOCK_COMMAND); ib.adapter.putByte(VALIDATION_KEY); // read back to verify if (!isPageLocked(page)) throw new OneWireIOException( "Read back from write incorrect, could not lock page"); } /** * Query to see if the specified page is locked. * See the method 'canLockPage()'. * * @param page number of page to see if locked * * @return 'true' if page locked. * * @throws OneWireIOException * @throws OneWireException */ public boolean isPageLocked (int page) throws OneWireIOException, OneWireException { // check if for valid page if (page != 0) throw new OneWireException( "Invalid page number for this memory bank"); // attempt to put device at the correct speed ib.doSpeed(); // read status and return result return (( byte ) readStatus() == ( byte ) LOCKED_FLAG); } /** * Redirect the specifed page in the current memory bank to a new page. * Not supported by all devices. See the method 'canRedirectPage()'. * * @param page number of page to redirect * @param newPage new page number to redirect to * * @throws OneWireIOException * @throws OneWireException */ public void redirectPage (int page, int newPage) throws OneWireIOException, OneWireException { // only needs to be implemented if supported by hardware throw new OneWireException( "Page redirection not supported by this memory bank"); } /** * Query to see if the specified page is redirected. * Not supported by all devices. See the method 'canRedirectPage()'. * * @param page number of page check for redirection * * @return return the new page number or 0 if not redirected * * @throws OneWireIOException * @throws OneWireException * * @deprecated As of 1-Wire API 0.01, replaced by {@link #getRedirectedPage(int)} */ public int isPageRedirected (int page) throws OneWireIOException, OneWireException { return 0; } /** * Gets the page redirection of the specified page. * Not supported by all devices. * * @param page page to check for redirection * * @return the new page number or 0 if not redirected * * @throws OneWireIOException on a 1-Wire communication error such as * no device present or a CRC read from the device is incorrect. This could be * caused by a physical interruption in the 1-Wire Network due to * shorts or a newly arriving 1-Wire device issuing a 'presence pulse'. * @throws OneWireException on a communication or setup error with the 1-Wire * adapter. * * @see #canRedirectPage() canRedirectPage * @see #redirectPage(int,int) redirectPage * @since 1-Wire API 0.01 */ public int getRedirectedPage (int page) throws OneWireIOException, OneWireException { return 0; } /** * Lock the redirection option for the specifed page in the current * memory bank. Not supported by all devices. See the method * 'canLockRedirectPage()'. * * @param page number of page to redirect * * @throws OneWireIOException * @throws OneWireException */ public void lockRedirectPage (int page) throws OneWireIOException, OneWireException { // only needs to be implemented if supported by hardware throw new OneWireException( "Lock Page redirection not supported by this memory bank"); } /** * Query to see if the specified page has redirection locked. * Not supported by all devices. See the method 'canRedirectPage()'. * * @param page number of page check for locked redirection * * @return return 'true' if redirection is locked for this page * * @throws OneWireIOException * @throws OneWireException */ public boolean isRedirectPageLocked (int page) throws OneWireIOException, OneWireException { return false; } //-------- //-------- Bank specific methods //-------- /** * Read the status register for this memory bank. * * @param readBuf byte array to put data read. Must have at least * 'getExtraInfoLength()' elements. * @throws OneWireIOException * @throws OneWireException */ protected void readStatus (byte[] readBuf) throws OneWireIOException, OneWireException { readBuf [0] = ( byte ) readStatus(); } /** * Read the status register for this memory bank. * * @return the status register byte * * @throws OneWireIOException * @throws OneWireException */ protected byte readStatus () throws OneWireIOException, OneWireException { // select the device if (!ib.adapter.select(ib.address)) throw new OneWireIOException("Device select failed"); // do the read status sequence ib.adapter.putByte(READ_STATUS_COMMAND); // validation key ib.adapter.putByte(0); return ( byte ) ib.adapter.getByte(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -