2013-11-20

SPI Loop Back testing notes

*** Setting up all SPI Channels ***

SPI Channel number = 0
SPI Channel number = 1

*** Setting up all Slave Selects ***

Setting up all SPI Slave Select ports
SPI = 0, 0  Port = 0, 2
SPI = 0, 1  Port = 0, 7
SPI = 0, 2  Port = 0, 11
SPI = 0, 3  Port = 2, 5
SPI = 0, 4  Port = 1, 9
SPI = 0, 5  Port = 2, 4
Setting up all SPI Slave Select ports
SPI = 1, 0  Port = 2, 0
SPI = 1, 1  Port = 2, 0
SPI = 1, 2  Port = 2, 0
SPI = 1, 3  Port = 2, 0
SPI = 1, 4  Port = 2, 0
SPI = 1, 5  Port = 2, 0

*** End of Setting up ***    

*** Tx Rx buffer contents before transfer ***
Index    0  1
55 aa
00 00

*** Tx Rx buffer contents after transfer ***
Index    0  1
55 aa
00 00

*** Now looping SPI transfers every mS ***

...

// ***************************************************************************
// Program  - Fong EKG v5.08
// Function - Testing Olimex EKG
// Author   - TL Fong
// Build    - 2013.11.20.01
// Date     - 2013nov20hkt1201
// Hardware - WuyinS/Olimex/CooCox/MagicBlue/WHUT/Somy LPC1114/C14/301
//            Olimex EKG/EMG R.B 2011
// Software - CoIDE 1.7.5, CoLinkEx 1.1, Flash Magic v7.66, GCC ARM 4.7
// **************************************************************************

#include "test050.h"

// ***************************************************************************
// Main Function
// ***************************************************************************

int main()
{
testSpiLoopBack();
}


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




// ***********************************************************************
// test050.h 2013nov20hkt0942
// ***********************************************************************

#include "led050.h"
#include "key050.h"
#include "spi050.h"
#include "adc050.h"
#include "eeprom050.h"
#include "mled050.h"
#include "spi080.h"

void testSpiLoopBack()
{
setupAllSpiChannelAndSlaveSelect080();
// spiLoopBack080(SPI_CHANNEL_0, SLAVE_SELECT_0); // good
// spiLoopBack080(SPI_CHANNEL_0, SLAVE_SELECT_1); // good
// spiLoopBack080(SPI_CHANNEL_0, SLAVE_SELECT_2); // bad
// spiLoopBack080(SPI_CHANNEL_0, SLAVE_SELECT_3); // bad
// spiLoopBack080(SPI_CHANNEL_0, SLAVE_SELECT_4); // good
// spiLoopBack080(SPI_CHANNEL_0, SLAVE_SELECT_5); // bad
}

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




// ***********************************************************************
// spi080.h 2013nov20hkt0915
// ***********************************************************************

#include "gpio050.h"
#include "led050.h"
#include "lpc11xx_ssp.h"
#include "semihosting.h"
#include "stdio.h"
#include "config050.h"

#ifndef SPI_HEADER_SEEN
#define SPI_HEADER_SEEN

// *** SPI #defines ***

#define MAX_SPI_CHANNEL_NUMBER 2
#define MAX_SPI_SLAVE_SELECT_NUMBER 6

#define SPI_CHANNEL_0 0
#define SPI_CHANNEL_1 1

#define SLAVE_SELECT_0 0
#define SLAVE_SELECT_1 1
#define SLAVE_SELECT_2 2
#define SLAVE_SELECT_3 3
#define SLAVE_SELECT_4 4
#define SLAVE_SELECT_5 5

// *** SPI PortPinArray assignment ***

portPinArray PortPinArraySsel0  = {PORT0, PIN2};
portPinArray PortPinArraySsel1  = {PORT2, PIN0};

portPinArray PortPinArraySsel00 = {PORT0, PIN2};
portPinArray PortPinArraySsel01 = {PORT0, PIN7};
portPinArray PortPinArraySsel02 = {PORT0, PIN11};
portPinArray PortPinArraySsel03 = {PORT2, PIN5};
portPinArray PortPinArraySsel04 = {PORT1, PIN9};
portPinArray PortPinArraySsel05 = {PORT2, PIN4};

portPinArray PortPinArraySsel10 = {PORT2, PIN0};
portPinArray PortPinArraySsel11 = {PORT2, PIN0};
portPinArray PortPinArraySsel12 = {PORT2, PIN0};
portPinArray PortPinArraySsel13 = {PORT2, PIN0};
portPinArray PortPinArraySsel14 = {PORT2, PIN0};
portPinArray PortPinArraySsel15 = {PORT2, PIN0};

// portPinArray *SlaveSelectPortPinArrayPointerArray0[] = {&PortPinArraySsel00, &PortPinArraySsel01};
// portPinArray *SlaveSelectPortPinArrayPointerArray1[] = {&PortPinArraySsel00, &PortPinArraySsel01};

portPinArray *SlaveSelectPortPinArrayPointerArrayArray[2][6] =
          {&PortPinArraySsel00, &PortPinArraySsel01, &PortPinArraySsel02, \
           &PortPinArraySsel03, &PortPinArraySsel04, &PortPinArraySsel05, \
           &PortPinArraySsel10, &PortPinArraySsel11, &PortPinArraySsel12, \
           &PortPinArraySsel13, &PortPinArraySsel14, &PortPinArraySsel15};

// ***************************************************************************
// Function - Setup SPI channels and slave select ports
// 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);
}
}

void setupOneSpiSlaveSelect080(uint8_t spiChannelNumber, uint8_t slaveSelectNumber)
{
// portPinArray *slaveSelectPortPinArrayPointer;
// slaveSelectPortPinArrayPointer = SlaveSelectPortPinArrayPointerArrayArray[spiChannelNumber][slaveSelectNumber];
// printf("\nslaveSelectPortPinArray Port = %02d \n", (*slaveSelectPortPinArrayPointer)[PORT_INDEX]);
// printf("slaveSelectPortPinArray Pin  = %02d \n", (*slaveSelectPortPinArrayPointer)[PIN_INDEX]);
// setupGpioPinOutputHigh050(*slaveSelectPortPinArrayPointer);

// printf("\nPortPinArray Port = %1d  ", (*(SlaveSelectPortPinArrayPointerArrayArray[spiChannelNumber][slaveSelectNumber]))[PORT_INDEX]);
// printf("PortPinArray Pin  = %01d \n",  (*(SlaveSelectPortPinArrayPointerArrayArray[spiChannelNumber][slaveSelectNumber]))[PIN_INDEX]);

   printf("Port = %1d, %1d", \
     (*(SlaveSelectPortPinArrayPointerArrayArray[spiChannelNumber][slaveSelectNumber]))[PORT_INDEX], \
     (*(SlaveSelectPortPinArrayPointerArrayArray[spiChannelNumber][slaveSelectNumber]))[PIN_INDEX]);
setupGpioPinOutputHigh050(*(SlaveSelectPortPinArrayPointerArrayArray[spiChannelNumber][slaveSelectNumber]));
}

void setupAllSpiSlaveSelect080(uint8_t spiChannelNumber)
{
uint8_t i; // Slave select number 0 ~ 5

printf("\nSetting up all SPI Slave Select ports");

    i = 0;
    printf("\nSPI = %1d, %1d  ", spiChannelNumber, i);
setupOneSpiSlaveSelect080(spiChannelNumber, i);

    i = 1;
    printf("\nSPI = %1d, %1d  ", spiChannelNumber, i);
setupOneSpiSlaveSelect080(spiChannelNumber, i);

    i = 2;
    printf("\nSPI = %1d, %1d  ", spiChannelNumber, i);
setupOneSpiSlaveSelect080(spiChannelNumber, i);

    i = 3;
    printf("\nSPI = %1d, %1d  ", spiChannelNumber, i);
setupOneSpiSlaveSelect080(spiChannelNumber, i);

    i = 4;
    printf("\nSPI = %1d, %1d  ", spiChannelNumber, i);
setupOneSpiSlaveSelect080(spiChannelNumber, i);

    i = 5;
    printf("\nSPI = %1d, %1d  ", spiChannelNumber, i);
setupOneSpiSlaveSelect080(spiChannelNumber, i);
}

void setupAllSpiChannelAndSlaveSelect080()
{
printf("\n\n*** Setting up all SPI Channels ***\n\n");
setupOneSpiChannel080(SPI_CHANNEL_0);
setupOneSpiChannel080(SPI_CHANNEL_1);

printf("\n\n*** Setting up all Slave Selects ***\n\n");
setupAllSpiSlaveSelect080(SPI_CHANNEL_0);
setupAllSpiSlaveSelect080(SPI_CHANNEL_1);

printf("\n\n*** End of Setting up ***\n\n");
printf("\n\n                         \n\n");
printf("\n\n                         \n\n");
}

void spiTransmit080(uint8_t spiChannelNumber, uint8_t slaveSelectNumber, \
           SSP_DATA_SETUP_Type xferConfig)
{
portPinArray *slaveSelectPortPinArrayPointer;
slaveSelectPortPinArrayPointer = SlaveSelectPortPinArrayPointerArrayArray[spiChannelNumber][slaveSelectNumber];

if (spiChannelNumber == 0)
{
setGpioDataPinLow01(*SlaveSelectPortPinArrayPointerArrayArray[spiChannelNumber][slaveSelectNumber]);
SSP_ReadWrite(LPC_SSP0, &xferConfig, SSP_TRANSFER_POLLING);
setGpioDataPinHigh01(*SlaveSelectPortPinArrayPointerArrayArray[spiChannelNumber][slaveSelectNumber]);
}
else // (spiChannelNumber == 1)
{
setGpioDataPinLow01(*SlaveSelectPortPinArrayPointerArrayArray[spiChannelNumber][slaveSelectNumber]);
SSP_ReadWrite(LPC_SSP1, &xferConfig, SSP_TRANSFER_POLLING);
setGpioDataPinHigh01(*SlaveSelectPortPinArrayPointerArrayArray[spiChannelNumber][slaveSelectNumber]);
}
}

void spiLoopBack080(uint8_t spiChannelNumber, uint8_t 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 ***
spiTransmit080(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");
    while (1)
    {
        spiTransmit080(spiChannelNumber, slaveSelectNumber, xferConfig);
        delayMilliSecond(1);
    }
}

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




No comments:

Post a Comment