FRAM SPI Access Library

Dependents:   FRAM_Dump SDFile_Logger

Committer:
APS_Lab
Date:
Thu Jun 22 08:30:47 2017 +0000
Revision:
0:c31c06ec36da
Child:
1:f29f20865693
FRAM Dump;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
APS_Lab 0:c31c06ec36da 1 /**
APS_Lab 0:c31c06ec36da 2 ******************************************************************************
APS_Lab 0:c31c06ec36da 3 * @file MB85RSxx_SPI.cpp
APS_Lab 0:c31c06ec36da 4 * @author APS Lab
APS_Lab 0:c31c06ec36da 5 * @version V1.0.0
APS_Lab 0:c31c06ec36da 6 * @date 16 June 2017
APS_Lab 0:c31c06ec36da 7 * @brief MB85RSxx_SPI class implementation
APS_Lab 0:c31c06ec36da 8 ******************************************************************************
APS_Lab 0:c31c06ec36da 9 * @attention
APS_Lab 0:c31c06ec36da 10 *
APS_Lab 0:c31c06ec36da 11 * Permission is hereby granted, free of charge, to any person obtaining a copy
APS_Lab 0:c31c06ec36da 12 * of this software and associated documentation files (the "Software"), to deal
APS_Lab 0:c31c06ec36da 13 * in the Software without restriction, including without limitation the rights
APS_Lab 0:c31c06ec36da 14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
APS_Lab 0:c31c06ec36da 15 * copies of the Software, and to permit persons to whom the Software is
APS_Lab 0:c31c06ec36da 16 * furnished to do so, subject to the following conditions:
APS_Lab 0:c31c06ec36da 17 *
APS_Lab 0:c31c06ec36da 18 * The above copyright notice and this permission notice shall be included in
APS_Lab 0:c31c06ec36da 19 * all copies or substantial portions of the Software.
APS_Lab 0:c31c06ec36da 20 *
APS_Lab 0:c31c06ec36da 21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
APS_Lab 0:c31c06ec36da 22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
APS_Lab 0:c31c06ec36da 23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
APS_Lab 0:c31c06ec36da 24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
APS_Lab 0:c31c06ec36da 25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
APS_Lab 0:c31c06ec36da 26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
APS_Lab 0:c31c06ec36da 27 * THE SOFTWARE.
APS_Lab 0:c31c06ec36da 28 */
APS_Lab 0:c31c06ec36da 29 #include "mbed.h"
APS_Lab 0:c31c06ec36da 30 #include "MB85RSxx_SPI.h"
APS_Lab 0:c31c06ec36da 31
APS_Lab 0:c31c06ec36da 32 MB85RSxx_SPI::MB85RSxx_SPI(PinName mosi, PinName miso, PinName sclk, PinName nss)
APS_Lab 0:c31c06ec36da 33 :
APS_Lab 0:c31c06ec36da 34 _spi_p(new SPI(mosi, miso, sclk)),
APS_Lab 0:c31c06ec36da 35 _spi(*_spi_p),
APS_Lab 0:c31c06ec36da 36 _di_p(new DigitalOut(nss)),
APS_Lab 0:c31c06ec36da 37 _di(*_di_p)
APS_Lab 0:c31c06ec36da 38 {
APS_Lab 0:c31c06ec36da 39 _spi.format(8,0);
APS_Lab 0:c31c06ec36da 40 _spi.frequency(10000000);
APS_Lab 0:c31c06ec36da 41 }
APS_Lab 0:c31c06ec36da 42
APS_Lab 0:c31c06ec36da 43 MB85RSxx_SPI::~MB85RSxx_SPI()
APS_Lab 0:c31c06ec36da 44 {
APS_Lab 0:c31c06ec36da 45 if (NULL != _spi_p)
APS_Lab 0:c31c06ec36da 46 delete _spi_p;
APS_Lab 0:c31c06ec36da 47 }
APS_Lab 0:c31c06ec36da 48
APS_Lab 0:c31c06ec36da 49 void MB85RSxx_SPI::Init(void)
APS_Lab 0:c31c06ec36da 50 {
APS_Lab 0:c31c06ec36da 51 char sts;
APS_Lab 0:c31c06ec36da 52 _di=0;
APS_Lab 0:c31c06ec36da 53 _spi.write(WRDI);
APS_Lab 0:c31c06ec36da 54 _di=1;
APS_Lab 0:c31c06ec36da 55 wait(0.2);
APS_Lab 0:c31c06ec36da 56 _di=0;
APS_Lab 0:c31c06ec36da 57 _spi.write(WREN);
APS_Lab 0:c31c06ec36da 58 _di=1;
APS_Lab 0:c31c06ec36da 59 wait(0.2);
APS_Lab 0:c31c06ec36da 60 _di=0;
APS_Lab 0:c31c06ec36da 61 _spi.write(WRSR);
APS_Lab 0:c31c06ec36da 62 _spi.write(0x02);
APS_Lab 0:c31c06ec36da 63 _di=1;
APS_Lab 0:c31c06ec36da 64 wait(0.2);
APS_Lab 0:c31c06ec36da 65 _di=0;
APS_Lab 0:c31c06ec36da 66 _spi.write(RDSR);
APS_Lab 0:c31c06ec36da 67 sts=_spi.write(0x00);
APS_Lab 0:c31c06ec36da 68 _di=1;
APS_Lab 0:c31c06ec36da 69 printf("FRAM Status %x\n", sts);
APS_Lab 0:c31c06ec36da 70 }
APS_Lab 0:c31c06ec36da 71
APS_Lab 0:c31c06ec36da 72 void MB85RSxx_SPI::write(uint32_t address, char data)
APS_Lab 0:c31c06ec36da 73 {
APS_Lab 0:c31c06ec36da 74 _di=0;
APS_Lab 0:c31c06ec36da 75 _spi.write(WREN);
APS_Lab 0:c31c06ec36da 76 _di=1;
APS_Lab 0:c31c06ec36da 77 _di=0;
APS_Lab 0:c31c06ec36da 78 _spi.write(WRITE);
APS_Lab 0:c31c06ec36da 79 _spi.write((address & 0x3F00) >> 8);//Addr H
APS_Lab 0:c31c06ec36da 80 _spi.write((address & 0x00FF)); //Addr L
APS_Lab 0:c31c06ec36da 81 _spi.write(data); //Write Data
APS_Lab 0:c31c06ec36da 82 _di=1;
APS_Lab 0:c31c06ec36da 83 }
APS_Lab 0:c31c06ec36da 84
APS_Lab 0:c31c06ec36da 85 char MB85RSxx_SPI::read(uint32_t address)
APS_Lab 0:c31c06ec36da 86 {
APS_Lab 0:c31c06ec36da 87 char ch;
APS_Lab 0:c31c06ec36da 88 _di=0;
APS_Lab 0:c31c06ec36da 89 _spi.write(READ);
APS_Lab 0:c31c06ec36da 90 _spi.write((address & 0x3F00) >> 8);//Addr H
APS_Lab 0:c31c06ec36da 91 _spi.write((address & 0x00FF)); //Addr L
APS_Lab 0:c31c06ec36da 92 ch = _spi.write(0x00);//Write dummy 0x00, Reade Data
APS_Lab 0:c31c06ec36da 93 _di=1;
APS_Lab 0:c31c06ec36da 94 return ch;
APS_Lab 0:c31c06ec36da 95 }
APS_Lab 0:c31c06ec36da 96
APS_Lab 0:c31c06ec36da 97
APS_Lab 0:c31c06ec36da 98 void MB85RSxx_SPI::write(uint32_t address, char *data, uint32_t length)
APS_Lab 0:c31c06ec36da 99 {
APS_Lab 0:c31c06ec36da 100 int idx;
APS_Lab 0:c31c06ec36da 101 for(idx=0; idx < length; idx++)
APS_Lab 0:c31c06ec36da 102 {
APS_Lab 0:c31c06ec36da 103 MB85RSxx_SPI::write(address+idx, *(data+idx));
APS_Lab 0:c31c06ec36da 104 }
APS_Lab 0:c31c06ec36da 105 }
APS_Lab 0:c31c06ec36da 106
APS_Lab 0:c31c06ec36da 107 uint32_t MB85RSxx_SPI::read(uint32_t address, char *data, uint32_t length)
APS_Lab 0:c31c06ec36da 108 {
APS_Lab 0:c31c06ec36da 109 uint32_t error=0;
APS_Lab 0:c31c06ec36da 110 uint32_t idx;
APS_Lab 0:c31c06ec36da 111 for(idx=0; idx < length; idx++)
APS_Lab 0:c31c06ec36da 112 {
APS_Lab 0:c31c06ec36da 113 *(data+idx) = MB85RSxx_SPI::read(address+idx);
APS_Lab 0:c31c06ec36da 114 }
APS_Lab 0:c31c06ec36da 115 return error;
APS_Lab 0:c31c06ec36da 116 }
APS_Lab 0:c31c06ec36da 117