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