Cypress F-RAM FM25W256 library
Dependents: Hello-FM25W256 Hello-FM25W256
Diff: FM25W256.cpp
- Revision:
- 0:5a552209903c
- Child:
- 1:bb2b1e4bfb6e
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/FM25W256.cpp Fri Mar 04 15:49:51 2016 +0000 @@ -0,0 +1,75 @@ +#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; +}