📄 memorybankeeprom.java
字号:
*/ public boolean hasExtraInfo () { return extraInfo; } /** * Query to get the length in bytes of extra information that * is read when read a page in the current memory bank. See * 'hasExtraInfo()'. * * @return number of bytes in Extra Information read when reading * pages in the current memory bank. */ public int getExtraInfoLength () { return extraInfoLength; } /** * Query to get a string description of what is contained in * the Extra Informationed return when reading pages in the current * memory bank. See 'hasExtraInfo()'. * * @return string describing extra information. */ public String getExtraInfoDescription () { return extraInfoDescription; } /** * Set the write verification for the 'write()' method. * * @param doReadVerf true (default) verify write in 'write' * false, don't verify write (used on * Write-Once bit manipulation) */ public void setWriteVerification (boolean doReadVerf) { writeVerification = doReadVerf; } //-------- //-------- OTPMemoryBank query methods //-------- /** * Query to see if current memory bank pages can be redirected * to another pages. This is mostly used in Write-Once memory * to provide a means to update. * * @return 'true' if current memory bank pages can be redirected * to a new page. */ public boolean canRedirectPage () { return false; } /** * Query to see if current memory bank pages can be locked. A * locked page would prevent any changes to the memory. * * @return 'true' if current memory bank pages can be redirected * to a new page. */ public boolean canLockPage () { return lockPage; } /** * Query to see if current memory bank pages can be locked from * being redirected. This would prevent a Write-Once memory from * being updated. * * @return 'true' if current memory bank pages can be locked from * being redirected to a new page. */ public boolean canLockRedirectPage () { return false; } //-------- //-------- MemoryBank I/O methods //-------- /** * Read memory 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. * * @param startAddr starting physical address * @param readContinue if 'true' then device read is continued without * re-selecting. This can only be used if the new * read() 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 len length in bytes to read * * @throws OneWireIOException * @throws OneWireException */ public void read (int startAddr, boolean readContinue, byte[] readBuf, int offset, int len) throws OneWireIOException, OneWireException { byte[] buff = new byte[150]; System.arraycopy(ffBlock,0,buff,0,150); // check if read exceeds memory if ((startAddr + len) > (pageLength * numberPages)) throw new OneWireException("Read exceeds memory bank end"); // attempt to put device at max desired speed if (!readContinue) { checkSpeed(); // select the device if (ib.adapter.select(ib.address)) { buff[0] = READ_MEMORY_COMMAND; // address 1 buff[1] = ( byte ) (startAddr & 0xFF); // address 2 buff[2] = ( byte ) (((startAddr & 0xFFFF) >>> 8) & 0xFF); ib.adapter.dataBlock(buff, 0, len+3); // extract the data System.arraycopy(buff, 3, readBuf, offset, len); } else throw new OneWireIOException("Device select failed"); } else { ib.adapter.dataBlock(buff, 0, len); // extract the data System.arraycopy(buff, 0, readBuf, offset, len); } } /** * Write memory in the current bank. It is recommended that * when writing data that some structure in the data is created * to provide error free reading back with read(). Or the * method 'writePagePacket()' could be used which automatically * wraps the data in a length and CRC. * * When using on Write-Once devices care must be taken to write into * into empty space. If write() is used to write over an unlocked * page on a Write-Once device it will fail. If write verification * is turned off with the method 'setWriteVerification(false)' then * the result will be an 'AND' of the existing data and the new data. * * @param startAddr starting address * @param writeBuf byte array containing data to write * @param offset offset into writeBuf to get data * @param len length in bytes to write * * @throws OneWireIOException * @throws OneWireException */ public void write (int startAddr, byte[] writeBuf, int offset, int len) throws OneWireIOException, OneWireException { int i, room_left; // return if nothing to do if (len == 0) return; if((isPageLocked(0) && (startAddr < 32)) || (isPageLocked(1))) throw new OneWireIOException("The page is locked."); // attempt to put device at speed checkSpeed(); // check if write exceeds memory if ((startAddr + len) > size) throw new OneWireException("Write exceeds memory bank end"); // check if trying to write read only bank if (isReadOnly()) throw new OneWireException("Trying to write read-only memory bank"); // loop while still have pages to write int startx = 0, nextx = 0; // (start and next index into writeBuf) byte[] raw_buf = new byte [8]; byte[] memory = new byte [128]; byte[] scratchpad = new byte[8]; byte[] es_data = new byte[3]; int abs_addr = startAddr; int pl = 8; read(0,false,memory,0,128); do { // calculate room left in current page room_left = pl - ((abs_addr + startx) % pl); // check if block left will cross end of page if ((len - startx) > room_left) nextx = startx + room_left; else nextx = len; System.arraycopy(memory,(((startx+startAddr)/8)*8),raw_buf,0,8); if((nextx-startx) == 8) { System.arraycopy(writeBuf,offset+startx,raw_buf,0,8); } else { if(((startAddr+nextx)%8) == 0) { System.arraycopy(writeBuf,offset+startx,raw_buf,((startAddr+startx)%8), 8-((startAddr+startx)%8)); } else { System.arraycopy(writeBuf,offset+startx,raw_buf,((startAddr+startx)%8), ((startAddr+nextx)%8)-((startAddr+startx)%8)); } } // write the page of data to scratchpad if(!writeScratchpad(abs_addr+startx+room_left-8,raw_buf,0,8)) throw new OneWireIOException("Invalid CRC16 in write"); if(!readScratchpad(scratchpad,0,8,es_data)) throw new OneWireIOException("Read scratchpad was not successful."); if((es_data[2] & 0x20) == 0x20) { throw new OneWireIOException("The write scratchpad command was not completed."); } else { for(i=0;i<8;i++) if(scratchpad[i] != raw_buf[i]) { throw new OneWireIOException("The read back of the data in the scratch pad did " + "not match."); } } // Copy data from scratchpad into memory copyScratchpad(es_data); if(startAddr >= pageLength) System.arraycopy(raw_buf,0,memory,(((startx+startAddr)/8)*8)-32,8); else System.arraycopy(raw_buf,0,memory,(((startx+startAddr)/8)*8),8); // point to next index startx = nextx; } while (nextx < len); } //-------- //-------- PagedMemoryBank I/O methods //-------- /** * Read page 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. * * @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 * * @throws OneWireIOException * @throws OneWireException */ public void readPage (int page, boolean readContinue, byte[] readBuf, int offset) throws OneWireIOException, OneWireException { 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 { throw new OneWireException( "Read extra information not supported on this memory bank"); } /** * 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 { throw new OneWireException( "Read extra information not supported on this memory bank"); } /** * 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 read((page*pageLength), readContinue, raw_buf, 0, pageLength); // 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -