Cypress F-RAM FM25W256 library

Dependents:   Hello-FM25W256 Hello-FM25W256

Committer:
MACRUM
Date:
Sat Mar 05 14:05:39 2016 +0000
Revision:
1:bb2b1e4bfb6e
Parent:
0:5a552209903c
Add write protect functions and more comments

Who changed what in which revision?

UserRevisionLine numberNew 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 #include "FM25W256.h"
MACRUM 0:5a552209903c 28
MACRUM 0:5a552209903c 29 FM25W256::FM25W256(PinName mosi, PinName miso, PinName clk, PinName cs)
MACRUM 0:5a552209903c 30 : _spi(mosi, miso, clk), _cs(cs)
MACRUM 0:5a552209903c 31 {
MACRUM 0:5a552209903c 32 _spi.format(8, 0);
MACRUM 1:bb2b1e4bfb6e 33 _spi.frequency(FM25W256_CLK);
MACRUM 0:5a552209903c 34 _cs = 1;
MACRUM 1:bb2b1e4bfb6e 35 wait_ms(1); // tPU = 1ms (min.)
MACRUM 0:5a552209903c 36 }
MACRUM 0:5a552209903c 37
MACRUM 0:5a552209903c 38 FM25W256::FM25W256(SPI &spi, PinName cs)
MACRUM 0:5a552209903c 39 : _spi(spi), _cs(cs)
MACRUM 0:5a552209903c 40 {
MACRUM 0:5a552209903c 41 _spi.format(8, 0);
MACRUM 1:bb2b1e4bfb6e 42 _spi.frequency(FM25W256_CLK);
MACRUM 0:5a552209903c 43 _cs = 1;
MACRUM 1:bb2b1e4bfb6e 44 wait_ms(1); // tPU = 1ms (min.)
MACRUM 0:5a552209903c 45 }
MACRUM 0:5a552209903c 46
MACRUM 0:5a552209903c 47 void FM25W256::write(uint16_t address, uint8_t data)
MACRUM 0:5a552209903c 48 {
MACRUM 0:5a552209903c 49 _cs = 0;
MACRUM 0:5a552209903c 50 _spi.write(CMD_WREN);
MACRUM 0:5a552209903c 51 _cs = 1;
MACRUM 0:5a552209903c 52
MACRUM 0:5a552209903c 53 _cs = 0;
MACRUM 0:5a552209903c 54 _spi.write(CMD_WRITE);
MACRUM 0:5a552209903c 55 _spi.write(address >> 8);
MACRUM 0:5a552209903c 56 _spi.write(address & 0xFF);
MACRUM 0:5a552209903c 57 _spi.write(data);
MACRUM 0:5a552209903c 58 _cs = 1;
MACRUM 0:5a552209903c 59 }
MACRUM 0:5a552209903c 60
MACRUM 0:5a552209903c 61 void FM25W256::write(uint16_t address, uint8_t *data, uint16_t size)
MACRUM 0:5a552209903c 62 {
MACRUM 0:5a552209903c 63 _cs = 0;
MACRUM 0:5a552209903c 64 _spi.write(CMD_WREN);
MACRUM 0:5a552209903c 65 _cs = 1;
MACRUM 0:5a552209903c 66
MACRUM 0:5a552209903c 67 _cs = 0;
MACRUM 0:5a552209903c 68 _spi.write(CMD_WRITE);
MACRUM 0:5a552209903c 69 _spi.write(address >> 8);
MACRUM 0:5a552209903c 70 _spi.write(address & 0xFF);
MACRUM 0:5a552209903c 71 while(size--) {
MACRUM 0:5a552209903c 72 _spi.write(*data++);
MACRUM 0:5a552209903c 73 }
MACRUM 0:5a552209903c 74 _cs = 1;
MACRUM 0:5a552209903c 75 }
MACRUM 0:5a552209903c 76
MACRUM 0:5a552209903c 77 uint8_t FM25W256::read(uint16_t address)
MACRUM 0:5a552209903c 78 {
MACRUM 0:5a552209903c 79 uint8_t data;
MACRUM 0:5a552209903c 80
MACRUM 0:5a552209903c 81 _cs = 0;
MACRUM 0:5a552209903c 82 _spi.write(CMD_READ);
MACRUM 0:5a552209903c 83 _spi.write(address >> 8);
MACRUM 0:5a552209903c 84 _spi.write(address & 0xFF);
MACRUM 0:5a552209903c 85 data = _spi.write(0);
MACRUM 0:5a552209903c 86 _cs = 1;
MACRUM 0:5a552209903c 87
MACRUM 0:5a552209903c 88 return data;
MACRUM 0:5a552209903c 89 }
MACRUM 0:5a552209903c 90
MACRUM 0:5a552209903c 91 void FM25W256::read(uint16_t address, uint8_t *buf, uint16_t size)
MACRUM 0:5a552209903c 92 {
MACRUM 0:5a552209903c 93 _cs = 0;
MACRUM 0:5a552209903c 94 _spi.write(CMD_READ);
MACRUM 0:5a552209903c 95 _spi.write(address >> 8);
MACRUM 0:5a552209903c 96 _spi.write(address & 0xFF);
MACRUM 0:5a552209903c 97 while (size--) {
MACRUM 0:5a552209903c 98 *buf++ = _spi.write(0);
MACRUM 0:5a552209903c 99 }
MACRUM 0:5a552209903c 100 _cs = 1;
MACRUM 0:5a552209903c 101 }
MACRUM 1:bb2b1e4bfb6e 102
MACRUM 1:bb2b1e4bfb6e 103 void FM25W256::wirte_status(uint8_t data)
MACRUM 1:bb2b1e4bfb6e 104 {
MACRUM 1:bb2b1e4bfb6e 105 _cs = 0;
MACRUM 1:bb2b1e4bfb6e 106 _spi.write(CMD_WREN);
MACRUM 1:bb2b1e4bfb6e 107 _cs = 1;
MACRUM 1:bb2b1e4bfb6e 108
MACRUM 1:bb2b1e4bfb6e 109 _cs = 0;
MACRUM 1:bb2b1e4bfb6e 110 _spi.write(CMD_WRSR);
MACRUM 1:bb2b1e4bfb6e 111 _spi.write(data);
MACRUM 1:bb2b1e4bfb6e 112 _cs = 1;
MACRUM 1:bb2b1e4bfb6e 113 }
MACRUM 1:bb2b1e4bfb6e 114
MACRUM 1:bb2b1e4bfb6e 115 uint8_t FM25W256::read_status()
MACRUM 1:bb2b1e4bfb6e 116 {
MACRUM 1:bb2b1e4bfb6e 117 uint8_t data;
MACRUM 1:bb2b1e4bfb6e 118 _cs = 0;
MACRUM 1:bb2b1e4bfb6e 119 _spi.write(CMD_RDSR);
MACRUM 1:bb2b1e4bfb6e 120 data = _spi.write(0);
MACRUM 1:bb2b1e4bfb6e 121 _cs = 1;
MACRUM 1:bb2b1e4bfb6e 122
MACRUM 1:bb2b1e4bfb6e 123 return data;
MACRUM 1:bb2b1e4bfb6e 124 }
MACRUM 1:bb2b1e4bfb6e 125
MACRUM 1:bb2b1e4bfb6e 126 void FM25W256::set_write_protect(E_WP bp)
MACRUM 1:bb2b1e4bfb6e 127 {
MACRUM 1:bb2b1e4bfb6e 128 // Set WPEN, BP0 and BP1
MACRUM 1:bb2b1e4bfb6e 129 wirte_status((1 << 7) | (bp << 2));
MACRUM 1:bb2b1e4bfb6e 130 }
MACRUM 1:bb2b1e4bfb6e 131
MACRUM 1:bb2b1e4bfb6e 132 void FM25W256::clear_write_protect()
MACRUM 1:bb2b1e4bfb6e 133 {
MACRUM 1:bb2b1e4bfb6e 134 wirte_status(0);
MACRUM 1:bb2b1e4bfb6e 135 }
MACRUM 1:bb2b1e4bfb6e 136