📄 memorybankeprom.java
字号:
* 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 { // 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; } /** * 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 { // 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 mbLockRedirect.setWriteVerification(false); // write the lock bit mbLockRedirect.write(nbyt + lockRedirectOffset, wr_byte, 0, 1); // read back to verify if (!isRedirectPageLocked(page)) { forceVerify(); throw new OneWireIOException( "Read back from write incorrect, could not lock redirect byte"); } } /** * 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 { // read page that lock redirect bit is on int pg_len = mbLockRedirect.getPageLength(); int read_pg = (page + lockRedirectOffset) / (pg_len * 8); // read page with bit byte[] read_buf = new byte [pg_len]; mbLockRedirect.readPageCRC(read_pg, false, read_buf, 0); // return boolean on lock redirect bit int index = (page + lockRedirectOffset) - (read_pg * 8 * pg_len); int nbyt = (index >>> 3); int nbit = index - (nbyt << 3); return !(((read_buf [nbyt] >>> nbit) & 0x01) == 0x01); } //-------- //-------- Bank specific methods //-------- /** * Read a complete memory page with CRC verification provided by the * device with extra information. Not supported by all devices. * If not extra information available then just call with extraLength=0. * * @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 * @param extraLength length of extra information * * @throws OneWireIOException * @throws OneWireException */ protected void readPageCRC (int page, boolean readContinue, byte[] readBuf, int offset, byte[] extraInfo, int extraLength) throws OneWireIOException, OneWireException { int len = 0, lastcrc = 0; byte[] raw_buf = new byte [pageLength + numCRCBytes]; // only needs to be implemented if supported by hardware if (!pageAutoCRC) throw new OneWireException( "Read page with CRC not supported in this memory bank"); // attempt to put device at max desired speed if (!readContinue) checkSpeed(); // check if read exceeds memory if (page > numberPages) throw new OneWireException("Read exceeds memory bank end"); // 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 with: command, address, (extraInfo?), (crc?) len = 3 + extraInfoLength; if (crcAfterAddress) len += numCRCBytes; System.arraycopy(ffBlock, 0, raw_buf, 0, len); raw_buf [0] = READ_PAGE_WITH_CRC; int addr = page * pageLength + startPhysicalAddress; raw_buf [1] = ( byte ) (addr & 0xFF); raw_buf [2] = ( byte ) (((addr & 0xFFFF) >>> 8) & 0xFF); // do the first block ib.adapter.dataBlock(raw_buf, 0, len); } else if (extraInfoLength > 0) { // build first block with: extraInfo, crc len = extraInfoLength + numCRCBytes; System.arraycopy(ffBlock, 0, raw_buf, 0, len); // do the first block ib.adapter.dataBlock(raw_buf, 0, len); } // check CRC if (numCRCBytes == 2) lastcrc = CRC16.compute(raw_buf, 0, len, 0); else lastcrc = CRC8.compute(raw_buf, 0, len, 0); if ((extraInfoLength > 0) || (crcAfterAddress)) { // check CRC if (numCRCBytes == 2) { if (lastcrc != 0x0000B001) { forceVerify(); throw new OneWireIOException("Invalid CRC16 read from device"); } } else { if (lastcrc != 0) { forceVerify(); throw new OneWireIOException("Invalid CRC8 read from device"); } } lastcrc = 0; // extract the extra information if ((extraInfoLength > 0) && (extraInfo != null)) System.arraycopy(raw_buf, len - extraInfoLength - numCRCBytes, extraInfo, 0, extraLength); } // pre-fill with 0xFF System.arraycopy(ffBlock, 0, raw_buf, 0, raw_buf.length); // send block to read data + crc ib.adapter.dataBlock(raw_buf, 0, raw_buf.length); // check the CRC if (numCRCBytes == 2) { if (CRC16.compute(raw_buf, 0, raw_buf.length, lastcrc) != 0x0000B001) { forceVerify(); throw new OneWireIOException("Invalid CRC16 read from device"); } } else { if (CRC8.compute(raw_buf, 0, raw_buf.length, lastcrc) != 0) { forceVerify(); throw new OneWireIOException("Invalid CRC8 read from device"); } } // extract the page data System.arraycopy(raw_buf, 0, readBuf, offset, pageLength); } /** * Program an EPROM byte at the specified address. * * @param addr address * @param data data byte to program * @param writeContinue if 'true' then device programming is continued without * re-selecting. This can only be used if the new * programByte() continious where the last one * stopped and it is inside a * 'beginExclusive/endExclusive' block. * * @return the echo byte after programming. This should be the desired * byte to program if the location was previously unprogrammed. * * @throws OneWireIOException * @throws OneWireException */ protected byte programByte (int addr, byte data, boolean writeContinue) throws OneWireIOException, OneWireException { int lastcrc = 0, len; if (!writeContinue) { // select the device if (!ib.adapter.select(ib.address)) { forceVerify(); throw new OneWireIOException("device not present"); } // pre-fill with 0xFF byte[] raw_buf = new byte [6]; System.arraycopy(ffBlock, 0, raw_buf, 0, raw_buf.length); // contruct packet raw_buf [0] = ( byte ) WRITE_MEMORY_COMMAND; raw_buf [1] = ( byte ) (addr & 0xFF); raw_buf [2] = ( byte ) (((addr & 0xFFFF) >>> 8) & 0xFF); raw_buf [3] = ( byte ) data; if (numCRCBytes == 2) { lastcrc = CRC16.compute(raw_buf, 0, 4, 0); len = 6; } else { lastcrc = CRC8.compute(raw_buf, 0, 4, 0); len = 5; } // send block to read data + crc ib.adapter.dataBlock(raw_buf, 0, len); // check CRC if (numCRCBytes == 2) { if (CRC16.compute(raw_buf, 4, 2, lastcrc) != 0x0000B001) { forceVerify(); throw new OneWireIOException("Invalid CRC16 read from device"); } } else { if (CRC8.compute(raw_buf, 4, 1, lastcrc) != 0) { forceVerify(); throw new OneWireIOException("Invalid CRC8 read from device"); } } } else { // send the data ib.adapter.putByte(data); // check CRC from device if (numCRCBytes == 2) { lastcrc = CRC16.compute(data, addr); lastcrc = CRC16.compute(ib.adapter.getByte(), lastcrc); if (CRC16.compute(ib.adapter.getByte(), lastcrc) != 0x0000B001) { forceVerify(); throw new OneWireIOException("Invalid CRC16 read from device"); } } else { lastcrc = CRC8.compute(data, addr); if (CRC8.compute(ib.adapter.getByte(), lastcrc) != 0) { forceVerify(); throw new OneWireIOException("Invalid CRC8 read from device"); } } } // send the pulse ib.adapter.startProgramPulse(DSPortAdapter.CONDITION_NOW); // return the result return ( byte ) ib.adapter.getByte(); } //-------- //-------- checkSpeed methods //-------- /** * Check the device speed if has not been done before or if * an error was detected. * * @throws OneWireIOException * @throws OneWireException */ public void checkSpeed () throws OneWireIOException, OneWireException { synchronized (this) { // only check the speed if (doSetSpeed) { // attempt to set the correct speed and verify device present ib.doSpeed(); // no execptions so clear flag doSetSpeed = false; } } } /** * Set the flag to indicate the next 'checkSpeed()' will force * a speed set and verify 'doSpeed()'. */ public void forceVerify () { synchronized (this) { doSetSpeed = true; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -