Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update 001SPISlaveRxString.ino #8

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

Resources/Source_code/Workspace/stm32f4xx_drivers/Debug/
81 changes: 34 additions & 47 deletions Resources/Arduino/spi/001SPISlaveRxString/001SPISlaveRxString.ino
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,18 @@
#define SPI_MOSI 11
#define SPI_SS 10

char dataBuff[500];
#define MAX_I2C_PAYLOAD_LENGTH 500

char dataBuff[MAX_I2C_PAYLOAD_LENGTH];
//Initialize SPI slave.
void SPI_SlaveInit(void)
{
#if 0
// Initialize SPI pins.
pinMode(SPI_SCK, INPUT);
pinMode(SPI_MOSI, INPUT);
pinMode(SPI_MISO, OUTPUT);
pinMode(SPI_SS, INPUT);

// Enable SPI as slave.
SPCR = (1 << SPE);
#endif
// Initialize SPI pins.
pinMode(SCK, INPUT);
pinMode(MOSI, INPUT);
pinMode(MISO, OUTPUT);
pinMode(SS, INPUT);
//make SPI as slave


// Enable SPI as slave.
SPCR = (1 << SPE);
}
Expand All @@ -45,71 +36,67 @@ void SPI_SlaveInit(void)
uint8_t SPI_SlaveReceive(void)
{
/* Wait for reception complete */
//Serial.println("in SPI_SlaveReceive - waiting for reception complete");
while(!(SPSR & (1<<SPIF)));
//Serial.println("reception complete !!!");
/* Return Data Register */
return SPDR;
}


//sends one byte of data
//send one byte of data
void SPI_SlaveTransmit(char data)
{
/* Start transmission */
SPDR = data;

/* Wait for transmission complete */
while(!(SPSR & (1<<SPIF)));
}


// The setup() function runs right after reset.
void setup()
{
// Initialize serial communication
Serial.begin(9600);

// Initialize SPI Slave.
SPI_SlaveInit();

Serial.println("Slave Initialized");
}
uint16_t dataLen = 0;
uint32_t i = 0;


// The loop function runs continuously after setup().
void loop()
{
uint32_t i;
uint8_t dataLen8;
//uint16_t dataLen16; // To support more than 255 bytes payload transfer in a single chunk. currently not in use.
Serial.println("\nSlave waiting for SS to go low");



Serial.println("Slave waiting for ss to go low");
while(digitalRead(SS) );
while(digitalRead(SS));
// Important !!! Avoid prints here! Enabling the print in the next line delays the slave Arduino and this cause it to miss data bytes sent from the master unless the master will add some delay between sending the length and the payload.
//Serial.println("SS is now Low");

// Serial.println("start");

//1. read the length
// dataLen = (uint16_t)( SPI_SlaveReceive() | (SPI_SlaveReceive() << 8) );
//dataLen16 = (uint16_t)( SPI_SlaveReceive() | (SPI_SlaveReceive() << 8) );
//Serial.println(String(dataLen,HEX));
i = 0;
dataLen = SPI_SlaveReceive();
for(i = 0 ; i < dataLen ; i++ )
{
dataBuff[i] = SPI_SlaveReceive();
}

dataLen8 = SPI_SlaveReceive();
//Serial.print("Received Data Len: ");
//Serial.println(String(dataLen8, DEC));
//Serial.println("Following will be the Received Data: ");

// Serial.println(String(i,HEX));
for(i = 0 ; i < dataLen8 ; i++ ) {
dataBuff[i] = SPI_SlaveReceive();
}
dataBuff[i] = '\0';

Serial.println("Rcvd:");
Serial.println(dataBuff);
Serial.print("Length:");
Serial.println(dataLen);




}

Serial.print("Rcvd: '");
Serial.print(dataBuff);
Serial.print("'. Length: ");
Serial.println(dataLen8);



// Serial.println("Slave waiting for ss to go high");
// while(!digitalRead(SS));
// Serial.println("SS HIGH\n\n");
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
/*

* SPI pin numbers:
* SCK 13 // Serial Clock.
* MISO 12 // Master In Slave Out.
* MOSI 11 // Master Out Slave In.
* SS 10 // Slave Select
*

*/

#include <SPI.h>
Expand All @@ -17,18 +14,17 @@ boolean ledState = HIGH; // LED state flag.

uint8_t dataBuff[255];

uint8_t board_id[10] = "ARDUINOUNO";

#define NACK 0xA5
#define ACK 0xF5
char board_id[10] = "ARDUINOUNO";

#define NACK 0xA5
#define ACK 0xF5

//command codes
#define COMMAND_LED_CTRL 0x50
#define COMMAND_SENSOR_READ 0x51
#define COMMAND_LED_READ 0x52
#define COMMAND_PRINT 0x53
#define COMMAND_ID_READ 0x54
#define COMMAND_PRINT 0x53
#define COMMAND_ID_READ 0x54

#define LED_ON 1
#define LED_OFF 0
Expand All @@ -44,12 +40,12 @@ uint8_t board_id[10] = "ARDUINOUNO";
void SPI_SlaveInit(void)
{
// Initialize SPI pins.
pinMode(SCK, INPUT);
pinMode(SCK, INPUT);
pinMode(MOSI, INPUT);
pinMode(MISO, OUTPUT);
pinMode(SS, INPUT);
pinMode(SS, INPUT);

//make SPI as slave

// Enable SPI as slave.
SPCR = (1 << SPE);
}
Expand All @@ -72,6 +68,8 @@ void SPI_SlaveTransmit(uint8_t data)

/* Wait for transmission complete */
while(!(SPSR & (1<<SPIF)));
// Serial.print("SPISlave sent byte: 0x");
// Serial.println(data, HEX);
}


Expand All @@ -80,102 +78,113 @@ void setup()
{
// Initialize serial for troubleshooting.
Serial.begin(9600);

// Initialize slave LED pin.
pinMode(led, OUTPUT);

digitalWrite(led,LOW);

// Initialize SPI Slave.
SPI_SlaveInit();

Serial.println("Slave Initialized");
}


byte checkData(byte commnad)
byte checkCmdOpcode(byte cmdOpcode)
{
//todo
return ACK;
Serial.print("command received: 0x");
Serial.println(cmdOpcode, HEX);
if ((cmdOpcode == COMMAND_LED_CTRL) ||
(cmdOpcode == COMMAND_SENSOR_READ) ||
(cmdOpcode == COMMAND_LED_READ) ||
(cmdOpcode == COMMAND_PRINT) ||
(cmdOpcode == COMMAND_ID_READ)) {
return ACK;
}
// Illegal opcode received.
return NACK;
}

// The loop function runs continuously after setup().
void loop()
{
byte data,command,len,ackornack=NACK;
byte data, cmdOpcode, len, ackOrNack = NACK;

//1. fist make sure that ss is low . so lets wait until ss is low
Serial.println("Slave waiting for ss to go low");
//1. wait until ss is low
Serial.println("Slave waiting for SS to go low");
while(digitalRead(SS) );
// Avoid prints here !!! It slows the slave and as a result received bytes will not be read.
//Serial.println("Slave's SS is now low (Slave was selected for communication)");

//2. now lets wait until rx buffer has a byte
command = SPI_SlaveReceive();
ackornack = checkData(command);

SPI_SlaveTransmit(ackornack);

//2. wait until rx buffer has a byte
cmdOpcode = SPI_SlaveReceive();
ackOrNack = checkCmdOpcode(cmdOpcode);
SPI_SlaveTransmit(ackOrNack);
len = SPI_SlaveReceive(); //dummy byte

if(command == COMMAND_LED_CTRL)
{
if (cmdOpcode == COMMAND_LED_CTRL) {
//read 2 more bytes pin number and value
uint8_t pin = SPI_SlaveReceive();
uint8_t value = SPI_SlaveReceive();
Serial.println("RCVD:COMMAND_LED_CTRL");
if(value == (uint8_t)LED_ON)
{
Serial.print("RCVD:COMMAND_LED_CTRL Set pin ");
Serial.print(pin);
if (value == (uint8_t)LED_ON) {
Serial.println(" ON (1).");
digitalWrite(pin,HIGH);
}else if (value == (uint8_t) LED_OFF)
{
} else if (value == (uint8_t)LED_OFF) {
Serial.println(" OFF (0).");
digitalWrite(pin,LOW);
} else {
Serial.print(" -- ERROR: value unexpected: 0x");
Serial.println(value, HEX);
}

}else if ( command == COMMAND_SENSOR_READ)
{

} else if (cmdOpcode == COMMAND_SENSOR_READ) {
//read analog pin number
uint16_t aread;
uint8_t pin = SPI_SlaveReceive();
//pinMode(pin+14, INPUT_PULLUP);
uint8_t val;
uint8_t dummyByte;
aread = analogRead(pin+14);
val = map(aread, 0, 1023, 0, 255);

SPI_SlaveTransmit(val);

val = SPI_SlaveReceive(); //dummy read

Serial.println("RCVD:COMMAND_SENSOR_READ");

dummyByte = SPI_SlaveReceive(); //dummy read
Serial.print("RCVD:COMMAND_SENSOR_READ from pin");
Serial.print(pin);
Serial.print(" Analog port value: ");
Serial.print(aread);
Serial.print(", ");
Serial.println(val);


}else if ( command == COMMAND_LED_READ)
{
} else if (cmdOpcode == COMMAND_LED_READ) {
uint8_t pin = SPI_SlaveReceive();
uint8_t val = digitalRead(pin);
SPI_SlaveTransmit(val);
Serial.print("RCVD:COMMAND_LED_READ. LED pin val: "); // prints are defered in order not to hold the SPI communication
Serial.println(val);
val = SPI_SlaveReceive(); //dummy read
Serial.println("RCVD:COMMAND_LED_READ");

}else if ( command == COMMAND_PRINT)
{

} else if (cmdOpcode == COMMAND_PRINT) {
uint8_t len = SPI_SlaveReceive();
for(int i=0 ; i < len ; i++)
for (int i = 0; i < len; i++)
{
dataBuff[i] = SPI_SlaveReceive();
}
Serial.println((char*)dataBuff);

Serial.println("RCVD:COMMAND_PRINT");

}else if ( command == COMMAND_ID_READ)
{
for(int i=0 ; i < strlen(board_id) ; i++)
dataBuff[len] = 0; // null termination at the end of the received string.
Serial.println("RCVD:COMMAND_PRINT"); // This print is defered to the end of the handle in order not to miss any SPI RXed byte
Serial.print("printed string len: ");
Serial.println(len);
Serial.println((char*)&dataBuff);

} else if (cmdOpcode == COMMAND_ID_READ) {
// send the length of the responding ID string.
SPI_SlaveTransmit(strlen(board_id));
// send the ID string.
for(int i = 0; i < strlen(board_id); i++)
{
SPI_SlaveTransmit(board_id[i]);
SPI_SlaveReceive(); //dummy read
}
SPI_SlaveReceive();
Serial.println("RCVD:COMMAND_ID_READ");
Serial.print("RCVD:COMMAND_ID_READ. Board string sent: ");
Serial.println(board_id);
}


Serial.println("");
}