📄 memorybankeprom.java
字号:
} /** * Query to see if current memory bank pages need the adapter to * have a 'PowerDelivery' feature in order to write to the memory. * * @return 'true' if writing to the current memory bank pages * requires 'PowerDelivery'. */ public boolean needsPowerDelivery () { return powerDelivery; } /** * Query to get the starting physical address of this bank. Physical * banks are sometimes sub-divided into logical banks due to changes * in attributes. * * @return physical starting address of this logical bank. */ public int getStartPhysicalAddress () { return startPhysicalAddress; } /** * Query to get the memory bank size in bytes. * * @return memory bank size in bytes. */ public int getSize () { return size; } //-------- //-------- PagedMemoryBank query methods //-------- /** * Query to get the number of pages in current memory bank. * * @return number of pages in current memory bank */ public int getNumberPages () { return numberPages; } /** * Query to get page length in bytes in current memory bank. * * @return page length in bytes in current memory bank */ public int getPageLength () { return pageLength; } /** * Query to get Maximum data page length in bytes for a packet * read or written in the current memory bank. See the 'ReadPagePacket()' * and 'WritePagePacket()' methods. This method is only usefull * if the current memory bank is general purpose memory. * * @return max packet page length in bytes in current memory bank */ public int getMaxPacketDataLength () { return maxPacketDataLength; } /** * Query to see if current memory bank pages can be read with * the contents being verified by a device generated CRC. * This is used to see if the 'ReadPageCRC()' can be used. * * @return 'true' if current memory bank can be read with self * generated CRC. */ public boolean hasPageAutoCRC () { return pageAutoCRC; } /** * Query to see if current memory bank pages when read deliver * extra information outside of the normal data space. Examples * of this may be a redirection byte, counter, tamper protection * bytes, or SHA-1 result. If this method returns true then the * methods 'ReadPagePacket()' and 'readPageCRC()' with 'extraInfo' * parameter can be used. * * @return 'true' if reading the current memory bank pages * provides extra information. * * @deprecated As of 1-Wire API 0.01, replaced by {@link #hasExtraInfo()} */ public boolean haveExtraInfo () { return extraInfo; } /** * Checks to see if this memory bank's pages deliver extra * information outside of the normal data space, when read. Examples * of this may be a redirection byte, counter, tamper protection * bytes, or SHA-1 result. If this method returns true then the * methods with an 'extraInfo' parameter can be used: * {@link #readPage(int,boolean,byte[],int,byte[]) readPage}, * {@link #readPageCRC(int,boolean,byte[],int,byte[]) readPageCRC}, and * {@link #readPagePacket(int,boolean,byte[],int,byte[]) readPagePacket}. * * @return <CODE> true </CODE> if reading the this memory bank's * pages provides extra information * * @see #readPage(int,boolean,byte[],int,byte[]) readPage(extra) * @see #readPageCRC(int,boolean,byte[],int,byte[]) readPageCRC(extra) * @see #readPagePacket(int,boolean,byte[],int,byte[]) readPagePacket(extra) * @since 1-Wire API 0.01 */ 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 redirectPage; } /** * 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 lockRedirectPage; } //-------- //-------- 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 { int i; // 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(); // check if status memory if (READ_PAGE_WITH_CRC == STATUS_READ_PAGE_COMMAND) { // no regular read memory so must use readPageCRC int start_pg = startAddr / pageLength; int end_pg = ((startAddr + len) / pageLength) - 1; if (((startAddr + len) % pageLength) > 0) end_pg++; byte[] raw_buf = new byte [(end_pg - start_pg + 1) * pageLength]; // loop to read the pages for (int pg = start_pg; pg <= end_pg; pg++) readPageCRC(pg, !(pg == start_pg), raw_buf, (pg - start_pg) * pageLength, null, 0); // extract out the data System.arraycopy(raw_buf, (startAddr % pageLength), readBuf, offset, len); } // regular memory so use standard read memory command else { // see if need to access the device if (!readContinue) { // select the device if (!ib.adapter.select(ib.address)) { forceVerify(); throw new OneWireIOException("Device select failed"); } // build start reading memory block byte[] raw_buf = new byte [4]; raw_buf [0] = READ_MEMORY_COMMAND; raw_buf [1] = ( byte ) ((startAddr + startPhysicalAddress) & 0xFF); raw_buf [2] = ( byte ) ((((startAddr + startPhysicalAddress) & 0xFFFF) >>> 8) & 0xFF); raw_buf [3] = ( byte ) 0xFF; // check if get a 1 byte crc in a normal read. int num_bytes = (normalReadCRC) ? 4 : 3; // do the first block for command, address ib.adapter.dataBlock(raw_buf, 0, num_bytes); } // pre-fill readBuf with 0xFF int pgs = len / pageLength; int extra = len % pageLength; for (i = 0; i < pgs; i++) System.arraycopy(ffBlock, 0, readBuf, offset + i * pageLength, pageLength); System.arraycopy(ffBlock, 0, readBuf, offset + pgs * pageLength, extra); // send second block to read data, return result ib.adapter.dataBlock(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; byte result; // return if nothing to do if (len == 0) return; // check if power delivery is available if (!ib.adapter.canProgram()) throw new OneWireException( "Program voltage required but not available"); // check if trying to write read only bank if (isReadOnly()) throw new OneWireException("Trying to write read-only memory bank"); // check if write exceeds memory if ((startAddr + len) > (pageLength * numberPages)) throw new OneWireException("Write exceeds memory bank end"); // set the program pulse duration ib.adapter.setProgramPulseDuration(DSPortAdapter.DELIVERY_EPROM); // attempt to put device at max desired speed checkSpeed(); // loop while still have bytes to write boolean write_continue = false; for (i = 0; i < len; i++) { result = programByte(startAddr + i + startPhysicalAddress, writeBuf [offset + i], write_continue); if (writeVerification) { if (( byte ) result == ( byte ) writeBuf [offset + i]) write_continue = true; else { forceVerify(); throw new OneWireIOException( "Read back byte on EPROM programming did not match"); } } } } //-------- //-------- 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -