Cypress F-RAM FM25W256 library
Dependents: Hello-FM25W256 Hello-FM25W256
FM25W256.h
00001 /* Cypress FM25W256 F-RAM component library 00002 * 00003 * Copyright (c) 2016 ARM Limited 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 * * 00017 * @author Toyomasa Watarai 00018 * @version 1.0 00019 * @date 5-March-2016 00020 * 00021 * http://www.cypress.com/products/nonvolatile-ram 00022 * http://www.cypress.com/documentation/datasheets/fm25w256-256-kbit-32-k-8-serial-spi-f-ram?source=search&keywords=FM25W256&cat=technical_documents 00023 * 00024 */ 00025 00026 #include "mbed.h" 00027 00028 #ifndef __FM25W256_HEAD__ 00029 #define __FM25W256_HEAD__ 00030 00031 #define CMD_WREN 0x06 00032 #define CMD_WRDI 0x04 00033 #define CMD_RDSR 0x05 00034 #define CMD_WRSR 0x01 00035 #define CMD_READ 0x03 00036 #define CMD_WRITE 0x02 00037 00038 #define FM25W256_CLK (20000000) // SPI clock 20MHz 00039 00040 /** An interface for the Cypress 32k byte FM25W256 F-RAM over SPI 00041 * 00042 * @code 00043 * #include "mbed.h" 00044 * #include "FM25W256.h" 00045 * 00046 * Serial pc(USBTX, USBRX); 00047 * FM25W256 f_ram(dp2, dp1, dp6, dp18); 00048 * 00049 * int main() 00050 * { 00051 * uint8_t buf[16]; 00052 * // Fill buffer 00053 * for(int i=0; i<16; i++) { 00054 * buf[i] = i; 00055 * } 00056 * // Write data to F-RAM 00057 * f_ram.write(0, buf, 16); 00058 * 00059 * // Read data from F-RAM 00060 * uint16_t adrs = 0; 00061 * for(int i=0; i<16; i++) { 00062 * pc.printf("0x%04X : ", i * 16); 00063 * for(int j=0; j<16; j++) { 00064 * pc.printf("%02X ", f_ram.read(adrs++)); 00065 * } 00066 * pc.printf("\n"); 00067 * } 00068 * 00069 * while(1) { 00070 * } 00071 * } 00072 * @endcode 00073 */ 00074 class FM25W256 { 00075 public: 00076 00077 enum E_WP { 00078 BANK_NONE = 0, 00079 BANK_UPPER_QUARTER = 1, 00080 BANK_UPPER_HALF = 2, 00081 BANK_ALL = 3 00082 }; 00083 00084 /** Create an interface 00085 * 00086 * @param mosi SPI master-out-slave-in 00087 * @param miso SPI master-in-slave-out 00088 * @param clk SPI clock 00089 * @param cs chip select pin - any free Digital pin will do 00090 */ 00091 FM25W256(PinName mosi, PinName miso, PinName clk, PinName cs); 00092 00093 /** Create an interface 00094 * 00095 * @param &spi SPI instance 00096 * @param cs chip select pin - any free Digital pin will do 00097 */ 00098 FM25W256(SPI &spi, PinName cs); 00099 00100 /** write a byte to F-RAM 00101 * @param address The address F-RAM to write to 00102 * @param data The byte to write there 00103 */ 00104 void write(uint16_t address, uint8_t data); 00105 00106 /** write multiple bytes to F-RAM from a buffer 00107 * @param address The F-RAM address write to 00108 * @param data The buffer to write from 00109 * @param size The number of bytes to write 00110 */ 00111 void write(uint16_t address, uint8_t *data, uint16_t size); 00112 00113 /** read a byte from F-RAM 00114 * @param address The address to read from 00115 * @return the character at that address 00116 */ 00117 uint8_t read(uint16_t address); 00118 00119 /** read multiple bytes from F-RAM into a buffer 00120 * @param address The F-RAM address to read from 00121 * @param data The buffer to read into (must be big enough!) 00122 * @param size The number of bytes to read 00123 */ 00124 void read(uint16_t address, uint8_t *data, uint16_t size); 00125 00126 /** write a byte to the status register 00127 * @param data The byte to write the register 00128 */ 00129 void wirte_status(uint8_t data); 00130 00131 /** read a byte from the status register 00132 * @return the character at the register 00133 */ 00134 uint8_t read_status(); 00135 00136 /** Set write protect mode 00137 * @param bp E_WP enum value 00138 */ 00139 void set_write_protect(E_WP bp); 00140 00141 /** Set write protect to non-protect mode 00142 */ 00143 void clear_write_protect(); 00144 00145 protected: 00146 SPI _spi; 00147 DigitalOut _cs; 00148 }; 00149 00150 #endif
Generated on Wed Jul 13 2022 07:21:54 by 1.7.2