Fujitsu MB85RSxx serial FRAM test program

Dependencies:   mbed MB85RSxx_SPI

Connectivity

MB82RSxx pinmbed LPC1114FN28 pinmbed LPC1768 pinArudino form factor
1 (_CS)9 (dp9)8 (p8)D10
2 (SO)1 (dp1)6 (p6)D12
3 (_WP)21 (VDD)40 (VOUT)3V3
4 (VSS)22 (GND)1 (GND)GND
5 (SI)2 (dp2)5 (p5)D11
6 (SCK)6 (dp6)7 (p7)D13
7 (_HOLD)21 (VDD)40 (VOUT)3V3
8 (VDD)21 (VDD)40 (VOUT)3V3

main.cpp

Committer:
MACRUM
Date:
2017-04-16
Revision:
0:20b32d13a758
Child:
1:bdf8b378dbc9

File content as of revision 0:20b32d13a758:

#include "mbed.h"

DigitalOut myled(LED1);
Serial pc(USBTX, USBRX);

#if defined(TARGET_LPC1768)
SPI _spi(p5, p6, p7); // mosi, miso, sclk
DigitalOut _cs(p8);
#elif defined(TARGET_LPC1114)
SPI _spi(dp2, dp1, dp6); // mosi, miso, sclk
DigitalOut _cs(dp9);
#else // Arduino R3 Shield form factor
SPI _spi(D11, D12, D13); // mosi, miso, sclk
DigitalOut _cs(D10);
#endif

#define MB85_WREN   0x06
#define MB85_WRDI   0x04
#define MB85_RDSR   0x05
#define MB85_WRSR   0x01
#define MB85_READ   0x03
#define MB85_WRITE  0x02
#define MB85_RDID   0x9F
#define MB85_FSTRD  0x0B
#define MB85_SLEEP  0xB9

#define MB85_DENSITY_64K    0x3
#define MB85_DENSITY_256K   0x5
#define MB85_DENSITY_512K   0x6
#define MB85_DENSITY_1M     0x7
#define MB85_DENSITY_2M     0x8

static int _address_bits = 0;

int read_device_id(uint8_t* device_id)
{
    _cs = 0;
    _spi.write(MB85_RDID);
    for (int i = 0; i < 4; i++) {
        *device_id++ = (uint8_t)_spi.write(0);
    }
    _cs = 1;
    
    return 0;
}

uint8_t read_status()
{
    _cs = 0;
    _spi.write(MB85_RDSR);
    uint8_t st = (uint8_t)_spi.write(0);
    _cs = 1;
    
    return st;
}

int read(uint32_t address, uint8_t* data, uint32_t len)
{
    _cs = 0;
    
    _spi.write(MB85_READ);
    if (_address_bits == 24) {
        _spi.write((uint8_t)((address >> 16) & 0xFF));
    }
    _spi.write((uint8_t)((address >>  8) & 0xFF));
    _spi.write((uint8_t)((address >>  0) & 0xFF));
    for (uint32_t i = 0; i < len; i++) {
        *data++ = (uint8_t)_spi.write(0);
    }
    _cs = 1;
    
    return 0;
}

uint8_t read(uint32_t address)
{
    uint8_t data;
    _cs = 0;
    _spi.write(MB85_READ);
    if (_address_bits == 24) {
        _spi.write((uint8_t)((address >> 16) & 0xFF));
    }
    _spi.write((uint8_t)((address >>  8) & 0xFF));
    _spi.write((uint8_t)((address >>  0) & 0xFF));
    data = (uint8_t)_spi.write(0);
    _cs = 1;
    
    return data;
}

int write(uint32_t address, uint8_t* data, uint32_t len)
{
    _cs = 0;
    _spi.write(MB85_WRITE);
    if (_address_bits == 24) {
        _spi.write((uint8_t)((address >> 16) & 0xFF));
    }
    _spi.write((uint8_t)((address >>  8) & 0xFF));
    _spi.write((uint8_t)((address >>  0) & 0xFF));
    for (uint32_t i = 0; i < len; i++) {
        _spi.write(*data++);
    }
    _cs = 1;
    
    return 0;
}

int write(uint32_t address, uint8_t data)
{
    _cs = 0;
    _spi.write(MB85_WRITE);
    if (_address_bits == 24) {
        _spi.write((uint8_t)((address >> 16) & 0xFF));
    }
    _spi.write((uint8_t)((address >>  8) & 0xFF));
    _spi.write((uint8_t)((address >>  0) & 0xFF));
    _spi.write(data);
    _cs = 1;
    
    return 0;
}

int fill(uint32_t address, uint8_t data, uint32_t len)
{
    _cs = 0;
    _spi.write(MB85_WRITE);
    if (_address_bits == 24) {
        _spi.write((uint8_t)((address >> 16) & 0xFF));
    }
    _spi.write((uint8_t)((address >>  8) & 0xFF));
    _spi.write((uint8_t)((address >>  0) & 0xFF));
    for (uint32_t i = 0; i < len; i++) {
        _spi.write(data);
    }
    _cs = 1;
    
    return 0;
}

void write_enable()
{
    _cs = 0;
    _spi.write(MB85_WREN);
    _cs = 1;
}

void write_disable()
{
    _cs = 0;
    _spi.write(MB85_WRDI);
    _cs = 1;
}

int main() {
    uint8_t buf[16];
    uint32_t address;

    pc.baud(115200);
    pc.printf("\nFujitsu MB85RSxxx FRAM test program\n\n");
    
    // Initialize SPI
    _spi.format(8, 0);
    _cs = 1;

    // Read device ID and detect memory density for addressing    
    read_device_id(buf);
    if ((buf[2] & 0x1F) > MB85_DENSITY_512K) {
        _address_bits = 24;
    } else {
        _address_bits = 16;
    }
    pc.printf("read device ID = 0x%x 0x%x 0x%x 0x%x\n", buf[0], buf[1], buf[2], buf[3]);
    write_enable();
    pc.printf("read status (WREN) = 0x%x\n", read_status());
    write_disable();
    pc.printf("read status (WRDI) = 0x%x\n", read_status());
    
    // Write 0 data
    write_enable();
    fill(0, 0, 256);

    // Prepare write data
    for (int i = 0; i < 16; i++) {
        buf[i] = i;
    }
    
    // Write data with write enable
    write_enable();
    write(0x00, buf, 16);
    
    // Attempt to write data (not written)
    write(0x10, buf, 16);
    
    // Write data with write enable
    write_enable();
    write(0x20, buf, 16);
    
    // Read data
    for (address = 0; address < 0x80; address += 16) {
        read(address, buf, 16);
        pc.printf("%08X : ", address);
        for (int i = 0; i < 16; i++) {
            pc.printf("%02X ", buf[i]);    
        }
        pc.printf("\n");
    }

    // Write number from 0 to 255
    pc.printf("\n");
    for (address = 0; address < 0x100; address++) {
        write_enable();
        write(address, (uint8_t)address);
    }
    // Read data
    for (address = 0; address < 0x100; address += 16) {
        read(address, buf, 16);
        pc.printf("%08X : ", address);
        for (int i = 0; i < 16; i++) {
            pc.printf("%02X ", buf[i]);    
        }
        pc.printf("\n");
    }

    while(1) {
        myled = 1;
        wait(0.2);
        myled = 0;
        wait(0.2);
    }
}