📄 memorybankeprom.java
字号:
* led off and it is inside a * 'beginExclusive/endExclusive' block. * @param readBuf byte array to place read data into * @param offset offset into readBuf to place data * * @throws OneWireIOException * @throws OneWireException */ public void readPage (int page, boolean readContinue, byte[] readBuf, int offset) throws OneWireIOException, OneWireException { if (pageAutoCRC) readPageCRC(page, readContinue, readBuf, offset, null, extraInfoLength); else read(page * pageLength, readContinue, readBuf, offset, pageLength); } /** * Read page with extra information in the current bank with no * CRC checking (device or data). The resulting data from this API * may or may not be what is on the 1-Wire device. It is recommends * that the data contain some kind of checking (CRC) like in the * readPagePacket() method or have the 1-Wire device provide the * CRC as in readPageCRC(). readPageCRC() however is not * supported on all memory types, see 'hasPageAutoCRC()'. * If neither is an option then this method could be called more * then once to at least verify that the same thing is read consistantly. * 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 * readPage() continious where the last one * led off and it is inside a * 'beginExclusive/endExclusive' block. * @param readBuf byte array to place read data into * @param offset offset into readBuf to place data * @param extraInfo byte array to put extra info read into * * @throws OneWireIOException * @throws OneWireException */ public void readPage (int page, boolean readContinue, byte[] readBuf, int offset, byte[] extraInfo) throws OneWireIOException, OneWireException { // check if current bank is not scratchpad bank, or not page 0 if (!this.extraInfo) throw new OneWireException( "Read extra information not supported on this memory bank"); readPageCRC(page, readContinue, readBuf, offset, extraInfo, extraInfoLength); } /** * 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 * @param extraInfo byte array to put extra info read into * * @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, byte[] extraInfo) throws OneWireIOException, OneWireException { byte[] raw_buf = new byte [pageLength]; // check if current bank is not scratchpad bank, or not page 0 if (!this.extraInfo) throw new OneWireException( "Read extra information not supported on this memory bank"); // read entire page with read page CRC readPageCRC(page, readContinue, raw_buf, 0, extraInfo, extraInfoLength); // check if length is realistic if ((raw_buf [0] & 0x00FF) > maxPacketDataLength) { forceVerify(); 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 { forceVerify(); throw new OneWireIOException("Invalid CRC16 in packet read"); } } /** * 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 [pageLength]; // read entire page with read page CRC readPageCRC(page, readContinue, raw_buf, 0, null, extraInfoLength); // check if length is realistic if ((raw_buf [0] & 0x00FF) > maxPacketDataLength) { forceVerify(); 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 { forceVerify(); 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 > maxPacketDataLength) throw new OneWireIOException( "Length of packet requested exceeds page size"); // see if this bank is general read/write if (!generalPurposeMemory) throw new OneWireException( "Current bank is not general purpose memory"); // 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 * pageLength, 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 { readPageCRC(page, readContinue, readBuf, offset, null, extraInfoLength); } /** * 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 { readPageCRC(page, readContinue, readBuf, offset, extraInfo, extraInfoLength); } //-------- //-------- 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 { // create byte to write to mlLock to lock page int nbyt = (page >>> 3); int nbit = page - (nbyt << 3); byte[] wr_byte = new byte [1]; wr_byte [0] = ( byte ) ~(0x01 << nbit); // bit field so turn off write verification mbLock.setWriteVerification(false); // write the lock bit mbLock.write(nbyt + lockOffset, wr_byte, 0, 1); // read back to verify if (!isPageLocked(page)) { forceVerify(); 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 { // read page that locked bit is on int pg_len = mbLock.getPageLength(); int read_pg = (page + lockOffset) / (pg_len * 8); // read page with bit byte[] read_buf = new byte [pg_len]; mbLock.readPageCRC(read_pg, false, read_buf, 0); // return boolean on locked bit int index = (page + lockOffset) - (read_pg * 8 * pg_len); int nbyt = (index >>> 3); int nbit = index - (nbyt << 3); return !(((read_buf [nbyt] >>> nbit) & 0x01) == 0x01); } /** * 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 { // create byte to redirect page byte[] wr_byte = new byte [1]; wr_byte [0] = ( byte ) ~newPage; // writing byte so turn on write verification mbRedirect.setWriteVerification(true); // write the redirection byte mbRedirect.write(page + redirectOffset, wr_byte, 0, 1); } /** * 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 { // read page that redirect byte is on int pg_len = mbRedirect.getPageLength(); int read_pg = (page + redirectOffset) / pg_len; // read page with byte byte[] read_buf = new byte [pg_len]; mbRedirect.readPageCRC(read_pg, false, read_buf, 0); // return page return ~read_buf [(page + redirectOffset) % pg_len] & 0x000000FF; } /** * 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -