1

I am making a function for one of my devices, my device needs a particular SPI setting. I was hoping to save the current setting of the SPI class into some variable, load in the required setting, do a transfer, and after that load back the old setting.

I know there is an object for the SPISettings, but i dont know how to pull the current settings without me having to create a global variable that keeps track of it.

void sendSPI(){
 SPISettings oldSettings = ??? ;   //save old SPI Settings
 SPISettings mySettings(14000000, MSBFIRST, SPI_MODE0);

 digitalWrite(CSPIN,LOW);
 SPI.beginTransaction(mySettings); //load in new SPI Settings
 SPI.transfer(0x0);
 digitalWrite(CSPIN,HIGH);

 SPI.beginTransaction(mySettings); //Revert to old SPI Settings
}
10
  • What about trying static?
    – st2000
    Apr 9 at 14:26
  • @st2000 the problem is i do not know the function for access the current settings
    – DrakeJest
    Apr 9 at 14:43
  • every library which uses SPI should start the transaction with the setting for the specific device. so you don't have to restore the settings. what are your other devices?
    – Juraj
    Apr 9 at 16:26
  • I'm going to assume you know what you want (but this may be a case where there are better ways to do what you want in C++ when considering the problem from a higher level perspective). In your class, write a method that will return ("expose") the desired variables. Then, each time you instantiate your class, call those functions to return the desired values.
    – st2000
    Apr 9 at 16:29
  • 1
    @st2000 "Once you have your SPI parameters, use SPI.beginTransaction() to begin using the SPI port." from docs.arduino.cc/learn/communication/…
    – Juraj
    Apr 9 at 16:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.