SPI issues, not sending data

Hi all, I am having some trouble using SPI, I am using a raspberry pi pico but I am using the arduino IDE to do all the software. Pretty much I am making a function generator using AD9834, below is my code, I dont think I am getting any SPI communication, as I can't see anything on my scope, ik a logic analyser would be better but I don't have one.

I havent used SPI a lot, so is there anything obvious that I have done wrong in the code, I followed a youtube video as a guide. Only thing im not sure is that in the video he declares the TX pin (SDATA for AD9834) but then never uses it, isn't the TX pin the one that sends the data?

code shown here:

#include <SPI.h>

const int On_Board_LED = 25;
const int RST = 0;
const int FSYNC1 = 5;


void setup() {
  // put your setup code here, to run once:
  pinMode(On_Board_LED, OUTPUT);
  pinMode(RST, OUTPUT);
  pinMode(FSYNC1, OUTPUT);


  digitalWrite(On_Board_LED, LOW);
  digitalWrite(RST, HIGH);
  digitalWrite(FSYNC1, HIGH);
  
  SPI.begin();
  SPI.beginTransaction(SPISettings(5000000, MSBFIRST, SPI_MODE0));
 

  
  AD_RESET();
  AD_FREQ();
  AD_ENABLE();
  
  delay(500);
  digitalWrite(RST, LOW);
}

void AD_RESET(){
  digitalWrite(FSYNC1, LOW);

 
  
  SPI.transfer(0x21);
  SPI.transfer(0x00);
  digitalWrite(FSYNC1, HIGH);

  
}

void AD_FREQ(){
  digitalWrite(FSYNC1, LOW);
  
  //These 4 SPI trasfer lines are for setting the frequency
  //Refer to the Data sheet for how this is done
  SPI.transfer(0x7F);
  SPI.transfer(0x7C);
  SPI.transfer(0x7B);
  SPI.transfer(0x60);
  
  //This is setting the phase of the signal to 0 phase adjustment
  SPI.transfer(0x00);
  SPI.transfer(0x00);
  digitalWrite(FSYNC1, HIGH);
  
  
}


void AD_ENABLE(){
  
  digitalWrite(FSYNC1, LOW);
  SPI.transfer(0x22);
  SPI.transfer(0x00);
  digitalWrite(FSYNC1, HIGH);

}


void loop() {
  // put your main code here, to run repeatedly:
  
  digitalWrite(On_Board_LED, HIGH);
  
}

Anyway any help with this would be greatly appreciated.

Thanks in advance Dean.

No experience of a Pi Pico, but I'd start by trying to detect your pin 5 going low as that seems to be the Slave Select pin you are using. Your scope should be able to trigger on this.

If you've got a 2nd channel available, then I'd suggest trying to detect the SPI CLK signal as well.

Does the Pi Pico have the ability to map it's pins to whatever peripherals it chooses? You may need to define the pins for CLK and MOSI.

....

Just found this page:
https://arduino-pico.readthedocs.io/en/latest/pins.html
Seems there are function calls to set the SPI pins:

::setSCK(pin)
::setCS(pin)
::setRX(pin)
::setTX(pin)

Hi @markd833, I have been able to detect my pin 5 CS pin going low as you mentioned.

what do you mean by have the ability to map its pin to whatever peripherals.

Also why are function called to the SPI pins useful, does this mean setting a different pin to be 1 of the 4 SPI pins? I am just not sure I understand.

Any clarifications on these points would be great, thank you so much.

These new microcontrollers that we can get now have the ability to route a signal from one of their internal pieces of hardware to one of several physical pins on the chip.

On the good old MEGA328P 28-pin DIP package, for example, the SPI hardware always appears on pins 17 (MOSI), 18 (MISO) and 19 (SCK). That can't be changed.

On the newer micros, that's not the case - depending on the actual micro you have. Some have what they call alternate pins, so you have a choice of 2 (or sometimes more) sets of pins where the signals will appear. These are pre-defined and you have to configure the hardware to tell it which set of pins you want to use.

I think, and I'll remind you of my earlier caveat on the Pi Pico, that you can make the SPI hardware signals (MOSI, MISO & SCK) appear on any actual pin on the package. So if you wanted the SPI SCK signal to be output on pin 1, then you can configure the i/o pin multiplexer (think that's the fancy name for it) to route that signal to pin 1.

Those 4 function calls probably need to be called in your setup() routine to tell the hardware which pins you would like the SPI signals to appear on.

I just read through the link I gave you previously, and there's an SPI example for the SD card library that is:

For example, because the SD library uses the SPI library, we can make it use a non-default pinout with a simple call

void setup() {
    SPI.setRX(4);
    SPI.setTX(7);
    SPI.setSCK(6);
    SPI.setCS(5);
    SD.begin(5);
}

That looks like the i/o mux inside the Pi Pico is being told which pins the SPI signals should appear on.

The tricky bit is figuring out the relationship between those numbers and the actual pins on the edge of the board. The DroneBotWorkshop has a pinout that seems to define the pins around the edge of the board as GP0 .. GP28. It would be nice to think that the 0 .. 28 are the number you pass into those functions to route the SPI signals where you want.

EDIT: This discussion might help:

Right, thank you so much, this info is very helpful.

Thanks once again

@markd833 Hi mark, upon trying to set the SPI pins I get an error saying SPIclass has no member function setRX, setTX, etc. All I wrote was what you showed in the void setup, am I missing something?

Thanks in advance,
Dean

Well that's interesting ...

Ok, so I don't have a PI Pico but I installed the Pi Pico board through the boards manager choosing "Arduino Mbed OS RP2040 Boards" and compiled this simple bit of code:

#include <SPI.h>

void setup() {
  // put your setup code here, to run once:
  SPI.setRX(4);
  SPI.setTX(7);
  SPI.setSCK(6);
  SPI.setCS(5);
}

void loop() {
  // put your main code here, to run repeatedly:
}

The compiler threw out the same error message for me as well.

I did a bit of digging and the following seemed to work for me - i.e. it compiled without those errors.

Firstly, I uninstalled the "Arduino Mbed OS RP2040 Boards". Then I found this website:

I followed the readme and installed via the Additional Boards Manager in Preferences. Once complete the boards manager gave me a ton of RP2040 boards to choose from. I chose "Raspberry Pi Pico" and compiled the same code.

This time it compiled without any complaints. See if that helps you out.

1 Like

Right great, thank you I will check that out now.

Thanks,
Dean.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.