lpcware

LPC11C24 SPI Program

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by rohit on Sun Jun 07 22:45:16 MST 2015
Hi

I have been trying to commincate between my lpc11c24(master) and altera  fpga(slave) via spi protocol.
i created my own program for the same which is given below.

#include "chip.h"
#include "board.h"
#include "LPC11xx.h"

typedef struct
{
uint8_t *txBuff;         /**< Pointer to array of bytes to be transmitted */
int     txSz;// Number of bytes in transmit array,
 

uint8_t *rxBuff;        /**< Pointer memory where bytes received from I2C be stored */

int     rxSz;// Number of bytes to received,

} SSP_XFER;



static uint8_t MOSI[16];
static uint8_t MISO[16];


void read_init()
{

    LPC_IOCON->PIO1_6                               |= 0x01;                                           //configure UART RXD pin
    LPC_IOCON->PIO1_7                               |= 0x01;                                           //configure UART TXD pin
    LPC_SYSCON->SYSAHBCLKCTRL          |= (1<<12);                                      //enable clock to UART
    LPC_UART->FCR                                      |= 0x01;                                           //enable FIFO
    LPC_UART->LCR                                      |= 0x03;                                           //set for 8 bit data width, 1 stop bit, no parity
    LPC_UART->TER                                      |= 0x80;                                           //enable transmission
    Chip_UART_SetBaud(LPC_USART, 115200);
}


static int con_get_input(const char *str)
{
int val=0,j;
uint8_t i;
ADDRESS:
DEBUGOUT("%s", str);

while(!(LPC_UART->LSR & 0x01));
i=LPC_UART->RBR;

if(i>=48 && i<=57)
{
while(i != 0)
{
val=val+(i-48);
val=val*10;
for(j=0;j<0xFFFF;j++);
i=LPC_UART->RBR;

}

DEBUGOUT("%d",val/10);
return (val/10);
}
else
{
DEBUGOUT("\r\nError..!!! Please enter a number\r\n");
goto ADDRESS;

}
}


void ssp_init()
{
DEBUGOUT("\r\nInitializing SPI.....!!!\r\n");

      LPC_SYSCON->SYSAHBCLKCTRL                |= (1<<11);       // enable clock to ssp0
           LPC_SYSCON->SSP0CLKDIV                       = 0x9B;
      LPC_SYSCON->PRESETCTRL                       = 0x01;

      LPC_IOCON->PIO0_8   |= 0x01;        //MISO Config
      LPC_IOCON->PIO0_9           |= 0x01;        //MOSI Config
      LPC_IOCON->PIO0_2   |= 0x01; //SSEL0 config
      LPC_IOCON->SCK_LOC  |= 0x02; //set sck0 in PIO0_6
      LPC_IOCON->PIO0_6   |= 0x02; //set PIO0_6 as SCK

      LPC_SSP0->CPSR    = 0x0F; 
      LPC_SSP0->CR0    = 0x0F; 
      LPC_SSP0->CR1    = 0x02;   // SSP master mode

}



static void ssp_rw_input(SSP_XFER *xfer,int ops)
{
uint8_t addr,data;

if(ops==1)
{
addr=con_get_input("\r\nEnter address to which data should be written:");
MOSI[0]=addr;
xfer->txBuff=MOSI;

data=con_get_input("\r\nEnter the data to be written:");                                         //writing only 1 byte of data for now
MOSI[1]=data;
xfer->txBuff =MOSI;
xfer->txSz=1;
}
else

if(ops==2)
{
addr=con_get_input("\r\nEnter address to from which data is to be read from:");
MOSI[0]=addr;
xfer->txSz = 1;
xfer->txBuff = MOSI;
}
}

int main(void)
{
char ch[]="\r\n\t0)exit demo"
   "\r\n\t1)write data"
"\r\n\t2)read data";
int len=2;
int flag=0;
static SSP_XFER xfer;

SystemCoreClockUpdate();
Board_Init();
read_init();

DEBUGOUT("\r\nWelcome to SPI Protocol");

int option;
ssp_init();
LABEL:

while(!flag)
{
DEBUGOUT("%s",ch);
     option=con_get_input("\r\nSelect option:");

     switch(option)
     {
      case 0:flag=1;
          DEBUGOUT("\r\n Exiting SPI demo....goodbye...\r\n");
        break;

      case 1: ssp_rw_input(&xfer,1);
           while(!(LPC_SSP0->SR & 0x02)); //check whether transmit buffer is full
           while(len)
           {
           LPC_SSP0->DR = *xfer.txBuff++;
           len--;
           }
           while(!(LPC_SSP0->SR & 0x01));// wait until byte is sent

           DEBUGOUT("\r\nData sent.... :-)");
           goto LABEL;

      case 2:
           ssp_rw_input(&xfer,2);
           while(!(LPC_SSP0->SR & 0x02));//check whether transmit buffer is full
           LPC_SSP0->DR = *xfer.txBuff;

           while(!(LPC_SSP0->SR & 0x01));// wait until byte is sent

           while(!(LPC_SSP0->SR & 0x04));//check whether recieve buffer is full
           MISO[0] = LPC_SSP0->DR;

                      DEBUGOUT("\r\nRecieved Data-->");
                      DEBUGOUT("%d",MISO[0]);
          goto LABEL;
      }
    }
return 0 ;
}

the program returns no error and wrting data has been successfull.however reading the data is not working.it always returns zero.

Can someone please help me ,i am new to LPC11C24 . Is my program entirely wrong?
PS: i did have a look at the example program,however couldnt understand much so i made this program by refering the datasheet .

Outcomes