|
| 1 | +#include "flash.h" |
| 2 | + |
| 3 | +#define BLOCK_SIZE 512 |
| 4 | + |
| 5 | +typedef struct { |
| 6 | + uint32_t Size; |
| 7 | + uint32_t Address; |
| 8 | +} SectorInfo_t; |
| 9 | + |
| 10 | +typedef struct { |
| 11 | + uint16_t AlgoVer; |
| 12 | + uint8_t Name[128]; |
| 13 | + uint16_t Type; |
| 14 | + uint32_t BaseAddr; |
| 15 | + uint32_t TotalSize; |
| 16 | + uint32_t PageSize; |
| 17 | + uint32_t Reserved; |
| 18 | + uint8_t ErasedVal; |
| 19 | + uint8_t Padding[3]; |
| 20 | + uint32_t TimeoutProg; |
| 21 | + uint32_t TimeoutErase; |
| 22 | + SectorInfo_t SectorInfo[2]; |
| 23 | +} FlashDevice_t; |
| 24 | + |
| 25 | +extern const FlashDevice_t FlashDevice __attribute__((section(".constdata"))); |
| 26 | + |
| 27 | +const FlashDevice_t FlashDevice = { |
| 28 | + 0x0101, |
| 29 | + "DP32G030 Internal Flash", |
| 30 | + 1, |
| 31 | + 0x00000000, |
| 32 | + 0x0000F000, |
| 33 | + 0x00000200, |
| 34 | + 0x00000000, |
| 35 | + 0xFF, |
| 36 | + { 0x00, 0x00, 0x00}, |
| 37 | + 500, |
| 38 | + 500, |
| 39 | + { |
| 40 | + { 0x00000200, 0x00000000 }, |
| 41 | + { 0xFFFFFFFF, 0xFFFFFFFF }, |
| 42 | + } |
| 43 | +}; |
| 44 | + |
| 45 | +static void WaitNotBusy(void) |
| 46 | +{ |
| 47 | + while (1) { |
| 48 | + if ((FLASH_ST & FLASH_ST_BUSY_MASK) == FLASH_ST_BUSY_BITS_READY) { |
| 49 | + return; |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +static void WaitForEmpty(void) |
| 55 | +{ |
| 56 | + while (1) { |
| 57 | + if ((FLASH_ST & FLASH_ST_PROG_BUF_EMPTY_MASK) == FLASH_ST_PROG_BUF_EMPTY_BITS_EMPTY) { |
| 58 | + return; |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +static void FlashLock(void) |
| 64 | +{ |
| 65 | + FLASH_LOCK = FLASH_LOCK_LOCK_BITS_LOCK; |
| 66 | +} |
| 67 | + |
| 68 | +static void FlashUnlock(void) |
| 69 | +{ |
| 70 | + FLASH_UNLOCK = FLASH_UNLOCK_UNLOCK_BITS_UNLOCK; |
| 71 | +} |
| 72 | + |
| 73 | +static void FlashStart(void) |
| 74 | +{ |
| 75 | + FlashUnlock(); |
| 76 | + |
| 77 | + FLASH_START = FLASH_START_START_BITS_START; |
| 78 | +} |
| 79 | + |
| 80 | +static void FlashStop(void) |
| 81 | +{ |
| 82 | + FLASH_CFG = (FLASH_CFG & ~FLASH_CFG_MODE_MASK) | FLASH_CFG_MODE_VALUE_READ_AHB; |
| 83 | +} |
| 84 | + |
| 85 | +static void FlashHalfSector(uint32_t Address, const uint8_t *pBuffer, uint32_t WordCount) |
| 86 | +{ |
| 87 | + const uint32_t *pWord = (const uint32_t *)pBuffer; |
| 88 | + uint32_t i; |
| 89 | + |
| 90 | + WaitNotBusy(); |
| 91 | + |
| 92 | + FLASH_CFG = (FLASH_CFG & ~FLASH_CFG_MODE_MASK) | FLASH_CFG_MODE_BITS_PROGRAM; |
| 93 | + FLASH_ADDR = Address >> 2; |
| 94 | + FLASH_WDATA = *pWord++; |
| 95 | + |
| 96 | + FlashStart(); |
| 97 | + |
| 98 | + for (i = 1; i < WordCount; i++) { |
| 99 | + WaitForEmpty(); |
| 100 | + FLASH_WDATA = *pWord++; |
| 101 | + } |
| 102 | + |
| 103 | + WaitNotBusy(); |
| 104 | + |
| 105 | + FlashStop(); |
| 106 | + FlashLock(); |
| 107 | +} |
| 108 | + |
| 109 | +// -------- |
| 110 | + |
| 111 | +int Init(uint32_t Addr, uint32_t Freq, uint32_t Func) __attribute__((section("PrgCode"))); |
| 112 | +int UnInit(uint32_t Func) __attribute__((section("PrgCode"))); |
| 113 | +int EraseChip(void) __attribute__((section("PrgCode"))); |
| 114 | +int EraseSector(uint32_t SectorAddr) __attribute__((section("PrgCode"))); |
| 115 | +int ProgramPage(uint32_t DestAddr, uint32_t NumBytes, const uint8_t *pSrcBuff) __attribute__((section("PrgCode"))); |
| 116 | +int CheckBlank(uint32_t Addr, uint32_t NumBytes, uint8_t BlankValue) __attribute__((section("PrgCode"))); |
| 117 | + |
| 118 | +int Init(uint32_t Addr, uint32_t Freq, uint32_t Func) |
| 119 | +{ |
| 120 | + if (FLASH_MASK != 6) { |
| 121 | + FLASH_MASK = 0; |
| 122 | + WaitNotBusy(); |
| 123 | + FLASH_MASK = 6; |
| 124 | + WaitNotBusy(); |
| 125 | + if (FLASH_MASK != 6) { |
| 126 | + return 1; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + return 0; |
| 131 | +} |
| 132 | + |
| 133 | +int UnInit(uint32_t Func) |
| 134 | +{ |
| 135 | + // restore the MCU |
| 136 | + |
| 137 | + return 0; |
| 138 | +} |
| 139 | + |
| 140 | +int EraseChip(void) |
| 141 | +{ |
| 142 | + uint16_t i; |
| 143 | + |
| 144 | + for (i = 0; i < 0xF000; i += 512) { |
| 145 | + int ret = EraseSector(i); |
| 146 | + |
| 147 | + if (ret) { |
| 148 | + return ret; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return 0; |
| 153 | +} |
| 154 | + |
| 155 | +int EraseSector(uint32_t SectorAddr) |
| 156 | +{ |
| 157 | + |
| 158 | + if (SectorAddr % 512 || SectorAddr >= 0xF000) { |
| 159 | + return 1; |
| 160 | + } |
| 161 | + |
| 162 | + WaitNotBusy(); |
| 163 | + |
| 164 | + FLASH_CFG = (FLASH_CFG & ~FLASH_CFG_MODE_MASK) | FLASH_CFG_MODE_BITS_ERASE; |
| 165 | + |
| 166 | + FLASH_ADDR = SectorAddr >> 2; |
| 167 | + |
| 168 | + FlashStart(); |
| 169 | + WaitNotBusy(); |
| 170 | + FlashStop(); |
| 171 | + FlashLock(); |
| 172 | + |
| 173 | + return 0; |
| 174 | +} |
| 175 | + |
| 176 | + |
| 177 | +int ProgramPage(uint32_t DestAddr, uint32_t NumBytes, const uint8_t *pBuffer) |
| 178 | +{ |
| 179 | + if (DestAddr % BLOCK_SIZE || DestAddr >= 0xF000 || NumBytes % BLOCK_SIZE || NumBytes > 0xF000 || (DestAddr + NumBytes) > 0xF000) { |
| 180 | + return 1; |
| 181 | + } |
| 182 | + |
| 183 | + while (NumBytes >= BLOCK_SIZE) { |
| 184 | + FlashHalfSector(DestAddr + 0x0000, pBuffer + 0x0000, BLOCK_SIZE / 4); |
| 185 | + FlashHalfSector(DestAddr + 0x0100, pBuffer + 0x0100, BLOCK_SIZE / 4); |
| 186 | + NumBytes -= BLOCK_SIZE; |
| 187 | + pBuffer += BLOCK_SIZE; |
| 188 | + } |
| 189 | + |
| 190 | + FlashStop(); |
| 191 | + FlashLock(); |
| 192 | + |
| 193 | + return 0; |
| 194 | +} |
0 commit comments