diff --git a/ptxdist/local_src/ecu-control/ecuioanalog.cpp b/ptxdist/local_src/ecu-control/ecuioanalog.cpp index e3841ef..80c00e8 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 @@ -274,6 +275,7 @@ int EcuIOAnalog::writeOutChannel(const ECuParameter ¶meters) int EcuIOAnalog::readChannelFromADS7953(int channel, unsigned long &ulValue) { +ulValue = 0xDEADBEEF; int iRet (RETCODE_OK); int fd = -1; char usChannel=0; @@ -282,6 +284,8 @@ int EcuIOAnalog::readChannelFromADS7953(int channel, unsigned long &ulValue) unsigned char rd_buf[64]; 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); @@ -299,14 +303,30 @@ int EcuIOAnalog::readChannelFromADS7953(int channel, unsigned long &ulValue) iRet =RETCODE_FILE_ACCESS_ERROR; } else{ +#if 0 /* horrible debug code follows... */ +uint8_t spimode = 0xDE; +retSpiNum = ioctl(fd, SPI_IOC_RD_MODE, &spimode); +if (retSpiNum != -1) + (void)fprintf(stderr, "spi_mode=%d\n", (int)spimode); +else + (void)fprintf(stderr, "SPI_IOC_RD_MODE failed, errno=%s\n", strerror(errno)); + +spimode = SPI_MODE_3; +retSpiNum = ioctl(fd, SPI_IOC_WR_MODE, &spimode); +if (retSpiNum == -1) + (void)fprintf(stderr, "SPI_IOC_WR_MODE failed, errno=%s\n", strerror(errno)); +#endif xfer.tx_buf = (__u64)&wr_buf[0]; xfer.rx_buf = (__u64)&rd_buf[0]; xfer.len = 6; xfer.cs_change = 0; xfer.bits_per_word = 16; +#if 1 xfer.delay_usecs = 10; - +#else + xfer.speed_hz = 20000000; +#endif /* Mode control register * D15-12 : 0001 selects manual mode * D11 : 1 enable programming of bits D6-D0 @@ -325,16 +345,40 @@ int EcuIOAnalog::readChannelFromADS7953(int channel, unsigned long &ulValue) pUS = (unsigned short*)&wr_buf[0]; xfer.len = 6; *pUS = 0x1800 | ( usChannel << 7 ); + +(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"); - ioctl(fd, SPI_IOC_MESSAGE(1), &xfer); + errno=0; + retSpiNum = ioctl(fd, SPI_IOC_MESSAGE(1), &xfer); +(void)fprintf(stderr, "EcuIOAnalog::readChannelFromADS7953(): retSpiNum=%d, should be 2!, strerror=%s\n", + (int)retSpiNum, + strerror(errno)); - pUS = (unsigned short*)&rd_buf[4]; + pUS = (unsigned short*)&rd_buf[4]; // fixme: endian ? 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*(16/8)) ; 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]; + ulValue = ((long)(rd_buf[5] & 0x0f)) << 8L | ((long)rd_buf[4]); iRet = RETCODE_OK; +(void)fprintf(stderr, "EcuIOAnalog::readChannelFromADS7953(): OK\n"); } else{ +(void)fprintf(stderr, "EcuIOAnalog::readChannelFromADS7953(): readChannel == usChannel failed\n"); iRet = RETCODE_GENERAL_ERROR; } close(fd);