diff --git a/ptxdist/local_src/ecu-control/ecuioanalog.cpp b/ptxdist/local_src/ecu-control/ecuioanalog.cpp index e3841ef..842b5a2 100644 --- a/ptxdist/local_src/ecu-control/ecuioanalog.cpp +++ b/ptxdist/local_src/ecu-control/ecuioanalog.cpp @@ -30,6 +30,7 @@ #include #include +#include #include #include #include @@ -52,6 +53,11 @@ #include "tle82353sa.h" #include "pca9505.h" +#define ADS_USE_LIBCOMMON 1 +#ifdef ADS_USE_LIBCOMMON +#include "ads7953.h" +#endif + /*****************************************************************************/ /* here: Defines */ /*****************************************************************************/ @@ -274,14 +280,68 @@ int EcuIOAnalog::writeOutChannel(const ECuParameter ¶meters) int EcuIOAnalog::readChannelFromADS7953(int channel, unsigned long &ulValue) { +#ifdef ADS_USE_LIBCOMMON +ulValue = 0xDEADBEEF; + + + struct ADS7953::ADS_CHANNEL_DATA ads_data; + ADS7953 *chip = NULL; + int usChannel = -1; + + (void)memset(&ads_data, 0, sizeof(ads_data)); + + + if ((channel > 0) && (channel < 17)) + { + chip = new ADS7953(ADS7953_ADC0_DEVICEPATH); + usChannel = channel - 1; + } + else if ((channel > 16) && (channel < 33)){ + chip = new ADS7953(ADS7953_ADC1_DEVICEPATH); + usChannel = channel - 16 - 1; + } + + if (!chip) + { + (void)fprintf(stderr, + "EcuIOAnalog::readChannelFromADS7953: could not get chip for %d\n", + channel); + return RETCODE_GENERAL_ERROR; + } + + (void)fprintf(stderr, "EcuIOAnalog::readChannelFromADS7953: chip at %lx, usChannel=%d\n", + (long)chip, + (int)usChannel); + + chip->setAuto2MaxChannel(16); + + // fixme: retcode!! + if (!chip->readChannels(&ads_data)) + (void)fprintf(stderr, "readChannels() 1 failed\n"); + + for(int cn=0 ; cn < 16 ; cn++) + { + // fixme: ads_data.mask needs to be honored + (void)fprintf(stderr, "cn=%d\tdata=%lx\n", (int)cn, (long)ads_data.data[cn]); + } + + ulValue = ads_data.data[usChannel]; + + delete chip; + + return RETCODE_OK; +#else +ulValue = 0xDEADBEEF; int iRet (RETCODE_OK); int fd = -1; char usChannel=0; unsigned short *pUS; - unsigned char wr_buf[32]; - unsigned char rd_buf[64]; + unsigned char wr_buf[4096]; + unsigned char rd_buf[4096]; unsigned short readChannel; struct spi_ioc_transfer xfer; + int retSpiNum; + memset(&xfer, 0, sizeof xfer); memset(&wr_buf[0], 0, sizeof wr_buf); memset(&rd_buf[0], 0, sizeof rd_buf); @@ -322,26 +382,55 @@ int EcuIOAnalog::readChannelFromADS7953(int channel, unsigned long &ulValue) */ /* program the channel */ +#define SIZEOF_MODE0_COMMAND (6) pUS = (unsigned short*)&wr_buf[0]; - xfer.len = 6; + xfer.len = SIZEOF_MODE0_COMMAND; *pUS = 0x1800 | ( usChannel << 7 ); +#if 1 + *++pUS = 0x1800 | ( usChannel << 7 ); + xfer.len += SIZEOF_MODE0_COMMAND; +#endif - ioctl(fd, SPI_IOC_MESSAGE(1), &xfer); - pUS = (unsigned short*)&rd_buf[4]; +(void)fprintf(stderr, "wr_buf='"); +for (unsigned int cn=0 ; cn < (xfer.len*2) ; cn++) +{ + (void)fprintf(stderr, "%2.2x ", (int)wr_buf[cn]); +} +(void)fprintf(stderr, "'\n"); + + errno=0; + retSpiNum = ioctl(fd, SPI_IOC_MESSAGE(2), &xfer); +(void)fprintf(stderr, "EcuIOAnalog::readChannelFromADS7953(): retSpiNum=%d, should be 2!, strerror=%s\n", + (int)retSpiNum, + strerror(errno)); + readChannel = ( rd_buf[5] >> 4 ); +(void)fprintf(stderr, "EcuIOAnalog::readChannelFromADS7953(): readChannel=%d\n, usChannel=%d\n", + (int)readChannel, + (int)usChannel); + +(void)fprintf(stderr, "rd_buf='"); +for (int cn=0 ; cn < retSpiNum ; cn++) +{ + (void)fprintf(stderr, "%2.2x ", (int)rd_buf[cn]); +} +(void)fprintf(stderr, "'\n"); + if( readChannel == usChannel ){ ulValue = (rd_buf[5] & 0x0f) << 8 | rd_buf[4]; iRet = RETCODE_OK; +(void)fprintf(stderr, "EcuIOAnalog::readChannelFromADS7953(): OK\n"); } else{ iRet = RETCODE_GENERAL_ERROR; +(void)fprintf(stderr, "EcuIOAnalog::readChannelFromADS7953(): readChannel == usChannel failed\n"); } close(fd); } return iRet; - +#endif /* ADS_USE_LIBCOMMON */ } int EcuIOAnalog::readChannelFromMAX11628( int channel, unsigned long &ulValue )