This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

SPI problem with LPC2129 (using MCB2100)

I have pull up resistors on the /SSEL pins of the SPI bus. I want to use the LPC2129 as master, let him send 10 bytes .. 1 time Ox55 and 9 times 0x00 but it seems not to work. Is there something wrong with the code?

With a scope I see the P0.0 work well as chip selector but I don't see a clk running also no data on the mosi/miso lines.

Here is my code:


#include <LPC21xx.H>
#include "Timer.h"                                    functions
unsigned int dummy;
unsigned int status = 0x0;
unsigned int s          = 0x0;
unsigned int i          = 0x0;
unsigned int cnt        = 0x0;
unsigned int buf[10];



void SPI0_Init(void)
{
        S0SPCCR         = 0xFA;
        S0SPCR          = 0x28;
        IOSET1          = 0x00020000;
}

int SPI0_WriteAA(void)
{
        S0SPDR = 0x55;

        while(!(S0SPSR & 0x80)){}

        if (S0SPSR != 0x80)
        {
                IOCLR1          = 0x00400000;
                IOSET1          = 0x00800000;
                status          = 0x1;
        }
        else
        {
                IOCLR1          = 0x00800000;
                IOSET1          = 0x00400000;
                status          = 0x0;
        }

        s = S0SPSR;

        return(status);
}

int SPI0_WriteFF(void)
{
        S0SPDR  = 0x00;

        while(!(S0SPSR & 0x80)){};

        if (S0SPSR != 0x80)
        {
                IOCLR1          = 0x00400000;
                IOSET1          = 0x00800000;
                status          = 0x1;
        }
        else
        {
                IOCLR1          = 0x00800000;
                IOSET1          = 0x00400000;
                status          = 0x0;
        }

        s = S0SPSR;

        return(status);
}

int SPI0_Read(void)
{
        dummy = S0SPDR;                                                                 //read data register
        buf[cnt] = dummy;
        if (cnt < 10) {
        cnt++;
        }
        return(dummy);
}

void wait (void)
{
  unsigned long i;

  i = timeval;
  while ((i + 100) != timeval);
}

void waitstartup (void)
{
  unsigned long i;

  i = timeval;
  while ((i + 15) != timeval);
}

int main (void)
{
        int c;

        PINSEL0         = 0x00005500;
        IODIR0          = 0x00010001;
        IOCLR0          = 0xFFFFFFFF;
        IOSET0          = 0x00010001;
        PINSEL1         = 0x154542A8;
        IODIR1          = 0x00FF0000;
        IOCLR1          = 0x00FF0000;
        IOSET1          = 0x00010000;

        SPI0_Init();
        init_timer();
        waitstartup();

        IOCLR0  = 0x00000001;
        IOSET1  = 0x00040000;

        while (1)
        {
                if (i==0)
                {
                        SPI0_WriteAA();

                        if (status == 0x0)
                        {
                                SPI0_Read();
                        }

                        for (c=0;c<9;c++)
                        {
                                SPI0_WriteFF();

                                if (status == 0x0)
                                {
                                        SPI0_Read();
                                }
                        }
                   i=1;

                IOSET0          = 0x00000001;
                wait();
                IOCLR1          = 0x00040000;
                }
        }
}

Parents
  • The missing edit buttons can in some cases be handled by the preview function.

    A couple of notes about your comments:

    PINSEL0 selects mode for a numbers of P0.x pins, so the comment shouldn't mention "Pin select" but what special configuration you are specifying (such as enabling the SPI pins).

    IODIR0 selects I/O direction for a number of pins, so the comment "Select pin direction" doesn't give much extra help. Which pins are set as outputs?

    The comment "GPIO P0.3 and P0.16 as outputs" doesn't seem to match the I0DIR0 assign.

    If you declare a constant SSEL = 0, you may write

    IOSET1 = 1 << SSEL;
    

    Your program performs just about everything using global variables. That doesn't help you a lot. It isn't visible which global variables are updated by which calls, so when reading the code, you can not see if an if statement makes sense or not.

    Note how similar SPI0_WriteAA and SPI0_WriteFF are? That may be a good indication that you should just have a SPI0_Write() function, and use a parameter to inform what value to send.

Reply
  • The missing edit buttons can in some cases be handled by the preview function.

    A couple of notes about your comments:

    PINSEL0 selects mode for a numbers of P0.x pins, so the comment shouldn't mention "Pin select" but what special configuration you are specifying (such as enabling the SPI pins).

    IODIR0 selects I/O direction for a number of pins, so the comment "Select pin direction" doesn't give much extra help. Which pins are set as outputs?

    The comment "GPIO P0.3 and P0.16 as outputs" doesn't seem to match the I0DIR0 assign.

    If you declare a constant SSEL = 0, you may write

    IOSET1 = 1 << SSEL;
    

    Your program performs just about everything using global variables. That doesn't help you a lot. It isn't visible which global variables are updated by which calls, so when reading the code, you can not see if an if statement makes sense or not.

    Note how similar SPI0_WriteAA and SPI0_WriteFF are? That may be a good indication that you should just have a SPI0_Write() function, and use a parameter to inform what value to send.

Children
No data