x

Dependents:   20180621_FT813

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FRAMSPI.cpp Source File

FRAMSPI.cpp

00001 /* FRAMSPI - Ramtron FRAM FM24VXX series driver
00002 * Copyright (c) 2012 Oleg Evsegneev
00003 * Released under the MIT License: http://mbed.org/license/mit
00004 *
00005 * Datasheet http://www.ramtron.com/files/datasheets/FM25V02_ds.pdf
00006 */
00007  
00008 #include "mbed.h"
00009 #include "FRAMSPI.h"
00010  
00011 FRAMSPI::FRAMSPI(SPI& spi, PinName ncs) : _spi(spi), _ncs(ncs) {
00012     deselect();
00013 }
00014  
00015 void FRAMSPI::select() {
00016     _ncs = 0;
00017 }
00018  
00019 void FRAMSPI::deselect() {
00020     _ncs = 1;
00021 }
00022  
00023 void FRAMSPI::readID( char *buff ) {
00024     select();
00025     _spi.write(READ_SN);
00026     for( int i=0; i<8; i++ )
00027         buff[i] = (char) _spi.write(0);
00028     deselect();
00029 }
00030  
00031 void FRAMSPI::readSN( char *buff ) {
00032     select();
00033     _spi.write(READ_SN);
00034     for( int i=0; i<2; i++ )
00035         buff[i] = (char) _spi.write(0);
00036     deselect();
00037 }
00038  
00039 void FRAMSPI::prepareCommand(char cmd, int addr) {
00040     if( cmd == WRITE ){
00041         select();
00042         _spi.write(WRITE_ENABLED);
00043         deselect();
00044     }
00045     select();
00046     _spi.write(cmd);
00047     _spi.write(addr >> 8);
00048     _spi.write(addr & 0xFF);
00049 }
00050  
00051 // write
00052 void FRAMSPI::write(int addr, char byte) {
00053     prepareCommand(WRITE, addr);
00054     _spi.write(byte);
00055     deselect();
00056 }
00057  
00058 void FRAMSPI::write(int addr, char *buff, int cnt) {
00059     prepareCommand(WRITE, addr);
00060     for (int i = 0; i < cnt; i++) {
00061         _spi.write(buff[i]);
00062     }
00063     deselect();
00064 }
00065  
00066 // read
00067 char FRAMSPI::read(int addr) {
00068     char res;
00069     prepareCommand(READ, addr);
00070     res = _spi.write(0);
00071     deselect();
00072     return res;
00073 }
00074  
00075 void FRAMSPI::read(int addr, char * buff, int cnt) {
00076     prepareCommand(READ, addr);
00077     for (int i = 0; i < cnt; i++) {
00078         buff[i] = _spi.write(0);
00079     }
00080     deselect();
00081 }