FRAM SPI Access Library

Dependents:   FRAM_Dump SDFile_Logger

Committer:
APS_Lab
Date:
Fri Aug 18 05:27:05 2017 +0000
Revision:
2:9a0f9faada7f
Parent:
0:c31c06ec36da
No change

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