1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
| #include "fsl_debug_console.h"
#include "fsl_flexspi.h" #include "fsl_debug_console.h" #include "fsl_cache.h"
#include "pin_mux.h" #include "board.h" #include "clock_config.h" #include "fsl_common.h"
#include "FreeRTOS.h" #include "task.h"
#include "flexspi_nor_flash_ops.h"
#define NOR_CMD_LUT_SEQ_IDX_READID1 10 #define NOR_CMD_LUT_SEQ_IDX_READSTATUS1 11 #define NOR_CMD_LUT_SEQ_IDX_WRITEENABLE1 12 #define NOR_CMD_LUT_SEQ_IDX_ERASESECTOR1 13 #define NOR_CMD_LUT_SEQ_IDX_PAGEPROGRAM_SINGLE1 14 #define NOR_CMD_LUT_SEQ_IDX_READ_NORMAL1 15
#define FLASH_PAGE_SIZE 256
static const uint32_t customLUT[64] = { [4 * NOR_CMD_LUT_SEQ_IDX_READID1] = FLEXSPI_LUT_SEQ(kFLEXSPI_Command_SDR, kFLEXSPI_1PAD, 0xAB, kFLEXSPI_Command_DUMMY_SDR, kFLEXSPI_1PAD, 0x18), [4 * NOR_CMD_LUT_SEQ_IDX_READID1 + 1] = FLEXSPI_LUT_SEQ(kFLEXSPI_Command_READ_SDR, kFLEXSPI_1PAD, 0x04, kFLEXSPI_Command_STOP, kFLEXSPI_1PAD, 0), [4 * NOR_CMD_LUT_SEQ_IDX_READSTATUS1] = FLEXSPI_LUT_SEQ(kFLEXSPI_Command_SDR, kFLEXSPI_1PAD, 0x05, kFLEXSPI_Command_READ_SDR, kFLEXSPI_1PAD, 0x04), [4 * NOR_CMD_LUT_SEQ_IDX_WRITEENABLE1] = FLEXSPI_LUT_SEQ(kFLEXSPI_Command_SDR, kFLEXSPI_1PAD, 0x06, kFLEXSPI_Command_STOP, kFLEXSPI_1PAD, 0), [4 * NOR_CMD_LUT_SEQ_IDX_ERASESECTOR1] = FLEXSPI_LUT_SEQ(kFLEXSPI_Command_SDR, kFLEXSPI_1PAD, 0x20, kFLEXSPI_Command_RADDR_SDR, kFLEXSPI_1PAD, 0x18), [4 * NOR_CMD_LUT_SEQ_IDX_PAGEPROGRAM_SINGLE1] = FLEXSPI_LUT_SEQ(kFLEXSPI_Command_SDR, kFLEXSPI_1PAD, 0x02, kFLEXSPI_Command_RADDR_SDR, kFLEXSPI_1PAD, 0x18), [4 * NOR_CMD_LUT_SEQ_IDX_PAGEPROGRAM_SINGLE1 + 1] = FLEXSPI_LUT_SEQ(kFLEXSPI_Command_WRITE_SDR, kFLEXSPI_1PAD, 0x04, kFLEXSPI_Command_STOP, kFLEXSPI_1PAD, 0), [4 * NOR_CMD_LUT_SEQ_IDX_READ_NORMAL1] = FLEXSPI_LUT_SEQ(kFLEXSPI_Command_SDR, kFLEXSPI_1PAD, 0x03, kFLEXSPI_Command_RADDR_SDR, kFLEXSPI_1PAD, 0x18), [4 * NOR_CMD_LUT_SEQ_IDX_READ_NORMAL1 + 1] = FLEXSPI_LUT_SEQ(kFLEXSPI_Command_READ_SDR, kFLEXSPI_1PAD, 0x04, kFLEXSPI_Command_STOP, kFLEXSPI_1PAD, 0), };
uint8_t __attribute__((section("NonCacheable"))) qspi_erase_sector(FLEXSPI_Type *base, uint32_t address) {
status_t status;
taskENTER_CRITICAL(); SCB_InvalidateDCache(); status = flexspi_nor_flash_erase_sector(base, address); taskEXIT_CRITICAL();
if (status != kStatus_Success) { PRINTF("Erase sector failure !\r\n"); return 0; } return 1; }
uint8_t __attribute__((section("NonCacheable"))) qspi_page_program(FLEXSPI_Type *base, uint32_t dstAddr, uint32_t *src, size_t length) { status_t status;
taskENTER_CRITICAL(); SCB_InvalidateDCache(); status = flexspi_nor_flash_page_program(base, dstAddr, (void *)src, length); taskEXIT_CRITICAL(); if (status != kStatus_Success) { PRINTF("Page program failure !\r\n"); return 0; } return 1; }
uint8_t Qspi_Read_Sector(FLEXSPI_Type *base, uint32_t address, uint8_t *read_buf, size_t leng) {
memcpy(read_buf, (void *)(FlexSPI_AMBA_BASE + (address)), leng); return 1; }
uint8_t Qspi_Write_NoCheck(FLEXSPI_Type *base, uint8_t *pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite) { uint16_t pageremain; pageremain = FLASH_PAGE_SIZE - WriteAddr % FLASH_PAGE_SIZE; if (NumByteToWrite <= pageremain) { pageremain = NumByteToWrite; }
while (1) { qspi_page_program(base, WriteAddr, (void *)pBuffer, pageremain); if (NumByteToWrite == pageremain) break; else { pBuffer += pageremain; WriteAddr += pageremain;
NumByteToWrite -= pageremain; if (NumByteToWrite > FLASH_PAGE_SIZE) pageremain = FLASH_PAGE_SIZE; else pageremain = NumByteToWrite; } }
return 1; }
void SysFlashOpen(void) { status_t status; uint8_t vendorID = 0;
FLEXSPI_UpdateLUT(FLEXSPI, NOR_CMD_LUT_SEQ_IDX_READID1*4, &customLUT[NOR_CMD_LUT_SEQ_IDX_READID1*4], 64);
taskENTER_CRITICAL(); SCB_InvalidateDCache(); status = flexspi_nor_get_vendor_id(FLEXSPI, &vendorID); taskEXIT_CRITICAL(); if (status != kStatus_Success) { return; } PRINTF("QSPI Flash Vendor ID: 0x%x\r\n", vendorID); } static unsigned char buff[0x1000] = {0};
void SysFlashErase(unsigned int addr, unsigned int length) { unsigned int endaddr = addr + length;
if(length >= 0x1000) { if(addr % 0x1000 != 0) { Qspi_Read_Sector(FLEXSPI, addr&0xfffff000, (uint8_t *)buff, addr%0x1000); qspi_erase_sector(FLEXSPI, addr&0xfffff000); Qspi_Write_NoCheck(FLEXSPI, (uint8_t *)buff, addr&0xfffff000, addr%0x1000); addr = (addr&0xfffff000) + 0x1000; }
while(addr < (endaddr&0xfffff000)) { qspi_erase_sector(FLEXSPI, addr); addr += 0x1000; }
if(endaddr % 0x1000 != 0) { Qspi_Read_Sector(FLEXSPI, endaddr, (uint8_t *)buff, 0x1000-(endaddr%0x1000)); qspi_erase_sector(FLEXSPI, endaddr&0xfffff000); Qspi_Write_NoCheck(FLEXSPI, (uint8_t *)buff, endaddr, 0x1000-(endaddr%0x1000)); } }else { Qspi_Read_Sector(FLEXSPI, addr&0xfffff000, (uint8_t *)buff, addr%0x1000); Qspi_Read_Sector(FLEXSPI, endaddr, (uint8_t *)&buff[endaddr%0x1000], 0x1000-(endaddr%0x1000)); qspi_erase_sector(FLEXSPI, addr&0xfffff000); Qspi_Write_NoCheck(FLEXSPI, (uint8_t *)buff, addr&0xfffff000, addr%0x1000); Qspi_Write_NoCheck(FLEXSPI, (uint8_t *)&buff[endaddr%0x1000], endaddr, 0x1000-(endaddr%0x1000)); } }
void SysFlashRead(unsigned char* buffer, unsigned int len, unsigned int offset) { Qspi_Read_Sector(FLEXSPI, offset, buffer, len); }
void SysFlashWrite(unsigned char* buffer, unsigned int len, unsigned int offset) { Qspi_Write_NoCheck(FLEXSPI, buffer, offset, len); }
|