186 lines
5.1 KiB
ObjectPascal
186 lines
5.1 KiB
ObjectPascal
unit m.iic;
|
|
|
|
interface
|
|
uses
|
|
m.base,
|
|
m.iic.types;
|
|
|
|
type
|
|
TmodIIC = class( TmodBase, IIIC)
|
|
protected
|
|
function Wait( addr : word;
|
|
tmo : word = 100): TIICStatus;
|
|
|
|
function Write( addr : word;
|
|
count : byte;
|
|
buffer : pointer;
|
|
tmo : word = 100): TIICStatus;
|
|
|
|
function Read( addr : word;
|
|
count : byte;
|
|
buffer : pointer;
|
|
tmo : word = 100): TIICStatus;
|
|
|
|
function ReadRSW( addr : word;
|
|
subaddr : word;
|
|
count : byte;
|
|
buffer : pointer;
|
|
tmo : word = 100): TIICStatus;
|
|
end;
|
|
|
|
|
|
implementation
|
|
uses
|
|
windows, math,
|
|
mr.dev.usb.pipe;
|
|
|
|
|
|
function CyWORD( value: word): word; overload;
|
|
begin
|
|
result := (LoByte(value) shl 8) or HiByte(value);
|
|
end;
|
|
|
|
function CyWORD( value: byte): word; overload;
|
|
begin
|
|
result := value shl 8;
|
|
end;
|
|
|
|
|
|
|
|
{ TmodIIC }
|
|
|
|
// @@@: IIIC ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
//
|
|
// IIIC
|
|
//
|
|
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// ================================================================================================
|
|
// wait
|
|
// ================================================================================================
|
|
function TmodIIC.Wait(addr: word; tmo: word): TIICStatus;
|
|
begin
|
|
result := IIC_TMO;
|
|
|
|
end;
|
|
|
|
// ================================================================================================
|
|
// write
|
|
// ================================================================================================
|
|
function TmodIIC.Write(addr: word; count: byte; buffer: pointer; tmo: word): TIICStatus;
|
|
var
|
|
buf: TIICPacket;
|
|
len: cardinal;
|
|
|
|
p01: TPipe;
|
|
p81: TPipe;
|
|
|
|
begin
|
|
result := IIC_TMO;
|
|
|
|
if Assigned(fDevice) then
|
|
begin
|
|
p01 := fDevice.Pipes[$01];
|
|
p81 := fDevice.Pipes[$81];
|
|
|
|
if Assigned(p01) and Assigned(p81) then
|
|
begin
|
|
FillChar( buf, sizeof(buf), 0);
|
|
|
|
buf.head.command := IIC_WRITE;
|
|
buf.head.timeout := tmo;
|
|
buf.head.address := CyWORD(addr);
|
|
buf.head.length := min(count, 32);
|
|
|
|
MoveMemory(@buf.data[0], buffer, buf.head.length);
|
|
|
|
p01.Write( buf, sizeof( TIICPacket), len);
|
|
p81.Read( buf, sizeof( TIICPacketHead), len)
|
|
end
|
|
end
|
|
end;
|
|
|
|
// ================================================================================================
|
|
// read
|
|
// ================================================================================================
|
|
function TmodIIC.Read(addr: word; count: byte; buffer: pointer; tmo: word): TIICStatus;
|
|
var
|
|
buf: TIICPacket;
|
|
len: cardinal;
|
|
|
|
p01: TPipe;
|
|
p81: TPipe;
|
|
|
|
begin
|
|
result := IIC_TMO;
|
|
|
|
if Assigned(fDevice) then
|
|
begin
|
|
p01 := fDevice.Pipes[$01];
|
|
p81 := fDevice.Pipes[$81];
|
|
|
|
if Assigned(p01) and Assigned(p81) then
|
|
begin
|
|
FillChar( buf, sizeof(buf), 0);
|
|
|
|
buf.head.command := IIC_READ;
|
|
buf.head.timeout := tmo;
|
|
buf.head.address := CyWORD(addr);
|
|
buf.head.length := min(count, 32);
|
|
|
|
if p01.Write( buf, sizeof( TIICPacketHead), len) then
|
|
begin
|
|
p81.Read( buf, sizeof( TIICPacket), len);
|
|
|
|
result := TIICStatus(buf.head.timeout);
|
|
|
|
if result = IIC_OK then
|
|
MoveMemory(buffer, @buf.data[0], buf.head.length)
|
|
end;
|
|
|
|
end
|
|
end
|
|
end;
|
|
|
|
// ================================================================================================
|
|
// read with repeated start condition, 16 bit sub address
|
|
// ================================================================================================
|
|
function TmodIIC.ReadRSW(addr: word; subaddr: word; count: byte; buffer: pointer; tmo: word): TIICStatus;
|
|
var
|
|
buf: TIICPacket;
|
|
len: cardinal;
|
|
|
|
p01: TPipe;
|
|
p81: TPipe;
|
|
|
|
begin
|
|
result := IIC_TMO;
|
|
|
|
if Assigned(fDevice) then
|
|
begin
|
|
p01 := fDevice.Pipes[$01];
|
|
p81 := fDevice.Pipes[$81];
|
|
|
|
if Assigned(p01) and Assigned(p81) then
|
|
begin
|
|
FillChar( buf, sizeof(buf), 0);
|
|
|
|
buf.head.command := IIC_READ_RSW;
|
|
buf.head.timeout := tmo;
|
|
buf.head.address := CyWORD(addr);
|
|
buf.head.subaddr := CyWORD(subaddr);
|
|
buf.head.length := min(count, 32);
|
|
|
|
p01.Write( buf, sizeof( TIICPacketHead), len);
|
|
p81.Read( buf, sizeof( TIICPacket), len);
|
|
|
|
MoveMemory(buffer, @buf.data[0], buf.head.length)
|
|
end
|
|
end
|
|
end;
|
|
|
|
end.
|