Dear Community,
I'd like to use the SPI interface of the ArduinoDUE to transfer a 100byte string using the extended SPI method
The problem is I don't know hot to convert my string in SPI data. Can I have an hint on how proceed with this task?
Best,
CorteX
Transfer all chars or bytes in your string using SPI_CONTINUE, except for the last one. Eventually you have to add a (byte) or (uint8_t) type cast to the char, if transfer() insists in only transfering bytes.
If you have C strings of variable size, you can transfer all chars in a loop, until you hit the terminating zero. Then break out of the loop, and send the final zero.
Dear DrDiettrich,
many thanks, can you write, please, that code because I'm new to arduino programming.
Basically I have to.
- convert the sting to char (or byte array)
- go through the array with a for loop (or while if I insert a termination character)
- Transfer with SPI_CONTINUE
am I right?
CorteX
A string conversion is not normally required, a type cast should be sufficient. More could be said if you present your code.
You could code something like that, from reply #35, to transfer 100 bytes in 19 us:
TurboSpi
Dear DrDiettrich and ard_newbie,
here is the part of the code we are discussing: basically I have a DWM1000 receiver that put data inside "message" and send it over serial. I want to send that data over SPI to an FPGA.
#include <SPI.h>
#include <DW1000.h>
// connection pins
const uint8_t PIN_RST = 9; // reset pin
const uint8_t PIN_IRQ = 2; // irq pin
const uint8_t PIN_SS = SS; // spi select pin
// DEBUG packet sent status and count
volatile boolean received = false;
volatile boolean error = false;
volatile int16_t numReceived = 0; // todo check int type
String message;
void setup() {
// DEBUG monitoring
Serial.begin(250000);
Serial.println(F("### DW1000-arduino-receiver-test ###"));
// initialize the driver
DW1000.begin(PIN_IRQ, PIN_RST);
DW1000.select(PIN_SS);
Serial.println(F("DW1000 initialized ..."));
// general configuration
DW1000.newConfiguration();
DW1000.setDefaults();
DW1000.setDeviceAddress(6);
DW1000.setNetworkId(10);
DW1000.enableMode(DW1000.MODE_SHORTDATA_FAST_ACCURACY);
DW1000.commitConfiguration();
Serial.println(F("Committed configuration ..."));
// DEBUG chip info and registers pretty printed
char msg[128];
DW1000.getPrintableDeviceIdentifier(msg);
Serial.print("Device ID: "); Serial.println(msg);
DW1000.getPrintableExtendedUniqueIdentifier(msg);
Serial.print("Unique ID: "); Serial.println(msg);
DW1000.getPrintableNetworkIdAndShortAddress(msg);
Serial.print("Network ID & Device Address: "); Serial.println(msg);
DW1000.getPrintableDeviceMode(msg);
Serial.print("Device mode: "); Serial.println(msg);
// attach callback for (successfully) received messages
DW1000.attachReceivedHandler(handleReceived);
DW1000.attachReceiveFailedHandler(handleError);
DW1000.attachErrorHandler(handleError);
// start reception
receiver();
}
void handleReceived() {
// status change on reception success
received = true;
}
void handleError() {
error = true;
}
void receiver() {
DW1000.newReceive();
DW1000.setDefaults();
// so we don't need to restart the receiver manually
DW1000.receivePermanently(true);
DW1000.startReceive();
}
void loop() {
// enter on confirmation of ISR status change (successfully received)
if (received) {
// get data as string
DW1000.getData(message);
Serial.println(message);
// !!!!!!!!!!!!HERE COMES SPI TRANFERRING 2 FPGA !!!!!!!!!!!!!!!!!!!1
received = false;
}
if (error) {
Serial.println("Error receiving a message");
error = false;
DW1000.getData(message);
Serial.print("Error data is ... "); Serial.println(message);
}
}
Basically the Turbo SPI is something I like VERY much but I have the DWM1000 still on SPI and I have to check if that device still works...
How can I cast efficently from string to uint8_t?
CorteX
Try to send message.length() chars from message.c_str(). See String reference for more information.