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.h
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 This file contains the class of an MB85RSxx FRAM library with SPI interface
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
APS_Lab 0:c31c06ec36da 30 #ifndef MBED_MB85RSxx_SPI_H
APS_Lab 0:c31c06ec36da 31 #define MBED_MB85RSxx_SPI_H
APS_Lab 0:c31c06ec36da 32
APS_Lab 0:c31c06ec36da 33 #include "mbed.h"
APS_Lab 0:c31c06ec36da 34
APS_Lab 0:c31c06ec36da 35 #define MB85_DENSITY_64K 0x3
APS_Lab 0:c31c06ec36da 36 #define MB85_DENSITY_256K 0x5
APS_Lab 0:c31c06ec36da 37 #define MB85_DENSITY_512K 0x6
APS_Lab 0:c31c06ec36da 38 #define MB85_DENSITY_1M 0x7
APS_Lab 0:c31c06ec36da 39 #define MB85_DENSITY_2M 0x8
APS_Lab 0:c31c06ec36da 40 #define MB85RC_SPI_ADDRESS 0xA0
APS_Lab 0:c31c06ec36da 41 #define SPI_WRITE 0
APS_Lab 0:c31c06ec36da 42 #define SPI_READ 1
APS_Lab 0:c31c06ec36da 43
APS_Lab 0:c31c06ec36da 44 #define WREN 0x06
APS_Lab 0:c31c06ec36da 45 #define WRDI 0x04
APS_Lab 0:c31c06ec36da 46 #define RDSR 0x05
APS_Lab 0:c31c06ec36da 47 #define WRSR 0x01
APS_Lab 0:c31c06ec36da 48 #define READ 0x03
APS_Lab 0:c31c06ec36da 49 #define WRITE 0x02
APS_Lab 0:c31c06ec36da 50
APS_Lab 0:c31c06ec36da 51 /** Interface for accessing Fujitsu MB85RSxx FRAM
APS_Lab 0:c31c06ec36da 52 *
APS_Lab 0:c31c06ec36da 53 * @code
APS_Lab 0:c31c06ec36da 54 * #include "mbed.h"
APS_Lab 0:c31c06ec36da 55 * #include "MB85RSxx_SPI.h"
APS_Lab 0:c31c06ec36da 56 *
APS_Lab 0:c31c06ec36da 57 * Serial pc(USBTX, USBRX);
APS_Lab 0:c31c06ec36da 58 *
APS_Lab 0:c31c06ec36da 59 * #if defined(TARGET_STM32F401RE)
APS_Lab 0:c31c06ec36da 60 * MB85RSxx_SPI fram(D11, D12, D13, D10);
APS_Lab 0:c31c06ec36da 61 * #else
APS_Lab 0:c31c06ec36da 62 * MB85RSxx_SPI fram(D5, D6, D7, D8);
APS_Lab 0:c31c06ec36da 63 * #endif
APS_Lab 0:c31c06ec36da 64 *
APS_Lab 0:c31c06ec36da 65 * int main()
APS_Lab 0:c31c06ec36da 66 * {
APS_Lab 0:c31c06ec36da 67 * char buf[16];
APS_Lab 0:c31c06ec36da 68 * uint32_t address;
APS_Lab 0:c31c06ec36da 69 *
APS_Lab 0:c31c06ec36da 70 * fram.fill(0, 0, 256);
APS_Lab 0:c31c06ec36da 71 *
APS_Lab 0:c31c06ec36da 72 * for (int i = 0; i < 16; i++) {
APS_Lab 0:c31c06ec36da 73 * buf[i] = i;
APS_Lab 0:c31c06ec36da 74 * }
APS_Lab 0:c31c06ec36da 75 * fram.write(0, buf, 16);
APS_Lab 0:c31c06ec36da 76 *
APS_Lab 0:c31c06ec36da 77 * for (address = 0; address < 0x80; address += 16) {
APS_Lab 0:c31c06ec36da 78 * fram.read(address, buf, 16);
APS_Lab 0:c31c06ec36da 79 * pc.printf("%08X : ", address);
APS_Lab 0:c31c06ec36da 80 * for (int i = 0; i < 16; i++) {
APS_Lab 0:c31c06ec36da 81 * pc.printf("%02X ", buf[i]);
APS_Lab 0:c31c06ec36da 82 * }
APS_Lab 0:c31c06ec36da 83 * pc.printf("\n");
APS_Lab 0:c31c06ec36da 84 * }
APS_Lab 0:c31c06ec36da 85 * }
APS_Lab 0:c31c06ec36da 86 *
APS_Lab 0:c31c06ec36da 87 * @endcode
APS_Lab 0:c31c06ec36da 88 */
APS_Lab 0:c31c06ec36da 89
APS_Lab 0:c31c06ec36da 90 /** MB85RSxx_SPI class
APS_Lab 0:c31c06ec36da 91 *
APS_Lab 0:c31c06ec36da 92 * MB85RSxx_SPI: A library to access Fujitsu MB85RSxx_SPI FRAM
APS_Lab 0:c31c06ec36da 93 *
APS_Lab 0:c31c06ec36da 94 */
APS_Lab 0:c31c06ec36da 95 class MB85RSxx_SPI
APS_Lab 0:c31c06ec36da 96 {
APS_Lab 0:c31c06ec36da 97 public:
APS_Lab 0:c31c06ec36da 98
APS_Lab 0:c31c06ec36da 99 /** Create an MB85RSxx_SPI instance
APS_Lab 0:c31c06ec36da 100 * which is connected to specified I2C pins with specified address
APS_Lab 0:c31c06ec36da 101 *
APS_Lab 0:c31c06ec36da 102 * @param mosi SPI master out pin
APS_Lab 0:c31c06ec36da 103 * @param miso SPI master in pin
APS_Lab 0:c31c06ec36da 104 * @param sclk SPI clock pin
APS_Lab 0:c31c06ec36da 105 * @param nss SPI node select pin
APS_Lab 0:c31c06ec36da 106 */
APS_Lab 0:c31c06ec36da 107 MB85RSxx_SPI(PinName mosi, PinName miso, PinName sclk, PinName nss);
APS_Lab 0:c31c06ec36da 108
APS_Lab 0:c31c06ec36da 109 /** Destructor of MB85RCxx_I2C
APS_Lab 0:c31c06ec36da 110 */
APS_Lab 0:c31c06ec36da 111 virtual ~MB85RSxx_SPI();
APS_Lab 0:c31c06ec36da 112
APS_Lab 0:c31c06ec36da 113
APS_Lab 0:c31c06ec36da 114 /** Initialize FRAM **/
APS_Lab 0:c31c06ec36da 115 void Init(void);
APS_Lab 0:c31c06ec36da 116
APS_Lab 0:c31c06ec36da 117 /** Write data to memory address
APS_Lab 0:c31c06ec36da 118 *
APS_Lab 0:c31c06ec36da 119 * @param address Memory address
APS_Lab 0:c31c06ec36da 120 * @param data data to write out to memory
APS_Lab 0:c31c06ec36da 121 *
APS_Lab 0:c31c06ec36da 122 */
APS_Lab 0:c31c06ec36da 123 void write(uint32_t address, char data);
APS_Lab 0:c31c06ec36da 124
APS_Lab 0:c31c06ec36da 125 /** Read data from memory address
APS_Lab 0:c31c06ec36da 126 *
APS_Lab 0:c31c06ec36da 127 * @param FRAM address
APS_Lab 0:c31c06ec36da 128 * return data
APS_Lab 0:c31c06ec36da 129 */
APS_Lab 0:c31c06ec36da 130 char read(uint32_t addr);
APS_Lab 0:c31c06ec36da 131
APS_Lab 0:c31c06ec36da 132 /** Write data to memory address
APS_Lab 0:c31c06ec36da 133 *
APS_Lab 0:c31c06ec36da 134 * @param address Memory address
APS_Lab 0:c31c06ec36da 135 * @param data Pointer to the byte-array data to write
APS_Lab 0:c31c06ec36da 136 * @param length Number of bytes to write
APS_Lab 0:c31c06ec36da 137 *
APS_Lab 0:c31c06ec36da 138 */
APS_Lab 0:c31c06ec36da 139 void write(uint32_t address, char *data, uint32_t length);
APS_Lab 0:c31c06ec36da 140
APS_Lab 0:c31c06ec36da 141 /** Read data from memory address
APS_Lab 0:c31c06ec36da 142 *
APS_Lab 0:c31c06ec36da 143 * @param FRAM address
APS_Lab 0:c31c06ec36da 144 * @param FRAM data Pointer to the byte-array to read data in to
APS_Lab 0:c31c06ec36da 145 * @prama length Number of bytes to read
APS_Lab 0:c31c06ec36da 146 * return 0:success, 1:pointer error
APS_Lab 0:c31c06ec36da 147 */
APS_Lab 0:c31c06ec36da 148 uint32_t read(uint32_t address, char *data, uint32_t length);
APS_Lab 0:c31c06ec36da 149
APS_Lab 0:c31c06ec36da 150
APS_Lab 0:c31c06ec36da 151 private:
APS_Lab 0:c31c06ec36da 152 SPI *_spi_p;
APS_Lab 0:c31c06ec36da 153 SPI &_spi;
APS_Lab 0:c31c06ec36da 154 DigitalOut *_di_p;
APS_Lab 0:c31c06ec36da 155 DigitalOut &_di;
APS_Lab 0:c31c06ec36da 156 };
APS_Lab 0:c31c06ec36da 157
APS_Lab 0:c31c06ec36da 158 #endif