<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.arm.com/utility/feedstylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>LPC2148 SPI Problem</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/31420/lpc2148-spi-problem</link><description> 
Hi 

 
I have been working on LPC2148 for a project. 
I have tried to get the spi0 working. 

 
Currently the code compiles without a problem but is not
responding as it is supposed to. 
I would like to know if there is any problem with the code in</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: LPC2148 SPI Problem</title><link>https://community.arm.com/thread/128067?ContentTypeID=1</link><pubDate>Tue, 05 Mar 2013 14:07:31 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:190f992f-9757-41b5-a725-fb1b57835c26</guid><dc:creator>mystery iit</dc:creator><description>&lt;p&gt;&lt;p&gt;
I have tried to loop back MOSI and MISO and test.&lt;/p&gt;

&lt;p&gt;
The code appears to be working sometimes based on the led
signals...&lt;br /&gt;
This is what I could infer:&lt;/p&gt;

&lt;p&gt;
1)code is sending a character&lt;br /&gt;
2)Code is receiving a character&lt;br /&gt;
3)The received character is not 0xff or the looped back character but
something else.&lt;/p&gt;

&lt;p&gt;
Is there a specific clock i need to use for spi?I use 12Mhz and
use 1/8 th for spi.&lt;br /&gt;
Should I initialize anything for loopback or receive?&lt;/p&gt;

&lt;p&gt;
The datasheet says read the S0SPSR to clear register before
receiving data...how do I do that?&lt;/p&gt;

&lt;p&gt;
Here is the code for your reference&lt;/p&gt;

&lt;pre&gt;

#include  &amp;lt;lpc214x.h&amp;gt;              //Includes LPC2148 register definitions


void spi_init();                        //To define SPI
void spi_send(char p);
char spi_receive();

int  main(void)
{

        char p=&amp;#39;l&amp;#39;;
        int i=0,j=0,k=0;
        for(j=0;j&amp;lt;12000;j++)
                        {
                                        for(k=0;k&amp;lt;1000;k++);
                        }
   PINSEL0 = (1&amp;lt;&amp;lt;8)|(1&amp;lt;&amp;lt;10)|(1&amp;lt;&amp;lt;12)|(1&amp;lt;&amp;lt;14);                // Enable MISO MOSI SCK and Slave Select
   PINSEL1 = 0x00000000;
   PINSEL2 = 0x00000000;
         IO0DIR = (1&amp;lt;&amp;lt;14);
         IO0SET |=(1&amp;lt;&amp;lt;14);                                        //Keep slave select as negetive always
   IO1DIR = (1&amp;lt;&amp;lt;19) | (1&amp;lt;&amp;lt;18) | (1&amp;lt;&amp;lt;17) | (1&amp;lt;&amp;lt;16);
                spi_init();                                     //Initialize spi

        while (1)
        {

                spi_send(&amp;#39;0&amp;#39;);

        //Send a character and receive the same character
                while(p!=&amp;#39;0&amp;#39;)
                {

                        p=spi_receive();
                        if(p==(char)0xff)
                        {
                                IO1CLR=(1&amp;lt;&amp;lt;19)|(1&amp;lt;&amp;lt;18)|(1&amp;lt;&amp;lt;17)|(1&amp;lt;&amp;lt;16);                 //to check if no data is received without interrupt flag
                        }
                }
                while(p==&amp;#39;0&amp;#39;)
                {
                        IO1CLR=(1&amp;lt;&amp;lt;19)|(1&amp;lt;&amp;lt;18)|(1&amp;lt;&amp;lt;17)|(1&amp;lt;&amp;lt;16);                         //if data is received properly
                        IO1SET=(1&amp;lt;&amp;lt;19)|(1&amp;lt;&amp;lt;17)|(1&amp;lt;&amp;lt;16);
                        while(1);
                }


}
}

void spi_init()
{
        S0SPCR|=0x20;         //Master Mode
        S0SPCCR=0x8;

}
void spi_send(char p)
{
        IO0CLR |=(1&amp;lt;&amp;lt;14);
        S0SPDR = p;
  while ((!S0SPSR &amp;amp; 0x80));
        S0SPSR &amp;amp;=~ 0x80;
        IO0SET |=(1&amp;lt;&amp;lt;14);
}
char spi_receive()
{
        char p;
        IO0CLR |=(1&amp;lt;&amp;lt;14);
        S0SPDR=0xff;
        IO1SET=(1&amp;lt;&amp;lt;18)|(1&amp;lt;&amp;lt;17)|(1&amp;lt;&amp;lt;16);     //to check if function is entered
  while ((!S0SPSR &amp;amp; 0x80));
        p=S0SPDR;
        IO1SET=(1&amp;lt;&amp;lt;19)|(1&amp;lt;&amp;lt;17)|(1&amp;lt;&amp;lt;16);     //to check if data is received
        return p;
        IO0SET |=(1&amp;lt;&amp;lt;14);
}
&lt;/pre&gt;

&lt;p&gt;
Any help is gratefully appreciated.&lt;/p&gt;

&lt;p&gt;
Thank You&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: LPC2148 SPI Problem</title><link>https://community.arm.com/thread/117210?ContentTypeID=1</link><pubDate>Tue, 05 Mar 2013 09:30:36 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:887b81ab-d396-4351-9232-e136b53d3c0b</guid><dc:creator>mystery iit</dc:creator><description>&lt;p&gt;&lt;p&gt;
Thank you for your answer&lt;/p&gt;

&lt;p&gt;
I use flash magic to program the MCU and load the value of the
flash start and all...&lt;/p&gt;

&lt;p&gt;
I haven&amp;#39;t used any of the above code for UART and it works.So I
feel that is not the solution.&lt;br /&gt;
Is there something else wrong with the code?or am i forgetting to
initialize something important?&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: LPC2148 SPI Problem</title><link>https://community.arm.com/thread/84841?ContentTypeID=1</link><pubDate>Mon, 04 Mar 2013 13:11:07 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:a05f7e51-56af-4bc3-96f5-7951f249e69e</guid><dc:creator>Lucio Faustinelli</dc:creator><description>&lt;p&gt;&lt;p&gt;
----------------------------------&lt;br /&gt;
STARTUP CODE (startup_LPC17xx.s)&lt;br /&gt;
----------------------------------&lt;br /&gt;
Reset_Handler PROC&lt;br /&gt;
EXPORT Reset_Handler [WEAK]&lt;br /&gt;
IMPORT SystemInit&lt;br /&gt;
IMPORT __main&lt;br /&gt;
MOV R0,#0&lt;br /&gt;
MSR CONTROL, R0&lt;br /&gt;
LDR R0, =SystemInit&lt;br /&gt;
BLX R0&lt;br /&gt;
LDR R0, =__main&lt;br /&gt;
BX R0&lt;br /&gt;
ENDP&lt;/p&gt;

&lt;p&gt;
----------------------------&lt;br /&gt;
EXECUTE USER CODE FUNCTION&lt;br /&gt;
----------------------------&lt;br /&gt;
void execUserCode( void )&lt;br /&gt;
{ __disable_irq( );&lt;br /&gt;
SCB -&amp;gt; VTOR = ( USER_FLASH_START &amp;amp; 0x1FFFFF80 );&lt;br /&gt;
bootJump( USER_FLASH_START ); // User Flash start is the position of
the vector table for the app }&lt;/p&gt;

&lt;p&gt;
-----------------------------------&lt;br /&gt;
BOOT JUMP TO APPLICATION FUNCTION&lt;br /&gt;
-----------------------------------&lt;br /&gt;
__asm void bootJump( U32 address )&lt;br /&gt;
{ LDR SP, [R0]&lt;br /&gt;
LDR PC, [R0, #4]&lt;br /&gt;
}&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: LPC2148 SPI Problem</title><link>https://community.arm.com/thread/81041?ContentTypeID=1</link><pubDate>Mon, 04 Mar 2013 13:08:22 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:d3669329-0974-48a5-a345-571c7de6f5f3</guid><dc:creator>mystery iit</dc:creator><description>&lt;p&gt;&lt;p&gt;
I apologize for the inconvenience...here is the code with
comments:&lt;/p&gt;

&lt;p&gt;
The code sends a predefined data and on getting the same data back
,changes the state of its pins .LPC2148 acts as a master to a device
which relays the same data sent to it.&lt;/p&gt;

&lt;pre&gt;

#include  &amp;lt;lpc214x.h&amp;gt;              //Includes LPC2148 register definitions


void spi_init();                        //To define SPI
void spi_send(char p);
char spi_receive();

int  main(void)
{
        char p;
        int i=0;
   PINSEL0 = (1&amp;lt;&amp;lt;8)|(1&amp;lt;&amp;lt;10)|(1&amp;lt;&amp;lt;12)|(1&amp;lt;&amp;lt;14);                // Enable MISO MOSI SCK and Slave Select
   PINSEL1 = 0x00000000;
   PINSEL2 = 0x00000000;
         IO0DIR = (1&amp;lt;&amp;lt;14);
         IO0CLR |=(1&amp;lt;&amp;lt;14);                                        //Keep slave select as negetive always
   IO1DIR = (1&amp;lt;&amp;lt;19) | (1&amp;lt;&amp;lt;18) | (1&amp;lt;&amp;lt;17) | (1&amp;lt;&amp;lt;16);
                spi_init();                                     //Initialize spi

        while (1)
        {
                spi_send(&amp;#39;0&amp;#39;);                                  //Send a character and receive the same character
                while(p!=&amp;#39;0&amp;#39;)
                {
                        p=spi_receive();
                }

                spi_send(&amp;#39;x&amp;#39;);
                while(p!=&amp;#39;x&amp;#39;)
                {
                        p=spi_receive();
                }

                spi_send(&amp;#39;f&amp;#39;);
                while(p!=&amp;#39;f&amp;#39;)
                {
                        p=spi_receive();
                }

                spi_send(&amp;#39;f&amp;#39;);
                while(p!=&amp;#39;f&amp;#39;)
                {
                        p=spi_receive();
                }
                i+=1;
                p=&amp;#39;o&amp;#39;;
                switch(i%4)     //change state of pin when the given configuration is sent
                {
                        case 1:
                        IO1CLR=(1&amp;lt;&amp;lt;19)|(1&amp;lt;&amp;lt;18)|(1&amp;lt;&amp;lt;17)|(1&amp;lt;&amp;lt;16);
                        IO1SET=(1&amp;lt;&amp;lt;19);
                        break;
                        case 2:
                        IO1CLR=(1&amp;lt;&amp;lt;19)|(1&amp;lt;&amp;lt;18)|(1&amp;lt;&amp;lt;17)|(1&amp;lt;&amp;lt;16);
                        IO1SET=(1&amp;lt;&amp;lt;18);
                        break;
                        case 3:
                        IO1CLR=(1&amp;lt;&amp;lt;19)|(1&amp;lt;&amp;lt;18)|(1&amp;lt;&amp;lt;17)|(1&amp;lt;&amp;lt;16);
                        IO1SET=(1&amp;lt;&amp;lt;17);
                        break;
                        case 0:
                        IO1CLR=(1&amp;lt;&amp;lt;19)|(1&amp;lt;&amp;lt;18)|(1&amp;lt;&amp;lt;17)|(1&amp;lt;&amp;lt;16);
                        IO1SET=(1&amp;lt;&amp;lt;16);
                        break;

}
}
}
void spi_init()
{
        S0SPCR|=(1&amp;lt;&amp;lt;5);          //Master mode
        S0SPCCR=0x60;            //12500 Hz clock

}
void spi_send(char p)
{
        int value;
        value=(int)p;
        S0SPDR = value;
  while (!(S0SPSR &amp;amp; 0x80));         //checks for data flag to go off
}
char spi_receive()
{
        char p;
  while (!(S0SPSR &amp;amp; 0x80));          //waits till data is received.
        p=(char)S0SPDR;
        return p;
}

&lt;/pre&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: LPC2148 SPI Problem</title><link>https://community.arm.com/thread/68133?ContentTypeID=1</link><pubDate>Mon, 04 Mar 2013 10:51:36 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:1f7b4548-cabe-4b9f-a070-5292b8a61ebd</guid><dc:creator>ImPer Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
Doesn&amp;#39;t look readable.&lt;/p&gt;

&lt;p&gt;
You forgot to tag it as code.&lt;/p&gt;

&lt;p&gt;
You forgot to use comments, to actually document your code. Magic
constants doesn&amp;#39;t help you.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>