Interfacing Multiple SPI Slaves with FX3/CX3/FX3S - KBA229105
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
Author: HemanthR_06 Version: **
Translation - Japanese: FX3/CX3/FX3Sとの多重SPIインターフェース - KBA229105 - Community Translated (JA)
Question:
How to interface multiple SPI slaves with FX3?
Answer:
The default SDK example, UsbSpiRegMode, available in the path mentioned below supports interfacing only a single SPI slave. This may not be suitable for applications where multiple slaves are to be interfaced.
Firmware example path: FX3_Installation_path\EZ-USB FX3 SDK\1.3\firmware\serialif_examples\cyfxusbspiregmode
This article shows how to configure FX3 to work with multiple SPI slaves (two slaves). We can use FX3 GPIOs as software-controlled SSN (Slave Select – Active low) lines between the FX3 and the two slaves. Figure 1 shows the block diagram for this use case.
Figure 1: Interfacing FX3 to Multiple SPI Slaves
Data Transfer Flow:
1. Configure a simple GPIO, say GPIO[33] as SSN line for SPI Slave 1; configure another simple GPIO, say GPIO[34] as SSN line for SPI Slave 2.
2. Whenever the master wants to communicate with any of the slaves, follow these steps:
- Drive the Slave Select GPIO low using CyU3PGpioSetValue(GPIO_NUMBER, CyFalse).
- Send/receive SPI data.
- Drive the Slave Select GPIO high using CyU3PGpioSetValue(GPIO_NUMBER, CyTrue).
Firmware Modifications:
1. Define two GPIOs to be used as Software Slave Select lines.
#define GPIO_SSN1 (33)
#define GPIO_SSN2 (34)
2. Initialize the GPIO block in the firmware and configure two GPIOs as simple GPIOs to be used as software slave select functionality as follows:
/* Configure GPIO 33 as output(GPIO_SSN1). */
gpioConfig.outValue = CyTrue;
gpioConfig.inputEn = CyFalse;
gpioConfig.driveLowEn = CyTrue;
gpioConfig.driveHighEn = CyTrue;
gpioConfig.intrMode = CY_U3P_GPIO_NO_INTR;
apiRetStatus = CyU3PGpioSetSimpleConfig(GPIO_SSN1, &gpioConfig);
if (apiRetStatus != CY_U3P_SUCCESS)
{
/* Error handling */
CyU3PDebugPrint (4, "CyU3PGpioSetSimpleConfig for GPIO Id %d failed, error code = %d\n",
GPIO_SSN1, apiRetStatus);
CyFxAppErrorHandler(apiRetStatus);
}
/* Configure GPIO 34 as output(GPIO_SSN2) */
gpioConfig.outValue = CyTrue;
gpioConfig.inputEn = CyFalse;
gpioConfig.driveLowEn = CyTrue;
gpioConfig.driveHighEn = CyTrue;
gpioConfig.intrMode = CY_U3P_GPIO_NO_INTR;
apiRetStatus = CyU3PGpioSetSimpleConfig(GPIO_SSN2, &gpioConfig);
if (apiRetStatus != CY_U3P_SUCCESS)
{
/* Error handling */
CyU3PDebugPrint (4, "CyU3PGpioSetSimpleConfig for GPIO Id %d failed, error code = %d\n",
GPIO_SSN1, apiRetStatus);
CyFxAppErrorHandler(apiRetStatus);
}
}
3. In the SPI initialization block, add :
- spiConfig.ssnCtrl = CY_U3P_SPI_SSN_CTRL_FW; and
- spiConfig.cpol = CyTrue;
This will enable the slave select to be configured using the firmware (software-controlled SSN lines with active low polarity).
4. For data transfer using the software-controlled SSN lines, do the following:
/* For slave 1*/
/* Drive GPIO low- GPIO_SSN1*/
CyU3PGpioSimpleSetValue (GPIO_SSN1, CyFalse);
/* Send or Receive data */
status = CyU3PSpiTransmitWords (location, 4);
if (status != CY_U3P_SUCCESS)
{
CyU3PDebugPrint (2, "SPI WRITE command failed\r\n");
/* drive GPIO high - GPIO_SSN1*/
CyU3PGpioSimpleSetValue (GPIO_SSN1, CyTrue);
return status;
}
status = CyU3PSpiTransmitWords (buffer, glSpiPageSize);
if (status != CY_U3P_SUCCESS)
{
CyU3PGpioSimpleSetValue (GPIO_SSN1, CyTrue);
return status;
}
/* Drive GPIO high - GPIO_SSN1*/
CyU3PGpioSimpleSetValue (GPIO_SSN1, CyTrue);
The above process can be used for reading data from the slave as well by using the CyU3PSpiReceiveWords()API, by asserting and de-asserting the GPIO as described earlier.
The above code snippet can be modified to be used for Slave 2 by using GPIO_SSN2 in place of GPIO_SSN1.
You can use the UsbSpiRegMode example with the above modification to interface two slaves with FX3 using software-controlled SSN lines.
Note: If the application involves more slaves, the required software-controlled SSN lines used should be equal to the number of slaves.