Cypress F-RAM FM25W256 library
Dependents: Hello-FM25W256 Hello-FM25W256
FM25W256.h@3:aa1c1f07c942, 2016-03-05 (annotated)
- Committer:
- MACRUM
- Date:
- Sat Mar 05 14:14:28 2016 +0000
- Revision:
- 3:aa1c1f07c942
- Parent:
- 2:4939ff09bf12
Fix comment again
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 | 3:aa1c1f07c942 | 72 | * @endcode |
MACRUM | 2:4939ff09bf12 | 73 | */ |
MACRUM | 0:5a552209903c | 74 | class FM25W256 { |
MACRUM | 0:5a552209903c | 75 | public: |
MACRUM | 1:bb2b1e4bfb6e | 76 | |
MACRUM | 1:bb2b1e4bfb6e | 77 | enum E_WP { |
MACRUM | 1:bb2b1e4bfb6e | 78 | BANK_NONE = 0, |
MACRUM | 1:bb2b1e4bfb6e | 79 | BANK_UPPER_QUARTER = 1, |
MACRUM | 1:bb2b1e4bfb6e | 80 | BANK_UPPER_HALF = 2, |
MACRUM | 1:bb2b1e4bfb6e | 81 | BANK_ALL = 3 |
MACRUM | 1:bb2b1e4bfb6e | 82 | }; |
MACRUM | 1:bb2b1e4bfb6e | 83 | |
MACRUM | 1:bb2b1e4bfb6e | 84 | /** Create an interface |
MACRUM | 2:4939ff09bf12 | 85 | * |
MACRUM | 2:4939ff09bf12 | 86 | * @param mosi SPI master-out-slave-in |
MACRUM | 2:4939ff09bf12 | 87 | * @param miso SPI master-in-slave-out |
MACRUM | 2:4939ff09bf12 | 88 | * @param clk SPI clock |
MACRUM | 2:4939ff09bf12 | 89 | * @param cs chip select pin - any free Digital pin will do |
MACRUM | 2:4939ff09bf12 | 90 | */ |
MACRUM | 0:5a552209903c | 91 | FM25W256(PinName mosi, PinName miso, PinName clk, PinName cs); |
MACRUM | 1:bb2b1e4bfb6e | 92 | |
MACRUM | 1:bb2b1e4bfb6e | 93 | /** Create an interface |
MACRUM | 2:4939ff09bf12 | 94 | * |
MACRUM | 2:4939ff09bf12 | 95 | * @param &spi SPI instance |
MACRUM | 2:4939ff09bf12 | 96 | * @param cs chip select pin - any free Digital pin will do |
MACRUM | 2:4939ff09bf12 | 97 | */ |
MACRUM | 0:5a552209903c | 98 | FM25W256(SPI &spi, PinName cs); |
MACRUM | 1:bb2b1e4bfb6e | 99 | |
MACRUM | 1:bb2b1e4bfb6e | 100 | /** write a byte to F-RAM |
MACRUM | 2:4939ff09bf12 | 101 | * @param address The address F-RAM to write to |
MACRUM | 2:4939ff09bf12 | 102 | * @param data The byte to write there |
MACRUM | 2:4939ff09bf12 | 103 | */ |
MACRUM | 0:5a552209903c | 104 | void write(uint16_t address, uint8_t data); |
MACRUM | 1:bb2b1e4bfb6e | 105 | |
MACRUM | 1:bb2b1e4bfb6e | 106 | /** write multiple bytes to F-RAM from a buffer |
MACRUM | 2:4939ff09bf12 | 107 | * @param address The F-RAM address write to |
MACRUM | 2:4939ff09bf12 | 108 | * @param data The buffer to write from |
MACRUM | 2:4939ff09bf12 | 109 | * @param size The number of bytes to write |
MACRUM | 2:4939ff09bf12 | 110 | */ |
MACRUM | 0:5a552209903c | 111 | void write(uint16_t address, uint8_t *data, uint16_t size); |
MACRUM | 1:bb2b1e4bfb6e | 112 | |
MACRUM | 1:bb2b1e4bfb6e | 113 | /** read a byte from F-RAM |
MACRUM | 2:4939ff09bf12 | 114 | * @param address The address to read from |
MACRUM | 2:4939ff09bf12 | 115 | * @return the character at that address |
MACRUM | 2:4939ff09bf12 | 116 | */ |
MACRUM | 0:5a552209903c | 117 | uint8_t read(uint16_t address); |
MACRUM | 1:bb2b1e4bfb6e | 118 | |
MACRUM | 1:bb2b1e4bfb6e | 119 | /** read multiple bytes from F-RAM into a buffer |
MACRUM | 2:4939ff09bf12 | 120 | * @param address The F-RAM address to read from |
MACRUM | 2:4939ff09bf12 | 121 | * @param data The buffer to read into (must be big enough!) |
MACRUM | 2:4939ff09bf12 | 122 | * @param size The number of bytes to read |
MACRUM | 2:4939ff09bf12 | 123 | */ |
MACRUM | 0:5a552209903c | 124 | void read(uint16_t address, uint8_t *data, uint16_t size); |
MACRUM | 0:5a552209903c | 125 | |
MACRUM | 1:bb2b1e4bfb6e | 126 | /** write a byte to the status register |
MACRUM | 2:4939ff09bf12 | 127 | * @param data The byte to write the register |
MACRUM | 2:4939ff09bf12 | 128 | */ |
MACRUM | 1:bb2b1e4bfb6e | 129 | void wirte_status(uint8_t data); |
MACRUM | 1:bb2b1e4bfb6e | 130 | |
MACRUM | 1:bb2b1e4bfb6e | 131 | /** read a byte from the status register |
MACRUM | 2:4939ff09bf12 | 132 | * @return the character at the register |
MACRUM | 2:4939ff09bf12 | 133 | */ |
MACRUM | 1:bb2b1e4bfb6e | 134 | uint8_t read_status(); |
MACRUM | 1:bb2b1e4bfb6e | 135 | |
MACRUM | 1:bb2b1e4bfb6e | 136 | /** Set write protect mode |
MACRUM | 2:4939ff09bf12 | 137 | * @param bp E_WP enum value |
MACRUM | 2:4939ff09bf12 | 138 | */ |
MACRUM | 1:bb2b1e4bfb6e | 139 | void set_write_protect(E_WP bp); |
MACRUM | 1:bb2b1e4bfb6e | 140 | |
MACRUM | 1:bb2b1e4bfb6e | 141 | /** Set write protect to non-protect mode |
MACRUM | 2:4939ff09bf12 | 142 | */ |
MACRUM | 1:bb2b1e4bfb6e | 143 | void clear_write_protect(); |
MACRUM | 1:bb2b1e4bfb6e | 144 | |
MACRUM | 0:5a552209903c | 145 | protected: |
MACRUM | 0:5a552209903c | 146 | SPI _spi; |
MACRUM | 0:5a552209903c | 147 | DigitalOut _cs; |
MACRUM | 0:5a552209903c | 148 | }; |
MACRUM | 0:5a552209903c | 149 | |
MACRUM | 0:5a552209903c | 150 | #endif |