Author Topic: SIMBLEE SPI  (Read 792 times)

gouthampal

  • RFduino Newbie
  • *
  • Posts: 7
  • Karma: +0/-0
    • View Profile
SIMBLEE SPI
« on: January 13, 2017, 04:38:15 PM »
I am working with LDC1101 using SPI protocol. I wrote the code for ARDUINO UNO (working fine) and I need to get this working with simblee.
I followed the rfduino quick start guide and added the rfduino libraries to the ~/hardware/arduino/ folder.
But I am unable to use the SPI library in that folder. The SPI in the /arduino/avr folder is being used.

Could someone help me with this issue?
I also tried programming with the normal arduino SPI library connecting the spi pins to pins 13,12,11,10 like in arduino uno, but did not work.

Any other way to do SPI on simblee?



tolson

  • Global Moderator
  • *****
  • Posts: 860
  • Karma: +20/-0
    • View Profile
    • Thomas Olson Consulting
Re: SIMBLEE SPI
« Reply #1 on: January 15, 2017, 02:13:56 PM »
For SImblee you should use the Simblee guide, not the RFduino guide.

Under the newer Arduino IDEs you do not install RFdigital (RFduino and/or Simblee) libraries directly into the file system.
You enter a path to a JSON file in preferences. And then use Board Manager to select and install the Simblee Library.


MRF

  • RFduino Newbie
  • *
  • Posts: 6
  • Karma: +0/-0
    • View Profile
Re: SIMBLEE SPI
« Reply #2 on: January 15, 2017, 11:27:56 PM »
Hi,

I'm using the ADS1118 ADC with the simblee and communication via SPI works fine.
I saw you replied to my older post, when I also got Problems, but now everything works.
I'm posting a section of my code, maybe you can see how I got to use SPI and it will help you:  ;)
Sorry for my bad English.  ::)


Code: [Select]
#include <SPI.h>         // SPI

// SPI init
SPISettings ADS1118(SPI_CLOCK_DIV4, MSBFIRST, SPI_MODE1);
const int cs1 = 6;  // Chip Select 1

struct myPacket // struct myPacket
{
  uint16_t rawData[4];  // for raw temperature data             4*2 = 8 Byte
  long rawTemp[2];      // for cold junction temperature  2*4 = 8 Byte --> 16 Byte
};
struct myPacket packet; // defines paket so send

// Config Register ADS1118
/* * Bit 15:    1   - Start a single conversion / Always read back as 0 (default)
   * Bit 14-12: 000 - Multiplexer AIN0(Pos.) and AIN1(Neg.)
   * Bit 11-09: 010 - FSR +- 2.048 VDC (default)
   *            101 - FSR +- 0.256 VDC
   * Bit 08:    1   - Power Down & Single shot (default)
   *            0   - Continuous Conversion mode
   * Bit 07-05: 100 - 128 SPS (default)
   *            111 - 860 SPS
   * Bit 04:    0   - ADC Mode (default)
   *            1   - Temperature sensor mode
   * Bit 03:    0   - Pull-up on DOUT disabled
   *            1   - Pull-up on DOUT enabled (default)
   * Bit 02-01: 01  - Valid data / Update config register (default)
   * Bit 00:    1   - Not used / Reserved */

void setup()
{
  SPISettings ADS1118(SPI_CLOCK_DIV4, MSBFIRST, SPI_MODE1); // SPI init
  SPI.begin();                   
  pinMode(cs1, OUTPUT);
  digitalWrite(cs1, HIGH);
  delay(2);
 
  SPI.beginTransaction(ADS1118);  // ADS1118 init
  digitalWrite(cs1, LOW);         // activate ChipSelect
  SPI.transfer(0b10001111);
  SPI.transfer(0b11111010);
  SPI.transfer(0);
  SPI.transfer(0);
  digitalWrite(cs1, HIGH);
  SPI.endTransaction();
  delay(2);
}
   
void loop()
{   
    Simblee_ULPDelay(SECONDS(30));
    readTemp();
}

//-------------------------------------------

float readTemp()
{
  SPI.beginTransaction(ADS1118);
  digitalWrite(cs1, LOW);  // activate ChipSelect
  // intern temperature ( cold junction )
  byte DataMSB = SPI.transfer(0b10001111); 
  byte DataLSB = SPI.transfer(0b11101010);
  byte ConfigMSB = SPI.transfer(0);
  byte ConfigLSB = SPI.transfer(0);
  digitalWrite(cs1, HIGH);
  SPI.endTransaction();
  delay(2);
  // Build the raw value from the most and least significant bits
  word Data = (DataMSB << 8) | DataLSB;
  packet.rawTemp[0] = (Data >> 2) * 31.25;

  SPI.beginTransaction(ADS1118);
  digitalWrite(cs1, LOW);  // activate ChipSelect
  // measure channel A0 und A1 in Single-Shot-Mode
  DataMSB = SPI.transfer(0b10111111);
  DataLSB = SPI.transfer(0b11101010);
  ConfigMSB = SPI.transfer(0);
  ConfigLSB = SPI.transfer(0);
  digitalWrite(cs1, HIGH);
  SPI.endTransaction();
  delay(2);
  // Build the raw value from the most and least significant bits
  packet.rawData[0] = (DataMSB << 8) | DataLSB;

  SPI.beginTransaction(ADS1118);
  digitalWrite(cs1, LOW);  // activate ChipSelect
  // measure channel A2 und A3 in Single-Shot-Mode
  DataMSB = SPI.transfer(0b10001111);
  DataLSB = SPI.transfer(0b11111010);
  ConfigMSB = SPI.transfer(0);
  ConfigLSB = SPI.transfer(0);
  digitalWrite(cs1, HIGH);
  SPI.endTransaction();
  delay(2);
  // Build the raw value from the most and least significant bits
  packet.rawData[1] = (DataMSB << 8) | DataLSB;
}

gouthampal

  • RFduino Newbie
  • *
  • Posts: 7
  • Karma: +0/-0
    • View Profile
Re: SIMBLEE SPI
« Reply #3 on: January 17, 2017, 10:41:14 AM »
Thanks,
I got it working. Issue was, the bits were reversed in the simblee SPI, So had to feed in the data and then the address.

Code: [Select]
#include <SPI.h>

#include <Messenger.h>

#include <SPI.h>

#include <Kalman.h>

#define window 10 //change window for average command
double measurement, filteredMeasurement;
Kalman myFilter(0.125,32,1023,0);
const int slaveSelectPin = 6;
// Instantiate Messenger object with the message function and the default separator
// (the space character)
Messenger message = Messenger();


// Define messenger function
void messageCompleted() {
  // This loop will echo each element of the message separately
  while ( message.available() ) {
   
    if ( message.checkString("sleep") ) {
             spi_write(0x01,0x0B); //changed
             byte x= spi_read(0xFF8B); //changed
             Serial.println(x,HEX);
     
    } else if ( message.checkString("wakeup") ) {
            //spi_write(0x01,0x67);
            spi_write(0x00,0x0B); //changed
            byte y= spi_read(0xFF8B); //changed
            Serial.println(y,HEX);
           
    } else if ( message.checkString("readrp") ) {
     
        unsigned int Rp_data=Rp_read();
        Serial.println(Rp_data);
       
    } else if ( message.checkString("check") ){
      byte id = spi_read(0xFFBF); //changed
      byte status_reg = spi_read(0xFFA0); //changed
       
      Serial.println(id,HEX);
      Serial.println(status_reg,HEX);

      if(id == 0xD4 && (status_reg ==0x28 ||status_reg ==0xA8 ))
      Serial.println ("LDC1101 working fine");
      else
      Serial.println("wakeup LDC1101");
     
      } else if ( message.checkString("averagerp") ) {
      long int sum=0, average=0;
      for (int i=0;i<window;i++)   //change window for number of reading to be averaged (default 10)
      {
        unsigned int Rp_data=Rp_read();
      //  Serial.println(Rp_data);
        sum+=Rp_data;
         
      }
    average=sum/10;
    //Serial.print("Average:");
    Serial.println(average);
  }else if ( message.checkString("readlh") ) {
     
        unsigned long int Lh_data=Lh_read();
        Serial.println(Lh_data);
       
    } else if ( message.checkString("readlp") ) {
     
        unsigned int Lp_data=Lp_read();
        Serial.println(Lp_data);
       
    }
  }
}

void setup() {
  // Initiate Serial Communication
  Serial.begin(115200);
  message.attach(messageCompleted);
 
  pinMode (slaveSelectPin, OUTPUT);
     SPI.begin();

    SPI.beginTransaction(SPISettings(SPI_CLOCK_DIV4, MSBFIRST, SPI_MODE3  )); //changed
 
}

void loop() {
 
  // The following line is the most effective way of
  // feeding the serial data to Messenger
  while ( Serial.available() ) message.process( Serial.read() );


}

//Functions

byte spi_read(int address)
{
  digitalWrite(slaveSelectPin, LOW);
  byte Val = SPI.transfer16(address);
 
  digitalWrite(slaveSelectPin, HIGH);
  return Val;
}

void spi_write(byte address,byte data)
{
 
  int val= word(address,data);
  digitalWrite(slaveSelectPin, LOW);
  byte Val = SPI.transfer16(val);
  digitalWrite(slaveSelectPin, HIGH);
 
}

unsigned int Rp_read()
{
  byte Rp_Low = spi_read(0xFFA1); //changed
  byte Rp_High = spi_read(0xFFA2); //changed
 
  unsigned int conc_val = word(Rp_High, Rp_Low);
 
  return conc_val;
}

unsigned long int Lh_read()
{
 
  byte Lh_Low = spi_read(0xFFB8); //changed
  byte Lh_Mid = spi_read(0xFFB9); //changed
  byte Lh_High = spi_read(0xFFBA); //changed

  unsigned long int conc_val2 =   Lh_High * pow(2,16) + word(Lh_Mid, Lh_Low); //  Lh_High * pow(2,16) + word(Lh_Mid, Lh_Low)
  return conc_val2;
 
}

unsigned int Lp_read()
{
  byte Rp_Low = spi_read(0xFFA3); //changed
  byte Rp_High = spi_read(0xFFA4); //changed
 
  unsigned int conc_val3 = word(Rp_High, Rp_Low);
 
  return conc_val3;
}