SPI: Arduino (master) with LPC1114 (slave)

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

SPI: Arduino (master) with LPC1114 (slave)

3,892件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by dirtyBits on Fri Sep 16 13:03:17 MST 2011
I want to connect [B]LPC1114 [/B] as SPI slave with [B]Arduino Duemilanove[/B] (ATmega328) as SPI master. But having trouble finding [B]built functions/macros/code[/B] to [B]receive data[/B] by LPC1114 as SSP.
[IMG]http://i54.tinypic.com/20jfzpg.jpg[/IMG]
I have code ready and works [B]fine[/B] from Arduino side but need help writing the code for LPCXpresso LPC1114. [B]I can't figure out where to begin with? [/B]:mad:
#include <SPI.h>                     // SPI library
const int SS = 10;                   // SS pin no.10
byte dataP[5] = {45,15,2,95,105};

void setup()
{  
  pinMode(SS, OUTPUT);               // SS as Output
  SPI.begin();                       // initialize SPI
}

void loop() 
{
  digitalWrite(SS,LOW);              // SS enable
  for(int i=0; i<5; i++)
  {
    SPI.transfer(dataP);        // Transfer data
  }
  digitalWrite(SS,HIGH);             // SS disable
}


SPI settings are as follow;
MSB first
8 bits per transfer
CPOL=0
CPHA=0
Max clock frequency 4MHz!
0 件の賞賛
返信
16 返答(返信)

3,414件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by navayo on Wed Apr 17 08:03:23 MST 2013
Just for reference:
LPC11xx user manual page 226 describes that in continuous send mode (arduino as master) the chip select must be pulled up after  every byte.
0 件の賞賛
返信

3,414件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ktownsend on Thu Sep 22 08:15:52 MST 2011
Have you had any luck with this?
0 件の賞賛
返信

3,414件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ktownsend on Tue Sep 20 07:52:44 MST 2011
[SIZE=3][B]NXP_Europe, NXP_USA[/B][/SIZE]

[SIZE=3]Can anyone provide a [B]simple code[/B] for LPC1114 to receive 8 byte of Data in SLave SPI mode?[/SIZE]
[SIZE=3]Or is it too much to ask? :mad:[/SIZE]

Not to put too sharp a point on it, but everyone here who figured out the relatively straight-forward thing you're trying to do accomplished it the same way ... reading the manuals and looking at the information that is out there, and then using a bit of logic to piece it all together. People can give you answers (I posted some code myself for you for SPI), but they can't teach you to think for yourself which is the most important thing if you really want to do anything useful with embedded devices. You seems to follow the same development model a lot of forum posters do on a lot of forums ... GIVE ME A SAMPLE NOW .. and when it doesn't work ... WHY DOESN'T THIS WORK, WHAT'S WRONG. ITS VERY IMPORTANT I HAVE THIS NOW. :(

I'm happy to help peoplem beginners included, but no one is helping you by not making you think for yourself a bit as well. SPI is probably the easiest serial bus available. I don't say that to criticise you, only to say that it's the easiest one to try to understand and get working on your own because it's so simple compared to I2C or some other more complicated options.  It's a godo place to start to learn and digest yourself rather than something nasty like USB.

You really need to make an effort to fill in the blanks yourself. There are many people on this forum (myself included) who are happy to help, but spoon-feeding won't help anyone and you will need to learn to learn yourself if you want to get very far in the embedded world.
0 件の賞賛
返信

3,414件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Mon Sep 19 05:49:41 MST 2011
Come on, that's not too difficult :eek:
Just switching to slave mode and reading busy and receive bit

LPCXpresso has 2 SSP controller, so just use semihosting, connect 4 wires and write from master to slave.

/*
===============================================================================
 Name        : main.c of LPC11_SPI_4_dummies sample
 Author      : Zero
 Version     : 1.0
 Copyright   : no copyright, sample
 Description : SSP sample connect ssp0 - ssp1: MISO / MOSI / Sck / SSEL
===============================================================================
*/

#ifdef __USE_CMSIS
#include "LPC11xx.h"
#endif

#include <cr_section_macros.h>
#include <NXP/crp.h>

// Variable to store CRP value in. Will be placed automatically
// by the linker when "Enable Code Read Protect" selected.
// See crp.h header for more information
__CRP const unsigned int CRP_WORD = CRP_NO_CRP ;

#include <stdio.h>

#define USE_CS0            1            //0=GPIO
#define FIFOSIZE        8

/* Port0.2 is the SSP select pin */
#define SSP0_SEL        (0x1<<2)

/* SSP Status register */
#define SSPSR_TFE       (0x1<<0)
#define SSPSR_TNF       (0x1<<1)
#define SSPSR_RNE       (0x1<<2)
#define SSPSR_RFF       (0x1<<3)
#define SSPSR_BSY       (0x1<<4)

/* SSP CR1 register */
#define SSPCR1_LBM      (0x1<<0)
#define SSPCR1_SSE      (0x1<<1)
#define SSPCR1_MS       (0x1<<2)
#define SSPCR1_SOD      (0x1<<3)

volatile uint8_t sp0_read;
volatile uint8_t sp0_write;
volatile uint8_t sp1_read;
volatile uint8_t sp1_write;

void SPI0_init(void)
{
 uint8_t i, Dummy;
 LPC_SYSCON->PRESETCTRL |= (0x1<<0);
 LPC_SYSCON->SYSAHBCLKCTRL |= (0x1<<11);//clock SSP0
 LPC_SYSCON->SSP0CLKDIV = 0x02;            //Divided by 2
 LPC_IOCON->PIO0_8           &= ~0x07;    //SSP I/O config
 LPC_IOCON->PIO0_8           |= 0x01;    //SSP MISO
 LPC_IOCON->PIO0_9           &= ~0x07;
 LPC_IOCON->PIO0_9           |= 0x01;    //SSP MOSI
 LPC_IOCON->SCK_LOC = 0x01;
 LPC_IOCON->PIO2_11 = 0x01;
#if USE_CS0
  LPC_IOCON->PIO0_2 &= ~0x07;
  LPC_IOCON->PIO0_2 |= 0x01;            //SSP SSEL
#else
/* Enable AHB clock to the GPIO domain. */
 LPC_SYSCON->SYSAHBCLKCTRL |= (1<<6);
 LPC_IOCON->PIO0_2 &= ~0x07;        /* SSP SSEL is a GPIO pin */
  /* port0, bit 2 is set to GPIO output and high */
  LPC_GPIO0->DIR  |= (1<<2);
  LPC_GPIO0->DATA |= (1<<2);
#endif
// Set DSS data to 8-bit, Frame format SPI, CPOL = 0, CPHA = 0, and SCR is 12
 LPC_SSP0->CR0 = 0x0B07;
// SSPCPSR clock prescale register, master mode, minimum divisor is 0x02
 LPC_SSP0->CPSR = 0x2;
 for ( i = 0; i < FIFOSIZE; i++ )
 {
  Dummy = LPC_SSP0->DR;                    //clear the RxFIFO
 }
//master, SSP Enabled
 LPC_SSP0->CR1 = SSPCR1_SSE;
 return;
}

void SPI1_init(void)
{
 uint8_t i, Dummy;
 LPC_SYSCON->PRESETCTRL |= (0x1<<2);
 LPC_SYSCON->SYSAHBCLKCTRL |= (0x1<<18);//clock SSP1
 LPC_SYSCON->SSP1CLKDIV = 0x02;            //Divided by 2
 LPC_IOCON->PIO2_2 &= ~0x07;            //SSP I/O config
 LPC_IOCON->PIO2_2 |= 0x02;                //SSP MISO
 LPC_IOCON->PIO2_3 &= ~0x07;
 LPC_IOCON->PIO2_3 |= 0x02;                //SSP MOSI
 LPC_IOCON->PIO2_1 &= ~0x07;
 LPC_IOCON->PIO2_1 |= 0x02;                //SSP CLK
 LPC_IOCON->PIO2_0 &= ~0x07;
 LPC_IOCON->PIO2_0 |= 0x02;                //SSP SSEL
// Set DSS data to 8-bit, Frame format SPI, CPOL = 0, CPHA = 0, and SCR is 12
 LPC_SSP1->CR0 = 0x0B07;
// SSPCPSR clock prescale register, master mode, minimum divisor is 0x02
 LPC_SSP1->CPSR = 0x2;
 for ( i = 0; i < FIFOSIZE; i++ )
 {
  Dummy = LPC_SSP1->DR;                    //clear the RxFIFO
 }
//slave, SSP Enabled
 if ( LPC_SSP1->CR1 & SSPCR1_SSE )
 {
/* The slave bit can't be set until SSE bit is zero. */
  LPC_SSP1->CR1 &= ~SSPCR1_SSE;
 }
 LPC_SSP1->CR1 = SSPCR1_MS;                //Enable slave bit first
 LPC_SSP1->CR1 |= SSPCR1_SSE;            //Enable SSP
 return;
}

uint8_t SPI0_write(uint8_t byte)
{
 uint8_t readback;
#if !USE_CS0
 LPC_GPIO0->DATA &=~(1<<2);
#endif
 LPC_SSP0->DR = byte;
/* Wait until the Busy bit is cleared */
  while ((LPC_SSP0->SR & (SSPSR_BSY|SSPSR_RNE)) != SSPSR_RNE );
  readback = LPC_SSP0->DR;
#if !USE_CS0
 LPC_GPIO0->DATA |=(1<<2);
#endif
 return(readback);
}

int main(void)
{
 volatile static int i = 0 ;
 printf("Hello World\n");
 SPI0_init();                            //init #0: master
 SPI1_init();                            //init #1: slave
 while(1)
 {
  i++ ;
  if(i >10000)                            //delay
  {
   i = 0;
   sp0_write = 0xAA;                    //
   sp0_read = SPI0_write(sp0_write);    //write command 0xAA
   printf("Master: %02X->%02X\n",sp0_write,sp0_read);
  }                                        //end delay
  if((LPC_SSP1->SR & (SSPSR_BSY|SSPSR_RNE))==SSPSR_RNE)//received data
  {
   sp1_read = LPC_SSP1->DR;                //read
   sp1_write++;
   LPC_SSP1->DR = sp1_write;            //write
   printf("Slave : %02X->%02X\n",sp1_read,sp1_write);
  }
 }
 return 0 ;
}
Result:

Quote:

Hello World
Master: AA->00
Slave : AA->01
Master: AA->01
Slave : AA->02
Master: AA->02
Slave : AA->03
Master: AA->03
Slave : AA->04
Master: AA->04
Slave : AA->05
Master: AA->05
Slave : AA->06
Master: AA->06
Slave : AA->07
Master: AA->07
Slave : AA->08
Master: AA->08
Slave : AA->09
Master: AA->09
Slave : AA->0A
Master: AA->0A

Project: [ATTACH]552[/ATTACH]
0 件の賞賛
返信

3,414件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by hzrnbgy on Mon Sep 19 04:41:00 MST 2011
I never tested this on my LPC11U14 as im lazy to solder the header pins yet

If the LPC runs at 48MHz, this will yield an 8MHz SPI slave. Just changed the CPSR (divider) to obtain desired SPI clock

void ssp0_init(void);

// TODO: insert other definitions and declarations here

int main(void)
{
uint8_t data;
ssp0_init();

// Enter an infinite loop, just incrementing a counter
while(1)
{
if(LPC_SSP0->SR & 1<<2)
{
//data received, do something here
data=LPC_SSP0->DR;
}
}
return 0 ;
}

void ssp0_init()
{
LPC_IOCON->PIO0_2 = 1<<7;
LPC_IOCON->PIO0_2 |=(0x01<<0) | (0x02<<3);//ssel0, pull-up

LPC_IOCON->PIO0_6 |= (0x02<<0);//sck0
LPC_IOCON->PIO0_6 &= ~(0x03<<3);//no pull-up or down

LPC_IOCON->PIO0_8 |= (0x01<<0);//miso0
LPC_IOCON->PIO0_8 &= ~(0x03<<3);//no pull-up or down

LPC_IOCON->PIO0_9 |= (0x01<<0);//mosi0
LPC_IOCON->PIO0_9 &= ~(0x03<<3);//no pull-up or down

//enable clock on ssp0
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<11);
//ssp0 pre-scaler
LPC_SYSCON->SSP0CLKDIV = 1; //ssp0 peripheral clock
//de-assert reset on ssp0 block
LPC_SYSCON->PRESETCTRL |= (1<<0);

//ssp0 block registers
LPC_SSP0->CR0 |= (0x07<<0) | (0x00<<4) | (0x00<<6) | (0x00<<7) | (0x00<<8);//8bit SPI, CPOL0, CPHA0
LPC_SSP0->CR1 |= (1<<2);//slave mode
LPC_SSP0->CPSR = 6;//ssp0clk/6 for spi clock
LPC_SSP0->CR1 |= (1<<1);//enable ssp0

return;
}


can you let me know if it works so I can use it on mine eventually
0 件の賞賛
返信

3,414件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by dirtyBits on Mon Sep 19 03:32:14 MST 2011
[SIZE=3][B]NXP_Europe, NXP_USA
[/B]
Can anyone provide a [B]simple code[/B] for LPC1114 to receive 8 byte of Data in SLave SPI mode?
Or is it too much to ask? :mad:[/SIZE]
0 件の賞賛
返信

3,414件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by dirtyBits on Mon Sep 19 03:21:15 MST 2011
Still not working :mad:
0 件の賞賛
返信

3,414件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by dirtyBits on Sun Sep 18 01:49:15 MST 2011
After reading the SSP example code, it seems only few codes are needed to receive a byte in Slave mode as follows;
#include "driver_config.h"
#include "target_config.h"
#include "gpio.h"
#include "timer32.h"
#include "ssp.h"

#define SSP_NUM0

uint8_t src_addr[SSP_BUFSIZE]; 
uint8_t dest_addr[SSP_BUFSIZE];


/******************************************************************************
**   Main Function  main()
******************************************************************************/
int main (void)
{
  uint32_t i;
  SystemInit();
  SSP_IOConfig( SSP_NUM ); /* initialize SSP port, share pins with SPI1 on port2(p2.0-3). */
  SSP_Init( SSP_NUM );

  for ( i = 0; i < SSP_BUFSIZE; i++ )
  {
src_addr = (uint8_t)i;
dest_addr = 0;
  }

SSP_Receive( SSP_NUM, (uint8_t *)dest_addr, SSP_BUFSIZE );
for ( i = 0; i < SSP_BUFSIZE; i++ )
 {
   if ( src_addr != dest_addr )
{
  while ( 1 );/* Verification failure, fatal error */
} 
  }


Is there anything missing in the code?:confused:
0 件の賞賛
返信

3,414件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by dirtyBits on Sun Sep 18 01:43:52 MST 2011

Quote: hzrnbgy
//heres what I have for SSP1 slave on pins P0.6,P0.7, P0.8, P0.9 on a 96MHz 1768. You'll probably have to change the clock if you are using a LPC1769
.
.
.
makes me wonder, why are you using the Arduino as the master while the LCP1769 is clearly the more powerful card?



Thanks,
I'll try this on LPC1114!
I'm making peripheral product for Arduino that's why I'm using it as Master!
0 件の賞賛
返信

3,414件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by hzrnbgy on Sun Sep 18 01:20:37 MST 2011
//heres what I have for SSP1 slave on pins P0.6,P0.7, P0.8, P0.9 on a 96MHz 1768. You'll probably have to change the clock if you are using a LPC1769

void ssp1_init(void)
{
    LPC_SC->PCONP |= (1<<10);                               //power up SSP1
    LPC_SC->PCLKSEL0 |= (2<<20);                            //run SSP1 at 48MHz (prescale 2)
   
    //configure PIN functions
    LPC_PINCON->PINSEL0 |= (2<<18) | (2<<16) | (2<<14) | (2<<12);     //MOSI1, MISO1, SCK1, SSEL1
   
    LPC_PINCON->PINMODE0 |= (2<<18) | (2<<16) | (2<<14);    //no pull-up or down on MOSI1, MISO1, SCK1
    LPC_PINCON->PINMODE0 |= (3<<12); pull-up on SSEL1
   
    //frame format, 8bits SPI format, CPOL0, CPHA0
    LPC_SSP1->CR0 &= ~(0xFF<<8 | 1<<7 | 1<<6 | 3<<4);
    LPC_SSP1->CR0 |= (7<<0);
   
    //slave mode
    LPC_SSP1->CR1 &= ~(1<<3 | 1<<0);
    LPC_SSP1->CR1 |= (1<<2);
   
    //clock counter/prescaler setup
    LPC_SSP1->CPSR = 6;                                     //run SSP1 clock at 8MHz
   
    //enable SSP controller
    LPC_SSP1->CR1 |= (1<<1);
}

you might want to turn on received interrupt so you don't have to poll the status register to determine pending transfer

makes me wonder, why are you using the Arduino as the master while the LCP1769 is clearly the more powerful card?
0 件の賞賛
返信

3,414件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by dirtyBits on Sun Sep 18 00:42:40 MST 2011
:(
0 件の賞賛
返信

3,414件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by dirtyBits on Sat Sep 17 02:28:17 MST 2011
[SIZE=2]Just want to transmit 256 byte data from Arduino to LPC1114.
Is there any simpler way to do it then scratch my head trying to understand SSP Example code? It seems easier to write Bit-bang code for SPI Slave by myself than getting learning to use SSP example!
Aaarghhh :mad::mad::mad::mad:[/SIZE]

Zero...wake up man!
0 件の賞賛
返信

3,414件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by dirtyBits on Fri Sep 16 23:12:08 MST 2011
[SIZE=2]Aaaargh!
Why don't NXP people make it simple to use; by making a [B]single header file [/B]like Arduino have [B]SPI.h[/B] and still you can customize all the configuration options.
I just want to use LPC1114 in my project because of its cheaper cost and higher speed but it doesn't seem easy to learn and use!
And thanks a lott [B]Zero[/B] without your help I wouldn't even be at this stage!

[/SIZE]
0 件の賞賛
返信

3,414件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Fri Sep 16 13:57:20 MST 2011
Two options:

#1 Copy the complete project (in Eclipse), change settings in in config headers (\config directory) and driver headers (\driver directory).

#2 Copy all used config and source files (ssp.h, ssp.c, gpio.h, gpio.c...) to \src directory of your own project.
0 件の賞賛
返信

3,414件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by dirtyBits on Fri Sep 16 13:33:35 MST 2011
Thanks Zero,
I saw that example but don't know which files do I need to use from SSP example project to my project? [B]ssp_main.c[/B] ? or any of the [B]#include[/B] files?? or the whole project has be to be there in project explorer and has to be build together with my project/source file?

Plz shed some light on this...:)
0 件の賞賛
返信

3,414件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Fri Sep 16 13:23:13 MST 2011

Quote:

ssp
=====================
This project contains an ssp example for the LPCXpresso board. To use
it, compile it as master and slave (SSP_SLAVE=0) and program it into
two different boards. Information should be sent from the src_addr
buffer to the dest_addr buffer. Connect pins PIO0_2, PIO0_6, PIO0_8,
and PIO0_9 between both boards.The code can also be configured to
test loopback or work with an EEPROM.

With ssp sample in your example folder?
0 件の賞賛
返信