Cypress F-RAM FM25W256 library
Dependents: Hello-FM25W256 Hello-FM25W256
FM25W256.h@2:4939ff09bf12, 2016-03-05 (annotated)
- Committer:
- MACRUM
- Date:
- Sat Mar 05 14:12:08 2016 +0000
- Revision:
- 2:4939ff09bf12
- Parent:
- 1:bb2b1e4bfb6e
- Child:
- 3:aa1c1f07c942
Fix comment
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MACRUM | 1:bb2b1e4bfb6e | 1 | /* Cypress FM25W256 F-RAM component library |
MACRUM | 1:bb2b1e4bfb6e | 2 | * |
MACRUM | 1:bb2b1e4bfb6e | 3 | * Copyright (c) 2016 ARM Limited |
MACRUM | 1:bb2b1e4bfb6e | 4 | * |
MACRUM | 1:bb2b1e4bfb6e | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
MACRUM | 1:bb2b1e4bfb6e | 6 | * you may not use this file except in compliance with the License. |
MACRUM | 1:bb2b1e4bfb6e | 7 | * You may obtain a copy of the License at |
MACRUM | 1:bb2b1e4bfb6e | 8 | * |
MACRUM | 1:bb2b1e4bfb6e | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
MACRUM | 1:bb2b1e4bfb6e | 10 | * |
MACRUM | 1:bb2b1e4bfb6e | 11 | * Unless required by applicable law or agreed to in writing, software |
MACRUM | 1:bb2b1e4bfb6e | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
MACRUM | 1:bb2b1e4bfb6e | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
MACRUM | 1:bb2b1e4bfb6e | 14 | * See the License for the specific language governing permissions and |
MACRUM | 1:bb2b1e4bfb6e | 15 | * limitations under the License. |
MACRUM | 1:bb2b1e4bfb6e | 16 | * * |
MACRUM | 1:bb2b1e4bfb6e | 17 | * @author Toyomasa Watarai |
MACRUM | 1:bb2b1e4bfb6e | 18 | * @version 1.0 |
MACRUM | 1:bb2b1e4bfb6e | 19 | * @date 5-March-2016 |
MACRUM | 1:bb2b1e4bfb6e | 20 | * |
MACRUM | 1:bb2b1e4bfb6e | 21 | * http://www.cypress.com/products/nonvolatile-ram |
MACRUM | 1:bb2b1e4bfb6e | 22 | * http://www.cypress.com/documentation/datasheets/fm25w256-256-kbit-32-k-8-serial-spi-f-ram?source=search&keywords=FM25W256&cat=technical_documents |
MACRUM | 1:bb2b1e4bfb6e | 23 | * |
MACRUM | 1:bb2b1e4bfb6e | 24 | */ |
MACRUM | 1:bb2b1e4bfb6e | 25 | |
MACRUM | 0:5a552209903c | 26 | #include "mbed.h" |
MACRUM | 0:5a552209903c | 27 | |
MACRUM | 0:5a552209903c | 28 | #ifndef __FM25W256_HEAD__ |
MACRUM | 0:5a552209903c | 29 | #define __FM25W256_HEAD__ |
MACRUM | 0:5a552209903c | 30 | |
MACRUM | 0:5a552209903c | 31 | #define CMD_WREN 0x06 |
MACRUM | 0:5a552209903c | 32 | #define CMD_WRDI 0x04 |
MACRUM | 0:5a552209903c | 33 | #define CMD_RDSR 0x05 |
MACRUM | 0:5a552209903c | 34 | #define CMD_WRSR 0x01 |
MACRUM | 0:5a552209903c | 35 | #define CMD_READ 0x03 |
MACRUM | 0:5a552209903c | 36 | #define CMD_WRITE 0x02 |
MACRUM | 0:5a552209903c | 37 | |
MACRUM | 1:bb2b1e4bfb6e | 38 | #define FM25W256_CLK (20000000) // SPI clock 20MHz |
MACRUM | 1:bb2b1e4bfb6e | 39 | |
MACRUM | 1:bb2b1e4bfb6e | 40 | /** An interface for the Cypress 32k byte FM25W256 F-RAM over SPI |
MACRUM | 2:4939ff09bf12 | 41 | * |
MACRUM | 2:4939ff09bf12 | 42 | * @code |
MACRUM | 2:4939ff09bf12 | 43 | * #include "mbed.h" |
MACRUM | 2:4939ff09bf12 | 44 | * #include "FM25W256.h" |
MACRUM | 2:4939ff09bf12 | 45 | * |
MACRUM | 2:4939ff09bf12 | 46 | * Serial pc(USBTX, USBRX); |
MACRUM | 2:4939ff09bf12 | 47 | * FM25W256 f_ram(dp2, dp1, dp6, dp18); |
MACRUM | 2:4939ff09bf12 | 48 | * |
MACRUM | 2:4939ff09bf12 | 49 | * int main() |
MACRUM | 2:4939ff09bf12 | 50 | * { |
MACRUM | 2:4939ff09bf12 | 51 | * uint8_t buf[16]; |
MACRUM | 2:4939ff09bf12 | 52 | * // Fill buffer |
MACRUM | 2:4939ff09bf12 | 53 | * for(int i=0; i<16; i++) { |
MACRUM | 2:4939ff09bf12 | 54 | * buf[i] = i; |
MACRUM | 2:4939ff09bf12 | 55 | * } |
MACRUM | 2:4939ff09bf12 | 56 | * // Write data to F-RAM |
MACRUM | 2:4939ff09bf12 | 57 | * f_ram.write(0, buf, 16); |
MACRUM | 2:4939ff09bf12 | 58 | * |
MACRUM | 2:4939ff09bf12 | 59 | * // Read data from F-RAM |
MACRUM | 2:4939ff09bf12 | 60 | * uint16_t adrs = 0; |
MACRUM | 2:4939ff09bf12 | 61 | * for(int i=0; i<16; i++) { |
MACRUM | 2:4939ff09bf12 | 62 | * pc.printf("0x%04X : ", i * 16); |
MACRUM | 2:4939ff09bf12 | 63 | * for(int j=0; j<16; j++) { |
MACRUM | 2:4939ff09bf12 | 64 | * pc.printf("%02X ", f_ram.read(adrs++)); |
MACRUM | 2:4939ff09bf12 | 65 | * } |
MACRUM | 2:4939ff09bf12 | 66 | * pc.printf("\n"); |
MACRUM | 2:4939ff09bf12 | 67 | * } |
MACRUM | 2:4939ff09bf12 | 68 | * |
MACRUM | 2:4939ff09bf12 | 69 | * while(1) { |
MACRUM | 2:4939ff09bf12 | 70 | * } |
MACRUM | 2:4939ff09bf12 | 71 | * } |
MACRUM | 2:4939ff09bf12 | 72 | */ |
MACRUM | 0:5a552209903c | 73 | class FM25W256 { |
MACRUM | 0:5a552209903c | 74 | public: |
MACRUM | 1:bb2b1e4bfb6e | 75 | |
MACRUM | 1:bb2b1e4bfb6e | 76 | enum E_WP { |
MACRUM | 1:bb2b1e4bfb6e | 77 | BANK_NONE = 0, |
MACRUM | 1:bb2b1e4bfb6e | 78 | BANK_UPPER_QUARTER = 1, |
MACRUM | 1:bb2b1e4bfb6e | 79 | BANK_UPPER_HALF = 2, |
MACRUM | 1:bb2b1e4bfb6e | 80 | BANK_ALL = 3 |
MACRUM | 1:bb2b1e4bfb6e | 81 | }; |
MACRUM | 1:bb2b1e4bfb6e | 82 | |
MACRUM | 1:bb2b1e4bfb6e | 83 | /** Create an interface |
MACRUM | 2:4939ff09bf12 | 84 | * |
MACRUM | 2:4939ff09bf12 | 85 | * @param mosi SPI master-out-slave-in |
MACRUM | 2:4939ff09bf12 | 86 | * @param miso SPI master-in-slave-out |
MACRUM | 2:4939ff09bf12 | 87 | * @param clk SPI clock |
MACRUM | 2:4939ff09bf12 | 88 | * @param cs chip select pin - any free Digital pin will do |
MACRUM | 2:4939ff09bf12 | 89 | */ |
MACRUM | 0:5a552209903c | 90 | FM25W256(PinName mosi, PinName miso, PinName clk, PinName cs); |
MACRUM | 1:bb2b1e4bfb6e | 91 | |
MACRUM | 1:bb2b1e4bfb6e | 92 | /** Create an interface |
MACRUM | 2:4939ff09bf12 | 93 | * |
MACRUM | 2:4939ff09bf12 | 94 | * @param &spi SPI instance |
MACRUM | 2:4939ff09bf12 | 95 | * @param cs chip select pin - any free Digital pin will do |
MACRUM | 2:4939ff09bf12 | 96 | */ |
MACRUM | 0:5a552209903c | 97 | FM25W256(SPI &spi, PinName cs); |
MACRUM | 1:bb2b1e4bfb6e | 98 | |
MACRUM | 1:bb2b1e4bfb6e | 99 | /** write a byte to F-RAM |
MACRUM | 2:4939ff09bf12 | 100 | * @param address The address F-RAM to write to |
MACRUM | 2:4939ff09bf12 | 101 | * @param data The byte to write there |
MACRUM | 2:4939ff09bf12 | 102 | */ |
MACRUM | 0:5a552209903c | 103 | void write(uint16_t address, uint8_t data); |
MACRUM | 1:bb2b1e4bfb6e | 104 | |
MACRUM | 1:bb2b1e4bfb6e | 105 | /** write multiple bytes to F-RAM from a buffer |
MACRUM | 2:4939ff09bf12 | 106 | * @param address The F-RAM address write to |
MACRUM | 2:4939ff09bf12 | 107 | * @param data The buffer to write from |
MACRUM | 2:4939ff09bf12 | 108 | * @param size The number of bytes to write |
MACRUM | 2:4939ff09bf12 | 109 | */ |
MACRUM | 0:5a552209903c | 110 | void write(uint16_t address, uint8_t *data, uint16_t size); |
MACRUM | 1:bb2b1e4bfb6e | 111 | |
MACRUM | 1:bb2b1e4bfb6e | 112 | /** read a byte from F-RAM |
MACRUM | 2:4939ff09bf12 | 113 | * @param address The address to read from |
MACRUM | 2:4939ff09bf12 | 114 | * @return the character at that address |
MACRUM | 2:4939ff09bf12 | 115 | */ |
MACRUM | 0:5a552209903c | 116 | uint8_t read(uint16_t address); |
MACRUM | 1:bb2b1e4bfb6e | 117 | |
MACRUM | 1:bb2b1e4bfb6e | 118 | /** read multiple bytes from F-RAM into a buffer |
MACRUM | 2:4939ff09bf12 | 119 | * @param address The F-RAM address to read from |
MACRUM | 2:4939ff09bf12 | 120 | * @param data The buffer to read into (must be big enough!) |
MACRUM | 2:4939ff09bf12 | 121 | * @param size The number of bytes to read |
MACRUM | 2:4939ff09bf12 | 122 | */ |
MACRUM | 0:5a552209903c | 123 | void read(uint16_t address, uint8_t *data, uint16_t size); |
MACRUM | 0:5a552209903c | 124 | |
MACRUM | 1:bb2b1e4bfb6e | 125 | /** write a byte to the status register |
MACRUM | 2:4939ff09bf12 | 126 | * @param data The byte to write the register |
MACRUM | 2:4939ff09bf12 | 127 | */ |
MACRUM | 1:bb2b1e4bfb6e | 128 | void wirte_status(uint8_t data); |
MACRUM | 1:bb2b1e4bfb6e | 129 | |
MACRUM | 1:bb2b1e4bfb6e | 130 | /** read a byte from the status register |
MACRUM | 2:4939ff09bf12 | 131 | * @return the character at the register |
MACRUM | 2:4939ff09bf12 | 132 | */ |
MACRUM | 1:bb2b1e4bfb6e | 133 | uint8_t read_status(); |
MACRUM | 1:bb2b1e4bfb6e | 134 | |
MACRUM | 1:bb2b1e4bfb6e | 135 | /** Set write protect mode |
MACRUM | 2:4939ff09bf12 | 136 | * @param bp E_WP enum value |
MACRUM | 2:4939ff09bf12 | 137 | */ |
MACRUM | 1:bb2b1e4bfb6e | 138 | void set_write_protect(E_WP bp); |
MACRUM | 1:bb2b1e4bfb6e | 139 | |
MACRUM | 1:bb2b1e4bfb6e | 140 | /** Set write protect to non-protect mode |
MACRUM | 2:4939ff09bf12 | 141 | */ |
MACRUM | 1:bb2b1e4bfb6e | 142 | void clear_write_protect(); |
MACRUM | 1:bb2b1e4bfb6e | 143 | |
MACRUM | 0:5a552209903c | 144 | protected: |
MACRUM | 0:5a552209903c | 145 | SPI _spi; |
MACRUM | 0:5a552209903c | 146 | DigitalOut _cs; |
MACRUM | 0:5a552209903c | 147 | }; |
MACRUM | 0:5a552209903c | 148 | |
MACRUM | 0:5a552209903c | 149 | #endif |