Author Topic: SPI Communication  (Read 30915 times)

Cez

  • RFduino Jr. Member
  • **
  • Posts: 21
  • Karma: +2/-0
    • View Profile
SPI Communication
« on: November 08, 2013, 08:25:39 PM »
Hello,

I am trying to use RFduino to control a digital potentiometer using SPI. I already know how to do SPI communication with Arduino Uno, but how do I do SPI with RFduino? Obviously the pin connections are different. I couldn't find SPI included with the RFduino examples.

Can someone point me in the right direction? Thanks.
« Last Edit: November 08, 2013, 08:44:36 PM by Cez »

Cez

  • RFduino Jr. Member
  • **
  • Posts: 21
  • Karma: +2/-0
    • View Profile
Re: SPI Communication
« Reply #1 on: November 09, 2013, 05:31:20 PM »
For those who are wondering, I figured it out. I dug around in the source code and found where the SPI pins are defined. They are as follows:

SS        6
MOSI    5
MISO    3
SCK      4

I couldn't find any documentation for this, so I hope this helps anyone who is looking to use SPI with RFduino.

ruben

  • RFduino Jr. Member
  • **
  • Posts: 22
  • Karma: +0/-0
    • View Profile
Re: SPI Communication
« Reply #2 on: November 10, 2013, 09:19:47 AM »
I am still trying to figure out which ones are used for I2C communication. Any thoughts?

Cez

  • RFduino Jr. Member
  • **
  • Posts: 21
  • Karma: +2/-0
    • View Profile
Re: SPI Communication
« Reply #3 on: November 10, 2013, 04:03:59 PM »
I am still trying to figure out which ones are used for I2C communication. Any thoughts?

In the RFduino library folder (the one you copied to your Arduino installation directory following the quick start guide), open the file called "variant.h" located in this directory: RFduino\variants\RFduino

The info you are looking for should be there. It's where I found the SPI pins.

Here's the specific part I think you're looking for:

/*
 * Wire Interfaces
 */
#define PIN_WIRE_SDA         (6u)
#define PIN_WIRE_SCL         (5u)
« Last Edit: November 10, 2013, 04:08:01 PM by Cez »

ruben

  • RFduino Jr. Member
  • **
  • Posts: 22
  • Karma: +0/-0
    • View Profile
Re: SPI Communication
« Reply #4 on: November 14, 2013, 01:51:21 PM »
So if MOSI is 5 and SCL is 5
and if SS is 6 and SDA is 6, can the SD shield be used at the same time as Wire?
If not, there must be a command to set the wire "port" to different GPIO pins, right?

RFadmin

  • Administrator
  • *****
  • Posts: 77
  • Karma: +9/-2
    • View Profile
Re: SPI Communication
« Reply #5 on: November 14, 2013, 07:06:57 PM »
Hi everyone,

Instead of using Wire.begin(); (uses the default SCL on GPIO 5 and SDA on GPIO 6)

You can use Wire.beginOnPins(clock pin, data pin); this allows you to set your own clock and data pins.

stonez56

  • RFduino Newbie
  • *
  • Posts: 7
  • Karma: +0/-0
    • View Profile
Re: SPI Communication
« Reply #6 on: December 29, 2013, 03:53:44 AM »
Hi,
Any one tried "Wire.beginOnPins(clock pin, data pin); " command and worked? I tried to change SDA=Pin1 SCL=Pin2 on my RFduino, but it didn't work. However, SDA=Pin6, SCL=Pin5 work well though.

Stonez

edorphy

  • RFduino Full Member
  • ***
  • Posts: 86
  • Karma: +0/-0
    • View Profile
Re: SPI Communication
« Reply #7 on: December 29, 2013, 10:33:31 AM »
Stone,
We're you using serial also? Serial defaults to pin 0 and 1. (Look at the programming shield)
You shouldn't try physically multiplexing two protocols on one pin if you are using the serial library

Are you supplying via USB?

Your problem might be related to one of the above.

If you have an oscilloscope, try scoping out the clock and see if you get the square wave you expect.

stonez56

  • RFduino Newbie
  • *
  • Posts: 7
  • Karma: +0/-0
    • View Profile
Re: SPI Communication
« Reply #8 on: December 30, 2013, 05:10:20 AM »
Sorry, I am very new to electronics as well as to Rfduino.

I was trying to change the SCL & SDL to other pins when I was setting up the LCD display module. The LCD module requires 5V, so I have 5V power from an adaptor.

Code: [Select]
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

#define SCL_CLOCK_PIN 3
#define SDA_DATA_PIN 2


LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display

void showNumber(){
 
  int min1 = 1;
  int max1 = 3;
  int i = 0;
      lcd.setCursor(0,2);
      lcd.print("Random:");
 
  for(i=0;i<=50;i++){
    lcd.setCursor(8,2);

    //lcd.print(random(min1, max1));

    if(random(min1, max1)==1){
      lcd.print("playing");
    }else{
      lcd.print("learning");
    }
    delay(100);
    lcd.setCursor(8,2);
    lcd.print("           ");
   
    delay(100);
  }
 
}

void setup()
{
  //You may setup your own clock pin(SCL), data pin(SDA) 2013-12-14
  //[color=red]Wire.beginOnPins(SCL_CLOCK_PIN, SDA_DATA_PIN);[/color]
 
  lcd.init();                      // initialize the lcd
  lcd.init();
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(3,0);
  lcd.print("Hello, world!");
  lcd.setCursor(2,1);
  lcd.print(" Arduino!");
   lcd.setCursor(0,2);
  lcd.print("Ryan is learning");
   lcd.setCursor(2,3);
  lcd.print("Javascript!!");
  delay(2000);
}


void loop()
{
    lcd.init();
      lcd.setCursor(1,0);
  lcd.print("Ryan is learning");
  delay(1000);
        lcd.setCursor(0,1);
        lcd.print(" or ");
  delay(1000);
  lcd.setCursor(0,2);
  lcd.print("Ryan is playing?");
  delay(1000);
   lcd.setCursor(0,3);
   lcd.print("!@#$%*()_+");
   delay(1000);
   
  showNumber();
}

mkay

  • RFduino Team
  • *****
  • Posts: 405
  • Karma: +15/-0
    • View Profile
Re: SPI Communication
« Reply #9 on: January 02, 2014, 11:29:02 AM »
Hi Stonez, you must use Wire.beginOnPins(clock pin, data pin); to change the I2C pins, and it looks like the Wire is being handled by the LiquidCrystal_I2C library, so you would have to change the pins in the library.

You may want to try replacing Wire.begin(); to Wire.beginOnPins(clock pin, data pin); in your library.

Hope that helps  :)

kazhugu

  • RFduino Newbie
  • *
  • Posts: 7
  • Karma: +0/-0
    • View Profile
Re: SPI Communication
« Reply #10 on: January 18, 2014, 12:58:35 AM »
Hi Cez,

<spi.h> didn't work for me too. But I was able to bit-bang and got spi working. I modified this code (http://www.maximintegrated.com/design/tools/appnotes/4184/4184_spi.c). Hope this helps.

void spiWriteReg(const unsigned char regData)
{

  unsigned char SPICount;                               // Counter used to clock out the data
  unsigned char SPIData;                                // Define a data structure for the SPI data.

  SPIData = regData;                                    // Preload the data to be sent
                                                     
 
  for (SPICount = 0; SPICount < 8; SPICount++)          // Prepare to clock out the Address byte
  {
    if (SPIData & 0x80)                                 // Check for a 1
      digitalWrite (spi_mosi, HIGH);                    // and set the MOSI line appropriately
    else
      digitalWrite (spi_mosi, LOW);
    digitalWrite (spi_sck, HIGH);                       // Toggle the clock line
    digitalWrite (spi_sck, LOW);
    SPIData <<= 1;                                      // Rotate to get the next bit

  }                                                                                                                     
}

I then called this function to send one byte:

          digitalWrite(ssPin, LOW);
          spiWriteReg(0x55);
          digitalWrite(ssPin, HIGH);

I had to set up the spi pins manually at the start.

Regards,
Bala

MichaelJay

  • RFduino Newbie
  • *
  • Posts: 2
  • Karma: +0/-0
    • View Profile
Re: SPI Communication
« Reply #11 on: January 25, 2014, 08:53:25 AM »
Just trying to figure out SPI Communication on this RFDuino also. Looking at the SCK (GPIO4)  and MOSI (GPIO5) lines with a scope, I see nothing being sent when I transmit using the standard SPI.h library. Looking through this thread - do I read it that this library on the RFduino does not work?

mkay

  • RFduino Team
  • *****
  • Posts: 405
  • Karma: +15/-0
    • View Profile
Re: SPI Communication
« Reply #12 on: January 27, 2014, 03:33:54 PM »
The SPI should be working fine, I have used it with the microSD shield several times. What kind of problem are you running into?

kazhugu

  • RFduino Newbie
  • *
  • Posts: 7
  • Karma: +0/-0
    • View Profile
Re: SPI Communication
« Reply #13 on: February 21, 2014, 07:23:35 AM »
For me, I could only get spi to work by bitbanging. Do you have an example code for RFDuino as an SPI master?

Thanks.

mkay

  • RFduino Team
  • *****
  • Posts: 405
  • Karma: +15/-0
    • View Profile
Re: SPI Communication
« Reply #14 on: February 21, 2014, 05:39:45 PM »
Hi Kazhugu, the SD card examples use SPI, take a look at those, they may help.

 

anything