Modifying SpiRam to write/read ints etc.

Hello everyone,

So I have recently started using an external SRAM chip (23LC1024) which connects via SPI. Anyway i wanted to make my life a bit simpler as i intend on using it within a lot of projects and would like methods for easily storing integers and integer arrays (initially).

I thought I had struck gold when i managed to write the functions for reading and writing a single int

Additional lines in header fle

int read_int(long address);
    int write_int(long address, int data_int);

Additional lines in cpp file

int SpiRAM::write_int(long address, int data_int)
{
  _prepare(STREAM_MODE,WRITE,address);
  SPI.transfer(data_int/256);
  SPI.transfer(data_int%256);
  disable();

  return data_int;
}

int SpiRAM::read_int(long address)
{
  int read_int;

  _prepare(STREAM_MODE,READ,address);
  read_int = SPI.transfer(0xFF)*256+SPI.transfer(0xFF);
  disable();

  return read_int;
}

Boom this works big pat on the back (First time ever trying to even modify a library never mind update one)

Anyway there is a big advantage time wise to put the device in stream mode once and then write a whole array so i thought i'll just modify it to accept an array... and this is where i am stuck.

header code

void read_ints(long address,int *data_int, long length);
    void write_ints(long address,int *data_int, long length);

cpp code

void SpiRAM::write_ints(long address,int *data_int,long length)
{
  long i;
  _prepare(STREAM_MODE,WRITE,address);
  for (i=0;i<length;i++){
    SPI.transfer(data_int[i]/256);
    SPI.transfer(data_int[i]%256);
}
  disable();
}

void SpiRAM::read_ints(long address,int *data_int, long length)
{
  int read_int;
  long i;

  _prepare(STREAM_MODE,READ,address);
  for (i=0;i<length;i++){
    read_int[i] = SPI.transfer(0xFF)*256+SPI.transfer(0xFF);
  }
  disable();

}

I followed the same principle as for the char arrays but I can't get it to work.

void SpiRAM::write_stream(long address, char *buffer, long length)
{
  long i;

  _prepare(STREAM_MODE,WRITE,address);
  for (i = 0; i < length; i++) {
    SPI.transfer(buffer[i]);
  }
  disable();
}

I always get this sort of errors

D:\Users\Dropbox\Arduino\libraries\SpiRAM\SpiRAM.cpp: In member function 'void SpiRAM::read_ints(long int, int*, long int)':
D:\Users\Dropbox\Arduino\libraries\SpiRAM\SpiRAM.cpp:167:15: error: invalid types 'int[long int]' for array subscript

Any suggestions?? (i'm sure i have been stumped by this problem before, or one similar but can never get my head round how to solve it)

note - be kind I only learnt c programming through messing around with arduinos. Mechanical Engineering student hah.

SpiRAM.zip (10.4 KB)

Is it really possible to store such a large array that you need a long for the index type?

Ahhh i fixed it!

It turned out that the issue was not passing a pointer for the int array but a number of coding errors within the functions read_ints and write_ints which caused the problem!

SpiRAM.zip (10.4 KB)

hi bosses

i want to add spi ram 23k256 to arduino due.the code works on uno but it doesn't work on due.

coz i add 250khz of samples into due sram and it crashes.when i add it on uno it works but uno is slow.i

have to get 250000 samples in 1 sec from analog in and need to return stand deviation in 1 sec.any

advice is appreciated.

Your best bet is to do it the way calculators do it and simply save the statistics, so you can do the calculation at any time. you just need to save the number of data points, the sum of the data and the sum of the squares. The mean and std deviation can be calculated from that.

thanks for the advice.i was doing well to find max/min and sum.but when i need (data-avg)^2 the problem comes.coz i need the average to continue.when i got the average adc read have completed.to recall all data 1 by 1 i need them to put in250000 array.i have to call all data 1 by 1 to calulate (data-avg)^2 again.so there comes large sram requirement.standard d is very useful,but i need avg to continue.
thanks for d help.

bro keith.i got yr point.sorry that i am lousy in statistics.just now i search sum of sqaure to get standard deviation.i will try yr idea soon thanks.
formula https://people.richland.edu/james/lecture/m170/ch03-var.htmlhttps://people.richland.edu/james/lecture/m170/ch03-var.html

sayagyimyo I have worked out why the code doesn't work on your Due - I hard coded the library when i first wrote it as it was easier and I was just learning.

On arduino Due the integers are 4 bytes not 2 which is why the read/write int parts will malfunction.

I haven't got a chance to check this now but try updating the spiRam_extended.cpp with this

///////////////////////////
// Int transfer functions
int SpiRAM::write_int(long address, int data_int)
{
  dataUnion.intNum = data_int;
  _prepare(STREAM_MODE,WRITE,address);
  for (int i = 0;i<sizeof(int);i++){}
    SPI.transfer(dataUnion.b[i]);
  }
  disable();

  return data_int;
}

int SpiRAM::read_int(long address)
{
  _prepare(STREAM_MODE,READ,address);
  for (int i = 0;i<sizeof(int);i++){}
    dataUnion.b[i] = SPI.transfer(0xFF);
  }
  disable();
  return dataUnion.intNum;

}

It will only change the writing and reading of single ints but i'm sure you could extend this to the array functions

If you need any other help I will try and do an update on this when i get back from Paris but here is the location where I will post tutorial and news updates and the link to the github

Got bored and modified the array reading and writing options - again untested as i currently dont have a due or the time :stuck_out_tongue:

void SpiRAM::write_ints(long address,int *data_int,long length)
{
  long i;
  _prepare(STREAM_MODE,WRITE,address);
  for (i=0;i<length;i++){
    dataUnion.intNum = data_int[i];
    for (int j = 0;j<sizeof(int);j++){}
        SPI.transfer(dataUnion.b[j]);
    }
  }
  disable();
}

void SpiRAM::read_ints(long address,int *data_int, long length)
{
  long i;
  _prepare(STREAM_MODE,READ,address);
  for (i=0;i<length;i++){
    for (int j = 0;j<sizeof(int);j++){}
        dataUnion.b[j] = SPI.transfer(0xFF);
    }
    data_int[i] = dataUnion.intNum;
  }
  disable();

}

Thanks mr slzer. I will just need write byte and read byte.due got some errors while compling like clock =0 got redeclared this and that.i will just wait for d complete library in zip file.anyway i don't need sram for this while after getting mr keith idea and the code run beautiful ly and give me standard d in just 837 ms .thanks again for help from arduino experts.have a good day :slight_smile: