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.
Solved! Go to Solution.
Hi, Jithin,
As you know that the SPI uses synchronous protocol, it transmit data and receive data simultaneously, so you code is correct to read/write data by either checking the SPRF or SPTEF, not both.
I have browsed the SPI timing of LTC6803, I do not know what is mode 3. It seems that the SPI of LTC6803 only supports the mode:CPHA = 1 CPOL = 1, in the mode, the /CSBI falling edge CAN NOT trigger the MISO signal valid,(the edge of SCKI make the MISO bit valid), the drawback of the mode is that the received data bit may disorder or lose synchronization, i do not know if it is your problem.
Hope it can help you
BR
XiangJun Rong
Hi, Jithin,
As you know that the SPI uses synchronous protocol, it transmit data and receive data simultaneously, so you code is correct to read/write data by either checking the SPRF or SPTEF, not both.
I have browsed the SPI timing of LTC6803, I do not know what is mode 3. It seems that the SPI of LTC6803 only supports the mode:CPHA = 1 CPOL = 1, in the mode, the /CSBI falling edge CAN NOT trigger the MISO signal valid,(the edge of SCKI make the MISO bit valid), the drawback of the mode is that the received data bit may disorder or lose synchronization, i do not know if it is your problem.
Hope it can help you
BR
XiangJun Rong