2013-11-21

SPI Testing notes


int main()
{
// testSpiSselPin();
    testSpiLoopBack();
}



void testSpiLoopBack()
{
while (1)
{
//testSpiLoopBack082(Olimex20SlaveSelectPortPinArrayPointerArrayArray, \
 //                 SPI_CHANNEL_0, SLAVE_SELECT_0);
// testSpiLoopBack082(Olimex20SlaveSelectPortPinArrayPointerArrayArray, \
  //                SPI_CHANNEL_0, SLAVE_SELECT_1);
// testSpiLoopBack082(Olimex20SlaveSelectPortPinArrayPointerArrayArray, \
  //                SPI_CHANNEL_0, SLAVE_SELECT_2);
// testSpiLoopBack082(Olimex20SlaveSelectPortPinArrayPointerArrayArray, \
  //                SPI_CHANNEL_0, SLAVE_SELECT_3);
testSpiLoopBack082(Olimex20SlaveSelectPortPinArrayPointerArrayArray, \
                  SPI_CHANNEL_0, SLAVE_SELECT_4);
// testSpiLoopBack082(Olimex20SlaveSelectPortPinArrayPointerArrayArray, \
  //                SPI_CHANNEL_0, SLAVE_SELECT_5);
// testSpiLoopBack082(Olimex20SlaveSelectPortPinArrayPointerArrayArray, \
  //                SPI_CHANNEL_0, SLAVE_SELECT_6);
}
}

void testSpiLoopBack082(portPinArrayPointerArray portPinArraryPointerArrayArray[], \
                        uint8_t spiChannelNumber, uint8_t slaveSelectNumber)
{
spiLoopBack082(portPinArraryPointerArrayArray, \
              spiChannelNumber, slaveSelectNumber);
}



// ***************************************************************************
// SPI Functions
// ***************************************************************************

// ***************************************************************************
// Function - Setup SPI channels
// Date     - 2013nov20
// Notes    - 1. Port P06 selected as Slave Select default, BUT DISABLED!!!
//               Instead, P02 and P20 are the default SSEL00 and SSEL10.
//            2. There is a bug in lpc11xx_iocon.c v1.0
//            3. SlaveSelect GPIO port is initialized to output Low
//            4. portPinArray PortPinArraySsel00 = {PORT0, PIN2}; // SSEL00
//               portPinArray PortPinArraySsel10 = {PORT2, PIN0}; // SSEL10
// ***************************************************************************
void setupOneSpiChannel080(uint8_t spiChannelNumber)
{
printf("\nSPI Channel number = %01d", spiChannelNumber);

if (spiChannelNumber == 0)
{
// Enable SSP0 block clock
SYSCON_AHBPeriphClockCmd(SYSCON_AHBPeriph_SSP0, ENABLE);

// Reset SSP0 and clock divider
SYSCON_PeriphResetCmd(SYSCON_RSTPeriph_SSP0, ENABLE);
SYSCON_PeriphResetCmd(SYSCON_RSTPeriph_SSP0, DISABLE);
SYSCON_SetSPI0ClockDiv(10);

// Assign GPIO pins for SPI
SSP_SSP0PinsInit(SCK0_PIO0_6, DISABLE); // Select P06, BUT disable

// Initialize SSP with default configuration (Master mode, 8 bit data)
SSP_CFG_Type SSP_ConfigStruct;
SSP_ConfigStructInit(&SSP_ConfigStruct);
SSP_Init(LPC_SSP0, &SSP_ConfigStruct);

// Enable SSP peripheral
SSP_Cmd(LPC_SSP0, ENABLE);
}
else // (if spiChannelNumber == 1)
{
// Enable SSP1 block clock
SYSCON_AHBPeriphClockCmd(SYSCON_AHBPeriph_SSP1, ENABLE);

// Reset SSP1 and clock divider
SYSCON_PeriphResetCmd(SYSCON_RSTPeriph_SSP1, ENABLE);
SYSCON_PeriphResetCmd(SYSCON_RSTPeriph_SSP1, DISABLE);
SYSCON_SetSPI1ClockDiv(10);

// No need to assign GPIO pins for SPI1, just disable
SSP_SSP1PinsInit(DISABLE); // disable SSEL

// Note: A bug!!! in Coocox lpc11xx_iocon.c v1.0 26Jan2010
// IOCON_SetPinFunc(IOCON_PIO2_2, PIO2_2_FUN_MISO1);
// IOCON_SetPinFunc(IOCON_PIO2_3, PIO2_3_FUN_PIO_MOSI1);
// IOCON_SetPinFunc(IOCON_PIO2_1, PIO2_1_FUN_SCK1);
// if(useSSEL == ENABLE) {
        //    IOCON_SetPinFunc(IOCON_PIO2_0, PIO2_0_FUN_SSEL1);

// Initialize SSP with default configuration (Master mode, 8 bit data)
SSP_CFG_Type SSP_ConfigStruct;
SSP_ConfigStructInit(&SSP_ConfigStruct);
SSP_Init(LPC_SSP1, &SSP_ConfigStruct);

// Enable SSP1 peripheral
SSP_Cmd(LPC_SSP1, ENABLE);
}
}


// ***************************************************************************
// Function - Setup SPI slave select ports
// Date     - 2013nov21
// ***************************************************************************
void setupSpiSlaveSelectPort(portPinArrayPointerArray portPinArraryPointerArrayArray[], \
                             uint8_t spiChannelNumber, uint8_t slaveSelectNumber)
{
initializeOutputPin01High(portPinArraryPointerArrayArray[spiChannelNumber][slaveSelectNumber]);
}

void setupAllSpiChannelSlaveSelectPorts080(portPinArrayPointerArray portPinArraryPointerArrayArray[], \
                                  uint8_t spiChannelNumber)
{
    uint8_t i;
    for (i = 0; i < MAX_SPI_SLAVE_SELECT_NUMBER; i++)
    {
    setupSpiSlaveSelectPort(portPinArraryPointerArrayArray, \
                           spiChannelNumber, i);
    }
}

// ***************************************************************************
// Function - SPI transmit fuctions
// Date     - 2013nov21
// ***************************************************************************
void spiTransmit082(portPinArrayPointerArray portPinArraryPointerArrayArray[], \
           uint8_t spiChannelNumber, uint8_t slaveSelectNumber, \
           SSP_DATA_SETUP_Type xferConfig)
{
setGpioDataPinLow01(portPinArraryPointerArrayArray[spiChannelNumber][slaveSelectNumber]);
if (spiChannelNumber == 0)
SSP_ReadWrite(LPC_SSP0, &xferConfig, SSP_TRANSFER_POLLING);
else // (spiChannelNumber == 1)
SSP_ReadWrite(LPC_SSP1, &xferConfig, SSP_TRANSFER_POLLING);
    setGpioDataPinHigh01(portPinArraryPointerArrayArray[spiChannelNumber][slaveSelectNumber]);
}

// ***************************************************************************
// Function - SPI loop back test
// Date     - 2013nov21
// ***************************************************************************
void spiLoopBack082(portPinArrayPointerArray portPinArraryPointerArrayArray[], \
                    uint8_t spiChannelNumber, uint8_t slaveSelectNumber)
{
// *** Set up SPI channel and slave select port ***
setupOneSpiChannel080(spiChannelNumber);
setupSpiSlaveSelectPort(portPinArraryPointerArrayArray, \
                       spiChannelNumber, slaveSelectNumber);

    // *** Setup transfer buffers TxBuf, RxBuf ***
#define BUFFER_SIZE 2

uint8_t TxBuf[BUFFER_SIZE];
uint8_t RxBuf[BUFFER_SIZE];
uint8_t TxBuf0[BUFFER_SIZE]; // to store init data for printing
uint8_t RxBuf0[BUFFER_SIZE]; // to store init data for printing

    // *** Setup xferConfig ***
    SSP_DATA_SETUP_Type xferConfig;
    xferConfig.tx_data = (void*)TxBuf;
    xferConfig.rx_data = (void*)RxBuf;
    xferConfig.length = BUFFER_SIZE;

    // *** Initialize transfer buffers ***************************************

    TxBuf[0] = 0x55;
    TxBuf[1] = 0xaa;
RxBuf[0] = 0x00;
RxBuf[1] = 0x00;

memcpy(TxBuf0, TxBuf, BUFFER_SIZE); // store init values
memcpy(RxBuf0, RxBuf, BUFFER_SIZE); // store init values

    // *** SPI Transfer ***
spiTransmit082(portPinArraryPointerArrayArray, \
      spiChannelNumber, slaveSelectNumber, xferConfig);

    // *** Print results *****************************************************

printf("\n*** Tx Rx buffer contents before transfer *** \n\n");
printf("Index    0  1 \n\n");
printf("%02x %02x \n", TxBuf0[0], TxBuf0[1]);
printf("%02x %02x \n", RxBuf0[0], RxBuf0[1]);

printf("\n*** Tx Rx buffer contents after transfer *** \n\n");
printf("Index    0  1 \n\n");
printf("%02x %02x \n", TxBuf[0], TxBuf[1]);
printf("%02x %02x \n", RxBuf[0], RxBuf[1]);

    // *** Debug only !!! ***

    printf("\n*** Now looping SPI transfers every mS *** \n\n");

    uint8_t i;
    for (i = 0; i < 2000; i++)
    {
    spiTransmit082(portPinArraryPointerArrayArray, \
          spiChannelNumber, slaveSelectNumber, xferConfig);
        delayMilliSecond(1);
    }
}

// ***************************************************************************
// End
// ***************************************************************************

No comments:

Post a Comment