This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TM4C129ENCPDT: SPI

Part Number: TM4C129ENCPDT

Hi All

In my code, I am able to read the device ID from SPI flash but unable to do read  operations. . And when I write a dummy data A B C D and try to read it I am getting all 0xFFs only. Please have a look into my code and help me.

Thanks in Advance.

Im using spi configuration in it rtos

void SPI_CONFIG(void)
{
SPI_Params_init(&spiParams);
spiParams.transferMode = SPI_MODE_BLOCKING;
spiParams.transferTimeout = SPI_WAIT_FOREVER;
spiParams.transferCallbackFxn = NULL;
spiParams.mode = SPI_MASTER;
spiParams.bitRate = 1000000;
spiParams.dataSize = 8;
spiParams.frameFormat = SPI_POL1_PHA1;

spiHandle = SPI_open(Board_SPI0,&spiParams);
if(spiHandle == NULL)
{
// printf("unable to open spi2...!");
}
}


// Function to make flash memory Write Enable

void flashWriteEnable(void)
{
controlCSPin(true); // Set CS pin low to enable the device
// Enable Write Enable
transmitBuffer[0] = 0x06; // Write Enable command
SPI_Transaction spiTransaction;
spiTransaction.count = 1;
spiTransaction.txBuf = transmitBuffer;
spiTransaction.rxBuf = NULL;

if (SPI_transfer(spiHandle, &spiTransaction))
{
// printf("Write Enable successful....!");
}
else
{
// printf("Write Enable error occurred.......!");
return;
}
controlCSPin(false); // Set CS pin high to disable the device
}


// Function to Erase Sector of flash memory
void flashSectorErase(uint32_t address)
{
controlCSPin(true); // Set CS pin low to enable the device
// Erase Sector
SPI_Transaction spiTransaction;
// spiTransaction.count = length + 4;
// spiTransaction.count = length;
spiTransaction.txBuf = transmitBuffer1;
spiTransaction.rxBuf = receiveBuffer1;

transmitBuffer[0] = 0x20; // Sector Erase command
transmitBuffer[1] = (address >> 16) & 0xFF; // Address byte 2
transmitBuffer[2] = (address >> 8) & 0xFF; // Address byte 1
transmitBuffer[3] = address & 0xFF; // Address byte 0
spiTransaction.count = 4;

if (SPI_transfer(spiHandle, &spiTransaction))
{
// printf("Sector Erase successful.....!");
}
else
{
// printf("Sector Erase error occurred.....!");
return;
}
}


// Function to write data to W25Q flash memory
void flashWrite(uint32_t address, uint8_t *data, uint32_t length)
{
flashWriteEnable();
flashSectorErase(address);
// Read the status register of the W25Q32 flash memory
uint8_t statusRegister = readStatusRegister();

SPI_Transaction spiTransaction;
// spiTransaction.count = length + 4;
spiTransaction.count = length;
spiTransaction.txBuf = transmitBuffer1;
spiTransaction.rxBuf = receiveBuffer1;


// Wait for erase operation to complete (can use status register polling)
if((statusRegister & 0x02)) //((statusRegister & 0x01) == 0) busy
{
// Page Program
controlCSPin(true); // Set CS pin low to enable the device
transmitBuffer[0] = 0x02; // Page Program command
transmitBuffer[1] = (address >> 16) & 0xFF; // Address byte 2
transmitBuffer[2] = (address >> 8) & 0xFF; // Address byte 1
transmitBuffer[3] = address & 0xFF; // Address byte 0

memcpy(&transmitBuffer[4], data, length);
spiTransaction.count = length + 4;

if (SPI_transfer(spiHandle, &spiTransaction))
{
// printf("Page Program successful.....!");
}
else
{
// printf("Page Program error occurred.....!");
return;
}

controlCSPin(false); // Set CS pin high to disable the device
}

}

// Function to read data from W25Q flash memory
void flashRead(uint32_t address, uint8_t *data, uint32_t length)
{
controlCSPin(true); // Set CS pin low to enable the device
transmitBuffer1[0] = 0x03; // Read command
transmitBuffer1[1] = (address >> 16) & 0xFF; // Address byte 2
transmitBuffer1[2] = (address >> 8) & 0xFF; // Address byte 1
transmitBuffer1[3] = address & 0xFF; // Address byte 0

SPI_Transaction spiTransaction;
spiTransaction.count = length + 4;
// spiTransaction.count = length;
spiTransaction.txBuf = transmitBuffer1;
spiTransaction.rxBuf = receiveBuffer1;

if (SPI_transfer(spiHandle, &spiTransaction))
{
memcpy(data, &receiveBuffer1[4], length);
// printf("Read successful.......!");
}
else
{
// printf("Read error occurred......!");
}
controlCSPin(false); // Set CS pin high to disable the device
}

  • Hi,

      You didn't mention the part number of your SPI-flash. I can't just look at your long lines of code and tell what is wrong. I strongly suggest you use a scope or even better a logic analyzer that will reveal the problem quickly. Look at the logic analyzer capture and check If you are sending the correct commands from the master. Are you meeting the timing requirements for the slave? Are you sending the correct write command and data for A, B, C, D?  Are you sending the correct read command? You need to compare the commands you send against the command formats as dictated in the slave device datasheet. If the commands you send are correct and the flash is not responding accordingly then you can  investigate the slave side. If the slave is returning the correct data on the bus but the master is not capturing the data then it is a software issue on the master side. Again, a logic analyzer or scope is the best way to troubleshoot the problem instead of going over hundreds of lines of code to spot mistakes. 

  • Hi Charles,

    Sorry for the trouble ,the slave which I'm using is W25Q32JV. Needed delay is provided.Let me probe the communications channel.