Go Down

Topic: SPI Slave [SOLVED] (Read 5 times) previous topic - next topic

zuzzuz

ok, this is my new code with preconfigured rx_data as 0x00, and your modification.

Code: [Select]
#include <SPI.h>

#define SCK_PIN   13
#define MISO_PIN  12
#define MOSI_PIN  11
#define SS_PIN    10

void SlaveInit(void) {
  // Set MISO output, all others input
  pinMode(SCK_PIN, INPUT);
  pinMode(MOSI_PIN, INPUT);
  pinMode(MISO_PIN, OUTPUT);
  pinMode(SS_PIN, INPUT);

  // Enable SPI
  SPCR = B00101100;
  SPCR = (1<<SPE);
}
uint8_t RX_Data[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                       0x00, 0x00, 0x00, 0x00};

uint8_t ReadByte(void) {
uint8_t *ptr = RX_Data;
for (int i = 0; i < 32; i++){
// SPDR = 0x01; // preload the data reg
  while(!(SPSR & (1<<SPIF))) ;
*ptr++ = SPDR; // save byte
}
}

void setup()
{
  Serial.begin(9600);
  SlaveInit();
}

void loop()
{

ReadByte();

for (int j = 0;j<32;j++){  // note loop values
  Serial.print(RX_Data[j], HEX);
  Serial.print(',');
  }
Serial.println(' ');
}




Code: [Select]
0,40,40,20,20,60,60,10,10,50,50,30,30,70,70,8,8,48,48,28,28,68,68,18,18,58,58,38,38,78,78,4,
28,28,68,68,18,18,58,58,38,38,78,78,4,0,40,40,20,20,60,60,10,10,50,50,30,30,70,70,8,8,48,48,

Graynomad

What have you got on the Arduino's SS pin? I see you are controlling it with the master, is it connected properly?

It seems that most bytes are being received twice, if we ignore that and look at the bits in the first few values

Code: [Select]
40 10000000
20 01000000
60 11000000

10 00010000
50 01010000
30 00110000
70 01110000

8 00000100
48 01001000
28 00101000
68 01101000
18 00011000
58 01011000
38 00111000
78 01111000


We can see there is a progression of sorts from the MSB position. In fact when I did the first 3 values I thought "Yes that's it, it's an MSB/LSB problem".

I still can't get the pattern but I think it's partly to do with transmitting MSB first and receiving LSB first , or vice versa.

Check out which is being used at both ends. I don't know what the Arduino SPI library defaults to but there is a SPI.setBitOrder() function to force it.

Also just thinking about the speed, at 2MBs you have 500nS to read and store a character, that's only 8 instructions on the Arduino, not long enough to do the job I think. Slow down the master (a lot) to prove this one way or another. This does have a quasi-random look to it that might be explained by the two ends being synced but the slave not being able to keep up.


_____
Rob
Rob Gray aka the GRAYnomad www.robgray.com

zuzzuz


What have you got on the Arduino's SS pin? I see you are controlling it with the master, is it connected properly?

It seems that most bytes are being received twice, if we ignore that and look at the bits in the first few values

Code: [Select]
40 10000000
20 01000000
60 11000000

10 00010000
50 01010000
30 00110000
70 01110000

8 00000100
48 01001000
28 00101000
68 01101000
18 00011000
58 01011000
38 00111000
78 01111000


We can see there is a progression of sorts from the MSB position. In fact when I did the first 3 values I thought "Yes that's it, it's an MSB/LSB problem".

I still can't get the pattern but I think it's partly to do with transmitting MSB first and receiving LSB first , or vice versa.

Check out which is being used at both ends. I don't know what the Arduino SPI library defaults to but there is a SPI.setBitOrder() function to force it.

Also just thinking about the speed, at 2MBs you have 500nS to read and store a character, that's only 8 instructions on the Arduino, not long enough to do the job I think. Slow down the master (a lot) to prove this one way or another. This does have a quasi-random look to it that might be explained by the two ends being synced but the slave not being able to keep up.


_____
Rob

Thx for great help.
Now i should go to work, this evening when i come back i will do some modify, but i don't understand why if i configured all in lsb i've this this problem (SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB; STM32  DORD = 1 DORD - Sends data least Significant Bit First when 1, most Significant Bit first when 0 ARDUINO
For clock i will downgrade clock speed of stm32 from 24mhz to 8 mhz then i will have a 8/12 Mhz on the SPI and i can easily see if there is a clock speed problem.

zuzzuz

#18
Aug 27, 2012, 06:03 pm Last Edit: Aug 27, 2012, 06:46 pm by zuzzuz Reason: 1
ok i slowed with prescale baud rate to: (24mhz/32) and configured master as MSB:
now i have this:
Code: [Select]
0,0,1,0,0,2,3,0,0,0,1,4,4,6,7,0,0,0,1,0,0,2,3,8,8,8,9,C,C,E,F,0,
8,8,9,C,C,E,F,0,0,0,1,0,0,2,3,0,0,0,1,4,4,6,7,0,0,0,1,0,0,2,3,8,
6,7,0,0,0,1,0,0,2,3,8,8,8,9,C,C,E,F,0,0,0,1,0,0,2,3,0,0,0,1,4,4,
1,4,4,6,7,0,0,0,1,0,0,2,3,8,8,8,9,C,C,E,F,0,0,0,1,0,0,2,3,0,0,0,
0,0,1,4,4,6,7,0,0,0,1,0,0,2,3,8,8,8,9,C,C,E,F,0,0,0,1,0,0,2,3,0,


P.S.
Multiple lines isn't a problem i use a delay to master to send only 32bit, i write here more than 1 line cause it can help to identify the problem.
P.P.S
I try to change DORD, for have LSB or MSB but nothing change.

zuzzuz

#19
Aug 27, 2012, 09:23 pm Last Edit: Aug 27, 2012, 09:32 pm by zuzzuz Reason: 1
i try to use new libraries of spi (1.01)

Code: [Select]
#include <SPI.h>

#define SCK   13
#define MISO  12
#define MOSI  11
#define SS    10


uint8_t RX_Data[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                      0x00, 0x00, 0x00, 0x00};

uint8_t ReadByte(void) {
uint8_t *ptr = RX_Data;
for (int i = 0; i < 32; i++){
// SPDR = 0x01; // preload the data reg
 while(!(SPSR & (1<<SPIF))) ;
*ptr++ = SPDR; // save byte
}
}

void setup()
{
 pinMode(SCK, INPUT);
 pinMode(MOSI, INPUT);
 pinMode(MISO, OUTPUT);
 pinMode(SS, INPUT);
 
 Serial.begin(9600);
 SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE3);
 //SPI.setClockDivider();
 SPI.begin();
 
}

void loop()
{

ReadByte();

for (int j = 0;j<32;j++){  // note loop values
 Serial.print(RX_Data[j], HEX);
 Serial.print(',');
 }
Serial.println(' ');
}

With this code SPI doesn't starts, i should to add "SPCR = (1<<SPE); but this shouldn't be necessary from library...
if i add this i have this output:
Code: [Select]
C,F,0,1,0,3,0,1,4,7,0,1,0,3,8,9,C,F,0,1,0,3,0,1,4,7,0,1,0,3,8,9,
C,F,0,1,0,3,0,1,4,7,0,1,0,3,8,9,C,F,0,1,0,3,0,1,4,7,0,1,0,3,8,9,


Graynomad

I've run out of ideas here I think.

I see you changed the clock phase and polarity as pylon suggested in reply #1? Maybe it's worth playing with them again.
______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

zuzzuz

SOLVED
Code: [Select]
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,


This is the final code
Code: [Select]
#include <SPI.h>

#define SCK_PIN   13
#define MISO_PIN  12
#define MOSI_PIN  11
#define SS_PIN    10

void SlaveInit(void) {

 pinMode(SCK_PIN, INPUT);
 pinMode(MOSI_PIN, INPUT);
 pinMode(MISO_PIN, OUTPUT);
 pinMode(SS_PIN, INPUT);


SPCR = (0<<SPIE) ;

SPCR=  (0<<DORD);
SPCR=(0<<MSTR);
SPCR=(0<<CPOL);
SPCR=(0<<CPHA);
SPCR=(0<<SPR1);
SPCR=(0<<SPR0);
SPCR = (1<<SPE);
 
}
uint8_t RX_Data[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                      0x00, 0x00, 0x00, 0x00};

uint8_t ReadByte(void) {

for (int i = 0; i < 32; i++){
 RX_Data[i] = SPI.transfer(0x00);
}
}

void setup()
{
 Serial.begin(9600);
 SlaveInit();
}

void loop()
{

ReadByte();

for (int j = 0;j<32;j++){  // note loop values
 Serial.print(RX_Data[j], HEX);
 Serial.print(',');
 }
Serial.println(' ');
}


Thx a lot for your help, without that i will leave this project some time ago.
I rewrote routine and all setting from begin, and i use a part of new libreries.

Graynomad

Good stuff. Often the way to fix something that just refuses to work is do a rewrite.

I still don't see why the direct approach to SPDR didn't work, I have done almost exactly the same before with no problems. Here is the code from SPI.transfer

Code: [Select]
SPDR = _data;
  while (!(SPSR & _BV(SPIF)))
    ;
  return SPDR;


Apart from writing to SPDR because it is designed to send bytes as well it's really the same as your code. Go figure.

Now that I look at this I wonder if it works at high speed, just for an exercise can you crank the master back up to 2MHz and see what happens?

After I posted last night I wondered why you weren't using the SPI library and thought I'd suggest that in the morning. No need to now :)

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

zuzzuz




Now that I look at this I wonder if it works at high speed, just for an exercise can you crank the master back up to 2MHz and see what happens?


______
Rob

Now i will go to work when i will come back i will try.
Bye

zuzzuz

ok i do this test 24mhz/8 as u can see there is some error sometimes
Code: [Select]
1,2,3,4,5,2,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,3,8,9,A,B,C,D,E,F,10,11,12,1,14,11,16,13,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,2,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,12,17,18,19,1A,1B,1C,1D,1E,F,20,
1,2,3,0,5,6,3,8,9,A,B,C,D,E,F,0,11,12,13,10,1,16,13,8,19,1A,1B,1C,D,1E,1F,0,
1,2,3,0,5,2,7,8,9,8,B,C,D,E,F,10,11,12,13,10,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,0,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,10,5,16,17,18,19,1A,1B,1C,1D,1E,1F,0,
1,2,3,0,5,6,3,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,13,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,0,5,6,7,8,9,A,B,C,D,E,F,10,11,2,13,10,15,16,17,18,19,8,1B,1C,1D,1E,1F,20,
1,2,3,0,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,4,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,2,7,8,9,A,B,C,D,E,F,10,11,12,13,14,5,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,0,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,2,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,0,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,A,1B,1C,1D,1E,1F,20,
1,2,3,0,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,0,
1,2,3,0,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,10,15,16,17,18,19,1A,1B,1C,1D,1E,1F,0,
1,2,3,0,5,6,7,8,9,A,B,C,D,E,F,0,11,12,1,0,15,16,17,18,9,1A,1B,1C,1D,1E,1F,0,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,0,11,12,13,14,15,2,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,0,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,1,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,12,17,18,19,18,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,2,7,8,9,A,B,C,D,E,F,10,11,12,13,4,5,12,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,0,5,6,3,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,0,
1,2,3,0,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,0,
1,2,3,4,5,2,7,8,9,A,B,C,D,E,F,10,11,12,13,10,15,16,13,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,12,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,2,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,2,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,11,16,17,18,19,1A,1B,1C,1D,1E,1F,0,
1,0,3,4,5,2,7,8,9,A,B,C,D,E,F,0,11,12,13,0,15,12,7,18,19,A,1B,C,1D,1E,1F,0,


At 24Mhz / 16 no Error found
Code: [Select]
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,

Bye

zuzzuz

Now i will try to do arduino as master and stm32 as slave... i hope to have less problem :p

zuzzuz

i tryed to traspose my master code to arduino i have just a slave code to stm32 that work good (tested ok)
Code: [Select]
#include <SPI.h>

const int buttonPin = 2;     // the number of the pushbutton pin
int buttonState = 0;         // variable for reading the pushbutton status


void setup() {
     
pinMode(buttonPin, INPUT);    
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);


}

void loop(){

  buttonState = digitalRead(buttonPin);
   if (buttonState == HIGH) {    

digitalWrite(SS, LOW);
int a = SPI.transfer(0x12);
digitalWrite(SS, HIGH);

          }
 


   }


In slave nothing data is arrived.
Some Idea?

zuzzuz

i try to indentify the problem with this debug..
Code: [Select]
#include <SPI.h>

#define SCK   13
#define MISO  12
#define MOSI  11
#define SS    10

const int buttonPin = 2;     // the number of the pushbutton pin
int buttonState = 0;         // variable for reading the pushbutton status

uint8_t RX_Data[32] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
                       0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
                       0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
                       0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C,
                       0x1D, 0x1E, 0x1F, 0x20};

void setup() {
 
  pinMode(SCK, OUTPUT);
  pinMode(MOSI, OUTPUT);
  pinMode(MOSI, INPUT);
  pinMode(SS, OUTPUT);       
 
  pinMode(buttonPin, INPUT);     
   
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);

Serial.begin(9600);
}

void loop(){

  SPCR = (0<<SPIE) ;

SPCR=  (0<<DORD);
SPCR=(1<<MSTR);
SPCR=(0<<CPOL);
SPCR=(0<<CPHA);
SPCR=(0<<SPR1);
SPCR=(0<<SPR0);
SPCR = (1<<SPE);

   buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH) {     

digitalWrite(SS, LOW);

for (int i = 0; i < 32; i++){
   
   Serial.print(RX_Data[i], HEX);
Serial.print(',');

SPI.transfer(RX_Data[i]);

}

digitalWrite(SS, HIGH);

           }
 


    }


with this code i will know what i'm sending time by time, indeed i have a identify that code stops at spi.transfer routine

COM3 (serial) code:
Code: [Select]
1,

a little help?

Graynomad

These lines

SPCR=  (0<<DORD);
SPCR=(1<<MSTR);
SPCR = (1<<SPE);
SPCR=(0<<CPHA);
SPCR=(0<<SPR1);

Are already done by the SPI.begin(), SPI.setBitOrder() and SPI.setDataMode() functions so you should remove them.

But this line

  SPCR = (0<<SPIE) ;

Is a real problem. You are enabling interrupts and have no ISR to handle them so I'd say the code goes to la-la land after you transmit the first byte. Remember that with SPI you transmit and receive at the same time so somewhere in the SPI.transfer() function you get an interrupt from the SPI port and you don't handle it.

Remove that line and see what happens.

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

zuzzuz

#29
Sep 02, 2012, 02:12 pm Last Edit: Sep 02, 2012, 02:41 pm by zuzzuz Reason: 1
thx now it's run partially. From master arduino i see this
Code: [Select]
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F,20,
But from slave i recive only odd from 3 to 65
Code
Code: [Select]
#include <SPI.h>

#define SCK   13
#define MISO  12
#define MOSI  11
#define SS    10

const int buttonPin = 2;     // the number of the pushbutton pin
int buttonState = 0;         // variable for reading the pushbutton status

uint8_t RX_Data[32] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
                       0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
                       0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
                       0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C,
                       0x1D, 0x1E, 0x1F, 0x20};

void setup() {
 
  pinMode(SCK, OUTPUT);
  pinMode(MOSI, OUTPUT);
  pinMode(MOSI, INPUT);
  pinMode(SS, OUTPUT);       
 
  pinMode(buttonPin, INPUT);     
   
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);

Serial.begin(9600);
}

void loop(){

   buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH) {     

digitalWrite(SS, LOW);

for (int i = 0; i < 32; i++){
   
   Serial.print(RX_Data[i], HEX);
Serial.print(',');

SPI.transfer (RX_Data[i]);
delay (100);
}

digitalWrite(SS, HIGH);

           }
 


    }

Go Up