FRAM SPI Access Library

Dependents:   FRAM_Dump SDFile_Logger

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MB85RSxx_SPI.h Source File

MB85RSxx_SPI.h

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    MB85RSxx_SPI.h
00004  * @author  APS Lab
00005  * @version V1.0.0
00006  * @date    26 June 2017
00007  * @brief   This file contains the class of an MB85RSxx FRAM library with SPI interface
00008  ******************************************************************************
00009  * @attention
00010  *
00011  * Permission is hereby granted, free of charge, to any person obtaining a copy
00012  * of this software and associated documentation files (the "Software"), to deal
00013  * in the Software without restriction, including without limitation the rights
00014  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00015  * copies of the Software, and to permit persons to whom the Software is
00016  * furnished to do so, subject to the following conditions:
00017  *
00018  * The above copyright notice and this permission notice shall be included in
00019  * all copies or substantial portions of the Software.
00020  *
00021  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00022  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00023  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00024  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00025  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00026  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00027  * THE SOFTWARE.
00028  */
00029  
00030 #ifndef MBED_MB85RSxx_SPI_H
00031 #define MBED_MB85RSxx_SPI_H
00032 
00033 #include "mbed.h"
00034  
00035 #define WREN    0x06
00036 #define WRDI    0x04
00037 #define RDSR    0x05
00038 #define WRSR    0x01
00039 #define READ    0x03
00040 #define WRITE   0x02
00041 
00042 #define SR_WPEN 0x80
00043 #define SR_BP0  0x08
00044 #define SR_BP1  0x04
00045 #define SR_WEL  0x02
00046 
00047 /**  Interface for accessing Fujitsu MB85RSxx FRAM
00048  *
00049  * @code
00050  * #include "mbed.h"
00051  * #include "MB85RSxx_SPI.h"
00052  * 
00053  * Serial pc(USBTX, USBRX);
00054  * 
00055  * #if defined(TARGET_STM32F401RE)
00056  * MB85RSxx_SPI fram(D11, D12, D13, D10);
00057  * #else
00058  * MB85RSxx_SPI fram(D5, D6, D7, D8);
00059  * #endif
00060  * 
00061  * int main()
00062  * {
00063  *     char buf[16];
00064  *     uint32_t address;
00065  *     
00066  *     fram.fill(0, 0, 256);
00067  * 
00068  *     for (int i = 0; i < 16; i++) {
00069  *         buf[i] = i;
00070  *     }
00071  *     fram.write(0, buf, 16);
00072  * 
00073  *     for (address = 0; address < 0x80; address += 16) {
00074  *         fram.read(address, buf, 16);
00075  *         pc.printf("%08X : ", address);
00076  *         for (int i = 0; i < 16; i++) {
00077  *             pc.printf("%02X ", buf[i]);    
00078  *         }
00079  *         pc.printf("\n");
00080  *     }
00081  * }
00082  * 
00083  * @endcode
00084  */
00085 
00086 /** MB85RSxx_SPI class
00087  *
00088  *  MB85RSxx_SPI: A library to access Fujitsu MB85RSxx_SPI FRAM
00089  *
00090  */ 
00091 class MB85RSxx_SPI
00092 {
00093 public:
00094 
00095     /** Create an MB85RSxx_SPI instance
00096      *  which is connected to specified I2C pins with specified address
00097      *
00098      * @param mosi SPI master out pin
00099      * @param miso SPI master in pin
00100      * @param sclk SPI clock pin
00101      * @param nss  SPI node select pin
00102      */
00103     MB85RSxx_SPI(PinName mosi, PinName miso, PinName sclk, PinName nss);
00104 
00105     /** Destructor of MB85RCxx_I2C
00106      */
00107     virtual ~MB85RSxx_SPI();
00108 
00109     
00110     /** Initialize FRAM **/
00111     void Init(void);
00112 
00113     /** Write data to memory address
00114      *
00115      * @param address Memory address
00116      * @param data data to write out to memory
00117      *
00118      */
00119     void write(uint32_t address, char data);
00120     
00121     /** Read data from memory address 
00122      *
00123      * @param FRAM address
00124      * return data
00125      */
00126     char read(uint32_t addr);
00127 
00128     /** Write data to memory address
00129      *
00130      * @param address Memory address
00131      * @param data Pointer to the byte-array data to write
00132      * @param length Number of bytes to write
00133      *
00134      */
00135     void write(uint32_t address, char *data, uint32_t length);
00136     
00137     /** Read data from memory address 
00138      *
00139      * @param FRAM address
00140      * @param FRAM data Pointer to the byte-array to read data in to
00141      * @prama length Number of bytes to read
00142      */
00143     void read(uint32_t address, char *data, uint32_t length);
00144 
00145 
00146 private:
00147     SPI         *_spi_p;
00148     SPI         &_spi;
00149     DigitalOut  *_di_p;
00150     DigitalOut  &_di;
00151 };
00152 
00153 #endif