Send hex data from a arduino(slave) to rh850 controller via SPI

Hello,

Please, the project I am currently doing involves sending data to an RH850 controller. How it works is this, first, the RH850, which is the master sends a request and the slave, the Arduino, sends a response, which is a Hex code (0x400000) to the master (RH850). However, I am now confused as to what to do,

Please give me your advice.

Read how SPI communication and the associated library works maybe? :innocent:

https://www.arduino.cc/en/reference/SPI

There is no library at all for it

I meant the SPI library as per the link

In the examples provided, I saw no instance of that. With nick gammon's example, I have been able to successfully make 2 arduinos talk to each other, but, I am not able to transmit hex value 0x400000.

/*
  This project demonstrates SPI communication protocol by listening to status requests from a master controller, then, the slave device, an arduino,
  after getting the status message, sends a response message to the master, telling it it's status. We will use the SPI library.
*/

#include <SPI.h>

#define cycle_start 3

unsigned long startCycleTime;
unsigned long endCycleTime;
const long CycleTime = 5000;

long time = 0;
long debounce = 200;

volatile boolean received_Data;
volatile byte command = 0;

uint32_t display[4] = {};
int myMessages[4] = {0x400000, 0x200000, 0x002000, 0x100000};

uint16_t value = 400000;

ISR(SPI_STC_vect) {
  byte c = SPDR;
  command = c;

  if (command == 0) {
    command = c;
    SPDR = 0;
  } else {
    if (command == 1) {
      SPDR = value;
    }
  }

}

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

  pinMode(MISO, OUTPUT);
  pinMode(cycle_start, INPUT_PULLUP);

  SPCR |= _BV(SPE);
  SPCR |= _BV(SPIE);
}

void loop() {

    if (digitalRead(SS) == HIGH){
    command = 0;
    }

    Serial.println(value, DEC);
}

Currently, this is what I have written for the slave device.

That statement is confusing. Which device is the SPI BUS MASTER?

Why not? Just send the three bytes consequtively.

@gfvalvo, the RH850 is the master.

Please, I am not really conversant with bytes sending. What 3 bytes are you talking about

The value 0x400000 is made up of 3 bytes. Send them one at a time.

@gfvalvo, please help me out with this. I don't know how to.

Which arduino are you using? On a uno or mega for example and int is only 2 bytes. You won’t store those values.

To your question, it would be something like this

digitalWrite(SS, HIGH);
SPI.begin ();
…

digitalWrite(SS, LOW);
SPI.transfer (0x40);
SPI.transfer (0x00);
SPI.transfer (0x00);
digitalWrite(SS, HIGH);

….
SPI.end ();  // this turns SPI hardware off, so may be not what you want to do always

@J-M-L, I am using an uno, but, it is serving as a slave device. The examples that i have seen so far, I haven't seen SPI.transfer(val) for slave devices, hence the confusing. Is it possible to do that on a slave device?

I am aware that with SPDR, I can send data to the master from the slave, which is what I did as shown in the code above.

isn't this for the master device, if I am using an arduino?

Ah right, the SPI library only supports Master mode.

There are possibilities though, See SPI Slave Programming for Arduino - Arduino Stack Exchange for example

I have worked that out and configured it as a slave device, but, I need to send data in this format (0x400000) to the master. SPDR can only send 8 bits of data at the same time. That's what I really need help with.

The protocol only exchanges one byte at a time

You need to call SPI.transfer() multiple times (3 for 3 bytes at least) on the master and the slave must manage through a state machine what the next ask is for.

Edit: did you see this for example

How many bytes are there in the request message of RH850 Controller?
Provide an arbitrary example of that message.

I will find out and get back to you