Skip to content

Instantly share code, notes, and snippets.

@pkourany
Created July 20, 2014 05:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pkourany/6a21f031d197057f6174 to your computer and use it in GitHub Desktop.
Save pkourany/6a21f031d197057f6174 to your computer and use it in GitHub Desktop.
Fujitsu FRAM alpha library - for single chip only (for now)
/*
Fujitsu FRAM alpha library
*/
#include "FRAM.h"
FRAM::FRAM(uint8_t chipSelect)
{
_cs = chipSelect;
pinMode(_cs, OUTPUT);
digitalWrite(_cs, HIGH);
}
int8_t FRAM::FRAMRead(uint16_t addr, uint8_t* buf, uint16_t count)
{
if (addr > FRAM_ADDR_MAX)
return -1;
PIN_MAP[_cs].gpio_peripheral->BRR = PIN_MAP[_cs].gpio_pin; //FRAM CS LOW
SPI.transfer(CMD_READ);
SPI.transfer((uint8_t)(addr >> 8));
SPI.transfer((uint8_t)(addr & 0xFF));
for (uint16_t i=0; i < count; i++)
buf[i] = SPI.transfer(DUMMY_BYTE);
PIN_MAP[_cs].gpio_peripheral->BSRR = PIN_MAP[_cs].gpio_pin; //FRAM CS HIGH
return 0;
}
int8_t FRAM::FRAMWrite(uint16_t addr, uint8_t* buf, uint16_t count)
{
if (addr > FRAM_ADDR_MAX)
return -1;
PIN_MAP[_cs].gpio_peripheral->BRR = PIN_MAP[_cs].gpio_pin; //FRAM CS LOW
SPI.transfer(CMD_WREN); //write enable
PIN_MAP[_cs].gpio_peripheral->BSRR = PIN_MAP[_cs].gpio_pin; //FRAM CS HIGH
PIN_MAP[_cs].gpio_peripheral->BRR = PIN_MAP[_cs].gpio_pin; //FRAM CS LOW
SPI.transfer(CMD_WRITE); //write command
SPI.transfer((uint8_t)(addr >> 8));
SPI.transfer((uint8_t)(addr & 0xFF));
for (uint16_t i = 0;i < count;i++)
SPI.transfer(buf[i]);
PIN_MAP[_cs].gpio_peripheral->BSRR = PIN_MAP[_cs].gpio_pin; //FRAM CS HIGH
return 0;
}
int8_t FRAM::FRAMisPresent(void)
{
uint8_t a[4] = {0,0,0,0};
uint8_t results;
PIN_MAP[_cs].gpio_peripheral->BRR = PIN_MAP[_cs].gpio_pin;
SPI.transfer(CMD_RDID);
for (uint8_t i = 0; i < 4; i++)
a[i] = SPI.transfer(DUMMY_BYTE);
PIN_MAP[_cs].gpio_peripheral->BSRR = PIN_MAP[_cs].gpio_pin;
uint16_t prodID = (a[2] << 8) + a[3];
if ((a[0] != 0x04) || (prodID != 0x0302))
return -1;
else
return 0;
}
/*
Fujitsu FRAM alpha library
*/
#include "application.h"
#define CMD_WREN 0x06 // 0000 0110 Set Write Enable Latch
#define CMD_WRDI 0x04 // 0000 0100 Write Disable
#define CMD_RDSR 0x05 // 0000 0101 Read Status Register
#define CMD_WRSR 0x01 // 0000 0001 Write Status Register
#define CMD_READ 0x03 // 0000 0011 Read Memory Data
#define CMD_WRITE 0x02 // 0000 0010 Write Memory Data
#define CMD_RDID 0x9F // 1001 1111 Read device ID
#define DUMMY_BYTE 0x7E // Dummy write value for SPI read
#define FRAM_ADDR_MAX 0x1fff // Max address value (8091)
#define FRAM_CS_PIN SS // FRAM chip select
class FRAM
{
private:
uint8_t _cs;
public:
FRAM(uint8_t chipSelect);
int8_t FRAMRead(uint16_t addr, uint8_t* buf, uint16_t count);
int8_t FRAMWrite(uint16_t addr, uint8_t* buf, uint16_t count);
int8_t FRAMisPresent(void);
};
#include "FRAM.h"
FRAM fram(A2);
uint16_t addr = 0;
uint8_t test;
void setup(void) {
Serial.begin(115200);
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
SPI.setClockDivider(SPI_CLOCK_DIV4);
while (!Serial.available()) SPARK_WLAN_Loop();
Serial.println("Starting test");
if (fram.FRAMisPresent() == 0) {
Serial.println("Found SPI FRAM");
} else {
Serial.println("No SPI FRAM found ... check your connections\r\n");
while (1);
}
// Read the first byte
fram.FRAMRead(0x0, &test, 1);
Serial.print("Restarted "); Serial.print(test); Serial.println(" times");
// Test write ++
test++;
fram.FRAMWrite(0x0, &test, 1);
for (uint16_t a = 1; a < 8192; a++) {
uint8_t v = (uint8_t)(a & 0xFF);
fram.FRAMWrite(a, &v, 1);
}
// dump the entire 8K of memory!
uint8_t value;
for (uint16_t a = 0; a < 8192; a++) {
fram.FRAMRead(a, &value, 1);
if ((a % 32) == 0) {
Serial.print("\n 0x"); Serial.print(a, HEX); Serial.print(": ");
}
Serial.print("0x");
if (value < 0x1)
Serial.print('0');
Serial.print(value, HEX); Serial.print(" ");
}
}
void loop(void) {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment