2013-10-19

SPI transfer loop back testing OK

Now I have added the following code to check if SPI transfer is OK.  With and without looping back MOSI to MISO, the test functions blinks 2 times and 4 times respectively.  So everything has been running smoothly.

    // *** Check loop back data
    uint8_t xferStatus = 1;
    uint8_t k;
    for (k = 0; k < BUFFER_SIZE; k++)
    {
     if (Rx_Buf[k] != Tx_Buf[k])
     {
     xferStatus = 0;
     break;
     }
    }

    if (xferStatus == 1)
       blinkOneLed050(PortPinArrayP03, ONE_FIFTH_SECOND, ONE_HALF_SECOND, BLINK_2_TIMES);
    else
        blinkOneLed050(PortPinArrayP03, ONE_FIFTH_SECOND, ONE_HALF_SECOND, BLINK_4_TIMES);

.END

// ***********************************************************************
// Program  - Fong EKG v5.0
// Function - Olimex EKG Board Evaluation
// Author   - TL Fong
// Build    - 2013.10.19.03
// Date     - 2013oct19hkt2104
// Hardware - Olimex/CooCox/MagicBlue/WHUT/Somy LPC1114/C14/301/FN28
//            Olimex EKG board (SHIELD-EKG/EMG Rev B, Bulgaria 2011)
// Software - GCC ARM 4.7, CoIDE 1.7.4,  CoLinkEx 1.1, Flash Magic v7.51
// ***********************************************************************

#include "test050.h" // main tests

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

int main()
{

testSpi03();
}

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



// ***********************************************************************
// test050.h 2013oct19hkt2105
// ***********************************************************************

#include "led050.h"
#include "key050.h"
#include "spi050.h"

void testSpi03() 
{
// *** Blink LED at P03 ***
setupGpioPinOutputLow050(PortPinArrayP03); // setup GPIO pin as output
blinkLed0501(PortPinArrayP03); // blink led

// *** Test SPI loopback ***
loopBackSpi03();
}

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



// ***********************************************************************
// spi050.h 2013oct19hkt2108
// ***********************************************************************

#include "lpc11xx_ssp.h"
#include "led050.h"

void loopBackSpi03()
{
// Setup and initialize transfer buffers 
#define BUFFER_SIZE 0x04
uint8_t Tx_Buf[BUFFER_SIZE];
uint8_t Rx_Buf[BUFFER_SIZE];
initBuffer(Tx_Buf, Rx_Buf, 0xa5, 0x00);

    // Setup SPI0 with GPIO pin P06 as SPI clock
setUpSpi050(SCK0_PIO0_6);

    // Setup xfer data configuration struct
    SSP_DATA_SETUP_Type xferConfig;
    xferConfig.tx_data = Tx_Buf;
    xferConfig.rx_data = Rx_Buf;
    xferConfig.length = BUFFER_SIZE;

    // *** Repeat transfer for oscilloscope to display signal waveforms ***
    /* uint8_t j;
    for (j = 0; j < 10000000; j++)
    {
    delayMilliSecond(1);
    setGpioDataPinHigh01(PortPinArrayP03);
        SSP_ReadWrite(LPC_SSP0, &xferConfig, SSP_TRANSFER_POLLING);
        setGpioDataPinLow01(PortPinArrayP03);
    } */

    // *** Single transfer ***
    SSP_ReadWrite(LPC_SSP0, &xferConfig, SSP_TRANSFER_POLLING);

    // *** Check loop back data
    uint8_t xferStatus = 1;
    uint8_t k;
    for (k = 0; k < BUFFER_SIZE; k++)
    {
    if (Rx_Buf[k] != Tx_Buf[k])
    {
    xferStatus = 0;
    break;
    }
    }

    if (xferStatus == 1)
      blinkOneLed050(PortPinArrayP03, ONE_FIFTH_SECOND, ONE_HALF_SECOND, BLINK_2_TIMES);
    else
        blinkOneLed050(PortPinArrayP03, ONE_FIFTH_SECOND, ONE_HALF_SECOND, BLINK_4_TIMES);
}

// *** Private SPI test functions ***

void setUpSpi050(SCK0_Position_Typedef sck0)
{
// 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, ENABLE);

// 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);
}

void initBuffer(uint8_t Tx_Buf[BUFFER_SIZE], uint8_t Rx_Buf[BUFFER_SIZE], uint8_t TxInitByte, uint8_t RxInitByte)
{
uint8_t i;
for (i = 0; i < BUFFER_SIZE; i++)
{
Tx_Buf[i] = TxInitByte;
Rx_Buf[i] = RxInitByte;
}
}

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



No comments:

Post a Comment