drv_flash.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-11-30 flybreak first version
  9. */
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include "board.h"
  14. #include "hal_data.h"
  15. #include "drv_common.h"
  16. #if defined(RT_USING_FAL)
  17. #include "fal.h"
  18. #endif
  19. //#define DRV_DEBUG
  20. #define LOG_TAG "drv.flash"
  21. #ifdef DRV_DEBUG
  22. #define DBG_LVL DBG_LOG
  23. #else
  24. #define DBG_LVL DBG_INFO
  25. #endif /* DRV_DEBUG */
  26. #include <rtdbg.h>
  27. #if BSP_FEATURE_FLASH_HP_VERSION
  28. /* FLASH API */
  29. #define R_FLASH_Open R_FLASH_HP_Open
  30. #define R_FLASH_Reset R_FLASH_HP_Reset
  31. #define R_FLASH_Write R_FLASH_HP_Write
  32. #define R_FLASH_Erase R_FLASH_HP_Erase
  33. #define R_FLASH_StartUpAreaSelect R_FLASH_HP_StartUpAreaSelect
  34. /* BSP_FEATURE_FLASH */
  35. #define FLASH_CF_WRITE_SIZE BSP_FEATURE_FLASH_HP_CF_WRITE_SIZE
  36. #else /* FLASH LP */
  37. /* FLASH API */
  38. #define R_FLASH_Open R_FLASH_LP_Open
  39. #define R_FLASH_Reset R_FLASH_LP_Reset
  40. #define R_FLASH_Write R_FLASH_LP_Write
  41. #define R_FLASH_Erase R_FLASH_LP_Erase
  42. #define R_FLASH_StartUpAreaSelect R_FLASH_LP_StartUpAreaSelect
  43. /* BSP_FEATURE_FLASH */
  44. #define FLASH_CF_WRITE_SIZE BSP_FEATURE_FLASH_LP_CF_WRITE_SIZE
  45. #endif
  46. int _flash_init(void)
  47. {
  48. fsp_err_t err = FSP_SUCCESS;
  49. /* Open Flash_HP */
  50. err = R_FLASH_Open(&g_flash_ctrl, &g_flash_cfg);
  51. /* Handle Error */
  52. if (FSP_SUCCESS != err)
  53. {
  54. LOG_E("\r\n Flah_HP_Open API failed");
  55. }
  56. /* Setup Default Block 0 as Startup Setup Block */
  57. err = R_FLASH_StartUpAreaSelect(&g_flash_ctrl, FLASH_STARTUP_AREA_BLOCK0, true);
  58. if (err != FSP_SUCCESS)
  59. {
  60. LOG_E("\r\n Flah_HP_StartUpAreaSelect API failed");
  61. }
  62. return 0;
  63. }
  64. /**
  65. * Read data from flash.
  66. * @note This operation's units is word.
  67. *
  68. * @param addr flash address
  69. * @param buf buffer to store read data
  70. * @param size read bytes size
  71. *
  72. * @return result
  73. */
  74. int _flash_read(rt_uint32_t addr, rt_uint8_t *buf, size_t size)
  75. {
  76. size_t i;
  77. for (i = 0; i < size; i++, buf++, addr++)
  78. {
  79. *buf = *(rt_uint8_t *) addr;
  80. }
  81. return size;
  82. }
  83. /**
  84. * Write data to flash.
  85. * @note This operation's units is word.
  86. * @note This operation must after erase. @see flash_erase.
  87. *
  88. * @param addr flash address
  89. * @param buf the write data buffer
  90. * @param size write bytes size
  91. *
  92. * @return result
  93. */
  94. int _flash_write(rt_uint32_t addr, const rt_uint8_t *buf, size_t size)
  95. {
  96. rt_err_t result = RT_EOK;
  97. rt_base_t level;
  98. fsp_err_t err = FSP_SUCCESS;
  99. size_t written_size = 0;
  100. if (size % FLASH_CF_WRITE_SIZE)
  101. {
  102. LOG_E("Flash Write size must be an integer multiple of %d", FLASH_CF_WRITE_SIZE);
  103. }
  104. while (written_size < size)
  105. {
  106. level = rt_hw_interrupt_disable();
  107. R_FLASH_Reset(&g_flash_ctrl);
  108. /* Write code flash data*/
  109. err = R_FLASH_Write(&g_flash_ctrl, (uint32_t)(buf + written_size), addr + written_size, FLASH_CF_WRITE_SIZE);
  110. rt_hw_interrupt_enable(level);
  111. /* Error Handle */
  112. if (FSP_SUCCESS != err)
  113. {
  114. LOG_E("Write API failed");
  115. return -RT_EIO;
  116. }
  117. written_size += FLASH_CF_WRITE_SIZE;
  118. }
  119. if (result != RT_EOK)
  120. {
  121. return result;
  122. }
  123. return size;
  124. }
  125. typedef bool (*block_func)(int no, size_t addr, size_t size, void *parm1, void *parm2, void *parm3);
  126. void fal_block_iter(const struct fal_flash_dev *flash, block_func func, void *parm1, void *parm2, void *parm3)
  127. {
  128. int no = 0;
  129. size_t addr = flash->addr;
  130. for (int i = 0; i < FAL_DEV_BLK_MAX; i++)
  131. {
  132. /* blocks[i] */
  133. const struct flash_blk *cur_blk = &flash->blocks[i];
  134. if (cur_blk->size == 0 || cur_blk->count == 0)
  135. {
  136. break;
  137. }
  138. int j = cur_blk->count;
  139. while (j--)
  140. {
  141. /* block_no */
  142. if (func(no, addr, cur_blk->size, parm1, parm2, parm3) == true)
  143. {
  144. return;
  145. }
  146. addr += cur_blk->size;
  147. no ++;
  148. }
  149. }
  150. }
  151. bool calculate_block_num(int no, size_t addr, size_t size, void *erase_addr, void *erase_size, void *number)
  152. {
  153. rt_uint32_t e_addr = (rt_uint32_t)erase_addr;
  154. size_t e_size = (size_t)erase_size;
  155. int *i = (int *)number;
  156. LOG_D("block_num: no: %d, addr is (0x%p), size: %d\n, i:%d", no, addr, size, *i);
  157. LOG_D("erase_addr: (0x%p), erase_size: %d\n", e_addr, e_size);
  158. if (e_addr >= addr && e_addr < addr + size)
  159. {
  160. (*i) ++;
  161. return false;
  162. }
  163. else if (addr > e_addr && addr < e_addr + e_size)
  164. {
  165. (*i) ++;
  166. return false;
  167. }
  168. else if (addr >= e_addr + e_size)
  169. {
  170. return true;
  171. }
  172. return false;
  173. }
  174. /**
  175. * Erase data on flash.
  176. * @note This operation is irreversible.
  177. * @note This operation's units is different which on many chips.
  178. *
  179. * @param addr flash address
  180. * @param size erase bytes size
  181. *
  182. * @return result
  183. */
  184. #if BSP_FEATURE_FLASH_HP_VERSION
  185. int _flash_hp0_erase(rt_uint32_t addr, size_t size)
  186. #else
  187. int _flash_lp_erase(rt_uint32_t addr, size_t size)
  188. #endif
  189. {
  190. fsp_err_t err = FSP_SUCCESS;
  191. rt_base_t level;
  192. int block_num = 0;
  193. if ((addr + size) > BSP_FEATURE_FLASH_CODE_FLASH_START + BSP_ROM_SIZE_BYTES)
  194. {
  195. LOG_E("ERROR: erase outrange flash size! addr is (0x%p)\n", (void *)(addr + size));
  196. return -RT_EINVAL;
  197. }
  198. if (size < 1)
  199. {
  200. return -RT_EINVAL;
  201. }
  202. level = rt_hw_interrupt_disable();
  203. R_FLASH_Reset(&g_flash_ctrl);
  204. fal_block_iter(&_onchip_flash_hp0, &calculate_block_num, (void *)addr, (void *)size, &block_num);
  205. /* Erase Block */
  206. #if BSP_FEATURE_FLASH_HP_VERSION
  207. err = R_FLASH_Erase(&g_flash_ctrl,
  208. RT_ALIGN_DOWN(addr, BSP_FEATURE_FLASH_HP_CF_REGION0_BLOCK_SIZE),
  209. block_num);
  210. #else
  211. err = R_FLASH_Erase(&g_flash_ctrl,
  212. RT_ALIGN_DOWN(addr, BSP_FEATURE_FLASH_LP_CF_BLOCK_SIZE),
  213. ((size - 1) / BSP_FEATURE_FLASH_LP_CF_BLOCK_SIZE + 1));
  214. #endif
  215. rt_hw_interrupt_enable(level);
  216. if (err != FSP_SUCCESS)
  217. {
  218. LOG_E("Erase failed:addr (0x%p), size %d", (void *)addr, size);
  219. return -RT_EIO;
  220. }
  221. LOG_D("erase done: addr (0x%p), size %d", (void *)addr, size);
  222. return size;
  223. }
  224. #if defined(RT_USING_FAL)
  225. #if BSP_FEATURE_FLASH_HP_VERSION
  226. static int fal_flash_hp0_read(long offset, rt_uint8_t *buf, size_t size);
  227. static int fal_flash_hp0_write(long offset, const rt_uint8_t *buf, size_t size);
  228. static int fal_flash_hp0_erase(long offset, size_t size);
  229. const struct fal_flash_dev _onchip_flash_hp0 =
  230. {
  231. "onchip_flash_hp0",
  232. BSP_FEATURE_FLASH_CODE_FLASH_START,
  233. BSP_ROM_SIZE_BYTES,
  234. BSP_FEATURE_FLASH_HP_CF_REGION0_BLOCK_SIZE,
  235. {
  236. _flash_init,
  237. fal_flash_hp0_read,
  238. fal_flash_hp0_write,
  239. fal_flash_hp0_erase
  240. },
  241. (BSP_FEATURE_FLASH_HP_CF_WRITE_SIZE * 8)
  242. , {
  243. {
  244. .size = BSP_FEATURE_FLASH_HP_CF_REGION0_BLOCK_SIZE,
  245. .count = BSP_FEATURE_FLASH_HP_CF_REGION0_SIZE / BSP_FEATURE_FLASH_HP_CF_REGION0_BLOCK_SIZE
  246. },
  247. {
  248. .size = BSP_FEATURE_FLASH_HP_CF_REGION1_BLOCK_SIZE,
  249. .count = (BSP_ROM_SIZE_BYTES - BSP_FEATURE_FLASH_HP_CF_REGION0_SIZE) / BSP_FEATURE_FLASH_HP_CF_REGION1_BLOCK_SIZE
  250. },
  251. }
  252. };
  253. /* code flash region0 */
  254. static int fal_flash_hp0_read(long offset, rt_uint8_t *buf, size_t size)
  255. {
  256. return _flash_read(_onchip_flash_hp0.addr + offset, buf, size);
  257. }
  258. static int fal_flash_hp0_write(long offset, const rt_uint8_t *buf, size_t size)
  259. {
  260. return _flash_write(_onchip_flash_hp0.addr + offset, buf, size);
  261. }
  262. static int fal_flash_hp0_erase(long offset, size_t size)
  263. {
  264. return _flash_hp0_erase(_onchip_flash_hp0.addr + offset, size);
  265. }
  266. #else /* flash lp code flash */
  267. static int fal_flash_lp_read(long offset, rt_uint8_t *buf, size_t size);
  268. static int fal_flash_lp_write(long offset, const rt_uint8_t *buf, size_t size);
  269. static int fal_flash_lp_erase(long offset, size_t size);
  270. const struct fal_flash_dev _onchip_flash_lp =
  271. {
  272. "onchip_flash_lp",
  273. FLASH_START_ADDRESS,
  274. BSP_ROM_SIZE_BYTES,
  275. BSP_FEATURE_FLASH_LP_CF_BLOCK_SIZE,
  276. {
  277. _flash_init,
  278. fal_flash_lp_read,
  279. fal_flash_lp_write,
  280. fal_flash_lp_erase
  281. },
  282. (BSP_FEATURE_FLASH_LP_CF_WRITE_SIZE * 8)
  283. };
  284. static int fal_flash_lp_read(long offset, rt_uint8_t *buf, size_t size)
  285. {
  286. return _flash_read(_onchip_flash_lp.addr + offset, buf, size);
  287. }
  288. static int fal_flash_lp_write(long offset, const rt_uint8_t *buf, size_t size)
  289. {
  290. return _flash_write(_onchip_flash_lp.addr + offset, buf, size);
  291. }
  292. static int fal_flash_lp_erase(long offset, size_t size)
  293. {
  294. return _flash_lp_erase(_onchip_flash_lp.addr + offset, size);
  295. }
  296. #endif
  297. #endif