Skip to content
Snippets Groups Projects
Commit 673869c4 authored by mfenner's avatar mfenner
Browse files

External I2C Programming Routines added

parent 76eb2b97
No related branches found
No related tags found
No related merge requests found
# Project: Project1
# Makefile created by Dev-C++ 4.9.9.2
CPP = g++.exe -D__DEBUG__
CC = gcc.exe -D__DEBUG__
WINDRES = windres.exe
RES =
OBJ = main.o $(RES)
LINKOBJ = main.o $(RES)
LIBS = -L"C:/Dev-Cpp/lib" libMPSSE.a libMPSSE.a -g3 -fmessage-length=0
INCS = -I"C:/Dev-Cpp/include"
CXXINCS = -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"
BIN = proghyt.exe
CXXFLAGS = $(CXXINCS) -traditional-cpp -fno-access-control -g3 -fmessage-length=0
CFLAGS = $(INCS) -traditional-cpp -fno-access-control -g3 -fmessage-length=0
RM = rm -f
.PHONY: all all-before all-after clean clean-custom
all: all-before proghyt.exe all-after
clean: clean-custom
${RM} $(OBJ) $(BIN)
$(BIN): $(OBJ)
$(CPP) $(LINKOBJ) -o "proghyt.exe" $(LIBS)
main.o: main.c
$(CPP) -c main.c -o main.o $(CXXFLAGS)
/*!
* \file sample-dynamic.c
*
* \author FTDI
* \date 20110512
*
* Copyright 2011 Future Technology Devices International Limited
* Company Confidential
*
* Project: libMPSSE
* Module: I2C Sample Application - Interfacing 24LC024H I2C EEPROM
*
* Rivision History:
* 0.1 - 20110513 - initial version
* 0.2 - 20110801 - Changed LatencyTimer to 255
* Attempt to open channel only if available
* Added & modified macros
* Change APIs I2C_GetChannelInfo & OpenChannel to start indexing from 0
* 0.3 - 20111212 - Added comments
*/
/******************************************************************************/
/* Include files */
/******************************************************************************/
/* Standard C libraries */
#include<stdio.h>
#include<stdlib.h>
/* OS specific libraries */
#ifdef _WIN32
#include<windows.h>
#endif
#ifdef __linux
#include<dlfcn.h>
#endif
/* Include D2XX header*/
#include "ftd2xx.h"
/* Include libMPSSE header */
#include "libMPSSE_i2c.h"
/******************************************************************************/
/* Macro and type defines */
/******************************************************************************/
/* Helper macros */
#ifdef _WIN32
#define GET_FUN_POINTER GetProcAddress
#define CHECK_ERROR(exp) {if(exp==NULL){printf("%s:%d:%s(): NULL expression\
encountered \n",__FILE__, __LINE__, __FUNCTION__);exit(1);}else{;}};
#endif
#ifdef __linux
#define GET_FUN_POINTER dlsym
#define CHECK_ERROR(exp) {if(dlerror() != NULL){printf("line %d: ERROR \
dlsym\n",__LINE__);}}
#endif
#define APP_CHECK_STATUS(exp) {if(exp!=FT_OK){printf("%s:%d:%s(): status(0x%x) \
!= FT_OK\n",__FILE__, __LINE__, __FUNCTION__,exp);exit(1);}else{;}};
#define CHECK_NULL(exp){if(exp==NULL){printf("%s:%d:%s(): NULL expression \
encountered \n",__FILE__, __LINE__, __FUNCTION__);exit(1);}else{;}};
/* Application specific macro definations */
#define I2C_DEVICE_ADDRESS_EEPROM 0x57
#define I2C_DEVICE_BUFFER_SIZE 256
#define I2C_WRITE_COMPLETION_RETRY 10
#define START_ADDRESS_EEPROM 0x00 /*read/write start address inside the EEPROM*/
#define END_ADDRESS_EEPROM 0x10
#define RETRY_COUNT_EEPROM 10 /* number of retries if read/write fails */
#define CHANNEL_TO_OPEN 1 /*0 for first available channel, 1 for next... */
#define DATA_OFFSET 2
/* Declaration of function pointers */
typedef FT_STATUS (*pfunc_I2C_GetNumChannels)(uint32 *numChannels);
pfunc_I2C_GetNumChannels p_I2C_GetNumChannels;
typedef FT_STATUS (*pfunc_I2C_GetChannelInfo)(uint32 index, \
FT_DEVICE_LIST_INFO_NODE *chanInfo);
pfunc_I2C_GetChannelInfo p_I2C_GetChannelInfo;
typedef FT_STATUS (*pfunc_I2C_OpenChannel)(uint32 index, FT_HANDLE *handle);
pfunc_I2C_OpenChannel p_I2C_OpenChannel;
typedef FT_STATUS (*pfunc_I2C_CloseChannel)(FT_HANDLE handle);
pfunc_I2C_CloseChannel p_I2C_CloseChannel;
typedef FT_STATUS (*pfunc_I2C_InitChannel)(FT_HANDLE handle, ChannelConfig \
*config);
pfunc_I2C_InitChannel p_I2C_InitChannel;
typedef FT_STATUS (*pfunc_I2C_DeviceRead)(FT_HANDLE handle,uint32 \
deviceAddress,uint32 sizeToTransfer, uint8 *buffer, uint32 \
*sizeTransfered, uint32 options);
pfunc_I2C_DeviceRead p_I2C_DeviceRead;
typedef FT_STATUS (*pfunc_I2C_DeviceWrite)(FT_HANDLE handle, uint32 \
deviceAddress,uint32 sizeToTransfer, uint8 *buffer, uint32 \
*sizeTransfered, uint32 options);
pfunc_I2C_DeviceWrite p_I2C_DeviceWrite;
/******************************************************************************/
/* Global variables */
/******************************************************************************/
uint32 channels;
FT_HANDLE ftHandle;
ChannelConfig channelConf;
FT_STATUS status;
uint8 buffer[I2C_DEVICE_BUFFER_SIZE];
/******************************************************************************/
/* Public function definitions */
/******************************************************************************/
/*!
* \brief Writes to EEPROM
*
* This function writes a byte to a specified address within the 24LC024H EEPROM
*
* \param[in] slaveAddress Address of the I2C slave (EEPROM)
* \param[in] registerAddress Address of the memory location inside the slave to where the byte
* is to be written
* \param[in] data The byte that is to be written
* \return Returns status code of type FT_STATUS(see D2XX Programmer's Guide)
* \sa Datasheet of 24LC024H http://ww1.microchip.com/downloads/en/devicedoc/22102a.pdf
* \note
* \warning
*/
FT_STATUS write_byte(uint8 slaveAddress, uint8 registerAddress, uint8 data)
{
FT_STATUS status;
uint32 bytesToTransfer = 0;
uint32 bytesTransfered;
bool writeComplete=0;
uint32 retry=0;
bytesToTransfer=0;
bytesTransfered=0;
buffer[bytesToTransfer++]=registerAddress; /* Byte addressed inside EEPROM */
buffer[bytesToTransfer++]=data;
status = p_I2C_DeviceWrite(ftHandle, slaveAddress, bytesToTransfer, buffer,\
&bytesTransfered, \
I2C_TRANSFER_OPTIONS_START_BIT|I2C_TRANSFER_OPTIONS_STOP_BIT);
APP_CHECK_STATUS(status);
/* poll to check completition */
while((writeComplete==0) && (retry<I2C_WRITE_COMPLETION_RETRY))
{
bytesToTransfer=0;
bytesTransfered=0;
buffer[bytesToTransfer++]=registerAddress; /*Byte addressed inside EEPROM*/
status = p_I2C_DeviceWrite(ftHandle, slaveAddress, bytesToTransfer, \
buffer, &bytesTransfered, \
I2C_TRANSFER_OPTIONS_START_BIT|I2C_TRANSFER_OPTIONS_BREAK_ON_NACK);
if(bytesToTransfer==bytesTransfered)
{
writeComplete=1;
printf("... Write done\n");
}
retry++;
}
return status;
}
/*!
* \brief Reads from EEPROM
*
* This function reads a byte from a specified address within the 24LC024H EEPROM
*
* \param[in] slaveAddress Address of the I2C slave (EEPROM)
* \param[in] registerAddress Address of the memory location inside the slave from where the
* byte is to be read
* \param[in] *data Address to where the byte is to be read
* \return Returns status code of type FT_STATUS(see D2XX Programmer's Guide)
* \sa Datasheet of 24LC024H http://ww1.microchip.com/downloads/en/devicedoc/22102a.pdf
* \note
* \warning
*/
FT_STATUS read_byte(uint8 slaveAddress, uint8 registerAddress, uint8 *data)
{
FT_STATUS status;
uint32 bytesToTransfer = 0;
uint32 bytesTransfered;
bytesToTransfer=0;
bytesTransfered=0;
buffer[bytesToTransfer++]=registerAddress; /* Byte addressed inside EEPROM */
status = p_I2C_DeviceWrite(ftHandle, slaveAddress, bytesToTransfer, buffer,\
&bytesTransfered, I2C_TRANSFER_OPTIONS_START_BIT);
bytesToTransfer=1;
bytesTransfered=0;
status |= p_I2C_DeviceRead(ftHandle, slaveAddress, bytesToTransfer, buffer,\
&bytesTransfered, I2C_TRANSFER_OPTIONS_START_BIT);
*data = buffer[0];
return status;
}
/*!
* \brief Main function / Entry point of the sample application
*
* This function is the entry point to the sample application. It opens the channel, writes to the
* EEPROM and reads back.
*
* \param[in] none
* \return Returns 0 for success
* \sa
* \note
* \warning
*/
int main()
{
#ifdef _WIN32
#ifdef _MSC_VER
HMODULE h_libMPSSE;
#else
HANDLE h_libMPSSE;
#endif
#endif
#ifdef __linux
void *h_libMPSSE;
#endif
FT_STATUS status;
FT_DEVICE_LIST_INFO_NODE devList;
uint8 address;
uint8 data;
int i,j;
channelConf.ClockRate = I2C_CLOCK_FAST_MODE;/*i.e. 400000 KHz*/
channelConf.LatencyTimer= 255;
//channelConf.Options = I2C_DISABLE_3PHASE_CLOCKING;
//channelConf.Options = I2C_ENABLE_DRIVE_ONLY_ZERO;
/* load library */
#ifdef _WIN32
#ifdef _MSC_VER
h_libMPSSE = LoadLibrary(L"libMPSSE.dll");
#else
h_libMPSSE = LoadLibrary("libMPSSE.dll");
#endif
if(NULL == h_libMPSSE)
{
printf("Failed loading libMPSSE.dll. Please check if the file exists in the working directory\n");
}
#endif
#ifdef __linux
h_libMPSSE = dlopen("libMPSSE.so",RTLD_LAZY);
if(!h_libMPSSE)
{
printf("Failed loading libMPSSE.so\n");
}
#endif
/* init function pointers */
p_I2C_GetNumChannels = (pfunc_I2C_GetNumChannels)GET_FUN_POINTER(h_libMPSSE, "I2C_GetNumChannels");
CHECK_ERROR (p_I2C_GetNumChannels);
p_I2C_GetChannelInfo = (pfunc_I2C_GetChannelInfo)GET_FUN_POINTER(h_libMPSSE, "I2C_GetChannelInfo");
CHECK_ERROR(p_I2C_GetChannelInfo);
p_I2C_OpenChannel = (HINSTANCE__*) (pfunc_I2C_OpenChannel)GET_FUN_POINTER(h_libMPSSE, "I2C_OpenChannel");
CHECK_ERROR(p_I2C_OpenChannel);
p_I2C_CloseChannel = (pfunc_I2C_CloseChannel)GET_FUN_POINTER(h_libMPSSE,"I2C_CloseChannel");
CHECK_ERROR(p_I2C_CloseChannel);
p_I2C_InitChannel = (pfunc_I2C_InitChannel)GET_FUN_POINTER(h_libMPSSE, "I2C_InitChannel");
CHECK_ERROR(p_I2C_InitChannel);
p_I2C_DeviceRead = (pfunc_I2C_DeviceRead)GET_FUN_POINTER(h_libMPSSE, "I2C_DeviceRead");
CHECK_ERROR(p_I2C_DeviceRead);
p_I2C_DeviceWrite = (pfunc_I2C_DeviceWrite)GET_FUN_POINTER(h_libMPSSE, "I2C_DeviceWrite");
CHECK_ERROR(p_I2C_DeviceWrite);
status = p_I2C_GetNumChannels(&channels);
APP_CHECK_STATUS(status);
printf("Number of available I2C channels = %d\n",channels);
if(channels>0)
{
for(i=0;i<channels;i++)
{
status = p_I2C_GetChannelInfo(i,&devList);
APP_CHECK_STATUS(status);
printf("Information on channel number %d:\n",i);
/*print the dev info*/
printf(" Flags=0x%x\n",devList.Flags);
printf(" Type=0x%x\n",devList.Type);
printf(" ID=0x%x\n",devList.ID);
printf(" LocId=0x%x\n",devList.LocId);
printf(" SerialNumber=%s\n",devList.SerialNumber);
printf(" Description=%s\n",devList.Description);
printf(" ftHandle=0x%x\n",devList.ftHandle);/* 0 if not open*/
}
/* Open the first available channel */
status = p_I2C_OpenChannel(CHANNEL_TO_OPEN,&ftHandle);
APP_CHECK_STATUS(status);
printf("\nhandle=0x%x status=%d\n",ftHandle,status);
status = p_I2C_InitChannel(ftHandle,&channelConf);
APP_CHECK_STATUS(status);
for(address=START_ADDRESS_EEPROM;address<END_ADDRESS_EEPROM;address++)
{
printf("writing address = %d data = %d",address,address+DATA_OFFSET);
write_byte(I2C_DEVICE_ADDRESS_EEPROM,address,address+DATA_OFFSET);
}
for(address=START_ADDRESS_EEPROM;address<END_ADDRESS_EEPROM;address++)
{
status = read_byte(I2C_DEVICE_ADDRESS_EEPROM,address, &data);
for(j=0; ((j<RETRY_COUNT_EEPROM) && (FT_OK !=status)); j++)
{
printf("read error... retrying \n");
status = read_byte(I2C_DEVICE_ADDRESS_EEPROM,address, &data);
}
if((RETRY_COUNT_EEPROM==j) && (FT_OK != status))
{
printf("failed reading\n");
}
printf("reading address %d data read=%d\n", address, data);
}
status = p_I2C_CloseChannel(ftHandle);
}
return 0;
}
[Project]
FileName=proghyt.dev
Name=Project1
UnitCount=1
Type=1
Ver=1
ObjFiles=
Includes=
Libs=
PrivateResource=
ResourceIncludes=
MakeIncludes=
Compiler=
CppCompiler=
Linker=libMPSSE.a_@@_libMPSSE.a_@@_
IsCpp=1
Icon=
ExeOutput=
ObjectOutput=
OverrideOutput=0
OverrideOutputName=proghyt.exe
HostApplication=
Folders=
CommandLine=
UseCustomMakefile=0
CustomMakefile=
IncludeVersionInfo=0
SupportXPThemes=0
CompilerSet=0
CompilerSettings=0101000000000001001000
[Unit1]
FileName=main.c
CompileCpp=1
Folder=
Compile=1
Link=1
Priority=1000
OverrideBuildCmd=0
BuildCmd=$(CPP) -c main.c -o main.o $(CXXFLAGS)
[VersionInfo]
Major=0
Minor=1
Release=1
Build=1
LanguageID=1033
CharsetID=1252
CompanyName=
FileVersion=
FileDescription=Developed using the Dev-C++ IDE
InternalName=
LegalCopyright=
LegalTrademarks=
OriginalFilename=
ProductName=
ProductVersion=
AutoIncBuildNr=0
[Unit3]
FileName=libMPSSE_i2c.h
CompileCpp=1
Folder=Project1
Compile=1
Link=1
Priority=1000
OverrideBuildCmd=0
BuildCmd=
[Editor_1]
CursorCol=3
CursorRow=12
TopLine=107
LeftChar=1
[Editors]
Focused=0
Order=0,-1
[Editor_0]
Open=1
Top=1
CursorCol=28
CursorRow=252
TopLine=145
LeftChar=1
File added
This diff is collapsed.
File added
/*!
* \file libMPSSE_i2c.h
*
* \author FTDI
* \date 20110505
*
* Copyright 2011 Future Technology Devices International Limited
* Company Confidential
*
* Project: libMPSSE
* Module: I2C
*
* Rivision History:
* 0.1 - initial version
* 0.2 - 20110708 - added FT_ReadGPIO, FT_WriteGPIO & 3-phase clocking
* 0.3 - 20111025 - modified for supporting 64bit linux
* added I2C_TRANSFER_OPTIONS_NACK_LAST_BYTE
*/
#ifndef LIBMPSSE_I2C_H
#define LIBMPSSE_I2C_H
#include "ftd2xx.h"
/******************************************************************************/
/* Macro defines */
/******************************************************************************/
#ifdef _MSC_VER
#define FTDI_API extern "C"
#else
#define FTDI_API
#endif
/* Options to I2C_DeviceWrite & I2C_DeviceRead */
/*Generate start condition before transmitting */
#define I2C_TRANSFER_OPTIONS_START_BIT 0x00000001
/*Generate stop condition before transmitting */
#define I2C_TRANSFER_OPTIONS_STOP_BIT 0x00000002
/*Continue transmitting data in bulk without caring about Ack or nAck from device if this bit is
not set. If this bit is set then stop transitting the data in the buffer when the device nAcks*/
#define I2C_TRANSFER_OPTIONS_BREAK_ON_NACK 0x00000004
/* libMPSSE-I2C generates an ACKs for every byte read. Some I2C slaves require the I2C
master to generate a nACK for the last data byte read. Setting this bit enables working with such
I2C slaves */
#define I2C_TRANSFER_OPTIONS_NACK_LAST_BYTE 0x00000008
/* no address phase, no USB interframe delays */
#define I2C_TRANSFER_OPTIONS_FAST_TRANSFER_BYTES 0x00000010
#define I2C_TRANSFER_OPTIONS_FAST_TRANSFER_BITS 0x00000020
#define I2C_TRANSFER_OPTIONS_FAST_TRANSFER 0x00000030
/* if I2C_TRANSFER_OPTION_FAST_TRANSFER is set then setting this bit would mean that the
address field should be ignored. The address is either a part of the data or this is a special I2C
frame that doesn't require an address*/
#define I2C_TRANSFER_OPTIONS_NO_ADDRESS 0x00000040
#define I2C_CMD_GETDEVICEID_RD 0xF9
#define I2C_CMD_GETDEVICEID_WR 0xF8
#define I2C_GIVE_ACK 1
#define I2C_GIVE_NACK 0
/* 3-phase clocking is enabled by default. Setting this bit in ConfigOptions will disable it */
#define I2C_DISABLE_3PHASE_CLOCKING 0x0001
/* The I2C master should actually drive the SDA line only when the output is LOW. It should be
tristate the SDA line when the output should be high. This tristating the SDA line during output
HIGH is supported only in FT232H chip. This feature is called DriveOnlyZero feature and is
enabled when the following bit is set in the options parameter in function I2C_Init */
#define I2C_ENABLE_DRIVE_ONLY_ZERO 0x0002
/******************************************************************************/
/* Type defines */
/******************************************************************************/
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned long long uint64;
typedef signed char int8;
typedef signed short int16;
typedef signed long long int64;
#ifndef _MSC_VER
//typedef unsigned char bool;
#endif
#ifdef __x86_64__
typedef unsigned int uint32;
typedef signed int int32;
#else
typedef unsigned long uint32;
typedef signed long int32;
#endif
typedef enum I2C_ClockRate_t{
I2C_CLOCK_STANDARD_MODE = 100000, /* 100kb/sec */
I2C_CLOCK_FAST_MODE = 400000, /* 400kb/sec */
I2C_CLOCK_FAST_MODE_PLUS = 1000000, /* 1000kb/sec */
I2C_CLOCK_HIGH_SPEED_MODE = 3400000 /* 3.4Mb/sec */
}I2C_CLOCKRATE;
/* Channel configuration information */
typedef struct ChannelConfig_t
{
I2C_CLOCKRATE ClockRate;
uint8 LatencyTimer;
uint32 Options;
}ChannelConfig;
/******************************************************************************/
/* External variables */
/******************************************************************************/
/******************************************************************************/
/* Function declarations */
/******************************************************************************/
FTDI_API FT_STATUS I2C_GetNumChannels(uint32 *numChannels);
FTDI_API FT_STATUS I2C_GetChannelInfo(uint32 index,
FT_DEVICE_LIST_INFO_NODE *chanInfo);
FTDI_API FT_STATUS I2C_OpenChannel(uint32 index, FT_HANDLE *handle);
FTDI_API FT_STATUS I2C_InitChannel(FT_HANDLE handle, ChannelConfig *config);
FTDI_API FT_STATUS I2C_CloseChannel(FT_HANDLE handle);
FTDI_API FT_STATUS I2C_DeviceRead(FT_HANDLE handle, uint32 deviceAddress,
uint32 sizeToTransfer, uint8 *buffer, uint32 *sizeTransfered, uint32 options);
FTDI_API FT_STATUS I2C_DeviceWrite(FT_HANDLE handle, uint32 deviceAddress,
uint32 sizeToTransfer, uint8 *buffer, uint32 *sizeTransfered, uint32 options);
FTDI_API void Init_libMPSSE(void);
FTDI_API void Cleanup_libMPSSE(void);
FTDI_API FT_STATUS FT_WriteGPIO(FT_HANDLE handle, uint8 dir, uint8 value);
FTDI_API FT_STATUS FT_ReadGPIO(FT_HANDLE handle,uint8 *value);
/******************************************************************************/
#endif /*LIBMPSSE_I2C_H*/
/*!
* \file sample-static.c
*
* \author FTDI
* \date 20110512
*
* Copyright 2011 Future Technology Devices International Limited
* Company Confidential
*
* Project: libMPSSE
* Module: I2C Sample Application - Interfacing 24LC024H I2C EEPROM
*
* Rivision History:
* 0.1 - 20110513 - initial version
* 0.2 - 20110801 - Changed LatencyTimer to 255
* Attempt to open channel only if available
* Added & modified macros
* Change I2C_GetChannelInfo & OpenChannel to start indexing from 0
* 0.3 - 20111212 - Added comments
*/
/******************************************************************************/
/* Include files */
/******************************************************************************/
/* Standard C libraries */
#include<stdio.h>
#include<stdlib.h>
/* OS specific libraries */
#ifdef _WIN32
#include<windows.h>
#endif
/* Include D2XX header*/
#include "ftd2xx.h"
/* Include libMPSSE header */
#include "libMPSSE_i2c.h"
/******************************************************************************/
/* Macro and type defines */
/******************************************************************************/
/* Helper macros */
#define APP_CHECK_STATUS(exp) {if(exp!=FT_OK){printf("%s:%d:%s(): status(0x%x) \
!= FT_OK\n",__FILE__, __LINE__, __FUNCTION__,exp);exit(1);}else{;}};
#define CHECK_NULL(exp){if(exp==NULL){printf("%s:%d:%s(): NULL expression \
encountered \n",__FILE__, __LINE__, __FUNCTION__);exit(1);}else{;}};
/* Application specific macro definations */
#define I2C_DEVICE_ADDRESS_EEPROM 0x57
#define I2C_DEVICE_BUFFER_SIZE 256
#define I2C_WRITE_COMPLETION_RETRY 10
#define START_ADDRESS_EEPROM 0x00 /*read/write start address inside the EEPROM*/
#define END_ADDRESS_EEPROM 0x10
#define RETRY_COUNT_EEPROM 10 /* number of retries if read/write fails */
#define CHANNEL_TO_OPEN 1 /*0 for first available channel, 1 for next... */
#define DATA_OFFSET 1
/******************************************************************************/
/* Global variables */
/******************************************************************************/
uint32 channels;
FT_HANDLE ftHandle;
ChannelConfig channelConf;
FT_STATUS status;
uint8 buffer[I2C_DEVICE_BUFFER_SIZE];
/******************************************************************************/
/* Public function definitions */
/******************************************************************************/
/*!
* \brief Writes to EEPROM
*
* This function writes a byte to a specified address within the 24LC024H EEPROM
*
* \param[in] slaveAddress Address of the I2C slave (EEPROM)
* \param[in] registerAddress Address of the memory location inside the slave to where the byte
* is to be written
* \param[in] data The byte that is to be written
* \return Returns status code of type FT_STATUS(see D2XX Programmer's Guide)
* \sa Datasheet of 24LC024H http://ww1.microchip.com/downloads/en/devicedoc/22102a.pdf
* \note
* \warning
*/
FT_STATUS write_byte(uint8 slaveAddress, uint8 registerAddress, uint8 data)
{
uint32 bytesToTransfer = 0;
uint32 bytesTransfered;
bool writeComplete=0;
uint32 retry=0;
bytesToTransfer=0;
bytesTransfered=0;
buffer[bytesToTransfer++]=registerAddress; /* Byte addressed inside EEPROM */
buffer[bytesToTransfer++]=data;
status = I2C_DeviceWrite(ftHandle, slaveAddress, bytesToTransfer, buffer, \
&bytesTransfered, I2C_TRANSFER_OPTIONS_START_BIT|I2C_TRANSFER_OPTIONS_STOP_BIT);
/* poll to check completition */
while((writeComplete==0) && (retry<I2C_WRITE_COMPLETION_RETRY))
{
bytesToTransfer=0;
bytesTransfered=0;
buffer[bytesToTransfer++]=registerAddress; /* Addressed inside EEPROM */
status = I2C_DeviceWrite(ftHandle, slaveAddress, bytesToTransfer,\
buffer, &bytesTransfered, \
I2C_TRANSFER_OPTIONS_START_BIT|I2C_TRANSFER_OPTIONS_BREAK_ON_NACK);
if((FT_OK == status) && (bytesToTransfer == bytesTransfered))
{
writeComplete=1;
printf(" ... Write done\n");
}
retry++;
}
return status;
}
/*!
* \brief Reads from EEPROM
*
* This function reads a byte from a specified address within the 24LC024H EEPROM
*
* \param[in] slaveAddress Address of the I2C slave (EEPROM)
* \param[in] registerAddress Address of the memory location inside the slave from where the
* byte is to be read
* \param[in] *data Address to where the byte is to be read
* \return Returns status code of type FT_STATUS(see D2XX Programmer's Guide)
* \sa Datasheet of 24LC024H http://ww1.microchip.com/downloads/en/devicedoc/22102a.pdf
* \note
* \warning
*/
FT_STATUS read_byte(uint8 slaveAddress, uint8 registerAddress, uint8 *data)
{
FT_STATUS status;
uint32 bytesToTransfer = 0;
uint32 bytesTransfered;
bytesToTransfer=0;
bytesTransfered=0;
buffer[bytesToTransfer++]=registerAddress; /*Byte addressed inside EEPROM */
status = I2C_DeviceWrite(ftHandle, slaveAddress, bytesToTransfer, buffer, \
&bytesTransfered, I2C_TRANSFER_OPTIONS_START_BIT);
APP_CHECK_STATUS(status);
bytesToTransfer=1;
bytesTransfered=0;
status |= I2C_DeviceRead(ftHandle, slaveAddress, bytesToTransfer, buffer, \
&bytesTransfered, I2C_TRANSFER_OPTIONS_START_BIT);
APP_CHECK_STATUS(status);
*data = buffer[0];
return status;
}
/*!
* \brief Main function / Entry point to the sample application
*
* This function is the entry point to the sample application. It opens the channel, writes to the
* EEPROM and reads back.
*
* \param[in] none
* \return Returns 0 for success
* \sa
* \note
* \warning
*/
int main()
{
FT_STATUS status;
FT_DEVICE_LIST_INFO_NODE devList;
uint8 address;
uint8 data;
int i,j;
#ifdef _MSC_VER
Init_libMPSSE();
#endif
channelConf.ClockRate = I2C_CLOCK_FAST_MODE;/*i.e. 400000 KHz*/
channelConf.LatencyTimer= 255;
//channelConf.Options = I2C_DISABLE_3PHASE_CLOCKING;
//channelConf.Options = I2C_ENABLE_DRIVE_ONLY_ZERO;
status = I2C_GetNumChannels(&channels);
APP_CHECK_STATUS(status);
printf("Number of available I2C channels = %d\n",channels);
if(channels>0)
{
for(i=0;i<channels;i++)
{
status = I2C_GetChannelInfo(i,&devList);
APP_CHECK_STATUS(status);
printf("Information on channel number %d:\n",i);
/*print the dev info*/
printf(" Flags=0x%x\n",devList.Flags);
printf(" Type=0x%x\n",devList.Type);
printf(" ID=0x%x\n",devList.ID);
printf(" LocId=0x%x\n",devList.LocId);
printf(" SerialNumber=%s\n",devList.SerialNumber);
printf(" Description=%s\n",devList.Description);
printf(" ftHandle=0x%x\n",devList.ftHandle);/*is 0 unless open*/
}
/* Open the first available channel */
status = I2C_OpenChannel(CHANNEL_TO_OPEN,&ftHandle);
APP_CHECK_STATUS(status);
printf("\nhandle=0x%x status=%d\n",ftHandle,status);
status = I2C_InitChannel(ftHandle,&channelConf);
APP_CHECK_STATUS(status);
for(address=START_ADDRESS_EEPROM;address<END_ADDRESS_EEPROM;address++)
{
printf("writing address = %d data = %d", address, \
address+DATA_OFFSET);
status = write_byte(I2C_DEVICE_ADDRESS_EEPROM, address, \
address+DATA_OFFSET);
for(j=0; ((j<RETRY_COUNT_EEPROM) && (FT_OK !=status)); j++)
{
printf("---- writing again to address = %d, data =%d\n", \
address, address+DATA_OFFSET);
status = write_byte(I2C_DEVICE_ADDRESS_EEPROM, address, \
address+DATA_OFFSET);
}
APP_CHECK_STATUS(status);
}
printf("\n");
for(address=START_ADDRESS_EEPROM; address<END_ADDRESS_EEPROM; address++)
{
status = read_byte(I2C_DEVICE_ADDRESS_EEPROM,address, &data);
for(j=0; ((j<RETRY_COUNT_EEPROM) && (FT_OK !=status)); j++)
{
printf("read error... retrying \n");
status = read_byte(I2C_DEVICE_ADDRESS_EEPROM,address, &data);
}
printf("reading address %d data read=%d\n",address,data);
}
status = I2C_CloseChannel(ftHandle);
}
#ifdef _MSC_VER
Cleanup_libMPSSE();
#endif
return 0;
}
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="proghyt" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/proghyt" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/proghyt" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
<Add option="-fexceptions" />
</Compiler>
<Unit filename="main.cpp" />
<Extensions>
<code_completion />
<envvars />
<debugger />
<lib_finder disable_auto="1" />
</Extensions>
</Project>
</CodeBlocks_project_file>
# depslib dependency file v1.0
1434625867 source:d:\hw_designs\fuse-relay-board\mcu_firmware\trunk\ftdi_i2c\proghyt\main.cpp
<stdio.h>
<stdlib.h>
<windows.h>
"ftd2xx.h"
"libMPSSE_i2c.h"
1320115055 d:\hw_designs\fuse-relay-board\mcu_firmware\trunk\ftdi_i2c\proghyt\ftd2xx.h
1434622805 d:\hw_designs\fuse-relay-board\mcu_firmware\trunk\ftdi_i2c\proghyt\libmpsse_i2c.h
"ftd2xx.h"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment