⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ft2232ci2cunit.pas

📁 Delphi源码,实现I2C接口,非常适合硬件开发人员
💻 PAS
字号:
unit FT2232CI2CUnit;

interface

uses
  Windows;

type FTC_STATUS = Integer;

const MAX_NUM_DEVICE_NAME_CHARS = 100;

type TDeviceName = array [0..(MAX_NUM_DEVICE_NAME_CHARS - 1)] of Char;
type PDeviceName = ^TDeviceName;

const MAX_WRITE_CONTROL_BYTES_BUFFER_SIZE = 255;    // 256 bytes

type WriteControlByteBuffer = array[0..(MAX_WRITE_CONTROL_BYTES_BUFFER_SIZE - 1)] of Byte;
type PWriteControlByteBuffer = ^WriteControlByteBuffer;

type FtcPageWriteData = record
  dwNumPages: Dword;
  dwNumBytesPerPage: Dword;
end;

type PFtcPageWriteData = ^FtcPageWriteData;

const MAX_WRITE_DATA_BYTES_BUFFER_SIZE = 65535;    // 64k bytes

type WriteDataByteBuffer = array[0..(MAX_WRITE_DATA_BYTES_BUFFER_SIZE - 1)] of Byte;
type PWriteDataByteBuffer = ^WriteDataByteBuffer;

const MAX_READ_DATA_BYTES_BUFFER_SIZE = 65535;    // 64k bytes

type ReadDataByteBuffer = array[0..(MAX_READ_DATA_BYTES_BUFFER_SIZE - 1)] of Byte;
type PReadDataByteBuffer = ^ReadDataByteBuffer;

const MAX_NUM_DLL_VERSION_CHARS = 10;

type TDllVersion = array [0..(MAX_NUM_DLL_VERSION_CHARS - 1)] of Char;
type PDllVersion = ^TDllVersion;

const MAX_NUM_ERROR_MESSAGE_CHARS = 100;

type TErrorMessage = array [0..(MAX_NUM_ERROR_MESSAGE_CHARS - 1)] of Char;
type PErrorMessage = ^TErrorMessage;

type TFT2232CI2C = class
 public
  constructor create;
  function  GetNumDevices(pNumDevices: PDword): FTC_STATUS;
  function  GetDeviceNameLocID(DeviceNameIndex: Dword; pDeviceNameBuffer: PDeviceName; BufferSize: Dword; pLocationID: PDword): FTC_STATUS;
  function  OpenEx(DeviceName: String; LocationID: Dword; pFtHandle: PDword): FTC_STATUS;
  function  Open(pFtHandle: PDword): FTC_STATUS;
  function  Close(fthandle: Dword): FTC_STATUS;
  function  InitDevice(fthandle: Dword; ClockDivisor: Dword): FTC_STATUS;
  function  GetClock(ClockDivisor: Dword; pClockFrequencyHz: PDword): FTC_STATUS;
  function  SetClock(fthandle: Dword; ClockDivisor: Dword; pClockFrequencyHz: PDword): FTC_STATUS;
  function  SetLoopback(fthandle: Dword; bLoopbackState: LongBool): FTC_STATUS;
  function  SetMode(fthandle: Dword; CommsMode: Dword): FTC_STATUS;
  function  Write(fthandle: Dword; pWriteControlBuffer: PWriteControlByteBuffer; NumControlBytesToWrite: Dword; bControlAcknowledge: LongBool; ControlAckTimeoutSecs: Dword; bStopCondition: LongBool; DataWriteTypes: Dword; pWriteDataBuffer: PWriteDataByteBuffer; NumDataBytesToWrite: Dword; bDataAcknowledge: LongBool; DataAckTimeoutSecs: Dword; pPageWriteData: PFtcPageWriteData): FTC_STATUS;
  function  Read(fthandle: Dword; pWriteControlBuffer: PWriteControlByteBuffer; NumControlBytesToWrite: Dword; bControlAcknowledge: LongBool; ControlAckTimeoutSecs: Dword; DataReadTypes: Dword; pReadDataBuffer: PReadDataByteBuffer; NumDataBytesToRead: Dword): FTC_STATUS;
  function  GetDllVersion(DllVersionBuffer: pDllVersion; BufferSize: Dword): FTC_STATUS;
  function  GetErrorCodeString(Language: String; StatusCode: FTC_STATUS; pErrorMessageBuffer: PErrorMessage; BufferSize: Dword): FTC_STATUS;
end;


const
  FT2232CI2C_DLL_Name = 'ftci2c.dll';

  NO_WRITE_TYPE = 0;
  BYTE_WRITE_TYPE = 1;
  PAGE_WRITE_TYPE = 2;

  BYTE_READ_TYPE = 1;
  BLOCK_READ_TYPE = 2;

  STANDARD_MODE = 1;
  FAST_MODE = 2;

  FTC_SUCCESS = 0;

  ENGLISH = 'EN';

  
implementation

function I2C_GetNumDevices(pNumDevices: PDword): FTC_STATUS; stdcall ; External FT2232CI2C_DLL_Name name 'I2C_GetNumDevices';
function I2C_GetDeviceNameLocID(DeviceNameIndex: Dword; pDeviceNameBuffer: PDeviceName; BufferSize: Dword; pLocationID: PDword): FTC_STATUS; stdcall ; External FT2232CI2C_DLL_Name name 'I2C_GetDeviceNameLocID';
function I2C_OpenEx(DeviceName: String; LocationID: Dword; pFtHandle: PDword): FTC_STATUS; stdcall ; External FT2232CI2C_DLL_Name name 'I2C_OpenEx';
function I2C_Open(pFtHandle: PDword): FTC_STATUS; stdcall ; External FT2232CI2C_DLL_Name name 'I2C_Open';
function I2C_Close(fthandle: Dword): FTC_STATUS; stdcall ; External FT2232CI2C_DLL_Name name 'I2C_Close';
function I2C_InitDevice(fthandle: Dword; ClockFrequencyValue: Dword): FTC_STATUS; stdcall ; External FT2232CI2C_DLL_Name name 'I2C_InitDevice';
function I2C_GetClock(ClockDivisor: Dword; pClockFrequencyHz: PDword): FTC_STATUS; stdcall ; External FT2232CI2C_DLL_Name name 'I2C_GetClock';
function I2C_SetClock(fthandle: Dword; ClockDivisor: Dword; pClockFrequencyHz: PDword): FTC_STATUS; stdcall ; External FT2232CI2C_DLL_Name name 'I2C_SetClock';
function I2C_SetLoopback(fthandle: Dword; bLoopbackState: LongBool): FTC_STATUS; stdcall ; External FT2232CI2C_DLL_Name name 'I2C_SetLoopback';
function I2C_SetMode(fthandle: Dword; CommsMode: Dword): FTC_STATUS;  stdcall ; External FT2232CI2C_DLL_Name name 'I2C_SetMode';
function I2C_Write(fthandle: Dword; pWriteControlBuffer: PWriteControlByteBuffer; NumControlBytesToWrite: Dword; bControlAcknowledge: LongBool; ControlAckTimeoutSecs: Dword; bStopCondition: LongBool; DataWriteTypes: Dword; pWriteDataBuffer: PWriteDataByteBuffer; NumDataBytesToWrite: Dword; bDataAcknowledge: LongBool; DataAckTimeoutSecs: Dword; pPageWriteData: PFtcPageWriteData): FTC_STATUS; stdcall ; External FT2232CI2C_DLL_Name name 'I2C_Write';
function I2C_Read(fthandle: Dword; pWriteControlBuffer: PWriteControlByteBuffer; NumControlBytesToWrite: Dword; bControlAcknowledge: LongBool; ControlAckTimeoutSecs: Dword; DataReadTypes: Dword; pReadDataBuffer: PReadDataByteBuffer; NumDataBytesToRead: Dword): FTC_STATUS; stdcall ; External FT2232CI2C_DLL_Name name 'I2C_Read';
function I2C_GetDllVersion(DllVersionBuffer: pDllVersion; BufferSize: Dword): FTC_STATUS; stdcall ; External FT2232CI2C_DLL_Name name 'I2C_GetDllVersion';
function I2C_GetErrorCodeString(Language: PChar; StatusCode: FTC_STATUS; ErrorMessageBuffer: PErrorMessage; BufferSize: Dword): FTC_STATUS; stdcall ; External FT2232CI2C_DLL_Name name 'I2C_GetErrorCodeString';


constructor TFT2232CI2C.create;
begin
end;

function  TFT2232CI2C.GetNumDevices(pNumDevices: PDword): FTC_STATUS;
begin
  Result := I2C_GetNumDevices(pNumDevices);
end;

function  TFT2232CI2C.GetDeviceNameLocID(DeviceNameIndex: Dword; pDeviceNameBuffer: PDeviceName;
                                               BufferSize: Dword; pLocationID: PDword): FTC_STATUS;
begin
  Result := I2C_GetDeviceNameLocID(DeviceNameIndex, pDeviceNameBuffer, BufferSize, pLocationID);
end;

function  TFT2232CI2C.OpenEx(DeviceName: String; LocationID: Dword; pFtHandle: PDword): FTC_STATUS;
begin
  Result := I2C_OpenEx(DeviceName, LocationID, pFtHandle);
end;

function  TFT2232CI2C.Open(pFtHandle: PDword): FTC_STATUS;
begin
  Result := I2C_Open(pFtHandle);
end;

function  TFT2232CI2C.Close(fthandle: Dword): FTC_STATUS;
begin
  Result := I2C_Close(fthandle);
end;

function  TFT2232CI2C.InitDevice(fthandle: Dword; ClockDivisor: Dword): FTC_STATUS;
begin
  Result := I2C_InitDevice(fthandle, ClockDivisor);
end;

function  TFT2232CI2C.GetClock(ClockDivisor: Dword; pClockFrequencyHz: PDword): FTC_STATUS;
begin
  Result := I2C_GetClock(ClockDivisor, pClockFrequencyHz);
end;

function  TFT2232CI2C.SetClock(fthandle: Dword; ClockDivisor: Dword; pClockFrequencyHz: PDword): FTC_STATUS;
begin
  Result :=  I2C_SetClock(fthandle, ClockDivisor, pClockFrequencyHz);
end;

function  TFT2232CI2C.SetLoopback(fthandle: Dword; bLoopbackState: LongBool): FTC_STATUS;
begin
  Result := I2C_SetLoopback(fthandle, bLoopbackState);
end;

function  TFT2232CI2C.SetMode(fthandle: Dword; CommsMode: Dword): FTC_STATUS;
begin
  Result := I2C_SetMode(fthandle, CommsMode);
end;

function  TFT2232CI2C.Write(fthandle: Dword; pWriteControlBuffer: PWriteControlByteBuffer; NumControlBytesToWrite: Dword;
                            bControlAcknowledge: LongBool; ControlAckTimeoutSecs: Dword;
                            bStopCondition: LongBool; DataWriteTypes: Dword;
                            pWriteDataBuffer: PWriteDataByteBuffer; NumDataBytesToWrite: Dword;
                            bDataAcknowledge: LongBool; DataAckTimeoutSecs: Dword;
                            pPageWriteData: PFtcPageWriteData): FTC_STATUS;
begin
  Result := I2C_Write(fthandle, pWriteControlBuffer, NumControlBytesToWrite, bControlAcknowledge, ControlAckTimeoutSecs, bStopCondition, DataWriteTypes, pWriteDataBuffer, NumDataBytesToWrite, bDataAcknowledge, DataAckTimeoutSecs, pPageWriteData);
end;

function  TFT2232CI2C.Read(fthandle: Dword; pWriteControlBuffer: PWriteControlByteBuffer;
                           NumControlBytesToWrite: Dword; bControlAcknowledge: LongBool;
                           ControlAckTimeoutSecs: Dword; DataReadTypes: Dword;
                           pReadDataBuffer: PReadDataByteBuffer; NumDataBytesToRead: Dword): FTC_STATUS;
begin
  Result := I2C_Read(fthandle, pWriteControlBuffer, NumControlBytesToWrite, bControlAcknowledge, ControlAckTimeoutSecs, DataReadTypes, pReadDataBuffer, NumDataBytesToRead);
end;

function  TFT2232CI2C.GetDllVersion(DllVersionBuffer: pDllVersion; BufferSize: Dword): FTC_STATUS;
begin
  Result := I2C_GetDllVersion(DllVersionBuffer, BufferSize);
end;

function  TFT2232CI2C.GetErrorCodeString(Language: String; StatusCode: FTC_STATUS; pErrorMessageBuffer: PErrorMessage; BufferSize: Dword): FTC_STATUS;
begin
  Result := I2C_GetErrorCodeString(PChar(Language), StatusCode, pErrorMessageBuffer, BufferSize);
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -