|
| 1 | +/* |
| 2 | + * Copyright (c) 2006-2018, RT-Thread Development Team |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Change Logs: |
| 7 | + * Date Author Notes |
| 8 | + * 2020-06-27 NU-LL first version |
| 9 | + */ |
| 10 | + |
| 11 | +#include "board.h" |
| 12 | + |
| 13 | +#ifdef BSP_USING_ON_CHIP_FLASH |
| 14 | +#include "drv_config.h" |
| 15 | +#include "drv_flash.h" |
| 16 | + |
| 17 | +#if defined(PKG_USING_FAL) |
| 18 | +#include "fal.h" |
| 19 | +#endif |
| 20 | + |
| 21 | +// #define DRV_DEBUG |
| 22 | +#define LOG_TAG "drv.flash" |
| 23 | +#include <drv_log.h> |
| 24 | + |
| 25 | +/** |
| 26 | + * @brief Gets the page of a given address |
| 27 | + * @param Addr: Address of the FLASH Memory |
| 28 | + * @retval The page of a given address |
| 29 | + */ |
| 30 | +static uint32_t GetPage(uint32_t addr) |
| 31 | +{ |
| 32 | + uint32_t page = 0; |
| 33 | + page = RT_ALIGN_DOWN(addr-STM32_FLASH_START_ADRESS, FLASH_PAGE_SIZE)/FLASH_PAGE_SIZE; |
| 34 | + return page; |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Read data from flash. |
| 39 | + * @note This operation's units is word. |
| 40 | + * |
| 41 | + * @param addr flash address |
| 42 | + * @param buf buffer to store read data |
| 43 | + * @param size read bytes size |
| 44 | + * |
| 45 | + * @return result |
| 46 | + */ |
| 47 | +int stm32_flash_read(rt_uint32_t addr, rt_uint8_t *buf, size_t size) |
| 48 | +{ |
| 49 | + size_t i; |
| 50 | + |
| 51 | + if ((addr + size) > STM32_FLASH_END_ADDRESS) |
| 52 | + { |
| 53 | + LOG_E("read outrange flash size! addr is (0x%p)", (void *)(addr + size)); |
| 54 | + return -RT_EINVAL; |
| 55 | + } |
| 56 | + |
| 57 | + for (i = 0; i < size; i++, buf++, addr++) |
| 58 | + { |
| 59 | + *buf = *(rt_uint8_t *) addr; |
| 60 | + } |
| 61 | + |
| 62 | + return size; |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Write data to flash. |
| 67 | + * @note This operation's units is word. |
| 68 | + * @note This operation must after erase. @see flash_erase. |
| 69 | + * |
| 70 | + * @param addr flash address |
| 71 | + * @param buf the write data buffer |
| 72 | + * @param size write bytes size |
| 73 | + * |
| 74 | + * @return result |
| 75 | + */ |
| 76 | +int stm32_flash_write(rt_uint32_t addr, const uint8_t *buf, size_t size) |
| 77 | +{ |
| 78 | + size_t i, j; |
| 79 | + rt_err_t result = 0; |
| 80 | + rt_uint64_t write_data = 0, temp_data = 0; |
| 81 | + |
| 82 | + if ((addr + size) > STM32_FLASH_END_ADDRESS) |
| 83 | + { |
| 84 | + LOG_E("ERROR: write outrange flash size! addr is (0x%p)\n", (void*)(addr + size)); |
| 85 | + return -RT_EINVAL; |
| 86 | + } |
| 87 | + |
| 88 | + if(addr % 8 != 0) |
| 89 | + { |
| 90 | + LOG_E("write addr must be 8-byte alignment"); |
| 91 | + return -RT_EINVAL; |
| 92 | + } |
| 93 | + |
| 94 | + HAL_FLASH_Unlock(); |
| 95 | + |
| 96 | + __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGSERR); |
| 97 | + |
| 98 | + if (size < 1) |
| 99 | + { |
| 100 | + return -RT_ERROR; |
| 101 | + } |
| 102 | + |
| 103 | + for (i = 0; i < size;) |
| 104 | + { |
| 105 | + if ((size - i) < 8) |
| 106 | + { |
| 107 | + for (j = 0; (size - i) > 0; i++, j++) |
| 108 | + { |
| 109 | + temp_data = *buf; |
| 110 | + write_data = (write_data) | (temp_data << 8 * j); |
| 111 | + buf ++; |
| 112 | + } |
| 113 | + } |
| 114 | + else |
| 115 | + { |
| 116 | + for (j = 0; j < 8; j++, i++) |
| 117 | + { |
| 118 | + temp_data = *buf; |
| 119 | + write_data = (write_data) | (temp_data << 8 * j); |
| 120 | + buf ++; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /* write data */ |
| 125 | + if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, addr, write_data) == HAL_OK) |
| 126 | + { |
| 127 | + /* Check the written value */ |
| 128 | + if (*(uint64_t*)addr != write_data) |
| 129 | + { |
| 130 | + LOG_E("ERROR: write data != read data\n"); |
| 131 | + result = -RT_ERROR; |
| 132 | + goto __exit; |
| 133 | + } |
| 134 | + } |
| 135 | + else |
| 136 | + { |
| 137 | + result = -RT_ERROR; |
| 138 | + goto __exit; |
| 139 | + } |
| 140 | + |
| 141 | + temp_data = 0; |
| 142 | + write_data = 0; |
| 143 | + |
| 144 | + addr += 8; |
| 145 | + } |
| 146 | + |
| 147 | +__exit: |
| 148 | + HAL_FLASH_Lock(); |
| 149 | + if (result != 0) |
| 150 | + { |
| 151 | + return result; |
| 152 | + } |
| 153 | + |
| 154 | + return size; |
| 155 | +} |
| 156 | + |
| 157 | +/** |
| 158 | + * Erase data on flash. |
| 159 | + * @note This operation is irreversible. |
| 160 | + * @note This operation's units is different which on many chips. |
| 161 | + * |
| 162 | + * @param addr flash address |
| 163 | + * @param size erase bytes size |
| 164 | + * |
| 165 | + * @return result |
| 166 | + */ |
| 167 | +int stm32_flash_erase(rt_uint32_t addr, size_t size) |
| 168 | +{ |
| 169 | + rt_err_t result = RT_EOK; |
| 170 | + uint32_t PAGEError = 0; |
| 171 | + |
| 172 | + /*Variable used for Erase procedure*/ |
| 173 | + FLASH_EraseInitTypeDef EraseInitStruct; |
| 174 | + |
| 175 | + if ((addr + size) > STM32_FLASH_END_ADDRESS) |
| 176 | + { |
| 177 | + LOG_E("ERROR: erase outrange flash size! addr is (0x%p)\n", (void *)(addr + size)); |
| 178 | + return -RT_EINVAL; |
| 179 | + } |
| 180 | + |
| 181 | + HAL_FLASH_Unlock(); |
| 182 | + |
| 183 | + /* Fill EraseInit structure*/ |
| 184 | + EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES; |
| 185 | + EraseInitStruct.Page = GetPage(addr); |
| 186 | + EraseInitStruct.NbPages = (size + FLASH_PAGE_SIZE - 1) / FLASH_PAGE_SIZE; |
| 187 | + |
| 188 | + if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK) |
| 189 | + { |
| 190 | + result = -RT_ERROR; |
| 191 | + goto __exit; |
| 192 | + } |
| 193 | + |
| 194 | +__exit: |
| 195 | + HAL_FLASH_Lock(); |
| 196 | + |
| 197 | + if (result != RT_EOK) |
| 198 | + { |
| 199 | + return result; |
| 200 | + } |
| 201 | + |
| 202 | + LOG_D("erase done: addr (0x%p), size %d", (void *)addr, size); |
| 203 | + return size; |
| 204 | +} |
| 205 | + |
| 206 | +#if defined(PKG_USING_FAL) |
| 207 | + |
| 208 | +static int fal_flash_read(long offset, rt_uint8_t *buf, size_t size); |
| 209 | +static int fal_flash_write(long offset, const rt_uint8_t *buf, size_t size); |
| 210 | +static int fal_flash_erase(long offset, size_t size); |
| 211 | + |
| 212 | +const struct fal_flash_dev stm32_onchip_flash = { "onchip_flash", STM32_FLASH_START_ADRESS, STM32_FLASH_SIZE, FLASH_PAGE_SIZE, {NULL, fal_flash_read, fal_flash_write, fal_flash_erase} }; |
| 213 | + |
| 214 | +static int fal_flash_read(long offset, rt_uint8_t *buf, size_t size) |
| 215 | +{ |
| 216 | + return stm32_flash_read(stm32_onchip_flash.addr + offset, buf, size); |
| 217 | +} |
| 218 | + |
| 219 | +static int fal_flash_write(long offset, const rt_uint8_t *buf, size_t size) |
| 220 | +{ |
| 221 | + return stm32_flash_write(stm32_onchip_flash.addr + offset, buf, size); |
| 222 | +} |
| 223 | + |
| 224 | +static int fal_flash_erase(long offset, size_t size) |
| 225 | +{ |
| 226 | + return stm32_flash_erase(stm32_onchip_flash.addr + offset, size); |
| 227 | +} |
| 228 | + |
| 229 | +#endif |
| 230 | +#endif /* BSP_USING_ON_CHIP_FLASH */ |
0 commit comments