Hi, i need to use my arduino as slave spi, master is a st discovery board.
I use this sketch to configure spi:
#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 = B00000000;
SPCR = (1<<SPE);
}
uint8_t ReadByte(void) {
while(!(SPSR & (1<<SPIF))) ;
return SPDR;
}
void WriteByte(uint8_t value) {
SPDR = value;
while (!(SPSR & (1<<SPIF))) ;
return;
}
void setup()
{
Serial.begin(9600);
SlaveInit();
}
void loop()
{
uint8_t RX_Data;
RX_Data = ReadByte();
WriteByte(0x01);
Serial.println(RX_Data);
// Serial.println(RX_Data, HEX);
Serial.println("rx");
}
This is the configuration of master
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPIy, &SPI_InitStructure);
I send this
uint8_t SPIy_Buffer_Tx[BufferSize] = {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};
but i recive this
FFFFFFC0
rx
40
rx
FFFFFF88
rx
18
rx
FFFFFFF0
rx
FFFFFFF0
rx
30
rx
10
rx
20
rx
4
rx
FFFFFF88
rx
50
rx
60
rx
40
rx
78
rx
58
rx
68
rx
48
rx
70
rx
50
rx
60
rx
40
rx
FFFFFFF8
rx
4
rx
someone can help me to understand where i do something wrong?
thx.