Hi,
The length passed to NASpiReadWrite() is used for both the Tx Length and the Rx length. Effectively the data from the Tx buffer is clocked out and the data in the Rx buffer is clocked in using the same clock, atr the same time. The way to interpret this does depend a bit on the exact way the peripheral works. If youare ony reading then you can ignore the data in the Tx buffer, but it still must be there to provide data even though it is ignored. Similarly when writing data will be read into the Rx buffer so that must be there also.
The Initialisation I use to set up the GPIO is:
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ConfigureGPIO:
Description:
This routine is used to configure the GPIO pins that will be used for
the SPI interface. It uses the pin definitions from spi.h and is
caled by the main Module Initialisation routine.
Parameters
None
Return
None
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
static void ConfigureGPIO(void)
{
#if (PROCESSOR == ns9210 || PROCESSOR == ns9215)
NAconfigureGPIOpin(APP_SPI_MASTER_GPIO_CS, NA_GPIO_FUNC4_STATE, 0);
NAconfigureGPIOpin(APP_SPI_MASTER_GPIO_CLK, NA_GPIO_FUNC4_STATE, 0);
NAconfigureGPIOpin(APP_SPI_MASTER_GPIO_RXD, NA_GPIO_FUNC4_STATE, 0);
NAconfigureGPIOpin(APP_SPI_MASTER_GPIO_TXD, NA_GPIO_FUNC4_STATE, 0);
#elif (PROCESSOR == ns9360 || PROCESSOR == ns7520)
NAconfigureGPIOpin(APP_SPI_MASTER_GPIO_CS, NA_GPIO_OUTPUT_STATE, 1); /*enable*/
NAsetGPIOpin (APP_SPI_MASTER_GPIO_CS, 1);
NAconfigureGPIOpin(APP_SPI_MASTER_GPIO_CLK, NA_GPIO_OUTPUT_STATE, 0); /*clk*/
NAsetGPIOpin (APP_SPI_MASTER_GPIO_CLK, 0);
NAconfigureGPIOpin(APP_SPI_MASTER_GPIO_CS, NA_GPIO_FUNC0_STATE, 0);
NAconfigureGPIOpin(APP_SPI_MASTER_GPIO_CLK, NA_GPIO_FUNC0_STATE, 0);
#else
#error "set up the GPIO pins here.\nThen, comment this line out to build your platform."
#endif
}
Then the device registration is done by:
/*****************************************************************************
SPI_OpenModule:
Description:
This routine is used to initialise the SPI module.
The required GPIO signals asr set up and a device description is added
to the NetOS driver table
NOTE: This currently supports a master interface optimised for CML C-BUS
Parameters
None
Returns
status SPI_OK (0) = Success
SPI_ERR_MEMORY = Failed to generate memory pool
SPI_ERR_REGISTER = Failed to register device
*****************************************************************************/
int SPI_OpenModule(void)
{
UINT status;
NaStatus reg_stat;
const char *ERR_FMT = "ERR:[%08ld] SPI_InitModule: %s() failed with status %d.\n";
/* Configure the hardware signal we will use */
ConfigureGPIO();
/* Create the memory pool to be used for SPI buffers */
status = CreateBufferPool();
if (status) {
return(SPI_ERR_MEMORY);
}
reg_stat = NASpiRegisterDevice(&GBL_CMX_Device);
if (NA_IS_ERROR(reg_stat))
{
printf(ERR_FMT, tx_time_get(), "NASpiRegisterDevice", reg_stat);
return(SPI_ERR_REGISTER);
}
return (SPI_ERR_OK);
}
And the actual transfer is:
/*****************************************************************************
SPI_ReadWrite:
Description:
This routine is used to transfer data on the SPI bus. It should passed a
buffer structure that was allocated by SPI_AllocBuffers, and with data to
be written set up at the wr_algn_ptr. The routine reads and writes at the
same time. When the routine returns read data will be at the rd_align_ptr
location.
Parameters
device Name of device for data transfer (SPI_CMX7143)
buffs pointer to structure containing read and write buffers
length Number of bytes to be transferred
Returns
Pointer to allocated structue (NOTE: use SPI_FreeBuffers to free this)
NULL error allocating buffers
*****************************************************************************/
int SPI_ReadWrite(char * device, struct str_spi_buffers *buffs, int length)
{
NaStatus status;
const char *ERR_FMT = "ERR:[%08ld] SPI_ReadWrite: %s() failed with status %d.\n";
char *wr_ptr = (char *)(buffs->wr_algn_ptr);
char *rd_ptr = (char *)(buffs->rd_algn_ptr);
ShowBuff("SPT", wr_ptr, length);
status = NASpiReadWrite(device, wr_ptr, rd_ptr, length);
ShowBuff("SPR", rd_ptr, length);
if (NA_IS_ERROR(status))
{
printf(ERR_FMT, tx_time_get(), "NASpiReadWrite", status);
return(SPI_ERR_REGISTER);
}
return (SPI_ERR_OK);
}
The actual GPIO pins used are defined by
/*********************************************************************
Definition of the Port and GPIO Signals to be used
*********************************************************************/
#define PORTA 0
#define PORTB 1
#define PORTC 2
#define PORTD 3
#if (PROCESSOR == ns9215)
#define APP_SPI_MASTER_PORT PORTA
#define APP_SPI_SLAVE_PORT PORTA
#define APP_SPI_MASTER_GPIO_CS 0
#define APP_SPI_MASTER_GPIO_CLK 5
#define APP_SPI_MASTER_GPIO_RXD 3
#define APP_SPI_MASTER_GPIO_TXD 7
#define APP_SPI_SLAVE_GPIO_CS 0
#define APP_SPI_SLAVE_GPIO_CLK 5
#define APP_SPI_SLAVE_GPIO_RXD 3
#define APP_SPI_SLAVE_GPIO_TXD 7
#elif (PROCESSOR == ns9210)
#define APP_SPI_SLAVE_PORT PORTA
#define APP_SPI_MASTER_PORT PORTA
#define APP_SPI_MASTER_GPIO_CS 0
#define APP_SPI_MASTER_GPIO_CLK 5
#define APP_SPI_MASTER_GPIO_RXD 3
#define APP_SPI_MASTER_GPIO_TXD 7
#define APP_SPI_SLAVE_GPIO_CS 0
#define APP_SPI_SLAVE_GPIO_CLK 5
#define APP_SPI_SLAVE_GPIO_RXD 3
#define APP_SPI_SLAVE_GPIO_TXD 7
#elif (PROCESSOR == ns9360)
#define APP_SPI_MASTER_PORT PORTB
#define APP_SPI_SLAVE_PORT PORTB
#if (APP_SPI_MASTER_PORT == PORTB)
#define APP_SPI_MASTER_GPIO_CS 7
#define APP_SPI_MASTER_GPIO_CLK 6
#elif (APP_SPI_MASTER_PORT == PORTC)
#define APP_SPI_MASTER_GPIO_CS 23
#define APP_SPI_MASTER_GPIO_CLK 22
#elif (APP_SPI_MASTER_PORT == PORTD)
#define APP_SPI_MASTER_GPIO_CS 27
#define APP_SPI_MASTER_GPIO_CLK 26
#endif
#if (APP_SPI_SLAVE_PORT == PORTB)
#define APP_SPI_SLAVE_GPIO_CS 7
#define APP_SPI_SLAVE_GPIO_CLK 6
#elif (APP_SPI_SLAVE_PORT == PORTC)
#define APP_SPI_SLAVE_GPIO_CS 23
#define APP_SPI_SLAVE_GPIO_CLK 22
#elif (APP_SPI_SLAVE_PORT == PORTD)
#define APP_SPI_SLAVE_GPIO_CS 27
#define APP_SPI_SLAVE_GPIO_CLK 26
#endif
#elif (PROCESSOR == ns7520)
#define APP_SPI_MASTER_PORT PORTB
#define APP_SPI_MASTER_GPIO_CS 0
#define APP_SPI_MASTER_GPIO_CLK 4
#else
#error "SPI support not available on this processor or platform."
#endif
I have only used this with a ConnectCore9P_9215 to communicate with a SPI compatiable device and it works very well apart from being rather slow.
Hope this helps