📄 memorybankad.java
字号:
// loop to read the pages for (int pg = start_pg; pg <= end_pg; pg++) readPageCRC(pg, !(pg == start_pg), raw_buf, (pg - start_pg) * PAGE_LENGTH); // extract out the data System.arraycopy(raw_buf, (startAddr % PAGE_LENGTH), 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 { byte[] raw_buf = new byte [7]; int cnt = 0, start_addr, addr, end_addr, lastcrc; // return if nothing to do if (len == 0) return; // attempt to put device at max desired speed checkSpeed(); // select the device if (!ib.adapter.select(ib.address)) { forceVerify(); throw new OneWireIOException("Device select failed"); } // build packet raw_buf [cnt++] = WRITE_MEMORY_COMMAND; start_addr = startAddr + startPhysicalAddress; end_addr = start_addr + len; raw_buf [cnt++] = ( byte ) (start_addr & 0xFF); raw_buf [cnt++] = ( byte ) (((start_addr & 0xFFFF) >>> 8) & 0xFF); // loop for each byte to write for (addr = start_addr; addr < end_addr; addr++) { // add the byte to write to buffer raw_buf [cnt++] = writeBuf [offset + addr - start_addr]; // initialize crc16 lastcrc = CRC16.compute(raw_buf, 0, cnt, (addr == start_addr) ? 0 : addr); // add the read crc and echo byte to block System.arraycopy(ffBlock, 0, raw_buf, cnt, 3); // perform the block ib.adapter.dataBlock(raw_buf, 0, cnt + 3); // check the CRC if (CRC16.compute(raw_buf, cnt, 2, lastcrc) != 0x0000B001) { forceVerify(); throw new OneWireIOException("Invalid CRC16 read from device"); } // check echo if (raw_buf [cnt + 2] != writeBuf [offset + addr - start_addr]) { forceVerify(); throw new OneWireIOException("Write byte echo was invalid"); } // reset the buffer and loop cnt = 0; } } //-------- //-------- 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 with read page CRC readPageCRC(page, readContinue, readBuf, offset); } /** * 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 { // only needs to be implemented if supported by hardware throw new OneWireException( "Read page with extra-info not supported by this memory bank"); } /** * Read a Universal Data Packet. * * The Universal Data Packet always starts on page boundaries but * can end anywhere in the page. The structure specifies the length of * data bytes not including the length byte and the CRC16 bytes. * There is one length byte. The CRC16 is first initialized to * the page number. This provides a check to verify the page that * was intended is being read. The CRC16 is then calculated over * the length and data bytes. The CRC16 is then inverted and stored * low byte first followed by the high byte. This is structure is * used by this method to verify the data but is not returned, only * the data payload is returned. * * @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 read from the device and 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_LENGTH]; // read entire page with read page CRC readPageCRC(page, readContinue, raw_buf, 0); // check if length is realistic if (raw_buf [0] > (PAGE_LENGTH - 3)) 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 * @param extraInfo byte array to put extra info read into * * @return number of data bytes read from the device and 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 { // only needs to be implemented if supported by hardware throw new OneWireException( "Read page packet with extra-info not supported by this memory bank"); } /** * 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_LENGTH - 3) 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_LENGTH, 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 { byte[] raw_buf = new byte [5 + PAGE_LENGTH]; int len; // attempt to put device at max desired speed if (!readContinue) checkSpeed(); // 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 len = raw_buf.length; System.arraycopy(ffBlock, 0, raw_buf, 0, len); raw_buf [0] = READ_MEMORY_COMMAND; int addr = page * PAGE_LENGTH + startPhysicalAddress; raw_buf [1] = ( byte ) (addr & 0xFF); raw_buf [2] = ( byte ) (((addr & 0xFFFF) >>> 8) & 0xFF); } else { len = PAGE_LENGTH + 2; System.arraycopy(ffBlock, 0, raw_buf, 0, len); } // do the block ib.adapter.dataBlock(raw_buf, 0, len); // check the CRC if (CRC16.compute(raw_buf, 0, len, 0) != 0x0000B001) { forceVerify(); throw new OneWireIOException("Invalid CRC16 read from device"); } // extract the data to return System.arraycopy(raw_buf, len - 2 - PAGE_LENGTH, readBuf, offset, PAGE_LENGTH); } /** * 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 and extra-info not supported by this memory bank"); } //-------- //-------- 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 + -