private Byte[] RegisterReadArray(LGWRegisters register, UInt16 size) { if (register.RegisterPage != -1 && register.RegisterPage != this._selectedPage) // select proper register page if needed { this.PageSwitch((Byte)register.RegisterPage); } return(this.SPIreadRegisterBurst((Byte)(0x00 | (register.Address & 0x7F)), size)); }
private void RegisterWriteArray(LGWRegisters register, Byte[] value) { if (register.ReadonlyRegister) { throw new ArgumentException("Register is a readonly register, you cant write!", register.GetType().ToString()); } if (register.RegisterPage != -1 && register.RegisterPage != this._selectedPage) // select proper register page if needed { this.PageSwitch((Byte)register.RegisterPage); } this.SPIwriteRegisterBurstRaw((Byte)(0x80 | (register.Address & 0x7F)), value); // do the burst write }
private void RegisterWrite(LGWRegisters register, Int32 value, Byte mux = 0) { if (register.Equals(Registers.PAGE_REG)) { this.PageSwitch((Byte)value); return; } else if (register.Equals(Registers.SOFT_RESET)) { if ((value & 0x01) != 0) { this.SPIwriteRegisterRaw((Byte)(0x80 | (Registers.SOFT_RESET.Address & 0x7F)), 0x80, mux); } return; } if (register.ReadonlyRegister) { throw new ArgumentException("Register is a readonly register, you cant write!", register.GetType().ToString()); } if (register.RegisterPage != -1 && register.RegisterPage != this._selectedPage) { this.PageSwitch((Byte)register.RegisterPage); } Byte[] buf = new Byte[4]; if (register.SizeInBits == 8 && register.BitOffset == 0) { this.SPIwriteRegisterRaw((Byte)(0x80 | (register.Address & 0x7F)), (Byte)value, mux); } else if (register.BitOffset + register.SizeInBits <= 8) // single-byte read-modify-write, offs:[0-7], leng:[1-7] { buf[0] = this.SPIreadRegister((Byte)(0x00 | (register.Address & 0x7F)), mux); buf[1] = (Byte)(((1 << register.SizeInBits) - 1) << register.BitOffset); // bit mask buf[2] = (Byte)(((Byte)value) << register.BitOffset); // new data offsetted buf[3] = (Byte)((~buf[1] & buf[0]) | (buf[1] & buf[2])); // mixing old & new data this.SPIwriteRegisterRaw((Byte)(0x80 | (register.Address & 0x7F)), buf[3], mux); } else if (register.BitOffset == 0 && register.SizeInBits > 0 && register.SizeInBits <= 32) // multi-byte direct write routine { Byte size = (Byte)((register.SizeInBits + 7) / 8); // add a byte if it's not an exact multiple of 8 Byte[] mbuf = new Byte[size]; for (Byte i = 0; i < size; ++i) // big endian register file for a file on N bytes Least significant byte is stored in buf[0], most one in buf[N - 1] { mbuf[i] = (Byte)(0x000000FF & value); value >>= 8; } this.SPIwriteRegisterBurstRaw((Byte)(0x80 | (register.Address & 0x7F)), mbuf, mux); // write the register in one burst } else { throw new ArgumentException("Register spanning multiple memory bytes but with an offset!", register.GetType().ToString()); } }
private Int32 RegisterRead(LGWRegisters register, Byte mux = 0) { if (register.RegisterPage != -1 && register.RegisterPage != this._selectedPage) { this.PageSwitch((Byte)register.RegisterPage); } Byte[] bufu = new Byte[4]; SByte[] bufs = new SByte[4]; if (register.BitOffset + register.SizeInBits <= 8) // read one byte, then shift and mask bits to get reg value with sign extension if needed { bufu[0] = this.SPIreadRegister((Byte)(0x00 | (register.Address & 0x7F)), mux); bufu[1] = (Byte)(bufu[0] << (8 - register.SizeInBits - register.BitOffset)); // left-align the data if (register.SignedInt == true) { bufs[2] = (SByte)(bufs[1] >> (8 - register.SizeInBits)); // right align the data with sign extension (ARITHMETIC right shift) return(bufs[2]); // signed pointer -> 32b sign extension } else { bufu[2] = (Byte)(bufu[1] >> (8 - register.SizeInBits)); // right align the data, no sign extension return(bufu[2]); // unsigned pointer -> no sign extension } } else if (register.BitOffset == 0 && register.SizeInBits > 0 && register.SizeInBits <= 32) { Byte size = (Byte)((register.SizeInBits + 7) / 8); // add a byte if it's not an exact multiple of 8 bufu = this.SPIreadRegisterBurst((Byte)(0x00 | (register.Address & 0x7F)), size, mux); UInt32 u = 0; for (SByte i = (SByte)(size - 1); i >= 0; --i) { u = bufu[i] + (u << 8); // transform a 4-byte array into a 32 bit word } if (register.SignedInt == true) { u <<= 32 - register.SizeInBits; // left-align the data return((Int32)u >> (32 - register.SizeInBits)); // right-align the data with sign extension (ARITHMETIC right shift) } else { return((Int32)u); // unsigned value -> return 'as is' } } else // register spanning multiple memory bytes but with an offset { throw new ArgumentException("Register size and offset are not supported!", register.GetType().ToString()); } }