x
FRAMSPI.cpp@0:0411eff88bac, 2018-07-23 (annotated)
- Committer:
- JackB
- Date:
- Mon Jul 23 12:23:33 2018 +0000
- Revision:
- 0:0411eff88bac
FRAM
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JackB | 0:0411eff88bac | 1 | /* FRAMSPI - Ramtron FRAM FM24VXX series driver |
JackB | 0:0411eff88bac | 2 | * Copyright (c) 2012 Oleg Evsegneev |
JackB | 0:0411eff88bac | 3 | * Released under the MIT License: http://mbed.org/license/mit |
JackB | 0:0411eff88bac | 4 | * |
JackB | 0:0411eff88bac | 5 | * Datasheet http://www.ramtron.com/files/datasheets/FM25V02_ds.pdf |
JackB | 0:0411eff88bac | 6 | */ |
JackB | 0:0411eff88bac | 7 | |
JackB | 0:0411eff88bac | 8 | #include "mbed.h" |
JackB | 0:0411eff88bac | 9 | #include "FRAMSPI.h" |
JackB | 0:0411eff88bac | 10 | |
JackB | 0:0411eff88bac | 11 | FRAMSPI::FRAMSPI(SPI& spi, PinName ncs) : _spi(spi), _ncs(ncs) { |
JackB | 0:0411eff88bac | 12 | deselect(); |
JackB | 0:0411eff88bac | 13 | } |
JackB | 0:0411eff88bac | 14 | |
JackB | 0:0411eff88bac | 15 | void FRAMSPI::select() { |
JackB | 0:0411eff88bac | 16 | _ncs = 0; |
JackB | 0:0411eff88bac | 17 | } |
JackB | 0:0411eff88bac | 18 | |
JackB | 0:0411eff88bac | 19 | void FRAMSPI::deselect() { |
JackB | 0:0411eff88bac | 20 | _ncs = 1; |
JackB | 0:0411eff88bac | 21 | } |
JackB | 0:0411eff88bac | 22 | |
JackB | 0:0411eff88bac | 23 | void FRAMSPI::readID( char *buff ) { |
JackB | 0:0411eff88bac | 24 | select(); |
JackB | 0:0411eff88bac | 25 | _spi.write(READ_SN); |
JackB | 0:0411eff88bac | 26 | for( int i=0; i<8; i++ ) |
JackB | 0:0411eff88bac | 27 | buff[i] = (char) _spi.write(0); |
JackB | 0:0411eff88bac | 28 | deselect(); |
JackB | 0:0411eff88bac | 29 | } |
JackB | 0:0411eff88bac | 30 | |
JackB | 0:0411eff88bac | 31 | void FRAMSPI::readSN( char *buff ) { |
JackB | 0:0411eff88bac | 32 | select(); |
JackB | 0:0411eff88bac | 33 | _spi.write(READ_SN); |
JackB | 0:0411eff88bac | 34 | for( int i=0; i<2; i++ ) |
JackB | 0:0411eff88bac | 35 | buff[i] = (char) _spi.write(0); |
JackB | 0:0411eff88bac | 36 | deselect(); |
JackB | 0:0411eff88bac | 37 | } |
JackB | 0:0411eff88bac | 38 | |
JackB | 0:0411eff88bac | 39 | void FRAMSPI::prepareCommand(char cmd, int addr) { |
JackB | 0:0411eff88bac | 40 | if( cmd == WRITE ){ |
JackB | 0:0411eff88bac | 41 | select(); |
JackB | 0:0411eff88bac | 42 | _spi.write(WRITE_ENABLED); |
JackB | 0:0411eff88bac | 43 | deselect(); |
JackB | 0:0411eff88bac | 44 | } |
JackB | 0:0411eff88bac | 45 | select(); |
JackB | 0:0411eff88bac | 46 | _spi.write(cmd); |
JackB | 0:0411eff88bac | 47 | _spi.write(addr >> 8); |
JackB | 0:0411eff88bac | 48 | _spi.write(addr & 0xFF); |
JackB | 0:0411eff88bac | 49 | } |
JackB | 0:0411eff88bac | 50 | |
JackB | 0:0411eff88bac | 51 | // write |
JackB | 0:0411eff88bac | 52 | void FRAMSPI::write(int addr, char byte) { |
JackB | 0:0411eff88bac | 53 | prepareCommand(WRITE, addr); |
JackB | 0:0411eff88bac | 54 | _spi.write(byte); |
JackB | 0:0411eff88bac | 55 | deselect(); |
JackB | 0:0411eff88bac | 56 | } |
JackB | 0:0411eff88bac | 57 | |
JackB | 0:0411eff88bac | 58 | void FRAMSPI::write(int addr, char *buff, int cnt) { |
JackB | 0:0411eff88bac | 59 | prepareCommand(WRITE, addr); |
JackB | 0:0411eff88bac | 60 | for (int i = 0; i < cnt; i++) { |
JackB | 0:0411eff88bac | 61 | _spi.write(buff[i]); |
JackB | 0:0411eff88bac | 62 | } |
JackB | 0:0411eff88bac | 63 | deselect(); |
JackB | 0:0411eff88bac | 64 | } |
JackB | 0:0411eff88bac | 65 | |
JackB | 0:0411eff88bac | 66 | // read |
JackB | 0:0411eff88bac | 67 | char FRAMSPI::read(int addr) { |
JackB | 0:0411eff88bac | 68 | char res; |
JackB | 0:0411eff88bac | 69 | prepareCommand(READ, addr); |
JackB | 0:0411eff88bac | 70 | res = _spi.write(0); |
JackB | 0:0411eff88bac | 71 | deselect(); |
JackB | 0:0411eff88bac | 72 | return res; |
JackB | 0:0411eff88bac | 73 | } |
JackB | 0:0411eff88bac | 74 | |
JackB | 0:0411eff88bac | 75 | void FRAMSPI::read(int addr, char * buff, int cnt) { |
JackB | 0:0411eff88bac | 76 | prepareCommand(READ, addr); |
JackB | 0:0411eff88bac | 77 | for (int i = 0; i < cnt; i++) { |
JackB | 0:0411eff88bac | 78 | buff[i] = _spi.write(0); |
JackB | 0:0411eff88bac | 79 | } |
JackB | 0:0411eff88bac | 80 | deselect(); |
JackB | 0:0411eff88bac | 81 | } |