lpcware

LPC1788 Uboot SPI driver

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by sekil on Tue Oct 23 15:55:03 MST 2012
Please help me write correctly function spi_xfer:

this my code:

int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
const void *dout, void *din, unsigned long flags)
{
unsigned intlen;
u8 tmp;
intret, i;
const uchar *txp = dout;
uchar *rxp = din;

if (bitlen == 0)
/* Finish any previously submitted transfers */
goto out;

if (bitlen % 8) {
/* Errors always terminate an ongoing transfer */
flags |= SPI_XFER_END;
printf("Error lens\n");
goto out;
}

len = bitlen >> 3;
LPC_SSP1->ICR = SSP_ICR_BITMASK;

if (flags & SPI_XFER_BEGIN) spi_cs_activate(slave);
//printf ("Activete CS ok\n");
if (dout)
{//printf ("try send dout\n");
for (i=0; i<len; i++)
{
LPC_SSP1->DR = SSP_DR_BITMASK(txp);
//printf("sends - %x\n",txp);
}
}
if (din)
{
while (LPC_SSP1->SR & SSP_SR_RNE)
{
            tmp = (SSP_DR_BITMASK(LPC_SSP1->DR));
        }
for (i=0; i<len; i++)
{
LPC_SSP1->DR = 0xff;
}
while ( (LPC_SSP1->SR & (SSP_SR_BSY|SSP_SR_RNE)) != SSP_SR_RNE );
tmp = LPC_SSP1->DR; //without this i always get 0xff in first read byte :(((
//printf ("read test = %x\n",tmp);
for (i=0;i<len;i++)
        {
if (LPC_SSP1->SR & SSP_SR_RNE) *rxp++ =    (SSP_DR_BITMASK(LPC_SSP1->DR))& 0xff;
       }
}
if (flags & SPI_XFER_END) spi_cs_deactivate(slave);
return 0;

out:
if (flags & SPI_XFER_END) spi_cs_deactivate(slave);
return 0;
}

This function work, but not normal - i get time out if i write/erase data to at45db321d SPI flash.
P.S. I'm not professional programmer - but I'm learning :)
P.S.S Sory for my English

Outcomes