Exemplo n.º 1
0
        /// <summary> 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.
        ///
        /// </summary>
        /// <param name="startAddr">    starting address, relative to the starting physical address
        /// of this memory bank
        /// </param>
        /// <param name="writeBuf">     byte array containing data to write
        /// </param>
        /// <param name="offset">       offset into writeBuf to get data
        /// </param>
        /// <param name="len">          length in bytes to write
        ///
        /// </param>
        /// <throws>  OneWireIOException </throws>
        /// <throws>  OneWireException </throws>
        public virtual void  write(int startAddr, byte[] writeBuf, int offset, int len)
        {
            //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
            if (DEBUG)
            {
                Debug.debug("-----------------------------------------------------------");
                Debug.debug("MemoryBankSHAEE.write(int,byte[],int,int) called");
                Debug.debug("  startAddr=0x" + Convert.toHexString((byte)startAddr));
                Debug.debug("  writeBuf", writeBuf, offset, len);
                Debug.debug("  startPhysicalAddress=0x" + Convert.toHexString((byte)startPhysicalAddress));
            }
            //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
            int room_left;

            if (!checked_Renamed)
            {
                checked_Renamed = ib.checkStatus();
            }

            // return if nothing to do
            if (len == 0)
            {
                return;
            }

            // attempt to put device at speed
            checkSpeed();

            // check to see if secret is set
            if (!ib.ContainerSecretSet)
            {
                throw new OneWireException("Secret is not set.");
            }

            // 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 (ReadOnly)
            {
                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[(size - (startAddr & 0xE0))];           // till end of memory
            int    abs_addr = startPhysicalAddress + startAddr;
            int    pl       = 8;

            read(startAddr & 0xE0, false, memory, 0, memory.Length);

            if (abs_addr >= 128)
            {
                ib.getContainerSecret(memory, 0);
            }

            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;
                }

                // bug fix, if updating pages two and three in the same write op
                // this used to fail, was (startAddr>=pageLength)
                if ((startx + startAddr) >= pageLength)
                {
                    Array.Copy(memory, (((startx + startAddr) / 8) * 8) - 32, raw_buf, 0, 8);
                }
                else
                {
                    Array.Copy(memory, (((startx + startAddr) / 8) * 8), raw_buf, 0, 8);
                }

                if ((nextx - startx) == 8)
                {
                    Array.Copy(writeBuf, offset + startx, raw_buf, 0, 8);
                }
                else if (((startAddr + nextx) % 8) == 0)
                {
                    Array.Copy(writeBuf, offset + startx, raw_buf, ((startAddr + startx) % 8), 8 - ((startAddr + startx) % 8));
                }
                else
                {
                    Array.Copy(writeBuf, offset + startx, raw_buf, ((startAddr + startx) % 8), ((startAddr + nextx) % 8) - ((startAddr + startx) % 8));
                }

                // write the page of data to scratchpad
                scratchpad.writeScratchpad(abs_addr + startx + room_left - 8, raw_buf, 0, 8);

                // Copy data from scratchpad into memory
                scratchpad.copyScratchpad(abs_addr + startx + room_left - 8, raw_buf, 0, memory, 0);

                // bug fix, if updating pages two and three in the same write op
                // this used to fail, was (startAddr>=pageLength)
                if ((startx + startAddr) >= pageLength)
                {
                    Array.Copy(raw_buf, 0, memory, (((startx + startAddr) / 8) * 8) - 32, 8);
                }
                else
                {
                    Array.Copy(raw_buf, 0, memory, (((startx + startAddr) / 8) * 8), 8);
                }

                // point to next index
                startx = nextx;
            }while (nextx < len);

            //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
            if (DEBUG)
            {
                Debug.debug("-----------------------------------------------------------");
            }
            //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
        }
Exemplo n.º 2
0
        //--------
        //-------- MemoryBank I/O methods
        //--------

        /// <summary> 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.
        ///
        /// </summary>
        /// <param name="startAddr">    starting address, relative to physical address for
        /// this memory bank.
        /// </param>
        /// <param name="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>
        /// <param name="readBuf">      byte array to place read data into
        /// </param>
        /// <param name="offset">       offset into readBuf to place data
        /// </param>
        /// <param name="len">          length in bytes to read
        ///
        /// </param>
        /// <throws>  OneWireIOException </throws>
        /// <throws>  OneWireException </throws>
        public virtual void  read(int startAddr, bool readContinue, byte[] readBuf, int offset, int len)
        {
            //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
            if (DEBUG)
            {
                Debug.debug("-----------------------------------------------------------");
                Debug.debug("MemoryBankSHAEE.read(int, boolean, byte[], int, int) called");
                Debug.debug("  startAddr=0x" + Convert.toHexString((byte)startAddr));
                Debug.debug("  readContinue=" + readContinue);
                Debug.debug("  offset=" + offset);
                Debug.debug("  len=" + len);
                Debug.debug("  this.startPhysicalAddress=0x" + Convert.toHexString((byte)startPhysicalAddress));
                Debug.debug("  this.pageLength=" + this.pageLength);
                Debug.debug("  this.numberPages=" + this.numberPages);
                Debug.stackTrace();
            }
            //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//

            // attempt to put device at max desired speed
            if (!readContinue && ib.adapterSet())
            {
                checkSpeed();
            }

            // check if read exceeds memory
            if ((startAddr + len) > (pageLength * numberPages))
            {
                throw new OneWireException("Read exceeds memory bank end.");
            }

            // see if need to access the device
            if (!readContinue)
            {
                // select the device
                if (!adapter.select(ib.Address))
                {
                    throw new OneWireIOException("Device select failed.");
                }

                // build start reading memory block
                int    addr    = startAddr + startPhysicalAddress;
                byte[] raw_buf = new byte[3];

                raw_buf[0] = READ_MEMORY;
                raw_buf[1] = (byte)(addr & 0xFF);
                raw_buf[2] = (byte)((SupportClass.URShift((addr & 0xFFFF), 8)) & 0xFF);

                // do the first block for command, address
                adapter.dataBlock(raw_buf, 0, 3);
                //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
                if (DEBUG)
                {
                    Debug.debug("  raw_buf", raw_buf, 0, 3);
                }
                //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
            }

            // pre-fill readBuf with 0xFF
            int pgs   = len / pageLength;
            int extra = len % pageLength;

            for (int i = 0; i < pgs; i++)
            {
                Array.Copy(ffBlock, 0, readBuf, offset + i * pageLength, pageLength);
            }
            Array.Copy(ffBlock, 0, readBuf, offset + pgs * pageLength, extra);

            // send second block to read data, return result
            adapter.dataBlock(readBuf, offset, len);

            //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
            if (DEBUG)
            {
                Debug.debug("  readBuf", readBuf, offset, len);
                Debug.debug("-----------------------------------------------------------");
            }
            //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
        }