-
mfenner authored
Function as command line tool not tested so far. git-svn-id: https://svnsrv.desy.de/desy/MSK_PROJECTS/other/Fuse-Relay-Board/MCU_Firmware/trunk@801 e4d10518-288a-426b-ba36-e453799dee6f
mfenner authoredFunction as command line tool not tested so far. git-svn-id: https://svnsrv.desy.de/desy/MSK_PROJECTS/other/Fuse-Relay-Board/MCU_Firmware/trunk@801 e4d10518-288a-426b-ba36-e453799dee6f
main.c 7.84 KiB
/*!
* \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>
#include <time.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_WRITE_COMPLETION_RETRY 10
#define CHANNEL_TO_OPEN 0
/******************************************************************************/
/* Global variables */
/******************************************************************************/
uint32 channels;
FT_HANDLE ftHandle;
ChannelConfig channelConf;
FT_STATUS status;
uint8 buffer[10];
void waitFor (unsigned int secs) {
unsigned long retTime = time(0) + secs; // Get finishing time.
while (time(0) < retTime); // Loop until it arrives.
}
/******************************************************************************/
/* 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_hyt(uint8 slaveAddress, uint8 commandByte, uint8 datah, uint8 datal)
{
uint32 bytesToTransfer = 0;
uint32 bytesTransfered;
bool writeComplete=0;
uint32 retry=0;
bytesToTransfer=0;
bytesTransfered=0;
buffer[bytesToTransfer++]=commandByte;
buffer[bytesToTransfer++]=datah;
buffer[bytesToTransfer++]=datal;
status = I2C_DeviceWrite(ftHandle, slaveAddress, bytesToTransfer, buffer, &bytesTransfered,
I2C_TRANSFER_OPTIONS_START_BIT |
I2C_TRANSFER_OPTIONS_STOP_BIT |
//I2C_TRANSFER_OPTIONS_NACK_LAST_BYTE |
I2C_TRANSFER_OPTIONS_FAST_TRANSFER_BYTES
//I2C_TRANSFER_OPTIONS_FAST_TRANSFER_BITS
//I2C_TRANSFER_OPTIONS_FAST_TRANSFER
);
APP_CHECK_STATUS(status);
return status;
}
FT_STATUS read_hyt(uint8 slaveAddress, uint8 *data, uint8 len)
{
FT_STATUS status;
uint32 bytesToTransfer = 0;
uint32 bytesTransfered;
status = I2C_DeviceRead(ftHandle, slaveAddress, len, buffer, &bytesTransfered,
I2C_TRANSFER_OPTIONS_START_BIT
//I2C_TRANSFER_OPTIONS_STOP_BIT |
//I2C_TRANSFER_OPTIONS_NACK_LAST_BYTE |
//I2C_TRANSFER_OPTIONS_FAST_TRANSFER_BYTES
);
APP_CHECK_STATUS(status);
*data = buffer[0];
return status;
}
int main(int argc, char *argv[])
{
FT_STATUS status;
FT_DEVICE_LIST_INFO_NODE devList;
uint8 address;
uint8 data;
uint8 CurrAddr=0x28;
uint8 NewAddr=0x31;
int i,j;
if (argc != 2)
{
printf("Expected two arguments: proghyt [CurrentAddr] [NewAddr]\r\n");
return 0;
} else printf("Changing address from %X to %X.\r\n",CurrAddr, NewAddr);
CurrAddr=strtol(argv[1], NULL, 0);
NewAddr=strtol(argv[2], NULL, 0);
Init_libMPSSE();
channelConf.ClockRate = 10000;/*i.e. 100000 KHz*/
channelConf.LatencyTimer= 2;
channelConf.Options = 0;
//channelConf.Options |= I2C_DISABLE_3PHASE_CLOCKING;
//channelConf.Options |= I2C_ENABLE_DRIVE_ONLY_ZERO;
//channelConf.Options |= I2C_TRANSFER_OPTIONS_FAST_TRANSFER_BYTES;
//channelConf.Options |= I2C_TRANSFER_OPTIONS_FAST_TRANSFER_BITS;
//channelConf.Options |= I2C_TRANSFER_OPTIONS_FAST_TRANSFER;
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("%d\r\n",status);
printf("\nhandle=0x%x status=%d\n",ftHandle,status);
status = I2C_InitChannel(ftHandle,&channelConf);
APP_CHECK_STATUS(status);
printf("%d\r\n",status);
printf("Setting GPIO Pin OFF.\r\n");
FT_WriteGPIO(ftHandle, 0x01, 0x00);
printf("%d\r\n",status);
waitFor(1);
printf("Setting GPIO Pin ON.\r\n");
FT_WriteGPIO(ftHandle, 0x01, 0x01);
printf("%d\r\n",status);
printf("Starting Command Mode.\r\n");
write_hyt(CurrAddr,0xA0,0x00,0x00);
//printf("%d\r\n",status);
read_hyt(CurrAddr, buffer, 1);
//printf("%X\r\n", buffer[0]);
if (buffer[0]==0x81) printf("success.\r\n");
else if (buffer[0] | 0x40) printf("Failed: stale.\r\n");
else printf("failed.\r\n");
//printf("Request I2C Address.\r\n");
//write_hyt(CurrAddr,0x1C,0x00,0x00);
//printf("%d\r\n",status);
//waitFor(1);
//printf("Reading I2C Address.\r\n");
//read_hyt( CurrAddr, buffer,3);
//printf("%X %X %X\r\n", buffer[0], buffer[1], buffer[2]);
printf("Writing new I2C Address.\r\n");
write_hyt(CurrAddr,0x5C,0x00,NewAddr);
//printf("%d\r\n",status);
read_hyt(CurrAddr, buffer, 1);
//printf("%X\r\n", buffer[0]);
if (buffer[0]==0x81) printf("success.\r\n");
else printf("failed.\r\n");
printf("Leaving Command Mode.\r\n");
write_hyt(CurrAddr,0x80,0x00,0x00);
//printf("%d\r\n",status);
waitFor(1);
printf("Setting GPIO Pin OFF.\r\n");
FT_WriteGPIO(ftHandle, 0x01, 0x00);
//printf("%d\r\n",status);
printf("Closing Handle\r\n");
status = I2C_CloseChannel(ftHandle);
}
Cleanup_libMPSSE();
system("pause");
return 0;
}