Initial check in
This commit is contained in:
@@ -0,0 +1,185 @@
|
||||
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.
|
||||
@@ -0,0 +1,64 @@
|
||||
unit m.iic.types;
|
||||
|
||||
interface
|
||||
type
|
||||
TIICCommand =
|
||||
(
|
||||
IIC_READ = $80,
|
||||
IIC_READ_RSW = $81,
|
||||
IIC_READ_RSB = $81,
|
||||
|
||||
IIC_WRITE = $00
|
||||
);
|
||||
|
||||
TIICPacketHead = packed record
|
||||
command : TIICCommand;
|
||||
address : WORD;
|
||||
subaddr : WORD;
|
||||
length : BYTE;
|
||||
reserved : BYTE;
|
||||
timeout : BYTE;
|
||||
end;
|
||||
|
||||
TIICPacket = packed record
|
||||
head : TIICPacketHead;
|
||||
data : array [0..31] of BYTE;
|
||||
end;
|
||||
|
||||
TIICStatus =
|
||||
(
|
||||
IIC_OK = 0, // IIC operation successfull
|
||||
IIC_TMO = 1, // Timeout
|
||||
IIC_ARB = 4, // bus arbitration error
|
||||
IIC_NAK = 5, // no ACK from slave
|
||||
|
||||
IIC_EADR = 90, // Invalid address
|
||||
IIC_ELEN = 91 // Invalid length
|
||||
);
|
||||
|
||||
IIIC = interface
|
||||
['{7D675CBC-2642-4DBD-B8FC-AEE5C3864E0E}']
|
||||
function Wait( addr : word;
|
||||
tmo : word): TIICStatus;
|
||||
|
||||
function Write( addr : word;
|
||||
count : byte;
|
||||
buffer : pointer;
|
||||
tmo : word): TIICStatus;
|
||||
|
||||
function Read( addr : word;
|
||||
count : byte;
|
||||
buffer : pointer;
|
||||
tmo : word): TIICStatus;
|
||||
|
||||
function ReadRSW( addr : word;
|
||||
subaddr : word;
|
||||
count : byte;
|
||||
buffer : pointer;
|
||||
tmo : word): TIICStatus;
|
||||
|
||||
end;
|
||||
|
||||
implementation
|
||||
|
||||
end.
|
||||
Reference in New Issue
Block a user