forked

Committer:
AnnaBridge
Date:
Wed Jun 21 17:46:44 2017 +0100
Revision:
167:e84263d55307
Child:
168:9672193075cf
This updates the lib to the mbed lib v 145

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AnnaBridge 167:e84263d55307 1 /* mbed Microcontroller Library
AnnaBridge 167:e84263d55307 2 * Copyright (c) 2013-2016 Realtek Semiconductor Corp.
AnnaBridge 167:e84263d55307 3 *
AnnaBridge 167:e84263d55307 4 * Licensed under the Apache License, Version 2.0 (the "License");
AnnaBridge 167:e84263d55307 5 * you may not use this file except in compliance with the License.
AnnaBridge 167:e84263d55307 6 * You may obtain a copy of the License at
AnnaBridge 167:e84263d55307 7 *
AnnaBridge 167:e84263d55307 8 * http://www.apache.org/licenses/LICENSE-2.0
AnnaBridge 167:e84263d55307 9 *
AnnaBridge 167:e84263d55307 10 * Unless required by applicable law or agreed to in writing, software
AnnaBridge 167:e84263d55307 11 * distributed under the License is distributed on an "AS IS" BASIS,
AnnaBridge 167:e84263d55307 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
AnnaBridge 167:e84263d55307 13 * See the License for the specific language governing permissions and
AnnaBridge 167:e84263d55307 14 * limitations under the License.
AnnaBridge 167:e84263d55307 15 */
AnnaBridge 167:e84263d55307 16
AnnaBridge 167:e84263d55307 17
AnnaBridge 167:e84263d55307 18 #include "objects.h"
AnnaBridge 167:e84263d55307 19 #include "PinNames.h"
AnnaBridge 167:e84263d55307 20
AnnaBridge 167:e84263d55307 21
AnnaBridge 167:e84263d55307 22 #include "pinmap.h"
AnnaBridge 167:e84263d55307 23
AnnaBridge 167:e84263d55307 24 #include "rtl8195a.h"
AnnaBridge 167:e84263d55307 25 #include "hal_spi_flash.h"
AnnaBridge 167:e84263d55307 26 #include "hal_platform.h"
AnnaBridge 167:e84263d55307 27 #include "rtl8195a_spi_flash.h"
AnnaBridge 167:e84263d55307 28 #include "hal_api.h"
AnnaBridge 167:e84263d55307 29 #include "flash_api.h"
AnnaBridge 167:e84263d55307 30
AnnaBridge 167:e84263d55307 31 extern u32 ConfigDebugInfo;
AnnaBridge 167:e84263d55307 32 extern SPIC_INIT_PARA SpicInitParaAllClk[3][CPU_CLK_TYPE_NO];
AnnaBridge 167:e84263d55307 33
AnnaBridge 167:e84263d55307 34 _LONG_CALL_
AnnaBridge 167:e84263d55307 35 extern VOID SpicWaitBusyDoneRtl8195A(VOID);
AnnaBridge 167:e84263d55307 36
AnnaBridge 167:e84263d55307 37 static int isinit = 0;
AnnaBridge 167:e84263d55307 38 static flash_t flashobj;
AnnaBridge 167:e84263d55307 39
AnnaBridge 167:e84263d55307 40 static void flash_init(flash_t * obj);
AnnaBridge 167:e84263d55307 41 static void flash_turnon();
AnnaBridge 167:e84263d55307 42 /**
AnnaBridge 167:e84263d55307 43 * global data structure
AnnaBridge 167:e84263d55307 44 */
AnnaBridge 167:e84263d55307 45 //flash_t flash;
AnnaBridge 167:e84263d55307 46
AnnaBridge 167:e84263d55307 47 /**
AnnaBridge 167:e84263d55307 48 * @brief Control the flash chip write protect enable/disable
AnnaBridge 167:e84263d55307 49 * @param protect: 1/0: protect/unprotect
AnnaBridge 167:e84263d55307 50 * @retval none
AnnaBridge 167:e84263d55307 51 */
AnnaBridge 167:e84263d55307 52 void flash_write_protect(flash_t *obj, uint32_t protect)
AnnaBridge 167:e84263d55307 53 {
AnnaBridge 167:e84263d55307 54 flash_turnon();
AnnaBridge 167:e84263d55307 55
AnnaBridge 167:e84263d55307 56 if(isinit == 0)
AnnaBridge 167:e84263d55307 57 flash_init(&flashobj);
AnnaBridge 167:e84263d55307 58 SpicWriteProtectFlashRtl8195A(protect);
AnnaBridge 167:e84263d55307 59 SpicDisableRtl8195A();
AnnaBridge 167:e84263d55307 60 }
AnnaBridge 167:e84263d55307 61
AnnaBridge 167:e84263d55307 62 /**
AnnaBridge 167:e84263d55307 63 * @brief Init Flash
AnnaBridge 167:e84263d55307 64 * @param obj: address of the flash object
AnnaBridge 167:e84263d55307 65 * @retval none
AnnaBridge 167:e84263d55307 66 */
AnnaBridge 167:e84263d55307 67 void flash_init(flash_t *obj)
AnnaBridge 167:e84263d55307 68 {
AnnaBridge 167:e84263d55307 69 //SPIC_INIT_PARA spic_init_para;
AnnaBridge 167:e84263d55307 70
AnnaBridge 167:e84263d55307 71 // Init SPI Flash Controller
AnnaBridge 167:e84263d55307 72 // DBG_8195A("Initial Spi Flash Controller\n");
AnnaBridge 167:e84263d55307 73 //SPI_FLASH_PIN_FCTRL(ON);
AnnaBridge 167:e84263d55307 74
AnnaBridge 167:e84263d55307 75 if (!SpicFlashInitRtl8195A(SpicOneBitMode)){
AnnaBridge 167:e84263d55307 76
AnnaBridge 167:e84263d55307 77 DBG_8195A("SPI Init Fail!!!!!!\n");
AnnaBridge 167:e84263d55307 78 HAL_WRITE32(SYSTEM_CTRL_BASE, REG_SYS_DSTBY_INFO3, HAL_READ32(SYSTEM_CTRL_BASE, REG_SYS_DSTBY_INFO3)|0xf);
AnnaBridge 167:e84263d55307 79 } else {
AnnaBridge 167:e84263d55307 80 isinit = 1;
AnnaBridge 167:e84263d55307 81 }
AnnaBridge 167:e84263d55307 82 flashobj.SpicInitPara.flashtype = SpicInitParaAllClk[0][0].flashtype;
AnnaBridge 167:e84263d55307 83
AnnaBridge 167:e84263d55307 84 }
AnnaBridge 167:e84263d55307 85 void flash_turnon()
AnnaBridge 167:e84263d55307 86 {
AnnaBridge 167:e84263d55307 87 SPI_FLASH_PIN_FCTRL(ON);
AnnaBridge 167:e84263d55307 88 SpicWaitBusyDoneRtl8195A();
AnnaBridge 167:e84263d55307 89 }
AnnaBridge 167:e84263d55307 90
AnnaBridge 167:e84263d55307 91 /**
AnnaBridge 167:e84263d55307 92 * @brief Erase flash sector
AnnaBridge 167:e84263d55307 93 * @param address: Specifies the starting address to be erased.
AnnaBridge 167:e84263d55307 94 * @retval none
AnnaBridge 167:e84263d55307 95 */
AnnaBridge 167:e84263d55307 96 void flash_erase_sector(flash_t *obj, uint32_t address)
AnnaBridge 167:e84263d55307 97 {
AnnaBridge 167:e84263d55307 98 flash_turnon();
AnnaBridge 167:e84263d55307 99
AnnaBridge 167:e84263d55307 100 if(isinit == 0)
AnnaBridge 167:e84263d55307 101 flash_init(&flashobj);
AnnaBridge 167:e84263d55307 102
AnnaBridge 167:e84263d55307 103 SpicSectorEraseFlashRtl8195A(SPI_FLASH_BASE + address);
AnnaBridge 167:e84263d55307 104 SpicDisableRtl8195A();
AnnaBridge 167:e84263d55307 105 }
AnnaBridge 167:e84263d55307 106
AnnaBridge 167:e84263d55307 107 void flash_erase_block(flash_t *obj, uint32_t address)
AnnaBridge 167:e84263d55307 108 {
AnnaBridge 167:e84263d55307 109 flash_turnon();
AnnaBridge 167:e84263d55307 110
AnnaBridge 167:e84263d55307 111 if(isinit == 0)
AnnaBridge 167:e84263d55307 112 flash_init(&flashobj);
AnnaBridge 167:e84263d55307 113
AnnaBridge 167:e84263d55307 114 SpicBlockEraseFlashRtl8195A(SPI_FLASH_BASE + address);
AnnaBridge 167:e84263d55307 115 SpicDisableRtl8195A();
AnnaBridge 167:e84263d55307 116 }
AnnaBridge 167:e84263d55307 117
AnnaBridge 167:e84263d55307 118
AnnaBridge 167:e84263d55307 119 /**
AnnaBridge 167:e84263d55307 120 * @brief Read a word from specified address
AnnaBridge 167:e84263d55307 121 * @param obj: Specifies the parameter of flash object.
AnnaBridge 167:e84263d55307 122 * @param address: Specifies the address to be read.
AnnaBridge 167:e84263d55307 123 * @param data: Specified the address to save the readback data.
AnnaBridge 167:e84263d55307 124 * @retval status: Success:1 or Failure: Others.
AnnaBridge 167:e84263d55307 125 */
AnnaBridge 167:e84263d55307 126 int flash_read_word(flash_t *obj, uint32_t address, uint32_t * data)
AnnaBridge 167:e84263d55307 127 {
AnnaBridge 167:e84263d55307 128
AnnaBridge 167:e84263d55307 129 flash_turnon();
AnnaBridge 167:e84263d55307 130 if(isinit == 0)
AnnaBridge 167:e84263d55307 131 flash_init(&flashobj);
AnnaBridge 167:e84263d55307 132 // Wait flash busy done (wip=0)
AnnaBridge 167:e84263d55307 133 SpicWaitWipDoneRefinedRtl8195A(flashobj.SpicInitPara);
AnnaBridge 167:e84263d55307 134
AnnaBridge 167:e84263d55307 135 * data = HAL_READ32(SPI_FLASH_BASE, address);
AnnaBridge 167:e84263d55307 136 SpicDisableRtl8195A();
AnnaBridge 167:e84263d55307 137
AnnaBridge 167:e84263d55307 138 return 1;
AnnaBridge 167:e84263d55307 139 }
AnnaBridge 167:e84263d55307 140
AnnaBridge 167:e84263d55307 141 /**
AnnaBridge 167:e84263d55307 142 * @brief Write a word to specified address
AnnaBridge 167:e84263d55307 143 * @param obj: Specifies the parameter of flash object.
AnnaBridge 167:e84263d55307 144 * @param address: Specifies the address to be programmed.
AnnaBridge 167:e84263d55307 145 * @param data: Specified the data to be programmed.
AnnaBridge 167:e84263d55307 146 * @retval status: Success:1 or Failure: Others.
AnnaBridge 167:e84263d55307 147 */
AnnaBridge 167:e84263d55307 148 int flash_write_word(flash_t *obj, uint32_t address, uint32_t data)
AnnaBridge 167:e84263d55307 149 {
AnnaBridge 167:e84263d55307 150 u8 flashtype = 0;
AnnaBridge 167:e84263d55307 151
AnnaBridge 167:e84263d55307 152 flash_turnon();
AnnaBridge 167:e84263d55307 153 if(isinit == 0)
AnnaBridge 167:e84263d55307 154 flash_init(&flashobj);
AnnaBridge 167:e84263d55307 155
AnnaBridge 167:e84263d55307 156
AnnaBridge 167:e84263d55307 157 flashtype = flashobj.SpicInitPara.flashtype;
AnnaBridge 167:e84263d55307 158
AnnaBridge 167:e84263d55307 159 //Write word
AnnaBridge 167:e84263d55307 160 HAL_WRITE32(SPI_FLASH_BASE, address, data);
AnnaBridge 167:e84263d55307 161
AnnaBridge 167:e84263d55307 162 // Wait spic busy done
AnnaBridge 167:e84263d55307 163 SpicWaitBusyDoneRtl8195A();
AnnaBridge 167:e84263d55307 164
AnnaBridge 167:e84263d55307 165 // Wait flash busy done (wip=0)
AnnaBridge 167:e84263d55307 166 if(flashtype == FLASH_MICRON){
AnnaBridge 167:e84263d55307 167 SpicWaitOperationDoneRtl8195A(flashobj.SpicInitPara);
AnnaBridge 167:e84263d55307 168 } else
AnnaBridge 167:e84263d55307 169 SpicWaitWipDoneRefinedRtl8195A(flashobj.SpicInitPara);
AnnaBridge 167:e84263d55307 170
AnnaBridge 167:e84263d55307 171 SpicDisableRtl8195A();
AnnaBridge 167:e84263d55307 172 return 1;
AnnaBridge 167:e84263d55307 173 }
AnnaBridge 167:e84263d55307 174
AnnaBridge 167:e84263d55307 175
AnnaBridge 167:e84263d55307 176 /**
AnnaBridge 167:e84263d55307 177 * @brief Read a stream of data from specified address
AnnaBridge 167:e84263d55307 178 * @param obj: Specifies the parameter of flash object.
AnnaBridge 167:e84263d55307 179 * @param address: Specifies the address to be read.
AnnaBridge 167:e84263d55307 180 * @param len: Specifies the length of the data to read.
AnnaBridge 167:e84263d55307 181 * @param data: Specified the address to save the readback data.
AnnaBridge 167:e84263d55307 182 * @retval status: Success:1 or Failure: Others.
AnnaBridge 167:e84263d55307 183 */
AnnaBridge 167:e84263d55307 184 int flash_stream_read(flash_t *obj, uint32_t address, uint32_t len, uint8_t * data)
AnnaBridge 167:e84263d55307 185 {
AnnaBridge 167:e84263d55307 186 u32 offset_to_align;
AnnaBridge 167:e84263d55307 187 u32 i;
AnnaBridge 167:e84263d55307 188 u32 read_word;
AnnaBridge 167:e84263d55307 189 uint8_t *ptr;
AnnaBridge 167:e84263d55307 190 uint8_t *pbuf;
AnnaBridge 167:e84263d55307 191
AnnaBridge 167:e84263d55307 192 flash_turnon();
AnnaBridge 167:e84263d55307 193
AnnaBridge 167:e84263d55307 194 if(isinit == 0)
AnnaBridge 167:e84263d55307 195 flash_init(&flashobj);
AnnaBridge 167:e84263d55307 196
AnnaBridge 167:e84263d55307 197
AnnaBridge 167:e84263d55307 198 // Wait flash busy done (wip=0)
AnnaBridge 167:e84263d55307 199 SpicWaitWipDoneRefinedRtl8195A(flashobj.SpicInitPara);
AnnaBridge 167:e84263d55307 200
AnnaBridge 167:e84263d55307 201 offset_to_align = address & 0x03;
AnnaBridge 167:e84263d55307 202 pbuf = data;
AnnaBridge 167:e84263d55307 203 if (offset_to_align != 0) {
AnnaBridge 167:e84263d55307 204 // the start address is not 4-bytes aligned
AnnaBridge 167:e84263d55307 205 read_word = HAL_READ32(SPI_FLASH_BASE, (address - offset_to_align));
AnnaBridge 167:e84263d55307 206 ptr = (uint8_t*)&read_word + offset_to_align;
AnnaBridge 167:e84263d55307 207 offset_to_align = 4 - offset_to_align;
AnnaBridge 167:e84263d55307 208 for (i=0;i<offset_to_align;i++) {
AnnaBridge 167:e84263d55307 209 *pbuf = *(ptr+i);
AnnaBridge 167:e84263d55307 210 pbuf++;
AnnaBridge 167:e84263d55307 211 len--;
AnnaBridge 167:e84263d55307 212 if (len == 0) {
AnnaBridge 167:e84263d55307 213 break;
AnnaBridge 167:e84263d55307 214 }
AnnaBridge 167:e84263d55307 215 }
AnnaBridge 167:e84263d55307 216 }
AnnaBridge 167:e84263d55307 217
AnnaBridge 167:e84263d55307 218 address = (((address-1) >> 2) + 1) << 2; // address = next 4-bytes aligned
AnnaBridge 167:e84263d55307 219
AnnaBridge 167:e84263d55307 220 ptr = (uint8_t*)&read_word;
AnnaBridge 167:e84263d55307 221 if ((u32)pbuf & 0x03) {
AnnaBridge 167:e84263d55307 222 while (len >= 4) {
AnnaBridge 167:e84263d55307 223 read_word = HAL_READ32(SPI_FLASH_BASE, address);
AnnaBridge 167:e84263d55307 224 for (i=0;i<4;i++) {
AnnaBridge 167:e84263d55307 225 *pbuf = *(ptr+i);
AnnaBridge 167:e84263d55307 226 pbuf++;
AnnaBridge 167:e84263d55307 227 }
AnnaBridge 167:e84263d55307 228 address += 4;
AnnaBridge 167:e84263d55307 229 len -= 4;
AnnaBridge 167:e84263d55307 230 }
AnnaBridge 167:e84263d55307 231 } else {
AnnaBridge 167:e84263d55307 232 while (len >= 4) {
AnnaBridge 167:e84263d55307 233 *((u32 *)pbuf) = HAL_READ32(SPI_FLASH_BASE, address);
AnnaBridge 167:e84263d55307 234 pbuf += 4;
AnnaBridge 167:e84263d55307 235 address += 4;
AnnaBridge 167:e84263d55307 236 len -= 4;
AnnaBridge 167:e84263d55307 237 }
AnnaBridge 167:e84263d55307 238 }
AnnaBridge 167:e84263d55307 239
AnnaBridge 167:e84263d55307 240 if (len > 0) {
AnnaBridge 167:e84263d55307 241 read_word = HAL_READ32(SPI_FLASH_BASE, address);
AnnaBridge 167:e84263d55307 242 for (i=0;i<len;i++) {
AnnaBridge 167:e84263d55307 243 *pbuf = *(ptr+i);
AnnaBridge 167:e84263d55307 244 pbuf++;
AnnaBridge 167:e84263d55307 245 }
AnnaBridge 167:e84263d55307 246 }
AnnaBridge 167:e84263d55307 247
AnnaBridge 167:e84263d55307 248 SpicDisableRtl8195A();
AnnaBridge 167:e84263d55307 249 return 1;
AnnaBridge 167:e84263d55307 250 }
AnnaBridge 167:e84263d55307 251
AnnaBridge 167:e84263d55307 252 /**
AnnaBridge 167:e84263d55307 253 * @brief Write a stream of data to specified address
AnnaBridge 167:e84263d55307 254 * @param obj: Specifies the parameter of flash object.
AnnaBridge 167:e84263d55307 255 * @param address: Specifies the address to be read.
AnnaBridge 167:e84263d55307 256 * @param len: Specifies the length of the data to write.
AnnaBridge 167:e84263d55307 257 * @param data: Specified the pointer of the data to be written.
AnnaBridge 167:e84263d55307 258 * @retval status: Success:1 or Failure: Others.
AnnaBridge 167:e84263d55307 259 */
AnnaBridge 167:e84263d55307 260 int flash_stream_write(flash_t *obj, uint32_t address, uint32_t len, uint8_t * data)
AnnaBridge 167:e84263d55307 261 {
AnnaBridge 167:e84263d55307 262 u32 offset_to_align;
AnnaBridge 167:e84263d55307 263 u32 align_addr;
AnnaBridge 167:e84263d55307 264 u32 i;
AnnaBridge 167:e84263d55307 265 u32 write_word;
AnnaBridge 167:e84263d55307 266 uint8_t *ptr;
AnnaBridge 167:e84263d55307 267 uint8_t *pbuf;
AnnaBridge 167:e84263d55307 268 u8 flashtype = 0;
AnnaBridge 167:e84263d55307 269
AnnaBridge 167:e84263d55307 270 flash_turnon();
AnnaBridge 167:e84263d55307 271
AnnaBridge 167:e84263d55307 272 if(isinit == 0)
AnnaBridge 167:e84263d55307 273 flash_init(&flashobj);
AnnaBridge 167:e84263d55307 274
AnnaBridge 167:e84263d55307 275 flashtype = flashobj.SpicInitPara.flashtype;
AnnaBridge 167:e84263d55307 276 offset_to_align = address & 0x03;
AnnaBridge 167:e84263d55307 277 pbuf = data;
AnnaBridge 167:e84263d55307 278 if (offset_to_align != 0) {
AnnaBridge 167:e84263d55307 279 // the start address is not 4-bytes aligned
AnnaBridge 167:e84263d55307 280 align_addr = (address - offset_to_align);
AnnaBridge 167:e84263d55307 281 write_word = HAL_READ32(SPI_FLASH_BASE, align_addr);
AnnaBridge 167:e84263d55307 282 ptr = (uint8_t*)&write_word + offset_to_align;
AnnaBridge 167:e84263d55307 283 offset_to_align = 4 - offset_to_align;
AnnaBridge 167:e84263d55307 284 for (i=0;i<offset_to_align;i++) {
AnnaBridge 167:e84263d55307 285 *(ptr+i) = *pbuf;
AnnaBridge 167:e84263d55307 286 pbuf++;
AnnaBridge 167:e84263d55307 287 len--;
AnnaBridge 167:e84263d55307 288 if (len == 0) {
AnnaBridge 167:e84263d55307 289 break;
AnnaBridge 167:e84263d55307 290 }
AnnaBridge 167:e84263d55307 291 }
AnnaBridge 167:e84263d55307 292 //Write word
AnnaBridge 167:e84263d55307 293 HAL_WRITE32(SPI_FLASH_BASE, align_addr, write_word);
AnnaBridge 167:e84263d55307 294 // Wait spic busy done
AnnaBridge 167:e84263d55307 295 SpicWaitBusyDoneRtl8195A();
AnnaBridge 167:e84263d55307 296 // Wait flash busy done (wip=0)
AnnaBridge 167:e84263d55307 297 if(flashtype == FLASH_MICRON){
AnnaBridge 167:e84263d55307 298 SpicWaitOperationDoneRtl8195A(flashobj.SpicInitPara);
AnnaBridge 167:e84263d55307 299 } else
AnnaBridge 167:e84263d55307 300 SpicWaitWipDoneRefinedRtl8195A(flashobj.SpicInitPara);
AnnaBridge 167:e84263d55307 301
AnnaBridge 167:e84263d55307 302
AnnaBridge 167:e84263d55307 303 }
AnnaBridge 167:e84263d55307 304
AnnaBridge 167:e84263d55307 305 address = (((address-1) >> 2) + 1) << 2; // address = next 4-bytes aligned
AnnaBridge 167:e84263d55307 306
AnnaBridge 167:e84263d55307 307 if ((u32)pbuf & 0x03) {
AnnaBridge 167:e84263d55307 308 while (len >= 4) {
AnnaBridge 167:e84263d55307 309 write_word = (u32)(*pbuf) | ((u32)(*(pbuf+1)) << 8) | ((u32)(*(pbuf+2)) << 16) | ((u32)(*(pbuf+3)) << 24);
AnnaBridge 167:e84263d55307 310 //Write word
AnnaBridge 167:e84263d55307 311 HAL_WRITE32(SPI_FLASH_BASE, address, write_word);
AnnaBridge 167:e84263d55307 312 // Wait spic busy done
AnnaBridge 167:e84263d55307 313 SpicWaitBusyDoneRtl8195A();
AnnaBridge 167:e84263d55307 314 // Wait flash busy done (wip=0)
AnnaBridge 167:e84263d55307 315 if(flashtype == FLASH_MICRON){
AnnaBridge 167:e84263d55307 316 SpicWaitOperationDoneRtl8195A(flashobj.SpicInitPara);
AnnaBridge 167:e84263d55307 317 } else
AnnaBridge 167:e84263d55307 318 SpicWaitWipDoneRefinedRtl8195A(flashobj.SpicInitPara);
AnnaBridge 167:e84263d55307 319
AnnaBridge 167:e84263d55307 320 pbuf += 4;
AnnaBridge 167:e84263d55307 321 address += 4;
AnnaBridge 167:e84263d55307 322 len -= 4;
AnnaBridge 167:e84263d55307 323 }
AnnaBridge 167:e84263d55307 324 } else {
AnnaBridge 167:e84263d55307 325 while (len >= 4) {
AnnaBridge 167:e84263d55307 326 //Write word
AnnaBridge 167:e84263d55307 327 HAL_WRITE32(SPI_FLASH_BASE, address, (u32)*((u32 *)pbuf));
AnnaBridge 167:e84263d55307 328 // Wait spic busy done
AnnaBridge 167:e84263d55307 329 SpicWaitBusyDoneRtl8195A();
AnnaBridge 167:e84263d55307 330 // Wait flash busy done (wip=0)
AnnaBridge 167:e84263d55307 331 if(flashtype == FLASH_MICRON){
AnnaBridge 167:e84263d55307 332 SpicWaitOperationDoneRtl8195A(flashobj.SpicInitPara);
AnnaBridge 167:e84263d55307 333 } else
AnnaBridge 167:e84263d55307 334 SpicWaitWipDoneRefinedRtl8195A(flashobj.SpicInitPara);
AnnaBridge 167:e84263d55307 335
AnnaBridge 167:e84263d55307 336 pbuf += 4;
AnnaBridge 167:e84263d55307 337 address += 4;
AnnaBridge 167:e84263d55307 338 len -= 4;
AnnaBridge 167:e84263d55307 339 }
AnnaBridge 167:e84263d55307 340 }
AnnaBridge 167:e84263d55307 341
AnnaBridge 167:e84263d55307 342 if (len > 0) {
AnnaBridge 167:e84263d55307 343 write_word = HAL_READ32(SPI_FLASH_BASE, address);
AnnaBridge 167:e84263d55307 344 ptr = (uint8_t*)&write_word;
AnnaBridge 167:e84263d55307 345 for (i=0;i<len;i++) {
AnnaBridge 167:e84263d55307 346 *(ptr+i) = *pbuf;
AnnaBridge 167:e84263d55307 347 pbuf++;
AnnaBridge 167:e84263d55307 348 }
AnnaBridge 167:e84263d55307 349 //Write word
AnnaBridge 167:e84263d55307 350 HAL_WRITE32(SPI_FLASH_BASE, address, write_word);
AnnaBridge 167:e84263d55307 351 // Wait spic busy done
AnnaBridge 167:e84263d55307 352 SpicWaitBusyDoneRtl8195A();
AnnaBridge 167:e84263d55307 353 // Wait flash busy done (wip=0)
AnnaBridge 167:e84263d55307 354 if(flashtype == FLASH_MICRON){
AnnaBridge 167:e84263d55307 355 SpicWaitOperationDoneRtl8195A(flashobj.SpicInitPara);
AnnaBridge 167:e84263d55307 356 } else
AnnaBridge 167:e84263d55307 357 SpicWaitWipDoneRefinedRtl8195A(flashobj.SpicInitPara);
AnnaBridge 167:e84263d55307 358
AnnaBridge 167:e84263d55307 359 }
AnnaBridge 167:e84263d55307 360
AnnaBridge 167:e84263d55307 361 SpicDisableRtl8195A();
AnnaBridge 167:e84263d55307 362 return 1;
AnnaBridge 167:e84263d55307 363 }
AnnaBridge 167:e84263d55307 364
AnnaBridge 167:e84263d55307 365
AnnaBridge 167:e84263d55307 366 /*
AnnaBridge 167:e84263d55307 367 Function Description:
AnnaBridge 167:e84263d55307 368 This function performans the same functionality as the function flash_stream_write.
AnnaBridge 167:e84263d55307 369 It enhances write performance by reducing overheads.
AnnaBridge 167:e84263d55307 370 Users can use either of functions depending on their needs.
AnnaBridge 167:e84263d55307 371
AnnaBridge 167:e84263d55307 372 * @brief Write a stream of data to specified address
AnnaBridge 167:e84263d55307 373 * @param obj: Specifies the parameter of flash object.
AnnaBridge 167:e84263d55307 374 * @param address: Specifies the address to be read.
AnnaBridge 167:e84263d55307 375 * @param Length: Specifies the length of the data to write.
AnnaBridge 167:e84263d55307 376 * @param data: Specified the pointer of the data to be written.
AnnaBridge 167:e84263d55307 377 * @retval status: Success:1 or Failure: Others.
AnnaBridge 167:e84263d55307 378
AnnaBridge 167:e84263d55307 379 */
AnnaBridge 167:e84263d55307 380
AnnaBridge 167:e84263d55307 381 int flash_burst_write(flash_t *obj, uint32_t address ,uint32_t Length, uint8_t * data)
AnnaBridge 167:e84263d55307 382 {
AnnaBridge 167:e84263d55307 383
AnnaBridge 167:e84263d55307 384 u32 OccuSize;
AnnaBridge 167:e84263d55307 385 u32 ProgramSize;
AnnaBridge 167:e84263d55307 386 u32 PageSize;
AnnaBridge 167:e84263d55307 387 u8 flashtype = 0;
AnnaBridge 167:e84263d55307 388
AnnaBridge 167:e84263d55307 389 PageSize = 256;
AnnaBridge 167:e84263d55307 390
AnnaBridge 167:e84263d55307 391 flash_turnon();
AnnaBridge 167:e84263d55307 392
AnnaBridge 167:e84263d55307 393 if(isinit == 0)
AnnaBridge 167:e84263d55307 394 flash_init(&flashobj);
AnnaBridge 167:e84263d55307 395
AnnaBridge 167:e84263d55307 396 flashtype = flashobj.SpicInitPara.flashtype;
AnnaBridge 167:e84263d55307 397
AnnaBridge 167:e84263d55307 398 OccuSize = address & 0xFF;
AnnaBridge 167:e84263d55307 399 if((Length >= PageSize) ||((Length + OccuSize) >= PageSize)){
AnnaBridge 167:e84263d55307 400 ProgramSize = PageSize - OccuSize;
AnnaBridge 167:e84263d55307 401 } else {
AnnaBridge 167:e84263d55307 402 ProgramSize = Length;
AnnaBridge 167:e84263d55307 403 }
AnnaBridge 167:e84263d55307 404
AnnaBridge 167:e84263d55307 405 flashobj.Length = Length;
AnnaBridge 167:e84263d55307 406 while(Length > 0){
AnnaBridge 167:e84263d55307 407 if(OccuSize){
AnnaBridge 167:e84263d55307 408 SpicUserProgramRtl8195A(data, flashobj.SpicInitPara, address, &(flashobj.Length));
AnnaBridge 167:e84263d55307 409 // Wait spic busy done
AnnaBridge 167:e84263d55307 410 SpicWaitBusyDoneRtl8195A();
AnnaBridge 167:e84263d55307 411 // Wait flash busy done (wip=0)
AnnaBridge 167:e84263d55307 412 if(flashtype == FLASH_MICRON){
AnnaBridge 167:e84263d55307 413 SpicWaitOperationDoneRtl8195A(flashobj.SpicInitPara);
AnnaBridge 167:e84263d55307 414 } else
AnnaBridge 167:e84263d55307 415 SpicWaitWipDoneRefinedRtl8195A(flashobj.SpicInitPara);
AnnaBridge 167:e84263d55307 416
AnnaBridge 167:e84263d55307 417 address += ProgramSize;
AnnaBridge 167:e84263d55307 418 data+= ProgramSize;
AnnaBridge 167:e84263d55307 419 Length -= ProgramSize;
AnnaBridge 167:e84263d55307 420 OccuSize = 0;
AnnaBridge 167:e84263d55307 421 } else{
AnnaBridge 167:e84263d55307 422 while((flashobj.Length) >= PageSize){
AnnaBridge 167:e84263d55307 423 SpicUserProgramRtl8195A(data, flashobj.SpicInitPara, address, &(flashobj.Length));
AnnaBridge 167:e84263d55307 424 // Wait spic busy done
AnnaBridge 167:e84263d55307 425 SpicWaitBusyDoneRtl8195A();
AnnaBridge 167:e84263d55307 426 // Wait flash busy done (wip=0)
AnnaBridge 167:e84263d55307 427 if(flashtype == FLASH_MICRON){
AnnaBridge 167:e84263d55307 428 SpicWaitOperationDoneRtl8195A(flashobj.SpicInitPara);
AnnaBridge 167:e84263d55307 429 } else
AnnaBridge 167:e84263d55307 430 SpicWaitWipDoneRefinedRtl8195A(flashobj.SpicInitPara);
AnnaBridge 167:e84263d55307 431
AnnaBridge 167:e84263d55307 432 address += PageSize;
AnnaBridge 167:e84263d55307 433 data+=PageSize;
AnnaBridge 167:e84263d55307 434 Length -= PageSize;
AnnaBridge 167:e84263d55307 435 }
AnnaBridge 167:e84263d55307 436 flashobj.Length = Length;
AnnaBridge 167:e84263d55307 437 if((flashobj.Length) > 0){
AnnaBridge 167:e84263d55307 438 SpicUserProgramRtl8195A(data, flashobj.SpicInitPara, address, &(flashobj.Length));
AnnaBridge 167:e84263d55307 439 // Wait spic busy done
AnnaBridge 167:e84263d55307 440 SpicWaitBusyDoneRtl8195A();
AnnaBridge 167:e84263d55307 441 // Wait flash busy done (wip=0)
AnnaBridge 167:e84263d55307 442 if(flashtype == FLASH_MICRON){
AnnaBridge 167:e84263d55307 443 SpicWaitOperationDoneRtl8195A(flashobj.SpicInitPara);
AnnaBridge 167:e84263d55307 444 } else
AnnaBridge 167:e84263d55307 445 SpicWaitWipDoneRefinedRtl8195A(flashobj.SpicInitPara);
AnnaBridge 167:e84263d55307 446
AnnaBridge 167:e84263d55307 447 break;
AnnaBridge 167:e84263d55307 448 }
AnnaBridge 167:e84263d55307 449 }
AnnaBridge 167:e84263d55307 450 flashobj.Length = Length;
AnnaBridge 167:e84263d55307 451 }
AnnaBridge 167:e84263d55307 452
AnnaBridge 167:e84263d55307 453 SpicDisableRtl8195A();
AnnaBridge 167:e84263d55307 454 return 1;
AnnaBridge 167:e84263d55307 455 }
AnnaBridge 167:e84263d55307 456 /**
AnnaBridge 167:e84263d55307 457 * @brief Read a stream of data from specified address
AnnaBridge 167:e84263d55307 458 * @param obj: Specifies the parameter of flash object.
AnnaBridge 167:e84263d55307 459 * @param address: Specifies the address to be read.
AnnaBridge 167:e84263d55307 460 * @param len: Specifies the length of the data to read.
AnnaBridge 167:e84263d55307 461 * @param data: Specified the address to save the readback data.
AnnaBridge 167:e84263d55307 462 * @retval status: Success:1 or Failure: Others.
AnnaBridge 167:e84263d55307 463 */
AnnaBridge 167:e84263d55307 464
AnnaBridge 167:e84263d55307 465 int flash_burst_read(flash_t *obj, uint32_t address, uint32_t Length, uint8_t * data)
AnnaBridge 167:e84263d55307 466 {
AnnaBridge 167:e84263d55307 467 flash_turnon();
AnnaBridge 167:e84263d55307 468
AnnaBridge 167:e84263d55307 469 if(isinit == 0)
AnnaBridge 167:e84263d55307 470 flash_init(&flashobj);
AnnaBridge 167:e84263d55307 471
AnnaBridge 167:e84263d55307 472 // Wait flash busy done (wip=0)
AnnaBridge 167:e84263d55307 473 SpicWaitWipDoneRefinedRtl8195A(flashobj.SpicInitPara);
AnnaBridge 167:e84263d55307 474 SpicUserReadRtl8195A(Length, address, data, SpicOneBitMode);
AnnaBridge 167:e84263d55307 475 SpicDisableRtl8195A();
AnnaBridge 167:e84263d55307 476 return 1;
AnnaBridge 167:e84263d55307 477 }
AnnaBridge 167:e84263d55307 478
AnnaBridge 167:e84263d55307 479 int flash_get_status(flash_t *obj)
AnnaBridge 167:e84263d55307 480 {
AnnaBridge 167:e84263d55307 481 u8 Status = 0;
AnnaBridge 167:e84263d55307 482
AnnaBridge 167:e84263d55307 483 flash_turnon();
AnnaBridge 167:e84263d55307 484
AnnaBridge 167:e84263d55307 485 if(isinit == 0)
AnnaBridge 167:e84263d55307 486 flash_init(&flashobj);
AnnaBridge 167:e84263d55307 487
AnnaBridge 167:e84263d55307 488 Status = SpicGetFlashStatusRefinedRtl8195A(flashobj.SpicInitPara);
AnnaBridge 167:e84263d55307 489
AnnaBridge 167:e84263d55307 490 SpicDisableRtl8195A();
AnnaBridge 167:e84263d55307 491 return Status;
AnnaBridge 167:e84263d55307 492 }
AnnaBridge 167:e84263d55307 493
AnnaBridge 167:e84263d55307 494 /*
AnnaBridge 167:e84263d55307 495 Function Description:
AnnaBridge 167:e84263d55307 496 Please refer to the datatsheet of flash for more details of the content of status register.
AnnaBridge 167:e84263d55307 497 The block protected area and the corresponding control bits are provided in the flash datasheet.
AnnaBridge 167:e84263d55307 498
AnnaBridge 167:e84263d55307 499 * @brief Set Status register to enable desired operation
AnnaBridge 167:e84263d55307 500 * @param obj: Specifies the parameter of flash object.
AnnaBridge 167:e84263d55307 501 * @param data: Specifies which bit users like to set
AnnaBridge 167:e84263d55307 502 ex: if users want to set the third bit, data = 0x8.
AnnaBridge 167:e84263d55307 503
AnnaBridge 167:e84263d55307 504 */
AnnaBridge 167:e84263d55307 505 int flash_set_status(flash_t *obj, uint32_t data)
AnnaBridge 167:e84263d55307 506 {
AnnaBridge 167:e84263d55307 507 flash_turnon();
AnnaBridge 167:e84263d55307 508
AnnaBridge 167:e84263d55307 509 if(isinit == 0)
AnnaBridge 167:e84263d55307 510 flash_init(&flashobj);
AnnaBridge 167:e84263d55307 511
AnnaBridge 167:e84263d55307 512 SpicSetFlashStatusRefinedRtl8195A(data, flashobj.SpicInitPara);
AnnaBridge 167:e84263d55307 513 SpicWaitWipDoneRefinedRtl8195A(flashobj.SpicInitPara);
AnnaBridge 167:e84263d55307 514 DBG_8195A("Status Register After Setting= %x\n", flash_get_status(&flashobj));
AnnaBridge 167:e84263d55307 515 SpicDisableRtl8195A();
AnnaBridge 167:e84263d55307 516 return 1;
AnnaBridge 167:e84263d55307 517 }
AnnaBridge 167:e84263d55307 518
AnnaBridge 167:e84263d55307 519 /*
AnnaBridge 167:e84263d55307 520 Function Description:
AnnaBridge 167:e84263d55307 521 This function aims to reset the status register, please make sure the operation is appropriate.
AnnaBridge 167:e84263d55307 522 */
AnnaBridge 167:e84263d55307 523 void flash_reset_status(flash_t *obj)
AnnaBridge 167:e84263d55307 524 {
AnnaBridge 167:e84263d55307 525 flash_turnon();
AnnaBridge 167:e84263d55307 526
AnnaBridge 167:e84263d55307 527 if(isinit == 0)
AnnaBridge 167:e84263d55307 528 flash_init(&flashobj);
AnnaBridge 167:e84263d55307 529
AnnaBridge 167:e84263d55307 530 SpicSetFlashStatusRefinedRtl8195A(0, flashobj.SpicInitPara);
AnnaBridge 167:e84263d55307 531 SpicWaitWipDoneRefinedRtl8195A(flashobj.SpicInitPara);
AnnaBridge 167:e84263d55307 532 SpicDisableRtl8195A();
AnnaBridge 167:e84263d55307 533 }
AnnaBridge 167:e84263d55307 534 /*
AnnaBridge 167:e84263d55307 535 Function Description:
AnnaBridge 167:e84263d55307 536 This function is only for Micron 512Mbit flash to access beyond 128Mbit by switching between four 128 Mbit area.
AnnaBridge 167:e84263d55307 537 Please refer to flash datasheet for more information about memory mapping.
AnnaBridge 167:e84263d55307 538 */
AnnaBridge 167:e84263d55307 539
AnnaBridge 167:e84263d55307 540 int flash_set_extend_addr(flash_t *obj, uint32_t data)
AnnaBridge 167:e84263d55307 541 {
AnnaBridge 167:e84263d55307 542 flash_turnon();
AnnaBridge 167:e84263d55307 543
AnnaBridge 167:e84263d55307 544 if(isinit == 0)
AnnaBridge 167:e84263d55307 545 flash_init(&flashobj);
AnnaBridge 167:e84263d55307 546
AnnaBridge 167:e84263d55307 547 SpicSetExtendAddrRtl8195A(data, flashobj.SpicInitPara);
AnnaBridge 167:e84263d55307 548 SpicWaitWipDoneRefinedRtl8195A(flashobj.SpicInitPara);
AnnaBridge 167:e84263d55307 549 DBG_8195A("Extended Address Register After Setting= %x\n", flash_get_extend_addr(&flashobj));
AnnaBridge 167:e84263d55307 550 SpicDisableRtl8195A();
AnnaBridge 167:e84263d55307 551 return 1;
AnnaBridge 167:e84263d55307 552 }
AnnaBridge 167:e84263d55307 553
AnnaBridge 167:e84263d55307 554 int flash_get_extend_addr(flash_t *obj)
AnnaBridge 167:e84263d55307 555 {
AnnaBridge 167:e84263d55307 556 u8 Status = 0;
AnnaBridge 167:e84263d55307 557
AnnaBridge 167:e84263d55307 558 flash_turnon();
AnnaBridge 167:e84263d55307 559 if(isinit == 0)
AnnaBridge 167:e84263d55307 560 flash_init(&flashobj);
AnnaBridge 167:e84263d55307 561 Status = SpicGetExtendAddrRtl8195A(flashobj.SpicInitPara);
AnnaBridge 167:e84263d55307 562
AnnaBridge 167:e84263d55307 563 SpicDisableRtl8195A();
AnnaBridge 167:e84263d55307 564 return Status;
AnnaBridge 167:e84263d55307 565 }
AnnaBridge 167:e84263d55307 566
AnnaBridge 167:e84263d55307 567