You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

133 lines
3.6 KiB

#include "spiPort.h"
#define LOG( X ) m_pLog->Log( "spiPort", this, X )
#define LOG_ERROR( X ) m_pLog->Log( "spiPort", this, _LOG_ERROR, X )
spiPort::spiPort():transportBus(){
m_ioFID = -1;
m_bConnected = false;
m_bInitComplete = false;
m_sDevicePath = NULL;
m_nLineSpeed = 2500000;
};
spiPort::spiPort( CSTR DevicePath, int Speed ): transportSerial() {
m_ioFID = -1;
m_bConnected = false;
m_nLineSpeed = Speed;
m_pDevicePath = (CSTR) malloc( strlen(DevicePath)+1);
strcpy( m_pDevicePath, DevicePath );
m_bInitComplete = HW_init( m_pDevicePath, m_nLineSpeed );
if( m_bInitComplete ) {
#ifdef DEBUG
char Buffer[CLOG];
sprintf( Buffer,"Init %s [%d] OK", m_pDevicePath, m_nLineSpeed );
LOG( Buffer );
#endif
};
};
// Destructor implementation
serialPort::~serialPort(){
};
bool spiPort::HW_init( CSRT Path, uint32_t Speed ){
uint8_t mode, lsb, bits;
uint32_t speed = 2500000;
if( ( m_ioFID = open( Path, O_RDWR ) ) < 0 ){
LOG_ERROR("Failed to open the bus.");
/* ERROR HANDLING; you can check errno to see what went wrong */
return false;
};
///////////////
// Verifications
///////////////
//possible modes: mode |= SPI_LOOP; mode |= SPI_CPHA; mode |= SPI_CPOL; mode |= SPI_LSB_FIRST; mode |= SPI_CS_HIGH; mode |= SPI_3WIRE; mode |= SPI_NO_CS; mode |= SPI_READY;
//multiple possibilities using |
/*
if (ioctl(file, SPI_IOC_WR_MODE, &mode)<0) {
perror("can't set spi mode");
return;
};
//*/
if( ioctl( m_ioFID, SPI_IOC_RD_MODE, &mode) < 0 ){
LOG_ERROR("SPI rd_mode");
return false;
};
if( ioctl( m_ioFID, SPI_IOC_RD_LSB_FIRST, &lsb) < 0 ){
LOG_ERROR("SPI rd_lsb_fist");
return;
};
//* sunxi supports only 8 bits
if( ioctl( m_ioFID, SPI_IOC_WR_BITS_PER_WORD, 8)<0 ){
LOG_ERROR("can't set bits per word");
return false;
};
//*/
if( ioctl( m_ioFID, SPI_IOC_RD_BITS_PER_WORD, &bits) < 0 ){
LOG_ERROR("SPI bits_per_word");
return false;
};
//*
if( ioctl( m_ioFID, SPI_IOC_WR_MAX_SPEED_HZ, &Speed)<0 ){
LOG_ERROR("can't set max speed hz");
return false;
};
//*/
if( ioctl( m_ioFID, SPI_IOC_RD_MAX_SPEED_HZ, &speed) < 0 ){
LOG_ERROR("SPI max_speed_hz");
return false;
};
#ifdef DEBUG
char Buffer[256];
sprintf( Buffer, "%s: spi mode %d, %d bits %sper word, %d Hz max\n", Path, mode, bits, lsb ? "(lsb first) " : "", speed );
LOG( Buffer );
#endif
xfer[0].tx_buf = m_pOutputBuffer;
xfer[0].len = 3; // Length of command to write
xfer[0].cs_change = 0; // Keep CS activated
xfer[0].delay_usecs = 0; // Delay in us
xfer[0].speed_hz = Speed; // Speed
xfer[0].bits_per_word = 8; // Bites per word 8
xfer[1].rx_buf = m_pInputBuffer;
xfer[1].len = 4; // Length of Data to read
xfer[1].cs_change = 0; // Keep CS activated
xfer[1].delay_usecs = 0;
xfer[1].speed_hz = Speed;
xfer[1].bits_per_word = 8;
return true;
};
char * spi_read(int add1,int add2,int nbytes ){
int status;
memset(buf, 0, sizeof buf);
memset(buf2, 0, sizeof buf2);
buf[0] = 0x01;
buf[1] = add1;
buf[2] = add2;
xfer[0].tx_buf = (unsigned long)buf;
xfer[0].len = 3; /* Length of command to write*/
xfer[1].rx_buf = (unsigned long) buf2;
xfer[1].len = nbytes; /* Length of Data to read */
status = ioctl(file, SPI_IOC_MESSAGE(2), xfer);
if (status < 0){
perror("SPI_IOC_MESSAGE");
return;
};
printf("env: %02x %02x %02x\n", buf[0], buf[1], buf[2]);
printf("ret: %02x %02x %02x %02x\n", buf2[0], buf2[1], buf2[2], buf2[3]);
com_serial=1;
failcount=0;
return buf2;
};
#undef LOG
#undef LOG_ERROR