I Have been trying to communicate my MC56f8037 DSC to a LTC6803 battery stack monitor IC through SPI communication. DSC is intented to act as a SPI master. I tried processor expert bean SyncroMaster for SPI communication. But reading values from the slave gave me incorrect values. Later I used a different SPI function other than processor expert function .But now I'm getting some values when reading from ve but those are also incorrect. Now I suspect my spi writing function also wrong.
SPI is working in MODE3 with 500Khz clock rate. SPI functon for reading and writing is
#define SPIReadStatReg() (void)QSPI0_SCTRL; // Clear Status reg.
#define SPIReadDataReg() (void)QSPI0_DRCV; // Clear Data reg
#define SPIWaitReceiveDone() while ((QSPI0_SCTRL&(0x01U<<3))) // Wait for full buf (SPRF)
#define SPIWaitTransferDone() while (!(QSPI0_SCTRL&(0x01U<<0)))//Wait for buf empty (SPTE)
void SPI_send_byte(unsigned char data)
{
SPIWaitTransferDone(); // Wait for buf empty
SPIReadStatReg(); // Read status register (possible SPRF, SPTEF)
SPIReadDataReg(); // Read receive data register. It clears flags
QSPI0_DXMIT = data; // Start transfer - send byte
SPIWaitTransferDone(); // Wait for transfer complete
SPIReadDataReg();
SPIWaitReceiveDone(); // Wait for receive complete
}
unsigned char SPI_read_byte(void)
{
unsigned char value=0; // function cannot directly return register value
SPIReadStatReg(); // Read status register (possible SPRF, SPTEF)
SPIReadDataReg(); // Read receive data register. It clears flags
/QSPI0_DXMIT =0x00; // Generate clocks for reading 1 byte of data!
value = QSPI0_DRCV;
SPIWaitReceiveDone(); // Wait for receive complete
return(value);
}
please correct me if im wrong with these functions and also suggest a better and a reliable method to establish SPI communication with a DSC master and any other slaves.