repo time
Dependencies: mbed MAX14720 MAX30205 USBDevice
e4a10ed6eb92/HSP/Devices/S25FS256/S25FS512.cpp@20:6d2af70c92ab, 2021-04-06 (annotated)
- Committer:
- darienf
- Date:
- Tue Apr 06 06:41:40 2021 +0000
- Revision:
- 20:6d2af70c92ab
another repo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
darienf | 20:6d2af70c92ab | 1 | /******************************************************************************* |
darienf | 20:6d2af70c92ab | 2 | * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. |
darienf | 20:6d2af70c92ab | 3 | * |
darienf | 20:6d2af70c92ab | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
darienf | 20:6d2af70c92ab | 5 | * copy of this software and associated documentation files (the "Software"), |
darienf | 20:6d2af70c92ab | 6 | * to deal in the Software without restriction, including without limitation |
darienf | 20:6d2af70c92ab | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
darienf | 20:6d2af70c92ab | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
darienf | 20:6d2af70c92ab | 9 | * Software is furnished to do so, subject to the following conditions: |
darienf | 20:6d2af70c92ab | 10 | * |
darienf | 20:6d2af70c92ab | 11 | * The above copyright notice and this permission notice shall be included |
darienf | 20:6d2af70c92ab | 12 | * in all copies or substantial portions of the Software. |
darienf | 20:6d2af70c92ab | 13 | * |
darienf | 20:6d2af70c92ab | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
darienf | 20:6d2af70c92ab | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
darienf | 20:6d2af70c92ab | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
darienf | 20:6d2af70c92ab | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
darienf | 20:6d2af70c92ab | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
darienf | 20:6d2af70c92ab | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
darienf | 20:6d2af70c92ab | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
darienf | 20:6d2af70c92ab | 21 | * |
darienf | 20:6d2af70c92ab | 22 | * Except as contained in this notice, the name of Maxim Integrated |
darienf | 20:6d2af70c92ab | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
darienf | 20:6d2af70c92ab | 24 | * Products, Inc. Branding Policy. |
darienf | 20:6d2af70c92ab | 25 | * |
darienf | 20:6d2af70c92ab | 26 | * The mere transfer of this software does not imply any licenses |
darienf | 20:6d2af70c92ab | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
darienf | 20:6d2af70c92ab | 28 | * trademarks, maskwork rights, or any other form of intellectual |
darienf | 20:6d2af70c92ab | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
darienf | 20:6d2af70c92ab | 30 | * ownership rights. |
darienf | 20:6d2af70c92ab | 31 | ******************************************************************************* |
darienf | 20:6d2af70c92ab | 32 | */ |
darienf | 20:6d2af70c92ab | 33 | |
darienf | 20:6d2af70c92ab | 34 | // |
darienf | 20:6d2af70c92ab | 35 | // Flash Non-Volatile Memory |
darienf | 20:6d2af70c92ab | 36 | // U27 S25FS512 |
darienf | 20:6d2af70c92ab | 37 | // Nimitz SPIM1 |
darienf | 20:6d2af70c92ab | 38 | // |
darienf | 20:6d2af70c92ab | 39 | |
darienf | 20:6d2af70c92ab | 40 | #include "mbed.h" |
darienf | 20:6d2af70c92ab | 41 | #include "S25FS512.h" |
darienf | 20:6d2af70c92ab | 42 | #include "QuadSpiInterface.h" |
darienf | 20:6d2af70c92ab | 43 | |
darienf | 20:6d2af70c92ab | 44 | #define IOMUX_IO_ENABLE 1 |
darienf | 20:6d2af70c92ab | 45 | |
darienf | 20:6d2af70c92ab | 46 | #define S25FS512_SPI_PORT 1 |
darienf | 20:6d2af70c92ab | 47 | #define S25FS512_CS_PIN 0 |
darienf | 20:6d2af70c92ab | 48 | #define S25FS512_CS_POLARITY 0 |
darienf | 20:6d2af70c92ab | 49 | #define S25FS512_CS_ACTIVITY_DELAY 0 |
darienf | 20:6d2af70c92ab | 50 | #define S25FS512_CS_INACTIVITY_DELAY 0 |
darienf | 20:6d2af70c92ab | 51 | #define S25FS512_CLK_HI 4 |
darienf | 20:6d2af70c92ab | 52 | #define S25FS512_CLK_LOW 4 |
darienf | 20:6d2af70c92ab | 53 | #define S25FS512_ALT_CLK 0 |
darienf | 20:6d2af70c92ab | 54 | #define S25FS512_CLK_POLARITY 0 |
darienf | 20:6d2af70c92ab | 55 | #define S25FS512_CLK_PHASE 0 |
darienf | 20:6d2af70c92ab | 56 | #define S25FS512_WRITE 1 |
darienf | 20:6d2af70c92ab | 57 | #define S25FS512_READ 0 |
darienf | 20:6d2af70c92ab | 58 | |
darienf | 20:6d2af70c92ab | 59 | #define INT_PORT_B 3 |
darienf | 20:6d2af70c92ab | 60 | #define INT_PIN_B 6 |
darienf | 20:6d2af70c92ab | 61 | |
darienf | 20:6d2af70c92ab | 62 | uint8_t flashBuffer[257 + 10]; |
darienf | 20:6d2af70c92ab | 63 | |
darienf | 20:6d2af70c92ab | 64 | //****************************************************************************** |
darienf | 20:6d2af70c92ab | 65 | S25FS512::S25FS512(QuadSpiInterface *_quadSpiInterface) { |
darienf | 20:6d2af70c92ab | 66 | this->quadSpiInterface = _quadSpiInterface; |
darienf | 20:6d2af70c92ab | 67 | } |
darienf | 20:6d2af70c92ab | 68 | |
darienf | 20:6d2af70c92ab | 69 | //****************************************************************************** |
darienf | 20:6d2af70c92ab | 70 | S25FS512::~S25FS512(void) { |
darienf | 20:6d2af70c92ab | 71 | } |
darienf | 20:6d2af70c92ab | 72 | |
darienf | 20:6d2af70c92ab | 73 | //****************************************************************************** |
darienf | 20:6d2af70c92ab | 74 | int S25FS512::init(void) { |
darienf | 20:6d2af70c92ab | 75 | setQuadMode(); |
darienf | 20:6d2af70c92ab | 76 | return 0; |
darienf | 20:6d2af70c92ab | 77 | } |
darienf | 20:6d2af70c92ab | 78 | |
darienf | 20:6d2af70c92ab | 79 | //****************************************************************************** |
darienf | 20:6d2af70c92ab | 80 | int S25FS512::wren4Wire(void) { |
darienf | 20:6d2af70c92ab | 81 | uint8_t cmdArray[8]; |
darienf | 20:6d2af70c92ab | 82 | // Send WREN |
darienf | 20:6d2af70c92ab | 83 | cmdArray[0] = 0x06; |
darienf | 20:6d2af70c92ab | 84 | wait_1mS(); |
darienf | 20:6d2af70c92ab | 85 | return reg_write_read_multiple_4Wire(cmdArray, 1, flashBuffer, 1); |
darienf | 20:6d2af70c92ab | 86 | } |
darienf | 20:6d2af70c92ab | 87 | |
darienf | 20:6d2af70c92ab | 88 | //****************************************************************************** |
darienf | 20:6d2af70c92ab | 89 | uint8_t S25FS512::wren(void) { |
darienf | 20:6d2af70c92ab | 90 | uint8_t cmdArray[8]; |
darienf | 20:6d2af70c92ab | 91 | // Send WREN |
darienf | 20:6d2af70c92ab | 92 | cmdArray[0] = 0x06; |
darienf | 20:6d2af70c92ab | 93 | wait_1mS(); |
darienf | 20:6d2af70c92ab | 94 | return reg_write_read_multiple_quad(cmdArray, 1, flashBuffer, 0); |
darienf | 20:6d2af70c92ab | 95 | } |
darienf | 20:6d2af70c92ab | 96 | |
darienf | 20:6d2af70c92ab | 97 | //****************************************************************************** |
darienf | 20:6d2af70c92ab | 98 | int8_t S25FS512::reg_write_read_multiple_quad_last(uint8_t *bufferOut, |
darienf | 20:6d2af70c92ab | 99 | uint8_t numberOut, |
darienf | 20:6d2af70c92ab | 100 | uint8_t *bufferIn, |
darienf | 20:6d2af70c92ab | 101 | uint8_t numberIn, |
darienf | 20:6d2af70c92ab | 102 | uint8_t last) { |
darienf | 20:6d2af70c92ab | 103 | int32_t success = 0; |
darienf | 20:6d2af70c92ab | 104 | |
darienf | 20:6d2af70c92ab | 105 | success = quadSpiInterface->SPI_Transmit( |
darienf | 20:6d2af70c92ab | 106 | bufferOut, numberOut, |
darienf | 20:6d2af70c92ab | 107 | bufferIn, numberIn, (int)last); |
darienf | 20:6d2af70c92ab | 108 | |
darienf | 20:6d2af70c92ab | 109 | if (success != 0) return -1; |
darienf | 20:6d2af70c92ab | 110 | return 0; |
darienf | 20:6d2af70c92ab | 111 | } |
darienf | 20:6d2af70c92ab | 112 | |
darienf | 20:6d2af70c92ab | 113 | //****************************************************************************** |
darienf | 20:6d2af70c92ab | 114 | int8_t S25FS512::reg_write_read_multiple_4Wire(uint8_t *bufferOut, |
darienf | 20:6d2af70c92ab | 115 | uint8_t numberOut, |
darienf | 20:6d2af70c92ab | 116 | uint8_t *bufferIn, |
darienf | 20:6d2af70c92ab | 117 | uint8_t numberIn) { |
darienf | 20:6d2af70c92ab | 118 | int32_t success = 0; |
darienf | 20:6d2af70c92ab | 119 | success = quadSpiInterface->SPI_Transmit4Wire(bufferOut, numberOut, bufferIn, |
darienf | 20:6d2af70c92ab | 120 | numberIn, (int)1); |
darienf | 20:6d2af70c92ab | 121 | |
darienf | 20:6d2af70c92ab | 122 | if (success != 0) return -1; |
darienf | 20:6d2af70c92ab | 123 | return 0; |
darienf | 20:6d2af70c92ab | 124 | } |
darienf | 20:6d2af70c92ab | 125 | |
darienf | 20:6d2af70c92ab | 126 | //****************************************************************************** |
darienf | 20:6d2af70c92ab | 127 | int8_t S25FS512::reg_write_read_multiple_quad(uint8_t *bufferOut, |
darienf | 20:6d2af70c92ab | 128 | uint8_t numberOut, |
darienf | 20:6d2af70c92ab | 129 | uint8_t *bufferIn, |
darienf | 20:6d2af70c92ab | 130 | uint8_t numberIn) { |
darienf | 20:6d2af70c92ab | 131 | int8_t ret; |
darienf | 20:6d2af70c92ab | 132 | ret = reg_write_read_multiple_quad_last(bufferOut, numberOut, bufferIn, |
darienf | 20:6d2af70c92ab | 133 | numberIn, 1); |
darienf | 20:6d2af70c92ab | 134 | return ret; |
darienf | 20:6d2af70c92ab | 135 | } |
darienf | 20:6d2af70c92ab | 136 | |
darienf | 20:6d2af70c92ab | 137 | //****************************************************************************** |
darienf | 20:6d2af70c92ab | 138 | void S25FS512::readID(uint8_t *id) { |
darienf | 20:6d2af70c92ab | 139 | uint8_t cmd = 0x9F; |
darienf | 20:6d2af70c92ab | 140 | reg_write_read_multiple_quad(&cmd, 1, id, 4); |
darienf | 20:6d2af70c92ab | 141 | } |
darienf | 20:6d2af70c92ab | 142 | |
darienf | 20:6d2af70c92ab | 143 | //****************************************************************************** |
darienf | 20:6d2af70c92ab | 144 | int8_t S25FS512::writeAnyRegister(uint32_t address, uint8_t data) { |
darienf | 20:6d2af70c92ab | 145 | uint8_t cmdArray[5]; |
darienf | 20:6d2af70c92ab | 146 | cmdArray[0] = 0x71; |
darienf | 20:6d2af70c92ab | 147 | cmdArray[1] = (address >> 16) & 0xFF; |
darienf | 20:6d2af70c92ab | 148 | cmdArray[2] = (address >> 8) & 0xFF; |
darienf | 20:6d2af70c92ab | 149 | cmdArray[3] = (address >> 0) & 0xFF; |
darienf | 20:6d2af70c92ab | 150 | cmdArray[4] = data; |
darienf | 20:6d2af70c92ab | 151 | return reg_write_read_multiple_quad(cmdArray, 5, flashBuffer, 0); |
darienf | 20:6d2af70c92ab | 152 | } |
darienf | 20:6d2af70c92ab | 153 | |
darienf | 20:6d2af70c92ab | 154 | int8_t S25FS512::writeAnyRegister4Wire(uint32_t address, uint8_t data) { |
darienf | 20:6d2af70c92ab | 155 | uint8_t cmdArray[5]; |
darienf | 20:6d2af70c92ab | 156 | cmdArray[0] = 0x71; |
darienf | 20:6d2af70c92ab | 157 | cmdArray[1] = (address >> 16) & 0xFF; |
darienf | 20:6d2af70c92ab | 158 | cmdArray[2] = (address >> 8) & 0xFF; |
darienf | 20:6d2af70c92ab | 159 | cmdArray[3] = (address >> 0) & 0xFF; |
darienf | 20:6d2af70c92ab | 160 | cmdArray[4] = data; |
darienf | 20:6d2af70c92ab | 161 | return reg_write_read_multiple_4Wire(cmdArray, 5, flashBuffer, 5); |
darienf | 20:6d2af70c92ab | 162 | } |
darienf | 20:6d2af70c92ab | 163 | |
darienf | 20:6d2af70c92ab | 164 | //****************************************************************************** |
darienf | 20:6d2af70c92ab | 165 | int8_t S25FS512::writeRegisters(void) { |
darienf | 20:6d2af70c92ab | 166 | uint8_t cmdArray[3]; |
darienf | 20:6d2af70c92ab | 167 | wait_1mS(); |
darienf | 20:6d2af70c92ab | 168 | cmdArray[0] = 0x01; |
darienf | 20:6d2af70c92ab | 169 | cmdArray[1] = 0x00; |
darienf | 20:6d2af70c92ab | 170 | cmdArray[2] = 0x02; // set Quad to 1 |
darienf | 20:6d2af70c92ab | 171 | reg_write_read_multiple_quad(cmdArray, 3, flashBuffer, 0); |
darienf | 20:6d2af70c92ab | 172 | return 0; |
darienf | 20:6d2af70c92ab | 173 | } |
darienf | 20:6d2af70c92ab | 174 | |
darienf | 20:6d2af70c92ab | 175 | //****************************************************************************** |
darienf | 20:6d2af70c92ab | 176 | int8_t S25FS512::readAnyRegister(uint32_t address, uint8_t *data, |
darienf | 20:6d2af70c92ab | 177 | uint32_t length) { |
darienf | 20:6d2af70c92ab | 178 | uint8_t cmdArray[4]; |
darienf | 20:6d2af70c92ab | 179 | cmdArray[0] = 0x65; |
darienf | 20:6d2af70c92ab | 180 | cmdArray[1] = (address >> 16) & 0xFF; |
darienf | 20:6d2af70c92ab | 181 | cmdArray[2] = (address >> 8) & 0xFF; |
darienf | 20:6d2af70c92ab | 182 | cmdArray[3] = (address >> 0) & 0xFF; |
darienf | 20:6d2af70c92ab | 183 | return reg_write_read_multiple_quad(cmdArray, 4, data, length); |
darienf | 20:6d2af70c92ab | 184 | } |
darienf | 20:6d2af70c92ab | 185 | |
darienf | 20:6d2af70c92ab | 186 | //****************************************************************************** |
darienf | 20:6d2af70c92ab | 187 | int8_t S25FS512::bulkErase(void) { |
darienf | 20:6d2af70c92ab | 188 | uint8_t cmdArray[1]; |
darienf | 20:6d2af70c92ab | 189 | cmdArray[0] = 0x60; |
darienf | 20:6d2af70c92ab | 190 | return reg_write_read_multiple_quad(cmdArray, 1, flashBuffer, 0); |
darienf | 20:6d2af70c92ab | 191 | } |
darienf | 20:6d2af70c92ab | 192 | |
darienf | 20:6d2af70c92ab | 193 | //****************************************************************************** |
darienf | 20:6d2af70c92ab | 194 | int8_t S25FS512::pageProgram(uint32_t address, uint8_t *buffer) { |
darienf | 20:6d2af70c92ab | 195 | uint32_t i; |
darienf | 20:6d2af70c92ab | 196 | uint8_t cmdArray[5 + 256]; |
darienf | 20:6d2af70c92ab | 197 | uint8_t *ptr; |
darienf | 20:6d2af70c92ab | 198 | |
darienf | 20:6d2af70c92ab | 199 | // for (i = 0; i < 256; i++) { |
darienf | 20:6d2af70c92ab | 200 | // dataArray[i] = i; |
darienf | 20:6d2af70c92ab | 201 | //} |
darienf | 20:6d2af70c92ab | 202 | cmdArray[0] = 0x02; // 0x71; |
darienf | 20:6d2af70c92ab | 203 | // cmdArray[1] = (address >> 24) & 0xFF; |
darienf | 20:6d2af70c92ab | 204 | cmdArray[1] = (address >> 16) & 0xFF; |
darienf | 20:6d2af70c92ab | 205 | cmdArray[2] = (address >> 8) & 0xFF; |
darienf | 20:6d2af70c92ab | 206 | cmdArray[3] = (address >> 0) & 0xFF; |
darienf | 20:6d2af70c92ab | 207 | for (i = 0; i < 256; i++) { |
darienf | 20:6d2af70c92ab | 208 | cmdArray[4 + i] = buffer[i]; |
darienf | 20:6d2af70c92ab | 209 | } |
darienf | 20:6d2af70c92ab | 210 | // reg_write_read_multiple_quad(cmdArray,256 + 4,flashBuffer,256 + 4); |
darienf | 20:6d2af70c92ab | 211 | |
darienf | 20:6d2af70c92ab | 212 | ptr = cmdArray; |
darienf | 20:6d2af70c92ab | 213 | reg_write_read_multiple_quad_last(ptr, 4 + 64, flashBuffer, 0, 0); |
darienf | 20:6d2af70c92ab | 214 | wait_1mS(); |
darienf | 20:6d2af70c92ab | 215 | ptr += (4 + 64); |
darienf | 20:6d2af70c92ab | 216 | reg_write_read_multiple_quad_last(ptr, 64, flashBuffer, 0, 0); |
darienf | 20:6d2af70c92ab | 217 | wait_1mS(); |
darienf | 20:6d2af70c92ab | 218 | ptr += 64; |
darienf | 20:6d2af70c92ab | 219 | reg_write_read_multiple_quad_last(ptr, 64, flashBuffer, 0, 0); |
darienf | 20:6d2af70c92ab | 220 | wait_1mS(); |
darienf | 20:6d2af70c92ab | 221 | ptr += 64; |
darienf | 20:6d2af70c92ab | 222 | reg_write_read_multiple_quad_last(ptr, 64, flashBuffer, 0, 1); |
darienf | 20:6d2af70c92ab | 223 | wait_1mS(); |
darienf | 20:6d2af70c92ab | 224 | return 0; |
darienf | 20:6d2af70c92ab | 225 | } |
darienf | 20:6d2af70c92ab | 226 | |
darienf | 20:6d2af70c92ab | 227 | //****************************************************************************** |
darienf | 20:6d2af70c92ab | 228 | int8_t S25FS512::quadIoRead_Pages(uint32_t address, uint8_t *buffer, |
darienf | 20:6d2af70c92ab | 229 | uint32_t numberOfPages) { |
darienf | 20:6d2af70c92ab | 230 | uint8_t cmdArray[5]; |
darienf | 20:6d2af70c92ab | 231 | uint8_t *ptr; |
darienf | 20:6d2af70c92ab | 232 | uint8_t last; |
darienf | 20:6d2af70c92ab | 233 | uint32_t i; |
darienf | 20:6d2af70c92ab | 234 | |
darienf | 20:6d2af70c92ab | 235 | cmdArray[0] = 0xEB; |
darienf | 20:6d2af70c92ab | 236 | cmdArray[1] = (address >> 16) & 0xFF; |
darienf | 20:6d2af70c92ab | 237 | cmdArray[2] = (address >> 8) & 0xFF; |
darienf | 20:6d2af70c92ab | 238 | cmdArray[3] = (address >> 0) & 0xFF; |
darienf | 20:6d2af70c92ab | 239 | ptr = buffer; |
darienf | 20:6d2af70c92ab | 240 | last = 0; |
darienf | 20:6d2af70c92ab | 241 | // only send the command |
darienf | 20:6d2af70c92ab | 242 | reg_write_read_multiple_quad_last(cmdArray, 4, ptr, 0, 0); |
darienf | 20:6d2af70c92ab | 243 | wait_1mS(); |
darienf | 20:6d2af70c92ab | 244 | reg_write_read_multiple_quad_last(cmdArray, 0, ptr, 5, 0); |
darienf | 20:6d2af70c92ab | 245 | wait_1mS(); |
darienf | 20:6d2af70c92ab | 246 | for (i = 0; i < numberOfPages; i++) { |
darienf | 20:6d2af70c92ab | 247 | reg_write_read_multiple_quad_last(cmdArray, 0, ptr, 64, 0); |
darienf | 20:6d2af70c92ab | 248 | wait_1mS(); |
darienf | 20:6d2af70c92ab | 249 | ptr += 64; |
darienf | 20:6d2af70c92ab | 250 | reg_write_read_multiple_quad_last(cmdArray, 0, ptr, 64, 0); |
darienf | 20:6d2af70c92ab | 251 | wait_1mS(); |
darienf | 20:6d2af70c92ab | 252 | ptr += 64; |
darienf | 20:6d2af70c92ab | 253 | reg_write_read_multiple_quad_last(cmdArray, 0, ptr, 64, 0); |
darienf | 20:6d2af70c92ab | 254 | wait_1mS(); |
darienf | 20:6d2af70c92ab | 255 | ptr += 64; |
darienf | 20:6d2af70c92ab | 256 | // check if this is the last page |
darienf | 20:6d2af70c92ab | 257 | if ((i + 1) == numberOfPages) { |
darienf | 20:6d2af70c92ab | 258 | last = 1; |
darienf | 20:6d2af70c92ab | 259 | } |
darienf | 20:6d2af70c92ab | 260 | reg_write_read_multiple_quad_last(cmdArray, 0, ptr, 64, last); |
darienf | 20:6d2af70c92ab | 261 | wait_1mS(); |
darienf | 20:6d2af70c92ab | 262 | ptr += 64; |
darienf | 20:6d2af70c92ab | 263 | } |
darienf | 20:6d2af70c92ab | 264 | return 0; |
darienf | 20:6d2af70c92ab | 265 | } |
darienf | 20:6d2af70c92ab | 266 | |
darienf | 20:6d2af70c92ab | 267 | //****************************************************************************** |
darienf | 20:6d2af70c92ab | 268 | int8_t S25FS512::checkBusy(void) { |
darienf | 20:6d2af70c92ab | 269 | uint8_t cmdArray[5]; |
darienf | 20:6d2af70c92ab | 270 | cmdArray[0] = 0x05; |
darienf | 20:6d2af70c92ab | 271 | reg_write_read_multiple_quad(cmdArray, 1, flashBuffer, 2); |
darienf | 20:6d2af70c92ab | 272 | return flashBuffer[1] & 0x1; |
darienf | 20:6d2af70c92ab | 273 | } |
darienf | 20:6d2af70c92ab | 274 | |
darienf | 20:6d2af70c92ab | 275 | //****************************************************************************** |
darienf | 20:6d2af70c92ab | 276 | void S25FS512::waitTillNotBusy(void) { |
darienf | 20:6d2af70c92ab | 277 | while (checkBusy() == 1) { |
darienf | 20:6d2af70c92ab | 278 | } |
darienf | 20:6d2af70c92ab | 279 | } |
darienf | 20:6d2af70c92ab | 280 | |
darienf | 20:6d2af70c92ab | 281 | //****************************************************************************** |
darienf | 20:6d2af70c92ab | 282 | int8_t S25FS512::sectorErase(uint32_t address) { |
darienf | 20:6d2af70c92ab | 283 | uint8_t cmdArray[5]; |
darienf | 20:6d2af70c92ab | 284 | cmdArray[0] = 0xD8; |
darienf | 20:6d2af70c92ab | 285 | cmdArray[1] = (address >> 16) & 0xFF; |
darienf | 20:6d2af70c92ab | 286 | cmdArray[2] = (address >> 8) & 0xFF; |
darienf | 20:6d2af70c92ab | 287 | cmdArray[3] = (address >> 0) & 0xFF; |
darienf | 20:6d2af70c92ab | 288 | return reg_write_read_multiple_quad(cmdArray, 4, flashBuffer, 0); |
darienf | 20:6d2af70c92ab | 289 | } |
darienf | 20:6d2af70c92ab | 290 | |
darienf | 20:6d2af70c92ab | 291 | //****************************************************************************** |
darienf | 20:6d2af70c92ab | 292 | int8_t S25FS512::parameterSectorErase(uint32_t address) { |
darienf | 20:6d2af70c92ab | 293 | uint8_t cmdArray[5]; |
darienf | 20:6d2af70c92ab | 294 | cmdArray[0] = 0x20; |
darienf | 20:6d2af70c92ab | 295 | cmdArray[1] = (address >> 16) & 0xFF; |
darienf | 20:6d2af70c92ab | 296 | cmdArray[2] = (address >> 8) & 0xFF; |
darienf | 20:6d2af70c92ab | 297 | cmdArray[3] = (address >> 0) & 0xFF; |
darienf | 20:6d2af70c92ab | 298 | reg_write_read_multiple_quad(cmdArray, 4, flashBuffer, 0); |
darienf | 20:6d2af70c92ab | 299 | return 0; |
darienf | 20:6d2af70c92ab | 300 | } |
darienf | 20:6d2af70c92ab | 301 | |
darienf | 20:6d2af70c92ab | 302 | #define ONE_MS (32768 / 500) |
darienf | 20:6d2af70c92ab | 303 | #define ONEHUNDRED_US (32768 / 1000) |
darienf | 20:6d2af70c92ab | 304 | #define TEM_MS (32768 / 50) |
darienf | 20:6d2af70c92ab | 305 | |
darienf | 20:6d2af70c92ab | 306 | //****************************************************************************** |
darienf | 20:6d2af70c92ab | 307 | void S25FS512::wait_1mS(void) { |
darienf | 20:6d2af70c92ab | 308 | wait_ms(1); |
darienf | 20:6d2af70c92ab | 309 | } |
darienf | 20:6d2af70c92ab | 310 | |
darienf | 20:6d2af70c92ab | 311 | //****************************************************************************** |
darienf | 20:6d2af70c92ab | 312 | void S25FS512::wait_100uS(void) { |
darienf | 20:6d2af70c92ab | 313 | wait_us(100); |
darienf | 20:6d2af70c92ab | 314 | } |
darienf | 20:6d2af70c92ab | 315 | |
darienf | 20:6d2af70c92ab | 316 | //****************************************************************************** |
darienf | 20:6d2af70c92ab | 317 | void S25FS512::wait_10mS(void) { |
darienf | 20:6d2af70c92ab | 318 | wait_ms(10); |
darienf | 20:6d2af70c92ab | 319 | } |
darienf | 20:6d2af70c92ab | 320 | |
darienf | 20:6d2af70c92ab | 321 | //****************************************************************************** |
darienf | 20:6d2af70c92ab | 322 | int8_t S25FS512::readIdentification(uint8_t *dataArray, uint8_t length) { |
darienf | 20:6d2af70c92ab | 323 | // 4QIOR = 0x9F |
darienf | 20:6d2af70c92ab | 324 | uint8_t cmdArray[1]; |
darienf | 20:6d2af70c92ab | 325 | cmdArray[0] = 0x9F; // read ID command |
darienf | 20:6d2af70c92ab | 326 | return reg_write_read_multiple_quad(cmdArray, 1, dataArray, length); |
darienf | 20:6d2af70c92ab | 327 | } |
darienf | 20:6d2af70c92ab | 328 | |
darienf | 20:6d2af70c92ab | 329 | //****************************************************************************** |
darienf | 20:6d2af70c92ab | 330 | uint8_t S25FS512::reset(void) { |
darienf | 20:6d2af70c92ab | 331 | uint8_t cmdArray[8]; |
darienf | 20:6d2af70c92ab | 332 | wait_1mS(); |
darienf | 20:6d2af70c92ab | 333 | cmdArray[0] = 0x66; |
darienf | 20:6d2af70c92ab | 334 | reg_write_read_multiple_quad(cmdArray, 1, flashBuffer, 0); |
darienf | 20:6d2af70c92ab | 335 | wait_1mS(); |
darienf | 20:6d2af70c92ab | 336 | cmdArray[0] = 0x99; |
darienf | 20:6d2af70c92ab | 337 | reg_write_read_multiple_quad(cmdArray, 1, flashBuffer, 0); |
darienf | 20:6d2af70c92ab | 338 | return 0; |
darienf | 20:6d2af70c92ab | 339 | } |
darienf | 20:6d2af70c92ab | 340 | |
darienf | 20:6d2af70c92ab | 341 | //****************************************************************************** |
darienf | 20:6d2af70c92ab | 342 | uint8_t S25FS512::enableHWReset(void) { |
darienf | 20:6d2af70c92ab | 343 | uint8_t data[8]; |
darienf | 20:6d2af70c92ab | 344 | wait_1mS(); |
darienf | 20:6d2af70c92ab | 345 | // CR2V Configuration Register-2 Volatile |
darienf | 20:6d2af70c92ab | 346 | // bit 5 |
darienf | 20:6d2af70c92ab | 347 | readAnyRegister(0x00800003, data, 8); |
darienf | 20:6d2af70c92ab | 348 | writeAnyRegister(0x00800003, 0x64); |
darienf | 20:6d2af70c92ab | 349 | return 0; |
darienf | 20:6d2af70c92ab | 350 | } |
darienf | 20:6d2af70c92ab | 351 | |
darienf | 20:6d2af70c92ab | 352 | //****************************************************************************** |
darienf | 20:6d2af70c92ab | 353 | uint8_t S25FS512::detect(void) { |
darienf | 20:6d2af70c92ab | 354 | uint8_t array[8]; |
darienf | 20:6d2af70c92ab | 355 | uint8_t array2[8]; |
darienf | 20:6d2af70c92ab | 356 | |
darienf | 20:6d2af70c92ab | 357 | // Send WREN |
darienf | 20:6d2af70c92ab | 358 | wren(); |
darienf | 20:6d2af70c92ab | 359 | // Send WREN |
darienf | 20:6d2af70c92ab | 360 | wren(); |
darienf | 20:6d2af70c92ab | 361 | // delay |
darienf | 20:6d2af70c92ab | 362 | wait_1mS(); |
darienf | 20:6d2af70c92ab | 363 | // Send WREN |
darienf | 20:6d2af70c92ab | 364 | wren(); |
darienf | 20:6d2af70c92ab | 365 | // delay |
darienf | 20:6d2af70c92ab | 366 | wait_1mS(); |
darienf | 20:6d2af70c92ab | 367 | |
darienf | 20:6d2af70c92ab | 368 | // Send write any register cmd |
darienf | 20:6d2af70c92ab | 369 | writeAnyRegister(0x0003, 0x48); |
darienf | 20:6d2af70c92ab | 370 | // delay |
darienf | 20:6d2af70c92ab | 371 | wait_1mS(); |
darienf | 20:6d2af70c92ab | 372 | array[0] = 0x9F; // read ID command |
darienf | 20:6d2af70c92ab | 373 | reg_write_read_multiple_quad(array, 1, array2, 7); |
darienf | 20:6d2af70c92ab | 374 | return 0; |
darienf | 20:6d2af70c92ab | 375 | } |
darienf | 20:6d2af70c92ab | 376 | |
darienf | 20:6d2af70c92ab | 377 | //****************************************************************************** |
darienf | 20:6d2af70c92ab | 378 | int S25FS512::setQuadMode(void) { |
darienf | 20:6d2af70c92ab | 379 | wait_1mS(); |
darienf | 20:6d2af70c92ab | 380 | wren4Wire(); |
darienf | 20:6d2af70c92ab | 381 | wait_1mS(); |
darienf | 20:6d2af70c92ab | 382 | writeAnyRegister4Wire(0x800002, 0x02); // set Quad = 1 |
darienf | 20:6d2af70c92ab | 383 | wait_1mS(); |
darienf | 20:6d2af70c92ab | 384 | wren4Wire(); |
darienf | 20:6d2af70c92ab | 385 | wait_1mS(); |
darienf | 20:6d2af70c92ab | 386 | writeAnyRegister4Wire(0x800003, 0x48); // set 8 latency, set QPI 4-4-4 |
darienf | 20:6d2af70c92ab | 387 | } |
darienf | 20:6d2af70c92ab | 388 | |
darienf | 20:6d2af70c92ab | 389 | //****************************************************************************** |
darienf | 20:6d2af70c92ab | 390 | uint32_t S25FS512::isPageEmpty(uint8_t *ptr) { |
darienf | 20:6d2af70c92ab | 391 | int i; |
darienf | 20:6d2af70c92ab | 392 | for (i = 0; i < 256; i++) { |
darienf | 20:6d2af70c92ab | 393 | if (ptr[i] != 0xFF) |
darienf | 20:6d2af70c92ab | 394 | return 0; |
darienf | 20:6d2af70c92ab | 395 | } |
darienf | 20:6d2af70c92ab | 396 | return 1; |
darienf | 20:6d2af70c92ab | 397 | } |
darienf | 20:6d2af70c92ab | 398 | |
darienf | 20:6d2af70c92ab | 399 | //****************************************************************************** |
darienf | 20:6d2af70c92ab | 400 | int8_t S25FS512::parameterSectorErase_Helper(uint32_t address) { |
darienf | 20:6d2af70c92ab | 401 | waitTillNotBusy(); |
darienf | 20:6d2af70c92ab | 402 | wait_100uS(); |
darienf | 20:6d2af70c92ab | 403 | wren(); |
darienf | 20:6d2af70c92ab | 404 | wait_100uS(); |
darienf | 20:6d2af70c92ab | 405 | parameterSectorErase(address); |
darienf | 20:6d2af70c92ab | 406 | wait_100uS(); |
darienf | 20:6d2af70c92ab | 407 | waitTillNotBusy(); |
darienf | 20:6d2af70c92ab | 408 | wait_100uS(); |
darienf | 20:6d2af70c92ab | 409 | return 0; |
darienf | 20:6d2af70c92ab | 410 | } |
darienf | 20:6d2af70c92ab | 411 | |
darienf | 20:6d2af70c92ab | 412 | //****************************************************************************** |
darienf | 20:6d2af70c92ab | 413 | int8_t S25FS512::sectorErase_Helper(uint32_t address) { |
darienf | 20:6d2af70c92ab | 414 | waitTillNotBusy(); |
darienf | 20:6d2af70c92ab | 415 | wait_100uS(); |
darienf | 20:6d2af70c92ab | 416 | wren(); |
darienf | 20:6d2af70c92ab | 417 | wait_100uS(); |
darienf | 20:6d2af70c92ab | 418 | if (address < 0x8000) { |
darienf | 20:6d2af70c92ab | 419 | parameterSectorErase(address); |
darienf | 20:6d2af70c92ab | 420 | } else { |
darienf | 20:6d2af70c92ab | 421 | sectorErase(address); |
darienf | 20:6d2af70c92ab | 422 | } |
darienf | 20:6d2af70c92ab | 423 | wait_100uS(); |
darienf | 20:6d2af70c92ab | 424 | waitTillNotBusy(); |
darienf | 20:6d2af70c92ab | 425 | wait_100uS(); |
darienf | 20:6d2af70c92ab | 426 | return 0; |
darienf | 20:6d2af70c92ab | 427 | } |
darienf | 20:6d2af70c92ab | 428 | |
darienf | 20:6d2af70c92ab | 429 | //****************************************************************************** |
darienf | 20:6d2af70c92ab | 430 | int8_t S25FS512::bulkErase_Helper(void) { |
darienf | 20:6d2af70c92ab | 431 | waitTillNotBusy(); |
darienf | 20:6d2af70c92ab | 432 | wait_100uS(); |
darienf | 20:6d2af70c92ab | 433 | wren(); |
darienf | 20:6d2af70c92ab | 434 | wait_100uS(); |
darienf | 20:6d2af70c92ab | 435 | bulkErase(); |
darienf | 20:6d2af70c92ab | 436 | wait_100uS(); |
darienf | 20:6d2af70c92ab | 437 | waitTillNotBusy(); |
darienf | 20:6d2af70c92ab | 438 | wait_100uS(); |
darienf | 20:6d2af70c92ab | 439 | return 0; |
darienf | 20:6d2af70c92ab | 440 | } |
darienf | 20:6d2af70c92ab | 441 | |
darienf | 20:6d2af70c92ab | 442 | //****************************************************************************** |
darienf | 20:6d2af70c92ab | 443 | // write a page worth of data (256 bytes) from buffer, offset defined where in |
darienf | 20:6d2af70c92ab | 444 | // the buffer to begin write |
darienf | 20:6d2af70c92ab | 445 | int8_t S25FS512::writePage_Helper(uint32_t pageNumber, uint8_t *buffer, |
darienf | 20:6d2af70c92ab | 446 | uint32_t offset) { |
darienf | 20:6d2af70c92ab | 447 | uint8_t *ptr; |
darienf | 20:6d2af70c92ab | 448 | waitTillNotBusy(); |
darienf | 20:6d2af70c92ab | 449 | wait_1mS(); |
darienf | 20:6d2af70c92ab | 450 | wren(); |
darienf | 20:6d2af70c92ab | 451 | ptr = &buffer[offset]; |
darienf | 20:6d2af70c92ab | 452 | wait_1mS(); |
darienf | 20:6d2af70c92ab | 453 | pageProgram(pageNumber << 8, ptr); |
darienf | 20:6d2af70c92ab | 454 | wait_1mS(); |
darienf | 20:6d2af70c92ab | 455 | return 0; |
darienf | 20:6d2af70c92ab | 456 | } |
darienf | 20:6d2af70c92ab | 457 | |
darienf | 20:6d2af70c92ab | 458 | //****************************************************************************** |
darienf | 20:6d2af70c92ab | 459 | // read pages from flash into buffer, offset defined where in the buffer use |
darienf | 20:6d2af70c92ab | 460 | int8_t S25FS512::readPages_Helper(uint32_t startPageNumber, |
darienf | 20:6d2af70c92ab | 461 | uint32_t endPageNumber, uint8_t *buffer, |
darienf | 20:6d2af70c92ab | 462 | uint32_t offset) { |
darienf | 20:6d2af70c92ab | 463 | uint8_t *ptr; |
darienf | 20:6d2af70c92ab | 464 | uint32_t page; |
darienf | 20:6d2af70c92ab | 465 | ptr = &buffer[offset]; |
darienf | 20:6d2af70c92ab | 466 | for (page = startPageNumber; page <= endPageNumber; page++) { |
darienf | 20:6d2af70c92ab | 467 | wait_100uS(); |
darienf | 20:6d2af70c92ab | 468 | quadIoRead_Pages((uint32_t)(page << 8), (uint8_t *)ptr, 1); |
darienf | 20:6d2af70c92ab | 469 | ptr += 0x100; |
darienf | 20:6d2af70c92ab | 470 | } |
darienf | 20:6d2af70c92ab | 471 | return 0; |
darienf | 20:6d2af70c92ab | 472 | } |