|
| 1 | +/* |
| 2 | + * Copyright (c) 2006-2023, RT-Thread Development Team |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Change Logs: |
| 7 | + * Date Author Notes |
| 8 | + * 2025-05-20 Chasel first version |
| 9 | + * |
| 10 | + */ |
| 11 | + |
| 12 | +#include <rtconfig.h> |
| 13 | +#include <rtdef.h> |
| 14 | + |
| 15 | +#ifdef BSP_USING_ON_CHIP_FLASH |
| 16 | +#include "drv_flash.h" |
| 17 | +#include <board.h> |
| 18 | + |
| 19 | +#if defined(RT_USING_FAL) |
| 20 | +#include "fal.h" |
| 21 | +#endif |
| 22 | + |
| 23 | +//#define DRV_DEBUG |
| 24 | +#define LOG_TAG "drv.flash" |
| 25 | +#include <drv_log.h> |
| 26 | + |
| 27 | +#define FLASH_PAGE_SIZE 4096 |
| 28 | + |
| 29 | +/** |
| 30 | + * @brief Gets the page of a given address |
| 31 | + * @param Addr: Address of the FLASH Memory |
| 32 | + * @retval The page of a given address |
| 33 | + */ |
| 34 | +static uint32_t GetPage(uint32_t addr) |
| 35 | +{ |
| 36 | + uint32_t page = 0; |
| 37 | + page = RT_ALIGN_DOWN(addr, FLASH_PAGE_SIZE); |
| 38 | + return page; |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Read data from flash. |
| 43 | + * @note This operation's units is word. |
| 44 | + * |
| 45 | + * @param addr flash address |
| 46 | + * @param buf buffer to store read data |
| 47 | + * @param size read bytes size |
| 48 | + * |
| 49 | + * @return result |
| 50 | + */ |
| 51 | +int ch32_flash_read(rt_uint32_t addr, rt_uint8_t *buf, size_t size) |
| 52 | +{ |
| 53 | + size_t i; |
| 54 | + |
| 55 | + if ((addr + size) > CH32_FLASH_END_ADDRESS) |
| 56 | + { |
| 57 | + LOG_E("read outrange flash size! addr is (0x%p)", (void *)(addr + size)); |
| 58 | + return -RT_EINVAL; |
| 59 | + } |
| 60 | + |
| 61 | + for (i = 0; i < size; i++, buf++, addr++) |
| 62 | + { |
| 63 | + *buf = *(rt_uint8_t *) addr; |
| 64 | + } |
| 65 | + |
| 66 | + return size; |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Write data to flash. |
| 71 | + * @note This operation's units is word. |
| 72 | + * @note This operation must after erase. @see flash_erase. |
| 73 | + * |
| 74 | + * @param addr flash address |
| 75 | + * @param buf the write data buffer |
| 76 | + * @param size write bytes size |
| 77 | + * |
| 78 | + * @return result |
| 79 | + */ |
| 80 | +int ch32_flash_write(rt_uint32_t addr, const rt_uint8_t *buf, size_t size) |
| 81 | +{ |
| 82 | + rt_err_t result = RT_EOK; |
| 83 | + FLASH_Status status = 0; |
| 84 | + rt_uint32_t end_addr = addr + size; |
| 85 | + |
| 86 | + if (addr % 4 != 0) |
| 87 | + { |
| 88 | + LOG_E("write addr must be 4-byte alignment"); |
| 89 | + return -RT_EINVAL; |
| 90 | + } |
| 91 | + |
| 92 | + if ((end_addr) > CH32_FLASH_END_ADDRESS) |
| 93 | + { |
| 94 | + LOG_E("write outrange flash size! addr is (0x%p)", (void *)(addr + size)); |
| 95 | + return -RT_EINVAL; |
| 96 | + } |
| 97 | + |
| 98 | + FLASH_Unlock(); |
| 99 | + FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_WRPRTERR); |
| 100 | + |
| 101 | + while (addr < end_addr) |
| 102 | + { |
| 103 | + status = FLASH_ProgramWord(addr, *((rt_uint32_t *)buf)); |
| 104 | + if (status == FLASH_COMPLETE) |
| 105 | + { |
| 106 | + if (*(rt_uint32_t *)addr != *(rt_uint32_t *)buf) |
| 107 | + { |
| 108 | + result = -RT_ERROR; |
| 109 | + break; |
| 110 | + } |
| 111 | + addr += 4; |
| 112 | + buf += 4; |
| 113 | + } |
| 114 | + else |
| 115 | + { |
| 116 | + result = -RT_ERROR; |
| 117 | + break; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + FLASH_Lock(); |
| 122 | + |
| 123 | + if (result != RT_EOK) |
| 124 | + { |
| 125 | + return result; |
| 126 | + } |
| 127 | + |
| 128 | + return size; |
| 129 | +} |
| 130 | + |
| 131 | +/** |
| 132 | + * Erase data on flash . |
| 133 | + * @note This operation is irreversible. |
| 134 | + * @note This operation's units is different which on many chips. |
| 135 | + * |
| 136 | + * @param addr flash address |
| 137 | + * @param size erase bytes size |
| 138 | + * |
| 139 | + * @return result |
| 140 | + */ |
| 141 | +int ch32_flash_erase(rt_uint32_t addr, size_t size) |
| 142 | +{ |
| 143 | + rt_err_t result = RT_EOK; |
| 144 | + FLASH_Status status = 0; |
| 145 | + uint32_t num_page = 0; |
| 146 | + uint32_t i = 0; |
| 147 | + |
| 148 | + if ((addr + size) > CH32_FLASH_END_ADDRESS) |
| 149 | + { |
| 150 | + LOG_E("ERROR: erase outrange flash size! addr is (0x%p)\n", (void *)(addr + size)); |
| 151 | + return -RT_EINVAL; |
| 152 | + } |
| 153 | + |
| 154 | + FLASH_Unlock(); |
| 155 | + |
| 156 | + num_page = (size + FLASH_PAGE_SIZE - 1) / FLASH_PAGE_SIZE; |
| 157 | + |
| 158 | + FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_WRPRTERR); |
| 159 | + |
| 160 | + for(i = 0; (i < num_page) && (status == FLASH_COMPLETE); i++) |
| 161 | + { |
| 162 | + status = FLASH_ErasePage(GetPage(addr + i * FLASH_PAGE_SIZE)); //Erase 4KB |
| 163 | + |
| 164 | + if(status != FLASH_COMPLETE) |
| 165 | + { |
| 166 | + LOG_E("FLASH Erase Fail\r\n"); |
| 167 | + result = -RT_ERROR; |
| 168 | + goto __exit; |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | +__exit: |
| 173 | + FLASH_Lock(); |
| 174 | + |
| 175 | + if (result != RT_EOK) |
| 176 | + { |
| 177 | + return result; |
| 178 | + } |
| 179 | + |
| 180 | + return size; |
| 181 | +} |
| 182 | + |
| 183 | + |
| 184 | +#if defined(RT_USING_FAL) |
| 185 | + |
| 186 | +static int fal_flash_read(long offset, rt_uint8_t *buf, size_t size); |
| 187 | +static int fal_flash_write(long offset, const rt_uint8_t *buf, size_t size); |
| 188 | +static int fal_flash_erase(long offset, size_t size); |
| 189 | + |
| 190 | +const struct fal_flash_dev ch32_onchip_flash = { "onchip_flash", CH32_FLASH_START_ADRESS, CH32_FLASH_SIZE, FLASH_PAGE_SIZE, {NULL, fal_flash_read, fal_flash_write, fal_flash_erase}, 8, {} ,}; |
| 191 | + |
| 192 | +static int fal_flash_read(long offset, rt_uint8_t *buf, size_t size) |
| 193 | +{ |
| 194 | + return ch32_flash_read(ch32_onchip_flash.addr + offset, buf, size); |
| 195 | +} |
| 196 | + |
| 197 | +static int fal_flash_write(long offset, const rt_uint8_t *buf, size_t size) |
| 198 | +{ |
| 199 | + return ch32_flash_write(ch32_onchip_flash.addr + offset, buf, size); |
| 200 | +} |
| 201 | + |
| 202 | +static int fal_flash_erase(long offset, size_t size) |
| 203 | +{ |
| 204 | + return ch32_flash_erase(ch32_onchip_flash.addr + offset, size); |
| 205 | +} |
| 206 | + |
| 207 | +#endif |
| 208 | +#endif /* BSP_USING_ON_CHIP_FLASH */ |
0 commit comments