EZ-USB FX3 Technical Reference Manual, Document Number: 001-76074 Rev. *F 180
Low Performance Peripherals (LPP)
8.5.3 Reads and Writes Using Register Transfers
The CyU3PSpiReceiveWords API reads data from the SPI slave device in register mode, and the CyU3PSpiTransmitBytes
API writes data to an SPI slave device in register mode. Following is the code for performing reads and writes to an SPI slave
using register transfers.
CyFxSpiTransfer (uint16_t pageAddress, uint16_t byteCount, uint8_t *buffer, CyBool_t
isRead)
{
uint8_t location[4];
uint32_t byteAddress = 0;
uint16_t pageCount = (byteCount / glSpiPageSize);
if ((byteCount % glSpiPageSize) != 0)
{
pageCount ++;
}
byteAddress = pageAddress * glSpiPageSize;
while (pageCount != 0)
{
location[1] = (byteAddress >> 16) & 0xFF; /* MS byte */
location[2] = (byteAddress >> 8) & 0xFF;
location[3] = byteAddress & 0xFF; /* LS byte */
if (isRead) /*Read*/
{
location[0] = 0x03; /* Read command. */
status = CyFxSpiWaitForStatus ();
CyU3PSpiSetSsnLine (CyFalse);
status = CyU3PSpiTransmitWords (location, 4);
if (status != CY_U3P_SUCCESS)
{
CyU3PDebugPrint (2, "SPI READ command failed\r\n");
CyU3PSpiSetSsnLine (CyTrue);
}
status = CyU3PSpiReceiveWords (buffer, glSpiPageSize);
if (status != CY_U3P_SUCCESS)
{
CyU3PSpiSetSsnLine (CyTrue);
}
CyU3PSpiSetSsnLine (CyTrue);
}
else /* Write */
{
location[0] = 0x02; /* Write command */
status = CyFxSpiWaitForStatus ();
CyU3PSpiSetSsnLine (CyFalse);
status = CyU3PSpiTransmitWords (location, 4);
if (status != CY_U3P_SUCCESS)
{
CyU3PDebugPrint (2, "SPI WRITE command failed\r\n");
CyU3PSpiSetSsnLine (CyTrue);
}