test
Fork of mbed-dev by
targets/TARGET_Realtek/TARGET_AMEBA/flash_ext.c@171:19eb464bc2be, 2017-08-03 (annotated)
- Committer:
- Kojto
- Date:
- Thu Aug 03 13:13:39 2017 +0100
- Revision:
- 171:19eb464bc2be
- Parent:
- 169:9672193075cf
This updates the lib to the mbed lib v 148
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AnnaBridge | 169:9672193075cf | 1 | /* mbed Microcontroller Library |
AnnaBridge | 169:9672193075cf | 2 | * Copyright (c) 2013-2016 Realtek Semiconductor Corp. |
AnnaBridge | 169:9672193075cf | 3 | * |
AnnaBridge | 169:9672193075cf | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
AnnaBridge | 169:9672193075cf | 5 | * you may not use this file except in compliance with the License. |
AnnaBridge | 169:9672193075cf | 6 | * You may obtain a copy of the License at |
AnnaBridge | 169:9672193075cf | 7 | * |
AnnaBridge | 169:9672193075cf | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
AnnaBridge | 169:9672193075cf | 9 | * |
AnnaBridge | 169:9672193075cf | 10 | * Unless required by applicable law or agreed to in writing, software |
AnnaBridge | 169:9672193075cf | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
AnnaBridge | 169:9672193075cf | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
AnnaBridge | 169:9672193075cf | 13 | * See the License for the specific language governing permissions and |
AnnaBridge | 169:9672193075cf | 14 | * limitations under the License. |
AnnaBridge | 169:9672193075cf | 15 | */ |
AnnaBridge | 169:9672193075cf | 16 | #include "objects.h" |
AnnaBridge | 169:9672193075cf | 17 | #include "PinNames.h" |
AnnaBridge | 169:9672193075cf | 18 | #include "pinmap.h" |
AnnaBridge | 169:9672193075cf | 19 | |
AnnaBridge | 169:9672193075cf | 20 | #include "rtl8195a.h" |
AnnaBridge | 169:9672193075cf | 21 | #include "flash_ext.h" |
AnnaBridge | 169:9672193075cf | 22 | |
AnnaBridge | 169:9672193075cf | 23 | extern u32 ConfigDebugInfo; |
AnnaBridge | 169:9672193075cf | 24 | extern SPIC_INIT_PARA SpicInitParaAllClk[3][CPU_CLK_TYPE_NO]; |
AnnaBridge | 169:9672193075cf | 25 | |
AnnaBridge | 169:9672193075cf | 26 | static int flash_inited = 0; |
AnnaBridge | 169:9672193075cf | 27 | static flash_t flashobj; |
AnnaBridge | 169:9672193075cf | 28 | static void flash_ext_init(void) |
AnnaBridge | 169:9672193075cf | 29 | { |
AnnaBridge | 169:9672193075cf | 30 | if (!SpicFlashInitRtl8195A(SpicOneBitMode)){ |
AnnaBridge | 169:9672193075cf | 31 | HAL_WRITE32(SYSTEM_CTRL_BASE, REG_SYS_DSTBY_INFO3, HAL_READ32(SYSTEM_CTRL_BASE, REG_SYS_DSTBY_INFO3)|0xf); |
AnnaBridge | 169:9672193075cf | 32 | } else { |
AnnaBridge | 169:9672193075cf | 33 | flash_inited = 1; |
AnnaBridge | 169:9672193075cf | 34 | } |
AnnaBridge | 169:9672193075cf | 35 | flashobj.SpicInitPara.flashtype = SpicInitParaAllClk[0][0].flashtype; |
AnnaBridge | 169:9672193075cf | 36 | } |
AnnaBridge | 169:9672193075cf | 37 | |
AnnaBridge | 169:9672193075cf | 38 | void __flash_ext_turnon(void) |
AnnaBridge | 169:9672193075cf | 39 | { |
AnnaBridge | 169:9672193075cf | 40 | SPI_FLASH_PIN_FCTRL(ON); |
AnnaBridge | 169:9672193075cf | 41 | SpicWaitBusyDoneRtl8195A(); |
AnnaBridge | 169:9672193075cf | 42 | |
AnnaBridge | 169:9672193075cf | 43 | if (flash_inited == 0) { |
AnnaBridge | 169:9672193075cf | 44 | flash_ext_init(); |
AnnaBridge | 169:9672193075cf | 45 | } |
AnnaBridge | 169:9672193075cf | 46 | } |
AnnaBridge | 169:9672193075cf | 47 | |
AnnaBridge | 169:9672193075cf | 48 | void __flash_ext_turnoff(void) |
AnnaBridge | 169:9672193075cf | 49 | { |
AnnaBridge | 169:9672193075cf | 50 | SpicDisableRtl8195A(); |
AnnaBridge | 169:9672193075cf | 51 | } |
AnnaBridge | 169:9672193075cf | 52 | |
AnnaBridge | 169:9672193075cf | 53 | void flash_ext_write_protect(flash_t *obj, uint32_t protect) |
AnnaBridge | 169:9672193075cf | 54 | { |
AnnaBridge | 169:9672193075cf | 55 | __flash_ext_turnon(); |
AnnaBridge | 169:9672193075cf | 56 | SpicWriteProtectFlashRtl8195A(protect); |
AnnaBridge | 169:9672193075cf | 57 | __flash_ext_turnoff(); |
AnnaBridge | 169:9672193075cf | 58 | } |
AnnaBridge | 169:9672193075cf | 59 | |
AnnaBridge | 169:9672193075cf | 60 | void __flash_ext_erase_sector(flash_t *obj, uint32_t address) |
AnnaBridge | 169:9672193075cf | 61 | { |
AnnaBridge | 169:9672193075cf | 62 | SpicSectorEraseFlashRtl8195A(SPI_FLASH_BASE+address); |
AnnaBridge | 169:9672193075cf | 63 | } |
AnnaBridge | 169:9672193075cf | 64 | |
AnnaBridge | 169:9672193075cf | 65 | void flash_ext_erase_sector(flash_t *obj, uint32_t address) |
AnnaBridge | 169:9672193075cf | 66 | { |
AnnaBridge | 169:9672193075cf | 67 | __flash_ext_turnon(); |
AnnaBridge | 169:9672193075cf | 68 | __flash_ext_erase_sector(obj, address); |
AnnaBridge | 169:9672193075cf | 69 | __flash_ext_turnoff(); |
AnnaBridge | 169:9672193075cf | 70 | } |
AnnaBridge | 169:9672193075cf | 71 | |
AnnaBridge | 169:9672193075cf | 72 | void flash_ext_erase_block(flash_t *obj, uint32_t address) |
AnnaBridge | 169:9672193075cf | 73 | { |
AnnaBridge | 169:9672193075cf | 74 | __flash_ext_turnon(); |
AnnaBridge | 169:9672193075cf | 75 | SpicBlockEraseFlashRtl8195A(SPI_FLASH_BASE+address); |
AnnaBridge | 169:9672193075cf | 76 | __flash_ext_turnoff(); |
AnnaBridge | 169:9672193075cf | 77 | } |
AnnaBridge | 169:9672193075cf | 78 | |
AnnaBridge | 169:9672193075cf | 79 | int flash_ext_read_word(flash_t *obj, uint32_t address, uint32_t *data) |
AnnaBridge | 169:9672193075cf | 80 | { |
AnnaBridge | 169:9672193075cf | 81 | __flash_ext_turnon(); |
AnnaBridge | 169:9672193075cf | 82 | |
AnnaBridge | 169:9672193075cf | 83 | SpicWaitWipDoneRefinedRtl8195A(flashobj.SpicInitPara); |
AnnaBridge | 169:9672193075cf | 84 | *data = HAL_READ32(SPI_FLASH_BASE, address); |
AnnaBridge | 169:9672193075cf | 85 | __flash_ext_turnoff(); |
AnnaBridge | 169:9672193075cf | 86 | return 0; |
AnnaBridge | 169:9672193075cf | 87 | } |
AnnaBridge | 169:9672193075cf | 88 | |
AnnaBridge | 169:9672193075cf | 89 | /** |
AnnaBridge | 169:9672193075cf | 90 | * @brief Write a word to specified address |
AnnaBridge | 169:9672193075cf | 91 | * @param obj: Specifies the parameter of flash object. |
AnnaBridge | 169:9672193075cf | 92 | * @param address: Specifies the address to be programmed. |
AnnaBridge | 169:9672193075cf | 93 | * @param data: Specified the data to be programmed. |
AnnaBridge | 169:9672193075cf | 94 | * @retval status: Success:1 or Failure: Others. |
AnnaBridge | 169:9672193075cf | 95 | */ |
AnnaBridge | 169:9672193075cf | 96 | int flash_ext_write_word(flash_t *obj, uint32_t address, uint32_t data) |
AnnaBridge | 169:9672193075cf | 97 | { |
AnnaBridge | 169:9672193075cf | 98 | u8 flashtype = 0; |
AnnaBridge | 169:9672193075cf | 99 | __flash_ext_turnon(); |
AnnaBridge | 169:9672193075cf | 100 | |
AnnaBridge | 169:9672193075cf | 101 | flashtype = flashobj.SpicInitPara.flashtype; |
AnnaBridge | 169:9672193075cf | 102 | HAL_WRITE32(SPI_FLASH_BASE, address, data); |
AnnaBridge | 169:9672193075cf | 103 | SpicWaitBusyDoneRtl8195A(); |
AnnaBridge | 169:9672193075cf | 104 | |
AnnaBridge | 169:9672193075cf | 105 | if(flashtype == FLASH_MICRON){ |
AnnaBridge | 169:9672193075cf | 106 | SpicWaitOperationDoneRtl8195A(flashobj.SpicInitPara); |
AnnaBridge | 169:9672193075cf | 107 | } else { |
AnnaBridge | 169:9672193075cf | 108 | SpicWaitWipDoneRefinedRtl8195A(flashobj.SpicInitPara); |
AnnaBridge | 169:9672193075cf | 109 | } |
AnnaBridge | 169:9672193075cf | 110 | |
AnnaBridge | 169:9672193075cf | 111 | __flash_ext_turnoff(); |
AnnaBridge | 169:9672193075cf | 112 | return 0; |
AnnaBridge | 169:9672193075cf | 113 | } |
AnnaBridge | 169:9672193075cf | 114 | |
AnnaBridge | 169:9672193075cf | 115 | /** |
AnnaBridge | 169:9672193075cf | 116 | * @brief Read a stream of data from specified address |
AnnaBridge | 169:9672193075cf | 117 | * @param obj: Specifies the parameter of flash object. |
AnnaBridge | 169:9672193075cf | 118 | * @param address: Specifies the address to be read. |
AnnaBridge | 169:9672193075cf | 119 | * @param len: Specifies the length of the data to read. |
AnnaBridge | 169:9672193075cf | 120 | * @param data: Specified the address to save the readback data. |
AnnaBridge | 169:9672193075cf | 121 | * @retval status: Success:1 or Failure: Others. |
AnnaBridge | 169:9672193075cf | 122 | */ |
AnnaBridge | 169:9672193075cf | 123 | int flash_ext_stream_read(flash_t *obj, uint32_t addr, uint32_t len, uint8_t *data) |
AnnaBridge | 169:9672193075cf | 124 | { |
AnnaBridge | 169:9672193075cf | 125 | uint32_t i, offset, word; |
AnnaBridge | 169:9672193075cf | 126 | uint8_t *ptr, *pbuf; |
AnnaBridge | 169:9672193075cf | 127 | |
AnnaBridge | 169:9672193075cf | 128 | __flash_ext_turnon(); |
AnnaBridge | 169:9672193075cf | 129 | |
AnnaBridge | 169:9672193075cf | 130 | SpicWaitWipDoneRefinedRtl8195A(flashobj.SpicInitPara); |
Kojto | 171:19eb464bc2be | 131 | |
AnnaBridge | 169:9672193075cf | 132 | offset = addr & 0x03; |
AnnaBridge | 169:9672193075cf | 133 | addr = addr & ~0x03; |
AnnaBridge | 169:9672193075cf | 134 | pbuf = data; |
AnnaBridge | 169:9672193075cf | 135 | if (offset != 0) { |
AnnaBridge | 169:9672193075cf | 136 | word = HAL_READ32(SPI_FLASH_BASE, addr); |
AnnaBridge | 169:9672193075cf | 137 | ptr = (uint8_t *)&word + offset; |
AnnaBridge | 169:9672193075cf | 138 | offset = 4 - offset; |
AnnaBridge | 169:9672193075cf | 139 | for (i = 0; i < offset && len > 0; i++, len--) { |
AnnaBridge | 169:9672193075cf | 140 | *pbuf = *ptr; |
AnnaBridge | 169:9672193075cf | 141 | pbuf++; |
AnnaBridge | 169:9672193075cf | 142 | ptr++; |
AnnaBridge | 169:9672193075cf | 143 | } |
AnnaBridge | 169:9672193075cf | 144 | addr += 4; |
AnnaBridge | 169:9672193075cf | 145 | } |
AnnaBridge | 169:9672193075cf | 146 | |
AnnaBridge | 169:9672193075cf | 147 | ptr = (uint8_t *)&word; |
AnnaBridge | 169:9672193075cf | 148 | if ((uint32_t)pbuf & 0x03) { |
AnnaBridge | 169:9672193075cf | 149 | while (len >= 4) { |
AnnaBridge | 169:9672193075cf | 150 | word = HAL_READ32(SPI_FLASH_BASE, addr); |
AnnaBridge | 169:9672193075cf | 151 | for (i = 0; i < 4; i++) { |
AnnaBridge | 169:9672193075cf | 152 | *pbuf = *(ptr+i); |
AnnaBridge | 169:9672193075cf | 153 | pbuf++; |
AnnaBridge | 169:9672193075cf | 154 | } |
AnnaBridge | 169:9672193075cf | 155 | addr += 4; |
AnnaBridge | 169:9672193075cf | 156 | len -= 4; |
AnnaBridge | 169:9672193075cf | 157 | } |
AnnaBridge | 169:9672193075cf | 158 | } else { |
AnnaBridge | 169:9672193075cf | 159 | while (len >= 4) { |
AnnaBridge | 169:9672193075cf | 160 | *((uint32_t *)pbuf) = HAL_READ32(SPI_FLASH_BASE, addr); |
AnnaBridge | 169:9672193075cf | 161 | pbuf += 4; |
AnnaBridge | 169:9672193075cf | 162 | addr += 4; |
AnnaBridge | 169:9672193075cf | 163 | len -= 4; |
AnnaBridge | 169:9672193075cf | 164 | } |
AnnaBridge | 169:9672193075cf | 165 | } |
AnnaBridge | 169:9672193075cf | 166 | |
AnnaBridge | 169:9672193075cf | 167 | if (len > 0) { |
AnnaBridge | 169:9672193075cf | 168 | word = HAL_READ32(SPI_FLASH_BASE, addr); |
AnnaBridge | 169:9672193075cf | 169 | for (i = 0; i < len; i++) { |
AnnaBridge | 169:9672193075cf | 170 | *pbuf = *(ptr+i); |
AnnaBridge | 169:9672193075cf | 171 | pbuf++; |
AnnaBridge | 169:9672193075cf | 172 | } |
AnnaBridge | 169:9672193075cf | 173 | } |
AnnaBridge | 169:9672193075cf | 174 | |
AnnaBridge | 169:9672193075cf | 175 | __flash_ext_turnoff(); |
AnnaBridge | 169:9672193075cf | 176 | return 0; |
AnnaBridge | 169:9672193075cf | 177 | } |
AnnaBridge | 169:9672193075cf | 178 | |
AnnaBridge | 169:9672193075cf | 179 | /** |
AnnaBridge | 169:9672193075cf | 180 | * @brief Write a stream of data to specified address |
AnnaBridge | 169:9672193075cf | 181 | * @param obj: Specifies the parameter of flash object. |
AnnaBridge | 169:9672193075cf | 182 | * @param address: Specifies the address to be read. |
AnnaBridge | 169:9672193075cf | 183 | * @param len: Specifies the length of the data to write. |
AnnaBridge | 169:9672193075cf | 184 | * @param data: Specified the pointer of the data to be written. |
AnnaBridge | 169:9672193075cf | 185 | * @retval status: Success:1 or Failure: Others. |
AnnaBridge | 169:9672193075cf | 186 | */ |
AnnaBridge | 169:9672193075cf | 187 | int __flash_ext_stream_write(flash_t *obj, uint32_t addr, uint32_t len, const uint8_t *data) |
AnnaBridge | 169:9672193075cf | 188 | { |
AnnaBridge | 169:9672193075cf | 189 | uint32_t i, offset, word; |
AnnaBridge | 169:9672193075cf | 190 | const uint8_t*pbuf; |
AnnaBridge | 169:9672193075cf | 191 | uint8_t *ptr; |
Kojto | 171:19eb464bc2be | 192 | u8 flashtype = 0; |
AnnaBridge | 169:9672193075cf | 193 | |
AnnaBridge | 169:9672193075cf | 194 | offset = addr & 0x03; |
AnnaBridge | 169:9672193075cf | 195 | addr = addr & ~0x03; |
AnnaBridge | 169:9672193075cf | 196 | pbuf = data; |
AnnaBridge | 169:9672193075cf | 197 | flashtype = flashobj.SpicInitPara.flashtype; |
Kojto | 171:19eb464bc2be | 198 | |
AnnaBridge | 169:9672193075cf | 199 | if (offset != 0) { |
AnnaBridge | 169:9672193075cf | 200 | word = HAL_READ32(SPI_FLASH_BASE, addr); |
AnnaBridge | 169:9672193075cf | 201 | ptr = (uint8_t *)&word + offset; |
AnnaBridge | 169:9672193075cf | 202 | offset = 4 - offset; |
AnnaBridge | 169:9672193075cf | 203 | for (i = 0; i < offset && len > 0; i++, len--) { |
AnnaBridge | 169:9672193075cf | 204 | *ptr = *pbuf; |
AnnaBridge | 169:9672193075cf | 205 | pbuf++; |
AnnaBridge | 169:9672193075cf | 206 | ptr++; |
AnnaBridge | 169:9672193075cf | 207 | } |
AnnaBridge | 169:9672193075cf | 208 | HAL_WRITE32(SPI_FLASH_BASE, addr, word); |
AnnaBridge | 169:9672193075cf | 209 | SpicWaitBusyDoneRtl8195A(); |
Kojto | 171:19eb464bc2be | 210 | |
AnnaBridge | 169:9672193075cf | 211 | if(flashtype == FLASH_MICRON){ |
AnnaBridge | 169:9672193075cf | 212 | SpicWaitOperationDoneRtl8195A(flashobj.SpicInitPara); |
AnnaBridge | 169:9672193075cf | 213 | } else { |
AnnaBridge | 169:9672193075cf | 214 | SpicWaitWipDoneRefinedRtl8195A(flashobj.SpicInitPara); |
AnnaBridge | 169:9672193075cf | 215 | } |
AnnaBridge | 169:9672193075cf | 216 | addr += 4; |
AnnaBridge | 169:9672193075cf | 217 | } |
AnnaBridge | 169:9672193075cf | 218 | |
AnnaBridge | 169:9672193075cf | 219 | if ((uint32_t)pbuf & 0x03) { |
AnnaBridge | 169:9672193075cf | 220 | while (len >= 4) { |
AnnaBridge | 169:9672193075cf | 221 | word = (uint32_t)(*pbuf) | ((uint32_t)(*(pbuf+1)) << 8)| |
AnnaBridge | 169:9672193075cf | 222 | ((uint32_t)(*(pbuf+2)) << 16) | ((uint32_t)(*(pbuf+3)) << 24); |
AnnaBridge | 169:9672193075cf | 223 | HAL_WRITE32(SPI_FLASH_BASE, addr, word); |
AnnaBridge | 169:9672193075cf | 224 | SpicWaitBusyDoneRtl8195A(); |
Kojto | 171:19eb464bc2be | 225 | |
AnnaBridge | 169:9672193075cf | 226 | if(flashtype == FLASH_MICRON){ |
AnnaBridge | 169:9672193075cf | 227 | SpicWaitOperationDoneRtl8195A(flashobj.SpicInitPara); |
AnnaBridge | 169:9672193075cf | 228 | } else { |
AnnaBridge | 169:9672193075cf | 229 | SpicWaitWipDoneRefinedRtl8195A(flashobj.SpicInitPara); |
AnnaBridge | 169:9672193075cf | 230 | } |
AnnaBridge | 169:9672193075cf | 231 | |
AnnaBridge | 169:9672193075cf | 232 | pbuf += 4; |
AnnaBridge | 169:9672193075cf | 233 | addr += 4; |
AnnaBridge | 169:9672193075cf | 234 | len -= 4; |
AnnaBridge | 169:9672193075cf | 235 | } |
AnnaBridge | 169:9672193075cf | 236 | } else { |
AnnaBridge | 169:9672193075cf | 237 | while (len >= 4) { |
AnnaBridge | 169:9672193075cf | 238 | HAL_WRITE32(SPI_FLASH_BASE, addr, (uint32_t)*((uint32_t *)pbuf)); |
AnnaBridge | 169:9672193075cf | 239 | SpicWaitBusyDoneRtl8195A(); |
Kojto | 171:19eb464bc2be | 240 | |
AnnaBridge | 169:9672193075cf | 241 | if(flashtype == FLASH_MICRON){ |
AnnaBridge | 169:9672193075cf | 242 | SpicWaitOperationDoneRtl8195A(flashobj.SpicInitPara); |
AnnaBridge | 169:9672193075cf | 243 | } else { |
AnnaBridge | 169:9672193075cf | 244 | SpicWaitWipDoneRefinedRtl8195A(flashobj.SpicInitPara); |
AnnaBridge | 169:9672193075cf | 245 | } |
AnnaBridge | 169:9672193075cf | 246 | |
AnnaBridge | 169:9672193075cf | 247 | pbuf += 4; |
AnnaBridge | 169:9672193075cf | 248 | addr += 4; |
AnnaBridge | 169:9672193075cf | 249 | len -= 4; |
AnnaBridge | 169:9672193075cf | 250 | } |
AnnaBridge | 169:9672193075cf | 251 | } |
AnnaBridge | 169:9672193075cf | 252 | |
AnnaBridge | 169:9672193075cf | 253 | if (len > 0) { |
AnnaBridge | 169:9672193075cf | 254 | word = HAL_READ32(SPI_FLASH_BASE, addr); |
AnnaBridge | 169:9672193075cf | 255 | ptr = (uint8_t*)&word; |
AnnaBridge | 169:9672193075cf | 256 | for (i = 0; i < len; i++) { |
AnnaBridge | 169:9672193075cf | 257 | *(ptr+i) = *pbuf; |
AnnaBridge | 169:9672193075cf | 258 | pbuf++; |
AnnaBridge | 169:9672193075cf | 259 | } |
AnnaBridge | 169:9672193075cf | 260 | |
AnnaBridge | 169:9672193075cf | 261 | HAL_WRITE32(SPI_FLASH_BASE, addr, word); |
AnnaBridge | 169:9672193075cf | 262 | SpicWaitBusyDoneRtl8195A(); |
Kojto | 171:19eb464bc2be | 263 | |
AnnaBridge | 169:9672193075cf | 264 | if(flashtype == FLASH_MICRON){ |
AnnaBridge | 169:9672193075cf | 265 | SpicWaitOperationDoneRtl8195A(flashobj.SpicInitPara); |
AnnaBridge | 169:9672193075cf | 266 | } else { |
AnnaBridge | 169:9672193075cf | 267 | SpicWaitWipDoneRefinedRtl8195A(flashobj.SpicInitPara); |
AnnaBridge | 169:9672193075cf | 268 | } |
AnnaBridge | 169:9672193075cf | 269 | } |
AnnaBridge | 169:9672193075cf | 270 | |
AnnaBridge | 169:9672193075cf | 271 | return 0; |
AnnaBridge | 169:9672193075cf | 272 | } |
AnnaBridge | 169:9672193075cf | 273 | |
Kojto | 171:19eb464bc2be | 274 | int flash_ext_stream_write(flash_t *obj, uint32_t addr, uint32_t len, const uint8_t *data) |
AnnaBridge | 169:9672193075cf | 275 | { |
AnnaBridge | 169:9672193075cf | 276 | int32_t status; |
AnnaBridge | 169:9672193075cf | 277 | |
AnnaBridge | 169:9672193075cf | 278 | __flash_ext_turnon(); |
AnnaBridge | 169:9672193075cf | 279 | status = __flash_ext_stream_write(obj, addr, len, data); |
AnnaBridge | 169:9672193075cf | 280 | __flash_ext_turnoff(); |
AnnaBridge | 169:9672193075cf | 281 | |
AnnaBridge | 169:9672193075cf | 282 | return status; |
AnnaBridge | 169:9672193075cf | 283 | } |
AnnaBridge | 169:9672193075cf | 284 | |
AnnaBridge | 169:9672193075cf | 285 | int flash_stream_read(flash_t *obj, uint32_t addr, uint32_t len, uint8_t *data) |
AnnaBridge | 169:9672193075cf | 286 | { |
AnnaBridge | 169:9672193075cf | 287 | return flash_ext_stream_read(obj, addr, len, data); |
AnnaBridge | 169:9672193075cf | 288 | } |
AnnaBridge | 169:9672193075cf | 289 | |
Kojto | 171:19eb464bc2be | 290 | int flash_stream_write(flash_t *obj, uint32_t addr, uint32_t len, const uint8_t *data) |
AnnaBridge | 169:9672193075cf | 291 | { |
AnnaBridge | 169:9672193075cf | 292 | return flash_ext_stream_write(obj, addr, len, data); |
AnnaBridge | 169:9672193075cf | 293 | } |
AnnaBridge | 169:9672193075cf | 294 | |
AnnaBridge | 169:9672193075cf | 295 | /* |
AnnaBridge | 169:9672193075cf | 296 | Function Description: |
AnnaBridge | 169:9672193075cf | 297 | This function performans the same functionality as the function flash_stream_write. |
AnnaBridge | 169:9672193075cf | 298 | It enhances write performance by reducing overheads. |
AnnaBridge | 169:9672193075cf | 299 | Users can use either of functions depending on their needs. |
AnnaBridge | 169:9672193075cf | 300 | |
AnnaBridge | 169:9672193075cf | 301 | * @brief Write a stream of data to specified address |
AnnaBridge | 169:9672193075cf | 302 | * @param obj: Specifies the parameter of flash object. |
AnnaBridge | 169:9672193075cf | 303 | * @param address: Specifies the address to be read. |
AnnaBridge | 169:9672193075cf | 304 | * @param length: Specifies the length of the data to write. |
AnnaBridge | 169:9672193075cf | 305 | * @param data: Specified the pointer of the data to be written. |
AnnaBridge | 169:9672193075cf | 306 | * @retval status: Success:1 or Failure: Others. |
AnnaBridge | 169:9672193075cf | 307 | |
AnnaBridge | 169:9672193075cf | 308 | */ |
AnnaBridge | 169:9672193075cf | 309 | |
AnnaBridge | 169:9672193075cf | 310 | int flash_ext_burst_write(flash_t *obj, uint32_t address ,uint32_t length, uint8_t *data) |
Kojto | 171:19eb464bc2be | 311 | { |
AnnaBridge | 169:9672193075cf | 312 | u32 OccuSize; |
AnnaBridge | 169:9672193075cf | 313 | u32 ProgramSize; |
AnnaBridge | 169:9672193075cf | 314 | u32 PageSize; |
AnnaBridge | 169:9672193075cf | 315 | u8 flashtype = 0; |
AnnaBridge | 169:9672193075cf | 316 | |
AnnaBridge | 169:9672193075cf | 317 | PageSize = 256; |
AnnaBridge | 169:9672193075cf | 318 | |
AnnaBridge | 169:9672193075cf | 319 | __flash_ext_turnon(); |
AnnaBridge | 169:9672193075cf | 320 | |
AnnaBridge | 169:9672193075cf | 321 | flashtype = flashobj.SpicInitPara.flashtype; |
AnnaBridge | 169:9672193075cf | 322 | OccuSize = address & 0xFF; |
AnnaBridge | 169:9672193075cf | 323 | if((length >= PageSize) ||((length + OccuSize) >= PageSize)){ |
AnnaBridge | 169:9672193075cf | 324 | ProgramSize = PageSize - OccuSize; |
AnnaBridge | 169:9672193075cf | 325 | } else { |
AnnaBridge | 169:9672193075cf | 326 | ProgramSize = length; |
AnnaBridge | 169:9672193075cf | 327 | } |
AnnaBridge | 169:9672193075cf | 328 | |
AnnaBridge | 169:9672193075cf | 329 | flashobj.Length = length; |
AnnaBridge | 169:9672193075cf | 330 | while(length > 0){ |
AnnaBridge | 169:9672193075cf | 331 | if(OccuSize){ |
AnnaBridge | 169:9672193075cf | 332 | SpicUserProgramRtl8195A(data, flashobj.SpicInitPara, address, &(flashobj.Length)); |
AnnaBridge | 169:9672193075cf | 333 | // Wait spic busy done |
AnnaBridge | 169:9672193075cf | 334 | SpicWaitBusyDoneRtl8195A(); |
AnnaBridge | 169:9672193075cf | 335 | // Wait flash busy done (wip=0) |
AnnaBridge | 169:9672193075cf | 336 | if(flashtype == FLASH_MICRON){ |
AnnaBridge | 169:9672193075cf | 337 | SpicWaitOperationDoneRtl8195A(flashobj.SpicInitPara); |
AnnaBridge | 169:9672193075cf | 338 | } else { |
AnnaBridge | 169:9672193075cf | 339 | SpicWaitWipDoneRefinedRtl8195A(flashobj.SpicInitPara); |
AnnaBridge | 169:9672193075cf | 340 | } |
AnnaBridge | 169:9672193075cf | 341 | |
AnnaBridge | 169:9672193075cf | 342 | address += ProgramSize; |
Kojto | 171:19eb464bc2be | 343 | data += ProgramSize; |
AnnaBridge | 169:9672193075cf | 344 | length -= ProgramSize; |
AnnaBridge | 169:9672193075cf | 345 | OccuSize = 0; |
AnnaBridge | 169:9672193075cf | 346 | } else{ |
AnnaBridge | 169:9672193075cf | 347 | while((flashobj.Length) >= PageSize){ |
AnnaBridge | 169:9672193075cf | 348 | SpicUserProgramRtl8195A(data, flashobj.SpicInitPara, address, &(flashobj.Length)); |
AnnaBridge | 169:9672193075cf | 349 | // Wait spic busy done |
AnnaBridge | 169:9672193075cf | 350 | SpicWaitBusyDoneRtl8195A(); |
AnnaBridge | 169:9672193075cf | 351 | // Wait flash busy done (wip=0) |
AnnaBridge | 169:9672193075cf | 352 | if(flashtype == FLASH_MICRON){ |
AnnaBridge | 169:9672193075cf | 353 | SpicWaitOperationDoneRtl8195A(flashobj.SpicInitPara); |
AnnaBridge | 169:9672193075cf | 354 | } else { |
AnnaBridge | 169:9672193075cf | 355 | SpicWaitWipDoneRefinedRtl8195A(flashobj.SpicInitPara); |
AnnaBridge | 169:9672193075cf | 356 | } |
AnnaBridge | 169:9672193075cf | 357 | |
AnnaBridge | 169:9672193075cf | 358 | address += PageSize; |
AnnaBridge | 169:9672193075cf | 359 | data += PageSize; |
AnnaBridge | 169:9672193075cf | 360 | length -= PageSize; |
AnnaBridge | 169:9672193075cf | 361 | } |
AnnaBridge | 169:9672193075cf | 362 | flashobj.Length = length; |
AnnaBridge | 169:9672193075cf | 363 | if((flashobj.Length) > 0){ |
AnnaBridge | 169:9672193075cf | 364 | SpicUserProgramRtl8195A(data, flashobj.SpicInitPara, address, &(flashobj.Length)); |
AnnaBridge | 169:9672193075cf | 365 | // Wait spic busy done |
AnnaBridge | 169:9672193075cf | 366 | SpicWaitBusyDoneRtl8195A(); |
AnnaBridge | 169:9672193075cf | 367 | // Wait flash busy done (wip=0) |
AnnaBridge | 169:9672193075cf | 368 | if(flashtype == FLASH_MICRON){ |
AnnaBridge | 169:9672193075cf | 369 | SpicWaitOperationDoneRtl8195A(flashobj.SpicInitPara); |
AnnaBridge | 169:9672193075cf | 370 | } else { |
AnnaBridge | 169:9672193075cf | 371 | SpicWaitWipDoneRefinedRtl8195A(flashobj.SpicInitPara); |
AnnaBridge | 169:9672193075cf | 372 | } |
AnnaBridge | 169:9672193075cf | 373 | |
AnnaBridge | 169:9672193075cf | 374 | break; |
AnnaBridge | 169:9672193075cf | 375 | } |
AnnaBridge | 169:9672193075cf | 376 | } |
AnnaBridge | 169:9672193075cf | 377 | flashobj.Length = length; |
AnnaBridge | 169:9672193075cf | 378 | } |
AnnaBridge | 169:9672193075cf | 379 | __flash_ext_turnoff(); |
AnnaBridge | 169:9672193075cf | 380 | return 0; |
AnnaBridge | 169:9672193075cf | 381 | |
AnnaBridge | 169:9672193075cf | 382 | } |
AnnaBridge | 169:9672193075cf | 383 | /** |
AnnaBridge | 169:9672193075cf | 384 | * @brief Read a stream of data from specified address |
AnnaBridge | 169:9672193075cf | 385 | * @param obj: Specifies the parameter of flash object. |
AnnaBridge | 169:9672193075cf | 386 | * @param address: Specifies the address to be read. |
AnnaBridge | 169:9672193075cf | 387 | * @param len: Specifies the length of the data to read. |
AnnaBridge | 169:9672193075cf | 388 | * @param data: Specified the address to save the readback data. |
AnnaBridge | 169:9672193075cf | 389 | * @retval status: Success:1 or Failure: Others. |
AnnaBridge | 169:9672193075cf | 390 | */ |
AnnaBridge | 169:9672193075cf | 391 | |
AnnaBridge | 169:9672193075cf | 392 | int flash_ext_burst_read(flash_t *obj, uint32_t address, uint32_t length, uint8_t *data) |
AnnaBridge | 169:9672193075cf | 393 | { |
AnnaBridge | 169:9672193075cf | 394 | __flash_ext_turnon(); |
AnnaBridge | 169:9672193075cf | 395 | SpicWaitWipDoneRefinedRtl8195A(flashobj.SpicInitPara); |
AnnaBridge | 169:9672193075cf | 396 | SpicUserReadRtl8195A(length, address, data, SpicOneBitMode); |
AnnaBridge | 169:9672193075cf | 397 | __flash_ext_turnoff(); |
AnnaBridge | 169:9672193075cf | 398 | return 0; |
AnnaBridge | 169:9672193075cf | 399 | } |
AnnaBridge | 169:9672193075cf | 400 | |
AnnaBridge | 169:9672193075cf | 401 | int flash_ext_get_status(flash_t *obj) |
AnnaBridge | 169:9672193075cf | 402 | { |
AnnaBridge | 169:9672193075cf | 403 | uint8_t status = 0; |
AnnaBridge | 169:9672193075cf | 404 | |
AnnaBridge | 169:9672193075cf | 405 | __flash_ext_turnon(); |
AnnaBridge | 169:9672193075cf | 406 | status = SpicGetFlashStatusRefinedRtl8195A(flashobj.SpicInitPara); |
AnnaBridge | 169:9672193075cf | 407 | __flash_ext_turnoff(); |
AnnaBridge | 169:9672193075cf | 408 | return status; |
AnnaBridge | 169:9672193075cf | 409 | } |
AnnaBridge | 169:9672193075cf | 410 | |
AnnaBridge | 169:9672193075cf | 411 | /* |
AnnaBridge | 169:9672193075cf | 412 | Function Description: |
AnnaBridge | 169:9672193075cf | 413 | Please refer to the datatsheet of flash for more details of the content of status register. |
AnnaBridge | 169:9672193075cf | 414 | The block protected area and the corresponding control bits are provided in the flash datasheet. |
AnnaBridge | 169:9672193075cf | 415 | |
AnnaBridge | 169:9672193075cf | 416 | * @brief Set Status register to enable desired operation |
AnnaBridge | 169:9672193075cf | 417 | * @param obj: Specifies the parameter of flash object. |
AnnaBridge | 169:9672193075cf | 418 | * @param data: Specifies which bit users like to set |
AnnaBridge | 169:9672193075cf | 419 | ex: if users want to set the third bit, data = 0x8. |
AnnaBridge | 169:9672193075cf | 420 | |
AnnaBridge | 169:9672193075cf | 421 | */ |
AnnaBridge | 169:9672193075cf | 422 | int flash_ext_set_status(flash_t *obj, uint32_t data) |
AnnaBridge | 169:9672193075cf | 423 | { |
AnnaBridge | 169:9672193075cf | 424 | __flash_ext_turnon(); |
AnnaBridge | 169:9672193075cf | 425 | SpicSetFlashStatusRefinedRtl8195A(data, flashobj.SpicInitPara); |
AnnaBridge | 169:9672193075cf | 426 | SpicWaitWipDoneRefinedRtl8195A(flashobj.SpicInitPara); |
AnnaBridge | 169:9672193075cf | 427 | __flash_ext_turnoff(); |
AnnaBridge | 169:9672193075cf | 428 | return 0; |
AnnaBridge | 169:9672193075cf | 429 | } |
AnnaBridge | 169:9672193075cf | 430 | |
AnnaBridge | 169:9672193075cf | 431 | /* |
AnnaBridge | 169:9672193075cf | 432 | Function Description: |
AnnaBridge | 169:9672193075cf | 433 | This function aims to reset the status register, please make sure the operation is appropriate. |
AnnaBridge | 169:9672193075cf | 434 | */ |
AnnaBridge | 169:9672193075cf | 435 | void flash_ext_reset_status(flash_t *obj) |
AnnaBridge | 169:9672193075cf | 436 | { |
AnnaBridge | 169:9672193075cf | 437 | __flash_ext_turnon(); |
Kojto | 171:19eb464bc2be | 438 | SpicSetFlashStatusRefinedRtl8195A(0, flashobj.SpicInitPara); |
AnnaBridge | 169:9672193075cf | 439 | SpicWaitWipDoneRefinedRtl8195A(flashobj.SpicInitPara); |
AnnaBridge | 169:9672193075cf | 440 | __flash_ext_turnoff(); |
AnnaBridge | 169:9672193075cf | 441 | } |
AnnaBridge | 169:9672193075cf | 442 | |
AnnaBridge | 169:9672193075cf | 443 | int flash_ext_get_extend_addr(flash_t *obj) |
AnnaBridge | 169:9672193075cf | 444 | { |
AnnaBridge | 169:9672193075cf | 445 | uint8_t addr = 0; |
AnnaBridge | 169:9672193075cf | 446 | |
AnnaBridge | 169:9672193075cf | 447 | __flash_ext_turnon(); |
AnnaBridge | 169:9672193075cf | 448 | addr = SpicGetExtendAddrRtl8195A(flashobj.SpicInitPara); |
AnnaBridge | 169:9672193075cf | 449 | __flash_ext_turnoff(); |
AnnaBridge | 169:9672193075cf | 450 | return addr; |
AnnaBridge | 169:9672193075cf | 451 | } |
AnnaBridge | 169:9672193075cf | 452 | /* |
AnnaBridge | 169:9672193075cf | 453 | Function Description: |
AnnaBridge | 169:9672193075cf | 454 | This function is only for Micron 512Mbit flash to access beyond 128Mbit by switching between four 128 Mbit area. |
AnnaBridge | 169:9672193075cf | 455 | Please refer to flash datasheet for more information about memory mapping. |
AnnaBridge | 169:9672193075cf | 456 | */ |
AnnaBridge | 169:9672193075cf | 457 | |
AnnaBridge | 169:9672193075cf | 458 | int flash_ext_set_extend_addr(flash_t *obj, uint32_t data) |
AnnaBridge | 169:9672193075cf | 459 | { |
AnnaBridge | 169:9672193075cf | 460 | __flash_ext_turnon(); |
AnnaBridge | 169:9672193075cf | 461 | SpicSetExtendAddrRtl8195A(data, flashobj.SpicInitPara); |
AnnaBridge | 169:9672193075cf | 462 | SpicWaitWipDoneRefinedRtl8195A(flashobj.SpicInitPara); |
AnnaBridge | 169:9672193075cf | 463 | __flash_ext_turnoff(); |
AnnaBridge | 169:9672193075cf | 464 | return 0; |
AnnaBridge | 169:9672193075cf | 465 | } |
AnnaBridge | 169:9672193075cf | 466 | |
AnnaBridge | 169:9672193075cf | 467 |