Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reading SPI as slave
02-12-2015, 04:50 PM,
#1
Reading SPI as slave
Hi all,
I would like to program my Pinguino 18F26j50 as slave SPI receiver to turn on and shut down two electric motor but I don't know how...
I have seen some examples made by Arduino, but they use interrupts. Do I need to use interrups?
I need to read some little string from Raspberry Pi, so I connected Rasp GPIO SCK to Pinguino Pin 25, MISO -> Pinguino Pin 26 and MOSI -> Pinguino Pin 18.
I need some hints to takle this problem in the right way.

Thanks in advance!
Reply
03-12-2015, 12:00 PM, (This post was last modified: 03-12-2015, 12:01 PM by gtcbreizh.)
#2
RE: Reading SPI as slave
Hi dkmorb,
I worked with Régis's help on June 2013 to test SPI communication between 2 18f2550 then 2 18f26j50.
I used interrupts.
With the 18f2550 it worked but with the 18f26j50 I met troubles. Then I gave up in favor of other tests.
I am going to test again.

I attach the 2 pde (spim3.pde for the master, spis3.pde for the slave). It can be a starting point. I used the second spi module, see spi.h to know the associated pins.

Lastly Régis modified the spi library to be able to use 2 spi modules. Do you use a recent Pinguino version ?

See you soon
André


Attached Files
.pde   spim3 .pde (Size: 829 bytes / Downloads: 4)
.pde   spis3.pde (Size: 706 bytes / Downloads: 5)
Reply
03-12-2015, 12:25 PM,
#3
RE: Reading SPI as slave
I verified quickly the connexions :

Rasp                         26j50
MOSI     ->            pin 26 SDI1 (it's an Input)
SCLK      ->            pin 25 SCK1
MISO     ->            pin 18 SDO1 (it's an Output)

N.B. we have to cross input and output

Notice I used the old spi.c/h in my examples. I try to update them rapidly to avoid errors.

André
Reply
03-12-2015, 03:14 PM, (This post was last modified: 03-12-2015, 03:21 PM by dkmorb.)
#4
RE: Reading SPI as slave
(03-12-2015, 12:25 PM)gtcbreizh Wrote: I verified quickly the connexions :

Rasp                         26j50
MOSI     ->            pin 26 SDI1 (it's an Input)
SCLK      ->            pin 25 SCK1
MISO     ->            pin 18 SDO1 (it's an Output)

N.B. we have to cross input and output

Notice I used the old spi.c/h in my examples. I try to update them rapidly to avoid errors.

André

Oh, many many many thanks!!! I'll try this evening...yep, i use last Pinguino IDE release ...
Reply
04-12-2015, 03:59 PM,
#5
RE: Reading SPI as slave
No way! This is my example... How can I be sure about slave mode?
SPI.begin(5,1,0) is no more available...

Code:
/*---------------------------------------------------
SDO on RB1
SDI on RB3
SCK on RB2
SS on RB0 but not requested by the slave
-----------------------------------------------------*/
#define TR7 TRISBbits.TRISB5
#define LA7 LATBbits.LATB5
#define TR6 TRISBbits.TRISB4
#define LA6 LATBbits.LATB4

void setup (void)
{
TR7=0;
TR6=0;
LA7=0;
LA6=0;
// turn on SPI in slave mode
//SPI.begin(5,1,0);
SPI2.begin();
}

// main loop - wait for flag set in interrupt routine
void loop (void)
{
u8 c;
c = SPI2.read();
if(c != 0)
toggle(USERLED);
} // end of loop
Reply
04-12-2015, 06:39 PM,
#6
RE: Reading SPI as slave
Hello dkmorb,
When you use only SPI2.begin() the default values are not correct.
Put before SPI2.begin();
SPI2_setDataMode(SPI_MODE1);
SPI2_setMode(SPI_SLAVE);
SPI[SPI2].phase =0;
SPI2_setClockDivider(SPI_CLOCK_DIV64); 
As soon as I am available I test it too.

When I tested 2 years ago I put 2 leds on pins 6 and 7 to debug functions in spi.c (see #define at the beginning of pde).
Bye
André 
Reply
09-12-2015, 03:18 PM,
#7
RE: Reading SPI as slave
Hi gtcbreizh,
I overcame a compiler problem adding #include <typedef.h> in spi.h,
but nothing happened with your code. I've a oscilloscope, how can I test pinguino pins?
Reply
09-12-2015, 11:34 PM,
#8
RE: Reading SPI as slave
Hi dkmorb,
After I unsuccessfully tried a spi communication between 2 18f26j50 I succeeded  with 2 18f4550 this evening. I find back the same situation as sometime ago during my first tests. I explained this at the beginning of this thread some days ago.
Why is it OK only with the 18f4550 ?
More on your side your test is between a RaspBerry and a  18f26j50. Are the device speeds compatible ? Are the 2 Spi timing procedures identical ?
Bye
André 
Reply
10-12-2015, 12:04 PM,
#9
Sad  RE: Reading SPI as slave
Hi gtcbreizh,
I'm not very experienced with SPI protocol, I just know some arduino code is working with Rasp. Or probably i made wrong connection... 
The code is:
Code:
#include <SPI.h>

char buf [100];
volatile byte pos;
volatile boolean process_it;

void setup (void)
{
 Serial.begin (115200);   // debugging

 // have to send on master in, *slave out*
 pinMode(MISO, OUTPUT);
 
 // turn on SPI in slave mode
 SPCR |= _BV(SPE);
 
 // get ready for an interrupt
 pos = 0;   // buffer empty
 process_it = false;

 // now turn on interrupts
 SPI.attachInterrupt();

}  // end of setup


// SPI interrupt routine
ISR (SPI_STC_vect)
{
byte c = SPDR;  // grab byte from SPI Data Register
 
 // add to buffer if room
 if (pos < sizeof buf)
   {
   buf [pos++] = c;
   
   // example: newline means time to process buffer
   if (c == '\n')
     process_it = true;
     
   }  // end of room available
}  // end of interrupt routine SPI_STC_vect

// main loop - wait for flag set in interrupt routine
void loop (void)
{
 if (process_it)
   {
   buf [pos] = 0;  
   Serial.println (buf);
   pos = 0;
   process_it = false;
   }  // end of flag set
   
}  // end of loop
Reply
10-12-2015, 12:29 PM,
#10
RE: Reading SPI as slave
Did you think at a common ground between the 2 boards ? [Image: smile.gif]
Anyway I have to clear up the problem with the 18f26j50. So I too could try the connection between Rasp and 18f26j50.
But this requests time !
Bye
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)