FRAM problems

Anyone else having problems with writing/reading to/from fram? I have a Indio unit with 1286 topboard. Using the example sketch, with a delay just after setup to make sure I don't miss the serial output, yields no result. And even if it does output something, it are some random ascii chars.

I already tried another ethernet module but no succes. Ethernet sketch works fine tho.

Argus
Argus
22
| 0 0 0
Asked on 4/1/15, 11:47 AM
1
vote
1415 Views

Hi,

I checked and found out that the wrong chip select pin was defined in the latest demo code (was D7, should have been D6). Sorry for the inconvenience, I've updated the downloadable files in the tech centre.

For a quick try please use this code:

//this sketch writes the string "This is a test" to the FRAM memory, reads it back and post the result to the serial terminal.

#include <SPI.h>

const byte CMD_WREN = 0x06; //0000 0110 Set Write Enable Latch
const byte CMD_WRDI = 0x04; //0000 0100 Write Disable
const byte CMD_RDSR = 0x05; //0000 0101 Read Status Register
const byte CMD_WRSR = 0x01; //0000 0001 Write Status Register
const byte CMD_READ = 0x03; //0000 0011 Read Memory Data
const byte CMD_WRITE = 0x02; //0000 0010 Write Memory Data

const int FRAM_CS1 = 6; //chip select 1

/**
 * Write to FRAM 
 * addr: starting address
 * buf: pointer to data
 * count: data length. 
 *        If this parameter is omitted, it is defaulted to one byte.
 * returns: 0 operation is successful
 *          1 address out of range
 */
int FRAMWrite(int addr, byte *buf, int count=1)
{


  if (addr > 0x7ff) return -1;

  byte addrMSB = (addr >> 8) & 0xff;
  byte addrLSB = addr & 0xff;

  digitalWrite(FRAM_CS1, LOW);   
  SPI.transfer(CMD_WREN);  //write enable 
  digitalWrite(FRAM_CS1, HIGH);

  digitalWrite(FRAM_CS1, LOW);
  SPI.transfer(CMD_WRITE); //write command
  SPI.transfer(addrMSB);
  SPI.transfer(addrLSB);

  for (int i = 0;i < count;i++) SPI.transfer(buf[i]);

  digitalWrite(FRAM_CS1, HIGH);

  return 0;
}

/**
 * Read from FRAM (assuming 2 FM25C160 are used)
 * addr: starting address
 * buf: pointer to data
 * count: data length. 
 *        If this parameter is omitted, it is defaulted to one byte.
 * returns: 0 operation is successful
 *          1 address out of range
 */
int FRAMRead(int addr, byte *buf, int count=1)
{

  if (addr > 0x7ff) return -1;

  byte addrMSB = (addr >> 8) & 0xff;
  byte addrLSB = addr & 0xff;

  digitalWrite(FRAM_CS1, LOW);

  SPI.transfer(CMD_READ);
  SPI.transfer(addrMSB);
  SPI.transfer(addrLSB);

  for (int i=0; i < count; i++) buf[i] = SPI.transfer(0x00);

  digitalWrite(FRAM_CS1, HIGH);

  return 0;
}

void setup()
{
  Serial.begin(9600);  
  delay(5000);
  pinMode(FRAM_CS1, OUTPUT);
  digitalWrite(FRAM_CS1, HIGH);


  //Setting up the SPI bus
  SPI.begin();
  SPI.setDataMode(SPI_MODE0);  
  SPI.setBitOrder(MSBFIRST);
  SPI.setClockDivider(SPI_CLOCK_DIV2);

  //Test
  char buf[]="This is a test";

  FRAMWrite(1, (byte*) buf, 14);  
  FRAMRead(1, (byte*) buf, 14);

  for (int i = 0; i < 14; i++) Serial.print(buf[i]);
}

void loop()
{
}

 

Loic De Buck
Loic De Buck
1237
| 3 1 3
Answered on 4/7/15, 3:40 AM
1
vote

Hi , I have used this code but my serial monitor prints "*449490:29:" not "This is a test" Any thoughts or help would be great

John Ross
on 6/23/17, 8:15 PM

Thx, I can confirm this works now.

Argus
Argus
22
| 0 0 0
Answered on 4/8/15, 8:45 AM
1
vote

As Loic mentioned, for FRAM on D21G see https://github.com/Industruino/democode/tree/master/fram_D21G

For FRAM on 32u4 / 1286 see https://github.com/Industruino/libraries#fram

Tom
Tom
5210
| 0 0 1
Answered on 8/23/17, 7:56 AM
0
vote

Hi I have used the above code on a SMAD21 top board with Ethernet module attached, it complies and loads but serial prints characters which do not match the "This is a test", any comments on this would be great?

John Ross
John Ross
7
| 0 0 0
Answered on 6/26/17, 2:24 PM
0
vote

Hi John, i was able to reproduce your error, there seem to be 2 things going on: 1) you are using SerialUSB with the Serial Monitor, and (at least in my setup with Linux) the USB automatically disconnects after upload for about 2-3 seconds, so i don't see any text printed immediately after startup. adding a delay(3000) after the SerialUSB.begin fixes this 2) the SPI clock divider seems to be different on the D21G; please try with SPI.setClockDivider(SPI_CLOCK_DIV8); I still don't get the full text correctly; it outputs "This is a EEEE" but we're getting closer.

Tom
on 6/27/17, 7:19 AM

Dear John, I've added an example sketch for the FRAM with D21G on github: https://github.com/Industruino/democode/tree/master/fram_D21G Because the M0 has a different way of setting up the SPI I had to change a few lines for that. I also set the CS lines of the SD card and Ethernet to HIGH to work correctly with the 5V level shifters on the D21G.

Loic De Buck
on 6/27/17, 8:24 AM

Your answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

Ask a Question

Keep Informed

About This Forum

This community is for professionals and enthusiasts of our products and services.

Read Guidelines

Question tools

7 follower(s)

Stats

Asked: 4/1/15, 11:47 AM
Seen: 1415 times
Last updated: 8/23/17, 7:56 AM