Cypress F-RAM FM25W256 library

Dependents:   Hello-FM25W256 Hello-FM25W256

FM25W256.cpp

Committer:
MACRUM
Date:
2016-03-04
Revision:
0:5a552209903c
Child:
1:bb2b1e4bfb6e

File content as of revision 0:5a552209903c:

#include "mbed.h"
#include "FM25W256.h"

FM25W256::FM25W256(PinName mosi, PinName miso, PinName clk, PinName cs)
    : _spi(mosi, miso, clk), _cs(cs)
{
    _spi.format(8, 0);
    _spi.frequency(20000000);
    _cs = 1;
}

FM25W256::FM25W256(SPI &spi, PinName cs)
    : _spi(spi), _cs(cs) 
{
    _spi.format(8, 0);
    _spi.frequency(20000000);
    _cs = 1;

}

void FM25W256::write(uint16_t address, uint8_t data)
{
    _cs = 0;
    _spi.write(CMD_WREN);
    _cs = 1;

    _cs = 0;
    _spi.write(CMD_WRITE);
    _spi.write(address >> 8);
    _spi.write(address & 0xFF);
    _spi.write(data);
    _cs = 1;
}

void FM25W256::write(uint16_t address, uint8_t *data, uint16_t size)
{
    _cs = 0;
    _spi.write(CMD_WREN);
    _cs = 1;

    _cs = 0;
    _spi.write(CMD_WRITE);
    _spi.write(address >> 8);
    _spi.write(address & 0xFF);
    while(size--) {
        _spi.write(*data++);
    }
    _cs = 1;
}

uint8_t FM25W256::read(uint16_t address)
{
    uint8_t data;

    _cs = 0;
    _spi.write(CMD_READ);
    _spi.write(address >> 8);
    _spi.write(address & 0xFF);
    data = _spi.write(0);
    _cs = 1;

    return data;
}

void FM25W256::read(uint16_t address, uint8_t *buf, uint16_t size)
{
    _cs = 0;
    _spi.write(CMD_READ);
    _spi.write(address >> 8);
    _spi.write(address & 0xFF);
    while (size--) {
        *buf++ = _spi.write(0);
    }
    _cs = 1;
}