extend memory 23K256

02 Mar 2011

Hi all,

I hooked up a microchip 23K256 (32 KB) to the Mbed. I used this programm:http://mbed.org/users/romilly/programs/Ser23K256/5z0rr

But no data is written to the memory. thus no output. Checked wiring three times, thats oke

Any suggestions ?

greets,

Marcel

02 Mar 2011

Can you show us your code?

04 Mar 2011

Sure i can:

#include "mbed.h"
#include "Ser23K256.h"


SPI spi(p11,p12,p13);
Ser23K256 sram(spi,p14);


int main() {
    char buff[50];
    sram.write(0, 'h');
    sram.write(1, 'i');
    sram.write(2, '!');
    sram.write(3, '\0');
    for (int address = 0; address < 4; address++) {
        buff[address] = sram.read(address);
    }
    printf("sram = %s\r\n", buff);
  while (1) {
    sram.write(0, "Hello world!",12);
    sram.read(0, buff, 12);
    buff[12]='\0';
    printf("now = %s\r\n", buff);
    wait(0.5);
    }
}

The strange thing is that (yesterday) is worked...once with the first part (with the loop) left out. After reloading the program, nothing. Tried a second memory module. same result. I have to test the modules on a arduino, see where that is leading to.

greets,

Marcel

04 Mar 2011

Hmm, since this is the test program from the library itself, it should be working. So this might be a hardware issue. Ideas that come to my mind:

  • the wiring is not OK (but you say you have checked this multiple times)
    • I for myself tend to mix MOSI and MISO
    • did you use the other SPI port?
  • add a capacitor (100nF) directly on the sRAM between Vcc and GND - you might have power supply problems otherwise
  • could it be you have a 23A256 - it uses different voltages?

Especially the last 2 things can explain why it worked the first time.

04 Mar 2011

Hi,

It's a 23K256 so Vsupply should be 3.3V (get it from Mbed) I will place a capacitor , and test again.

greets,

Marcel

04 Mar 2011

Tested it, also connected the hold pin to V+, now it seems to work. How do i rewrite the program so that i write unsigned int values that are in an array, to the ext. memory, and and read them out later.

greets,

Marcel

06 Mar 2011

You can use sprintf() to write into a character buffer, which you then can write to the sRAM. The other way round would then use sscanf. This would use an ASCII representation.

Or you write the value directly by providing the write function with the address of an e.g. float, and sizeof(float) as the length. This would require that you exactly know where everything lies in the sRAM.

16 Mar 2011

This part:

int main() {
    char buff[50];
    sram.write(0, 'h');
    sram.write(1, 'i');
    sram.write(2, '!');
    sram.write(3, '\0');
    for (int address = 0; address < 4; address++) {
        buff[address] = sram.read(address);
    }

doesn't appear to work in the example (see above) Last part works oke. Any idea's ?

16 Mar 2011

marcel van de Kamp wrote:

How do i rewrite the program so that i write unsigned int values that are in an array, to the ext. memory, and and read them out later.

If the array of ints is not too big, you can make a union with a char array, then write the char to the EEPROM. Then you read the chars back into the union and you can access the data as ints from the other side of the union.

For instance:

// Assume there are "n" ints to be stored
// Make a union of the n ints, and a char buffer of size 4*n
typedef union {
    struct {
        // param lists              Byte offset
        int variable_1;              // 0 to 3
        int variable_2;              // 4 to 7
        int variable_3;              // 8 to 11    
          . . . 
        int variable_n-1;            // 4(n-2) to (4n -5)    
        int variable_n;              // 4(n-1) to (4n -1) 
    } varlist;
        char cp[4n];
    } ptr;
} _EEPROM_PARAMS;

// Create a corresponding parameter set
_EEPROM_PARAMS p;

// Write and read the variables as follows
    p.varlist.variable_1 = something;
    somethingelse = p.varlist.variable_2;

// Write and read the EEPROM as follows
   for(i = 0; i < 4n; i++) {
       sram.write(i, p.ptr.cp[i]);
   }

   sram.read(0, p.ptr.cp, 4n);

The usual disclaimers: I haven't tested or compiled. But I have used something similar in my own projects, so this should work for you, too.

[Edits] A bunch of typos...and Andy's method seems more elegant in any case.

16 Mar 2011

There's plenty of ways to skin a cat as they say. I prefer to just cast:-

// Assume there are "n" ints to be stored
// Make a union of the n ints, and a char buffer of size 4*n
typedef struct {
    // param lists              Byte offset
    int variable_1;              // 0 to 3
    int variable_2;              // 4 to 7
    int variable_3;              // 8 to 11    
      . . . 
    int variable_n-1;            // 4(n-1) to (4n -1)    
    int variable_n;              // 4n to (4n+3) 
} _EEPROM_PARAMS;

// Create a corresponding 
_EEPROM_PARAMS p;

// Write and read the variables as follows
    p.varlist.variable_1 = something;
    somethingelse = p.varlist.variable_2;

// Write and read the EEPROM as follows
   char *s = (char *)&p;
   for(i = 0; i < sizeof(_EEPROM_PARAMS); i++, s++) {
       sram.write(i, *(s));
   }